Skip to content

Commit 67e8bc4

Browse files
authored
Merge pull request #1689 from ydb-platform/query-client
* Switched internal type of result `ydb.Driver.Query()` from `*internal/query.Client` to `query.Client` interface
2 parents c475608 + 4b262b8 commit 67e8bc4

File tree

9 files changed

+52
-57
lines changed

9 files changed

+52
-57
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Switched internal type of result `ydb.Driver.Query()` from `*internal/query.Client` to `query.Client` interface
2+
13
## v3.101.3
24
* Added `query.TransactionActor` type alias to `query.TxActor` for compatibility with `table.Client` API's
35
* Removed comment `experimental` from `ydb.ParamsBuilder` and `ydb.ParamsFromMap`

driver.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
4343
"github.com/ydb-platform/ydb-go-sdk/v3/log"
4444
"github.com/ydb-platform/ydb-go-sdk/v3/operation"
45+
"github.com/ydb-platform/ydb-go-sdk/v3/query"
4546
"github.com/ydb-platform/ydb-go-sdk/v3/ratelimiter"
4647
"github.com/ydb-platform/ydb-go-sdk/v3/scheme"
4748
"github.com/ydb-platform/ydb-go-sdk/v3/scripting"
@@ -228,7 +229,7 @@ func (d *Driver) Table() table.Client {
228229
}
229230

