Skip to content

Commit fd4142d

Browse files
committed
* Added implementation of database/sql driver over query service client
* Added `ydb.WithQueryService(bool)` option to explicitly enable `database/sql` driver over query service client * Added environment parameter `YDB_DATABASE_SQL_OVER_QUERY_SERVICE` to enable `database/sql` driver over query service client without code rewriting
1 parent 6022f09 commit fd4142d

Some content is hidden

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

49 files changed

+1411
-860
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Added implementation of `database/sql` driver over query service client
2+
* Added `ydb.WithQueryService(bool)` option to explicitly enable `database/sql` driver over query service client
3+
* Added environment parameter `YDB_DATABASE_SQL_OVER_QUERY_SERVICE` to enable `database/sql` driver over query service client without code rewriting
4+
15
## v3.94.0
26
* Refactored golang types mapping into ydb types using `ydb.ParamsFromMap` and `database/sql` query arguments
37
* Small breaking change: type mapping for `ydb.ParamsFromMap` and `database/sql` type `uuid.UUID` changed from ydb type `Text` to ydb type `UUID`

dsn.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/ydb-platform/ydb-go-sdk/v3/internal/dsn"
1313
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1414
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql"
15-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/conn/table"
1615
)
1716

1817
const tablePathPrefixTransformer = "table_path_prefix"
@@ -44,6 +43,22 @@ func UnregisterDsnParser(registrationID int) {
4443
dsnParsers[registrationID] = nil
4544
}
4645

