forked from go-gorp/gorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_bindings.go
405 lines (355 loc) · 10.8 KB
/
table_bindings.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright 2012 James Cooper. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gorp
import (
"bytes"
"fmt"
"reflect"
"sync"
"sync/atomic"
)
// BindingCache is a thread-safe cache for binding plans
type BindingCache struct {
cache sync.Map // map[reflect.Type]*bindPlan
}
// Get returns a cached binding plan or creates a new one
func (c *BindingCache) Get(key reflect.Type, creator func() *bindPlan) *bindPlan {
if plan, ok := c.cache.Load(key); ok {
return plan.(*bindPlan)
}
plan := creator()
actual, _ := c.cache.LoadOrStore(key, plan)
return actual.(*bindPlan)
}
// CustomScanner binds a database column value to a Go type.
// It provides type-safe scanning of database values into Go types.
//
// Example:
//
// type JSONField struct {
// Data map[string]interface{}
// }
//
// scanner := &CustomScanner{
// Holder: &[]byte{},
// Target: &field.Data,
// Binder: func(holder, target interface{}) error {
// b := holder.(*[]byte)
// return json.Unmarshal(*b, target)
// },
// }
type CustomScanner struct {
// After a row is scanned, Holder will contain the value from the database column.
// Initialize the CustomScanner with the concrete Go type you wish the database
// driver to scan the raw column into.
Holder interface{}
// Target typically holds a pointer to the target struct field to bind the Holder
// value to.
Target interface{}
// Binder is a custom function that converts the holder value to the target type
// and sets target accordingly. This function should return error if a problem
// occurs converting the holder to the target.
Binder func(holder interface{}, target interface{}) error
}
// ColumnFilter is used to filter columns when selectively updating.
// Return true to include the column in the update.
//
// Example:
//
// // Only update non-empty string columns
// filter := func(col *ColumnMap) bool {
// if v, ok := col.value.(string); ok {
// return v != ""
// }
// return true
// }
type ColumnFilter func(*ColumnMap) bool
// acceptAllFilter is the default filter that includes all columns
func acceptAllFilter(col *ColumnMap) bool {
return true
}
// Bind is called automatically by gorp after Scan()
func (cs CustomScanner) Bind() error {
return cs.Binder(cs.Holder, cs.Target)
}
// bindPlan represents a cached query plan for a specific operation
type bindPlan struct {
query string
argFields []string
keyFields []string
versField string
autoIncrIdx int
autoIncrFieldName string
once sync.Once
prepared atomic.Bool // Track if the plan has been prepared
}
// TypedBindPlan provides type-safe binding operations
type TypedBindPlan[T any] struct {
*bindPlan
conv TypeConverter
}
// NewTypedBindPlan creates a new type-safe binding plan
func NewTypedBindPlan[T any](plan *bindPlan, conv TypeConverter) *TypedBindPlan[T] {
return &TypedBindPlan[T]{
bindPlan: plan,
conv: conv,
}
}
// bindInstance represents a single execution of a bind plan
type bindInstance struct {
query string
args []interface{}
keys []interface{}
existingVersion int64
versField string
autoIncrIdx int
autoIncrFieldName string
}
// createBindInstance creates a new bind instance from the plan
func (plan *bindPlan) createBindInstance(elem reflect.Value, conv TypeConverter) (bindInstance, error) {
bi := bindInstance{
query: plan.query,
autoIncrIdx: plan.autoIncrIdx,
autoIncrFieldName: plan.autoIncrFieldName,
versField: plan.versField,
}
if plan.versField != "" {
bi.existingVersion = elem.FieldByName(plan.versField).Int()
}
// Pre-allocate slices to avoid reallocations
bi.args = make([]interface{}, 0, len(plan.argFields))
bi.keys = make([]interface{}, 0, len(plan.keyFields))
// Process argument fields
if err := plan.processArgFields(elem, conv, &bi); err != nil {
return bindInstance{}, err
}
// Process key fields
if err := plan.processKeyFields(elem, conv, &bi); err != nil {
return bindInstance{}, err
}
return bi, nil
}
// processArgFields processes argument fields for binding
func (plan *bindPlan) processArgFields(elem reflect.Value, conv TypeConverter, bi *bindInstance) error {
for _, field := range plan.argFields {
if field == versFieldConst {
newVer := bi.existingVersion + 1
bi.args = append(bi.args, newVer)
if bi.existingVersion == 0 {
elem.FieldByName(plan.versField).SetInt(int64(newVer))
}
continue
}
val := elem.FieldByName(field).Interface()
if conv != nil {
var err error
val, err = conv.ToDb(val)
if err != nil {
return fmt.Errorf("error converting field %s: %w", field, err)
}
}
bi.args = append(bi.args, val)
}
return nil
}
// processKeyFields processes key fields for binding
func (plan *bindPlan) processKeyFields(elem reflect.Value, conv TypeConverter, bi *bindInstance) error {
for _, field := range plan.keyFields {
val := elem.FieldByName(field).Interface()
if conv != nil {
var err error
val, err = conv.ToDb(val)
if err != nil {
return fmt.Errorf("error converting key field %s: %w", field, err)
}
}
bi.keys = append(bi.keys, val)
}
return nil
}
// CreateTypedInstance creates a type-safe bind instance
func (plan *TypedBindPlan[T]) CreateTypedInstance(elem T) (bindInstance, error) {
return plan.createBindInstance(reflect.ValueOf(elem), plan.conv)
}
// bindInsert creates a bind instance for an insert
func (t *TableMap) bindInsert(elem reflect.Value) (bindInstance, error) {
plan := &t.insertPlan
plan.once.Do(func() {
plan.autoIncrIdx = -1
s := bytes.Buffer{}
s2 := bytes.Buffer{}
s.WriteString(fmt.Sprintf("insert into %s (", t.dbmap.Dialect.QuotedTableForQuery(t.SchemaName, t.TableName)))
x := 0
first := true
for y := range t.Columns {
col := t.Columns[y]
if !(col.isAutoIncr && t.dbmap.Dialect.AutoIncrBindValue() == "") {
if !col.Transient {
if !first {
s.WriteString(",")
s2.WriteString(",")
}
s.WriteString(t.dbmap.Dialect.QuoteField(col.ColumnName))
if col.isAutoIncr {
s2.WriteString(t.dbmap.Dialect.AutoIncrBindValue())
plan.autoIncrIdx = y
plan.autoIncrFieldName = col.fieldName
} else {
if col.DefaultValue == "" {
s2.WriteString(t.dbmap.Dialect.BindVar(x))
if col == t.version {
plan.versField = col.fieldName
plan.argFields = append(plan.argFields, versFieldConst)
} else {
plan.argFields = append(plan.argFields, col.fieldName)
}
x++
} else {
s2.WriteString(col.DefaultValue)
}
}
first = false
}
} else {
plan.autoIncrIdx = y
plan.autoIncrFieldName = col.fieldName
}
}
s.WriteString(") values (")
s.WriteString(s2.String())
s.WriteString(")")
if plan.autoIncrIdx > -1 {
s.WriteString(t.dbmap.Dialect.AutoIncrInsertSuffix(t.Columns[plan.autoIncrIdx]))
}
s.WriteString(t.dbmap.Dialect.QuerySuffix())
plan.query = s.String()
})
return plan.createBindInstance(elem, t.dbmap.TypeConverter)
}
// bindUpdate creates a bind instance for an update
func (t *TableMap) bindUpdate(elem reflect.Value, colFilter ColumnFilter) (bindInstance, error) {
if colFilter == nil {
colFilter = acceptAllFilter
}
plan := &t.updatePlan
plan.once.Do(func() {
s := bytes.Buffer{}
s.WriteString(fmt.Sprintf("update %s set ", t.dbmap.Dialect.QuotedTableForQuery(t.SchemaName, t.TableName)))
x := 0
for y := range t.Columns {
col := t.Columns[y]
if !col.isAutoIncr && !col.Transient && colFilter(col) {
if x > 0 {
s.WriteString(", ")
}
s.WriteString(t.dbmap.Dialect.QuoteField(col.ColumnName))
s.WriteString("=")
s.WriteString(t.dbmap.Dialect.BindVar(x))
if col == t.version {
plan.versField = col.fieldName
plan.argFields = append(plan.argFields, versFieldConst)
} else {
plan.argFields = append(plan.argFields, col.fieldName)
}
x++
}
}
s.WriteString(" where ")
for y := range t.keys {
col := t.keys[y]
if y > 0 {
s.WriteString(" and ")
}
s.WriteString(t.dbmap.Dialect.QuoteField(col.ColumnName))
s.WriteString("=")
s.WriteString(t.dbmap.Dialect.BindVar(x))
plan.argFields = append(plan.argFields, col.fieldName)
plan.keyFields = append(plan.keyFields, col.fieldName)
x++
}
if plan.versField != "" {
s.WriteString(" and ")
s.WriteString(t.dbmap.Dialect.QuoteField(t.version.ColumnName))
s.WriteString("=")
s.WriteString(t.dbmap.Dialect.BindVar(x))
plan.argFields = append(plan.argFields, plan.versField)
}
s.WriteString(t.dbmap.Dialect.QuerySuffix())
plan.query = s.String()
})
return plan.createBindInstance(elem, t.dbmap.TypeConverter)
}
// bindDelete creates a bind instance for a delete
func (t *TableMap) bindDelete(elem reflect.Value) (bindInstance, error) {
plan := &t.deletePlan
plan.once.Do(func() {
s := bytes.Buffer{}
s.WriteString(fmt.Sprintf("delete from %s", t.dbmap.Dialect.QuotedTableForQuery(t.SchemaName, t.TableName)))
for y := range t.Columns {
col := t.Columns[y]
if !col.Transient {
if col == t.version {
plan.versField = col.fieldName
}
}
}
s.WriteString(" where ")
for x := range t.keys {
k := t.keys[x]
if x > 0 {
s.WriteString(" and ")
}
s.WriteString(t.dbmap.Dialect.QuoteField(k.ColumnName))
s.WriteString("=")
s.WriteString(t.dbmap.Dialect.BindVar(x))
plan.keyFields = append(plan.keyFields, k.fieldName)
plan.argFields = append(plan.argFields, k.fieldName)
}
if plan.versField != "" {
s.WriteString(" and ")
s.WriteString(t.dbmap.Dialect.QuoteField(t.version.ColumnName))
s.WriteString("=")
s.WriteString(t.dbmap.Dialect.BindVar(len(plan.argFields)))
plan.argFields = append(plan.argFields, plan.versField)
}
s.WriteString(t.dbmap.Dialect.QuerySuffix())
plan.query = s.String()
})
return plan.createBindInstance(elem, t.dbmap.TypeConverter)
}
// bindGet creates a bind instance for a get
func (t *TableMap) bindGet() *bindPlan {
plan := &t.getPlan
plan.once.Do(func() {
s := bytes.Buffer{}
s.WriteString("select ")
x := 0
for _, col := range t.Columns {
if !col.Transient {
if x > 0 {
s.WriteString(",")
}
s.WriteString(t.dbmap.Dialect.QuoteField(col.ColumnName))
plan.argFields = append(plan.argFields, col.fieldName)
x++
}
}
s.WriteString(" from ")
s.WriteString(t.dbmap.Dialect.QuotedTableForQuery(t.SchemaName, t.TableName))
s.WriteString(" where ")
for x := range t.keys {
col := t.keys[x]
if x > 0 {
s.WriteString(" and ")
}
s.WriteString(t.dbmap.Dialect.QuoteField(col.ColumnName))
s.WriteString("=")
s.WriteString(t.dbmap.Dialect.BindVar(x))
plan.keyFields = append(plan.keyFields, col.fieldName)
}
s.WriteString(t.dbmap.Dialect.QuerySuffix())
plan.query = s.String()
})
return plan
}