-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_table.go
238 lines (202 loc) · 6.46 KB
/
create_table.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package bob
import (
"errors"
"strings"
"github.com/lann/builder"
)
type CreateBuilder builder.Builder
type createData struct {
TableName string
IfNotExists bool
Schema string
Columns []ColumnDef
}
type ColumnDef struct {
Name string
Type string
Extras []string
}
func init() {
builder.Register(CreateBuilder{}, createData{})
}
// name sets the table name
func (b CreateBuilder) name(name string) CreateBuilder {
return builder.Set(b, "TableName", name).(CreateBuilder)
}
// ifNotExists adds IF NOT EXISTS to the query
func (b CreateBuilder) ifNotExists() CreateBuilder {
return builder.Set(b, "IfNotExists", true).(CreateBuilder)
}
// WithSchema specifies the schema to be used when using the schema-building commands.
func (b CreateBuilder) WithSchema(name string) CreateBuilder {
return builder.Set(b, "Schema", name).(CreateBuilder)
}
// StringColumn creates a column with VARCHAR(255) data type.
// For SQLite please refer to TextColumn.
func (b CreateBuilder) StringColumn(name string, extras ...string) CreateBuilder {
return builder.Append(b, "Columns", ColumnDef{
Name: name,
Type: "VARCHAR(255)",
Extras: extras,
}).(CreateBuilder)
}
// TextColumn creates a column with TEXT data type
func (b CreateBuilder) TextColumn(name string, extras ...string) CreateBuilder {
return builder.Append(b, "Columns", ColumnDef{
Name: name,
Type: "TEXT",
Extras: extras,
}).(CreateBuilder)
}
// UUIDColumn only available for PostgreSQL
func (b CreateBuilder) UUIDColumn(name string, extras ...string) CreateBuilder {
return builder.Append(b, "Columns", ColumnDef{
Name: name,
Type: "UUID",
Extras: extras,
}).(CreateBuilder)
}
// BooleanColumn only available for PostgreSQL
func (b CreateBuilder) BooleanColumn(name string, extras ...string) CreateBuilder {
return builder.Append(b, "Columns", ColumnDef{
Name: name,
Type: "BOOLEAN",
Extras: extras,
}).(CreateBuilder)
}
// IntegerColumn only available for PostgreSQL and SQLite.
// For MySQL and MSSQL, please refer to IntColumn,
func (b CreateBuilder) IntegerColumn(name string, extras ...string) CreateBuilder {
return builder.Append(b, "Columns", ColumnDef{
Name: name,
Type: "INTEGER",
Extras: extras,
}).(CreateBuilder)
}
// IntColumn only available for MySQL and MSSQL.
// For PostgreSQL and SQLite please refer to IntegerColumn.
func (b CreateBuilder) IntColumn(name string, extras ...string) CreateBuilder {
return builder.Append(b, "Columns", ColumnDef{
Name: name,
Type: "INT",
Extras: extras,
}).(CreateBuilder)
}
// RealColumn only available for MSSQL, PostgreSQL, and SQLite.
// For MySQL, please refer to FloatColumn, or create your own with AddColumn() with Type: "DOUBLE".
func (b CreateBuilder) RealColumn(name string, extras ...string) CreateBuilder {
return builder.Append(b, "Columns", ColumnDef{
Name: name,
Type: "REAL",
Extras: extras,
}).(CreateBuilder)
}
// FloatColumn only available for MySQL and MSSQL.
// For PostgreSQL and SQLite, please refer to RealColumn.
func (b CreateBuilder) FloatColumn(name string, extras ...string) CreateBuilder {
return builder.Append(b, "Columns", ColumnDef{
Name: name,
Type: "FLOAT",
Extras: extras,
}).(CreateBuilder)
}
func (b CreateBuilder) DateTimeColumn(name string, extras ...string) CreateBuilder {
return builder.Append(b, "Columns", ColumnDef{
Name: name,
Type: "DATETIME",
Extras: extras,
}).(CreateBuilder)
}
func (b CreateBuilder) TimeStampColumn(name string, extras ...string) CreateBuilder {
return builder.Append(b, "Columns", ColumnDef{
Name: name,
Type: "TIMESTAMP",
Extras: extras,
}).(CreateBuilder)
}
func (b CreateBuilder) TimeColumn(name string, extras ...string) CreateBuilder {
return builder.Append(b, "Columns", ColumnDef{
Name: name,
Type: "TIME",
Extras: extras,
}).(CreateBuilder)
}
func (b CreateBuilder) DateColumn(name string, extras ...string) CreateBuilder {
return builder.Append(b, "Columns", ColumnDef{
Name: name,
Type: "DATE",
Extras: extras,
}).(CreateBuilder)
}
// JSONColumn only available for MySQL and PostgreSQL.
// For MSSQL please use AddColumn(bob.ColumnDef{Name: "name", Type: "NVARCHAR(1000)"}).
// Not supported for SQLite.
func (b CreateBuilder) JSONColumn(name string, extras ...string) CreateBuilder {
return builder.Append(b, "Columns", ColumnDef{
Name: name,
Type: "JSON",
Extras: extras,
}).(CreateBuilder)
}
// JSONBColumn only available for PostgreSQL.
// For MySQL please refer to JSONColumn.
func (b CreateBuilder) JSONBColumn(name string, extras ...string) CreateBuilder {
return builder.Append(b, "Columns", ColumnDef{
Name: name,
Type: "JSONB",
Extras: extras,
}).(CreateBuilder)
}
// BlobColumn only available for MySQL and SQLite.
// For PostgreSQL and MSSQL, please use AddColumn(bob.ColumnDef{Name: "name", Type: "BYTEA"}).
func (b CreateBuilder) BlobColumn(name string, extras ...string) CreateBuilder {
return builder.Append(b, "Columns", ColumnDef{
Name: name,
Type: "BLOB",
Extras: extras,
}).(CreateBuilder)
}
// AddColumn sets custom columns
func (b CreateBuilder) AddColumn(column ColumnDef) CreateBuilder {
return builder.Append(b, "Columns", column).(CreateBuilder)
}
// ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (b CreateBuilder) ToSql() (string, []interface{}, error) {
data := builder.GetStruct(b).(createData)
return data.ToSql()
}
// ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (d *createData) ToSql() (sqlStr string, args []interface{}, err error) {
if len(d.TableName) == 0 || d.TableName == "" {
err = errors.New("create statements must specify a table")
return
}
if len(d.Columns) == 0 {
err = errors.New("a table should at least have one column")
return
}
var sql strings.Builder
sql.WriteString("CREATE TABLE ")
if d.IfNotExists {
sql.WriteString("IF NOT EXISTS ")
}
if d.Schema != "" {
sql.WriteString("\"" + d.Schema + "\".")
}
sql.WriteString("\"" + d.TableName + "\"")
sql.WriteString(" ")
var columnTypes []string
for i := 0; i < len(d.Columns); i++ {
var column []string
column = append(column, "\""+d.Columns[i].Name+"\" "+d.Columns[i].Type)
if len(d.Columns[i].Extras) > 0 {
column = append(column, strings.Join(d.Columns[i].Extras, " "))
}
columnTypes = append(columnTypes, strings.Join(column, " "))
}
sql.WriteString("(")
sql.WriteString(strings.Join(columnTypes, ", "))
sql.WriteString(");")
sqlStr = sql.String()
return
}