forked from yunionio/sqlchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
column.go
759 lines (663 loc) · 15.5 KB
/
column.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
package sqlchemy
import (
"bytes"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"yunion.io/x/log"
"yunion.io/x/pkg/gotypes"
"yunion.io/x/pkg/tristate"
"yunion.io/x/pkg/util/regutils"
"yunion.io/x/pkg/utils"
)
type IColumnSpec interface {
Name() string
ColType() string
Default() string
IsSupportDefault() bool
IsNullable() bool
IsPrimary() bool
IsUnique() bool
IsIndex() bool
ExtraDefs() string
DefinitionString() string
IsText() bool
IsSearchable() bool
IsAscii() bool
IsNumeric() bool
ConvertFromString(str string) string
// ConvertToString(str string) string
ConvertFromValue(val interface{}) interface{}
// ConvertToValue(str interface{}) interface{}
IsZero(val interface{}) bool
// IsEqual(v1, v2 interface{}) bool
Tags() map[string]string
IsPointer() bool
}
type SBaseColumn struct {
name string
dbName string
sqlType string
defaultString string
isPointer bool
isNullable bool
isPrimary bool
isUnique bool
isIndex bool
tags map[string]string
}
func (c *SBaseColumn) IsPointer() bool {
return c.isPointer
}
func (c *SBaseColumn) Name() string {
if len(c.dbName) > 0 {
return c.dbName
} else {
return c.name
}
}
func (c *SBaseColumn) ColType() string {
return c.sqlType
}
func (c *SBaseColumn) Default() string {
return c.defaultString
}
func (c *SBaseColumn) IsSupportDefault() bool {
return true
}
func (c *SBaseColumn) IsNullable() bool {
return c.isNullable
}
func (c *SBaseColumn) IsPrimary() bool {
return c.isPrimary
}
func (c *SBaseColumn) IsUnique() bool {
return c.isUnique
}
func (c *SBaseColumn) IsIndex() bool {
return c.isIndex
}
func (c *SBaseColumn) ExtraDefs() string {
return ""
}
func (c *SBaseColumn) IsText() bool {
return false
}
func (c *SBaseColumn) IsAscii() bool {
return false
}
func (c *SBaseColumn) IsSearchable() bool {
return false
}
func (c *SBaseColumn) IsNumeric() bool {
return false
}
func (c *SBaseColumn) ConvertFromString(str string) string {
return str
}
func (c *SBaseColumn) ConvertToString(str string) string {
return str
}
func (c *SBaseColumn) ConvertFromValue(val interface{}) interface{} {
return val
}
func (c *SBaseColumn) ConvertToValue(val interface{}) interface{} {
return val
}
func (c *SBaseColumn) Tags() map[string]string {
return c.tags
}
func definitionBuffer(c IColumnSpec) bytes.Buffer {
var buf bytes.Buffer
buf.WriteByte('`')
buf.WriteString(c.Name())
buf.WriteByte('`')
buf.WriteByte(' ')
buf.WriteString(c.ColType())
extra := c.ExtraDefs()
if len(extra) > 0 {
buf.WriteString(" ")
buf.WriteString(extra)
}
if !c.IsNullable() {
buf.WriteString(" NOT NULL")
}
def := c.Default()
if len(def) > 0 && c.IsSupportDefault() {
def = c.ConvertFromString(def)
buf.WriteString(" DEFAULT ")
if c.IsText() {
buf.WriteByte('\'')
}
buf.WriteString(def)
if c.IsText() {
buf.WriteByte('\'')
}
}
return buf
}
func NewBaseColumn(name string, sqltype string, tagmap map[string]string, isPointer bool) SBaseColumn {
var val string
var ok bool
dbName := ""
tagmap, val, ok = utils.TagPop(tagmap, TAG_NAME)
if ok {
dbName = val
}
defStr := ""
tagmap, val, ok = utils.TagPop(tagmap, TAG_DEFAULT)
if ok {
defStr = val
}
isNullable := true
tagmap, val, ok = utils.TagPop(tagmap, TAG_NULLABLE)
if ok {
isNullable = utils.ToBool(val)
}
isPrimary := false
tagmap, val, ok = utils.TagPop(tagmap, TAG_PRIMARY)
if ok {
isPrimary = utils.ToBool(val)
}
isUnique := false
tagmap, val, ok = utils.TagPop(tagmap, TAG_UNIQUE)
if ok {
isUnique = utils.ToBool(val)
}
isIndex := false
tagmap, val, ok = utils.TagPop(tagmap, TAG_INDEX)
if ok {
isIndex = utils.ToBool(val)
}
if isPrimary {
isNullable = false
}
return SBaseColumn{
name: name,
dbName: dbName,
sqlType: sqltype,
defaultString: defStr,
isNullable: isNullable,
isPrimary: isPrimary,
isUnique: isUnique,
isIndex: isIndex,
tags: tagmap,
isPointer: isPointer,
}
}
type SBaseWidthColumn struct {
SBaseColumn
width int
}
func (c *SBaseWidthColumn) ColType() string {
if c.width > 0 {
return fmt.Sprintf("%s(%d)", c.sqlType, c.width)
} else {
return c.sqlType
}
}
func NewBaseWidthColumn(name string, sqltype string, tagmap map[string]string, isPointer bool) SBaseWidthColumn {
width := 0
tagmap, v, ok := utils.TagPop(tagmap, TAG_WIDTH)
if ok {
width, _ = strconv.Atoi(v)
}
wc := SBaseWidthColumn{
SBaseColumn: NewBaseColumn(name, sqltype, tagmap, isPointer),
width: width,
}
return wc
}
type SBooleanColumn struct {
SBaseWidthColumn
}
func (c *SBooleanColumn) DefinitionString() string {
buf := definitionBuffer(c)
return buf.String()
}
func (c *SBooleanColumn) ConvertFromString(str string) string {
switch strings.ToLower(str) {
case "true", "yes", "on", "ok", "1":
return "1"
default:
return "0"
}
}
func (c *SBooleanColumn) ConvertFromValue(val interface{}) interface{} {
switch bVal := val.(type) {
case bool:
if bVal {
return 1
} else {
return 0
}
case *bool:
if gotypes.IsNil(bVal) {
return 0
} else if *bVal {
return 1
} else {
return 0
}
default:
return 0
}
}
func (c *SBooleanColumn) IsZero(val interface{}) bool {
if c.isPointer {
bVal := val.(*bool)
return bVal == nil
} else {
bVal := val.(bool)
return bVal == false
}
}
func NewBooleanColumn(name string, tagmap map[string]string, isPointer bool) SBooleanColumn {
bc := SBooleanColumn{SBaseWidthColumn: NewBaseWidthColumn(name, "TINYINT", tagmap, isPointer)}
if !bc.IsPointer() && len(bc.Default()) > 0 && bc.ConvertFromString(bc.Default()) == "1" {
log.Warningf("Non-pointer boolean type should not set default value: %s(%s)", name, tagmap)
// bc.defaultString = ""
}
return bc
}
type STristateColumn struct {
SBaseWidthColumn
}
func (c *STristateColumn) DefinitionString() string {
buf := definitionBuffer(c)
return buf.String()
}
func (c *STristateColumn) ConvertFromString(str string) string {
switch strings.ToLower(str) {
case "true", "yes", "on", "ok", "1":
return "1"
case "none", "null", "unknown":
return ""
default:
return "0"
}
}
func (c *STristateColumn) ConvertFromValue(val interface{}) interface{} {
bVal := val.(tristate.TriState)
if bVal == tristate.True {
return 1
} else {
return 0
}
}
func (c *STristateColumn) IsZero(val interface{}) bool {
if c.isPointer {
bVal := val.(*tristate.TriState)
return bVal == nil
} else {
bVal := val.(tristate.TriState)
return bVal == tristate.None
}
}
func NewTristateColumn(name string, tagmap map[string]string, isPointer bool) STristateColumn {
bc := STristateColumn{SBaseWidthColumn: NewBaseWidthColumn(name, "TINYINT", tagmap, isPointer)}
return bc
}
type SIntegerColumn struct {
SBaseWidthColumn
IsAutoIncrement bool
IsAutoVersion bool
IsUnsigned bool
}
func (c *SIntegerColumn) IsNumeric() bool {
return true
}
func (c *SIntegerColumn) ExtraDefs() string {
if c.IsAutoIncrement {
return "AUTO_INCREMENT"
}
return ""
}
func (c *SIntegerColumn) DefinitionString() string {
buf := definitionBuffer(c)
return buf.String()
}
func (c *SIntegerColumn) IsZero(val interface{}) bool {
if val == nil || (c.isPointer && reflect.ValueOf(val).IsNil()) {
return true
}
switch intVal := val.(type) {
case int8, int16, int32, int64, int, uint, uint8, uint16, uint32, uint64:
return intVal == 0
}
return true
}
func (c *SIntegerColumn) ColType() string {
str := (&c.SBaseWidthColumn).ColType()
if c.IsUnsigned {
str += " UNSIGNED"
}
return str
}
func NewIntegerColumn(name string, sqltype string, unsigned bool, tagmap map[string]string, isPointer bool) SIntegerColumn {
autoinc := false
tagmap, v, ok := utils.TagPop(tagmap, TAG_AUTOINCREMENT)
if ok {
autoinc = utils.ToBool(v)
}
autover := false
tagmap, v, ok = utils.TagPop(tagmap, TAG_AUTOVERSION)
if ok {
autover = utils.ToBool(v)
}
c := SIntegerColumn{
SBaseWidthColumn: NewBaseWidthColumn(name, sqltype, tagmap, isPointer),
IsAutoIncrement: autoinc,
IsAutoVersion: autover,
IsUnsigned: unsigned,
}
if autoinc {
c.isPrimary = true // autoincrement column must be primary key
c.isNullable = false
c.IsAutoVersion = false
} else if autover {
c.isPrimary = false
c.isNullable = false
if len(c.defaultString) == 0 {
c.defaultString = "0"
}
}
return c
}
type SFloatColumn struct {
SBaseColumn
}
func (c *SFloatColumn) IsNumeric() bool {
return true
}
func (c *SFloatColumn) DefinitionString() string {
buf := definitionBuffer(c)
return buf.String()
}
func (c *SFloatColumn) IsZero(val interface{}) bool {
if c.isPointer {
switch val.(type) {
case *float32:
return val.(*float32) == nil
case *float64:
return val.(*float64) == nil
}
} else {
switch val.(type) {
case float32:
return val.(float32) == 0.0
case float64:
return val.(float64) == 0.0
}
}
return true
}
func NewFloatColumn(name string, sqlType string, tagmap map[string]string, isPointer bool) SFloatColumn {
return SFloatColumn{SBaseColumn: NewBaseColumn(name, sqlType, tagmap, isPointer)}
}
type SDecimalColumn struct {
SBaseWidthColumn
Precision int
}
func (c *SDecimalColumn) ColType() string {
return fmt.Sprintf("%s(%d, %d)", c.sqlType, c.width, c.Precision)
}
func (c *SDecimalColumn) IsNumeric() bool {
return true
}
func (c *SDecimalColumn) DefinitionString() string {
buf := definitionBuffer(c)
return buf.String()
}
func (c *SDecimalColumn) IsZero(val interface{}) bool {
if c.isPointer {
switch val.(type) {
case *float32:
return val.(*float32) == nil
case *float64:
return val.(*float64) == nil
}
} else {
switch val.(type) {
case float32:
return val.(float32) == 0.0
case float64:
return val.(float64) == 0.0
}
}
return true
}
func NewDecimalColumn(name string, tagmap map[string]string, isPointer bool) SDecimalColumn {
tagmap, v, ok := utils.TagPop(tagmap, TAG_PRECISION)
if !ok {
panic(fmt.Sprintf("Field %q of float misses precision tag", name))
}
prec, err := strconv.Atoi(v)
if err != nil {
panic(fmt.Sprintf("Field precision of %q shoud be integer (%q)", name, v))
}
return SDecimalColumn{
SBaseWidthColumn: NewBaseWidthColumn(name, "DECIMAL", tagmap, isPointer),
Precision: prec,
}
}
type STextColumn struct {
SBaseWidthColumn
Charset string
}
func (c *STextColumn) IsSupportDefault() bool {
// https://stackoverflow.com/questions/3466872/why-cant-a-text-column-have-a-default-value-in-mysql
// MySQL does not support default for TEXT/BLOB
if c.sqlType == "VARCHAR" {
return true
} else {
return false
}
}
func (c *STextColumn) ColType() string {
return fmt.Sprintf("%s CHARACTER SET '%s'", c.SBaseWidthColumn.ColType(), c.Charset)
}
func (c *STextColumn) IsText() bool {
return true
}
func (c *STextColumn) IsSearchable() bool {
return true
}
func (c *STextColumn) IsAscii() bool {
if c.Charset == "ascii" {
return true
} else {
return false
}
}
func (c *STextColumn) DefinitionString() string {
buf := definitionBuffer(c)
return buf.String()
}
func (c *STextColumn) IsZero(val interface{}) bool {
if c.isPointer {
bval := val.(*string)
return bval == nil
} else {
bVal := val.(string)
return len(bVal) == 0
}
}
func NewTextColumn(name string, tagmap map[string]string, isPointer bool) STextColumn {
var width int
var sqltype string
widthStr, _ := tagmap[TAG_WIDTH]
if len(widthStr) > 0 && regutils.MatchInteger(widthStr) {
width, _ = strconv.Atoi(widthStr)
}
tagmap, txtLen, _ := utils.TagPop(tagmap, TAG_TEXT_LENGTH)
if width == 0 {
switch strings.ToLower(txtLen) {
case "medium":
sqltype = "MEDIUMTEXT"
case "long":
sqltype = "LONGTEXT"
default:
sqltype = "TEXT"
}
} else {
sqltype = "VARCHAR"
}
tagmap, charset, _ := utils.TagPop(tagmap, TAG_CHARSET)
if len(charset) == 0 {
charset = "utf8"
} else if charset != "utf8" && charset != "ascii" {
panic(fmt.Sprintf("Unsupported charset %s for %s", charset, name))
}
return STextColumn{
SBaseWidthColumn: NewBaseWidthColumn(name, sqltype, tagmap, isPointer),
Charset: charset,
}
}
/*type SStringColumn struct {
STextColumn
}
func (c *SStringColumn) DefinitionString() string {
buf := definitionBuffer(c)
return buf.String()
}
func NewStringColumn(name string, sqltype string, tagmap map[string]string) SStringColumn {
sc := SStringColumn{STextColumn: NewTextColumn(name, sqltype, tagmap)}
// if sc.width > 768 {
// log.Fatalf("Field %s width %d too with(>768)", name, sc.width)
// }
return sc
}*/
type SDateTimeColumn struct {
SBaseColumn
IsCreatedAt bool
IsUpdatedAt bool
}
func (c *SDateTimeColumn) IsText() bool {
return true
}
func (c *SDateTimeColumn) DefinitionString() string {
buf := definitionBuffer(c)
return buf.String()
}
func (c *SDateTimeColumn) IsZero(val interface{}) bool {
if c.isPointer {
bVal := val.(*time.Time)
return bVal == nil
} else {
bVal := val.(time.Time)
return bVal.IsZero()
}
}
func NewDateTimeColumn(name string, tagmap map[string]string, isPointer bool) SDateTimeColumn {
createdAt := false
updatedAt := false
tagmap, v, ok := utils.TagPop(tagmap, TAG_CREATE_TIMESTAMP)
if ok {
createdAt = utils.ToBool(v)
}
tagmap, v, ok = utils.TagPop(tagmap, TAG_UPDATE_TIMESTAMP)
if ok {
updatedAt = utils.ToBool(v)
}
dtc := SDateTimeColumn{
NewBaseColumn(name, "DATETIME", tagmap, isPointer),
createdAt, updatedAt,
}
return dtc
}
type CompoundColumn struct {
STextColumn
}
func (c *CompoundColumn) DefinitionString() string {
buf := definitionBuffer(c)
return buf.String()
}
func (c *CompoundColumn) IsZero(val interface{}) bool {
if val == nil {
return true
}
if c.isPointer && reflect.ValueOf(val).IsNil() {
return true
}
json := val.(gotypes.ISerializable)
return json.IsZero()
}
func (c *CompoundColumn) ConvertFromValue(val interface{}) interface{} {
bVal, ok := val.(gotypes.ISerializable)
if ok && bVal != nil {
return bVal.String()
} else {
return ""
}
}
func NewCompoundColumn(name string, tagmap map[string]string, isPointer bool) CompoundColumn {
dtc := CompoundColumn{NewTextColumn(name, tagmap, isPointer)}
return dtc
}
/*type JSONDictColumn struct {
JSONColumn
}
type JSONArrayColumn struct {
JSONColumn
}
func (c *JSONDictColumn) ConvertFromValue(val interface{}) interface{} {
bVal, ok := val.(*jsonutils.JSONDict)
if ok && bVal != nil {
return bVal.String()
} else {
return nil
}
}
func (c *JSONDictColumn) ConvertToValue(val interface{}) interface{} {
iVal, ok := val.(string)
if ok && len(iVal) > 0 {
json, err := jsonutils.ParseString(iVal)
if err == nil {
return json.(*jsonutils.JSONDict)
}
}
return nil
}
func (c *JSONDictColumn) IsZero(val interface{}) bool {
if val == nil {
return true
}
dict := val.(*jsonutils.JSONDict)
return dict.Size() == 0
}
func (c *JSONArrayColumn) ConvertFromValue(val interface{}) interface{} {
bVal, ok := val.(*jsonutils.JSONArray)
if ok && bVal != nil {
return bVal.String()
} else {
return nil
}
}
func (c *JSONArrayColumn) ConvertToValue(val interface{}) interface{} {
iVal, ok := val.(string)
if ok && len(iVal) > 0 {
json, err := jsonutils.ParseString(iVal)
if err == nil {
return json.(*jsonutils.JSONArray)
}
}
return nil
}
func (c *JSONArrayColumn) IsZero(val interface{}) bool {
if val == nil {
return true
}
dict := val.(*jsonutils.JSONArray)
return dict.Size() == 0
}
func NewJSONDictColumn(name string, tagmap map[string]string) JSONDictColumn {
dtc := JSONDictColumn{NewJSONColumn(name, tagmap)}
return dtc
}
func NewJSONArrayColumn(name string, tagmap map[string]string) JSONArrayColumn {
dtc := JSONArrayColumn{NewJSONColumn(name, tagmap)}
return dtc
}*/