230231
// Query returns query client
231-
func (d *Driver) Query() *internalQuery.Client {
232+
func (d *Driver) Query() query.Client {
232233
return d.query.Must()
233234
}
234235

dsn_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestParse(t *testing.T) {
3030
return xtable.New(context.Background(), nil, nil, opts...)
3131
}
3232
newQueryConn := func(opts ...xquery.Option) *xquery.Conn {
33-
return xquery.New(context.Background(), nil, nil, opts...)
33+
return xquery.New(context.Background(), nil, opts...)
3434
}
3535
compareConfigs := func(t *testing.T, lhs, rhs *config.Config) {
3636
require.Equal(t, lhs.Secure(), rhs.Secure())

internal/query/client.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ type (
4949
}
5050
)
5151

52+
func (c *Client) Config() *config.Config {
53+
return c.config
54+
}
55+
5256
func fetchScriptResults(ctx context.Context,
5357
client Ydb_Query_V1.QueryServiceClient,
5458
opID string, opts ...options.FetchScriptOption,
@@ -511,28 +515,28 @@ func (c *Client) DoTx(ctx context.Context, op query.TxOperation, opts ...options
511515
return nil
512516
}
513517

514-
func CreateSession(ctx context.Context, c *Client) (*Session, error) {
518+
func CreateSession(ctx context.Context, client Ydb_Query_V1.QueryServiceClient, cfg *config.Config) (*Session, error) {
515519
s, err := retry.RetryWithResult(ctx, func(ctx context.Context) (*Session, error) {
516520
var (
517521
createCtx context.Context
518522
cancelCreate context.CancelFunc
519523
)
520-
if d := c.config.SessionCreateTimeout(); d > 0 {
524+
if d := cfg.SessionCreateTimeout(); d > 0 {
521525
createCtx, cancelCreate = xcontext.WithTimeout(ctx, d)
522526
} else {
523527
createCtx, cancelCreate = xcontext.WithCancel(ctx)
524528
}
525529
defer cancelCreate()
526530

527-
s, err := createSession(createCtx, c.client,
528-
WithDeleteTimeout(c.config.SessionDeleteTimeout()),
529-
WithTrace(c.config.Trace()),
531+
s, err := createSession(createCtx, client,
532+
WithDeleteTimeout(cfg.SessionDeleteTimeout()),
533+
WithTrace(cfg.Trace()),
530534
)
531535
if err != nil {
532536
return nil, xerrors.WithStackTrace(err)
533537
}
534538

535-
s.lazyTx = c.config.LazyTx()
539+
s.lazyTx = cfg.LazyTx()
536540

537541
return s, nil
538542
})

internal/xsql/conn_helpers.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (c *Conn) Engine() Engine {
4646
}
4747

4848
func (c *Conn) GetDatabaseName() string {
49-
return c.connector.Name()
49+
return c.connector.parent.Name()
5050
}
5151

5252
func (c *Conn) normalizePath(tableName string) string {
@@ -153,11 +153,11 @@ func isSysDir(databaseName, dirAbsPath string) bool {
153153
func (c *Conn) getTables(ctx context.Context, absPath string, recursive, excludeSysDirs bool) (
154154
tables []string, _ error,
155155
) {
156-
if excludeSysDirs && isSysDir(c.connector.Name(), absPath) {
156+
if excludeSysDirs && isSysDir(c.connector.parent.Name(), absPath) {
157157
return nil, nil
158158
}
159159

160-
d, err := c.connector.Scheme().ListDirectory(ctx, absPath)
160+
d, err := c.connector.parent.Scheme().ListDirectory(ctx, absPath)
161161
if err != nil {
162162
return nil, xerrors.WithStackTrace(err)
163163
}
@@ -189,7 +189,7 @@ func (c *Conn) GetTables(ctx context.Context, folder string, recursive, excludeS
189189
) {
190190
absPath := c.normalizePath(folder)
191191

192-
e, err := c.connector.Scheme().DescribePath(ctx, absPath)
192+
e, err := c.connector.parent.Scheme().DescribePath(ctx, absPath)
193193
if err != nil {
194194
return nil, xerrors.WithStackTrace(err)
195195
}

internal/xsql/connector.go

+25-35
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ import (
1010

1111
"github.com/google/uuid"
1212
"github.com/jonboulle/clockwork"
13+
"github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1"
1314
"google.golang.org/grpc"
1415

1516
"github.com/ydb-platform/ydb-go-sdk/v3/internal/bind"
16-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query"
17+
internalQuery "github.com/ydb-platform/ydb-go-sdk/v3/internal/query"
18+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/config"
1719
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
1820
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
1921
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
2022
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/xquery"
2123
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/xtable"
2224
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
25+
"github.com/ydb-platform/ydb-go-sdk/v3/query"
2326
"github.com/ydb-platform/ydb-go-sdk/v3/retry/budget"
2427
"github.com/ydb-platform/ydb-go-sdk/v3/scheme"
2528
"github.com/ydb-platform/ydb-go-sdk/v3/scripting"
@@ -35,8 +38,9 @@ var (
3538
type (
3639
Engine uint8
3740
Connector struct {
38-
parent ydbDriver
39-
balancer grpc.ClientConnInterface
41+
parent ydbDriver
42+
balancer grpc.ClientConnInterface
43+
queryConfig *config.Config
4044

4145
processor Engine
4246

@@ -58,7 +62,7 @@ type (
5862
ydbDriver interface {
5963
Name() string
6064
Table() table.Client
61-
Query() *query.Client
65+
Query() query.Client
6266
Scripting() scripting.Client
6367
Scheme() scheme.Client
6468
}
@@ -75,6 +79,10 @@ func (e Engine) String() string {
7579
}
7680
}
7781

82+
func (c *Connector) Parent() ydbDriver {
83+
return c.parent
84+
}
85+
7886
func (c *Connector) RetryBudget() budget.Budget {
7987
return c.retryBudget
8088
}
@@ -95,26 +103,6 @@ func (c *Connector) TraceRetry() *trace.Retry {
95103
return c.traceRetry
96104
}
97105

98-
func (c *Connector) Query() *query.Client {
99-
return c.parent.Query()
100-
}
101-
102-
func (c *Connector) Name() string {
103-
return c.parent.Name()
104-
}
105-
106-
func (c *Connector) Table() table.Client {
107-
return c.parent.Table()
108-
}
109-
110-
func (c *Connector) Scripting() scripting.Client {
111-
return c.parent.Scripting()
112-
}
113-
114-
func (c *Connector) Scheme() scheme.Client {
115-
return c.parent.Scheme()
116-
}
117-
118106
const (
119107
QUERY = iota + 1
120108
TABLE
@@ -131,7 +119,7 @@ func (c *Connector) Connect(ctx context.Context) (_ driver.Conn, finalErr error)
131119

132120
switch c.processor {
133121
case QUERY:
134-
s, err := query.CreateSession(ctx, c.Query())
122+
s, err := internalQuery.CreateSession(ctx, Ydb_Query_V1.NewQueryServiceClient(c.balancer), c.queryConfig)
135123
defer func() {
136124
onDone(s, finalErr)
137125
}()
@@ -143,7 +131,7 @@ func (c *Connector) Connect(ctx context.Context) (_ driver.Conn, finalErr error)
143131

144132
conn := &Conn{
145133
processor: QUERY,
146-
cc: xquery.New(ctx, c, s, append(
134+
cc: xquery.New(ctx, s, append(
147135
c.QueryOpts,
148136
xquery.WithOnClose(func() {
149137
c.conns.Delete(id)
@@ -159,7 +147,7 @@ func (c *Connector) Connect(ctx context.Context) (_ driver.Conn, finalErr error)
159147
return conn, nil
160148

161149
case TABLE:
162-
s, err := c.Table().CreateSession(ctx) //nolint:staticcheck
150+
s, err := c.parent.Table().CreateSession(ctx) //nolint:staticcheck
163151
defer func() {
164152
onDone(s, finalErr)
165153
}()
@@ -171,7 +159,7 @@ func (c *Connector) Connect(ctx context.Context) (_ driver.Conn, finalErr error)
171159

172160
conn := &Conn{
173161
processor: TABLE,
174-
cc: xtable.New(ctx, c, s, append(c.TableOpts,
162+
cc: xtable.New(ctx, c.parent.Scripting(), s, append(c.TableOpts,
175163
xtable.WithOnClose(func() {
176164
c.conns.Delete(id)
177165
}))...,
@@ -193,10 +181,6 @@ func (c *Connector) Driver() driver.Driver {
193181
return c
194182
}
195183

196-
func (c *Connector) Parent() ydbDriver {
197-
return c.parent
198-
}
199-
200184
func (c *Connector) Close() error {
201185
select {
202186
case <-c.done:
@@ -212,10 +196,16 @@ func (c *Connector) Close() error {
212196
}
213197
}
214198

215-
func Open(parent ydbDriver, balancer grpc.ClientConnInterface, opts ...Option) (_ *Connector, err error) {
199+
func Open(
200+
parent ydbDriver,
201+
balancer grpc.ClientConnInterface,
202+
queryConfig *config.Config,
203+
opts ...Option,
204+
) (_ *Connector, err error) {
216205
c := &Connector{
217-
parent: parent,
218-
balancer: balancer,
206+
parent: parent,
207+
balancer: balancer,
208+
queryConfig: queryConfig,
219209
processor: func() Engine {
220210
if overQueryService, _ := strconv.ParseBool(os.Getenv("YDB_DATABASE_SQL_OVER_QUERY_SERVICE")); overQueryService {
221211
return QUERY

internal/xsql/xquery/conn.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ type Parent interface {
2828

2929
type Conn struct {
3030
ctx context.Context //nolint:containedctx
31-
parent Parent
3231
session *query.Session
3332
onClose []func()
3433
closed atomic.Bool
@@ -117,10 +116,9 @@ func (c *Conn) Explain(ctx context.Context, sql string, _ *params.Params) (ast s
117116
return ast, plan, nil
118117
}
119118

120-
func New(ctx context.Context, parent Parent, s *query.Session, opts ...Option) *Conn {
119+
func New(ctx context.Context, s *query.Session, opts ...Option) *Conn {
121120
cc := &Conn{
122121
ctx: ctx,
123-
parent: parent,
124122
session: s,
125123
}
126124

internal/xsql/xtable/conn.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ type (
2727
Conn struct {
2828
ctx context.Context //nolint:containedctx
2929

30-
parent Parent
31-
session table.ClosableSession // Immutable and r/o usage.
30+
scriptingClient scripting.Client
31+
session table.ClosableSession // Immutable and r/o usage.
3232

3333
fakeTxModes []QueryMode
3434

@@ -116,10 +116,10 @@ type resultNoRows struct{}
116116
func (resultNoRows) LastInsertId() (int64, error) { return 0, ErrUnsupported }
117117
func (resultNoRows) RowsAffected() (int64, error) { return 0, ErrUnsupported }
118118

119-
func New(ctx context.Context, parent Parent, s table.ClosableSession, opts ...Option) *Conn {
119+
func New(ctx context.Context, scriptingClient scripting.Client, s table.ClosableSession, opts ...Option) *Conn {
120120
cc := &Conn{
121121
ctx: ctx,
122-
parent: parent,
122+
scriptingClient: scriptingClient,
123123
session: s,
124124
defaultQueryMode: DataQueryMode,
125125
defaultTxControl: table.DefaultTxControl(),
@@ -169,7 +169,7 @@ func (c *Conn) executeSchemeQuery(ctx context.Context, sql string) (driver.Resul
169169
func (c *Conn) executeScriptingQuery(ctx context.Context, sql string, params *params.Params) (
170170
driver.Result, error,
171171
) {
172-
res, err := c.parent.Scripting().StreamExecute(ctx, sql, params)
172+
res, err := c.scriptingClient.StreamExecute(ctx, sql, params)
173173
if err != nil {
174174
return nil, badconn.Map(xerrors.WithStackTrace(err))
175175
}
@@ -227,7 +227,7 @@ func (c *Conn) execScanQuery(ctx context.Context, sql string, params *params.Par
227227
func (c *Conn) execScriptingQuery(ctx context.Context, sql string, params *params.Params) (
228228
driver.RowsNextResultSet, error,
229229
) {
230-
res, err := c.parent.Scripting().StreamExecute(ctx, sql, params)
230+
res, err := c.scriptingClient.StreamExecute(ctx, sql, params)
231231
if err != nil {
232232
return nil, badconn.Map(xerrors.WithStackTrace(err))
233233
}

sql.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ type SQLConnector interface {
223223
}
224224

225225
func Connector(parent *Driver, opts ...ConnectorOption) (SQLConnector, error) {
226-
c, err := xsql.Open(parent, parent.metaBalancer,
226+
c, err := xsql.Open(parent, parent.metaBalancer, parent.query.Must().Config(),
227227
append(
228228
append(
229229
parent.databaseSQLOptions,

0 commit comments

Comments
 (0)