-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlr.go
More file actions
executable file
·387 lines (340 loc) · 9.44 KB
/
sqlr.go
File metadata and controls
executable file
·387 lines (340 loc) · 9.44 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package sqlr
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
"sync"
)
// Dialect identifies the SQL dialect for placeholder rendering and a few
// dialect-specific parsing behaviors.
type Dialect int
// SQLR is the main entry point. It holds the selected dialect, configuration,
// and a pool of reusable *Builder instances.
// A single SQLR instance is safe for concurrent use.
type SQLR struct {
dialect Dialect
config Config
pool sync.Pool
}
// Builder assembles a single SQL statement and bound parameters.
// It is NOT safe for concurrent use and is single-use: after Build() it is
// automatically released back to the pool and must not be used again.
type Builder struct {
s *SQLR
parts []string
inputs []any
released bool
bag P
err error
}
// Config defines limits and behavior tweaks for the parser/binder.
type Config struct {
// MaxParams limits the total number of placeholders that can be emitted by
// a single Build().
// If = 0 (or omitted), it uses a sensible per-dialect default.
// If < 0, it's treated as "unlimited".
MaxParams int
// MaxNameLen limits the maximum allowed length of a placeholder name,
// e.g. ":this_is_a_name". Names longer than this cause ErrParamNameTooLong.
MaxNameLen int
}
// P is a convenient alias for map[string]any to use with Bind().
type P = map[string]any
// Execer abstracts *sql.DB / *sql.Tx ExecContext for easy testing.
type Execer interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}
// Queryer abstracts *sql.DB / *sql.Tx QueryContext for easy testing.
type Queryer interface {
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
}
const (
Postgres Dialect = iota
MySQL
SQLite
SQLServer
)
const cacheSize = 4096 // Default size for the field-index cache
var (
ErrParamMissing = errors.New("sqlr: missing parameter")
ErrSliceEmpty = errors.New("sqlr: empty slice")
ErrRowsEmpty = errors.New("sqlr: empty rows")
ErrRowsMalformed = errors.New("sqlr: malformed :rows placeholder")
ErrColumnNotFound = errors.New("sqlr: column not found")
ErrTooManyParams = errors.New("sqlr: too many parameters")
ErrParamNameTooLong = errors.New("sqlr: parameter name too long")
ErrFieldAmbiguous = errors.New("sqlr: ambiguous field name")
ErrBuilderReleased = errors.New("sqlr: builder already released; call Write() on *SQLR for a new query")
ErrMoreThanOneRow = errors.New("sqlr: more than one row")
)
// String returns the string representation of the dialect.
func (d Dialect) String() string {
switch d {
case Postgres:
return "postgres"
case MySQL:
return "mysql"
case SQLite:
return "sqlite"
case SQLServer:
return "sqlserver"
default:
return "unknown"
}
}
// New returns a new SQLR for the given dialect. Optionally provide a Config;
// unspecified fields fall back to sensible per-dialect defaults.
func New(dialect Dialect, cfg ...Config) *SQLR {
s := &SQLR{
dialect: dialect,
config: defaultConfig(dialect, cfg...),
}
s.pool.New = func() any {
return &Builder{
s: s,
parts: make([]string, 0, 16),
inputs: make([]any, 0, 8),
}
}
return s
}
// Write starts a new statement and returns a single-use Builder.
// You can add more chunks via Write/Writef, and bind data via Bind().
func (s *SQLR) Write(sql string) *Builder {
b := s.pool.Get().(*Builder)
b.s = s
b.released = false
b.err = nil
b.parts = b.parts[:0]
b.inputs = b.inputs[:0]
if sql != "" {
b.parts = append(b.parts, sql)
}
return b
}
// Write appends a raw SQL fragment. No auto-spacing is performed.
func (b *Builder) Write(sql string) *Builder {
if b.released {
b.err = ErrBuilderReleased
return b
}
if b.err != nil {
return b
}
b.parts = append(b.parts, sql)
return b
}
// Writef appends a formatted SQL fragment. No auto-spacing is performed.
func (b *Builder) Writef(format string, args ...any) *Builder {
if b.released {
b.err = ErrBuilderReleased
return b
}
if b.err != nil {
return b
}
b.parts = append(b.parts, fmt.Sprintf(format, args...))
return b
}
// Bind enqueues a parameter source. Supported forms:
// - nil (ignored)
// - struct with `db` tags (flattened through nested structs)
// - map[string]any or any reflect.Map
// - []struct / []map for :rows{...}
// - slices of primitives for :name expansion
// - k/v pairs (even number of args, first is string key)
//
// Multiple Bind() calls are allowed; resolution is "last one wins".
func (b *Builder) Bind(args ...any) *Builder {
if b.released {
b.err = ErrBuilderReleased
return b
}
if b.err != nil {
return b
}
switch len(args) {
case 0:
b.ensureBag()
return b
case 1:
if args[0] != nil {
b.inputs = append(b.inputs, args[0])
}
return b
default:
if len(args)%2 != 0 {
b.err = fmt.Errorf("sqlr: Bind expects even number of args (key,value,...), got %d", len(args))
return b
}
bag := b.ensureBag()
for i := 0; i < len(args); i += 2 {
k, ok := args[i].(string)
if !ok || k == "" {
b.err = fmt.Errorf("sqlr: Bind key at position %d must be a non-empty string (got %T)", i, args[i])
return b
}
bag[k] = args[i+1]
}
return b
}
}
// Build concatenates the query, performs binding, and RELEASES the builder
// back into the pool. After Build(), the builder must not be used again.
func (b *Builder) Build() (string, []any, error) {
if b.released {
return "", nil, ErrBuilderReleased
}
// Snapshot before defer to avoid races with Release()
q := strings.Join(b.parts, "")
d := b.s.dialect
cfg := b.s.config
// Local copy of inputs and append bag only if it has something
in := b.inputs
if len(b.bag) > 0 {
in = append(in, b.bag)
}
defer b.Release()
if b.err != nil {
return "", nil, b.err
}
out, args, err := parse(d, q, in, cfg)
return out, args, err
}
// Preview renders the SQL statement and bound args without releasing the Builder.
// Safe to call multiple times; identical to Build() except it does NOT Release().
// Use this to log/inspect the exact SQL and args that would be produced.
//
// If the builder has already been released, it returns ErrBuilderReleased.
func (b *Builder) Preview() (string, []any, error) {
if b.released {
return "", nil, ErrBuilderReleased
}
if b.err != nil {
return "", nil, b.err
}
q := strings.Join(b.parts, "")
d := b.s.dialect
cfg := b.s.config
// Local copy of inputs; append bag only if it has entries.
in := b.inputs
if len(b.bag) > 0 {
in = append(in, b.bag)
}
out, args, err := parse(d, q, in, cfg)
return out, args, err
}
// Release clears the builder and puts it back into the pool.
// It is safe to call Release multiple times; subsequent calls are no-ops.
func (b *Builder) Release() {
if b.released {
return
}
b.released = true
for i := range b.parts {
b.parts[i] = ""
}
b.parts = b.parts[:0]
for i := range b.inputs {
b.inputs[i] = nil
}
b.inputs = b.inputs[:0]
b.bag = nil
b.err = nil
b.s.pool.Put(b)
}
// Scalar wraps a value to force it to be treated as a single scalar argument
// even if it is a slice/array. Useful for ANY(:ids)-style idioms.
func Scalar(v any) any {
return scalar{v: v}
}
// Exec is a convenience that builds and executes the statement with context.Background().
func (b *Builder) Exec(db Execer) (sql.Result, error) {
return b.ExecContext(context.Background(), db)
}
// ScanOne builds and runs the statement, scanning exactly one row into dest.
// It returns sql.ErrNoRows if no rows are returned. It errors if more than one row.
func (b *Builder) ScanOne(db Queryer, dest any) error {
return b.ScanOneContext(context.Background(), db, dest)
}
// ScanAll builds and runs the statement, scanning all rows into dest slice.
func (b *Builder) ScanAll(db Queryer, dest any) error {
return b.ScanAllContext(context.Background(), db, dest)
}
// ExecContext builds and executes the statement with the provided context.
func (b *Builder) ExecContext(ctx context.Context, db Execer) (sql.Result, error) {
q, args, err := b.Build()
if err != nil {
return nil, err
}
return db.ExecContext(ctx, q, args...)
}
// ScanOneContext is the context-aware variant of ScanOne.
func (b *Builder) ScanOneContext(ctx context.Context, db Queryer, dest any) error {
q, args, err := b.Build()
if err != nil {
return err
}
rows, err := db.QueryContext(ctx, q, args...)
if err != nil {
return err
}
defer rows.Close()
if !rows.Next() {
if err := rows.Err(); err != nil {
return err
}
return sql.ErrNoRows
}
if err := scanOne(rows, dest); err != nil {
return err
}
// Must be at most ONE row
if rows.Next() {
return ErrMoreThanOneRow
}
return rows.Err()
}
// ScanAllContext is the context-aware variant of ScanAll.
func (b *Builder) ScanAllContext(ctx context.Context, db Queryer, dest any) error {
q, args, err := b.Build()
if err != nil {
return err
}
rows, err := db.QueryContext(ctx, q, args...)
if err != nil {
return err
}
defer rows.Close()
return scanAll(rows, dest)
}
// ensureBag makes sure the builder has a P bag for Bind(); creates if needed.
func (b *Builder) ensureBag() P {
if b.bag == nil {
b.bag = make(P, 8)
}
return b.bag
}
// defaultConfig merges user config with per-dialect defaults.
func defaultConfig(dialect Dialect, config ...Config) Config {
c := Config{}
if len(config) > 0 {
c = config[0]
}
if c.MaxParams == 0 {
switch dialect {
case SQLServer:
c.MaxParams = 2100
case SQLite:
c.MaxParams = 999
case Postgres, MySQL:
c.MaxParams = 65535
}
}
if c.MaxNameLen <= 0 {
c.MaxNameLen = 64
}
return c
}