Skip to content

Commit 253f801

Browse files
committed
* Added validation of WithTxControl option in non-interactive methods of Client and Session
1 parent 4ebb2e4 commit 253f801

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Added validation of WithTxControl option in non-interactive methods of Client and Session
2+
13
## v3.108.0
24
* Added `query.EmptyTxControl()` for empty transaction control (server-side defines transaction control by internal logic)
35
* Marked as deprecated `query.NoTx()` because this is wrong name for server-side transaction control inference

internal/query/client.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package query
22

33
import (
44
"context"
5+
"errors"
56
"time"
67

78
"github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1"
@@ -32,6 +33,10 @@ var (
3233
_ sessionPool = (*pool.Pool[*Session, Session])(nil)
3334
)
3435

36+
var (
37+
errNoCommit = xerrors.Wrap(errors.New("WithTxControl option is not allowed without CommitTx() option in Client methods, as these methods are non-interactive. You can either add the CommitTx() option to TxControl or use query.*TxControl methods (e.g., query.SnapshotReadOnlyTxControl) which already include the commit flag"))
38+
)
39+
3540
type (
3641
sessionPool interface {
3742
closer.Closer
@@ -173,6 +178,10 @@ func (c *Client) ExecuteScript(
173178
),
174179
}
175180

181+
if err := checkTxControlWithCommit(settings.TxControl()); err != nil {
182+
return nil, err
183+
}
184+
176185
request, grpcOpts, err := executeQueryScriptRequest(q, settings)
177186
if err != nil {
178187
return op, xerrors.WithStackTrace(err)
@@ -320,6 +329,10 @@ func (c *Client) QueryRow(ctx context.Context, q string, opts ...options.Execute
320329

321330
settings := options.ExecuteSettings(opts...)
322331

332+
if err := checkTxControlWithCommit(settings.TxControl()); err != nil {
333+
return nil, err
334+
}
335+
323336
onDone := trace.QueryOnQueryRow(c.config.Trace(), &ctx,
324337
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/query.(*Client).QueryRow"),
325338
q, settings.Label(),
@@ -366,6 +379,11 @@ func (c *Client) Exec(ctx context.Context, q string, opts ...options.Execute) (f
366379
defer cancel()
367380

368381
settings := options.ExecuteSettings(opts...)
382+
383+
if err := checkTxControlWithCommit(settings.TxControl()); err != nil {
384+
return err
385+
}
386+
369387
onDone := trace.QueryOnExec(c.config.Trace(), &ctx,
370388
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/query.(*Client).Exec"),
371389
q,
@@ -415,6 +433,11 @@ func (c *Client) Query(ctx context.Context, q string, opts ...options.Execute) (
415433
defer cancel()
416434

417435
settings := options.ExecuteSettings(opts...)
436+
437+
if err := checkTxControlWithCommit(settings.TxControl()); err != nil {
438+
return nil, err
439+
}
440+
418441
onDone := trace.QueryOnQuery(c.config.Trace(), &ctx,
419442
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/query.(*Client).Query"),
420443
q, settings.Label(),
@@ -470,6 +493,10 @@ func (c *Client) QueryResultSet(
470493
err error
471494
)
472495

496+
if err := checkTxControlWithCommit(settings.TxControl()); err != nil {
497+
return nil, err
498+
}
499+
473500
onDone := trace.QueryOnQueryResultSet(c.config.Trace(), &ctx,
474501
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/query.(*Client).QueryResultSet"),
475502
q, settings.Label(),
@@ -612,6 +639,14 @@ func New(ctx context.Context, cc grpc.ClientConnInterface, cfg *config.Config) *
612639
}
613640
}
614641

642+
// checkTxControlWithCommit checks that if WithTxControl is used, it must be with WithCommit
643+
func checkTxControlWithCommit(txControl options.TxControl) error {
644+
if txControl != nil && !txControl.Commit() {
645+
return xerrors.WithStackTrace(errNoCommit)
646+
}
647+
return nil
648+
}
649+
615650
func poolTrace(t *trace.Query) *pool.Trace {
616651
return &pool.Trace{
617652
OnNew: func(ctx *context.Context, call stack.Caller) func(limit int) {

internal/query/session.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ func (s *Session) QueryResultSet(
3636
onDone(finalErr)
3737
}()
3838

39-
r, err := s.execute(ctx, q, options.ExecuteSettings(opts...), withTrace(s.trace))
39+
settings := options.ExecuteSettings(opts...)
40+
if err := checkTxControlWithCommit(settings.TxControl()); err != nil {
41+
return nil, err
42+
}
43+
44+
r, err := s.execute(ctx, q, settings, withTrace(s.trace))
4045
if err != nil {
4146
return nil, xerrors.WithStackTrace(err)
4247
}
@@ -75,7 +80,12 @@ func (s *Session) QueryRow(ctx context.Context, q string, opts ...options.Execut
7580
onDone(finalErr)
7681
}()
7782

78-
row, err := s.queryRow(ctx, q, options.ExecuteSettings(opts...), withTrace(s.trace))
83+
settings := options.ExecuteSettings(opts...)
84+
if err := checkTxControlWithCommit(settings.TxControl()); err != nil {
85+
return nil, err
86+
}
87+
88+
row, err := s.queryRow(ctx, q, settings, withTrace(s.trace))
7989
if err != nil {
8090
return nil, xerrors.WithStackTrace(err)
8191
}
@@ -154,6 +164,11 @@ func (s *Session) execute(
154164

155165
func (s *Session) Exec(ctx context.Context, q string, opts ...options.Execute) (finalErr error) {
156166
settings := options.ExecuteSettings(opts...)
167+
168+
if err := checkTxControlWithCommit(settings.TxControl()); err != nil {
169+
return err
170+
}
171+
157172
onDone := trace.QueryOnSessionExec(s.trace, &ctx,
158173
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/query.(*Session).Exec"),
159174
s,
@@ -182,6 +197,11 @@ func (s *Session) Exec(ctx context.Context, q string, opts ...options.Execute) (
182197

183198
func (s *Session) Query(ctx context.Context, q string, opts ...options.Execute) (_ query.Result, finalErr error) {
184199
settings := options.ExecuteSettings(opts...)
200+
201+
if err := checkTxControlWithCommit(settings.TxControl()); err != nil {
202+
return nil, err
203+
}
204+
185205
onDone := trace.QueryOnSessionQuery(s.trace, &ctx,
186206
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/query.(*Session).Query"),
187207
s,

0 commit comments

Comments
 (0)