-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres_flavor.go
957 lines (787 loc) · 25.8 KB
/
postgres_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
package sqac
import (
"fmt"
"log"
"reflect"
"strconv"
"strings"
"github.com/1414C/sqac/common"
)
// PostgresFlavor is a postgres-specific implementation.
// Methods defined in the PublicDB interface of struct-type
// BaseFlavor are called by default for PostgresFlavor. If
// the method as it exists in the BaseFlavor implementation
// is not compatible with the schema-syntax required by
// Postgres, 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 PostgresFlavor
// struct-type.
type PostgresFlavor struct {
BaseFlavor
//================================================================
// possible local Postgres-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 (pf *PostgresFlavor) GetDBName() (dbName string) {
// qs := "SELECT current_database();"
qs := "SELECT * FROM current_catalog;"
pf.QsLog(qs)
row := pf.db.QueryRow(qs)
if row != nil {
err := row.Scan(&dbName)
if err != nil {
panic(err)
}
}
return dbName
}
// createTables creates tables on the postgres database referenced
// by pf.DB. This internally visible version is able to defer
// foreign-key creation if called with calledFromAlter = true.
func (pf *PostgresFlavor) createTables(calledFromAlter bool, i ...interface{}) ([]ForeignKeyBuffer, error) {
var tc TblComponents
fkBuffer := make([]ForeignKeyBuffer, 0)
// get the list of table Model{}s
di := i[0].([]interface{})
for t, ent := range di {
ftr := reflect.TypeOf(ent)
if pf.log {
log.Println("CreateTable() entity type:", ftr)
}
// determine the table name
tn := common.GetTableName(di[t])
if tn == "" {
return nil, fmt.Errorf("unable to determine table name in pf.createTables")
}
// if the table is found to exist, skip the creation
// and move on to the next table in the list.
if pf.ExistsTable(tn) {
if pf.log {
log.Printf("createTable - table %s exists - skipping...\n", tn)
}
continue
}
// build the create table schema and return all of the table info
tc = pf.buildTablSchema(tn, di[t])
pf.QsLog(tc.tblSchema)
// create the table on the db
pf.db.MustExec(tc.tblSchema)
for _, sq := range tc.seq {
start, _ := strconv.Atoi(sq.Value)
pf.AlterSequenceStart(sq.Name, start)
}
// create the table indices
for k, in := range tc.ind {
pf.CreateIndex(k, in)
}
// add foreign-key information to the buffer
for _, v := range tc.fkey {
fkv := ForeignKeyBuffer{
ent: ent,
fkinfo: v,
}
fkBuffer = append(fkBuffer, fkv)
}
}
// create the foreign-keys if any and if flag 'calledFromAlter = false'
// attempt to create the foreign-key, but maybe do not hit a hard-fail
// if FK creation fails. When called from within AlterTable, creation
// of new tables in the list is carried out first - by this method. It
// is possbile that a column required by for new foreign-key has yet to
// be added to one of the tables pending alteration. A soft failure
// for FK creation issues seems approriate here, and the data for the
// failed FK creation is added to the fkBuffer and passed back to the
// called (AlterTable), where the FK creation can be tried again
// following the completion of the table alterations.
if calledFromAlter == false {
for _, v := range fkBuffer {
err := pf.CreateForeignKey(v.ent, v.fkinfo.FromTable, v.fkinfo.RefTable, v.fkinfo.FromField, v.fkinfo.RefField)
if err != nil {
log.Printf("CreateForeignKey failed. got: %v", err)
return nil, err
}
}
} else {
return fkBuffer, nil // fkBuffer will always be !nil, but may be len==0
}
return nil, nil
}
// buildTableSchema builds a CREATE TABLE schema for the Postgres 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 (pf *PostgresFlavor) buildTablSchema(tn string, ent interface{}) TblComponents {
pKeys := ""
var sequences []common.SqacPair
indexes := make(map[string]IndexInfo)
fKeys := make([]FKeyInfo, 0)
tableSchema := "CREATE TABLE " + tn + " ("
// 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 Postgres 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.
for idx, fd := range fldef {
var col ColComponents
col.fName = fd.FName
col.fType = ""
col.fPrimaryKey = ""
col.fDefault = ""
col.fNullable = ""
// if the field has been marked as NoDB, continue with the next field
if fd.NoDB == true {
continue
}
switch fd.UnderGoType { // fd.GoType {
case "uint", "uint8", "uint16", "uint32", "uint64",
"int", "int8", "int16", "int32", "int64", "rune", "byte":
if strings.Contains(fd.UnderGoType, "64") {
col.fType = "bigint"
} else {
col.fType = "integer"
}
// read sqac tag pairs and apply
seqName := ""
for _, p := range fd.SqacPairs {
switch p.Name {
case "primary_key":
col.fPrimaryKey = "PRIMARY KEY"
pKeys = pKeys + fd.FName + ","
if p.Value == "inc" {
// warn that user-specified db_type type will be ignored
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 = ""
}
if strings.Contains(fd.UnderGoType, "64") {
col.fType = "bigserial"
} else {
col.fType = "serial"
}
}
case "start":
start, err := strconv.Atoi(p.Value)
if err != nil {
panic(err)
}
if seqName == "" && start > 0 {
seqName = tn + "_" + fd.FName + "_seq"
sequences = append(sequences, common.SqacPair{Name: seqName, Value: p.Value})
}
// case "type":
// col.uType = p.Value
case "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 = pf.processIndexTag(indexes, tn, fd.FName, "idx_", false, true)
case "unique":
indexes = pf.processIndexTag(indexes, tn, fd.FName, "idx_", true, true)
default:
indexes = pf.processIndexTag(indexes, tn, fd.FName, p.Value, false, false)
}
case "fkey":
fKeys = pf.processFKeyTag(fKeys, tn, fd.FName, p.Value)
default:
}
}
fldef[idx].FType = col.fType
case "string":
col.fType = "text"
for _, p := range fd.SqacPairs {
switch p.Name {
case "primary_key":
col.fPrimaryKey = "PRIMARY KEY"
pKeys = pKeys + fd.FName + ","
case "type":
col.uType = p.Value
case "nullable":
if p.Value == "false" {
col.fNullable = "NOT NULL"
}
case "default":
col.fDefault = "DEFAULT '" + p.Value + "'"
case "constraint":
if p.Value == "unique" {
col.fUniqueConstraint = "UNIQUE"
}
case "index":
switch p.Value {
case "non-unique":
indexes = pf.processIndexTag(indexes, tn, fd.FName, "idx_", false, true)
case "unique":
indexes = pf.processIndexTag(indexes, tn, fd.FName, "idx_", true, true)
default:
indexes = pf.processIndexTag(indexes, tn, fd.FName, p.Value, false, false)
}
case "fkey":
fKeys = pf.processFKeyTag(fKeys, tn, fd.FName, p.Value)
default:
}
}
fldef[idx].FType = col.fType
case "float32", "float64":
col.fType = "numeric"
for _, p := range fd.SqacPairs {
switch p.Name {
case "primary_key":
col.fPrimaryKey = "PRIMARY KEY"
pKeys = pKeys + fd.FName + ","
case "nullable":
if p.Value == "false" {
col.fNullable = "NOT NULL"
}
case "default":
col.fDefault = "DEFAULT '" + p.Value + "'"
case "constraint":
if p.Value == "unique" {
col.fUniqueConstraint = "UNIQUE"
}
case "index":
switch p.Value {
case "non-unique":
indexes = pf.processIndexTag(indexes, tn, fd.FName, "idx_", false, true)
case "unique":
indexes = pf.processIndexTag(indexes, tn, fd.FName, "idx_", true, true)
default:
indexes = pf.processIndexTag(indexes, tn, fd.FName, p.Value, false, false)
}
case "fkey":
fKeys = pf.processFKeyTag(fKeys, tn, fd.FName, p.Value)
default:
}
}
fldef[idx].FType = col.fType
case "bool":
col.fType = "boolean"
for _, p := range fd.SqacPairs {
switch p.Name {
case "primary_key":
pKeys = pKeys + fd.FName + ","
case "default":
col.fDefault = "DEFAULT " + p.Value
case "nullable":
if p.Value == "false" {
col.fNullable = "NOT NULL"
}
case "index":
switch p.Value {
case "non-unique":
indexes = pf.processIndexTag(indexes, tn, fd.FName, "idx_", false, true)
case "unique":
indexes = pf.processIndexTag(indexes, tn, fd.FName, "idx_", true, true)
default:
indexes = pf.processIndexTag(indexes, tn, fd.FName, p.Value, false, false)
}
case "fkey":
fKeys = pf.processFKeyTag(fKeys, tn, fd.FName, p.Value)
default:
}
}
fldef[idx].FType = col.fType
case "time.Time":
col.fType = "timestamp with time zone"
for _, p := range fd.SqacPairs {
switch p.Name {
case "primary_key":
col.fPrimaryKey = "PRIMARY KEY"
pKeys = pKeys + fd.FName + ","
case "default":
if p.Value != "eot()" {
col.fDefault = "DEFAULT " + p.Value
} else {
col.fDefault = "DEFAULT " + "make_timestamptz(9999, 12, 31, 23, 59, 59.9)"
}
case "index":
switch p.Value {
case "non-unique":
indexes = pf.processIndexTag(indexes, tn, fd.FName, "idx_", false, true)
case "unique":
indexes = pf.processIndexTag(indexes, tn, fd.FName, "idx_", true, true)
default:
indexes = pf.processIndexTag(indexes, tn, fd.FName, p.Value, false, false)
}
case "fkey":
fKeys = pf.processFKeyTag(fKeys, tn, fd.FName, p.Value)
default:
}
}
fldef[idx].FType = col.fType
// this is always nullable, and consequently the following are
// not supported default value, use as a primary key, use as an index.
case "*time.Time":
col.fType = "timestamp with time zone"
for _, p := range fd.SqacPairs {
switch p.Name {
case "default":
if p.Value != "eot()" {
col.fDefault = "DEFAULT " + p.Value
} else {
col.fDefault = "DEFAULT " + "make_timestamptz(9999, 12, 31, 23, 59, 59.9)"
}
case "fkey":
fKeys = pf.processFKeyTag(fKeys, tn, fd.FName, p.Value)
default:
// do nothing with other tag directives
}
}
default:
err := fmt.Errorf("go type %s is not presently supported", fldef[idx].FType)
panic(err)
}
// add the current column to the schema
if col.uType != "" {
tableSchema = tableSchema + col.fName + " " + col.uType
} else {
tableSchema = tableSchema + col.fName + " " + col.fType
}
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, ",")
tableSchema = tableSchema + ");"
}
if tableSchema != "" && pKeys != "" {
pKeys = strings.TrimSuffix(pKeys, ",")
tableSchema = tableSchema + "CONSTRAINT " + strings.ToLower(tn) + "_pkey PRIMARY KEY (" + pKeys + ") );"
}
// fill the return structure passing out the CREATE TABLE schema, and component info
rc := TblComponents{
tblSchema: tableSchema,
flDef: fldef,
seq: sequences,
ind: indexes,
fkey: fKeys,
pk: pKeys,
err: err,
}
if pf.log {
rc.Log()
}
return rc
}
// CreateTables creates tables on the postgres database referenced
// by pf.DB.
func (pf *PostgresFlavor) CreateTables(i ...interface{}) error {
// call createTables specifying that the call has not originated
// from within the AlterTables(...) method.
_, err := pf.createTables(false, i)
if err != nil {
return err
}
return nil
}
// DropTables drops tables on the postgres database referenced
// by pf.DB.
func (pf *PostgresFlavor) 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 pf.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 pf.ExistsTable(tn) {
if pf.log {
log.Printf("table %s exists - adding to drop schema...\n", tn)
}
dropSchema = dropSchema + "DROP TABLE IF EXISTS " + tn + ";"
}
}
if dropSchema != "" {
pf.ProcessSchema(dropSchema)
}
return nil
}
// AlterTables alters tables on the Postgres database referenced
// by pf.DB.
func (pf *PostgresFlavor) AlterTables(i ...interface{}) error {
var err error
fkBuffer := make([]ForeignKeyBuffer, 0)
ci := make([]interface{}, 0)
ai := make([]interface{}, 0)
// construct create-table and alter-table buffers
for t := range i {
// determine the table name
tn := common.GetTableName(i[t])
if tn == "" {
return fmt.Errorf("unable to determine table name in pf.AlterTables")
}
// if the table does not exist, add the Model{} definition to
// the CreateTables buffer (ci).
// if the table does exist, add the Model{} definition to the
// AlterTables buffer (ai).
if !pf.ExistsTable(tn) {
ci = append(ci, i[t])
} else {
ai = append(ai, i[t])
}
}
// if create-tables buffer 'ci' contains any entries, call createTables and
// take note of any returned foreign-key definitions.
if len(ci) > 0 {
fkBuffer, err = pf.createTables(true, ci)
if err != nil {
return err
}
}
// if alter-tables buffer 'ai' constains any entries, process the table
// deltas and take note of any new foreign-key definitions.
for t, ent := range ai {
// determine the table name
tn := common.GetTableName(ai[t])
if tn == "" {
return fmt.Errorf("unable to determine table name in pf.AlterTables")
}
// build the alter-table schema and get its components
tc := pf.buildTablSchema(tn, ai[t])
// go through the latest version of the model and check each
// field against its definition in the database.
alterSchema := "ALTER TABLE IF EXISTS " + tn
var cols []string
for _, fd := range tc.flDef {
// new columns first
if !pf.ExistsColumn(tn, fd.FName) && fd.NoDB == false {
colSchema := "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 + " NOT NULL"
}
default:
}
}
cols = append(cols, colSchema+",")
}
}
// ALTER TABLE ADD COLUMNS...
if len(cols) > 0 {
for _, c := range cols {
alterSchema = alterSchema + " " + c
}
alterSchema = strings.TrimSuffix(alterSchema, ",")
pf.ProcessSchema(alterSchema)
}
// add indexes if required
for k, v := range tc.ind {
if !pf.ExistsIndex(v.TableName, k) {
pf.CreateIndex(k, v)
}
}
// add to the list of foreign-keys
for _, v := range tc.fkey {
fkb := ForeignKeyBuffer{
ent: ent,
fkinfo: v,
}
fkBuffer = append(fkBuffer, fkb)
}
}
// all table alterations and creations have been completed at this point, with the
// exception of the foreign-key creations. iterate over the fkBuffer, check for
// the existence of each foreign-key and create those that do not yet exist.
for _, v := range fkBuffer {
fkn, err := common.GetFKeyName(v.ent, v.fkinfo.FromTable, v.fkinfo.RefTable, v.fkinfo.FromField, v.fkinfo.RefField)
if err != nil {
return err
}
fkExists, _ := pf.ExistsForeignKeyByName(v.ent, fkn)
if !fkExists {
err = pf.CreateForeignKey(v.ent, v.fkinfo.FromTable, v.fkinfo.RefTable, v.fkinfo.FromField, v.fkinfo.RefField)
if err != nil {
return err
}
}
}
return nil
}
// DestructiveResetTables drops tables on the db 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 (pf *PostgresFlavor) DestructiveResetTables(i ...interface{}) error {
err := pf.DropTables(i...)
if err != nil {
return err
}
err = pf.CreateTables(i...)
if err != nil {
return err
}
return nil
}
// ExistsTable checks the public schema of the connected Postgres
// DB for the existence of the provided table name. Note that
// the use of to_regclass(<obj_name>) checks for the existence of
// *any* object in the public schema that has that name. If obj/name
// consistency is maintained, this approach is fine.
func (pf *PostgresFlavor) ExistsTable(tn string) bool {
reqQuery := "SELECT to_regclass('public." + tn + "');"
pf.QsLog(reqQuery)
rows, err := pf.db.Query(reqQuery)
if err != nil {
panic(err)
}
defer rows.Close()
var s string
for rows.Next() {
err = rows.Scan(&s)
if err != nil {
return false
}
return true
}
return false
}
// ExistsColumn checks for the existence of the specified
// table-column checking for the column name. this is
// rather incomplete, but in many cases where there is
// a type-discrepancy it is necessary to drop and recreate
// the column - or the entire table if a key is involved.
// consider also that pg requies autoincrement fields to
// be specified as 'serial' or 'bigserial', but then goes
// on to report them as 'integer' in the actual db-scema.
func (pf *PostgresFlavor) ExistsColumn(tn string, cn string) bool {
n := 0
pf.QsLog("SELECT count(*) FROM INFORMATION_SCHEMA.columns WHERE table_name = ? AND column_name = ? AND table_schema = CURRENT_SCHEMA()", tn, cn)
row := pf.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.columns WHERE table_name = $1 AND column_name = $2 AND table_schema = CURRENT_SCHEMA()", tn, cn)
if row != nil {
row.Scan(&n)
if n > 0 {
return true
}
}
return false
}
// ExistsIndex checks the connected Postgres database for the presence
// of the specified index - assuming that the index-type has not
// been adjusted...
func (pf *PostgresFlavor) ExistsIndex(tn string, in string) bool {
n := 0
pf.QsLog("SELECT count(*) FROM pg_indexes WHERE tablename = ? AND indexname = ? AND schemaname = CURRENT_SCHEMA()", tn, in)
err := pf.db.QueryRow("SELECT count(*) FROM pg_indexes WHERE tablename = $1 AND indexname = $2 AND schemaname = CURRENT_SCHEMA()", tn, in).Scan(&n)
if err != nil {
return false
}
if n > 0 {
return true
}
return false
}
// DropIndex drops the specfied index on the connected Postgres database.
// tn is ignored for Postgres.
func (pf *PostgresFlavor) DropIndex(tn string, in string) error {
indexSchema := "DROP INDEX IF EXISTS " + in + ";"
pf.ProcessSchema(indexSchema)
return nil
}
// ExistsSequence checks the public schema of the connected Postgres
// DB for the existence of the provided sequence name.
func (pf *PostgresFlavor) ExistsSequence(sn string) bool {
var params []interface{}
reqQuery := "SELECT relname FROM pg_class WHERE relkind = 'S' AND relname::name = $1"
params = append(params, sn)
pf.QsLog(reqQuery, params...)
rows, err := pf.db.Query(reqQuery, params...)
if err != nil {
panic(err)
}
defer rows.Close()
var s string
for rows.Next() {
err = rows.Scan(&s)
if err != nil {
return false
}
return true
}
return false
}
// CreateSequence creates the required sequence on the connected Postgres
// database in the public schema. Panics on error.
func (pf *PostgresFlavor) CreateSequence(sn string, start int) {
seqSchema := "CREATE SEQUENCE " + sn + " START " + strconv.Itoa(start) + ";"
pf.ProcessSchema(seqSchema)
}
// AlterSequenceStart adjusts the starting value of the named sequence. This should
// be called very carefully, preferably only at the time that the table/sequence is
// created on the db. There are no safeguards here.
func (pf *PostgresFlavor) AlterSequenceStart(sn string, start int) error {
seqSchema := "ALTER SEQUENCE IF EXISTS " + sn + " RESTART WITH " + strconv.Itoa(start) + ";"
pf.ProcessSchema(seqSchema)
return nil
}
// GetNextSequenceValue is used primarily for testing. It returns
// the current value of the sequence assigned to the primary-key of the
// of the named Postgres table. Although it is possible to assign
// Postgres sequences to non-primary-key fields (composite key gen),
// sqac handle auto-increment as a primary-key constraint only.
func (pf *PostgresFlavor) GetNextSequenceValue(name string) (int, error) {
// determine the column name of the primary key
pKeyQuery := "SELECT c.column_name, c.ordinal_position FROM information_schema.key_column_usage AS c LEFT JOIN information_schema.table_constraints AS t ON t.constraint_name = c.constraint_name WHERE t.table_name = '" + name + "' AND t.constraint_type = 'PRIMARY KEY';"
var keyColumn string
var keyColumnPos int
pf.QsLog(pKeyQuery)
pf.db.QueryRow(pKeyQuery).Scan(&keyColumn, &keyColumnPos)
if keyColumn == "" {
return 0, fmt.Errorf("could not identify primary-key column for table %s", name)
}
// Postgres sequences have format '<tablename>_<keyColumn>_seq'
seqName := name + "_" + keyColumn + "_seq"
if pf.ExistsSequence(seqName) {
seq := 0
seqQuery := "SELECT nextval('" + seqName + "');"
pf.QsLog(seqQuery)
err := pf.db.QueryRow(seqQuery).Scan(&seq)
if err != nil {
return 0, err
}
return seq, nil
}
return 0, nil
}
// ExistsForeignKeyByName checks to see if the named foreign-key exists on the
// table corresponding to provided sqac model (i).
func (pf *PostgresFlavor) ExistsForeignKeyByName(i interface{}, fkn string) (bool, error) {
var count uint64
tn := common.GetTableName(i)
fkQuery := "SELECT COUNT(*) FROM information_schema.table_constraints WHERE constraint_name='" + fkn + "' AND table_name='" + tn + "';"
pf.QsLog(fkQuery)
err := pf.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 (pf *PostgresFlavor) 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 pf.ExistsForeignKeyByName(i, fkn)
}
//================================================================
// CRUD ops
//================================================================
// Create the entity (single-row) on the database
func (pf *PostgresFlavor) Create(ent interface{}) error {
var info CrudInfo
info.ent = ent
info.log = false
info.mode = "C"
err := pf.BuildComponents(&info)
if err != nil {
return err
}
// build the postgres insert query
insQuery := "INSERT INTO " + info.tn + info.fList + " VALUES " + info.vList + " RETURNING *;"
pf.QsLog(insQuery)
// clear the source data - deals with non-persistent columns
e := reflect.ValueOf(info.ent).Elem()
e.Set(reflect.Zero(e.Type()))
// attempt the insert and read the result back into info.resultMap
err = pf.db.QueryRowx(insQuery).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 (pf *PostgresFlavor) Update(ent interface{}) error {
var info CrudInfo
info.ent = ent
info.log = false
info.mode = "U"
err := pf.BuildComponents(&info)
if err != nil {
return err
}
keyList := ""
for k, s := range info.keyMap {
fType := reflect.TypeOf(s).String()
if pf.IsLog() {
log.Printf("CRUD UPDATED key: %v, value: %v\n", k, s)
log.Println("CRUD UPDATED TYPE:", fType)
}
// leave as Sprintf
if fType == "string" {
keyList = fmt.Sprintf("%s %s = '%v' AND", keyList, k, s)
} else {
keyList = fmt.Sprintf("%s %s = %v AND", keyList, k, s)
}
}
keyList = strings.TrimSuffix(keyList, " AND")
keyList = keyList + " RETURNING *;"
updQuery := "UPDATE " + info.tn + " SET " + info.fList + " = " + info.vList + " WHERE" + keyList
pf.QsLog(updQuery)
// clear the source data - deals with non-persistet columns
e := reflect.ValueOf(info.ent).Elem()
e.Set(reflect.Zero(e.Type()))
// attempt the update and read result back into resultMap
err = pf.db.QueryRowx(updQuery).StructScan(info.ent) //.MapScan(info.resultMap) // SliceScan
if err != nil {
return err
}
info.entValue = reflect.ValueOf(info.ent)
return nil
}