-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmodels.go
699 lines (587 loc) · 18.4 KB
/
models.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
package admin
import (
"database/sql"
"errors"
"fmt"
"io"
"net/http"
"reflect"
"sort"
"strings"
"github.com/extemporalgenome/slug"
"github.com/oal/admin/db"
"github.com/oal/admin/fields"
)
// NamedModel requires an AdminName method to be present, to override the model's displayed name in the admin panel.
type NamedModel interface {
AdminName() string
}
type SortedModel interface {
SortBy() string
}
type modelGroup struct {
admin *Admin
Name string
slug string
Models []*model
}
// RegisterModel adds a model to a model group.
func (g *modelGroup) RegisterModel(mdl interface{}) error {
modelType := reflect.TypeOf(mdl)
ind := reflect.Indirect(reflect.ValueOf(mdl))
name := typeToName(modelType)
tableName := typeToTableName(modelType, g.admin.NameTransform)
if named, ok := mdl.(NamedModel); ok {
name = named.AdminName()
}
newModel := model{
Name: name,
Slug: slug.SlugAscii(name),
tableName: tableName,
fields: []fields.Field{},
fieldNames: []string{},
listFields: []fields.Field{},
searchableColumns: []string{},
admin: g.admin,
}
// Set as registered so it can be used as a ForeignKey from other models
if _, ok := g.admin.registeredRels[modelType]; !ok {
g.admin.registeredRels[modelType] = &newModel
}
// Check if any fields previously registered is missing this model as a foreign key
for field, missingType := range g.admin.missingRels {
if missingType != modelType {
continue
}
field.SetModelSlug(newModel.Slug)
delete(g.admin.missingRels, field)
}
// Loop over struct fields and set up fields
for i := 0; i < ind.NumField(); i++ {
refl := modelType.Elem().Field(i)
fieldType := refl.Type
kind := fieldType.Kind()
// Expect pointers to be foreign keys and foreign keys to have the form Field[Id]
fieldName := refl.Name
if kind == reflect.Ptr {
fieldName += "Id"
}
// Parse key=val / key options from struct tag, used for configuration later
tag := refl.Tag.Get("admin")
if tag == "-" {
if i == 0 {
return errors.New("First column (id) can't be skipped.")
}
continue
}
tagMap, err := parseTag(tag)
if err != nil {
panic(err)
}
// ID (i == 0) is always shown
if i == 0 {
tagMap["list"] = ""
}
override, _ := tagMap["field"]
field := makeField(kind, override)
// If slice, get type / kind of elements instead
// makeField still needs to know it's a slice, but this is needed below
if kind == reflect.Slice {
fieldType = fieldType.Elem()
kind = fieldType.Kind()
}
// Relationships need some additional data added to them
if relField, ok := field.(fields.RelationalField); ok {
// If column is shown in list view, and a field in related model is set to be listed
if listField, ok := tagMap["list"]; ok && len(listField) != 0 {
if g.admin.NameTransform != nil {
listField = g.admin.NameTransform(listField)
}
relField.SetListColumn(listField)
}
relField.SetRelatedTable(typeToTableName(fieldType, g.admin.NameTransform))
// We also need the field to know what model it's related to
if regModel, ok := g.admin.registeredRels[fieldType]; ok {
relField.SetModelSlug(regModel.Slug)
} else {
g.admin.missingRels[relField] = fieldType
}
field, _ = relField.(fields.Field)
}
// Transform struct keys to DB column names if needed
var tableField string
if g.admin.NameTransform != nil {
tableField = g.admin.NameTransform(fieldName)
} else {
tableField = refl.Name
}
field.Attrs().Name = fieldName
field.Attrs().ColumnName = tableField
applyFieldTags(&newModel, field, tagMap)
newModel.fields = append(newModel.fields, field)
newModel.fieldNames = append(newModel.fieldNames, fieldName)
}
// Default sorting in list view
if sorted, ok := mdl.(SortedModel); ok && newModel.fieldByName(sorted.SortBy()) != nil {
newModel.sort = sorted.SortBy()
} else {
newModel.sort = "-Id"
}
g.admin.models[newModel.Slug] = &newModel
g.Models = append(g.Models, &newModel)
fmt.Println("Registered", newModel.Name)
return nil
}
func makeField(kind reflect.Kind, override string) fields.Field {
// First, check if we want to override a field, otherwise use one of the defaults
var field fields.Field
if customField := fields.GetCustom(override); customField != nil {
// Create field
customType := reflect.ValueOf(customField).Elem().Type()
newField := reflect.New(customType)
// Create BaseField in Field
baseField := newField.Elem().Field(0)
baseField.Set(reflect.ValueOf(&fields.BaseField{}))
field = newField.Interface().(fields.Field)
} else {
switch kind {
case reflect.String:
field = &fields.TextField{BaseField: &fields.BaseField{}}
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
field = &fields.IntField{BaseField: &fields.BaseField{}}
case reflect.Float32, reflect.Float64:
field = &fields.FloatField{BaseField: &fields.BaseField{}}
case reflect.Bool:
field = &fields.BooleanField{BaseField: &fields.BaseField{}}
case reflect.Struct:
field = &fields.TimeField{BaseField: &fields.BaseField{}}
case reflect.Ptr:
field = &fields.ForeignKeyField{BaseField: &fields.BaseField{}}
case reflect.Slice:
field = &fields.ManyToManyField{BaseField: &fields.BaseField{}}
default:
fmt.Println("Unknown field type")
field = &fields.TextField{BaseField: &fields.BaseField{}}
}
}
return field
}
func applyFieldTags(mdl *model, field fields.Field, tagMap map[string]string) {
// Read relevant config options from the tagMap
err := field.Configure(tagMap)
if err != nil {
panic(err)
}
if label, ok := tagMap["label"]; ok {
field.Attrs().Label = label
} else {
field.Attrs().Label = field.Attrs().Name
}
if _, ok := tagMap["blank"]; ok {
field.Attrs().Blank = true
}
if tab, ok := tagMap["rel_table"]; ok {
field.Attrs().RelationTable = tab
}
if _, ok := tagMap["null"]; ok {
field.Attrs().Null = true
}
if _, ok := tagMap["list"]; ok {
field.Attrs().List = true
mdl.listFields = append(mdl.listFields, field)
}
if help, ok := tagMap["help_text"]; ok {
field.Attrs().Help = help
}
if _, ok := tagMap["right"]; ok {
field.Attrs().Right = true
}
if _, ok := tagMap["search"]; ok {
field.Attrs().Searchable = true
mdl.searchableColumns = append(mdl.searchableColumns, field.Attrs().ColumnName)
}
if val, ok := tagMap["default"]; ok {
field.Attrs().DefaultValue = val
}
if width, ok := tagMap["width"]; ok {
i, err := parseInt(width)
if err != nil {
panic(err)
}
field.Attrs().Width = i
}
}
type model struct {
Name string
Slug string
fields []fields.Field
tableName string
fieldNames []string
listFields []fields.Field
searchableColumns []string
sort string
admin *Admin
}
func (m *model) renderForm(w io.Writer, data map[string]interface{}, defaults bool, errors map[string]string) {
var val interface{}
var ok bool
activeCol := 0
for _, fieldName := range m.fieldNames[1:] {
field := m.fieldByName(fieldName)
val, ok = data[fieldName]
if !ok && defaults {
val = field.Attrs().DefaultValue
}
// Error text displayed below field, if any
var err string
if errors != nil {
err = errors[fieldName]
}
field.Render(w, val, err, activeCol%12 == 0)
activeCol += field.Attrs().Width
}
}
func (m *model) fieldByName(name string) fields.Field {
for _, field := range m.fields {
if field.Attrs().Name == name {
return field
}
}
return nil
}
func (m *model) get(id int) (map[string]interface{}, error) {
cols := make([]string, 0, len(m.fieldNames))
m2mFields := map[string]struct{}{}
// Can't do * as column order in the DB might not match struct
for _, fieldName := range m.fieldNames {
// Add to m2mFields so we can load it later
if _, ok := m.fieldByName(fieldName).(*fields.ManyToManyField); ok {
m2mFields[fieldName] = struct{}{}
continue
}
// Normal columns will be loaded directly in the main query
if m.admin.NameTransform != nil {
fieldName = m.admin.NameTransform(fieldName)
}
cols = append(cols, fieldName)
}
q := m.admin.dialect.Queryf("SELECT %v FROM %v WHERE id = ?", strings.Join(cols, ", "), m.tableName)
row := m.admin.db.QueryRow(q, id)
result, err := db.ScanRow(len(cols), row)
if err != nil {
return nil, err
}
// Loop over fields, and run separate query for M2Ms. Iterator index i only increases if there is value for column in main query.
resultMap := map[string]interface{}{}
i := 0
for _, fieldName := range m.fieldNames {
if _, ok := m2mFields[fieldName]; ok {
// Get Id of all related rows
field, ok := m.fieldByName(fieldName).(*fields.ManyToManyField)
if !ok {
continue
}
table_name := fmt.Sprintf("%v_%v", m.tableName, field.ColumnName)
if field.GetRelationTable() != "" {
table_name = field.GetRelationTable()
}
relTable := field.GetRelatedTable()
q := m.admin.dialect.Queryf("SELECT %v_id FROM %v WHERE %v_id = ?", relTable, table_name, m.tableName)
rows, err := m.admin.db.Query(q, id)
if err != nil {
return nil, err
}
ids := []int{}
for rows.Next() {
var relId int
rows.Scan(&relId)
ids = append(ids, relId)
}
resultMap[fieldName] = ids
continue
}
// Any other data type
resultMap[fieldName] = result[i]
i++
}
return resultMap, nil
}
func (m *model) page(page int, search, sortBy string, sortDesc bool) ([][]interface{}, int, error) {
page--
// Ugly search. Will fix later.
doSearch := false
whereStr := ""
searchBlock := ""
aliasIndex := 1
if len(search) > 0 {
searchBlock = fmt.Sprintf(" WHERE %v.id IN (SELECT id FROM (SELECT %v.id, ", m.tableName, m.tableName)
}
cols := []string{}
tables := []string{m.tableName}
for _, field := range m.fields {
if field.Attrs().List {
var colSearch string
colName := fmt.Sprintf("%v.%v", m.tableName, field.Attrs().ColumnName)
if relField, ok := field.(fields.RelationalField); ok && len(relField.GetListColumn()) > 0 {
var fkColName, relTable string
if _, ok := field.(*fields.ManyToManyField); ok {
relTable = fmt.Sprintf("%v_%v", m.tableName, field.Attrs().ColumnName)
if relField.GetRelationTable() != "" {
relTable = relField.GetRelationTable()
}
fkColName = fmt.Sprintf("%v.%v", relField.GetRelatedTable(), relField.GetListColumn())
colName = fmt.Sprintf(`(SELECT GROUP_CONCAT(%v) FROM %v JOIN %v ON %v.%v_id = %v.id WHERE %v.%v_id = %v.id) AS "%v.%v"`,
fkColName, relTable, relField.GetRelatedTable(), relTable,
relField.GetRelatedTable(), relField.GetRelatedTable(),
relTable, m.tableName, m.tableName, m.tableName, field.Attrs().ColumnName)
} else if _, ok := field.(*fields.ForeignKeyField); ok {
relTable = relField.GetRelatedTable()
fkColName = fmt.Sprintf("%v.%v", relTable, relField.GetListColumn())
colName = fmt.Sprintf(`(SELECT GROUP_CONCAT(%v) FROM %v WHERE %v.id = %v.%v) AS "%v.%v"`,
fkColName, relTable, relTable, m.tableName, field.Attrs().ColumnName, m.tableName, field.Attrs().ColumnName)
}
colSearch = fmt.Sprintf(`%v AND %v LIKE "%%%v%%") AS alias%v, `, colName[:strings.LastIndex(colName, ")")], fkColName, search, aliasIndex)
} else {
// Non relational field:
colName = fmt.Sprintf(`%v AS "%v"`, colName, colName)
colSearch = fmt.Sprintf(`(SELECT %v.%v FROM %v AS _alias WHERE _alias.id = %v.id AND %v.%v LIKE "%%%v%%") AS alias%v, `,
m.tableName, field.Attrs().ColumnName, m.tableName, m.tableName,
m.tableName, field.Attrs().ColumnName, search, aliasIndex)
}
if len(search) > 0 && field.Attrs().Searchable {
doSearch = true
searchBlock += colSearch
aliasIndex++
}
if len(tables) == 0 {
tables = append(tables, m.tableName)
}
cols = append(cols, colName)
}
}
if doSearch {
// Chop last comma and space
searchBlock = searchBlock[:len(searchBlock)-2]
searchBlock += fmt.Sprintf(" FROM %v WHERE (", m.tableName)
for i := 1; i < aliasIndex; i++ {
searchBlock += fmt.Sprintf(`alias%v != ""`, i)
if i < aliasIndex-1 {
searchBlock += fmt.Sprintf(" OR ")
}
}
searchBlock += fmt.Sprintf(")))")
} else {
searchBlock = ""
}
sqlColumns := strings.Join(cols, ", ")
sqlTables := strings.Join(tables, ", ")
if len(sortBy) > 0 {
sortCol := sortBy
if m.admin.NameTransform != nil {
sortCol = m.admin.NameTransform(sortBy)
}
direction := "ASC"
if sortDesc {
direction = "DESC"
}
sortBy = fmt.Sprintf(` ORDER BY "%v.%v" %v`, m.tableName, sortCol, direction)
}
fromWhere := fmt.Sprintf("FROM %v %v%v", sqlTables, whereStr, searchBlock)
rowQuery := m.admin.dialect.Queryf("SELECT %v %v%v LIMIT %v,%v", sqlColumns, fromWhere, sortBy, page*25, 25)
// fmt.Printf("rowQuery: SELECT %v %v%v LIMIT %v,%v\n", sqlColumns, fromWhere, sortBy, page*25, 25)
var rows *sql.Rows
var countRow *sql.Row
var countQuery string
var err error
if doSearch {
countQuery = m.admin.dialect.Queryf("SELECT COUNT(*) FROM (SELECT %v %v)", sqlColumns, fromWhere)
} else {
countQuery = m.admin.dialect.Queryf("SELECT COUNT(*) FROM %v", m.tableName)
}
rows, err = m.admin.db.Query(rowQuery)
countRow = m.admin.db.QueryRow(countQuery)
numRows := 0
err = countRow.Scan(&numRows)
if err != nil {
fmt.Println("SCAN", err)
}
if err != nil {
return nil, numRows, err
}
numCols := len(cols)
results := [][]interface{}{}
for rows.Next() {
result, err := db.ScanRow(numCols, rows)
if err != nil {
return nil, numRows, err
}
results = append(results, result)
}
return results, numRows, nil
}
func (m *model) save(id int, req *http.Request) (map[string]interface{}, map[string]string, error) {
numFields := len(m.fieldNames) - 1 // No need for ID.
// Get existing data, if any, so we can check what values were changed (existing == nil for new rows)
var existing map[string]interface{}
if id != 0 {
var err error
existing, err = m.get(id)
if err != nil {
return nil, nil, err
}
}
// Get data from POST and fill a slice
data := map[string]interface{}{}
m2mData := map[string][]int{}
m2mChanges := false
dataErrors := map[string]string{}
hasErrors := false
for i := 0; i < numFields; i++ {
fieldName := m.fieldNames[i+1]
field := m.fieldByName(fieldName)
var existingVal interface{}
if existing != nil {
existingVal = existing[fieldName]
}
val, err := fields.Validate(field, req, existingVal)
if err != nil {
dataErrors[fieldName] = err.Error()
hasErrors = true
}
// ManyToManyField
if ids, ok := val.([]int); ok {
// Has M2M data changed?
if existingIds, ok := existingVal.([]int); ok {
// FIXME: Maybe not the best way to compare slices?
sort.Ints(ids)
sort.Ints(existingIds)
m2mChanges = fmt.Sprint(ids) != fmt.Sprint(existingIds)
} else if len(ids) > 0 {
m2mChanges = true
}
m2mData[fieldName] = ids
continue
}
data[fieldName] = val
}
if hasErrors {
return data, dataErrors, errors.New("Please correct the errors below.")
}
// Create query only with the changed data
changedCols := []string{}
changedData := []interface{}{}
for key, value := range data {
// Skip if not changed
if existing != nil && value == existing[key] {
continue
}
// Convert to DB version of name and append
col := key
if m.admin.NameTransform != nil {
col = m.admin.NameTransform(key)
}
if id != 0 {
col = fmt.Sprintf("%v = ?", col)
}
changedCols = append(changedCols, col)
changedData = append(changedData, value)
}
if len(changedCols) == 0 && !m2mChanges {
return nil, nil, errors.New(fmt.Sprintf("%v was not saved because there were no changes.", m.Name))
}
if len(changedCols) > 0 {
valMarks := strings.Repeat("?, ", len(changedCols))
valMarks = valMarks[0 : len(valMarks)-2]
// Insert / update
var q string
if id != 0 {
q = m.admin.dialect.Queryf("UPDATE %v SET %v WHERE id = %v", m.tableName, strings.Join(changedCols, ", "), id)
} else {
q = m.admin.dialect.Queryf("INSERT INTO %v(%v) VALUES(%v)", m.tableName, strings.Join(changedCols, ", "), valMarks)
}
result, err := m.admin.db.Exec(q, changedData...)
if err != nil {
fmt.Println(err)
return nil, nil, err
}
if newId, _ := result.LastInsertId(); id == 0 {
id = int(newId)
}
}
// if m2mChanges {
// Insert / update M2M
for fieldName, ids := range m2mData {
field, _ := m.fieldByName(fieldName).(*fields.ManyToManyField)
err := m.saveM2M(id, field, ids)
if err != nil {
return nil, nil, err
}
}
// }
return data, dataErrors, nil
}
func (m *model) saveM2M(id int, field *fields.ManyToManyField, relatedIds []int) error {
m2mTable := fmt.Sprintf("%v_%v", m.tableName, field.ColumnName)
if field.GetRelationTable() != "" {
m2mTable = field.GetRelationTable()
}
toColumn := fmt.Sprintf("%v_id", field.GetRelatedTable())
fromColumn := fmt.Sprintf("%v_id", m.tableName)
tx, err := m.admin.db.Begin()
if err != nil {
return err
}
defer tx.Commit()
existingRelQuery := m.admin.dialect.Queryf("SELECT %v FROM %v WHERE %v = ?", toColumn, m2mTable, fromColumn)
rows, err := tx.Query(existingRelQuery, id)
if err != nil {
return err
}
removeRels := map[int]bool{}
for rows.Next() {
var eId int
rows.Scan(&eId)
removeRels[eId] = true
}
// Add new, remove from removeRels as we go. Those still left in removeRels will be deleted.
for _, nId := range relatedIds {
if _, ok := removeRels[nId]; ok {
// Already exists,
delete(removeRels, nId)
} else {
// Relation doesn't exist yet, so add it
addRelQuery := m.admin.dialect.Queryf("INSERT INTO %v (%v, %v) VALUES (?, ?)", m2mTable, fromColumn, toColumn)
tx.Exec(addRelQuery, id, nId)
}
}
// Delete remaining Ids in removeRels as they're no longer related
for eId, _ := range removeRels {
removeRelQuery := m.admin.dialect.Queryf("DELETE FROM %v WHERE %v = ? AND %v = ?", m2mTable, fromColumn, toColumn)
tx.Exec(removeRelQuery, id, eId)
}
return nil
}
func (m *model) delete(id int) error {
_, err := m.get(id)
if err != nil {
return err
}
q := fmt.Sprintf("DELETE FROM %v WHERE id=?", m.tableName)
_, err = m.admin.db.Exec(q, id)
if err != nil {
return err
}
// Delete M2M relations
for _, fieldName := range m.fieldNames {
if field, ok := m.fieldByName(fieldName).(*fields.ManyToManyField); ok {
m2mTable := fmt.Sprintf("%v_%v", m.tableName, field.ColumnName)
if field.GetRelationTable() != "" {
m2mTable = field.GetRelationTable()
}
fromColumn := fmt.Sprintf("%v_id", m.tableName)
q := m.admin.dialect.Queryf("DELETE FROM %v WHERE %v = ?", m2mTable, fromColumn)
m.admin.db.Exec(q, id)
}
}
return nil
}