Skip to content

Commit

Permalink
fix & test sqlx
Browse files Browse the repository at this point in the history
  • Loading branch information
4el0ve4ek committed Dec 12, 2024
1 parent 9a4b4f8 commit fc262dc
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 5 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/jmoiron/sqlx v1.3.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang-jwt/jwt/v4 v4.4.1 h1:pC5DB52sCeK48Wlb9oPcdhnjkz1TKt1D/P7WKJ0kUcQ=
github.com/golang-jwt/jwt/v4 v4.4.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand Down Expand Up @@ -52,8 +53,12 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/jonboulle/clockwork v0.3.0 h1:9BSCMi8C+0qdApAp4auwX0RkLGUjs956h0EkuQymUhg=
github.com/jonboulle/clockwork v0.3.0/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down
17 changes: 12 additions & 5 deletions internal/bind/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,16 +337,23 @@ func supportNewTypeLink(x interface{}) string {
}

func toYdbParam(name string, value interface{}) (*params.Parameter, error) {
if na, ok := value.(driver.NamedValue); ok {
n, v := na.Name, na.Value
switch tv := value.(type) {
case driver.NamedValue:
n, v := tv.Name, tv.Value
if n != "" {
name = n
}
value = v
// case sql.NamedArg:
// n, v := tv.Name, tv.Value
// if n != "" {
// name = n
// }
// value = v
case *params.Parameter:
return tv, nil
}
if v, ok := value.(*params.Parameter); ok {
return v, nil
}

v, err := toValue(value)
if err != nil {
return nil, xerrors.WithStackTrace(err)
Expand Down
65 changes: 65 additions & 0 deletions tests/integration/database_sql_regression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,68 @@ func TestUUIDSerializationDatabaseSQLIssue1501(t *testing.T) {
require.Equal(t, id.String(), res.String())
})
}

func TestSQLX(t *testing.T) {
// test sqlx

t.Run("named-exec-context", func(t *testing.T) {
// test old behavior - for test way of safe work with data, written with bagged API version
var (
ctx = xtest.Context(t)
scope = newScope(t)
db = scope.SQLXDriver()
)

idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
id := [16]byte(uuid.MustParse(idString))

params := struct {
ID [16]byte `db:"val"`
}{
ID: id,
}

_, err := db.NamedExecContext(ctx, `
DECLARE $val AS UUID;
SELECT CAST($val AS Utf8)`,
params,
)

require.NoError(t, err)
})

t.Run("get-context", func(t *testing.T) {
// test old behavior - for test way of safe work with data, written with bagged API version
var (
ctx = xtest.Context(t)
scope = newScope(t)
db = scope.SQLXDriver()
)

idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
id := [16]byte(uuid.MustParse(idString))

params := struct {
ID [16]byte `db:"val"`
}{
ID: id,
}

var resp struct {
RespVal uuid.UUID `db:"resp_val"`
}

err := db.GetContext(
ctx,
&resp,
`
DECLARE $val AS UUID;
SELECT CAST($val AS Utf8) as resp_val
`,
params,
)

require.NoError(t, err)
require.Equal(t, idString, resp.RespVal.String())
})
}
5 changes: 5 additions & 0 deletions tests/integration/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"text/template"
"time"

"github.com/jmoiron/sqlx"
"github.com/rekby/fixenv"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
Expand Down Expand Up @@ -167,6 +168,10 @@ func (scope *scopeT) SQLDriverWithFolder(opts ...ydb.ConnectorOption) *sql.DB {
)
}

func (scope *scopeT) SQLXDriver(opts ...ydb.ConnectorOption) *sqlx.DB {
return sqlx.NewDb(scope.SQLDriver(opts...), "ydb")
}

func (scope *scopeT) Folder() string {
f := func() (*fixenv.GenericResult[string], error) {
driver := scope.Driver()
Expand Down

0 comments on commit fc262dc

Please sign in to comment.