forked from yunionio/sqlchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumn.go
More file actions
621 lines (538 loc) · 16.6 KB
/
column.go
File metadata and controls
621 lines (538 loc) · 16.6 KB
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
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dameng
import (
"bytes"
"database/sql"
"fmt"
"reflect"
"strconv"
"time"
"yunion.io/x/log"
"yunion.io/x/pkg/gotypes"
"yunion.io/x/pkg/tristate"
"yunion.io/x/pkg/utils"
"yunion.io/x/sqlchemy"
)
func columnDefinitionBuffer(c sqlchemy.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)
}
def := c.Default()
if hasDefault(c) {
def = sqlchemy.GetStringValue(c.ConvertFromString(def))
buf.WriteString(" DEFAULT ")
if c.IsText() {
buf.WriteByte('\'')
}
buf.WriteString(def)
if c.IsText() {
buf.WriteByte('\'')
}
}
if !c.IsNullable() {
buf.WriteString(" NOT NULL")
}
return buf
}
func hasDefault(c sqlchemy.IColumnSpec) bool {
// if c is NOT NULL, default value must be set
// if datetime, empty default value is allowed for not null
return (c.Default() != "" || (!c.IsNullable() && !c.IsDateTime())) && !c.IsAutoIncrement()
}
// SBooleanColumn represents a boolean type column, which is a int(1) for mysql, with value of true or false
type SBooleanColumn struct {
sqlchemy.SBaseWidthColumn
}
// DefinitionString implementation of SBooleanColumn for IColumnSpec
func (c *SBooleanColumn) DefinitionString() string {
buf := columnDefinitionBuffer(c)
return buf.String()
}
// ConvertFromString implementation of SBooleanColumn for IColumnSpec
func (c *SBooleanColumn) ConvertFromString(str string) interface{} {
switch sqlchemy.ConvertValueToBool(str) {
case true:
return 1
default:
return 0
}
}
// ConvertFromValue implementation of STristateColumn for IColumnSpec
func (c *SBooleanColumn) ConvertFromValue(val interface{}) interface{} {
switch sqlchemy.ConvertValueToBool(val) {
case true:
return 1
default:
return 0
}
}
// IsZero implementation of SBooleanColumn for IColumnSpec
func (c *SBooleanColumn) IsZero(val interface{}) bool {
if c.IsPointer() {
bVal := val.(*bool)
return bVal == nil
}
bVal := val.(bool)
return bVal == false
}
// NewBooleanColumn return an instance of SBooleanColumn
func NewBooleanColumn(name string, tagmap map[string]string, isPointer bool) SBooleanColumn {
bc := SBooleanColumn{SBaseWidthColumn: sqlchemy.NewBaseWidthColumn(name, "TINYINT", tagmap, isPointer)}
if !bc.IsPointer() && len(bc.Default()) > 0 && bc.ConvertFromString(bc.Default()) == 1 {
msg := fmt.Sprintf("Non-pointer boolean column should not default true: %s(%s)", name, tagmap)
panic(msg)
}
return bc
}
// STristateColumn represents a tristate type column, with value of true, false or none
type STristateColumn struct {
sqlchemy.SBaseWidthColumn
}
// DefinitionString implementation of STristateColumn for IColumnSpec
func (c *STristateColumn) DefinitionString() string {
buf := columnDefinitionBuffer(c)
return buf.String()
}
// ConvertFromString implementation of STristateColumn for IColumnSpec
func (c *STristateColumn) ConvertFromString(str string) interface{} {
switch sqlchemy.ConvertValueToTriState(str) {
case tristate.True:
return 1
case tristate.False:
return 0
default:
return sql.NullInt32{}
}
}
// ConvertFromValue implementation of STristateColumn for IColumnSpec
func (c *STristateColumn) ConvertFromValue(val interface{}) interface{} {
switch sqlchemy.ConvertValueToTriState(val) {
case tristate.True:
return 1
case tristate.False:
return 0
default:
return sql.NullInt32{}
}
}
// IsZero implementation of STristateColumn for IColumnSpec
func (c *STristateColumn) IsZero(val interface{}) bool {
if c.IsPointer() {
bVal := val.(*tristate.TriState)
return bVal == nil
}
bVal := val.(tristate.TriState)
return bVal == tristate.None
}
// NewTristateColumn return an instance of STristateColumn
func NewTristateColumn(table, name string, tagmap map[string]string, isPointer bool) STristateColumn {
if _, ok := tagmap[sqlchemy.TAG_NULLABLE]; ok {
// simply warning, for backward compatiblity reason
// tristate always nullable
// delete(tagmap, sqlchemy.TAG_NULLABLE)
log.Warningf("%s TristateColumn %s should have no nullable tag", table, name)
}
bc := STristateColumn{SBaseWidthColumn: sqlchemy.NewBaseWidthColumn(name, "TINYINT", tagmap, isPointer)}
return bc
}
// SIntegerColumn represents an integer type of column, with value of integer
type SIntegerColumn struct {
sqlchemy.SBaseColumn
// Is this column an autoincrement colmn
isAutoIncrement bool
// Is this column is a version column for this records
isAutoVersion bool
// Is this column a unsigned integer?
// isUnsigned bool
// If this column is an autoincrement column, AutoIncrementOffset records the initial offset
autoIncrementOffset int64
}
// IsNumeric implementation of SIntegerColumn for IColumnSpec
func (c *SIntegerColumn) IsNumeric() bool {
return true
}
// ExtraDefs implementation of SIntegerColumn for IColumnSpec
func (c *SIntegerColumn) ExtraDefs() string {
if c.isAutoIncrement {
return fmt.Sprintf("IDENTITY(%d, %d)", c.autoIncrementOffset, 1)
}
return ""
}
// DefinitionString implementation of SIntegerColumn for IColumnSpec
func (c *SIntegerColumn) DefinitionString() string {
buf := columnDefinitionBuffer(c)
return buf.String()
}
// IsZero implementation of SIntegerColumn for IColumnSpec
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
}
// ConvertFromString implementation of SBooleanColumn for IColumnSpec
func (c *SIntegerColumn) ConvertFromString(str string) interface{} {
intval := sqlchemy.ConvertValueToInteger(str)
switch c.ColType() {
case "TINYINT":
return int8(intval)
case "SMALLINT":
return int16(intval)
case "INT":
return int(intval)
case "BIGINT":
return int64(intval)
}
panic(fmt.Sprintf("unsupported type %s", c.ColType()))
}
func (c *SIntegerColumn) IsAutoVersion() bool {
return c.isAutoVersion
}
func (c *SIntegerColumn) IsAutoIncrement() bool {
return c.isAutoIncrement
}
func (c *SIntegerColumn) AutoIncrementOffset() int64 {
return c.autoIncrementOffset
}
func (c *SIntegerColumn) SetAutoIncrement(on bool) {
c.isAutoIncrement = on
}
func (c *SIntegerColumn) SetAutoIncrementOffset(offset int64) {
c.autoIncrementOffset = offset
}
// NewIntegerColumn return an instance of SIntegerColumn
func NewIntegerColumn(name string, sqltype string, tagmap map[string]string, isPointer bool) SIntegerColumn {
autoinc := false
autoincBase := int64(1)
tagmap, v, ok := utils.TagPop(tagmap, sqlchemy.TAG_AUTOINCREMENT)
if ok {
base, err := strconv.ParseInt(v, 10, 64)
if err == nil && base > 0 {
autoinc = true
autoincBase = base
} else {
autoinc = utils.ToBool(v)
}
}
autover := false
tagmap, v, ok = utils.TagPop(tagmap, sqlchemy.TAG_AUTOVERSION)
if ok {
autover = utils.ToBool(v)
}
c := SIntegerColumn{
SBaseColumn: sqlchemy.NewBaseColumn(name, sqltype, tagmap, isPointer),
isAutoIncrement: autoinc,
autoIncrementOffset: autoincBase,
isAutoVersion: autover,
}
if autoinc {
c.SetPrimary(true) // autoincrement column must be primary key
c.SetNullable(false)
c.isAutoVersion = false
} else if autover {
c.SetPrimary(false)
c.SetNullable(false)
if len(c.Default()) == 0 {
c.SetDefault("0")
}
}
return c
}
// SFloatColumn represents a float type column, e.g. float32 or float64
type SFloatColumn struct {
sqlchemy.SBaseColumn
}
// IsNumeric implementation of SFloatColumn for IColumnSpec
func (c *SFloatColumn) IsNumeric() bool {
return true
}
// DefinitionString implementation of SFloatColumn for IColumnSpec
func (c *SFloatColumn) DefinitionString() string {
buf := columnDefinitionBuffer(c)
return buf.String()
}
// IsZero implementation of SFloatColumn for IColumnSpec
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
}
// ConvertFromString implementation of SBooleanColumn for IColumnSpec
func (c *SFloatColumn) ConvertFromString(str string) interface{} {
floatVal := sqlchemy.ConvertValueToFloat(str)
switch c.ColType() {
case "FLOAT", "REAL":
return float32(floatVal)
case "DOUBLE":
return floatVal
}
panic(fmt.Sprintf("unsupported type %s", c.ColType()))
}
// NewFloatColumn returns an instance of SFloatColumn
func NewFloatColumn(name string, sqlType string, tagmap map[string]string, isPointer bool) SFloatColumn {
return SFloatColumn{SBaseColumn: sqlchemy.NewBaseColumn(name, sqlType, tagmap, isPointer)}
}
// SDecimalColumn represents a DECIMAL type of column, i.e. a float with fixed width of digits
type SDecimalColumn struct {
sqlchemy.SBaseWidthColumn
Precision int
}
// ColType implementation of SDecimalColumn for IColumnSpec
func (c *SDecimalColumn) ColType() string {
str := c.SBaseWidthColumn.ColType()
if str[len(str)-1] == ')' {
str = str[:len(str)-1]
}
return fmt.Sprintf("%s, %d)", str, c.Precision)
}
// IsNumeric implementation of SDecimalColumn for IColumnSpec
func (c *SDecimalColumn) IsNumeric() bool {
return true
}
// DefinitionString implementation of SDecimalColumn for IColumnSpec
func (c *SDecimalColumn) DefinitionString() string {
buf := columnDefinitionBuffer(c)
return buf.String()
}
// IsZero implementation of SDecimalColumn for IColumnSpec
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
}
// ConvertFromString implementation of SBooleanColumn for IColumnSpec
func (c *SDecimalColumn) ConvertFromString(str string) interface{} {
return sqlchemy.ConvertValueToFloat(str)
}
// NewDecimalColumn returns an instance of SDecimalColumn
func NewDecimalColumn(name string, tagmap map[string]string, isPointer bool) SDecimalColumn {
tagmap, v, ok := utils.TagPop(tagmap, sqlchemy.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: sqlchemy.NewBaseWidthColumn(name, "DECIMAL", tagmap, isPointer),
Precision: prec,
}
}
// STextColumn represents a text type of column
type STextColumn struct {
sqlchemy.SBaseWidthColumn
}
// IsText implementation of STextColumn for IColumnSpec
func (c *STextColumn) IsText() bool {
return true
}
// IsSearchable implementation of STextColumn for IColumnSpec
func (c *STextColumn) IsSearchable() bool {
return true
}
// IsAscii implementation of STextColumn for IColumnSpec
func (c *STextColumn) IsAscii() bool {
return false
}
// DefinitionString implementation of STextColumn for IColumnSpec
func (c *STextColumn) DefinitionString() string {
buf := columnDefinitionBuffer(c)
return buf.String()
}
// IsZero implementation of STextColumn for IColumnSpec
func (c *STextColumn) IsZero(val interface{}) bool {
if c.IsPointer() {
return gotypes.IsNil(val)
}
return reflect.ValueOf(val).Len() == 0
}
// ConvertFromString implementation of SBooleanColumn for IColumnSpec
func (c *STextColumn) ConvertFromString(str string) interface{} {
return str
}
func (c *STextColumn) IsString() bool {
return true
}
// NewTextColumn return an instance of STextColumn
func NewTextColumn(name string, sqlType string, tagmap map[string]string, isPointer bool) STextColumn {
return STextColumn{
SBaseWidthColumn: sqlchemy.NewBaseWidthColumn(name, sqlType, tagmap, isPointer),
}
}
// STimeTypeColumn represents a Detetime type of column, e.g. DateTime
type STimeTypeColumn struct {
sqlchemy.SBaseColumn
// timestamp precision of nanoseconds, 0-6
precision int
}
// IsText implementation of STimeTypeColumn for IColumnSpec
func (c *STimeTypeColumn) IsText() bool {
return true
}
// DefinitionString implementation of STimeTypeColumn for IColumnSpec
func (c *STimeTypeColumn) DefinitionString() string {
buf := columnDefinitionBuffer(c)
return buf.String()
}
// IsZero implementation of STimeTypeColumn for IColumnSpec
func (c *STimeTypeColumn) IsZero(val interface{}) bool {
if c.IsPointer() {
bVal := val.(*time.Time)
return bVal == nil
}
bVal := val.(time.Time)
return bVal.IsZero()
}
// ColType implementation of SIntegerColumn for IColumnSpec
func (c *STimeTypeColumn) ColType() string {
return fmt.Sprintf("%s(%d)", (&c.SBaseColumn).ColType(), c.precision)
}
// ConvertFromString implementation of SBooleanColumn for IColumnSpec
func (c *STimeTypeColumn) ConvertFromString(str string) interface{} {
return sqlchemy.ConvertValueToTime(str)
}
func (c *STimeTypeColumn) ConvertFromValue(val interface{}) interface{} {
return sqlchemy.ConvertValueToTime(val)
}
// NewTimeTypeColumn return an instance of STimeTypeColumn
func NewTimeTypeColumn(name string, typeStr string, tagmap map[string]string, isPointer bool) STimeTypeColumn {
tagmap, v, ok := utils.TagPop(tagmap, sqlchemy.TAG_PRECISION)
prec := 0
if ok {
prec, _ = strconv.Atoi(v)
if prec > 6 || prec < 0 {
log.Warningf("datetime field %s precision %d change to 6", name, prec)
prec = 0
}
}
dc := STimeTypeColumn{
SBaseColumn: sqlchemy.NewBaseColumn(name, typeStr, tagmap, isPointer),
precision: prec,
}
return dc
}
// SDateTimeColumn represents a DateTime type of column
type SDateTimeColumn struct {
STimeTypeColumn
// Is this column a 'created_at' field, which records the time of create this record
isCreatedAt bool
// Is this column a 'updated_at' field, which records the time when this record was updated
isUpdatedAt bool
}
// DefinitionString implementation of SDateTimeColumn for IColumnSpec
func (c *SDateTimeColumn) DefinitionString() string {
buf := columnDefinitionBuffer(c)
return buf.String()
}
func (c *SDateTimeColumn) IsCreatedAt() bool {
return c.isCreatedAt
}
func (c *SDateTimeColumn) IsUpdatedAt() bool {
return c.isUpdatedAt
}
func (c *SDateTimeColumn) IsDateTime() bool {
return true
}
// NewDateTimeColumn returns an instance of DateTime column
func NewDateTimeColumn(name string, tagmap map[string]string, isPointer bool) SDateTimeColumn {
createdAt := false
updatedAt := false
tagmap, v, ok := utils.TagPop(tagmap, sqlchemy.TAG_CREATE_TIMESTAMP)
if ok {
createdAt = utils.ToBool(v)
}
tagmap, v, ok = utils.TagPop(tagmap, sqlchemy.TAG_UPDATE_TIMESTAMP)
if ok {
updatedAt = utils.ToBool(v)
}
dtc := SDateTimeColumn{
STimeTypeColumn: NewTimeTypeColumn(name, "TIMESTAMP", tagmap, isPointer),
isCreatedAt: createdAt,
isUpdatedAt: updatedAt,
}
return dtc
}
// CompoundColumn represents a column of compound tye, e.g. a JSON, an Array, or a struct
type CompoundColumn struct {
STextColumn
sqlchemy.SBaseCompoundColumn
}
// DefinitionString implementation of CompoundColumn for IColumnSpec
func (c *CompoundColumn) DefinitionString() string {
buf := columnDefinitionBuffer(c)
return buf.String()
}
// IsZero implementation of CompoundColumn for IColumnSpec
func (c *CompoundColumn) IsZero(val interface{}) bool {
if val == nil {
return true
}
if c.IsPointer() && reflect.ValueOf(val).IsNil() {
return true
}
return false
}
// ConvertFromString implementation of CompoundColumn for IColumnSpec
func (c *CompoundColumn) ConvertFromString(str string) interface{} {
return c.SBaseCompoundColumn.ConvertFromString(str)
}
// ConvertFromValue implementation of CompoundColumn for IColumnSpec
func (c *CompoundColumn) ConvertFromValue(val interface{}) interface{} {
return c.SBaseCompoundColumn.ConvertFromValue(val)
}
// NewCompoundColumn returns an instance of CompoundColumn
func NewCompoundColumn(name string, sqlType string, tagmap map[string]string, isPointer bool) CompoundColumn {
dtc := CompoundColumn{STextColumn: NewTextColumn(name, sqlType, tagmap, isPointer)}
return dtc
}