forked from umisama/go-sqlbuilder
-
Notifications
You must be signed in to change notification settings - Fork 2
/
create.go
214 lines (186 loc) · 4.41 KB
/
create.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
package sqlbuilder
// CreateIndexStatement represents a "CREATE INDEX" statement.
type CreateIndexStatement struct {
table Table
columns []Column
name string
ifNotExists bool
err error
}
// CreateTableStatement represents a "CREATE TABLE" statement.
type CreateTableStatement struct {
table Table
ifNotExists bool
err error
}
// CreateTable returns new "CREATE TABLE" statement. The table is Table object to create.
func CreateTable(tbl Table) *CreateTableStatement {
if tbl == nil {
return &CreateTableStatement{
err: newError("table is nil."),
}
}
if _, ok := tbl.(*table); !ok {
return &CreateTableStatement{
err: newError("CreateTable can use only natural table."),
}
}
return &CreateTableStatement{
table: tbl,
}
}
// IfNotExists sets "IF NOT EXISTS" clause.
func (b *CreateTableStatement) IfNotExists() *CreateTableStatement {
if b.err != nil {
return b
}
b.ifNotExists = true
return b
}
// ToSql generates query string, placeholder arguments, and error.
func (b *CreateTableStatement) ToSql() (query string, args []interface{}, err error) {
bldr := newBuilder()
defer func() {
query, args, err = bldr.Query(), bldr.Args(), bldr.Err()
}()
if b.err != nil {
bldr.SetError(b.err)
return
}
bldr.Append("CREATE TABLE ")
if b.ifNotExists {
bldr.Append("IF NOT EXISTS ")
}
bldr.AppendItem(b.table)
if len(b.table.Columns()) != 0 {
bldr.Append(" ( ")
bldr.AppendItem(createTableColumnList(b.table.Columns()))
bldr.Append(" )")
} else {
bldr.SetError(newError("CreateTableStatement needs one or more columns."))
return
}
// table option
if tabopt, err := dialect().TableOptionToString(b.table.Option()); err == nil {
if len(tabopt) != 0 {
bldr.Append(" " + tabopt)
}
} else {
bldr.SetError(err)
}
return
}
// CreateIndex returns new "CREATE INDEX" statement. The table is Table object to create index.
func CreateIndex(tbl Table) *CreateIndexStatement {
if tbl == nil {
return &CreateIndexStatement{
err: newError("table is nil."),
}
}
if _, ok := tbl.(*table); !ok {
return &CreateIndexStatement{
err: newError("CreateTable can use only natural table."),
}
}
return &CreateIndexStatement{
table: tbl,
}
}
// IfNotExists sets "IF NOT EXISTS" clause.
func (b *CreateIndexStatement) IfNotExists() *CreateIndexStatement {
if b.err != nil {
return b
}
b.ifNotExists = true
return b
}
// IfNotExists sets "IF NOT EXISTS" clause. If not set this, returns error on ToSql().
func (b *CreateIndexStatement) Columns(columns ...Column) *CreateIndexStatement {
if b.err != nil {
return b
}
b.columns = columns
return b
}
// Name sets name for index.
// If not set this, auto generated name will be used.
func (b *CreateIndexStatement) Name(name string) *CreateIndexStatement {
if b.err != nil {
return b
}
b.name = name
return b
}
// ToSql generates query string, placeholder arguments, and returns err on errors.
func (b *CreateIndexStatement) ToSql() (query string, args []interface{}, err error) {
bldr := newBuilder()
defer func() {
query, args, err = bldr.Query(), bldr.Args(), bldr.Err()
}()
if b.err != nil {
bldr.SetError(b.err)
return
}
bldr.Append("CREATE INDEX ")
if b.ifNotExists {
bldr.Append("IF NOT EXISTS ")
}
if len(b.name) != 0 {
bldr.Append(dialect().QuoteField(b.name))
} else {
bldr.SetError(newError("name was not setted."))
return
}
bldr.Append(" ON ")
bldr.AppendItem(b.table)
if len(b.columns) != 0 {
bldr.Append(" ( ")
bldr.AppendItem(createIndexColumnList(b.columns))
bldr.Append(" )")
} else {
bldr.SetError(newError("columns was not setted."))
return
}
return
}
type createTableColumnList []Column
func (m createTableColumnList) serialize(bldr *builder) {
first := true
for _, column := range m {
if first {
first = false
} else {
bldr.Append(", ")
}
cc := column.config()
// Column name
bldr.AppendItem(cc)
bldr.Append(" ")
// SQL data name
str, err := dialect().ColumnTypeToString(cc)
if err != nil {
bldr.SetError(err)
}
bldr.Append(str)
str, err = dialect().ColumnOptionToString(cc.Option())
if err != nil {
bldr.SetError(err)
}
if len(str) != 0 {
bldr.Append(" " + str)
}
}
}
type createIndexColumnList []Column
func (m createIndexColumnList) serialize(bldr *builder) {
first := true
for _, column := range m {
if first {
first = false
} else {
bldr.Append(", ")
}
cc := column.config()
bldr.AppendItem(cc)
}
}