Skip to content

Commit 881ae0c

Browse files
committed
* Added binding ydb.WithWideTypes() which interprets time.Time and time.Duration as Timestamp64 and Interval64 YDB types
1 parent 41a3b5f commit 881ae0c

18 files changed

+394
-173
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Added binding `ydb.WithWideTypes()` which interprets `time.Time` and `time.Duration` as `Timestamp64` and `Interval64` YDB types
2+
13
## v3.103.0
24
* Supported wide `Interval64` type
35

internal/bind/auto_declare.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ func (m AutoDeclare) blockID() blockID {
1313
return blockDeclare
1414
}
1515

16-
func (m AutoDeclare) ToYdb(sql string, args ...interface{}) (
17-
yql string, newArgs []interface{}, err error,
16+
func (m AutoDeclare) ToYdb(sql string, args ...any) (
17+
yql string, newArgs []any, err error,
1818
) {
1919
params, err := Params(args...)
2020
if err != nil {

internal/bind/bind.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,24 @@ import (
1111
type blockID int
1212

1313
const (
14-
blockPragma = blockID(iota)
14+
blockDefault = blockID(iota)
15+
blockPragma
1516
blockDeclare
1617
blockYQL
18+
blockCastArgs
1719
)
1820

1921
type Bind interface {
20-
ToYdb(sql string, args ...interface{}) (
21-
yql string, newArgs []interface{}, _ error,
22+
ToYdb(sql string, args ...any) (
23+
yql string, newArgs []any, _ error,
2224
)
2325

2426
blockID() blockID
2527
}
2628

2729
type Bindings []Bind
2830

29-
func (bindings Bindings) ToYdb(sql string, args ...interface{}) (
31+
func (bindings Bindings) ToYdb(sql string, args ...any) (
3032
yql string, params params.Params, err error,
3133
) {
3234
if len(bindings) == 0 {

internal/bind/noop.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package bind
2+
3+
type noopBind struct{}
4+
5+
func (m noopBind) ToYdb(sql string, args ...any) (yql string, newArgs []any, _ error) {
6+
return sql, args, nil
7+
}
8+
9+
func (m noopBind) blockID() blockID {
10+
return blockCastArgs
11+
}

internal/bind/numeric_args.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (m NumericArgs) blockID() blockID {
1717
return blockYQL
1818
}
1919

20-
func (m NumericArgs) ToYdb(sql string, args ...interface{}) (yql string, newArgs []interface{}, err error) {
20+
func (m NumericArgs) ToYdb(sql string, args ...any) (yql string, newArgs []any, err error) {
2121
l := &sqlLexer{
2222
src: sql,
2323
stateFn: numericArgsStateFn,
@@ -36,7 +36,7 @@ func (m NumericArgs) ToYdb(sql string, args ...interface{}) (yql string, newArgs
3636
if err != nil {
3737
return "", nil, err
3838
}
39-
newArgs = make([]interface{}, len(parameters))
39+
newArgs = make([]any, len(parameters))
4040
for i, param := range parameters {
4141
newArgs[i] = param
4242
}
@@ -119,7 +119,7 @@ func numericArgsStateFn(l *sqlLexer) stateFn {
119119
}
120120
}
121121

122-
func parsePositionalParameters(args []interface{}) ([]*params.Parameter, error) {
122+
func parsePositionalParameters(args []any) ([]*params.Parameter, error) {
123123
newArgs := make([]*params.Parameter, len(args))
124124
for i, arg := range args {
125125
paramName := fmt.Sprintf("$p%d", i)

internal/bind/numeric_args_test.go

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,123 +17,123 @@ func TestNumericArgsBindRewriteQuery(t *testing.T) {
1717
)
1818
for _, tt := range []struct {
1919
sql string
20-
args []interface{}
20+
args []any
2121
yql string
22-
params []interface{}
22+
params []any
2323
err error
2424
}{
2525
{
2626
sql: `SELECT $123abc, $2`,
27-
args: []interface{}{
27+
args: []any{
2828
100,
2929
},
3030
err: ErrInconsistentArgs,
3131
},
3232
{
3333
sql: `SELECT $123abc, $1`,
34-
args: []interface{}{
34+
args: []any{
3535
200,
3636
},
3737
yql: `-- origin query with numeric args replacement
3838
SELECT $123abc, $p0`,
39-
params: []interface{}{
39+
params: []any{
4040
table.ValueParam("$p0", types.Int32Value(200)),
4141
},
4242
},
4343
{
4444
sql: `SELECT $1, $2`,
45-
args: []interface{}{
45+
args: []any{
4646
table.ValueParam("$name1", types.Int32Value(100)),
4747
table.ValueParam("$name2", types.Int32Value(200)),
4848
},
4949
yql: `-- origin query with numeric args replacement
5050
SELECT $name1, $name2`,
51-
params: []interface{}{
51+
params: []any{
5252
table.ValueParam("$name1", types.Int32Value(100)),
5353
table.ValueParam("$name2", types.Int32Value(200)),
5454
},
5555
},
5656
{
5757
sql: `SELECT $1, $2`,
58-
args: []interface{}{
58+
args: []any{
5959
table.ValueParam("$namedArg", types.Int32Value(100)),
6060
200,
6161
},
6262
yql: `-- origin query with numeric args replacement
6363
SELECT $namedArg, $p1`,
64-
params: []interface{}{
64+
params: []any{
6565
table.ValueParam("$namedArg", types.Int32Value(100)),
6666
table.ValueParam("$p1", types.Int32Value(200)),
6767
},
6868
},
6969
{
7070
sql: `SELECT $0, $1`,
71-
args: []interface{}{
71+
args: []any{
7272
100,
7373
200,
7474
},
7575
err: ErrUnexpectedNumericArgZero,
7676
},
7777
{
7878
sql: `SELECT $1, $2`,
79-
args: []interface{}{
79+
args: []any{
8080
100,
8181
200,
8282
},
8383
yql: `-- origin query with numeric args replacement
8484
SELECT $p0, $p1`,
85-
params: []interface{}{
85+
params: []any{
8686
table.ValueParam("$p0", types.Int32Value(100)),
8787
table.ValueParam("$p1", types.Int32Value(200)),
8888
},
8989
},
9090
{
9191
sql: `SELECT $1, $2`,
92-
args: []interface{}{
92+
args: []any{
9393
100,
9494
},
9595
err: ErrInconsistentArgs,
9696
},
9797
{
9898
sql: `SELECT $1, "$2"`,
99-
args: []interface{}{
99+
args: []any{
100100
100,
101101
},
102102
yql: `-- origin query with numeric args replacement
103103
SELECT $p0, "$2"`,
104-
params: []interface{}{
104+
params: []any{
105105
table.ValueParam("$p0", types.Int32Value(100)),
106106
},
107107
},
108108
{
109109
sql: `SELECT $1, '$2'`,
110-
args: []interface{}{
110+
args: []any{
111111
100,
112112
},
113113
yql: `-- origin query with numeric args replacement
114114
SELECT $p0, '$2'`,
115-
params: []interface{}{
115+
params: []any{
116116
table.ValueParam("$p0", types.Int32Value(100)),
117117
},
118118
},
119119
{
120120
sql: "SELECT $1, `$2`",
121-
args: []interface{}{
121+
args: []any{
122122
100,
123123
},
124124
yql: "-- origin query with numeric args replacement\nSELECT $p0, `$2`",
125-
params: []interface{}{
125+
params: []any{
126126
table.ValueParam("$p0", types.Int32Value(100)),
127127
},
128128
},
129129
{
130130
sql: "SELECT ?, $1, $p0",
131-
params: []interface{}{},
131+
params: []any{},
132132
err: ErrInconsistentArgs,
133133
},
134134
{
135135
sql: "SELECT $1, $2, $3",
136-
args: []interface{}{
136+
args: []any{
137137
1,
138138
"test",
139139
[]string{
@@ -144,7 +144,7 @@ SELECT $p0, '$2'`,
144144
},
145145
yql: `-- origin query with numeric args replacement
146146
SELECT $p0, $p1, $p2`,
147-
params: []interface{}{
147+
params: []any{
148148
table.ValueParam("$p0", types.Int32Value(1)),
149149
table.ValueParam("$p1", types.TextValue("test")),
150150
table.ValueParam("$p2", types.ListValue(
@@ -156,7 +156,7 @@ SELECT $p0, $p1, $p2`,
156156
},
157157
{
158158
sql: "SELECT $1, $2, $3, $1, $2",
159-
args: []interface{}{
159+
args: []any{
160160
1,
161161
"test",
162162
[]string{
@@ -167,7 +167,7 @@ SELECT $p0, $p1, $p2`,
167167
},
168168
yql: `-- origin query with numeric args replacement
169169
SELECT $p0, $p1, $p2, $p0, $p1`,
170-
params: []interface{}{
170+
params: []any{
171171
table.ValueParam("$p0", types.Int32Value(1)),
172172
table.ValueParam("$p1", types.TextValue("test")),
173173
table.ValueParam("$p2", types.ListValue(
@@ -179,7 +179,7 @@ SELECT $p0, $p1, $p2, $p0, $p1`,
179179
},
180180
{
181181
sql: "SELECT $1, $2, $3",
182-
args: []interface{}{
182+
args: []any{
183183
types.Int32Value(1),
184184
types.TextValue("test"),
185185
types.ListValue(
@@ -190,7 +190,7 @@ SELECT $p0, $p1, $p2, $p0, $p1`,
190190
},
191191
yql: `-- origin query with numeric args replacement
192192
SELECT $p0, $p1, $p2`,
193-
params: []interface{}{
193+
params: []any{
194194
table.ValueParam("$p0", types.Int32Value(1)),
195195
table.ValueParam("$p1", types.TextValue("test")),
196196
table.ValueParam("$p2", types.ListValue(
@@ -202,12 +202,12 @@ SELECT $p0, $p1, $p2`,
202202
},
203203
{
204204
sql: "SELECT $1, a, b, c WHERE id = $1 AND date < $2 AND value IN ($3)",
205-
args: []interface{}{
205+
args: []any{
206206
1, now, []string{"3"},
207207
},
208208
yql: `-- origin query with numeric args replacement
209209
SELECT $p0, a, b, c WHERE id = $p0 AND date < $p1 AND value IN ($p2)`,
210-
params: []interface{}{
210+
params: []any{
211211
table.ValueParam("$p0", types.Int32Value(1)),
212212
table.ValueParam("$p1", types.TimestampValueFromTime(now)),
213213
table.ValueParam("$p2", types.ListValue(types.TextValue("3"))),
@@ -225,47 +225,47 @@ SELECT 1`,
225225
},
226226
{
227227
sql: "SELECT $1, $2",
228-
args: []interface{}{
228+
args: []any{
229229
1,
230230
},
231231
err: ErrInconsistentArgs,
232232
},
233233
{
234234
sql: "SELECT $1, $2 -- some comment with $3",
235-
args: []interface{}{
235+
args: []any{
236236
100,
237237
200,
238238
},
239239
yql: `-- origin query with numeric args replacement
240240
SELECT $p0, $p1 -- some comment with $3`,
241-
params: []interface{}{
241+
params: []any{
242242
table.ValueParam("$p0", types.Int32Value(100)),
243243
table.ValueParam("$p1", types.Int32Value(200)),
244244
},
245245
},
246246
{
247247
sql: "SELECT $1 /* some comment with $3 */, $2",
248-
args: []interface{}{
248+
args: []any{
249249
100,
250250
200,
251251
},
252252
yql: `-- origin query with numeric args replacement
253253
SELECT $p0 /* some comment with $3 */, $p1`,
254-
params: []interface{}{
254+
params: []any{
255255
table.ValueParam("$p0", types.Int32Value(100)),
256256
table.ValueParam("$p1", types.Int32Value(200)),
257257
},
258258
},
259259
{
260260
sql: "SELECT $1, $2 -- some comment with $3",
261-
args: []interface{}{
261+
args: []any{
262262
100,
263263
},
264264
err: ErrInconsistentArgs,
265265
},
266266
{
267267
sql: "SELECT $1, $2, $3",
268-
args: []interface{}{
268+
args: []any{
269269
100,
270270
200,
271271
},
@@ -274,40 +274,40 @@ SELECT $p0 /* some comment with $3 */, $p1`,
274274
{
275275
sql: `
276276
SELECT $1 /* some comment with $3 */, $2`,
277-
args: []interface{}{
277+
args: []any{
278278
100,
279279
200,
280280
},
281281
yql: `-- origin query with numeric args replacement
282282
283283
SELECT $p0 /* some comment with $3 */, $p1`,
284-
params: []interface{}{
284+
params: []any{
285285
table.ValueParam("$p0", types.Int32Value(100)),
286286
table.ValueParam("$p1", types.Int32Value(200)),
287287
},
288288
},
289289
{
290290
sql: "SELECT $1, $2",
291-
args: []interface{}{
291+
args: []any{
292292
100,
293293
200,
294294
},
295295
yql: `-- origin query with numeric args replacement
296296
SELECT $p0, $p1`,
297-
params: []interface{}{
297+
params: []any{
298298
table.ValueParam("$p0", types.Int32Value(100)),
299299
table.ValueParam("$p1", types.Int32Value(200)),
300300
},
301301
},
302302
{
303303
sql: "SELECT $1, $2",
304-
args: []interface{}{
304+
args: []any{
305305
100,
306306
200,
307307
},
308308
yql: `-- origin query with numeric args replacement
309309
SELECT $p0, $p1`,
310-
params: []interface{}{
310+
params: []any{
311311
table.ValueParam("$p0", types.Int32Value(100)),
312312
table.ValueParam("$p1", types.Int32Value(200)),
313313
},

0 commit comments

Comments
 (0)