-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite_flavor.go
1019 lines (849 loc) · 28.8 KB
/
sqlite_flavor.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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package sqac
import (
"fmt"
"log"
"reflect"
"strconv"
"strings"
"github.com/1414C/sqac/common"
)
// SQLiteFlavor is a sqlite3-specific implementation.
// Methods defined in the PublicDB interface of struct-type
// BaseFlavor are called by default for SQLiteFlavor. If
// the method as it exists in the BaseFlavor implementation
// is not compatible with the schema-syntax required by
// SQLite, the method in question may be overridden.
// Overriding (redefining) a BaseFlavor method may be
// accomplished through the addition of a matching method
// signature and implementation on the SQLiteFlavor
// struct-type.
type SQLiteFlavor struct {
BaseFlavor
//================================================================
// possible local SQLite-specific overrides
//================================================================
// GetDBDriverName() string
// CreateTables(i ...interface{}) error
// DropTables(i ...interface{}) error
// AlterTables(i ...interface{}) error
// ExistsTable(i interface{}) bool
// ExistsColumn(tn string, cn string, ct string) bool
// CreateIndex(tn string, in string) error
// DropIndex(tn string, in string) error
// ExistsIndex(tn string, in string) bool
// CreateSequence(sn string, start string) error
// DropSequence(sn string) error
// ExistsSequence(sn string) bool
}
// GetDBName returns the name of the currently connected db
func (slf *SQLiteFlavor) GetDBName() (dbName string) {
dbNum := ""
dbMain := ""
qs := "PRAGMA database_list;"
slf.QsLog(qs)
row := slf.db.QueryRow(qs)
if row != nil {
err := row.Scan(&dbNum, &dbMain, &dbName)
if err != nil {
panic(err)
}
}
return dbName
}
// CreateTables creates tables on the sqlite3 database referenced
// by slf.DB.
func (slf *SQLiteFlavor) CreateTables(i ...interface{}) error {
for t, ent := range i {
ftr := reflect.TypeOf(ent)
if slf.log {
log.Println("CreateTable() entity type:", ftr)
}
// determine the table name
tn := common.GetTableName(i[t])
if tn == "" {
return fmt.Errorf("unable to determine table name in slf.CreateTables")
}
// if the table is found to exist, skip the creation
// and move on to the next table in the list.
if slf.ExistsTable(tn) {
if slf.log {
log.Printf("CreateTable - table %s exists - skipping...\n", tn)
}
continue
}
// get all the table parts and build the create schema
tc := slf.buildTablSchema(tn, i[t], false)
slf.QsLog(tc.tblSchema)
// execute the create schema against the db
slf.db.MustExec(tc.tblSchema)
for _, sq := range tc.seq {
start, _ := strconv.Atoi(sq.Value)
slf.AlterSequenceStart(sq.Name, start-1)
}
for k, in := range tc.ind {
slf.CreateIndex(k, in)
}
}
return nil
}
// AlterTables alters tables on the SQLite database referenced
// by slf.DB.
func (slf *SQLiteFlavor) AlterTables(i ...interface{}) error {
for t, ent := range i {
// determine the table name
tn := common.GetTableName(i[t])
if tn == "" {
return fmt.Errorf("unable to determine table name in slf.AlterTables")
}
// if the table does not exist, call CreateTables
// if the table does exist, examine it and perform
// alterations if necessary
if !slf.ExistsTable(tn) {
slf.CreateTables(ent)
continue
}
// build the altered table schema and get its components
tc := slf.buildTablSchema(tn, i[t], true)
// go through the latest version of the model and check each
// field against its definition in the database.
var cols []string
for _, fd := range tc.flDef {
// new columns first
if !slf.ExistsColumn(tn, fd.FName) && fd.NoDB == false {
colSchema := "ALTER TABLE " + tn + " ADD COLUMN " + fd.FName + " " + fd.FType
for _, p := range fd.SqacPairs {
switch p.Name {
case "primary_key":
// abort - adding primary key
panic(fmt.Errorf("aborting - cannot add a primary-key (table-field %s-%s) through migration", tn, fd.FName))
case "default":
if fd.UnderGoType == "string" {
colSchema = colSchema + " DEFAULT '" + p.Value + "'"
} else {
colSchema = colSchema + " DEFAULT " + p.Value
}
case "nullable":
if p.Value == "false" {
colSchema = colSchema + " DEFAULT " + p.Value
}
default:
}
}
cols = append(cols, colSchema+";")
}
}
// ALTER TABLE ADD COLUMN ...
if len(cols) > 0 {
for _, c := range cols {
if slf.IsLog() {
log.Println(c)
}
slf.ProcessSchema(c)
}
}
// add indexes if required
for k, v := range tc.ind {
if !slf.ExistsIndex(v.TableName, k) {
slf.CreateIndex(k, v)
}
}
// add foreign-keys if required - this is quite intensive, as the table
// will go through copy, drop, recreate, reload cycle for each foreign-
// key. it would be possible to react only to the first 'new' foreign-
// key in the list, as the entire model will be processed during an
// ADD CONSTRAINT ... FOREIGN KEY ... ...(..) operation.
for _, v := range tc.fkey {
fkn, err := common.GetFKeyName(ent, v.FromTable, v.RefTable, v.FromField, v.RefField)
if err != nil {
return err
}
fkExists, _ := slf.ExistsForeignKeyByName(ent, fkn)
if !fkExists {
err = slf.CreateForeignKey(ent, v.FromTable, v.RefTable, v.FromField, v.RefField)
if err != nil {
log.Println(err)
return err
}
}
}
}
return nil
}
// buildTableSchema builds a CREATE TABLE schema for the SQLite DB
// and returns it to the caller, along with the components determined from
// the db and sqac struct-tags. this method is used in CreateTables
// and AlterTables methods.
func (slf *SQLiteFlavor) buildTablSchema(tn string, ent interface{}, isAlter bool) TblComponents {
qt := slf.GetDBQuote()
pKeys := ""
var sequences []common.SqacPair
indexes := make(map[string]IndexInfo)
fKeys := make([]FKeyInfo, 0)
tableSchema := "CREATE TABLE IF NOT EXISTS " + qt + tn + qt + " ("
// get a list of the field names, go-types and db attributes.
// TagReader is a common function across db-flavors. For
// this reason, the db-specific-data-type for each field
// is determined locally.
fldef, err := common.TagReader(ent, nil)
if err != nil {
panic(err)
}
// set the SQLite field-types and build the table schema,
// as well as any other schemas that are needed to support
// the table definition. In all cases any foreign-key or
// index requirements must be deferred until all other
// artifacts have been created successfully.
// SQLite has basic types more along the lines of Postgres.
// support composite primary-keys (sort-of) by relying on the ROWID
// property of SQLite to autoincrement the one-and-only PRIMARY KEY field
// at time of INSERT. Twice as fast as AUTOINCREMENT. Use UNIQUE constraint
// and NOT NULL to artificially create the equivalient of a composite PRIMARY
// KEY.
// For example:
// CREATE TABLE IF NOT EXISTS "DoubleKey4" (
// "KeyOne" integer PRIMARY KEY,
// "KeyTwo" integer NOT NULL,
// "Description" VARCHAR(255),
// UNIQUE("KeyOne", "KeyTwo") );
//========================================================================================================
// DROP INDEX IF EXISTS idx_double_key4_new_column3;
// DROP TABLE IF EXISTS "DoubleKey4";
// CREATE TABLE IF NOT EXISTS "DoubleKey4" (
// "KeyOne" integer PRIMARY KEY AUTOINCREMENT,
// "KeyTwo" integer NOT NULL,
// "CreateDTUTC" datetime DEFAULT (datetime('now')),
// "ExpiryDT" datetime DEFAULT(datetime('now','+2 years')),
// "EOTDT" datetime DEFAULT('9999-12-31 23:59:59'),
// "Description" VARCHAR(255),
// "DefaultedText" VARCHAR(255) DEFAULT 'fiddlesticks',
// "DefaultedFloat" real NOT NULL DEFAULT 4.335,
// UNIQUE("KeyOne", "KeyTwo") );
// INSERT OR FAIL INTO "DoubleKey4" (KeyTwo, Description) VALUES ( 40,"Second Record");
// ALTER TABLE "DoubleKey4" ADD COLUMN "NewColumn2" bigint;
// ALTER TABLE "DoubleKey4" ADD COLUMN "NewColumn3" integer;
// ALTER TABLE "DoubleKey4" ADD COLUMN "NewColumn4" bool;
// ALTER TABLE "DoubleKey4" ADD COLUMN "NewColumn5" integer;
// CREATE UNIQUE INDEX idx_double_key4_new_column3 ON "DoubleKey4"("NewColumn2");
// CREATE INDEX idx_double_key4_new_column4_new_column5 ON "DoubleKey4"("NewColumn4, NewColumn5");
// SELECT * FROM sqlite_sequence WHERE "name" = "DoubleKey4";
// UPDATE "sqlite_sequence" SET "seq" = 50000000 WHERE "name" = "DoubleKey4";
// SELECT * FROM sqlite_sequence WHERE "name" = "DoubleKey4";
// SELECT * FROM "DoubleKey4";
//========================================================================================================
for idx, fd := range fldef {
var col ColComponents
col.fName = fd.FName
col.fType = ""
col.fPrimaryKey = ""
col.fDefault = ""
col.fNullable = ""
// date/time datetime - need now() function equivalent - also function to create a date-time from a string
// if the field has been marked as NoDB, continue with the next field
if fd.NoDB == true {
continue
}
switch fd.UnderGoType {
case "int64", "uint64":
col.fType = "bigint"
case "uint", "uint8", "uint16", "uint32", "int",
"int8", "int16", "int32", "rune", "byte":
col.fType = "integer"
case "float32", "float64":
col.fType = "real"
case "bool":
col.fType = "boolean"
case "string":
col.fType = "varchar(255)"
case "time.Time":
col.fType = "datetime"
default:
err := fmt.Errorf("go type %s is not presently supported", fldef[idx].FType)
panic(err)
}
fldef[idx].FType = col.fType
// read sqac tag pairs and apply
seqName := ""
if !strings.Contains(fd.GoType, "*time.Time") {
for _, p := range fd.SqacPairs {
switch p.Name {
case "primary_key":
pKeys = pKeys + " " + qt + fd.FName + qt + ","
// int-type primary keys will autoincrement based on ROWID,
// but the speed increase comes with the cost of losing control
// of the starting point of the range at time of table creation.
// addtionally, relying on the default keygen opens the door
// to the reuse of deleted keys - which is largely problematic
// (for me at least). for this reason, AUTOINCREMENT is used.
// if AUTOINCREMENT is requested on an int64/uint64, downcast
// the db-field-type to integer.
if p.Value == "inc" && strings.Contains(fd.UnderGoType, "int") {
if col.uType != "" {
log.Printf("WARNING: %s auto-incrementing primary-key field %s has user-specified db_type: %s user-type is ignored. \n", common.GetTableName(ent), col.fName, col.uType)
col.uType = ""
}
col.fPrimaryKey = "PRIMARY KEY"
if strings.Contains(fd.UnderGoType, "64") {
fldef[idx].FType = "integer"
col.fType = "integer"
}
col.fAutoInc = true
}
case "start":
start, err := strconv.Atoi(p.Value)
if err != nil {
panic(err)
}
if seqName == "" && start > 0 {
seqName = tn
sequences = append(sequences, common.SqacPair{Name: seqName, Value: p.Value})
}
case "default":
if fd.UnderGoType == "string" {
col.fDefault = "DEFAULT '" + p.Value + "'"
} else {
col.fDefault = "DEFAULT " + p.Value
}
if fd.UnderGoType == "bool" {
switch p.Value {
case "TRUE", "true":
p.Value = "1"
case "FALSE", "false":
p.Value = "0"
default:
}
col.fDefault = "DEFAULT " + p.Value
}
if fd.UnderGoType == "time.Time" {
switch p.Value {
case "now()":
p.Value = "(datetime('now'))"
case "eot()":
p.Value = "('9999-12-31 23:59:59')"
default:
}
col.fDefault = "DEFAULT " + p.Value
}
case "nullable":
if p.Value == "false" {
col.fNullable = "NOT NULL"
}
case "constraint":
if p.Value == "unique" {
col.fUniqueConstraint = "UNIQUE"
}
case "index":
switch p.Value {
case "non-unique":
indexes = slf.processIndexTag(indexes, tn, fd.FName, "idx_", false, true)
case "unique":
indexes = slf.processIndexTag(indexes, tn, fd.FName, "idx_", true, true)
default:
indexes = slf.processIndexTag(indexes, tn, fd.FName, p.Value, false, false)
}
case "fkey":
fKeys = slf.processFKeyTag(fKeys, tn, fd.FName, p.Value)
default:
}
}
} else { // *time.Time only supports default directive
for _, p := range fd.SqacPairs {
if p.Name == "default" {
switch p.Value {
case "now()":
p.Value = "(datetime('now'))"
case "eot()":
p.Value = "('9999-12-31 23:59:59')"
default:
}
col.fDefault = "DEFAULT " + p.Value
}
if p.Name == "primary_key" {
pKeys = pKeys + " " + qt + fd.FName + qt + ","
}
if p.Name == "fkey" {
fKeys = slf.processFKeyTag(fKeys, tn, fd.FName, p.Value)
}
}
}
fldef[idx].FType = col.fType
// add the current column to the schema
tableSchema = tableSchema + qt + col.fName + qt + " " + col.fType
if col.fPrimaryKey != "" {
tableSchema = tableSchema + " " + col.fPrimaryKey
}
if col.fAutoInc == true {
tableSchema = tableSchema + " AUTOINCREMENT"
}
if col.fNullable != "" {
tableSchema = tableSchema + " " + col.fNullable
}
if col.fDefault != "" {
tableSchema = tableSchema + " " + col.fDefault
}
if col.fUniqueConstraint != "" {
tableSchema = tableSchema + " " + col.fUniqueConstraint
}
tableSchema = tableSchema + ", "
}
if tableSchema != "" && pKeys == "" {
tableSchema = strings.TrimSpace(tableSchema)
tableSchema = strings.TrimSuffix(tableSchema, ",")
}
if tableSchema != "" && pKeys != "" {
pKeys = strings.TrimSuffix(pKeys, ",")
tableSchema = tableSchema + "UNIQUE(" + pKeys + ")"
}
// SQLite needs the foreign-key constraints added in the CREATE TABLE schema when
// building the schema for a CreateTables call. In an AlterTables call, it is not
// possible to add a new foreign-key to a SQLite table, so the foreign-key constraints
// are left out of the schema altogether. Existing foreign-keys should stay in-place
// and a list of foreign-keys to add is exported for use with the copy, drop, recreate,
// reload process used to add/drop foreign-keys.
if !isAlter {
if tableSchema != "" && len(fKeys) > 0 {
tableSchema = strings.TrimSpace(tableSchema)
lv := tableSchema[len(tableSchema)-1:]
if lv != "," {
tableSchema = tableSchema + ","
}
for _, v := range fKeys {
fkn, err := common.GetFKeyName(nil, v.FromTable, v.RefTable, v.FromField, v.RefField)
if err != nil {
log.Printf("WARNING: unable to determine foreign-key-name based on %v. SKIPPING.", v)
continue
}
tableSchema = tableSchema + " CONSTRAINT " + fkn + " FOREIGN KEY (" + v.FromField + ") REFERENCES " + v.RefTable + "(" + v.RefField + "),"
}
}
}
if tableSchema != "" {
tableSchema = strings.TrimSpace(tableSchema)
tableSchema = strings.TrimSuffix(tableSchema, ",")
tableSchema = tableSchema + ");"
}
// fill the return structure passing out the CREATE TABLE schema, and component info
rc := TblComponents{
tblSchema: tableSchema,
flDef: fldef,
seq: sequences,
ind: indexes,
pk: pKeys,
err: err,
}
if isAlter && len(fKeys) > 0 {
rc.fkey = fKeys
}
if slf.log {
rc.Log()
}
return rc
}
// DropTables drops tables on the SQLite db if they exist, based on
// the provided list of go struct definitions.
func (slf *SQLiteFlavor) DropTables(i ...interface{}) error {
dropSchema := ""
for t := range i {
// determine the table name
tn := common.GetTableName(i[t])
if tn == "" {
return fmt.Errorf("unable to determine table name in slf.DropTables")
}
// if the table is found to exist, add a DROP statement
// to the dropSchema string and move on to the next
// table in the list.
if slf.ExistsTable(tn) {
if slf.log {
log.Printf("table %s exists - adding to drop schema...\n", tn)
}
dropSchema = dropSchema + "DROP TABLE IF EXISTS " + tn + ";"
slf.ProcessSchema(dropSchema)
dropSchema = ""
}
}
return nil
}
// DestructiveResetTables drops tables on the SQLite db file if they exist,
// as well as any related objects such as sequences. this is
// useful if you wish to regenerated your table and the
// number-range used by an auto-incementing primary key.
func (slf *SQLiteFlavor) DestructiveResetTables(i ...interface{}) error {
err := slf.DropTables(i...)
if err != nil {
return err
}
err = slf.CreateTables(i...)
if err != nil {
return err
}
return nil
}
// ExistsTable checks that the specified table exists in the SQLite database file.
func (slf *SQLiteFlavor) ExistsTable(tn string) bool {
n := 0
reqQuery := "SELECT COUNT(*) FROM sqlite_master WHERE type=\"table\" AND name=\"" + tn + "\";"
slf.QsLog(reqQuery)
err := slf.db.QueryRow(reqQuery).Scan(&n)
if err != nil {
return false
}
if n == 0 {
return false
}
return true
}
// DropIndex drops the specfied index on the connected SQLite database. SQLite does
// not require the table name to drop an index, but it is provided in order to
// comply with the PublicDB interface definition.
func (slf *SQLiteFlavor) DropIndex(tn string, in string) error {
indexSchema := "DROP INDEX IF EXISTS " + in + ";"
slf.ProcessSchema(indexSchema)
return nil
}
// ExistsIndex checks the connected SQLite database for the presence
// of the specified index. This method is typically not required
// for SQLite, as the 'IF EXISTS' syntax is widely supported.
func (slf *SQLiteFlavor) ExistsIndex(tn string, in string) bool {
n := 0
indQuery := "SELECT COUNT(*) FROM sqlite_master WHERE \"type\" = \"index\" AND \"name\" = \"" + in + "\";"
slf.QsLog(indQuery)
slf.db.QueryRow(indQuery).Scan(&n)
if n > 0 {
return true
}
return false
}
// ExistsColumn checks the current SQLite database file and
// returns true if the named table-column is found to exist.
// this checks the column name only, not the column data-type
// or properties.
func (slf *SQLiteFlavor) ExistsColumn(tn string, cn string) bool {
if slf.ExistsTable(tn) {
// colQuery := fmt.Sprintf("PRAGMA table_info(\"%s\")", tn) // does not work - annoying
// without using the built-in PRAGMA, we have to rely on the table creation SQL
// that is stored in the sqlite_master table.
sqlString := ""
colQuery := "SELECT \"sql\" FROM sqlite_master WHERE \"type\" = \"table\" AND \"name\" = \"" + tn + "\""
slf.QsLog(colQuery)
slf.db.QueryRow(colQuery).Scan(&sqlString)
if sqlString == "" {
return false
}
if strings.Contains(sqlString, cn) {
return true
}
}
return false
}
// AlterSequenceStart may be used to make changes to the start value of the
// auto-increment field on the currently connected SQLite database file.
// This method is intended to be called at the time of table-creation, as
// updating the current value of the SQLite auto-increment may cause
// unanticipated difficulties if the target table already contains
// records.
func (slf *SQLiteFlavor) AlterSequenceStart(name string, start int) error {
asQuery := "UPDATE sqlite_sequence SET seq = " + strconv.Itoa(start) + " WHERE name = '" + name + "';"
slf.QsLog(asQuery)
result, err := slf.Exec(asQuery)
if err == nil {
ra, err := result.RowsAffected()
if err == nil && ra > 0 {
log.Println("ra==", ra)
return nil
}
}
asQuery = "INSERT INTO sqlite_sequence (name,seq) VALUES ('" + name + "', " + strconv.Itoa(start) + ");"
slf.QsLog(asQuery)
result, err = slf.Exec(asQuery)
if err != nil {
return err
}
ra, err := result.RowsAffected()
if err != nil || ra < 1 {
return err
}
return nil
}
// GetNextSequenceValue is used primarily for testing. It returns
// the current value of the SQLite auto-increment field for the named
// table.
func (slf *SQLiteFlavor) GetNextSequenceValue(name string) (int, error) {
seq := 0
if slf.ExistsTable(name) {
// colQuery := fmt.Sprintf("PRAGMA table_info(\"%s\")", tn) // does not work - annoying
// without using the built-in PRAGMA, we have to rely on the table creation SQL
// that is stored in the sqlite_master table - not very exact.
seqQuery := "SELECT \"seq\" FROM sqlite_sequence WHERE \"name\" = '" + name + "'"
slf.QsLog(seqQuery)
err := slf.db.QueryRow(seqQuery).Scan(&seq)
if err != nil {
return 0, err
}
return seq + 1, nil
}
return seq, nil
}
// CreateForeignKey creates a foreign key on an existing column in the database table
// specified by the i / ft parameter. SQLite does not support the addition of a
// foreign-key via ALTER TABLE, so the existing table has to be copied to a backup,
// and a new table created (hence parameter i) with the foreign-key constraint in
// the CREATE TABLE ... command. Foreign-key constraints are temporarily disabled
// on the db for the duration of the transaction processing.
// THIS SHOULD NOT BE CALLED DIRECTLY. IT IS FAR SAFER IN THE SQLITE CASE TO UPDATE
// THE SQAC-TAGS ON THE TABLE'S MODEL.
func (slf *SQLiteFlavor) CreateForeignKey(i interface{}, ft, rt, ff, rf string) error {
bakTn := ""
q := ""
// sql transaction command buffer
cmds := make([]string, 0)
// confirm the table name
tn := common.GetTableName(i)
if tn == "" || tn != ft {
return fmt.Errorf("unable to confirm table name in slf.CreateForeignKey")
}
// if the table is found to exist, copy it to a temp backup table - Sprintf for legibility
if slf.ExistsTable(tn) {
bakTn = fmt.Sprintf("_%s_bak", ft)
q = "DROP TABLE IF EXISTS " + bakTn + ";"
cmds = append(cmds, q)
q = fmt.Sprintf("ALTER TABLE %s RENAME TO _%s_bak;", ft, ft)
cmds = append(cmds, q)
q = "DROP TABLE IF EXISTS " + tn + ";"
cmds = append(cmds, q)
}
// determine the new fk constraint-name
fkn, err := common.GetFKeyName(i, ft, rt, ff, rf)
if err != nil {
return err
}
// build the new foreign-key constraint clause
fkc := fmt.Sprintf(" CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s(%s)", fkn, ff, rt, rf)
// build the new table schema with foreign-key constraint
tc := slf.buildTablSchema(tn, i, false)
q = strings.TrimSuffix(tc.tblSchema, ");")
q = strings.TrimSpace(q)
lv := q[len(q)-1:]
if lv != "," {
q = q + ","
}
q = fmt.Sprintf("%s%s%s", q, fkc, ");")
cmds = append(cmds, q)
// copy the data back - note that this can happen even if there is a
// foreign-key violation due to the prior PRAGMA. :)
if bakTn != "" {
q = "INSERT INTO " + ft + " SELECT * FROM " + bakTn + ";"
cmds = append(cmds, q)
// drop the backup table directly
q = "DROP TABLE IF EXISTS " + bakTn + ";"
cmds = append(cmds, q)
}
// disable foreign-key checks
_, err = slf.Exec("PRAGMA foreign_keys=off;")
if err != nil {
return err
}
// submit the transaction buffer
err = slf.ProcessTransaction(cmds)
if err != nil {
// attempt to reactivate foreign-key constraints
_, fkErr := slf.Exec("PRAGMA foreign_keys=on;")
if fkErr != nil {
log.Println("WARNING: FOREIGN KEY CONSTRAINTS ARE PRESENTLY DEACATIVATED!")
}
return err
}
// reactivate foreign-key constraints
_, err = slf.Exec("PRAGMA foreign_keys=on;")
if err != nil {
log.Println("WARNING: FOREIGN KEY CONSTRAINTS MAY PRESENTLY BE DEACATIVATED!")
return err
}
return nil
}
// DropForeignKey drops a foreign-key on an existing column. Since SQLite does not
// support the addition or deletion of foreign-key relationships on existing tables,
// the existing table is copied to a backup table, dropped and then recreated using
// the sqac model information contained in (i). It follows that in order for
// a foreign-key to be dropped, it must be removed from the sqac tag in the model
// definition.
func (slf *SQLiteFlavor) DropForeignKey(i interface{}, ft, fkn string) error {
bakTn := ""
q := ""
// sql transaction command buffer
cmds := make([]string, 0)
// confirm the table name
tn := common.GetTableName(i)
if tn == "" || tn != ft {
return fmt.Errorf("unable to confirm table name in slf.DropForeignKey")
}
// if the table is found to exist, copy it to a temp backup table
if slf.ExistsTable(tn) {
bakTn = fmt.Sprintf("_%s_bak", ft)
q = "DROP TABLE IF EXISTS " + bakTn + ";"
cmds = append(cmds, q)
q = fmt.Sprintf("ALTER TABLE %s RENAME TO _%s_bak;", ft, ft)
cmds = append(cmds, q)
q = "DROP TABLE IF EXISTS " + tn + ";"
cmds = append(cmds, q)
}
// build the new table schema without the foreign-key constraint (must be omitted from model)
tc := slf.buildTablSchema(tn, i, false)
cmds = append(cmds, tc.tblSchema)
// copy the data back
if bakTn != "" {
q = "INSERT INTO " + ft + " SELECT * FROM " + bakTn + ";"
cmds = append(cmds, q)
// drop the backup table directly
q = "DROP TABLE IF EXISTS " + bakTn + ";"
cmds = append(cmds, q)
}
// disable foreign-key checks to start the transaction processing
qs := "PRAGMA foreign_keys=off;"
slf.QsLog(qs)
_, err := slf.Exec(qs)
if err != nil {
return err
}
// submit the transaction buffer
err = slf.ProcessTransaction(cmds)
if err != nil {
// attempt to reactivate foreign-key constraints
_, fkErr := slf.Exec("PRAGMA foreign_keys=on;")
if fkErr != nil {
log.Println("WARNING: FOREIGN KEY CONSTRAINTS ARE PRESENTLY DEACATIVATED!")
}
return err
}
// reactivate foreign-key constraints
qs = "PRAGMA foreign_keys=on;"
slf.QsLog(qs)
_, err = slf.Exec(qs)
if err != nil {
log.Println("WARNING: FOREIGN KEY CONSTRAINTS MAY PRESENTLY BE DEACATIVATED!")
return err
}
return nil
}
// ExistsForeignKeyByName checks to see if the named foreign-key exists on the
// table corresponding to provided sqac model (i).
func (slf *SQLiteFlavor) ExistsForeignKeyByName(i interface{}, fkn string) (bool, error) {
var count uint64
tn := common.GetTableName(i)
fkQuery := fmt.Sprintf("SELECT COUNT(*) FROM sqlite_master WHERE tbl_name='%s' AND sql like'%%%s%%';", tn, fkn)
slf.QsLog(fkQuery)
err := slf.Get(&count, fkQuery)
if err != nil {
return false, nil
}
if count > 0 {
return true, nil
}
return false, nil
}
// ExistsForeignKeyByFields checks to see if a foreign-key exists between the named
// tables and fields.
func (slf *SQLiteFlavor) ExistsForeignKeyByFields(i interface{}, ft, rt, ff, rf string) (bool, error) {
fkn, err := common.GetFKeyName(i, ft, rt, ff, rf)
if err != nil {
return false, err
}
return slf.ExistsForeignKeyByName(i, fkn)
}
//================================================================
// CRUD ops
//================================================================
// Create the entity (single-row) on the database
func (slf *SQLiteFlavor) Create(ent interface{}) error {
var info CrudInfo
info.ent = ent
info.log = false
info.mode = "C"
err := slf.BuildComponents(&info)
if err != nil {
return err
}
// build the sqlite insert query
insFlds := ""
insVals := ""
for k, v := range info.fldMap {
if v == "DEFAULT" {
continue
}
insFlds = insFlds + " " + k + ", "
insVals = fmt.Sprintf("%s %s, ", insVals, v)
}
insFlds = strings.TrimSuffix(insFlds, ", ")
insVals = strings.TrimSuffix(insVals, ", ")
// build the sqlite insert query
insQuery := "INSERT OR FAIL INTO " + info.tn + " (" + insFlds + ") VALUES (" + insVals + ");"
slf.QsLog(insQuery)
// clear the source data - deals with non-persistet columns
e := reflect.ValueOf(info.ent).Elem()
e.Set(reflect.Zero(e.Type()))
// attempt the insert and read the result back into info.resultMap
result, err := slf.db.Exec(insQuery)
if err != nil {
return err
}
lastID, err := result.LastInsertId()
if err != nil {
return err
}
selQuery := "SELECT * FROM " + info.tn + " WHERE " + info.incKeyName + " = " + strconv.FormatInt(lastID, 10) + " LIMIT 1;"
slf.QsLog(selQuery)
err = slf.db.QueryRowx(selQuery).StructScan(info.ent) //.MapScan(info.resultMap) // SliceScan
if err != nil {
return err
}
info.entValue = reflect.ValueOf(info.ent)
return nil
}
// Update an existing entity (single-row) on the database
func (slf *SQLiteFlavor) Update(ent interface{}) error {
var info CrudInfo
info.ent = ent
info.log = false
info.mode = "U"
err := slf.BuildComponents(&info)
if err != nil {
return err
}
keyList := ""
for k, s := range info.keyMap {
fType := reflect.TypeOf(s).String()
if slf.IsLog() {
log.Printf("key: %v, value: %v\n", k, s)
log.Println("TYPE:", fType)
}
if fType == "string" { // also applies to time.Time at this point due to .Format()
keyList = fmt.Sprintf("%s %s = '%v' AND", keyList, k, s)
} else {
keyList = fmt.Sprintf("%s %s = %v AND", keyList, k, s)
}
// fmt.Printf("TypeOfKey: %v, keyName: %s\n", reflect.TypeOf(s), k)
}
keyList = strings.TrimSuffix(keyList, " AND")
colList := ""
for k, v := range info.fldMap {
colList = fmt.Sprintf("%s %s = %s, ", colList, k, v)
}
colList = strings.TrimSuffix(colList, ", ")
updQuery := "UPDATE OR FAIL " + info.tn + " SET " + colList + " WHERE " + keyList + ";"
slf.QsLog(updQuery)
// clear the source data - deals with non-persistent columns
e := reflect.ValueOf(info.ent).Elem()