format.go
6.4 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
package orm
import (
"bytes"
"fmt"
"sort"
"strconv"
"strings"
"github.com/go-pg/pg/internal"
"github.com/go-pg/pg/internal/parser"
"github.com/go-pg/pg/types"
)
var defaultFmter Formatter
type FormatAppender interface {
AppendFormat([]byte, QueryFormatter) []byte
}
type sepFormatAppender interface {
FormatAppender
AppendSep([]byte) []byte
}
//------------------------------------------------------------------------------
type queryParamsAppender struct {
query string
params []interface{}
}
var _ FormatAppender = (*queryParamsAppender)(nil)
var _ types.ValueAppender = (*queryParamsAppender)(nil)
func Q(query string, params ...interface{}) *queryParamsAppender {
return &queryParamsAppender{query, params}
}
func (q *queryParamsAppender) AppendFormat(b []byte, fmter QueryFormatter) []byte {
return fmter.FormatQuery(b, q.query, q.params...)
}
func (q *queryParamsAppender) AppendValue(b []byte, quote int) []byte {
return q.AppendFormat(b, defaultFmter)
}
func (q *queryParamsAppender) Value() types.Q {
b := q.AppendValue(nil, 1)
return types.Q(internal.BytesToString(b))
}
//------------------------------------------------------------------------------
type condGroupAppender struct {
sep string
cond []sepFormatAppender
}
var _ FormatAppender = (*condAppender)(nil)
var _ sepFormatAppender = (*condAppender)(nil)
func (q *condGroupAppender) AppendSep(b []byte) []byte {
return append(b, q.sep...)
}
func (q *condGroupAppender) AppendFormat(b []byte, fmter QueryFormatter) []byte {
b = append(b, '(')
for i, app := range q.cond {
if i > 0 {
b = app.AppendSep(b)
}
b = app.AppendFormat(b, fmter)
}
b = append(b, ')')
return b
}
//------------------------------------------------------------------------------
type condAppender struct {
sep string
cond string
params []interface{}
}
var _ FormatAppender = (*condAppender)(nil)
var _ sepFormatAppender = (*condAppender)(nil)
func (q *condAppender) AppendSep(b []byte) []byte {
return append(b, q.sep...)
}
func (q *condAppender) AppendFormat(b []byte, fmter QueryFormatter) []byte {
b = append(b, '(')
b = fmter.FormatQuery(b, q.cond, q.params...)
b = append(b, ')')
return b
}
//------------------------------------------------------------------------------
type fieldAppender struct {
field string
}
var _ FormatAppender = (*fieldAppender)(nil)
func (a fieldAppender) AppendFormat(b []byte, fmter QueryFormatter) []byte {
return types.AppendField(b, a.field, 1)
}
//------------------------------------------------------------------------------
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 Formatter struct {
namedParams map[string]interface{}
}
func (f Formatter) String() string {
if len(f.namedParams) == 0 {
return ""
}
var keys []string
for k, _ := range f.namedParams {
keys = append(keys, k)
}
sort.Strings(keys)
var ss []string
for _, k := range keys {
ss = append(ss, fmt.Sprintf("%s=%v", k, f.namedParams[k]))
}
return " " + strings.Join(ss, " ")
}
func (f Formatter) clone() Formatter {
var cp Formatter
for param, value := range f.namedParams {
cp.SetParam(param, value)
}
return cp
}
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) Append(dst []byte, src string, params ...interface{}) []byte {
if (params == nil && f.namedParams == nil) || strings.IndexByte(src, '?') == -1 {
return append(dst, src...)
}
return f.append(dst, parser.NewString(src), params)
}
func (f Formatter) AppendBytes(dst, src []byte, params ...interface{}) []byte {
if (params == nil && f.namedParams == nil) || bytes.IndexByte(src, '?') == -1 {
return append(dst, src...)
}
return f.append(dst, parser.New(src), params)
}
func (f Formatter) FormatQuery(dst []byte, query string, params ...interface{}) []byte {
return f.Append(dst, query, params...)
}
func (f Formatter) append(dst []byte, p *parser.Parser, params []interface{}) []byte {
var paramsIndex int
var namedParamsOnce bool
var tableParams *tableParams
var model TableModel
if len(params) > 0 {
var ok bool
model, ok = params[len(params)-1].(TableModel)
if ok {
params = params[:len(params)-1]
}
}
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(dst, f, id)
if ok {
continue
}
}
if model != nil {
dst, ok = model.AppendParam(dst, f, 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
}
type queryAppender interface {
AppendQuery(dst []byte) ([]byte, error)
}
func (f Formatter) appendParam(b []byte, param interface{}) []byte {
switch param := param.(type) {
case queryAppender:
bb, err := param.AppendQuery(b)
if err != nil {
return types.AppendError(b, err)
}
return bb
case FormatAppender:
return param.AppendFormat(b, f)
default:
return types.Append(b, param, 1)
}
}