Skip to content

Commit fc262dc

Browse files
committed
fix & test sqlx
1 parent 9a4b4f8 commit fc262dc

File tree

5 files changed

+88
-5
lines changed

5 files changed

+88
-5
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require (
2424
require (
2525
github.com/davecgh/go-spew v1.1.0 // indirect
2626
github.com/golang/protobuf v1.5.3 // indirect
27+
github.com/jmoiron/sqlx v1.3.5 // indirect
2728
github.com/pmezard/go-difflib v1.0.0 // indirect
2829
golang.org/x/sys v0.18.0 // indirect
2930
golang.org/x/text v0.14.0 // indirect

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m
2020
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE=
2121
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
2222
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
23+
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
2324
github.com/golang-jwt/jwt/v4 v4.4.1 h1:pC5DB52sCeK48Wlb9oPcdhnjkz1TKt1D/P7WKJ0kUcQ=
2425
github.com/golang-jwt/jwt/v4 v4.4.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
2526
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
@@ -52,8 +53,12 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
5253
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
5354
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5455
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
56+
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
57+
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
5558
github.com/jonboulle/clockwork v0.3.0 h1:9BSCMi8C+0qdApAp4auwX0RkLGUjs956h0EkuQymUhg=
5659
github.com/jonboulle/clockwork v0.3.0/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
60+
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
61+
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
5762
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5863
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5964
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=

internal/bind/params.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,16 +337,23 @@ func supportNewTypeLink(x interface{}) string {
337337
}
338338

339339
func toYdbParam(name string, value interface{}) (*params.Parameter, error) {
340-
if na, ok := value.(driver.NamedValue); ok {
341-
n, v := na.Name, na.Value
340+
switch tv := value.(type) {
341+
case driver.NamedValue:
342+
n, v := tv.Name, tv.Value
342343
if n != "" {
343344
name = n
344345
}
345346
value = v
347+
// case sql.NamedArg:
348+
// n, v := tv.Name, tv.Value
349+
// if n != "" {
350+
// name = n
351+
// }
352+
// value = v
353+
case *params.Parameter:
354+
return tv, nil
346355
}
347-
if v, ok := value.(*params.Parameter); ok {
348-
return v, nil
349-
}
356+
350357
v, err := toValue(value)
351358
if err != nil {
352359
return nil, xerrors.WithStackTrace(err)

tests/integration/database_sql_regression_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,68 @@ func TestUUIDSerializationDatabaseSQLIssue1501(t *testing.T) {
435435
require.Equal(t, id.String(), res.String())
436436
})
437437
}
438+
439+
func TestSQLX(t *testing.T) {
440+
// test sqlx
441+
442+
t.Run("named-exec-context", func(t *testing.T) {
443+
// test old behavior - for test way of safe work with data, written with bagged API version
444+
var (
445+
ctx = xtest.Context(t)
446+
scope = newScope(t)
447+
db = scope.SQLXDriver()
448+
)
449+
450+
idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
451+
id := [16]byte(uuid.MustParse(idString))
452+
453+
params := struct {
454+
ID [16]byte `db:"val"`
455+
}{
456+
ID: id,
457+
}
458+
459+
_, err := db.NamedExecContext(ctx, `
460+
DECLARE $val AS UUID;
461+
SELECT CAST($val AS Utf8)`,
462+
params,
463+
)
464+
465+
require.NoError(t, err)
466+
})
467+
468+
t.Run("get-context", func(t *testing.T) {
469+
// test old behavior - for test way of safe work with data, written with bagged API version
470+
var (
471+
ctx = xtest.Context(t)
472+
scope = newScope(t)
473+
db = scope.SQLXDriver()
474+
)
475+
476+
idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
477+
id := [16]byte(uuid.MustParse(idString))
478+
479+
params := struct {
480+
ID [16]byte `db:"val"`
481+
}{
482+
ID: id,
483+
}
484+
485+
var resp struct {
486+
RespVal uuid.UUID `db:"resp_val"`
487+
}
488+
489+
err := db.GetContext(
490+
ctx,
491+
&resp,
492+
`
493+
DECLARE $val AS UUID;
494+
SELECT CAST($val AS Utf8) as resp_val
495+
`,
496+
params,
497+
)
498+
499+
require.NoError(t, err)
500+
require.Equal(t, idString, resp.RespVal.String())
501+
})
502+
}

tests/integration/helpers_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"text/template"
1616
"time"
1717

18+
"github.com/jmoiron/sqlx"
1819
"github.com/rekby/fixenv"
1920
"github.com/stretchr/testify/require"
2021
"google.golang.org/grpc"
@@ -167,6 +168,10 @@ func (scope *scopeT) SQLDriverWithFolder(opts ...ydb.ConnectorOption) *sql.DB {
167168
)
168169
}
169170

171+
func (scope *scopeT) SQLXDriver(opts ...ydb.ConnectorOption) *sqlx.DB {
172+
return sqlx.NewDb(scope.SQLDriver(opts...), "ydb")
173+
}
174+
170175
func (scope *scopeT) Folder() string {
171176
f := func() (*fixenv.GenericResult[string], error) {
172177
driver := scope.Driver()

0 commit comments

Comments
 (0)