-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_flavor.go
1431 lines (1197 loc) · 40 KB
/
base_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 (
"database/sql"
"fmt"
"log"
"reflect"
"strings"
"github.com/1414C/sqac/common"
"github.com/jmoiron/sqlx"
)
// IndexInfo contains index definitions as read from the sqac:"index" tags
type IndexInfo struct {
TableName string
Unique bool
IndexFields []string
}
// FKeyInfo holds foreign-key defs read from the sqac:"fkey" tags
// sqac:"fkey:ref_table(ref_field)"
type FKeyInfo struct {
FromTable string
FromField string
RefTable string
RefField string
FKeyName string
}
// ForeignKeyBuffer is used to hold deferred foreign-key information
// pending the creation of all tables submitted in a CreateTable(...)
// or AlterTable(...) call.
type ForeignKeyBuffer struct {
ent interface{}
fkinfo FKeyInfo
}
// ColComponents is used to capture the field properties from sqac: tags
// during table creation and table alteration activities.
type ColComponents struct {
fName string
fType string
uType string // user-specified DB type
fPrimaryKey string
fAutoInc bool // not used for Postgres
fStart int // only used for HDB
fDefault string
fUniqueConstraint string
fNullable string
}
// TblComponents is used as a collector structure for internal table
// create / alter processing.
type TblComponents struct {
tblSchema string
flDef []common.FieldDef
seq []common.SqacPair
ind map[string]IndexInfo
fkey []FKeyInfo
pk string
err error
}
// GetParam defines a common structure for CRUD GET parameters.
type GetParam struct {
FieldName string
Operand string
ParamValue interface{}
NextOperator string
}
// Log dumps all of the raw table components to stdout is called for CreateTable
// and AlterTable operations if the main sqac logging has been activated via
// BaseFlavor.Log(true).
func (tc *TblComponents) Log() {
log.Println("====================================================================")
log.Println("TABLE SCHEMA:", tc.tblSchema)
log.Println()
for _, v := range tc.seq {
log.Println("SEQUENCE:", v)
}
log.Println("--")
for k, v := range tc.ind {
log.Printf("INDEX: k:%s fields:%v unique:%v tableName:%s\n", k, v.IndexFields, v.Unique, v.TableName)
}
log.Println("--")
log.Println("PRIMARY KEYS:", tc.pk)
log.Println("--")
for _, v := range tc.flDef {
log.Printf("FIELD DEF: fname:%s, ftype:%s, gotype:%s ,nodb:%v\n", v.FName, v.FType, v.GoType, v.NoDB)
for _, p := range v.SqacPairs {
log.Printf("FIELD PROPERTY: %s, %v\n", p.Name, p.Value)
}
log.Println("------")
}
log.Println("--")
log.Println("ERROR:", tc.err)
log.Println("====================================================================")
}
// CTick CBackTick and CDblQuote specify the quote
// style for for db field encapsulation in CREATE
// and ALTER table schemas
const CTick = "'"
const CBackTick = "`"
const CDblQuote = "\""
// PublicDB exposes functions for db related operations.
type PublicDB interface {
// postgres, sqlite, mariadb, hdb, hana etc.
GetDBDriverName() string
// activate / check logging
Log(b bool)
IsLog() bool
DBLog(b bool)
IsDBLog() bool
// set the *sqlx.DB handle for the PublicDB interface
SetDB(db *sqlx.DB)
GetDB() *sqlx.DB
// GetDBName reports the name of the currently connected db for
// information_schema access. File-based databases like
// sqlite report the name as the absolute path to the location
// of their database file.
GetDBName() string
// GetDBQuote reports the quoting preference for db-query construction.
// ' vs ` vs " for example
GetDBQuote() string
// Close the db-connection
Close() error
// set / get the max idle sqlx db-connections and max open sqlx db-connections
SetMaxIdleConns(n int)
SetMaxOpenConns(n int)
GetRelations(tn string) []string
// i=db/sqac tagged go struct-type
CreateTables(i ...interface{}) error
DropTables(i ...interface{}) error
AlterTables(i ...interface{}) error
DestructiveResetTables(i ...interface{}) error
ExistsTable(tn string) bool
// tn=tableName, cn=columnName
ExistsColumn(tn string, cn string) bool
// tn=tableName, in=indexName
CreateIndex(in string, index IndexInfo) error
DropIndex(tn string, in string) error
ExistsIndex(tn string, in string) bool
// sn=sequenceName, start=start-value, name is used to hold
// the name of the sequence, autoincrement or identity
// field name. the use of name depends on which db system
// has been connected.
CreateSequence(sn string, start int)
AlterSequenceStart(name string, start int) error
GetNextSequenceValue(name string) (int, error)
// select pg_get_serial_sequence('public.some_table', 'some_column');
DropSequence(sn string) error
ExistsSequence(sn string) bool
// CreateForeignKey(Entity{}, foreignkeytable, reftable, fkfield, reffield)
// &Entity{} (i) is only needed for SQLite - okay to pass nil in other cases.
CreateForeignKey(i interface{}, ft, rt, ff, rf string) error
DropForeignKey(i interface{}, ft, fkn string) error
ExistsForeignKeyByName(i interface{}, fkn string) (bool, error)
ExistsForeignKeyByFields(i interface{}, ft, rt, ff, rf string) (bool, error)
// process DDL/DML commands
ProcessSchema(schema string)
ProcessSchemaList(sList []string)
ProcessTransaction(tList []string) error
// sql package access
ExecuteQueryRow(queryString string, qParams ...interface{}) *sql.Row
ExecuteQuery(queryString string, qParams ...interface{}) (*sql.Rows, error)
Exec(queryString string, args ...interface{}) (sql.Result, error)
// sqlx package access
ExecuteQueryRowx(queryString string, qParams ...interface{}) *sqlx.Row
ExecuteQueryx(queryString string, qParams ...interface{}) (*sqlx.Rows, error)
Get(dst interface{}, queryString string, args ...interface{}) error
Select(dst interface{}, queryString string, args ...interface{}) error
// Boolean conversions
BoolToDBBool(b bool) *int
DBBoolToBool(interface{}) bool
TimeToFormattedString(i interface{}) string
// CRUD ops
Create(ent interface{}) error
Update(ent interface{}) error
Delete(ent interface{}) error // (id uint) error
GetEntity(ent interface{}) error // pass ptr to type containing key information
GetEntities(ents interface{}) (interface{}, error)
GetEntities2(ge GetEnt) error
GetEntities4(ents interface{})
GetEntitiesCP(ents interface{}, pList []GetParam, cmdMap map[string]interface{}) (uint64, error)
GetEntitiesWithCommands(ents interface{}, pList []GetParam, cmdMap map[string]interface{}) (interface{}, error)
}
// ensure consistency of interface implementation
var _ PublicDB = &BaseFlavor{}
// BaseFlavor is a supporting struct for interface PublicDB
type BaseFlavor struct {
db *sqlx.DB
log bool
dbLog bool
PublicDB
}
// Log sets the logging status
func (bf *BaseFlavor) Log(b bool) {
bf.log = b
}
// IsLog reports whether logging is active
func (bf *BaseFlavor) IsLog() bool {
return bf.log
}
// DBLog sets the db-access-logging status
func (bf *BaseFlavor) DBLog(b bool) {
bf.dbLog = b
}
// IsDBLog reports whether db-access-logging is active
func (bf *BaseFlavor) IsDBLog() bool {
return bf.dbLog
}
// QsLog is used to log SQL statements to stdout. Statements are text approximations
// of what was sent to the database. For the most part they should be correct, but
// quoting around parameter values is rudimentary.
func (bf *BaseFlavor) QsLog(queryString string, qParams ...interface{}) {
if !bf.dbLog {
return
}
if qParams != nil {
for _, v := range qParams {
switch v.(type) {
case string:
rVal := ""
if bf.GetDBDriverName() == "sqlite3" {
rVal = "\"" + v.(string) + "\""
} else {
rVal = "'" + v.(string) + "'"
}
queryString = strings.Replace(queryString, "?", rVal, 1)
default:
queryString = strings.Replace(queryString, "?", "%v", 1)
queryString = fmt.Sprintf(reflect.ValueOf(queryString).String(), v)
// queryString = strings.Replace(queryString, "?", v.(string), 1)
}
}
return
}
log.Println(queryString)
}
// SetDB sets the sqlx.DB connection in the
// db-flavor environment.
func (bf *BaseFlavor) SetDB(db *sqlx.DB) {
bf.db = db
}
// GetDB returns a *sqlx.DB pointer if one has
// been set in the db-flavor environment.
func (bf *BaseFlavor) GetDB() *sqlx.DB {
return bf.db
}
// Close closes the db-connection
func (bf *BaseFlavor) Close() error {
err := bf.db.Close()
if err != nil {
log.Println("failed to close db connection")
return err
}
return nil
}
// GetDBName returns the name of the currently connected db
func (bf *BaseFlavor) GetDBName() (dbName string) {
row := bf.db.QueryRow("SELECT DATABASE()")
//row := bf.db.QueryRow("SELECT * FROM current_catalog;")
if row != nil {
err := row.Scan(&dbName)
if err != nil {
log.Println("unable to determine DBName!")
panic(err)
}
}
return dbName
}
// GetDBQuote reports the quoting preference for db-query construction.
// this does not refer to quoted strings, but rather to the older(?)
// style of quoting table field-names in query-strings such as:
// SELECT "f1" FROM "t1" WHERE "v1" = <some_criteria>.
// in practice, it seems you can get away without quoting, but
// it is a nod to backward compatibility for existing db installs.
// ' vs ` vs " for example
func (bf *BaseFlavor) GetDBQuote() string {
switch bf.GetDBDriverName() {
case "postgres":
return CTick
case "mysql":
return CBackTick
case "sqlite":
return CDblQuote
case "mssql":
return ""
case "hdb":
return ""
default:
return CDblQuote
}
}
// SetMaxIdleConns calls sqlx.SetMaxIdleConns
func (bf *BaseFlavor) SetMaxIdleConns(n int) {
bf.db.SetMaxIdleConns(n)
}
// SetMaxOpenConns calls sqlx.SetMaxOpenConns
func (bf *BaseFlavor) SetMaxOpenConns(n int) {
bf.db.SetMaxOpenConns(n)
}
// GetDBDriverName returns the name of the current db-driver
func (bf *BaseFlavor) GetDBDriverName() string {
return bf.db.DriverName()
}
// BoolToDBBool converts a go-bool value into the
// DB bool representation. called for DB's that
// do not support a true/false boolean type.
func (bf *BaseFlavor) BoolToDBBool(b bool) *int {
var r int
switch b {
case true:
r = 1
return &r
case false:
r = 0
return &r
default:
return nil
}
}
// DBBoolToBool converts from the DB representation
// of a bool into the go-bool type. The is called for
// DB's that do not support a true/false boolean type.
func (bf *BaseFlavor) DBBoolToBool(i interface{}) bool {
switch i.(type) {
case string:
if i.(string) == "TRUE" || i.(string) == "true" {
return true
}
return false
case int:
if i.(int) == 1 {
return true
}
return false
case int64:
if i.(int64) == 1 {
return true
}
return false
default:
return false
}
}
// GetRelations is designed to take a tablename and use it
// to determine a list of related objects. this is just an
// idea, and the functionality will reqiure more than the
// return of a []string.
func (bf *BaseFlavor) GetRelations(tn string) []string {
return nil
}
// CreateTables creates tables on the db based on
// the provided list of go struct definitions.
func (bf *BaseFlavor) CreateTables(i ...interface{}) error {
// handled in each db flavor
return fmt.Errorf("method CreateTables has not been implemented for %s", bf.GetDBDriverName())
}
// DropTables drops tables on the db if they exist, based on
// the provided list of go struct definitions.
func (bf *BaseFlavor) 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 bf.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 bf.ExistsTable(tn) {
if bf.log {
log.Printf("table %s exists - adding to drop schema...\n", tn)
}
// submit 1 at a time for mysql
dropSchema = dropSchema + "DROP TABLE " + tn + ";"
bf.ProcessSchema(dropSchema)
dropSchema = ""
}
}
return nil
}
// AlterTables alters tables on the db based on
// the provided list of go struct definitions.
func (bf *BaseFlavor) AlterTables(i ...interface{}) error {
return fmt.Errorf("method AlterTables has not been implemented for %s", bf.GetDBDriverName())
}
// 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 (bf *BaseFlavor) DestructiveResetTables(i ...interface{}) error {
return fmt.Errorf("method DestructiveResetTable has not been implemented for %s", bf.GetDBDriverName())
}
// ExistsTable checks the currently connected database and
// returns true if the named table is found to exist.
func (bf *BaseFlavor) ExistsTable(tn string) bool {
n := 0
qs := "SELECT count(*) FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = ? AND table_name = ?;"
dbName := bf.GetDBName()
bf.QsLog(qs, dbName)
bf.db.QueryRow(qs, dbName, tn).Scan(&n)
if n > 0 {
return true
}
return false
}
// ExistsColumn checks the currently connected database 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 (bf *BaseFlavor) ExistsColumn(tn string, cn string) bool {
n := 0
qs := "SELECT COUNT(*) FROM information_schema.COLUMNS WHERE table_schema = ? AND table_name = ? AND column_name = ?;"
dbName := bf.GetDBName()
if bf.ExistsTable(tn) {
bf.QsLog(qs, dbName, tn, cn)
bf.db.QueryRow(qs, dbName, tn, cn).Scan(&n)
if n > 0 {
return true
}
}
return false
}
// CreateIndex creates the index contained in the incoming
// IndexInfo structure. indexes will be created as non-unique
// by default, and in multi-field situations, the fields will
// added to the index in the order they are contained in the
// IndexInfo.[]IndexFields slice.
func (bf *BaseFlavor) CreateIndex(in string, index IndexInfo) error {
// CREATE INDEX idx_material_num_int_example ON `equipment`(material_num, int_example)
fList := ""
indexSchema := ""
if len(index.IndexFields) == 1 {
fList = index.IndexFields[0]
in = "idx_" + index.TableName + "_" + fList
} else {
for _, f := range index.IndexFields {
fList = fList + f + ", "
}
fList = strings.TrimSuffix(fList, ", ")
}
if !index.Unique {
indexSchema = "CREATE INDEX " + in + " ON " + index.TableName + " (" + fList + ");"
} else {
indexSchema = "CREATE UNIQUE INDEX " + in + " ON " + index.TableName + " (" + fList + ");"
}
bf.ProcessSchema(indexSchema)
return nil
}
// DropIndex drops the specfied index on the connected database.
func (bf *BaseFlavor) DropIndex(tn string, in string) error {
return fmt.Errorf("method DropIndex has not been implemented for %s", bf.GetDBDriverName())
}
// ExistsIndex checks the connected database for the presence
// of the specified index.
func (bf *BaseFlavor) ExistsIndex(tn string, in string) bool {
n := 0
qs := "SELECT count(*) FROM INFORMATION_SCHEMA.STATISTICS WHERE table_schema = ? AND table_name = ? AND index_name = ?"
dbName := bf.GetDBName()
bf.QsLog(qs, dbName, tn, in)
bf.db.QueryRow(qs, dbName, tn, in).Scan(&n)
if n > 0 {
return true
}
return false
}
// CreateSequence may be used to create a new sequence on the
// currently connected database.
func (bf *BaseFlavor) CreateSequence(sn string, start int) {
log.Printf("method CreateSequence has not been implemented for %s\n", bf.GetDBDriverName())
return
}
// AlterSequenceStart may be used to make changes to the start value
// of the named sequence, autoincrement or identity field depending
// on the manner in which the currently connected database flavour
// handles key generation.
func (bf *BaseFlavor) AlterSequenceStart(name string, start int) error {
return fmt.Errorf("AlterSequenceStart has not been implemented for %s", bf.GetDBName())
}
// DropSequence may be used to drop the named sequence on the currently
// connected database. This is probably not needed, as we are now
// creating sequences on postgres in a more correct manner.
// select pg_get_serial_sequence('public.some_table', 'some_column');
func (bf *BaseFlavor) DropSequence(sn string) error {
return fmt.Errorf("DropSequence has not been implemented for %s", bf.GetDBDriverName())
}
// ExistsSequence checks for the presence of the named sequence on
// the currently connected database.
func (bf *BaseFlavor) ExistsSequence(sn string) bool {
log.Printf("method ExistsSequence has not been implemented for %s\n", bf.GetDBDriverName())
return false
}
// GetNextSequenceValue returns the next value of the named or derived
// sequence, auto-increment or identity field depending on which
// db-system is presently being used.
func (bf *BaseFlavor) GetNextSequenceValue(name string) (int, error) {
return 0, fmt.Errorf("ExistsSequence has not been implemented for %s", bf.GetDBDriverName())
}
// CreateForeignKey creates a foreign-key on an existing column.
func (bf *BaseFlavor) CreateForeignKey(i interface{}, ft, rt, ff, rf string) error {
schema := "ALTER TABLE " + ft + " ADD CONSTRAINT " + "fk_" + ft + "_" + rt + "_" + rf + " FOREIGN KEY(" + ff + ")" + " REFERENCES " + rt + "(" + rf + ");"
bf.QsLog(schema)
_, err := bf.Exec(schema)
if err != nil {
return err
}
return nil
}
// DropForeignKey drops a foreign-key on an existing column
func (bf *BaseFlavor) DropForeignKey(i interface{}, ft, fkn string) error {
schema := "ALTER TABLE " + ft + " DROP CONSTRAINT " + fkn + ";"
bf.QsLog(schema)
_, err := bf.Exec(schema)
if err != nil {
return err
}
return nil
}
// ExistsForeignKeyByName checks to see if the named foreign-key exists on the
// table corresponding to provided sqac model (i).
func (bf *BaseFlavor) ExistsForeignKeyByName(i interface{}, fkn string) (bool, error) {
return false, fmt.Errorf("ExistsForeignKeyByName(...) has not been implemented for %s", bf.GetDBDriverName())
}
// ExistsForeignKeyByFields checks to see if a foreign-key exists between the named
// tables and fields.
func (bf *BaseFlavor) ExistsForeignKeyByFields(i interface{}, ft, rt, ff, rf string) (bool, error) {
return false, fmt.Errorf("ExistsForeignKeyByFields(...) has not been implemented for %s", bf.GetDBDriverName())
}
//===============================================================================
// SQL Schema Processing
//===============================================================================
// ProcessSchema processes the schema against the connected DB.
func (bf *BaseFlavor) ProcessSchema(schema string) {
// MustExec panics on error, so just call it
// bf.DB.MustExec(schema)
bf.QsLog(schema)
result, err := bf.db.Exec(schema)
if err != nil {
log.Println("ProcessSchema err:", err)
}
// not all db's support rows affected, so reading it is
// for interests sake only.
ra, err := result.RowsAffected()
if err != nil {
return
}
if bf.log {
fmt.Printf("%d rows affected.\n", ra)
}
}
// ProcessSchemaList processes the schemas contained in sList
// in the order in which they were provided. Schemas are
// executed against the connected DB.
// DEPRECATED: USER ProcessTransactionList
func (bf *BaseFlavor) ProcessSchemaList(sList []string) {
// bf.DB.MustExec(query string, args ...interface{})
return
}
//===============================================================================
// SQL Query Processing
//===============================================================================
// ExecuteQueryRow processes the single-row query contained in queryString
// against the connected DB using sql/database.
func (bf *BaseFlavor) ExecuteQueryRow(queryString string, qParams ...interface{}) *sql.Row {
if qParams != nil {
queryString = bf.db.Rebind(queryString)
bf.QsLog(queryString, qParams...)
return bf.db.QueryRow(queryString, qParams...)
}
bf.QsLog(queryString)
return bf.db.QueryRow(queryString)
}
// ExecuteQuery processes the multi-row query contained in queryString
// against the connected DB using sql/database.
func (bf *BaseFlavor) ExecuteQuery(queryString string, qParams ...interface{}) (*sql.Rows, error) {
var rows *sql.Rows
var err error
if qParams != nil {
queryString = bf.db.Rebind(queryString)
bf.QsLog(queryString, qParams...)
rows, err = bf.db.Query(queryString, qParams...)
} else {
bf.QsLog(queryString)
rows, err = bf.db.Query(queryString)
}
return rows, err
}
// ExecuteQueryRowx processes the single-row query contained in queryString
// against the connected DB using sqlx.
func (bf *BaseFlavor) ExecuteQueryRowx(queryString string, qParams ...interface{}) *sqlx.Row {
if qParams != nil {
queryString = bf.db.Rebind(queryString)
bf.QsLog(queryString, qParams...)
return bf.db.QueryRowx(queryString, qParams...)
}
bf.QsLog(queryString)
return bf.db.QueryRowx(queryString)
}
// ExecuteQueryx processes the multi-row query contained in queryString
// against the connected DB using sqlx.
func (bf *BaseFlavor) ExecuteQueryx(queryString string, qParams ...interface{}) (*sqlx.Rows, error) {
var rows *sqlx.Rows
var err error
if qParams != nil {
queryString = bf.db.Rebind(queryString)
bf.QsLog(queryString, qParams...)
rows, err = bf.db.Queryx(queryString, qParams...)
} else {
bf.QsLog(queryString)
rows, err = bf.db.Queryx(queryString)
}
return rows, err
}
// Get reads a single row into the dst interface.
// This calls sqlx.Get(...)
func (bf *BaseFlavor) Get(dst interface{}, queryString string, args ...interface{}) error {
if args != nil {
queryString = bf.db.Rebind(queryString)
bf.QsLog(queryString, args...)
return bf.db.Get(dst, queryString, args...)
}
bf.QsLog(queryString)
return bf.db.Get(dst, queryString)
}
// Select reads some rows into the dst interface.
// This calls sqlx.Select(...)
func (bf *BaseFlavor) Select(dst interface{}, queryString string, args ...interface{}) error {
if args != nil {
queryString = bf.db.Rebind(queryString)
bf.QsLog(queryString, args...)
return bf.db.Select(dst, queryString, args...)
}
bf.QsLog(queryString)
return bf.db.Select(dst, queryString)
}
// Exec runs the queryString against the connected db
func (bf *BaseFlavor) Exec(queryString string, args ...interface{}) (sql.Result, error) {
var result sql.Result
var err error
if args != nil {
bf.QsLog(queryString, args...)
queryString = bf.db.Rebind(queryString)
result, err = bf.db.Exec(queryString, args...)
} else {
bf.QsLog(queryString)
result, err = bf.db.Exec(queryString)
}
return result, err
}
// ProcessTransaction processes the list of commands as a transaction.
// If any of the commands encounter an error, the transaction will be
// cancelled via a Rollback and the error message will be returned to
// the caller. It is assumed that tList contains bound queryStrings.
func (bf *BaseFlavor) ProcessTransaction(tList []string) error {
// begin the transaction
tx, err := bf.db.Begin()
if err != nil {
return err
}
// execute each command in the transaction set
for _, s := range tList {
bf.QsLog(s)
_, err = tx.Exec(s, nil)
if err != nil {
tx.Rollback()
return err
}
}
// commit
err = tx.Commit()
if err != nil {
tx.Rollback()
return err
}
return nil
}
// processFKeyTag places the data for foreign-key creation in a slice that will be
// added to the tc (TblComponents) struct. Foreign-keys are always created after all
// tables in a Create / Alter set have been processed in order to provide the greatest
// chance that the corresponding ref-table/field exist. Could add the constraint name
// here...
func (bf *BaseFlavor) processFKeyTag(fkeys []FKeyInfo, ft, ff, rv string) []FKeyInfo {
tf := strings.Split(rv, "(")
if len(tf) != 2 {
// panic for now
panic(fmt.Sprintf("unable to parse foreign-key sqac tag: %v", rv))
}
rt := tf[0]
rf := tf[1]
rt = strings.TrimSpace(rt)
rf = strings.Replace(rf, ")", "", 2)
rf = strings.TrimSpace(rf)
fk := FKeyInfo{
FromTable: ft, // from-table
FromField: ff, // from-field
RefTable: rt, // ref-table
RefField: rf, // ref-field
}
return append(fkeys, fk)
}
// processIndexTag is used to create or add to an entry in the working indexes map that is
// being built in a CreateTable or AlterTable method.
func (bf *BaseFlavor) processIndexTag(iMap map[string]IndexInfo, tableName string, fieldName string,
indexName string, unique bool, singleField bool) map[string]IndexInfo {
var fldIndex IndexInfo
// single column index
if singleField {
fldIndex.TableName = tableName
fldIndex.IndexFields = append(fldIndex.IndexFields, fieldName)
if unique {
fldIndex.Unique = true
} else {
fldIndex.Unique = false
}
indexName = indexName + tableName + "_" + fieldName
iMap[indexName] = fldIndex
return iMap
}
// multi-column indexes where the index-name is in the map
fldIndex, ok := iMap[indexName]
if ok {
fldIndex.IndexFields = append(fldIndex.IndexFields, fieldName)
iMap[indexName] = fldIndex
return iMap
}
// add a multi-column index to the map
fldIndex.TableName = tableName
fldIndex.IndexFields = append(fldIndex.IndexFields, fieldName)
fldIndex.Unique = false
iMap[indexName] = fldIndex
return iMap
}
// Delete - CRUD Delete an existing entity (single-row) on the database using the full-key
func (bf *BaseFlavor) Delete(ent interface{}) error { // (id uint) error
var info CrudInfo
info.ent = ent
info.log = false
info.mode = "D"
err := bf.BuildComponents(&info)
if err != nil {
return err
}
keyList := ""
for k, s := range info.keyMap {
fType := reflect.TypeOf(s).String()
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")
delQuery := "DELETE FROM " + info.tn + " WHERE " + keyList + ";"
bf.QsLog(delQuery)
result, err := bf.db.Exec(delQuery)
if err != nil {
log.Println("CRUD Delete error:", err)
}
ra, err := result.RowsAffected()
if err != nil {
return err
}
if bf.log {
fmt.Printf("%d rows affected.\n", ra)
}
return nil
}
// GetEntity - CRUD GetEntity gets an existing entity from the db using the primary
// key definition. It is expected that ID will have been populated in the body by
// the caller.
func (bf *BaseFlavor) GetEntity(ent interface{}) error {
var info CrudInfo
info.ent = ent
info.log = false
info.mode = "G"
err := bf.BuildComponents(&info)
if err != nil {
return err
}
keyList := ""
for k, s := range info.keyMap {
fType := reflect.TypeOf(s).String()
if bf.IsLog() {
log.Printf("CRUD GET ENTITY key: %v, value: %v\n", k, s)
log.Println("CRUD GET ENTITY TYPE:", fType)
}
// not worth coding the detailed cases here - use Sprintf %v
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")
selQuery := "SELECT * FROM " + info.tn + " WHERE " + keyList + ";"
bf.QsLog(selQuery)
// attempt read the entity row
err := bf.db.QueryRowx(selQuery).StructScan(info.ent) //.MapScan(info.resultMap) // SliceScan
if err != nil {
return err
}
info.entValue = reflect.ValueOf(info.ent)
return nil
}
return nil
}
// GetEntities is experimental - use GetEntitiesWithCommands.
func (bf *BaseFlavor) GetEntities(ents interface{}) (interface{}, error) {
// get the underlying data type of the interface{}
entTypeElem := reflect.TypeOf(ents).Elem()
// fmt.Println("entTypeElem:", entTypeElem)
// create a struct from the type
testVar := reflect.New(entTypeElem)
// determine the db table name
tn := common.GetTableName(ents)
selQuery := "SELECT * FROM " + tn + ";"
bf.QsLog(selQuery)
// read the rows
rows, err := bf.db.Queryx(selQuery)
if err != nil {
log.Printf("GetEntities for table &s returned error: %v\n", err.Error())
return nil, err
}
defer rows.Close()
// iterate over the rows collection and put the results
// into the ents interface (slice)
entsv := reflect.ValueOf(ents)
for rows.Next() {
err = rows.StructScan(testVar.Interface())
if err != nil {
log.Println("GetEntities scan error:", err)
return nil, err
}