-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_column.go
86 lines (76 loc) · 1.8 KB
/
table_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
package gosql
import "strings"
// { column_name data_type [ STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT } ] [ COMPRESSION compression_method ] [ COLLATE collation ] [ column_constraint [ ... ] ] }
type column struct {
// column name
name string
// data type
dataType string
// sort rule
collate string
// compression method
compression string
// column constraint
constraint constraintColumn
// column storage
storage Storage
}
// Collate set sort rule
func (c *column) Collate(collation string) *column {
c.collate = collation
return c
}
// Compression set compression method
func (c *column) Compression(method string) *column {
c.compression = method
return c
}
// Name set column name
func (c *column) Name(name string) *column {
c.name = name
return c
}
// Type set column type
func (c *column) Type(dataType string) *column {
c.dataType = dataType
return c
}
// Storage set column storage
func (c *column) Storage(storage Storage) *column {
c.storage = storage
return c
}
// Constraint get constraint
func (c *column) Constraint() *constraintColumn {
return &c.constraint
}
// IsEmpty check if empty
func (c *column) IsEmpty() bool {
return c == nil || (c.name == "" && c.dataType == "" && c.collate == "" && c.compression == "" && c.constraint.IsEmpty())
}
// String render column
func (c *column) String() string {
if c.IsEmpty() {
return ""
}
b := strings.Builder{}
if c.name != "" {
b.WriteString(c.name)
}
if c.dataType != "" {
b.WriteString(" " + c.dataType)
}
if c.compression != "" {
b.WriteString(" COMPRESSION " + c.compression)
}
if c.collate != "" {
b.WriteString(" COLLATE " + c.compression)
}
if c.storage != "" {
b.WriteString(" STORAGE " + string(c.storage))
}
if !c.constraint.IsEmpty() {
b.WriteString(c.constraint.String())
}
return b.String()
}