46+
var stringToType = map[string]QueryMode{
47+
"data": DataQueryMode,
48+
"scan": ScanQueryMode,
49+
"scheme": SchemeQueryMode,
50+
"scripting": ScriptingQueryMode,
51+
"query": QueryExecuteQueryMode,
52+
}
53+
54+
func queryModeFromString(s string) QueryMode {
55+
if t, ok := stringToType[s]; ok {
56+
return t
57+
}
58+
59+
return unknownQueryMode
60+
}
61+
4762
//nolint:funlen
4863
func parseConnectionString(dataSourceName string) (opts []Option, _ error) {
4964
info, err := dsn.Parse(dataSourceName)
@@ -60,25 +75,32 @@ func parseConnectionString(dataSourceName string) (opts []Option, _ error) {
6075
opts = append(opts, WithBalancer(balancers.FromConfig(balancer)))
6176
}
6277
if queryMode := info.Params.Get("go_query_mode"); queryMode != "" {
63-
mode := table.QueryModeFromString(queryMode)
64-
if mode == table.UnknownQueryMode {
78+
switch mode := queryModeFromString(queryMode); mode {
79+
case QueryExecuteQueryMode:
80+
opts = append(opts, withConnectorOptions(xsql.WithQueryService(true)))
81+
case unknownQueryMode:
6582
return nil, xerrors.WithStackTrace(fmt.Errorf("unknown query mode: %s", queryMode))
83+
default:
84+
opts = append(opts, withConnectorOptions(xsql.WithDefaultQueryMode(modeToMode(mode))))
6685
}
67-
opts = append(opts, withConnectorOptions(xsql.WithDefaultQueryMode(mode)))
6886
} else if queryMode := info.Params.Get("query_mode"); queryMode != "" {
69-
mode := table.QueryModeFromString(queryMode)
70-
if mode == table.UnknownQueryMode {
87+
switch mode := queryModeFromString(queryMode); mode {
88+
case QueryExecuteQueryMode:
89+
opts = append(opts, withConnectorOptions(xsql.WithQueryService(true)))
90+
case unknownQueryMode:
7191
return nil, xerrors.WithStackTrace(fmt.Errorf("unknown query mode: %s", queryMode))
92+
default:
93+
opts = append(opts, withConnectorOptions(xsql.WithDefaultQueryMode(modeToMode(mode))))
7294
}
73-
opts = append(opts, withConnectorOptions(xsql.WithDefaultQueryMode(mode)))
7495
}
7596
if fakeTx := info.Params.Get("go_fake_tx"); fakeTx != "" {
7697
for _, queryMode := range strings.Split(fakeTx, ",") {
77-
mode := table.QueryModeFromString(queryMode)
78-
if mode == table.UnknownQueryMode {
98+
switch mode := queryModeFromString(queryMode); mode {
99+
case unknownQueryMode:
79100
return nil, xerrors.WithStackTrace(fmt.Errorf("unknown query mode: %s", queryMode))
101+
default:
102+
opts = append(opts, withConnectorOptions(WithFakeTx(mode)))
80103
}
81-
opts = append(opts, withConnectorOptions(xsql.WithFakeTx(mode)))
82104
}
83105
}
84106
if info.Params.Has("go_query_bind") {

dsn_test.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"github.com/ydb-platform/ydb-go-sdk/v3/config"
1010
"github.com/ydb-platform/ydb-go-sdk/v3/internal/bind"
1111
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql"
12-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/conn/query"
13-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/conn/table"
12+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/legacy"
13+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/propose"
1414
)
1515

1616
func TestParse(t *testing.T) {
@@ -26,11 +26,11 @@ func TestParse(t *testing.T) {
2626

2727
return c
2828
}
29-
newTableConn := func(opts ...table.Option) *table.Conn {
30-
return table.New(context.Background(), nil, nil, opts...)
29+
newLegacyConn := func(opts ...legacy.Option) *legacy.Conn {
30+
return legacy.New(context.Background(), nil, nil, opts...)
3131
}
32-
newQueryConn := func(opts ...query.Option) *query.Conn {
33-
return query.New(context.Background(), nil, nil, opts...)
32+
newQueryConn := func(opts ...propose.Option) *propose.Conn {
33+
return propose.New(context.Background(), nil, nil, opts...)
3434
}
3535
compareConfigs := func(t *testing.T, lhs, rhs *config.Config) {
3636
require.Equal(t, lhs.Secure(), rhs.Secure())
@@ -71,7 +71,7 @@ func TestParse(t *testing.T) {
7171
config.WithDatabase("/local"),
7272
},
7373
connectorOpts: []xsql.Option{
74-
xsql.WithDefaultQueryMode(table.ScriptingQueryMode),
74+
xsql.WithDefaultQueryMode(legacy.ScriptingQueryMode),
7575
},
7676
err: nil,
7777
},
@@ -83,7 +83,7 @@ func TestParse(t *testing.T) {
8383
config.WithDatabase("/local"),
8484
},
8585
connectorOpts: []xsql.Option{
86-
xsql.WithDefaultQueryMode(table.ScriptingQueryMode),
86+
xsql.WithDefaultQueryMode(legacy.ScriptingQueryMode),
8787
xsql.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
8888
},
8989
err: nil,
@@ -96,7 +96,7 @@ func TestParse(t *testing.T) {
9696
config.WithDatabase("/local"),
9797
},
9898
connectorOpts: []xsql.Option{
99-
xsql.WithDefaultQueryMode(table.ScriptingQueryMode),
99+
xsql.WithDefaultQueryMode(legacy.ScriptingQueryMode),
100100
xsql.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
101101
xsql.WithQueryBind(bind.NumericArgs{}),
102102
},
@@ -110,7 +110,7 @@ func TestParse(t *testing.T) {
110110
config.WithDatabase("/local"),
111111
},
112112
connectorOpts: []xsql.Option{
113-
xsql.WithDefaultQueryMode(table.ScriptingQueryMode),
113+
xsql.WithDefaultQueryMode(legacy.ScriptingQueryMode),
114114
xsql.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
115115
xsql.WithQueryBind(bind.PositionalArgs{}),
116116
},
@@ -124,7 +124,7 @@ func TestParse(t *testing.T) {
124124
config.WithDatabase("/local"),
125125
},
126126
connectorOpts: []xsql.Option{
127-
xsql.WithDefaultQueryMode(table.ScriptingQueryMode),
127+
xsql.WithDefaultQueryMode(legacy.ScriptingQueryMode),
128128
xsql.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
129129
xsql.WithQueryBind(bind.AutoDeclare{}),
130130
},
@@ -138,7 +138,7 @@ func TestParse(t *testing.T) {
138138
config.WithDatabase("/local"),
139139
},
140140
connectorOpts: []xsql.Option{
141-
xsql.WithDefaultQueryMode(table.ScriptingQueryMode),
141+
xsql.WithDefaultQueryMode(legacy.ScriptingQueryMode),
142142
xsql.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
143143
},
144144
err: nil,
@@ -151,7 +151,7 @@ func TestParse(t *testing.T) {
151151
config.WithDatabase("/local"),
152152
},
153153
connectorOpts: []xsql.Option{
154-
xsql.WithDefaultQueryMode(table.ScriptingQueryMode),
154+
xsql.WithDefaultQueryMode(legacy.ScriptingQueryMode),
155155
xsql.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
156156
xsql.WithQueryBind(bind.PositionalArgs{}),
157157
xsql.WithQueryBind(bind.AutoDeclare{}),
@@ -166,8 +166,8 @@ func TestParse(t *testing.T) {
166166
config.WithDatabase("/local"),
167167
},
168168
connectorOpts: []xsql.Option{
169-
xsql.WithFakeTx(table.ScriptingQueryMode),
170-
xsql.WithFakeTx(table.SchemeQueryMode),
169+
WithFakeTx(ScriptingQueryMode),
170+
WithFakeTx(SchemeQueryMode),
171171
},
172172
err: nil,
173173
},
@@ -183,15 +183,15 @@ func TestParse(t *testing.T) {
183183
exp := newConnector(tt.connectorOpts...)
184184
act := newConnector(d.databaseSQLOptions...)
185185
t.Run("tableOptions", func(t *testing.T) {
186-
require.Equal(t, newTableConn(exp.TableOpts...), newTableConn(act.TableOpts...))
186+
require.Equal(t, newLegacyConn(exp.LegacyOpts...), newLegacyConn(act.LegacyOpts...))
187187
})
188188
t.Run("queryOptions", func(t *testing.T) {
189-
require.Equal(t, newQueryConn(exp.QueryOpts...), newQueryConn(act.QueryOpts...))
189+
require.Equal(t, newQueryConn(exp.Options...), newQueryConn(act.Options...))
190190
})
191-
exp.TableOpts = nil
192-
exp.QueryOpts = nil
193-
act.TableOpts = nil
194-
act.QueryOpts = nil
191+
exp.LegacyOpts = nil
192+
exp.Options = nil
193+
act.LegacyOpts = nil
194+
act.Options = nil
195195
require.Equal(t, exp.Bindings(), act.Bindings())
196196
require.Equal(t, exp, act)
197197
compareConfigs(t, config.New(tt.opts...), d.config)

internal/stack/record.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func buildRecordString(
163163
) string {
164164
buffer := xstring.Buffer()
165165
defer buffer.Free()
166-
if optionsHolder.packageAlias != "" {
166+
if optionsHolder.packageAlias != "" { //nolint:nestif
167167
buffer.WriteString(optionsHolder.packageAlias)
168168
} else {
169169
if optionsHolder.packagePath {

0 commit comments

Comments
 (0)