-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_of_definition.go
191 lines (166 loc) · 3.82 KB
/
table_of_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
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
package gosql
import "strings"
// list of column definition
type ofDefinitions []*ofDefinition
// String render all column definitions
func (c ofDefinitions) 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 ofDefinitions) Len() int {
return len(c)
}
// Add new definition
func (c *ofDefinitions) Add() (def *ofDefinition, n int) {
// maloc new definition
def = &ofDefinition{}
// index of new definition in definition list
n = len(*c)
// append new definition in list
*c = append(*c, def)
return
}
// Remove definition by n
func (c *ofDefinitions) Remove(n int) *ofDefinitions {
*c = append((*c)[:n], (*c)[n+1:]...)
return c
}
// Clear remove all definitions
func (c *ofDefinitions) Clear() *ofDefinitions {
*c = (*c)[:0]
return c
}
// AddConstraint add constraint definition
func (c *ofDefinitions) AddConstraint() *constraintTable {
def, _ := c.Add()
return def.Constraint()
}
// AddColumn add column definition
func (c *ofDefinitions) AddColumn(name string) *ofColumn {
def, _ := c.Add()
return def.Column().Name(name)
}
// OF type_name [ (
// { column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]
// | table_constraint }
// [, ... ]
//) ]
type ofType struct {
// of type
name string
// of definitions
ofDefinitions ofDefinitions
}
// Name set name
func (t *ofType) Name(name string) *ofType {
t.name = name
return t
}
// Columns get columns definitions
func (t *ofType) Columns() *ofDefinitions {
return &t.ofDefinitions
}
// IsEmpty check if empty
func (t *ofType) IsEmpty() bool {
return t == nil || (t.name == "" && t.ofDefinitions.Len() == 0)
}
// String render column
func (t *ofType) String() string {
if t.IsEmpty() {
return ""
}
b := strings.Builder{}
if t.name != "" {
b.WriteString(" OF " + t.name)
}
if t.ofDefinitions.Len() > 0 {
b.WriteString(" (" + t.ofDefinitions.String() + ")")
}
return b.String()
}
// { column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]
// | table_constraint }
// [, ... ]
type ofDefinition struct {
// column definition
ofColumn ofColumn
// constrain definition
constraintTable constraintTable
}
// Column get of column
func (c *ofDefinition) Column() *ofColumn {
return &c.ofColumn
}
// Constraint get constraint
func (c *ofDefinition) Constraint() *constraintTable {
return &c.constraintTable
}
// IsEmpty check if empty
func (c *ofDefinition) IsEmpty() bool {
return c == nil || (c.ofColumn.IsEmpty() && c.constraintTable.IsEmpty())
}
// String render column
func (c *ofDefinition) String() string {
if c.IsEmpty() {
return ""
}
b := strings.Builder{}
if !c.ofColumn.IsEmpty() {
b.WriteString(c.ofColumn.String())
} else if !c.constraintTable.IsEmpty() {
b.WriteString(c.constraintTable.String())
}
return b.String()
}
// column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]
type ofColumn struct {
// column name
name string
// WITH OPTIONS
withOptions bool
// constraint
constraint constraintColumn
}
// Name set name
func (c *ofColumn) Name(name string) *ofColumn {
c.name = name
return c
}
// WithOptions reset name
func (c *ofColumn) WithOptions() *ofColumn {
c.withOptions = true
return c
}
// Constraint get constraint
func (c *ofColumn) Constraint() *constraintColumn {
return &c.constraint
}
// IsEmpty check if empty
func (c *ofColumn) IsEmpty() bool {
return c == nil || (c.name == "" && c.constraint.IsEmpty())
}
// String render column
func (c *ofColumn) String() string {
if c.IsEmpty() {
return ""
}
b := strings.Builder{}
if c.name != "" {
b.WriteString(c.name)
}
if c.withOptions {
b.WriteString(" WITH OPTIONS")
}
if !c.constraint.IsEmpty() {
b.WriteString(c.constraint.String())
}
return b.String()
}