-
Notifications
You must be signed in to change notification settings - Fork 4
/
sqlite.go
895 lines (849 loc) · 18.6 KB
/
sqlite.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
// Package sql 数据库/数据处理相关工具
package sql
import (
"database/sql"
"errors"
"reflect"
"strconv"
"strings"
"time"
"unicode"
_ "modernc.org/sqlite" // 引入sqlite
"github.com/FloatTech/ttl"
)
var (
ErrNilDB = errors.New("sqlite: db is not initialized")
ErrNullResult = errors.New("sqlite: null result")
DriverName = "sqlite3"
)
// Sqlite 数据库对象
type Sqlite struct {
db *sql.DB
dbpath string
stmtcache *ttl.Cache[string, *sql.Stmt]
}
func New(dbpath string) Sqlite {
return Sqlite{dbpath: dbpath}
}
// Open 打开数据库
func (db *Sqlite) Open(cachettl time.Duration) (err error) {
if db.db == nil {
database, err := sql.Open(DriverName, db.dbpath)
if err != nil {
return err
}
db.db = database
}
if db.stmtcache == nil {
db.stmtcache = ttl.NewCacheOn(cachettl, [4]func(string, *sql.Stmt){
nil, nil,
func(_ string, stmt *sql.Stmt) { _ = stmt.Close() },
nil,
})
}
return
}
// Close 关闭数据库
func (db *Sqlite) Close() (err error) {
if db.db != nil {
err = db.db.Close()
db.db = nil
db.stmtcache.Destroy()
db.stmtcache = nil
}
return
}
func wraptable(table string) string {
first := []rune(table)[0]
if first < unicode.MaxLatin1 && unicode.IsDigit(first) {
return "[" + table + "]"
} else {
return "'" + table + "'"
}
}
func (db *Sqlite) compile(q string) (*sql.Stmt, error) {
stmt := db.stmtcache.Get(q)
if stmt == nil {
var err error
stmt, err = db.db.Prepare(q)
if err != nil {
return nil, err
}
db.stmtcache.Set(q, stmt)
}
return stmt, nil
}
func (db *Sqlite) mustcompile(q string) *sql.Stmt {
stmt := db.stmtcache.Get(q)
if stmt == nil {
var err error
stmt, err = db.db.Prepare(q)
if err != nil {
panic(err)
}
db.stmtcache.Set(q, stmt)
}
return stmt
}
// Create 生成数据库.
// 默认结构体的第一个元素为主键.
// 返回错误.
func (db *Sqlite) Create(table string, objptr interface{}, additional ...string) (err error) {
if db.db == nil {
err = ErrNilDB
return
}
var (
tags = tags(objptr)
kinds = kinds(objptr)
top = len(tags) - 1
cmd = make([]string, 0, 3*(len(tags)+1))
)
cmd = append(cmd, "CREATE TABLE IF NOT EXISTS", wraptable(table), "(")
if top == 0 {
pk, _, _ := strings.Cut(tags[0], ",")
cmd = append(cmd, pk, kinds[0], "PRIMARY KEY")
if len(additional) > 0 {
cmd = append(cmd, ",")
cmd = append(cmd, strings.Join(additional, ","))
}
cmd = append(cmd, ")")
} else {
for i := range tags {
name, addi, hasaddi := strings.Cut(tags[i], ",")
cmd = append(cmd, name, kinds[i])
if hasaddi && i > 0 {
cmd = append(cmd, addi)
}
switch i {
default:
cmd = append(cmd, ",")
case 0:
cmd = append(cmd, "PRIMARY KEY,")
case top:
if len(additional) > 0 {
cmd = append(cmd, ",")
cmd = append(cmd, strings.Join(additional, ","))
}
cmd = append(cmd, ")")
}
}
}
stmt, err := db.compile(strings.Join(cmd, " ") + ";")
if err != nil {
return err
}
_, err = stmt.Exec()
return
}
// Insert 插入数据集.
// 如果 PK 存在会覆盖.
// 默认结构体的第一个元素为主键.
// 返回错误.
func (db *Sqlite) Insert(table string, objptr interface{}) error {
if db.db == nil {
return ErrNilDB
}
table = wraptable(table)
stmt, err := db.compile("SELECT * FROM " + table + " limit 1;")
if err != nil {
return err
}
rows, err := stmt.Query()
if err != nil {
return err
}
if rows.Err() != nil {
return rows.Err()
}
tags, _ := rows.Columns()
rows.Close()
var (
vals = values(objptr)
top = len(tags) - 1
cmd = make([]string, 0, 2+4*len(tags))
)
cmd = append(cmd, "REPLACE INTO")
cmd = append(cmd, table)
if top == 0 {
cmd = append(cmd, "(")
cmd = append(cmd, tags[0])
cmd = append(cmd, ") VALUES ( ? )")
} else {
for i := range tags {
switch i {
default:
cmd = append(cmd, tags[i])
cmd = append(cmd, ",")
case 0:
cmd = append(cmd, "(")
cmd = append(cmd, tags[i])
cmd = append(cmd, ",")
case top:
cmd = append(cmd, tags[i])
cmd = append(cmd, ")")
}
}
for i := range tags {
switch i {
default:
cmd = append(cmd, "? ,")
case 0:
cmd = append(cmd, "VALUES ( ? ,")
case top:
cmd = append(cmd, "? )")
}
}
}
stmt, err = db.compile(strings.Join(cmd, " ") + ";")
if err != nil {
return err
}
_, err = stmt.Exec(vals...)
return err
}
// InsertUnique 插入数据集.
// 如果 PK 存在会报错.
// 默认结构体的第一个元素为主键.
// 返回错误.
func (db *Sqlite) InsertUnique(table string, objptr interface{}) error {
if db.db == nil {
return ErrNilDB
}
table = wraptable(table)
stmt, err := db.compile("SELECT * FROM " + table + " limit 1;")
if err != nil {
return err
}
rows, err := stmt.Query()
if err != nil {
return err
}
if rows.Err() != nil {
return rows.Err()
}
tags, _ := rows.Columns()
rows.Close()
var (
vals = values(objptr)
top = len(tags) - 1
cmd = make([]string, 0, 2+4*len(tags))
)
cmd = append(cmd, "INSERT INTO")
cmd = append(cmd, table)
if top == 0 {
cmd = append(cmd, "(")
cmd = append(cmd, tags[0])
cmd = append(cmd, ") VALUES ( ? )")
} else {
for i := range tags {
switch i {
default:
cmd = append(cmd, tags[i])
cmd = append(cmd, ",")
case 0:
cmd = append(cmd, "(")
cmd = append(cmd, tags[i])
cmd = append(cmd, ",")
case top:
cmd = append(cmd, tags[i])
cmd = append(cmd, ")")
}
}
for i := range tags {
switch i {
default:
cmd = append(cmd, "? ,")
case 0:
cmd = append(cmd, "VALUES ( ? ,")
case top:
cmd = append(cmd, "? )")
}
}
}
stmt, err = db.compile(strings.Join(cmd, " ") + ";")
if err != nil {
return err
}
_, err = stmt.Exec(vals...)
return err
}
// Find 查询数据库,写入第一条结果到 objptr.
// condition 可为"WHERE id = 0".
// 默认字段与结构体元素顺序一致.
// 返回错误.
func (db *Sqlite) Find(table string, objptr interface{}, condition string, questions ...interface{}) error {
if db.db == nil {
return ErrNilDB
}
q := "SELECT * FROM " + wraptable(table) + " " + condition + ";"
stmt, err := db.compile(q)
if err != nil {
return err
}
rows, err := stmt.Query(questions...)
if err != nil {
return err
}
if rows.Err() != nil {
return rows.Err()
}
defer rows.Close()
if !rows.Next() {
return ErrNullResult
}
err = rows.Scan(addrs(objptr)...)
for rows.Next() {
if err == nil {
return nil
}
err = rows.Scan(addrs(objptr)...)
}
return err
}
// Find 查询数据库,返回第一条结果.
// condition 可为"WHERE id = 0".
// 默认字段与结构体元素顺序一致.
// 返回错误.
func Find[T any](db *Sqlite, table string, condition string, questions ...interface{}) (obj T, err error) {
if db.db == nil {
err = ErrNilDB
return
}
q := "SELECT * FROM " + wraptable(table) + " " + condition + ";"
stmt, err := db.compile(q)
if err != nil {
return
}
rows, err := stmt.Query(questions...)
if err != nil {
return
}
err = rows.Err()
if err != nil {
return
}
defer rows.Close()
if !rows.Next() {
err = ErrNullResult
return
}
err = rows.Scan(addrs(&obj)...)
for rows.Next() {
if err == nil {
return
}
err = rows.Scan(addrs(&obj)...)
}
return
}
// Query 查询数据库,写入第一条结果到 objptr.
// q 为一整条查询语句, 慎用.
// 默认字段与结构体元素顺序一致.
// 返回错误.
func (db *Sqlite) Query(q string, objptr interface{}, args ...interface{}) error {
if db.db == nil {
return ErrNilDB
}
stmt, err := db.compile(q)
if err != nil {
return err
}
rows, err := stmt.Query(args...)
if err != nil {
return err
}
if rows.Err() != nil {
return rows.Err()
}
defer rows.Close()
if !rows.Next() {
return ErrNullResult
}
err = rows.Scan(addrs(objptr)...)
for rows.Next() {
if err == nil {
return err
}
err = rows.Scan(addrs(objptr)...)
}
return err
}
// Query 查询数据库,返回第一条结果.
// q 为一整条查询语句, 慎用.
// 默认字段与结构体元素顺序一致.
// 返回错误.
func Query[T any](db *Sqlite, q string, args ...interface{}) (obj T, err error) {
if db.db == nil {
err = ErrNilDB
return
}
stmt, err := db.compile(q)
if err != nil {
return
}
rows, err := stmt.Query(args...)
if err != nil {
return
}
err = rows.Err()
if err != nil {
return
}
defer rows.Close()
if !rows.Next() {
err = ErrNullResult
return
}
err = rows.Scan(addrs(&obj)...)
for rows.Next() {
if err == nil {
return
}
err = rows.Scan(addrs(&obj)...)
}
return
}
// CanFind 查询数据库是否有 condition.
// condition 可为"WHERE id = 0".
// 默认字段与结构体元素顺序一致.
// 返回错误.
func (db *Sqlite) CanFind(table string, condition string, questions ...interface{}) bool {
if db.db == nil {
return false
}
q := "SELECT * FROM " + wraptable(table) + " " + condition + ";"
stmt, err := db.compile(q)
if err != nil {
return false
}
rows, err := stmt.Query(questions...)
if err != nil {
return false
}
if rows.Err() != nil {
return false
}
defer rows.Close()
if !rows.Next() {
return false
}
_ = rows.Close()
return true
}
// CanQuery 查询数据库是否有 q.
// q 为一整条查询语句, 慎用.
// 默认字段与结构体元素顺序一致.
// 返回错误.
func (db *Sqlite) CanQuery(q string, questions ...interface{}) bool {
if db.db == nil {
return false
}
stmt, err := db.compile(q)
if err != nil {
return false
}
rows, err := stmt.Query(questions...)
if err != nil {
return false
}
if rows.Err() != nil {
return false
}
defer rows.Close()
if !rows.Next() {
return false
}
_ = rows.Close()
return true
}
// FindFor 查询数据库,用函数 f 遍历结果.
// condition 可为"WHERE id = 0".
// 默认字段与结构体元素顺序一致.
// 返回错误.
func (db *Sqlite) FindFor(table string, objptr interface{}, condition string, f func() error, questions ...interface{}) error {
if db.db == nil {
return ErrNilDB
}
q := "SELECT * FROM " + wraptable(table) + " " + condition + ";"
stmt, err := db.compile(q)
if err != nil {
return err
}
rows, err := stmt.Query(questions...)
if err != nil {
return err
}
if rows.Err() != nil {
return rows.Err()
}
defer rows.Close()
if !rows.Next() {
return ErrNullResult
}
err = rows.Scan(addrs(objptr)...)
if err == nil {
err = f()
}
for rows.Next() {
if err != nil {
return err
}
err = rows.Scan(addrs(objptr)...)
if err == nil {
err = f()
}
}
return err
}
// FindAll 查询数据库,返回多个结果.
// condition 可为"WHERE id = 0".
// 默认字段与结构体元素顺序一致.
// 返回错误.
func FindAll[T any](db *Sqlite, table string, condition string, questions ...interface{}) ([]*T, error) {
if db.db == nil {
return nil, ErrNilDB
}
q := "SELECT * FROM " + wraptable(table) + " " + condition + ";"
stmt, err := db.compile(q)
if err != nil {
return nil, err
}
rows, err := stmt.Query(questions...)
if err != nil {
return nil, err
}
if rows.Err() != nil {
return nil, rows.Err()
}
defer rows.Close()
if !rows.Next() {
return nil, ErrNullResult
}
vals := make([]*T, 1, 64)
var v T
err = rows.Scan(addrs(&v)...)
if err != nil {
return nil, err
}
vals[0] = &v
for rows.Next() {
if err != nil {
return nil, err
}
var v T
err = rows.Scan(addrs(&v)...)
if err == nil {
vals = append(vals, &v)
}
}
return vals, nil
}
// QueryFor 查询数据库,用函数 f 遍历结果.
// q 为一整条查询语句, 慎用.
// 默认字段与结构体元素顺序一致.
// 返回错误.
func (db *Sqlite) QueryFor(q string, objptr interface{}, f func() error, questions ...interface{}) error {
if db.db == nil {
return ErrNilDB
}
stmt, err := db.compile(q)
if err != nil {
return err
}
rows, err := stmt.Query(questions...)
if err != nil {
return err
}
if rows.Err() != nil {
return rows.Err()
}
defer rows.Close()
if !rows.Next() {
return ErrNullResult
}
err = rows.Scan(addrs(objptr)...)
if err == nil {
err = f()
}
for rows.Next() {
if err != nil {
return err
}
err = rows.Scan(addrs(objptr)...)
if err == nil {
err = f()
}
}
return err
}
// QueryAll 查询数据库,返回多个结果.
// q 为一整条查询语句, 慎用.
// 默认字段与结构体元素顺序一致.
// 返回错误.
func QueryAll[T any](db *Sqlite, q string, questions ...interface{}) ([]*T, error) {
if db.db == nil {
return nil, ErrNilDB
}
stmt, err := db.compile(q)
if err != nil {
return nil, err
}
rows, err := stmt.Query(questions...)
if err != nil {
return nil, err
}
if rows.Err() != nil {
return nil, rows.Err()
}
defer rows.Close()
if !rows.Next() {
return nil, ErrNullResult
}
vals := make([]*T, 1, 64)
var v T
err = rows.Scan(addrs(&v)...)
if err != nil {
return nil, err
}
vals[0] = &v
for rows.Next() {
if err != nil {
return nil, err
}
var v T
err = rows.Scan(addrs(&v)...)
if err == nil {
vals = append(vals, &v)
}
}
return vals, nil
}
// Pick 从 table 随机一行
func (db *Sqlite) Pick(table string, objptr interface{}, questions ...interface{}) error {
if db.db == nil {
return ErrNilDB
}
return db.Find(table, objptr, "ORDER BY RANDOM() limit 1", questions...)
}
// PickFor 从 table 随机多行
func (db *Sqlite) PickFor(table string, n uint, objptr interface{}, f func() error, questions ...interface{}) error {
if db.db == nil {
return ErrNilDB
}
return db.FindFor(table, objptr, "ORDER BY RANDOM() limit "+strconv.Itoa(int(n)), f, questions...)
}
// ListTables 列出所有表名
// 返回所有表名+错误
func (db *Sqlite) ListTables() (s []string, err error) {
if db.db == nil {
return nil, ErrNilDB
}
rows, err := db.mustcompile("SELECT name FROM sqlite_master where type='table' order by name;").Query()
if err != nil {
return
}
if rows.Err() != nil {
return nil, rows.Err()
}
defer rows.Close()
for rows.Next() {
if err != nil {
return
}
objptr := new(string)
err = rows.Scan(objptr)
if err == nil {
s = append(s, *objptr)
}
}
return
}
// Del 删除数据库表项.
// condition 可为"WHERE id = 0".
// 返回错误.
func (db *Sqlite) Del(table string, condition string, questions ...interface{}) error {
if db.db == nil {
return ErrNilDB
}
q := "DELETE FROM " + wraptable(table) + " " + condition + ";"
stmt, err := db.compile(q)
if err != nil {
return err
}
_, err = stmt.Exec(questions...)
return err
}
// Drop 删除数据库表
func (db *Sqlite) Drop(table string) error {
if db.db == nil {
return ErrNilDB
}
q := "DROP TABLE " + wraptable(table) + ";"
stmt, err := db.compile(q)
if err != nil {
return err
}
_, err = stmt.Exec()
return err
}
// Count 查询数据库行数.
// 返回行数以及错误.
func (db *Sqlite) Count(table string) (num int, err error) {
if db.db == nil {
return 0, ErrNilDB
}
stmt, err := db.compile("SELECT COUNT(1) FROM " + wraptable(table) + ";")
if err != nil {
return 0, err
}
rows, err := stmt.Query()
if err != nil {
return 0, err
}
if rows.Err() != nil {
return 0, rows.Err()
}
if rows.Next() {
err = rows.Scan(&num)
}
rows.Close()
return num, err
}
// tags 反射 返回结构体对象的 tag 数组
func tags(objptr interface{}) (tags []string) {
elem := reflect.ValueOf(objptr).Elem()
flen := elem.Type().NumField()
tags = make([]string, flen)
for i := 0; i < flen; i++ {
t := elem.Type().Field(i).Tag.Get("db")
if t == "" {
t = elem.Type().Field(i).Tag.Get("json")
if t == "" {
t = elem.Type().Field(i).Name
}
}
tags[i] = t
}
return
}
// kinds 反射 返回结构体对象的 kinds 数组
func kinds(objptr interface{}) (kinds []string) {
elem := reflect.ValueOf(objptr).Elem()
// 判断第一个元素是否为匿名字段
if elem.Type().Field(0).Anonymous {
elem = elem.Field(0)
}
flen := elem.Type().NumField()
kinds = make([]string, flen)
for i := 0; i < flen; i++ {
typ := elem.Field(i).Type().String()
switch typ {
case "bool", "*bool":
kinds[i] = "BOOLEAN"
case "int8", "*int8":
kinds[i] = "TINYINT"
case "uint8", "byte", "*uint8", "*byte":
kinds[i] = "UNSIGNED TINYINT"
case "int16", "*int16":
kinds[i] = "SMALLINT"
case "uint16", "*uint16":
kinds[i] = "UNSIGNED SMALLINT"
case "int", "*int":
kinds[i] = "INTEGER"
case "uint", "*uint", "uintptr", "*uintptr":
kinds[i] = "UNSIGNED INTEGER"
case "int32", "rune", "*int32", "*rune":
kinds[i] = "INT"
case "uint32", "*uint32":
kinds[i] = "UNSIGNED INT"
case "int64", "*int64":
kinds[i] = "BIGINT"
case "uint64", "*uint64":
kinds[i] = "UNSIGNED BIGINT"
case "float32", "*float32":
kinds[i] = "FLOAT"
case "float64", "*float64":
kinds[i] = "DOUBLE"
case "string", "[]string", "*string", "*[]string":
kinds[i] = "TEXT"
default:
k := elem.Field(i).Kind()
if k == reflect.Interface || k == reflect.Pointer {
typ = "*"
k = elem.Field(i).Elem().Kind()
}
switch k {
case reflect.Bool:
kinds[i] = "BOOLEAN"
case reflect.Int:
kinds[i] = "INTEGER"
case reflect.Int8:
kinds[i] = "TINYINT"
case reflect.Int16:
kinds[i] = "SMALLINT"
case reflect.Int32:
kinds[i] = "INT"
case reflect.Int64:
kinds[i] = "BIGINT"
case reflect.Uint:
kinds[i] = "UNSIGNED INTEGER"
case reflect.Uint8:
kinds[i] = "UNSIGNED TINYINT"
case reflect.Uint16:
kinds[i] = "UNSIGNED SMALLINT"
case reflect.Uint32:
kinds[i] = "UNSIGNED INT"
case reflect.Uint64:
kinds[i] = "UNSIGNED BIGINT"
case reflect.Uintptr:
kinds[i] = "UNSIGNED INTEGER"
case reflect.Float32:
kinds[i] = "FLOAT"
case reflect.Float64:
kinds[i] = "DOUBLE"
case reflect.String:
kinds[i] = "TEXT"
default:
kinds[i] = "BLOB"
}
}
if strings.Contains(typ, "*") || strings.Contains(typ, "[]") {
kinds[i] += " NULL"
} else {
kinds[i] += " NOT NULL"
}
}
return
}
var typstrarr = reflect.SliceOf(reflect.TypeOf(""))
// values 反射 返回结构体对象的 values 数组
func values(objptr interface{}) (values []interface{}) {
elem := reflect.ValueOf(objptr).Elem()
flen := elem.Type().NumField()
values = make([]interface{}, flen)
for i := 0; i < flen; i++ {
if elem.Field(i).Type() == typstrarr { // []string
values[i] = elem.Field(i).Index(0).Interface() // string
continue
}
values[i] = elem.Field(i).Interface()
}
return
}
// addrs 反射 返回结构体对象的 addrs 数组
func addrs(objptr interface{}) (addrs []interface{}) {
elem := reflect.ValueOf(objptr).Elem()
flen := elem.Type().NumField()
addrs = make([]interface{}, flen)
for i := 0; i < flen; i++ {
if elem.Field(i).Type() == typstrarr { // []string
s := reflect.ValueOf(make([]string, 1))
elem.Field(i).Set(s)
addrs[i] = s.Index(0).Addr().Interface() // string
continue
}
addrs[i] = elem.Field(i).Addr().Interface()
}
return
}