-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmssql_flavor.go
1151 lines (971 loc) · 30.2 KB
/
mssql_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"
"github.com/jmoiron/sqlx"
)
// MSSQLFlavor is a MSSQL-specific implementation.
// Methods defined in the PublicDB interface of struct-type
// BaseFlavor are called by default for MSSQLFlavor. If
// the method as it exists in the BaseFlavor implementation
// is not compatible with the schema-syntax required by
// MSSQL, 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 MSSQLFlavor
// struct-type.
type MSSQLFlavor struct {
BaseFlavor
//================================================================
// possible local MSSQL-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
}
// things to deal with:
// sqac:"primary_key:inc;start:55550000"
// sqac:"nullable:false"
// sqac:"default:0"
// sqac:"index:idx_material_num_serial_num
// sqac:"index:unique/non-unique"
// timestamp syntax and functions
// - pg now() equivalent
// - pg make_timestamptz(9999, 12, 31, 23, 59, 59.9) equivalent
// createTables creates tables on the mssql database referenced
// by msf.DB. This internally visible version is able to defer
// foreign-key creation if called with calledFromAlter = true.
func (msf *MSSQLFlavor) 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 msf.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 myf.CreateTables")
}
// if the table is found to exist, skip the creation
// and move on to the next table in the list.
if msf.ExistsTable(tn) {
if msf.log {
log.Printf("createTable - table %s exists - skipping...\n", tn)
}
continue
}
// build the create table schema and return all of the table info
tc = msf.buildTablSchema(tn, di[t])
msf.QsLog(tc.tblSchema)
// create the table on the db
msf.db.MustExec(tc.tblSchema)
for _, sq := range tc.seq {
start, _ := strconv.Atoi(sq.Value)
msf.AlterSequenceStart(sq.Name, start)
}
// create the table indices
for k, in := range tc.ind {
msf.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 := msf.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 MSSQL 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 (msf *MSSQLFlavor) buildTablSchema(tn string, ent interface{}) TblComponents {
qt := msf.GetDBQuote()
pKeys := ""
var sequences []common.SqacPair
indexes := make(map[string]IndexInfo)
fKeys := make([]FKeyInfo, 0)
tableSchema := "CREATE TABLE " + 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 MSSQL 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 {
case "int64", "uint64":
col.fType = "bigint"
case "int32", "uint32", "int", "uint":
col.fType = "int"
case "int16", "uint16":
col.fType = "smallint"
case "int8", "uint8", "byte", "rune":
col.fType = "tinyint"
case "float32", "float64":
col.fType = "numeric(38,7)" // default precision is 18
case "bool":
col.fType = "bit"
case "string":
col.fType = "varchar(255)" //
case "time.Time":
col.fType = "datetime2"
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":
col.fPrimaryKey = "PRIMARY KEY"
pKeys = pKeys + " " + qt + fd.FName + qt + ","
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 = ""
}
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 == "time.Time" {
switch p.Value {
case "now()":
p.Value = "GETDATE()"
case "eot()":
p.Value = "'9999-12-31 23:59:59.999'"
default:
}
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
}
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 = msf.processIndexTag(indexes, tn, fd.FName, "idx_", false, true)
case "unique":
indexes = msf.processIndexTag(indexes, tn, fd.FName, "idx_", true, true)
default:
indexes = msf.processIndexTag(indexes, tn, fd.FName, p.Value, false, false)
}
case "fkey":
fKeys = msf.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 = "GETDATE()"
case "eot()":
p.Value = "'9999-12-31 23:59:59.999'"
default:
}
col.fDefault = "DEFAULT " + p.Value
}
}
}
fldef[idx].FType = col.fType
// add the current column to the schema
if col.uType != "" {
tableSchema = tableSchema + qt + col.fName + qt + " " + col.uType
} else {
tableSchema = tableSchema + qt + col.fName + qt + " " + col.fType
}
if col.fAutoInc == true {
tableSchema = tableSchema + " IDENTITY(1,1)"
}
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 + "PRIMARY KEY (" + pKeys + ") )"
}
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,
fkey: fKeys,
pk: pKeys,
err: err,
}
if msf.log {
rc.Log()
}
return rc
}
// CreateTables creates tables on the mysql database referenced
// by msf.DB.
func (msf *MSSQLFlavor) CreateTables(i ...interface{}) error {
// call createTables specifying that the call has not originated
// from within the AlterTables(...) method.
_, err := msf.createTables(false, i)
if err != nil {
return err
}
return nil
}
// DropTables drops tables on the db if they exist, based on
// the provided list of go struct definitions.
func (msf *MSSQLFlavor) 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 msf.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 msf.ExistsTable(tn) {
if msf.log {
log.Printf("table %s exists - adding to drop schema...\n", tn)
}
dropSchema = dropSchema + "DROP TABLE " + tn + ";"
msf.ProcessSchema(dropSchema)
dropSchema = ""
}
}
return nil
}
// AlterTables alters tables on the MSSQL database referenced
// by msf.DB.
func (msf *MSSQLFlavor) 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 msf.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 !msf.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 = msf.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 msf.AlterTables")
}
// if the table does not exist, call CreateTables
// if the table does exist, examine it and perform
// alterations if necessary
if !msf.ExistsTable(tn) {
msf.CreateTables(ent)
continue
}
// build the altered table schema and get its components
tc := msf.buildTablSchema(tn, ai[t])
// go through the latest version of the model and check each
// field against its definition in the database.
qt := msf.GetDBQuote()
alterSchema := "ALTER TABLE " + qt + tn + qt + " ADD "
var cols []string
for _, fd := range tc.flDef {
// new columns first
if !msf.ExistsColumn(tn, fd.FName) && fd.NoDB == false {
colSchema := qt + fd.FName + qt + " " + 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":
switch fd.UnderGoType {
case "string":
colSchema = colSchema + " DEFAULT '" + p.Value + "'"
case "bool":
switch p.Value {
case "TRUE", "true":
p.Value = "1"
case "FALSE", "false":
p.Value = "0"
default:
}
default:
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, ",")
msf.ProcessSchema(alterSchema)
}
// add indexes if required
for k, v := range tc.ind {
if !msf.ExistsIndex(v.TableName, k) {
msf.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, _ := msf.ExistsForeignKeyByName(v.ent, fkn)
if !fkExists {
err = msf.CreateForeignKey(v.ent, v.fkinfo.FromTable, v.fkinfo.RefTable, v.fkinfo.FromField, v.fkinfo.RefField)
if err != nil {
log.Println(err)
return err
}
}
}
return nil
}
// ExistsTable checks the currently connected database and
// returns true if the named table is found to exist.
func (msf *MSSQLFlavor) ExistsTable(tn string) bool {
n := 0
etQuery := "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = '" + tn + "';"
msf.QsLog(etQuery)
msf.db.QueryRow(etQuery).Scan(&n)
if n > 0 {
return true
}
return false
}
// GetDBName returns the name of the currently connected db
func (msf *MSSQLFlavor) GetDBName() (dbName string) {
qs := "SELECT DB_NAME()"
msf.QsLog(qs)
row := msf.db.QueryRow(qs)
if row != nil {
err := row.Scan(&dbName)
if err != nil {
panic(err)
}
}
return dbName
}
// ExistsIndex checks the connected database for the presence
// of the specified index.
func (msf *MSSQLFlavor) ExistsIndex(tn string, in string) bool {
n := 0
qs := "SELECT COUNT(*) FROM sys.indexes WHERE name=? AND object_id = OBJECT_ID(?);"
msf.QsLog(qs, in, tn)
msf.db.QueryRow(qs, in, tn).Scan(&n)
if n > 0 {
return true
}
return false
}
// DropIndex drops the specfied index on the connected database.
func (msf *MSSQLFlavor) DropIndex(tn string, in string) error {
if msf.ExistsIndex(tn, in) {
indexSchema := "DROP INDEX " + in + " ON " + tn + ";"
msf.ProcessSchema(indexSchema)
return nil
}
return nil
}
// 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 (msf *MSSQLFlavor) ExistsColumn(tn string, cn string) bool {
n := 0
if msf.ExistsTable(tn) {
qs := "SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ? AND column_name = ?;"
msf.QsLog(qs, tn, cn)
msf.db.QueryRow(qs, tn, cn).Scan(&n)
if n > 0 {
return true
}
}
return false
}
// DestructiveResetTables drops tables on the MSSQL 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 (msf *MSSQLFlavor) DestructiveResetTables(i ...interface{}) error {
err := msf.DropTables(i...)
if err != nil {
return err
}
err = msf.CreateTables(i...)
if err != nil {
return err
}
return nil
}
// AlterSequenceStart may be used to make changes to the start value of the
// named identity-field on the currently connected MSSQL database.
func (msf *MSSQLFlavor) AlterSequenceStart(name string, start int) error {
// reseed the primary key
// DBCC CHECKIDENT ('dbo.depot', RESEED, 50000000);
alterSequenceSchema := "DBCC CHECKIDENT (" + name + ", RESEED, " + strconv.Itoa(start) + ")"
msf.ProcessSchema(alterSequenceSchema)
return nil
}
// GetNextSequenceValue is used primarily for testing. It returns
// the current value of the MSSQL identity (auto-increment) field for
// the named table.
func (msf *MSSQLFlavor) GetNextSequenceValue(name string) (int, error) {
seq := 0
if msf.ExistsTable(name) {
// "SELECT IDENT_CURRENT( 'tableNAme' );
seqQuery := "SELECT IDENT_CURRENT( '" + name + "' );"
msf.QsLog(seqQuery)
err := msf.db.QueryRow(seqQuery).Scan(&seq)
if err != nil {
return 0, err
}
return seq, nil
}
return seq, nil
}
// ExistsForeignKeyByName checks to see if the named foreign-key exists on the
// table corresponding to provided sqac model (i).
func (msf *MSSQLFlavor) ExistsForeignKeyByName(i interface{}, fkn string) (bool, error) {
var count uint64
fkQuery := "SELECT COUNT(*) FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME = '" + fkn + "';"
msf.QsLog(fkQuery)
err := msf.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 (msf *MSSQLFlavor) 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 msf.ExistsForeignKeyByName(i, fkn)
}
//================================================================
// CRUD ops
//================================================================
// Create the entity (single-row) on the database
func (msf *MSSQLFlavor) Create(ent interface{}) error {
var info CrudInfo
info.ent = ent
info.log = false
info.mode = "C"
err := msf.BuildComponents(&info)
if err != nil {
return err
}
// build the mssql 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 mssql insert query
insQuery := "INSERT INTO " + info.tn + " " + insFlds + " " + "VALUES " + insVals + ";"
msf.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
result, err := msf.db.Exec(insQuery)
if err != nil {
return err
}
lastID, err := result.LastInsertId()
if err != nil {
return err
}
// "SELECT * FROM %s WHERE %s = %v;", info.tn, info.incKeyName, lastID
selQuery := "SELECT * FROM " + info.tn + " WHERE " + info.incKeyName + " = " + strconv.FormatInt(lastID, 10) + ";"
msf.QsLog(selQuery)
err = msf.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 (msf *MSSQLFlavor) Update(ent interface{}) error {
var info CrudInfo
info.ent = ent
info.log = false
info.mode = "U"
err := msf.BuildComponents(&info)
if err != nil {
return err
}
keyList := ""
for k, s := range info.keyMap {
fType := reflect.TypeOf(s).String()
if msf.IsLog() {
log.Printf("key: %v, value: %v\n", k, s)
log.Println("TYPE:", fType)
}
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")
colList := ""
for k, v := range info.fldMap {
colList = fmt.Sprintf("%s %s = %s, ", colList, k, v)
}
colList = strings.TrimSuffix(colList, ", ")
// "UPDATE %s SET %s WHERE %s;", info.tn, colList, keyList
updQuery := "UPDATE " + info.tn + " SET " + colList + " WHERE " + keyList + ";"
msf.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 check for errors
_, err = msf.db.Exec(updQuery)
if err != nil {
return err
}
// read the updated row
selQuery := "SELECT * FROM " + info.tn + " WHERE " + keyList + ";"
msf.QsLog(selQuery)
err = msf.db.QueryRowx(selQuery).StructScan(info.ent) // .MapScan(info.resultMap) // SliceScan
if err != nil {
return err
}
info.entValue = reflect.ValueOf(info.ent)
return nil
}
// GetEntitiesWithCommands is a parameterized get. See the BaseFlavor implementation for more info.
func (msf *MSSQLFlavor) GetEntitiesWithCommands(ents interface{}, pList []GetParam, cmdMap map[string]interface{}) (interface{}, error) {
var err error
var count uint64
var row *sqlx.Row
paramString := ""
selQuery := ""
// 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)
// are there any parameters to include in the query?
var pv []interface{}
if pList != nil && len(pList) > 0 {
paramString = " WHERE"
for i := range pList {
paramString = paramString + " " + common.CamelToSnake(pList[i].FieldName) + " " + pList[i].Operand + " ? " + pList[i].NextOperator
pv = append(pv, pList[i].ParamValue)
}
}
if msf.log {
log.Println("constructed paramString:", paramString)
}
// received a $count command? this supercedes all, as it should not
// be mixed with any other $<commands>.
_, ok := cmdMap["count"]
if ok {
if paramString == "" {
selQuery = "SELECT COUNT(*) FROM " + tn + ";"
msf.QsLog(selQuery)
row = msf.ExecuteQueryRowx(selQuery)
} else {
selQuery = "SELECT COUNT(*) FROM " + tn + paramString + ";"
msf.QsLog(selQuery)
row = msf.ExecuteQueryRowx(selQuery, pv...)
}
err = row.Scan(&count)
if err != nil {
log.Fatal(err)
}
return count, nil
}
// no $count command - build query
var obString string
var limitString string
var offsetString string
var adString string
// received $orderby command?
obField, ok := cmdMap["orderby"]
if ok {
obString = " ORDER BY " + obField.(string)
}
// received $asc command?
_, ok = cmdMap["asc"]
if ok {
adString = " ASC"
}
// received $desc command?
_, ok = cmdMap["desc"]
if ok {
adString = " DESC"
}
// received $offset command?
offField, ok := cmdMap["offset"]
if ok {
offsetString = fmt.Sprintf(" OFFSET %v ROWS", offField)
}
// received $limit command?
limField, ok := cmdMap["limit"]
if ok {
if offsetString != "" {
limitString = " FETCH NEXT " + strconv.FormatInt(reflect.ValueOf(limField).Int(), 10) + " ROWS ONLY"
} else {
limitString = fmt.Sprintf("TOP(%v)", limField)
}
}
// -- SELECT COUNT(*) FROM library;
// -- SELECT * FROM library;
// -- SELECT * FROM library LIMIT 2;
// -- SELECT * FROM library OFFSET 2;
// -- SELECT * FROM library LIMIT 2 OFFSET 1;
// -- SELECT * FROM library ORDER BY ID DESC;
// -- SELECT * FROM library ORDER BY ID ASC;
// -- SELECT * FROM library ORDER BY name ASC;
// -- SELECT * FROM library ORDER BY ID ASC LIMIT 2 OFFSET 2;
// if $asc or $desc were specified with no $orderby, default to order by id
if obString == "" && adString != "" {
obString = " ORDER BY id"
}
if offsetString != "" && obString == "" {
obString = " ORDER BY id"
}
if limitString != "" && offsetString == "" {
selQuery = "SELECT " + limitString + " * FROM " + tn + paramString
} else {
selQuery = "SELECT * FROM " + tn + paramString
}
selQuery = msf.db.Rebind(selQuery)
// use SELECT (TOP n) * ...
if limitString != "" && offsetString == "" {
selQuery = selQuery + obString + adString + ";"
} else {
selQuery = selQuery + obString + adString + offsetString + limitString + ";"
}
msf.QsLog(selQuery)
// read the rows
rows, err := msf.db.Queryx(selQuery, pv...)
if err != nil {
log.Printf("GetEntitiesWithCommands 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("scan error:", err)
return nil, err
}
entsv = reflect.Append(entsv, testVar.Elem())
}
// ents = entsv.Interface()
return entsv.Interface(), nil
}
// GetEntitiesCP is a parameterized get. See the BaseFlavor implementation for more info.
func (msf *MSSQLFlavor) GetEntitiesCP(ents interface{}, pList []GetParam, cmdMap map[string]interface{}) (result uint64, err error) {
var count uint64
var row *sqlx.Row
paramString := ""
selQuery := ""
// get the underlying data type of the interface{} ([]ModelEtc)
sliceTypeElem := reflect.TypeOf(ents).Elem()
// get the underlying (struct?) type of the slice
t := reflect.Indirect(reflect.ValueOf(ents)).Type().Elem()
// create a struct from the type
dstRow := reflect.New(t)