-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
default.go
970 lines (794 loc) · 25.4 KB
/
default.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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
package table
import (
"encoding/json"
"errors"
"fmt"
"github.com/GoAdminGroup/go-admin/modules/config"
"github.com/GoAdminGroup/go-admin/modules/db"
"github.com/GoAdminGroup/go-admin/modules/db/dialect"
"github.com/GoAdminGroup/go-admin/modules/language"
"github.com/GoAdminGroup/go-admin/modules/logger"
"github.com/GoAdminGroup/go-admin/plugins/admin/modules"
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/form"
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/paginator"
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/parameter"
"github.com/GoAdminGroup/go-admin/template/types"
form2 "github.com/GoAdminGroup/go-admin/template/types/form"
"html/template"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)
type DefaultTable struct {
info *types.InfoPanel
form *types.FormPanel
detail *types.InfoPanel
connectionDriver string
connection string
canAdd bool
editable bool
deletable bool
exportable bool
primaryKey PrimaryKey
sourceURL string
getDataFun GetDataFun
}
type GetDataFun func(path string, params parameter.Parameters, isAll bool, ids []string) ([]map[string]interface{}, int)
type PanelInfo struct {
Thead types.Thead
InfoList types.InfoList
FormData []types.FormField
Paginator types.PaginatorAttribute
Title string
Description string
}
func NewDefaultTable(cfg Config) Table {
return DefaultTable{
info: types.NewInfoPanel(cfg.PrimaryKey.Name),
form: types.NewFormPanel(),
detail: types.NewInfoPanel(cfg.PrimaryKey.Name),
connectionDriver: cfg.Driver,
connection: cfg.Connection,
canAdd: cfg.CanAdd,
editable: cfg.Editable,
deletable: cfg.Deletable,
exportable: cfg.Exportable,
primaryKey: cfg.PrimaryKey,
sourceURL: cfg.SourceURL,
getDataFun: cfg.GetDataFun,
}
}
func (tb DefaultTable) Copy() Table {
return DefaultTable{
form: types.NewFormPanel().SetTable(tb.form.Table).
SetDescription(tb.form.Description).
SetTitle(tb.form.Title),
info: types.NewInfoPanel(tb.primaryKey.Name).SetTable(tb.info.Table).
SetDescription(tb.info.Description).
SetTitle(tb.info.Title).
SetGetDataFn(tb.info.GetDataFn),
detail: types.NewInfoPanel(tb.primaryKey.Name).SetTable(tb.detail.Table).
SetDescription(tb.detail.Description).
SetTitle(tb.detail.Title).
SetGetDataFn(tb.detail.GetDataFn),
connectionDriver: tb.connectionDriver,
connection: tb.connection,
canAdd: tb.canAdd,
editable: tb.editable,
deletable: tb.deletable,
exportable: tb.exportable,
primaryKey: tb.primaryKey,
sourceURL: tb.sourceURL,
getDataFun: tb.getDataFun,
}
}
func (tb DefaultTable) GetInfo() *types.InfoPanel {
return tb.info
}
func (tb DefaultTable) GetDetail() *types.InfoPanel {
return tb.detail
}
func (tb DefaultTable) GetForm() *types.FormPanel {
return tb.form
}
func (tb DefaultTable) GetCanAdd() bool {
return tb.canAdd && !tb.info.IsHideNewButton
}
func (tb DefaultTable) GetPrimaryKey() PrimaryKey {
return tb.primaryKey
}
func (tb DefaultTable) GetEditable() bool {
return tb.editable && !tb.info.IsHideEditButton
}
func (tb DefaultTable) GetDeletable() bool {
return tb.deletable && !tb.info.IsHideDeleteButton
}
func (tb DefaultTable) IsShowDetail() bool {
return !tb.info.IsHideDetailButton
}
func (tb DefaultTable) GetExportable() bool {
return tb.exportable && !tb.info.IsHideExportButton
}
// GetData query the data set.
func (tb DefaultTable) GetData(params parameter.Parameters, isAll bool) (PanelInfo, error) {
var (
data []map[string]interface{}
size int
beginTime = time.Now()
)
if tb.getDataFun != nil {
data, size = tb.getDataFun(params.URLPath, params, isAll, []string{})
} else if tb.sourceURL != "" {
data, size = tb.getDataFromURL(params.URLPath, params, isAll, []string{})
} else if tb.info.GetDataFn != nil {
data, size = tb.info.GetDataFn(params.WithIsAll(isAll))
} else if isAll {
return tb.getAllDataFromDatabase(params.URLPath, params)
} else {
return tb.getDataFromDatabase(params.URLPath, params, []string{})
}
infoList := make(types.InfoList, 0)
for i := 0; i < len(data); i++ {
infoList = append(infoList, tb.getTempModelData(data[i], params, []string{}))
}
thead, _, _, _, filterForm := tb.getTheadAndFilterForm(params, []string{})
endTime := time.Now()
return PanelInfo{
Thead: thead,
InfoList: infoList,
Paginator: paginator.Get(paginator.Config{
Size: size,
Param: params,
PageSizeList: tb.info.GetPageSizeList(),
}).SetExtraInfo(template.HTML(fmt.Sprintf("<b>" + language.Get("query time") + ": </b>" +
fmt.Sprintf("%.3fms", endTime.Sub(beginTime).Seconds()*1000)))),
Title: tb.info.Title,
FormData: filterForm,
Description: tb.info.Description,
}, nil
}
type GetDataFromURLRes struct {
Data []map[string]interface{}
Size int
}
func (tb DefaultTable) getDataFromURL(path string, params parameter.Parameters, isAll bool, ids []string) ([]map[string]interface{}, int) {
u := ""
if strings.Contains(tb.sourceURL, "?") {
u = tb.sourceURL + "&" + params.Join()
} else {
u = tb.sourceURL + "?" + params.Join()
}
res, err := http.Get(u + "&pk=" + strings.Join(ids, ","))
if err != nil {
return []map[string]interface{}{}, 0
}
defer func() {
_ = res.Body.Close()
}()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return []map[string]interface{}{}, 0
}
var data GetDataFromURLRes
err = json.Unmarshal(body, &data)
if err != nil {
return []map[string]interface{}{}, 0
}
return data.Data, data.Size
}
// GetDataWithIds query the data set.
func (tb DefaultTable) GetDataWithIds(params parameter.Parameters, ids []string) (PanelInfo, error) {
var (
data []map[string]interface{}
size int
beginTime = time.Now()
)
if tb.getDataFun != nil {
data, size = tb.getDataFun(params.URLPath, params, false, ids)
} else if tb.sourceURL != "" {
data, size = tb.getDataFromURL(params.URLPath, params, false, ids)
} else if tb.info.GetDataFn != nil {
data, size = tb.info.GetDataFn(params.WithPK(ids...))
} else {
return tb.getDataFromDatabase(params.URLPath, params, ids)
}
infoList := make([]map[string]types.InfoItem, 0)
for i := 0; i < len(data); i++ {
infoList = append(infoList, tb.getTempModelData(data[i], params, []string{}))
}
thead, _, _, _, filterForm := tb.getTheadAndFilterForm(params, []string{})
endTime := time.Now()
return PanelInfo{
Thead: thead,
InfoList: infoList,
Paginator: paginator.Get(paginator.Config{
Size: size,
Param: params,
PageSizeList: tb.info.GetPageSizeList(),
}).
SetExtraInfo(template.HTML(fmt.Sprintf("<b>" + language.Get("query time") + ": </b>" +
fmt.Sprintf("%.3fms", endTime.Sub(beginTime).Seconds()*1000)))),
Title: tb.info.Title,
FormData: filterForm,
Description: tb.info.Description,
}, nil
}
func (tb DefaultTable) getTempModelData(res map[string]interface{}, params parameter.Parameters, columns Columns) map[string]types.InfoItem {
var tempModelData = make(map[string]types.InfoItem)
headField := ""
primaryKeyValue := db.GetValueFromDatabaseType(tb.primaryKey.Type, res[tb.primaryKey.Name], len(columns) == 0)
for _, field := range tb.info.FieldList {
headField = field.Field
if field.Join.Valid() {
headField = field.Join.Table + "_goadmin_join_" + field.Field
}
if field.Hide {
continue
}
if !modules.InArrayWithoutEmpty(params.Columns, headField) {
continue
}
typeName := field.TypeName
if field.Join.Valid() {
typeName = db.Varchar
}
combineValue := db.GetValueFromDatabaseType(typeName, res[headField], len(columns) == 0).String()
var value interface{}
if len(columns) == 0 || modules.InArray(columns, headField) || field.Join.Valid() {
value = field.ToDisplay(types.FieldModel{
ID: primaryKeyValue.String(),
Value: combineValue,
Row: res,
})
} else {
value = field.ToDisplay(types.FieldModel{
ID: primaryKeyValue.String(),
Value: "",
Row: res,
})
}
if valueStr, ok := value.(string); ok {
tempModelData[headField] = types.InfoItem{
Content: template.HTML(valueStr),
Value: combineValue,
}
} else {
tempModelData[headField] = types.InfoItem{
Content: value.(template.HTML),
Value: combineValue,
}
}
}
tempModelData[tb.primaryKey.Name] = types.InfoItem{
Content: template.HTML(primaryKeyValue.String()),
Value: primaryKeyValue.String(),
}
return tempModelData
}
func (tb DefaultTable) getAllDataFromDatabase(path string, params parameter.Parameters) (PanelInfo, error) {
var (
connection = tb.db()
queryStatement = "select %s from %s %s %s order by " + modules.Delimiter(connection.GetDelimiter(), "%s") + " %s"
)
columns, _ := tb.getColumns(tb.info.Table)
thead, fields, joins := tb.info.FieldList.GetThead(types.TableInfo{
Table: tb.info.Table,
Delimiter: tb.db().GetDelimiter(),
Driver: tb.connectionDriver,
PrimaryKey: tb.primaryKey.Name,
}, params, columns)
fields += tb.info.Table + "." + modules.FilterField(tb.primaryKey.Name, connection.GetDelimiter())
var (
wheres = ""
whereArgs = make([]interface{}, 0)
existKeys = make([]string, 0)
)
wheres, whereArgs, existKeys = params.Statement(wheres, connection.GetDelimiter(), whereArgs, columns, existKeys,
tb.info.FieldList.GetFieldFilterProcessValue, tb.info.FieldList.GetFieldJoinTable)
wheres, whereArgs = tb.info.Wheres.Statement(wheres, connection.GetDelimiter(), whereArgs, existKeys, columns)
wheres, whereArgs = tb.info.WhereRaws.Statement(wheres, whereArgs)
if wheres != "" {
wheres = " where " + wheres
}
if !modules.InArray(columns, params.SortField) {
params.SortField = tb.primaryKey.Name
}
queryCmd := fmt.Sprintf(queryStatement, fields, tb.info.Table, joins, wheres, params.SortField, params.SortType)
logger.LogSQL(queryCmd, []interface{}{})
res, err := connection.QueryWithConnection(tb.connection, queryCmd, whereArgs...)
if err != nil {
return PanelInfo{}, err
}
infoList := make([]map[string]types.InfoItem, 0)
for i := 0; i < len(res); i++ {
infoList = append(infoList, tb.getTempModelData(res[i], params, columns))
}
return PanelInfo{
InfoList: infoList,
Thead: thead,
Title: tb.info.Title,
Description: tb.info.Description,
}, nil
}
func (tb DefaultTable) getDataFromDatabase(path string, params parameter.Parameters, ids []string) (PanelInfo, error) {
var (
connection = tb.db()
placeholder = modules.Delimiter(connection.GetDelimiter(), "%s")
queryStatement string
countStatement string
)
beginTime := time.Now()
if len(ids) > 0 {
queryStatement = "select %s from %s %s where " + tb.primaryKey.Name + " in (%s) %s order by " + placeholder + " %s"
countStatement = "select count(*) from " + placeholder + " %s where " + tb.primaryKey.Name + " in (%s)"
} else {
if connection.Name() == "mssql" {
queryStatement = "SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY " + placeholder + " %s) as ROWNUMBER_, %s from " +
placeholder + "%s %s %s ) as TMP_ WHERE TMP_.ROWNUMBER_ > ? AND TMP_.ROWNUMBER_ <= ?"
countStatement = "select count(*) as [size] from " + placeholder + " %s %s"
} else {
queryStatement = "select %s from " + placeholder + "%s %s %s order by " + placeholder + " %s LIMIT ? OFFSET ?"
countStatement = "select count(*) from " + placeholder + " %s %s"
}
}
columns, _ := tb.getColumns(tb.info.Table)
thead, fields, joins, joinTables, filterForm := tb.getTheadAndFilterForm(params, columns)
fields += tb.info.Table + "." + modules.FilterField(tb.primaryKey.Name, connection.GetDelimiter())
if !modules.InArray(columns, params.SortField) {
params.SortField = tb.primaryKey.Name
}
var (
wheres = ""
whereArgs = make([]interface{}, 0)
args = make([]interface{}, 0)
existKeys = make([]string, 0)
)
if len(ids) > 0 {
for _, value := range ids {
if value != "" {
wheres += value + ","
}
}
wheres = wheres[:len(wheres)-1]
} else {
wheres, whereArgs, existKeys = params.Statement(wheres, connection.GetDelimiter(), whereArgs, columns, existKeys,
tb.info.FieldList.GetFieldFilterProcessValue, tb.info.FieldList.GetFieldJoinTable)
wheres, whereArgs = tb.info.Wheres.Statement(wheres, connection.GetDelimiter(), whereArgs, existKeys, columns)
wheres, whereArgs = tb.info.WhereRaws.Statement(wheres, whereArgs)
if wheres != "" {
wheres = " where " + wheres
}
if connection.Name() == "mssql" {
args = append(whereArgs, (params.PageInt-1)*params.PageSizeInt, params.PageInt*params.PageSizeInt)
} else {
args = append(whereArgs, params.PageSize, (params.PageInt-1)*params.PageSizeInt)
}
}
groupBy := ""
if len(joinTables) > 0 {
groupBy = " GROUP BY " + tb.info.Table + "." + modules.FilterField(tb.GetPrimaryKey().Name, connection.GetDelimiter())
}
queryCmd := ""
if connection.Name() == "mssql" {
queryCmd = fmt.Sprintf(queryStatement, params.SortField, params.SortType, fields, tb.info.Table, joins, wheres, groupBy)
} else {
queryCmd = fmt.Sprintf(queryStatement, fields, tb.info.Table, joins, wheres, groupBy, params.SortField, params.SortType)
}
logger.LogSQL(queryCmd, args)
res, err := connection.QueryWithConnection(tb.connection, queryCmd, args...)
if err != nil {
return PanelInfo{}, err
}
infoList := make([]map[string]types.InfoItem, 0)
for i := 0; i < len(res); i++ {
infoList = append(infoList, tb.getTempModelData(res[i], params, columns))
}
// TODO: use the dialect
if len(ids) > 0 {
joins = ""
}
countCmd := fmt.Sprintf(countStatement, tb.info.Table, joins, wheres)
total, err := connection.QueryWithConnection(tb.connection, countCmd, whereArgs...)
if err != nil {
return PanelInfo{}, err
}
logger.LogSQL(countCmd, nil)
var size int
if tb.connectionDriver == "postgresql" {
size = int(total[0]["count"].(int64))
} else if tb.connectionDriver == "mssql" {
size = int(total[0]["size"].(int64))
} else {
size = int(total[0]["count(*)"].(int64))
}
endTime := time.Now()
return PanelInfo{
Thead: thead,
InfoList: infoList,
Paginator: paginator.Get(paginator.Config{
Size: size,
Param: params,
PageSizeList: tb.info.GetPageSizeList(),
}).
SetExtraInfo(template.HTML(fmt.Sprintf("<b>" + language.Get("query time") + ": </b>" +
fmt.Sprintf("%.3fms", endTime.Sub(beginTime).Seconds()*1000)))),
Title: tb.info.Title,
FormData: filterForm,
Description: tb.info.Description,
}, nil
}
// GetDataWithId query the single row of data.
func (tb DefaultTable) GetDataWithId(id string) ([]types.FormField, [][]types.FormField, []string, string, string, error) {
var (
res map[string]interface{}
columns Columns
fields = make([]string, 0)
)
if tb.getDataFun != nil {
list, _ := tb.getDataFun("", parameter.BaseParam(), false, []string{id})
if len(list) > 0 {
res = list[0]
}
} else if tb.sourceURL != "" {
list, _ := tb.getDataFromURL("", parameter.BaseParam(), false, []string{id})
if len(list) > 0 {
res = list[0]
}
} else if tb.detail.GetDataFn != nil {
list, _ := tb.detail.GetDataFn(parameter.BaseParam().WithPK(id))
if len(list) > 0 {
res = list[0]
}
} else if tb.info.GetDataFn != nil {
list, _ := tb.info.GetDataFn(parameter.BaseParam().WithPK(id))
if len(list) > 0 {
res = list[0]
}
} else {
columns, _ = tb.getColumns(tb.form.Table)
var err error
res, err = tb.sql().
Table(tb.form.Table).Select(fields...).
Where(tb.primaryKey.Name, "=", id).
First()
if err != nil {
return nil, nil, nil, "", "", err
}
}
formList := tb.form.FieldList.Copy()
for i := 0; i < len(tb.form.FieldList); i++ {
if modules.InArray(columns, formList[i].Field) {
fields = append(fields, formList[i].Field)
}
}
var (
groupFormList = make([][]types.FormField, 0)
groupHeaders = make([]string, 0)
)
if len(tb.form.TabGroups) > 0 {
for key, value := range tb.form.TabGroups {
list := make([]types.FormField, len(value))
for j := 0; j < len(value); j++ {
for _, field := range tb.form.FieldList {
if value[j] == field.Field {
rowValue := modules.AorB(modules.InArray(columns, field.Field) || len(columns) == 0,
db.GetValueFromDatabaseType(field.TypeName, res[field.Field], len(columns) == 0).String(), "")
list[j] = field.UpdateValue(id, rowValue, res)
if list[j].FormType == form2.File && list[j].Value != template.HTML("") {
list[j].Value2 = "/" + config.Get().Store.Prefix + "/" + string(list[j].Value)
}
break
}
}
}
groupFormList = append(groupFormList, list)
groupHeaders = append(groupHeaders, tb.form.TabHeaders[key])
}
return tb.form.FieldList, groupFormList, groupHeaders, tb.form.Title, tb.form.Description, nil
}
for key, field := range formList {
rowValue := modules.AorB(modules.InArray(columns, field.Field) || len(columns) == 0,
db.GetValueFromDatabaseType(field.TypeName, res[field.Field], len(columns) == 0).String(), "")
formList[key] = field.UpdateValue(id, rowValue, res)
if formList[key].FormType == form2.File && formList[key].Value != template.HTML("") {
formList[key].Value2 = "/" + config.Get().Store.Prefix + "/" + string(formList[key].Value)
}
}
return formList, groupFormList, groupHeaders, tb.form.Title, tb.form.Description, nil
}
// UpdateDataFromDatabase update data.
func (tb DefaultTable) UpdateDataFromDatabase(dataList form.Values) error {
dataList.Add(form.PostTypeKey, "0")
if tb.form.Validator != nil {
if err := tb.form.Validator(dataList); err != nil {
return err
}
}
if tb.form.UpdateFn != nil {
dataList.Delete(form.PostTypeKey)
return tb.form.UpdateFn(dataList)
}
if tb.form.PreProcessFn != nil {
dataList = tb.form.PreProcessFn(dataList)
}
_, err := tb.sql().Table(tb.form.Table).
Where(tb.primaryKey.Name, "=", dataList.Get(tb.primaryKey.Name)).
Update(tb.getInjectValueFromFormValue(dataList))
// TODO: some errors should be ignored.
if err != nil && !strings.Contains(err.Error(), "no affect") {
if tb.connectionDriver != db.DriverPostgresql && tb.connectionDriver != db.DriverMssql {
return err
}
if !strings.Contains(err.Error(), "LastInsertId is not supported") {
return err
}
}
// NOTE: Database Transaction may be considered here.
if tb.form.PostHook != nil {
go func() {
defer func() {
if err := recover(); err != nil {
logger.Error(err)
}
}()
dataList.Add(form.PostTypeKey, "0")
err := tb.form.PostHook(dataList)
if err != nil {
logger.Error(err)
}
}()
}
return nil
}
// InsertDataFromDatabase insert data.
func (tb DefaultTable) InsertDataFromDatabase(dataList form.Values) error {
dataList.Add(form.PostTypeKey, "1")
if tb.form.Validator != nil {
if err := tb.form.Validator(dataList); err != nil {
return err
}
}
if tb.form.InsertFn != nil {
dataList.Delete(form.PostTypeKey)
return tb.form.InsertFn(dataList)
}
if tb.form.PreProcessFn != nil {
dataList = tb.form.PreProcessFn(dataList)
}
id, err := tb.sql().Table(tb.form.Table).Insert(tb.getInjectValueFromFormValue(dataList))
// TODO: some errors should be ignored.
if err != nil {
if tb.connectionDriver != db.DriverPostgresql && tb.connectionDriver != db.DriverMssql {
return err
}
if !strings.Contains(err.Error(), "LastInsertId is not supported") {
return err
}
}
dataList.Add(tb.GetPrimaryKey().Name, strconv.Itoa(int(id)))
if tb.form.PostHook != nil {
go func() {
defer func() {
if err := recover(); err != nil {
logger.Error(err)
}
}()
dataList.Add(form.PostTypeKey, "1")
err := tb.form.PostHook(dataList)
if err != nil {
logger.Error(err)
}
}()
}
return nil
}
func (tb DefaultTable) getInjectValueFromFormValue(dataList form.Values) dialect.H {
var (
value = make(dialect.H)
exceptString = make([]string, 0)
columns, auto = tb.getColumns(tb.form.Table)
fun types.PostFieldFilterFn
)
if auto {
exceptString = []string{tb.primaryKey.Name, form.PreviousKey, form.MethodKey, form.TokenKey}
} else {
exceptString = []string{form.PreviousKey, form.MethodKey, form.TokenKey}
}
if !dataList.IsSingleUpdatePost() {
for _, field := range tb.form.FieldList {
if field.FormType.IsMultiSelect() {
if _, ok := dataList[field.Field+"[]"]; !ok {
dataList[field.Field+"[]"] = []string{""}
}
}
}
}
dataList = dataList.RemoveRemark()
for k, v := range dataList {
k = strings.Replace(k, "[]", "", -1)
if !modules.InArray(exceptString, k) {
if modules.InArray(columns, k) {
delimiter := ","
for i := 0; i < len(tb.form.FieldList); i++ {
if k == tb.form.FieldList[i].Field {
fun = tb.form.FieldList[i].PostFilterFn
delimiter = modules.SetDefault(tb.form.FieldList[i].DefaultOptionDelimiter, ",")
}
}
vv := modules.RemoveBlankFromArray(v)
if fun != nil {
value[k] = fun(types.PostFieldModel{
ID: dataList.Get(tb.primaryKey.Name),
Value: vv,
})
} else {
if len(vv) > 1 {
value[k] = strings.Join(vv, delimiter)
} else if len(vv) > 0 {
value[k] = vv[0]
} else {
value[k] = ""
}
}
} else {
fun := tb.form.FieldList.FindByFieldName(k).PostFilterFn
if fun != nil {
fun(types.PostFieldModel{
ID: dataList.Get(tb.primaryKey.Name),
Value: modules.RemoveBlankFromArray(v),
})
}
}
}
}
return value
}
// DeleteDataFromDatabase delete data.
func (tb DefaultTable) DeleteDataFromDatabase(id string) error {
idArr := strings.Split(id, ",")
if tb.info.DeleteFn != nil {
if len(idArr) == 0 {
return errors.New("wrong parameter")
}
return tb.info.DeleteFn(idArr)
}
if tb.info.PreDeleteFn != nil && len(idArr) > 0 {
if err := tb.info.PreDeleteFn(idArr); err != nil {
return err
}
}
for _, id := range idArr {
tb.delete(tb.form.Table, tb.primaryKey.Name, id)
}
if tb.info.DeleteHook != nil && len(idArr) > 0 {
go func() {
defer func() {
if err := recover(); err != nil {
logger.Error(err)
}
}()
if err := tb.info.DeleteHook(idArr); err != nil {
logger.Error(err)
}
}()
}
return nil
}
func GetNewFormList(groupHeaders []string,
group [][]string,
old []types.FormField) ([]types.FormField, [][]types.FormField, []string) {
if len(group) == 0 {
var newForm []types.FormField
for _, v := range old {
if !v.NotAllowAdd {
v.Editable = true
newForm = append(newForm, v.UpdateDefaultValue())
}
}
return newForm, [][]types.FormField{}, []string{}
}
var (
newForm = make([][]types.FormField, 0)
headers = make([]string, 0)
)
for key, value := range group {
list := make([]types.FormField, 0)
for i := 0; i < len(value); i++ {
for _, v := range old {
if v.Field == value[i] {
if !v.NotAllowAdd {
v.Editable = true
list = append(list, v.UpdateDefaultValue())
break
}
}
}
}
newForm = append(newForm, list)
headers = append(headers, groupHeaders[key])
}
return []types.FormField{}, newForm, headers
}
// ***************************************
// helper function for database operation
// ***************************************
func (tb DefaultTable) delete(table, key, id string) {
_ = tb.sql().Table(table).
Where(key, "=", id).
Delete()
}
func (tb DefaultTable) getTheadAndFilterForm(params parameter.Parameters, columns Columns) (types.Thead,
string, string, []string, []types.FormField) {
return tb.info.FieldList.GetTheadAndFilterForm(types.TableInfo{
Table: tb.info.Table,
Delimiter: tb.delimiter(),
Driver: tb.connectionDriver,
PrimaryKey: tb.primaryKey.Name,
}, params, columns)
}
// db is a helper function return raw db connection.
func (tb DefaultTable) db() db.Connection {
return db.GetConnectionFromService(services.Get(tb.connectionDriver))
}
func (tb DefaultTable) delimiter() string {
if tb.getDataFromDB() {
return tb.db().GetDelimiter()
}
return ""
}
func (tb DefaultTable) getDataFromDB() bool {
return tb.sourceURL == "" && tb.getDataFun == nil && tb.info.GetDataFn == nil && tb.detail.GetDataFn == nil
}
// sql is a helper function return db sql.
func (tb DefaultTable) sql() *db.SQL {
return db.WithDriverAndConnection(tb.connection, db.GetConnectionFromService(services.Get(tb.connectionDriver)))
}
type Columns []string
func (tb DefaultTable) getColumns(table string) (Columns, bool) {
columnsModel, _ := tb.sql().Table(table).ShowColumns()
columns := make(Columns, len(columnsModel))
switch tb.connectionDriver {
case "postgresql":
auto := false
for key, model := range columnsModel {
columns[key] = model["column_name"].(string)
if columns[key] == tb.primaryKey.Name {
if v, ok := model["column_default"].(string); ok {
if strings.Contains(v, "nextval") {
auto = true
}
}
}
}
return columns, auto
case "mysql":
auto := false
for key, model := range columnsModel {
columns[key] = model["Field"].(string)
if columns[key] == tb.primaryKey.Name {
if v, ok := model["Extra"].(string); ok {
if v == "auto_increment" {
auto = true
}
}
}
}
return columns, auto
case "sqlite":
for key, model := range columnsModel {
columns[key] = string(model["name"].(string))
}
num, _ := tb.sql().Table("sqlite_sequence").
Where("name", "=", tb.GetForm().Table).Count()
return columns, num > 0
case "mssql":
for key, model := range columnsModel {
columns[key] = string(model["column_name"].(string))
}
return columns, true
default:
panic("wrong driver")
}
}