table_create.go
5.0 KB
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
239
240
241
242
243
244
245
246
247
248
package orm
import (
"sort"
"strconv"
"github.com/go-pg/pg/v10/types"
)
type CreateTableOptions struct {
Varchar int // replaces PostgreSQL data type `text` with `varchar(n)`
Temp bool
IfNotExists bool
// FKConstraints causes CreateTable to create foreign key constraints
// for has one relations. ON DELETE hook can be added using tag
// `pg:"on_delete:RESTRICT"` on foreign key field. ON UPDATE hook can be added using tag
// `pg:"on_update:CASCADE"`
FKConstraints bool
}
type CreateTableQuery struct {
q *Query
opt *CreateTableOptions
}
var (
_ QueryAppender = (*CreateTableQuery)(nil)
_ QueryCommand = (*CreateTableQuery)(nil)
)
func NewCreateTableQuery(q *Query, opt *CreateTableOptions) *CreateTableQuery {
return &CreateTableQuery{
q: q,
opt: opt,
}
}
func (q *CreateTableQuery) String() string {
b, err := q.AppendQuery(defaultFmter, nil)
if err != nil {
panic(err)
}
return string(b)
}
func (q *CreateTableQuery) Operation() QueryOp {
return CreateTableOp
}
func (q *CreateTableQuery) Clone() QueryCommand {
return &CreateTableQuery{
q: q.q.Clone(),
opt: q.opt,
}
}
func (q *CreateTableQuery) Query() *Query {
return q.q
}
func (q *CreateTableQuery) AppendTemplate(b []byte) ([]byte, error) {
return q.AppendQuery(dummyFormatter{}, b)
}
func (q *CreateTableQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error) {
if q.q.stickyErr != nil {
return nil, q.q.stickyErr
}
if q.q.tableModel == nil {
return nil, errModelNil
}
table := q.q.tableModel.Table()
b = append(b, "CREATE "...)
if q.opt != nil && q.opt.Temp {
b = append(b, "TEMP "...)
}
b = append(b, "TABLE "...)
if q.opt != nil && q.opt.IfNotExists {
b = append(b, "IF NOT EXISTS "...)
}
b, err = q.q.appendFirstTable(fmter, b)
if err != nil {
return nil, err
}
b = append(b, " ("...)
for i, field := range table.Fields {
if i > 0 {
b = append(b, ", "...)
}
b = append(b, field.Column...)
b = append(b, " "...)
b = q.appendSQLType(b, field)
if field.hasFlag(NotNullFlag) {
b = append(b, " NOT NULL"...)
}
if field.hasFlag(UniqueFlag) {
b = append(b, " UNIQUE"...)
}
if field.Default != "" {
b = append(b, " DEFAULT "...)
b = append(b, field.Default...)
}
}
b = appendPKConstraint(b, table.PKs)
b = appendUniqueConstraints(b, table)
if q.opt != nil && q.opt.FKConstraints {
for _, rel := range table.Relations {
b = q.appendFKConstraint(fmter, b, rel)
}
}
b = append(b, ")"...)
if table.PartitionBy != "" {
b = append(b, " PARTITION BY "...)
b = append(b, table.PartitionBy...)
}
if table.Tablespace != "" {
b = q.appendTablespace(b, table.Tablespace)
}
return b, q.q.stickyErr
}
func (q *CreateTableQuery) appendSQLType(b []byte, field *Field) []byte {
if field.UserSQLType != "" {
return append(b, field.UserSQLType...)
}
if q.opt != nil && q.opt.Varchar > 0 &&
field.SQLType == "text" {
b = append(b, "varchar("...)
b = strconv.AppendInt(b, int64(q.opt.Varchar), 10)
b = append(b, ")"...)
return b
}
if field.hasFlag(PrimaryKeyFlag) {
return append(b, pkSQLType(field.SQLType)...)
}
return append(b, field.SQLType...)
}
func pkSQLType(s string) string {
switch s {
case pgTypeSmallint:
return pgTypeSmallserial
case pgTypeInteger:
return pgTypeSerial
case pgTypeBigint:
return pgTypeBigserial
}
return s
}
func appendPKConstraint(b []byte, pks []*Field) []byte {
if len(pks) == 0 {
return b
}
b = append(b, ", PRIMARY KEY ("...)
b = appendColumns(b, "", pks)
b = append(b, ")"...)
return b
}
func appendUniqueConstraints(b []byte, table *Table) []byte {
keys := make([]string, 0, len(table.Unique))
for key := range table.Unique {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
b = appendUnique(b, table.Unique[key])
}
return b
}
func appendUnique(b []byte, fields []*Field) []byte {
b = append(b, ", UNIQUE ("...)
b = appendColumns(b, "", fields)
b = append(b, ")"...)
return b
}
func (q *CreateTableQuery) appendFKConstraint(fmter QueryFormatter, b []byte, rel *Relation) []byte {
if rel.Type != HasOneRelation {
return b
}
b = append(b, ", FOREIGN KEY ("...)
b = appendColumns(b, "", rel.BaseFKs)
b = append(b, ")"...)
b = append(b, " REFERENCES "...)
b = fmter.FormatQuery(b, string(rel.JoinTable.SQLName))
b = append(b, " ("...)
b = appendColumns(b, "", rel.JoinFKs)
b = append(b, ")"...)
if s := onDelete(rel.BaseFKs); s != "" {
b = append(b, " ON DELETE "...)
b = append(b, s...)
}
if s := onUpdate(rel.BaseFKs); s != "" {
b = append(b, " ON UPDATE "...)
b = append(b, s...)
}
return b
}
func (q *CreateTableQuery) appendTablespace(b []byte, tableSpace types.Safe) []byte {
b = append(b, " TABLESPACE "...)
b = append(b, tableSpace...)
return b
}
func onDelete(fks []*Field) string {
var onDelete string
for _, f := range fks {
if f.OnDelete != "" {
onDelete = f.OnDelete
break
}
}
return onDelete
}
func onUpdate(fks []*Field) string {
var onUpdate string
for _, f := range fks {
if f.OnUpdate != "" {
onUpdate = f.OnUpdate
break
}
}
return onUpdate
}