format.go
7.1 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package orm
import (
"bytes"
"fmt"
"sort"
"strconv"
"strings"
"github.com/go-pg/pg/v10/internal"
"github.com/go-pg/pg/v10/internal/parser"
"github.com/go-pg/pg/v10/types"
)
var defaultFmter = NewFormatter()
type queryWithSepAppender interface {
QueryAppender
AppendSep([]byte) []byte
}
//------------------------------------------------------------------------------
type SafeQueryAppender struct {
query string
params []interface{}
}
var (
_ QueryAppender = (*SafeQueryAppender)(nil)
_ types.ValueAppender = (*SafeQueryAppender)(nil)
)
//nolint
func SafeQuery(query string, params ...interface{}) *SafeQueryAppender {
return &SafeQueryAppender{query, params}
}
func (q *SafeQueryAppender) AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error) {
return fmter.FormatQuery(b, q.query, q.params...), nil
}
func (q *SafeQueryAppender) AppendValue(b []byte, quote int) ([]byte, error) {
return q.AppendQuery(defaultFmter, b)
}
func (q *SafeQueryAppender) Value() types.Safe {
b, err := q.AppendValue(nil, 1)
if err != nil {
return types.Safe(err.Error())
}
return types.Safe(internal.BytesToString(b))
}
//------------------------------------------------------------------------------
type condGroupAppender struct {
sep string
cond []queryWithSepAppender
}
var (
_ QueryAppender = (*condAppender)(nil)
_ queryWithSepAppender = (*condAppender)(nil)
)
func (q *condGroupAppender) AppendSep(b []byte) []byte {
return append(b, q.sep...)
}
func (q *condGroupAppender) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error) {
b = append(b, '(')
for i, app := range q.cond {
if i > 0 {
b = app.AppendSep(b)
}
b, err = app.AppendQuery(fmter, b)
if err != nil {
return nil, err
}
}
b = append(b, ')')
return b, nil
}
//------------------------------------------------------------------------------
type condAppender struct {
sep string
cond string
params []interface{}
}
var (
_ QueryAppender = (*condAppender)(nil)
_ queryWithSepAppender = (*condAppender)(nil)
)
func (q *condAppender) AppendSep(b []byte) []byte {
return append(b, q.sep...)
}
func (q *condAppender) AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error) {
b = append(b, '(')
b = fmter.FormatQuery(b, q.cond, q.params...)
b = append(b, ')')
return b, nil
}
//------------------------------------------------------------------------------
type fieldAppender struct {
field string
}
var _ QueryAppender = (*fieldAppender)(nil)
func (a fieldAppender) AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error) {
return types.AppendIdent(b, a.field, 1), nil
}
//------------------------------------------------------------------------------
type dummyFormatter struct{}
func (f dummyFormatter) FormatQuery(b []byte, query string, params ...interface{}) []byte {
return append(b, query...)
}
func isPlaceholderFormatter(fmter QueryFormatter) bool {
if fmter == nil {
return false
}
b := fmter.FormatQuery(nil, "?", 0)
return bytes.Equal(b, []byte("?"))
}
//------------------------------------------------------------------------------
type QueryFormatter interface {
FormatQuery(b []byte, query string, params ...interface{}) []byte
}
type Formatter struct {
namedParams map[string]interface{}
model TableModel
}
var _ QueryFormatter = (*Formatter)(nil)
func NewFormatter() *Formatter {
return new(Formatter)
}
func (f *Formatter) String() string {
if len(f.namedParams) == 0 {
return ""
}
keys := make([]string, len(f.namedParams))
index := 0
for k := range f.namedParams {
keys[index] = k
index++
}
sort.Strings(keys)
ss := make([]string, len(keys))
for i, k := range keys {
ss[i] = fmt.Sprintf("%s=%v", k, f.namedParams[k])
}
return " " + strings.Join(ss, " ")
}
func (f *Formatter) clone() *Formatter {
cp := NewFormatter()
cp.model = f.model
if len(f.namedParams) > 0 {
cp.namedParams = make(map[string]interface{}, len(f.namedParams))
}
for param, value := range f.namedParams {
cp.setParam(param, value)
}
return cp
}
func (f *Formatter) WithTableModel(model TableModel) *Formatter {
cp := f.clone()
cp.model = model
return cp
}
func (f *Formatter) WithModel(model interface{}) *Formatter {
switch model := model.(type) {
case TableModel:
return f.WithTableModel(model)
case *Query:
return f.WithTableModel(model.tableModel)
case QueryCommand:
return f.WithTableModel(model.Query().tableModel)
default:
panic(fmt.Errorf("pg: unsupported model %T", model))
}
}
func (f *Formatter) setParam(param string, value interface{}) {
if f.namedParams == nil {
f.namedParams = make(map[string]interface{})
}
f.namedParams[param] = value
}
func (f *Formatter) WithParam(param string, value interface{}) *Formatter {
cp := f.clone()
cp.setParam(param, value)
return cp
}
func (f *Formatter) Param(param string) interface{} {
return f.namedParams[param]
}
func (f *Formatter) hasParams() bool {
return len(f.namedParams) > 0 || f.model != nil
}
func (f *Formatter) FormatQueryBytes(dst, query []byte, params ...interface{}) []byte {
if (params == nil && !f.hasParams()) || bytes.IndexByte(query, '?') == -1 {
return append(dst, query...)
}
return f.append(dst, parser.New(query), params)
}
func (f *Formatter) FormatQuery(dst []byte, query string, params ...interface{}) []byte {
if (params == nil && !f.hasParams()) || strings.IndexByte(query, '?') == -1 {
return append(dst, query...)
}
return f.append(dst, parser.NewString(query), params)
}
func (f *Formatter) append(dst []byte, p *parser.Parser, params []interface{}) []byte {
var paramsIndex int
var namedParamsOnce bool
var tableParams *tableParams
for p.Valid() {
b, ok := p.ReadSep('?')
if !ok {
dst = append(dst, b...)
continue
}
if len(b) > 0 && b[len(b)-1] == '\\' {
dst = append(dst, b[:len(b)-1]...)
dst = append(dst, '?')
continue
}
dst = append(dst, b...)
id, numeric := p.ReadIdentifier()
if id != "" {
if numeric {
idx, err := strconv.Atoi(id)
if err != nil {
goto restore_param
}
if idx >= len(params) {
goto restore_param
}
dst = f.appendParam(dst, params[idx])
continue
}
if f.namedParams != nil {
param, paramOK := f.namedParams[id]
if paramOK {
dst = f.appendParam(dst, param)
continue
}
}
if !namedParamsOnce && len(params) > 0 {
namedParamsOnce = true
tableParams, _ = newTableParams(params[len(params)-1])
}
if tableParams != nil {
dst, ok = tableParams.AppendParam(f, dst, id)
if ok {
continue
}
}
if f.model != nil {
dst, ok = f.model.AppendParam(f, dst, id)
if ok {
continue
}
}
restore_param:
dst = append(dst, '?')
dst = append(dst, id...)
continue
}
if paramsIndex >= len(params) {
dst = append(dst, '?')
continue
}
param := params[paramsIndex]
paramsIndex++
dst = f.appendParam(dst, param)
}
return dst
}
func (f *Formatter) appendParam(b []byte, param interface{}) []byte {
switch param := param.(type) {
case QueryAppender:
bb, err := param.AppendQuery(f, b)
if err != nil {
return types.AppendError(b, err)
}
return bb
default:
return types.Append(b, param, 1)
}
}