@@ -719,7 +719,7 @@ func (s *Session) routingKeyInfo(ctx context.Context, stmt string, keyspace stri
719
719
// Exec executes a batch operation and returns nil if successful
720
720
// otherwise an error is returned describing the failure.
721
721
func (b * Batch ) Exec () error {
722
- iter := b .session .executeBatch (b , nil )
722
+ iter := b .session .executeBatch (b , b . context )
723
723
return iter .Close ()
724
724
}
725
725
@@ -732,7 +732,7 @@ func (b *Batch) ExecContext(ctx context.Context) error {
732
732
733
733
// Iter executes a batch operation and returns an Iter object
734
734
// that can be used to access properties related to the execution like Iter.Attempts and Iter.Latency
735
- func (b * Batch ) Iter () * Iter { return b .IterContext (nil ) }
735
+ func (b * Batch ) Iter () * Iter { return b .IterContext (b . context ) }
736
736
737
737
// IterContext executes a batch operation with the provided context and returns an Iter object
738
738
// that can be used to access properties related to the execution like Iter.Attempts and Iter.Latency
@@ -766,7 +766,7 @@ func (s *Session) executeBatch(batch *Batch, ctx context.Context) *Iter {
766
766
// ExecuteBatch executes a batch operation and returns nil if successful
767
767
// otherwise an error is returned describing the failure.
768
768
func (s * Session ) ExecuteBatch (batch * Batch ) error {
769
- iter := s .executeBatch (batch , nil )
769
+ iter := s .executeBatch (batch , batch . context )
770
770
return iter .Close ()
771
771
}
772
772
@@ -786,7 +786,16 @@ func (s *Session) ExecuteBatchCAS(batch *Batch, dest ...interface{}) (applied bo
786
786
// Further scans on the interator must also remember to include
787
787
// the applied boolean as the first argument to *Iter.Scan
788
788
func (b * Batch ) ExecCAS (dest ... interface {}) (applied bool , iter * Iter , err error ) {
789
- iter = b .session .executeBatch (b , nil )
789
+ return b .ExecCASContext (b .context , dest ... )
790
+ }
791
+
792
+ // ExecCASContext executes a batch operation with the provided context and returns true if successful and
793
+ // an iterator (to scan additional rows if more than one conditional statement)
794
+ // was sent.
795
+ // Further scans on the interator must also remember to include
796
+ // the applied boolean as the first argument to *Iter.Scan
797
+ func (b * Batch ) ExecCASContext (ctx context.Context , dest ... interface {}) (applied bool , iter * Iter , err error ) {
798
+ iter = b .session .executeBatch (b , ctx )
790
799
if err := iter .checkErrAndNotFound (); err != nil {
791
800
iter .Close ()
792
801
return false , nil , err
@@ -814,7 +823,14 @@ func (s *Session) MapExecuteBatchCAS(batch *Batch, dest map[string]interface{})
814
823
// however it accepts a map rather than a list of arguments for the initial
815
824
// scan.
816
825
func (b * Batch ) MapExecCAS (dest map [string ]interface {}) (applied bool , iter * Iter , err error ) {
817
- iter = b .session .executeBatch (b , nil )
826
+ return b .MapExecCASContext (b .context , dest )
827
+ }
828
+
829
+ // MapExecCASContext executes a batch operation with the provided context much like ExecuteBatchCAS,
830
+ // however it accepts a map rather than a list of arguments for the initial
831
+ // scan.
832
+ func (b * Batch ) MapExecCASContext (ctx context.Context , dest map [string ]interface {}) (applied bool , iter * Iter , err error ) {
833
+ iter = b .session .executeBatch (b , ctx )
818
834
if err := iter .checkErrAndNotFound (); err != nil {
819
835
iter .Close ()
820
836
return false , nil , err
@@ -1057,6 +1073,8 @@ func (q *Query) CustomPayload(customPayload map[string][]byte) *Query {
1057
1073
return q
1058
1074
}
1059
1075
1076
+ // Deprecated: Context retrieval is deprecated. Pass context directly to execution methods
1077
+ // like ExecContext or IterContext instead.
1060
1078
func (q * Query ) Context () context.Context {
1061
1079
if q .context == nil {
1062
1080
return context .Background ()
@@ -1272,7 +1290,7 @@ func isUseStatement(stmt string) bool {
1272
1290
// Iter executes the query and returns an iterator capable of iterating
1273
1291
// over all results.
1274
1292
func (q * Query ) Iter () * Iter {
1275
- return q .IterContext (nil )
1293
+ return q .IterContext (q . context )
1276
1294
}
1277
1295
1278
1296
// IterContext executes the query with the provided context and returns an iterator capable of iterating
@@ -1297,7 +1315,14 @@ func (q *Query) iterInternal(c *Conn, ctx context.Context) *Iter {
1297
1315
// row into the map pointed at by m and discards the rest. If no rows
1298
1316
// were selected, ErrNotFound is returned.
1299
1317
func (q * Query ) MapScan (m map [string ]interface {}) error {
1300
- iter := q .Iter ()
1318
+ return q .MapScanContext (q .context , m )
1319
+ }
1320
+
1321
+ // MapScanContext executes the query with the provided context, copies the columns of the first selected
1322
+ // row into the map pointed at by m and discards the rest. If no rows
1323
+ // were selected, ErrNotFound is returned.
1324
+ func (q * Query ) MapScanContext (ctx context.Context , m map [string ]interface {}) error {
1325
+ iter := q .IterContext (ctx )
1301
1326
if err := iter .checkErrAndNotFound (); err != nil {
1302
1327
return err
1303
1328
}
@@ -1309,7 +1334,14 @@ func (q *Query) MapScan(m map[string]interface{}) error {
1309
1334
// row into the values pointed at by dest and discards the rest. If no rows
1310
1335
// were selected, ErrNotFound is returned.
1311
1336
func (q * Query ) Scan (dest ... interface {}) error {
1312
- iter := q .Iter ()
1337
+ return q .ScanContext (q .context , dest ... )
1338
+ }
1339
+
1340
+ // ScanContext executes the query with the provided context, copies the columns of the first selected
1341
+ // row into the values pointed at by dest and discards the rest. If no rows
1342
+ // were selected, ErrNotFound is returned.
1343
+ func (q * Query ) ScanContext (ctx context.Context , dest ... interface {}) error {
1344
+ iter := q .IterContext (ctx )
1313
1345
if err := iter .checkErrAndNotFound (); err != nil {
1314
1346
return err
1315
1347
}
@@ -1326,8 +1358,20 @@ func (q *Query) Scan(dest ...interface{}) error {
1326
1358
// SELECT * FROM. So using ScanCAS with INSERT is inherently prone to
1327
1359
// column mismatching. Use MapScanCAS to capture them safely.
1328
1360
func (q * Query ) ScanCAS (dest ... interface {}) (applied bool , err error ) {
1361
+ return q .ScanCASContext (q .context , dest ... )
1362
+ }
1363
+
1364
+ // ScanCASContext executes a lightweight transaction (i.e. an UPDATE or INSERT
1365
+ // statement containing an IF clause) with the provided context. If the transaction fails because
1366
+ // the existing values did not match, the previous values will be stored
1367
+ // in dest.
1368
+ //
1369
+ // As for INSERT .. IF NOT EXISTS, previous values will be returned as if
1370
+ // SELECT * FROM. So using ScanCAS with INSERT is inherently prone to
1371
+ // column mismatching. Use MapScanCAS to capture them safely.
1372
+ func (q * Query ) ScanCASContext (ctx context.Context , dest ... interface {}) (applied bool , err error ) {
1329
1373
q .disableSkipMetadata = true
1330
- iter := q .Iter ( )
1374
+ iter := q .IterContext ( ctx )
1331
1375
if err := iter .checkErrAndNotFound (); err != nil {
1332
1376
return false , err
1333
1377
}
@@ -1349,8 +1393,20 @@ func (q *Query) ScanCAS(dest ...interface{}) (applied bool, err error) {
1349
1393
// SELECT * FROM. So using ScanCAS with INSERT is inherently prone to
1350
1394
// column mismatching. MapScanCAS is added to capture them safely.
1351
1395
func (q * Query ) MapScanCAS (dest map [string ]interface {}) (applied bool , err error ) {
1396
+ return q .MapScanCASContext (q .context , dest )
1397
+ }
1398
+
1399
+ // MapScanCASContext executes a lightweight transaction (i.e. an UPDATE or INSERT
1400
+ // statement containing an IF clause) with the provided context. If the transaction fails because
1401
+ // the existing values did not match, the previous values will be stored
1402
+ // in dest map.
1403
+ //
1404
+ // As for INSERT .. IF NOT EXISTS, previous values will be returned as if
1405
+ // SELECT * FROM. So using ScanCAS with INSERT is inherently prone to
1406
+ // column mismatching. MapScanCAS is added to capture them safely.
1407
+ func (q * Query ) MapScanCASContext (ctx context.Context , dest map [string ]interface {}) (applied bool , err error ) {
1352
1408
q .disableSkipMetadata = true
1353
- iter := q .Iter ( )
1409
+ iter := q .IterContext ( ctx )
1354
1410
if err := iter .checkErrAndNotFound (); err != nil {
1355
1411
return false , err
1356
1412
}
@@ -1834,6 +1890,8 @@ func (b *Batch) SetConsistency(c Consistency) {
1834
1890
b .Cons = c
1835
1891
}
1836
1892
1893
+ // Deprecated: Context retrieval is deprecated. Pass context directly to execution methods
1894
+ // like ExecContext or IterContext instead.
1837
1895
func (b * Batch ) Context () context.Context {
1838
1896
if b .context == nil {
1839
1897
return context .Background ()
0 commit comments