forked from uptrace/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_index_create.go
246 lines (198 loc) · 5.19 KB
/
query_index_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
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
package bun
import (
"context"
"database/sql"
"github.com/uptrace/bun/internal"
"github.com/uptrace/bun/schema"
)
type CreateIndexQuery struct {
whereBaseQuery
unique bool
fulltext bool
spatial bool
concurrently bool
ifNotExists bool
index schema.QueryWithArgs
using schema.QueryWithArgs
include []schema.QueryWithArgs
}
func NewCreateIndexQuery(db *DB) *CreateIndexQuery {
q := &CreateIndexQuery{
whereBaseQuery: whereBaseQuery{
baseQuery: baseQuery{
db: db,
dbi: db.DB,
},
},
}
return q
}
func (q *CreateIndexQuery) DB(db DBI) *CreateIndexQuery {
q.dbi = db
return q
}
func (q *CreateIndexQuery) Model(model interface{}) *CreateIndexQuery {
q.setTableModel(model)
return q
}
func (q *CreateIndexQuery) Unique() *CreateIndexQuery {
q.unique = true
return q
}
func (q *CreateIndexQuery) Concurrently() *CreateIndexQuery {
q.concurrently = true
return q
}
func (q *CreateIndexQuery) IfNotExists() *CreateIndexQuery {
q.ifNotExists = true
return q
}
//------------------------------------------------------------------------------
func (q *CreateIndexQuery) Index(query string, args ...interface{}) *CreateIndexQuery {
q.index = schema.SafeQuery(query, args)
return q
}
//------------------------------------------------------------------------------
func (q *CreateIndexQuery) Table(tables ...string) *CreateIndexQuery {
for _, table := range tables {
q.addTable(schema.UnsafeIdent(table))
}
return q
}
func (q *CreateIndexQuery) TableExpr(query string, args ...interface{}) *CreateIndexQuery {
q.addTable(schema.SafeQuery(query, args))
return q
}
func (q *CreateIndexQuery) ModelTableExpr(query string, args ...interface{}) *CreateIndexQuery {
q.modelTable = schema.SafeQuery(query, args)
return q
}
func (q *CreateIndexQuery) Using(query string, args ...interface{}) *CreateIndexQuery {
q.using = schema.SafeQuery(query, args)
return q
}
//------------------------------------------------------------------------------
func (q *CreateIndexQuery) Column(columns ...string) *CreateIndexQuery {
for _, column := range columns {
q.addColumn(schema.UnsafeIdent(column))
}
return q
}
func (q *CreateIndexQuery) ColumnExpr(query string, args ...interface{}) *CreateIndexQuery {
q.addColumn(schema.SafeQuery(query, args))
return q
}
func (q *CreateIndexQuery) ExcludeColumn(columns ...string) *CreateIndexQuery {
q.excludeColumn(columns)
return q
}
//------------------------------------------------------------------------------
func (q *CreateIndexQuery) Include(columns ...string) *CreateIndexQuery {
for _, column := range columns {
q.include = append(q.include, schema.UnsafeIdent(column))
}
return q
}
func (q *CreateIndexQuery) IncludeExpr(query string, args ...interface{}) *CreateIndexQuery {
q.include = append(q.include, schema.SafeQuery(query, args))
return q
}
//------------------------------------------------------------------------------
func (q *CreateIndexQuery) Where(query string, args ...interface{}) *CreateIndexQuery {
q.addWhere(schema.SafeQueryWithSep(query, args, " AND "))
return q
}
func (q *CreateIndexQuery) WhereOr(query string, args ...interface{}) *CreateIndexQuery {
q.addWhere(schema.SafeQueryWithSep(query, args, " OR "))
return q
}
func (q *CreateIndexQuery) WhereGroup(sep string, fn func(*WhereQuery)) *CreateIndexQuery {
q.addWhereGroup(sep, fn)
return q
}
//------------------------------------------------------------------------------
func (q *CreateIndexQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, err error) {
if q.err != nil {
return nil, q.err
}
b = append(b, "CREATE "...)
if q.unique {
b = append(b, "UNIQUE "...)
}
if q.fulltext {
b = append(b, "FULLTEXT "...)
}
if q.spatial {
b = append(b, "SPATIAL "...)
}
b = append(b, "INDEX "...)
if q.concurrently {
b = append(b, "CONCURRENTLY "...)
}
if q.ifNotExists {
b = append(b, "IF NOT EXISTS "...)
}
b, err = q.index.AppendQuery(fmter, b)
if err != nil {
return nil, err
}
b = append(b, " ON "...)
b, err = q.appendFirstTable(fmter, b)
if err != nil {
return nil, err
}
if !q.using.IsZero() {
b = append(b, " USING "...)
b, err = q.using.AppendQuery(fmter, b)
if err != nil {
return nil, err
}
}
b = append(b, " ("...)
for i, col := range q.columns {
if i > 0 {
b = append(b, ", "...)
}
b, err = col.AppendQuery(fmter, b)
if err != nil {
return nil, err
}
}
b = append(b, ')')
if len(q.include) > 0 {
b = append(b, " INCLUDE ("...)
for i, col := range q.include {
if i > 0 {
b = append(b, ", "...)
}
b, err = col.AppendQuery(fmter, b)
if err != nil {
return nil, err
}
}
b = append(b, ')')
}
b, err = q.appendWhere(fmter, b)
if err != nil {
return nil, err
}
return b, nil
}
//------------------------------------------------------------------------------
func (q *CreateIndexQuery) Exec(
ctx context.Context, dest ...interface{},
) (res sql.Result, err error) {
bs := getByteSlice()
defer putByteSlice(bs)
queryBytes, err := q.AppendQuery(q.db.fmter, bs.b)
if err != nil {
return res, err
}
bs.b = queryBytes
query := internal.String(queryBytes)
res, err = q.exec(ctx, q, query)
if err != nil {
return res, err
}
return res, nil
}