-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_column_definition.go
134 lines (117 loc) · 2.81 KB
/
table_column_definition.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
package gosql
import "strings"
// list of column definition
type columnDefinitions []*columnDefinition
// String render all column definitions
func (c columnDefinitions) String() string {
b := strings.Builder{}
for i, definition := range c {
if i == 0 {
b.WriteString(definition.String())
} else {
b.WriteString(", " + definition.String())
}
}
return b.String()
}
// Len count of definition
func (c columnDefinitions) Len() int {
return len(c)
}
// Add new definition
func (c *columnDefinitions) Add() (def *columnDefinition, n int) {
// maloc new definition
def = &columnDefinition{}
// index of new definition in definition list
n = len(*c)
// append new definition in list
*c = append(*c, def)
return
}
// Swap definitions
func (c *columnDefinitions) Swap() {
if c == nil || len(*c) == 0 {
return
}
var i, j int
j = len(*c) - 1
for {
if i == j || i-1 == j {
break
}
buf := (*c)[i]
(*c)[i] = (*c)[j]
(*c)[j] = buf
i++
j--
}
return
}
// Remove definition by n
func (c *columnDefinitions) Remove(n int) *columnDefinitions {
*c = append((*c)[:n], (*c)[n+1:]...)
return c
}
// Clear remove all definitions
func (c *columnDefinitions) Clear() *columnDefinitions {
*c = (*c)[:0]
return c
}
// AddConstraint add constraint definition
func (c *columnDefinitions) AddConstraint() *constraintTable {
def, _ := c.Add()
return def.Constraint()
}
// AddColumn add column definition
func (c *columnDefinitions) AddColumn() *column {
def, _ := c.Add()
return def.Column()
}
// AddLike add like expression
func (c *columnDefinitions) AddLike() *likeTable {
def, _ := c.Add()
return def.Like()
}
// table column definition
// { column_name data_type [ COMPRESSION compression_method ] [ COLLATE collation ] [ column_constraint [ ... ] ]
// | table_constraint
// | LIKE source_table [ like_option ... ] }
type columnDefinition struct {
// column
column column
// or constraint
constraintTable constraintTable
// or like expression
like likeTable
}
// Column return column
func (d *columnDefinition) Column() *column {
return &d.column
}
// Constraint return constraint
func (d *columnDefinition) Constraint() *constraintTable {
return &d.constraintTable
}
// Like return like table
func (d *columnDefinition) Like() *likeTable {
return &d.like
}
// IsEmpty check is columnDefinition is empty
func (d *columnDefinition) IsEmpty() bool {
return d == nil || (d.column.IsEmpty() && d.constraintTable.IsEmpty() && d.like.IsEmpty())
}
// String render columnDefinition
func (d *columnDefinition) String() string {
if d.IsEmpty() {
return ""
}
b := strings.Builder{}
if !d.column.IsEmpty() {
b.WriteString(d.column.String())
} else if !d.constraintTable.IsEmpty() {
b.WriteString(d.constraintTable.String())
} else if !d.like.IsEmpty() {
b.WriteString(d.like.String())
}
return b.String()
}