-
Notifications
You must be signed in to change notification settings - Fork 11
/
callbacks.go
362 lines (312 loc) · 11.8 KB
/
callbacks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package publish2
import (
"fmt"
"reflect"
"strings"
"time"
"github.com/jinzhu/gorm"
"github.com/qor/qor/utils"
)
const (
ModeOff = "off"
ModeReverse = "reverse"
VersionMode = "publish:version:mode"
VersionNameMode = "publish:version:name"
VersionMultipleMode = "multiple"
ScheduleMode = "publish:schedule:mode"
ComingOnlineMode = "coming_online"
GoingOfflineMode = "going_offline"
ScheduledTime = "publish:schedule:current"
ScheduledStart = "publish:schedule:start"
ScheduledEnd = "publish:schedule:end"
VisibleMode = "publish:visible:mode"
)
func IsSchedulableModel(model interface{}) (ok bool) {
if model != nil {
_, ok = reflect.New(utils.ModelType(model)).Interface().(ScheduledInterface)
}
return
}
func IsVersionableModel(model interface{}) (ok bool) {
if model != nil {
_, ok = reflect.New(utils.ModelType(model)).Interface().(VersionableInterface)
}
return
}
func IsShareableVersionModel(model interface{}) (ok bool) {
if model != nil {
_, ok = reflect.New(utils.ModelType(model)).Interface().(ShareableVersionInterface)
}
return
}
func IsPublishReadyableModel(model interface{}) (ok bool) {
if model != nil {
_, ok = reflect.New(utils.ModelType(model)).Interface().(PublishReadyInterface)
}
return
}
func RegisterCallbacks(db *gorm.DB) {
if db.Callback().Query().Get("publish:query") == nil {
db.Callback().Query().Before("gorm:query").Register("publish:query", queryCallback)
}
if db.Callback().Query().Get("publish:fix_preload") == nil {
db.Callback().Query().After("gorm:preload").Register("publish:fix_preload", fixPreloadCallback)
}
if db.Callback().RowQuery().Get("publish:query") == nil {
db.Callback().RowQuery().Before("gorm:row_query").Register("publish:query", queryCallback)
}
if db.Callback().Create().Get("publish:versions") == nil {
db.Callback().Create().Before("gorm:begin_transaction").Register("publish:versions", createCallback)
}
if db.Callback().Update().Get("publish:versions") == nil {
db.Callback().Update().Before("gorm:begin_transaction").Register("publish:versions", updateCallback)
}
if db.Callback().Delete().Get("publish:versions") == nil {
db.Callback().Delete().Before("gorm:begin_transaction").Register("publish:versions", deleteCallback)
}
}
func queryCallback(scope *gorm.Scope) {
var (
isSchedulable = IsSchedulableModel(scope.Value)
isVersionable = IsVersionableModel(scope.Value)
isShareableVersion = IsShareableVersionModel(scope.Value)
isPublishReadyable = IsPublishReadyableModel(scope.Value)
conditions []string
conditionValues []interface{}
)
if isSchedulable {
var (
scheduledStartTime, scheduledEndTime, scheduledCurrentTime *time.Time
mode, _ = scope.DB().Get(ScheduleMode)
comingOnlineMode = mode == ComingOnlineMode
goingOfflineMode = mode == GoingOfflineMode
modeON = (mode != ModeOff) && !comingOnlineMode && !goingOfflineMode
)
if v, ok := scope.Get(ScheduledStart); ok {
if t, ok := v.(*time.Time); ok {
scheduledStartTime = t
} else if t, ok := v.(time.Time); ok {
scheduledStartTime = &t
}
if scheduledStartTime != nil {
if comingOnlineMode {
conditions = append(conditions, "scheduled_start_at >= ?")
conditionValues = append(conditionValues, scheduledStartTime)
} else if goingOfflineMode {
conditions = append(conditions, "scheduled_end_at >= ?")
conditionValues = append(conditionValues, scheduledStartTime)
} else if modeON {
conditions = append(conditions, "(scheduled_end_at IS NULL OR scheduled_end_at >= ?)")
conditionValues = append(conditionValues, scheduledStartTime)
}
}
}
if v, ok := scope.Get(ScheduledEnd); ok {
if t, ok := v.(*time.Time); ok {
scheduledEndTime = t
} else if t, ok := v.(time.Time); ok {
scheduledEndTime = &t
}
if scheduledEndTime != nil {
if comingOnlineMode {
conditions = append(conditions, "scheduled_start_at <= ?")
conditionValues = append(conditionValues, scheduledEndTime)
} else if goingOfflineMode {
conditions = append(conditions, "scheduled_end_at <= ?")
conditionValues = append(conditionValues, scheduledEndTime)
} else if modeON {
conditions = append(conditions, "(scheduled_start_at IS NULL OR scheduled_start_at <= ?)")
conditionValues = append(conditionValues, scheduledEndTime)
}
}
}
if len(conditions) == 0 {
if v, ok := scope.Get(ScheduledTime); ok {
if t, ok := v.(*time.Time); ok {
scheduledCurrentTime = t
} else if t, ok := v.(time.Time); ok {
scheduledCurrentTime = &t
}
}
if scheduledCurrentTime == nil {
now := gorm.NowFunc()
scheduledCurrentTime = &now
}
if comingOnlineMode {
conditions = append(conditions, "scheduled_start_at >= ?")
conditionValues = append(conditionValues, scheduledCurrentTime)
} else if goingOfflineMode {
conditions = append(conditions, "scheduled_end_at >= ?")
conditionValues = append(conditionValues, scheduledCurrentTime)
} else if modeON {
conditions = append(conditions, "(scheduled_start_at IS NULL OR scheduled_start_at <= ?) AND (scheduled_end_at IS NULL OR scheduled_end_at >= ?)")
conditionValues = append(conditionValues, scheduledCurrentTime, scheduledCurrentTime)
}
}
}
if isPublishReadyable {
switch mode, _ := scope.DB().Get(VisibleMode); mode {
case ModeOff:
default:
conditions = append(conditions, "publish_ready = ?")
conditionValues = append(conditionValues, true)
}
}
if isVersionable {
switch mode, _ := scope.DB().Get(VersionMode); mode {
case VersionMultipleMode:
scope.Search.Where(strings.Join(conditions, " AND "), conditionValues...)
default:
if versionName, ok := scope.DB().Get(VersionNameMode); ok && versionName != "" {
scope.Search.Where("version_name = ?", versionName)
} else {
var sql string
var primaryKeys []string
if scope.HasColumn("DeletedAt") {
conditions = append(conditions, "deleted_at IS NULL")
}
for _, primaryField := range scope.PrimaryFields() {
if primaryField.DBName != "version_name" {
primaryKeys = append(primaryKeys, fmt.Sprintf("%v.%v", scope.TableName(), primaryField.DBName))
}
}
primaryKeyCondition := strings.Join(primaryKeys, ",")
if len(conditions) == 0 {
sql = fmt.Sprintf("(%v, %v.version_priority) IN (SELECT %v, MAX(%v.version_priority) FROM %v GROUP BY %v)", primaryKeyCondition, scope.QuotedTableName(), primaryKeyCondition, scope.QuotedTableName(), scope.QuotedTableName(), primaryKeyCondition)
} else {
sql = fmt.Sprintf("(%v, %v.version_priority) IN (SELECT %v, MAX(%v.version_priority) FROM %v WHERE %v GROUP BY %v)", primaryKeyCondition, scope.QuotedTableName(), primaryKeyCondition, scope.QuotedTableName(), scope.QuotedTableName(), strings.Join(conditions, " AND "), primaryKeyCondition)
}
scope.Search.Where(sql, conditionValues...)
}
}
quotedTableName := scope.QuotedTableName()
scope.Search.Order(fmt.Sprintf("%v.%v, %v.version_priority DESC", quotedTableName, scope.Quote(scope.PrimaryKey()), quotedTableName))
} else {
if isShareableVersion {
var versionName string
if source, ok := scope.DB().Get("gorm:association:source"); ok {
if versionable, ok := source.(VersionableInterface); ok {
versionName = versionable.GetVersionName()
}
}
if v, ok := scope.DB().Get(VersionNameMode); ok && fmt.Sprint(v) != "" {
versionName = fmt.Sprint(v)
}
if versionName != "" {
var primaryKeys []string
for _, primaryField := range scope.PrimaryFields() {
if primaryField.DBName != "version_name" {
primaryKeys = append(primaryKeys, scope.Quote(primaryField.DBName))
}
}
primaryKeyCondition := strings.Join(primaryKeys, ",")
scope.Search.Where(
fmt.Sprintf("version_name = ? OR (version_name = ? AND (%v) NOT IN (SELECT %v FROM %v WHERE version_name = ?))", primaryKeyCondition, primaryKeyCondition, scope.QuotedTableName()),
versionName, "", versionName,
)
}
}
scope.Search.Where(strings.Join(conditions, " AND "), conditionValues...)
}
}
func fixPreloadCallback(scope *gorm.Scope) {
filterFilterValuesWithVersion := func(gormField *gorm.Field, versionName string) {
indirectFieldValue := reflect.Indirect(gormField.Field)
switch indirectFieldValue.Kind() {
case reflect.Slice:
resultsMap := map[string]int{}
results := reflect.New(indirectFieldValue.Type()).Elem()
for i := 0; i < indirectFieldValue.Len(); i++ {
if shareableVersion, ok := indirectFieldValue.Index(i).Addr().Interface().(ShareableVersionInterface); ok {
fieldPrimaryValue := fmt.Sprint(scope.New(shareableVersion).PrimaryKeyValue())
idx, ok := resultsMap[fieldPrimaryValue]
if !ok && (shareableVersion.GetSharedVersionName() == versionName || shareableVersion.GetSharedVersionName() == "") {
resultsMap[fieldPrimaryValue] = results.Len()
results = reflect.Append(results, indirectFieldValue.Index(i))
} else if shareableVersion.GetSharedVersionName() == versionName {
results.Index(idx).Set(indirectFieldValue.Index(i))
}
}
}
gormField.Set(results)
case reflect.Struct:
if shareableVersion, ok := indirectFieldValue.Interface().(ShareableVersionInterface); ok {
if shareableVersion.GetSharedVersionName() != "" && shareableVersion.GetSharedVersionName() != versionName {
gormField.Set(reflect.New(indirectFieldValue.Type()))
}
}
}
}
fixSharedVersionRecords := func(value interface{}, fieldName string) {
reflectValue := reflect.Indirect(reflect.ValueOf(value))
switch reflectValue.Kind() {
case reflect.Slice:
for i := 0; i < reflectValue.Len(); i++ {
v := reflectValue.Index(i)
if versionable, ok := v.Addr().Interface().(VersionableInterface); ok {
if fieldValue, ok := scope.New(v.Addr().Interface()).FieldByName(fieldName); ok {
filterFilterValuesWithVersion(fieldValue, versionable.GetVersionName())
}
}
}
case reflect.Struct:
if versionable, ok := value.(VersionableInterface); ok {
if fieldValue, ok := scope.New(value).FieldByName(fieldName); ok {
filterFilterValuesWithVersion(fieldValue, versionable.GetVersionName())
}
}
}
}
if IsVersionableModel(scope.Value) {
for _, field := range scope.Fields() {
if IsShareableVersionModel(reflect.New(field.Struct.Type).Interface()) {
fixSharedVersionRecords(scope.Value, field.Name)
}
}
}
}
func createCallback(scope *gorm.Scope) {
if IsVersionableModel(scope.Value) {
if field, ok := scope.FieldByName("VersionName"); ok {
if field.IsBlank {
field.Set(DefaultVersionName)
}
}
updateVersionPriority(scope)
}
if IsShareableVersionModel(scope.Value) {
if field, ok := scope.FieldByName("VersionName"); ok {
field.IsBlank = false
}
}
}
func updateCallback(scope *gorm.Scope) {
if IsVersionableModel(scope.Value) {
updateVersionPriority(scope)
}
}
func deleteCallback(scope *gorm.Scope) {
if versionName, ok := scope.DB().Get(VersionNameMode); ok && versionName != "" {
if IsVersionableModel(scope.Value) || IsShareableVersionModel(scope.Value) {
scope.Search.Where("version_name = ?", versionName)
}
}
}
func updateVersionPriority(scope *gorm.Scope) {
if field, ok := scope.FieldByName("VersionPriority"); ok {
var scheduledTime *time.Time
var versionName string
if scheduled, ok := scope.Value.(ScheduledInterface); ok {
scheduledTime = scheduled.GetScheduledStartAt()
}
if scheduledTime == nil {
unix := time.Unix(0, 0)
scheduledTime = &unix
}
if versionable, ok := scope.Value.(VersionableInterface); ok {
versionName = versionable.GetVersionName()
}
priority := fmt.Sprintf("%v_%v", scheduledTime.UTC().Format(time.RFC3339), versionName)
field.Set(priority)
}
}