@@ -2,6 +2,7 @@ package query
22
33import (
44 "context"
5+ "errors"
56 "time"
67
78 "github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1"
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+
3540type (
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+
615650func poolTrace (t * trace.Query ) * pool.Trace {
616651 return & pool.Trace {
617652 OnNew : func (ctx * context.Context , call stack.Caller ) func (limit int ) {
0 commit comments