Skip to content

Commit b538748

Browse files
authored
Merge pull request #1535 from ydb-platform/xsql
* Refactored `database/sql` driver internals for query-service client support in the future
2 parents 21fab94 + cf02c93 commit b538748

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1782
-1596
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Refactored `database/sql` driver internals for query-service client support in the future
2+
13
## v3.89.5
24
* Fixed nil pointer dereference in metabalancer initialization
35

driver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/ydb-platform/ydb-go-sdk/v3/discovery"
1515
"github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer"
1616
"github.com/ydb-platform/ydb-go-sdk/v3/internal/conn"
17+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/connector"
1718
internalCoordination "github.com/ydb-platform/ydb-go-sdk/v3/internal/coordination"
1819
coordinationConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/coordination/config"
1920
"github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials"
@@ -36,7 +37,6 @@ import (
3637
"github.com/ydb-platform/ydb-go-sdk/v3/internal/topic/topicclientinternal"
3738
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
3839
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
39-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql"
4040
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
4141
"github.com/ydb-platform/ydb-go-sdk/v3/log"
4242
"github.com/ydb-platform/ydb-go-sdk/v3/operation"
@@ -93,7 +93,7 @@ type (
9393
topic *xsync.Once[*topicclientinternal.Client]
9494
topicOptions []topicoptions.TopicOption
9595

96-
databaseSQLOptions []xsql.ConnectorOption
96+
databaseSQLOptions []connector.Option
9797

9898
pool *conn.Pool
9999

dsn.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
"github.com/ydb-platform/ydb-go-sdk/v3/balancers"
1010
"github.com/ydb-platform/ydb-go-sdk/v3/credentials"
1111
"github.com/ydb-platform/ydb-go-sdk/v3/internal/bind"
12+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/connector"
1213
"github.com/ydb-platform/ydb-go-sdk/v3/internal/dsn"
14+
tableSql "github.com/ydb-platform/ydb-go-sdk/v3/internal/table/conn"
1315
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
14-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql"
1516
)
1617

1718
const tablePathPrefixTransformer = "table_path_prefix"
@@ -59,45 +60,45 @@ func parseConnectionString(dataSourceName string) (opts []Option, _ error) {
5960
opts = append(opts, WithBalancer(balancers.FromConfig(balancer)))
6061
}
6162
if queryMode := info.Params.Get("go_query_mode"); queryMode != "" {
62-
mode := xsql.QueryModeFromString(queryMode)
63-
if mode == xsql.UnknownQueryMode {
63+
mode := tableSql.QueryModeFromString(queryMode)
64+
if mode == tableSql.UnknownQueryMode {
6465
return nil, xerrors.WithStackTrace(fmt.Errorf("unknown query mode: %s", queryMode))
6566
}
66-
opts = append(opts, withConnectorOptions(xsql.WithDefaultQueryMode(mode)))
67+
opts = append(opts, withConnectorOptions(connector.WithDefaultQueryMode(mode)))
6768
} else if queryMode := info.Params.Get("query_mode"); queryMode != "" {
68-
mode := xsql.QueryModeFromString(queryMode)
69-
if mode == xsql.UnknownQueryMode {
69+
mode := tableSql.QueryModeFromString(queryMode)
70+
if mode == tableSql.UnknownQueryMode {
7071
return nil, xerrors.WithStackTrace(fmt.Errorf("unknown query mode: %s", queryMode))
7172
}
72-
opts = append(opts, withConnectorOptions(xsql.WithDefaultQueryMode(mode)))
73+
opts = append(opts, withConnectorOptions(connector.WithDefaultQueryMode(mode)))
7374
}
7475
if fakeTx := info.Params.Get("go_fake_tx"); fakeTx != "" {
7576
for _, queryMode := range strings.Split(fakeTx, ",") {
76-
mode := xsql.QueryModeFromString(queryMode)
77-
if mode == xsql.UnknownQueryMode {
77+
mode := tableSql.QueryModeFromString(queryMode)
78+
if mode == tableSql.UnknownQueryMode {
7879
return nil, xerrors.WithStackTrace(fmt.Errorf("unknown query mode: %s", queryMode))
7980
}
80-
opts = append(opts, withConnectorOptions(xsql.WithFakeTx(mode)))
81+
opts = append(opts, withConnectorOptions(connector.WithFakeTx(mode)))
8182
}
8283
}
8384
if info.Params.Has("go_query_bind") {
84-
var binders []xsql.ConnectorOption
85+
var binders []connector.Option
8586
queryTransformers := strings.Split(info.Params.Get("go_query_bind"), ",")
8687
for _, transformer := range queryTransformers {
8788
switch transformer {
8889
case "declare":
89-
binders = append(binders, xsql.WithQueryBind(bind.AutoDeclare{}))
90+
binders = append(binders, connector.WithQueryBind(bind.AutoDeclare{}))
9091
case "positional":
91-
binders = append(binders, xsql.WithQueryBind(bind.PositionalArgs{}))
92+
binders = append(binders, connector.WithQueryBind(bind.PositionalArgs{}))
9293
case "numeric":
93-
binders = append(binders, xsql.WithQueryBind(bind.NumericArgs{}))
94+
binders = append(binders, connector.WithQueryBind(bind.NumericArgs{}))
9495
default:
9596
if strings.HasPrefix(transformer, tablePathPrefixTransformer) {
9697
prefix, err := extractTablePathPrefixFromBinderName(transformer)
9798
if err != nil {
9899
return nil, xerrors.WithStackTrace(err)
99100
}
100-
binders = append(binders, xsql.WithTablePathPrefix(prefix))
101+
binders = append(binders, connector.WithQueryBind(bind.TablePathPrefix(prefix)))
101102
} else {
102103
return nil, xerrors.WithStackTrace(
103104
fmt.Errorf("unknown query rewriter: %s", transformer),

dsn_test.go

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import (
88

99
"github.com/ydb-platform/ydb-go-sdk/v3/config"
1010
"github.com/ydb-platform/ydb-go-sdk/v3/internal/bind"
11-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql"
11+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/connector"
12+
querySql "github.com/ydb-platform/ydb-go-sdk/v3/internal/query/conn"
13+
tableSql "github.com/ydb-platform/ydb-go-sdk/v3/internal/table/conn"
1214
)
1315

1416
func TestParse(t *testing.T) {
15-
newConnector := func(opts ...xsql.ConnectorOption) *xsql.Connector {
16-
c := &xsql.Connector{}
17+
newConnector := func(opts ...connector.Option) *connector.Connector {
18+
c := &connector.Connector{}
1719
for _, opt := range opts {
1820
if opt != nil {
1921
if err := opt.Apply(c); err != nil {
@@ -24,6 +26,12 @@ func TestParse(t *testing.T) {
2426

2527
return c
2628
}
29+
newTableConn := func(opts ...tableSql.Option) *tableSql.Conn {
30+
return tableSql.New(context.Background(), nil, nil, opts...)
31+
}
32+
newQueryConn := func(opts ...querySql.Option) *querySql.Conn {
33+
return querySql.New(context.Background(), nil, nil, opts...)
34+
}
2735
compareConfigs := func(t *testing.T, lhs, rhs *config.Config) {
2836
require.Equal(t, lhs.Secure(), rhs.Secure())
2937
require.Equal(t, lhs.Endpoint(), rhs.Endpoint())
@@ -32,22 +40,9 @@ func TestParse(t *testing.T) {
3240
for _, tt := range []struct {
3341
dsn string
3442
opts []config.Option
35-
connectorOpts []xsql.ConnectorOption
43+
connectorOpts []connector.Option
3644
err error
3745
}{
38-
{
39-
dsn: "grpc://localhost:2135/local?go_fake_tx=scripting,scheme",
40-
opts: []config.Option{
41-
config.WithSecure(false),
42-
config.WithEndpoint("localhost:2135"),
43-
config.WithDatabase("/local"),
44-
},
45-
connectorOpts: []xsql.ConnectorOption{
46-
xsql.WithFakeTx(xsql.ScriptingQueryMode),
47-
xsql.WithFakeTx(xsql.SchemeQueryMode),
48-
},
49-
err: nil,
50-
},
5146
{
5247
dsn: "grpc://localhost:2135/local",
5348
opts: []config.Option{
@@ -75,8 +70,8 @@ func TestParse(t *testing.T) {
7570
config.WithEndpoint("localhost:2135"),
7671
config.WithDatabase("/local"),
7772
},
78-
connectorOpts: []xsql.ConnectorOption{
79-
xsql.WithDefaultQueryMode(xsql.ScriptingQueryMode),
73+
connectorOpts: []connector.Option{
74+
connector.WithDefaultQueryMode(tableSql.ScriptingQueryMode),
8075
},
8176
err: nil,
8277
},
@@ -87,9 +82,9 @@ func TestParse(t *testing.T) {
8782
config.WithEndpoint("localhost:2135"),
8883
config.WithDatabase("/local"),
8984
},
90-
connectorOpts: []xsql.ConnectorOption{
91-
xsql.WithDefaultQueryMode(xsql.ScriptingQueryMode),
92-
xsql.WithTablePathPrefix("path/to/tables"),
85+
connectorOpts: []connector.Option{
86+
connector.WithDefaultQueryMode(tableSql.ScriptingQueryMode),
87+
connector.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
9388
},
9489
err: nil,
9590
},
@@ -100,10 +95,10 @@ func TestParse(t *testing.T) {
10095
config.WithEndpoint("localhost:2135"),
10196
config.WithDatabase("/local"),
10297
},
103-
connectorOpts: []xsql.ConnectorOption{
104-
xsql.WithDefaultQueryMode(xsql.ScriptingQueryMode),
105-
xsql.WithTablePathPrefix("path/to/tables"),
106-
xsql.WithQueryBind(bind.NumericArgs{}),
98+
connectorOpts: []connector.Option{
99+
connector.WithDefaultQueryMode(tableSql.ScriptingQueryMode),
100+
connector.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
101+
connector.WithQueryBind(bind.NumericArgs{}),
107102
},
108103
err: nil,
109104
},
@@ -114,10 +109,10 @@ func TestParse(t *testing.T) {
114109
config.WithEndpoint("localhost:2135"),
115110
config.WithDatabase("/local"),
116111
},
117-
connectorOpts: []xsql.ConnectorOption{
118-
xsql.WithDefaultQueryMode(xsql.ScriptingQueryMode),
119-
xsql.WithTablePathPrefix("path/to/tables"),
120-
xsql.WithQueryBind(bind.PositionalArgs{}),
112+
connectorOpts: []connector.Option{
113+
connector.WithDefaultQueryMode(tableSql.ScriptingQueryMode),
114+
connector.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
115+
connector.WithQueryBind(bind.PositionalArgs{}),
121116
},
122117
err: nil,
123118
},
@@ -128,10 +123,10 @@ func TestParse(t *testing.T) {
128123
config.WithEndpoint("localhost:2135"),
129124
config.WithDatabase("/local"),
130125
},
131-
connectorOpts: []xsql.ConnectorOption{
132-
xsql.WithDefaultQueryMode(xsql.ScriptingQueryMode),
133-
xsql.WithTablePathPrefix("path/to/tables"),
134-
xsql.WithQueryBind(bind.AutoDeclare{}),
126+
connectorOpts: []connector.Option{
127+
connector.WithDefaultQueryMode(tableSql.ScriptingQueryMode),
128+
connector.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
129+
connector.WithQueryBind(bind.AutoDeclare{}),
135130
},
136131
err: nil,
137132
},
@@ -142,9 +137,9 @@ func TestParse(t *testing.T) {
142137
config.WithEndpoint("localhost:2135"),
143138
config.WithDatabase("/local"),
144139
},
145-
connectorOpts: []xsql.ConnectorOption{
146-
xsql.WithDefaultQueryMode(xsql.ScriptingQueryMode),
147-
xsql.WithTablePathPrefix("path/to/tables"),
140+
connectorOpts: []connector.Option{
141+
connector.WithDefaultQueryMode(tableSql.ScriptingQueryMode),
142+
connector.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
148143
},
149144
err: nil,
150145
},
@@ -155,11 +150,24 @@ func TestParse(t *testing.T) {
155150
config.WithEndpoint("localhost:2135"),
156151
config.WithDatabase("/local"),
157152
},
158-
connectorOpts: []xsql.ConnectorOption{
159-
xsql.WithDefaultQueryMode(xsql.ScriptingQueryMode),
160-
xsql.WithTablePathPrefix("path/to/tables"),
161-
xsql.WithQueryBind(bind.PositionalArgs{}),
162-
xsql.WithQueryBind(bind.AutoDeclare{}),
153+
connectorOpts: []connector.Option{
154+
connector.WithDefaultQueryMode(tableSql.ScriptingQueryMode),
155+
connector.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
156+
connector.WithQueryBind(bind.PositionalArgs{}),
157+
connector.WithQueryBind(bind.AutoDeclare{}),
158+
},
159+
err: nil,
160+
},
161+
{
162+
dsn: "grpc://localhost:2135/local?go_fake_tx=scripting,scheme",
163+
opts: []config.Option{
164+
config.WithSecure(false),
165+
config.WithEndpoint("localhost:2135"),
166+
config.WithDatabase("/local"),
167+
},
168+
connectorOpts: []connector.Option{
169+
connector.WithFakeTx(tableSql.ScriptingQueryMode),
170+
connector.WithFakeTx(tableSql.SchemeQueryMode),
163171
},
164172
err: nil,
165173
},
@@ -172,7 +180,20 @@ func TestParse(t *testing.T) {
172180
require.NoError(t, err)
173181
d, err := newConnectionFromOptions(context.Background(), opts...)
174182
require.NoError(t, err)
175-
require.Equal(t, newConnector(tt.connectorOpts...), newConnector(d.databaseSQLOptions...))
183+
exp := newConnector(tt.connectorOpts...)
184+
act := newConnector(d.databaseSQLOptions...)
185+
t.Run("tableOptions", func(t *testing.T) {
186+
require.Equal(t, newTableConn(exp.TableOpts...), newTableConn(act.TableOpts...))
187+
})
188+
t.Run("queryOptions", func(t *testing.T) {
189+
require.Equal(t, newQueryConn(exp.QueryOpts...), newQueryConn(act.QueryOpts...))
190+
})
191+
exp.TableOpts = nil
192+
exp.QueryOpts = nil
193+
act.TableOpts = nil
194+
act.QueryOpts = nil
195+
require.Equal(t, exp.Bindings(), act.Bindings())
196+
require.Equal(t, exp, act)
176197
compareConfigs(t, config.New(tt.opts...), d.config)
177198
}
178199
})

0 commit comments

Comments
 (0)