@@ -2,6 +2,7 @@ package query
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"time"
6
7
7
8
"github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1"
32
33
_ sessionPool = (* pool.Pool [* Session , Session ])(nil )
33
34
)
34
35
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
+
35
40
type (
36
41
sessionPool interface {
37
42
closer.Closer
@@ -173,6 +178,10 @@ func (c *Client) ExecuteScript(
173
178
),
174
179
}
175
180
181
+ if err := checkTxControlWithCommit (settings .TxControl ()); err != nil {
182
+ return nil , err
183
+ }
184
+
176
185
request , grpcOpts , err := executeQueryScriptRequest (q , settings )
177
186
if err != nil {
178
187
return op , xerrors .WithStackTrace (err )
@@ -320,6 +329,10 @@ func (c *Client) QueryRow(ctx context.Context, q string, opts ...options.Execute
320
329
321
330
settings := options .ExecuteSettings (opts ... )
322
331
332
+ if err := checkTxControlWithCommit (settings .TxControl ()); err != nil {
333
+ return nil , err
334
+ }
335
+
323
336
onDone := trace .QueryOnQueryRow (c .config .Trace (), & ctx ,
324
337
stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/query.(*Client).QueryRow" ),
325
338
q , settings .Label (),
@@ -366,6 +379,11 @@ func (c *Client) Exec(ctx context.Context, q string, opts ...options.Execute) (f
366
379
defer cancel ()
367
380
368
381
settings := options .ExecuteSettings (opts ... )
382
+
383
+ if err := checkTxControlWithCommit (settings .TxControl ()); err != nil {
384
+ return err
385
+ }
386
+
369
387
onDone := trace .QueryOnExec (c .config .Trace (), & ctx ,
370
388
stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/query.(*Client).Exec" ),
371
389
q ,
@@ -415,6 +433,11 @@ func (c *Client) Query(ctx context.Context, q string, opts ...options.Execute) (
415
433
defer cancel ()
416
434
417
435
settings := options .ExecuteSettings (opts ... )
436
+
437
+ if err := checkTxControlWithCommit (settings .TxControl ()); err != nil {
438
+ return nil , err
439
+ }
440
+
418
441
onDone := trace .QueryOnQuery (c .config .Trace (), & ctx ,
419
442
stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/query.(*Client).Query" ),
420
443
q , settings .Label (),
@@ -470,6 +493,10 @@ func (c *Client) QueryResultSet(
470
493
err error
471
494
)
472
495
496
+ if err := checkTxControlWithCommit (settings .TxControl ()); err != nil {
497
+ return nil , err
498
+ }
499
+
473
500
onDone := trace .QueryOnQueryResultSet (c .config .Trace (), & ctx ,
474
501
stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/query.(*Client).QueryResultSet" ),
475
502
q , settings .Label (),
@@ -612,6 +639,14 @@ func New(ctx context.Context, cc grpc.ClientConnInterface, cfg *config.Config) *
612
639
}
613
640
}
614
641
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
+
615
650
func poolTrace (t * trace.Query ) * pool.Trace {
616
651
return & pool.Trace {
617
652
OnNew : func (ctx * context.Context , call stack.Caller ) func (limit int ) {
0 commit comments