Skip to content

Commit 61e64e6

Browse files
committed
Add missing Context variant methods to Query and Batch
In CASSGO-22 we deprecated WithContext but we didn't deprecate Context(). IterContext and ExecContext were added as replacements but there are other execution methods that lack a Context variant. This patch addresses both of these issues. Patch by João Reis; reviewed by James Hartig for CASSGO-81
1 parent a7f7f31 commit 61e64e6

File tree

2 files changed

+69
-10
lines changed

2 files changed

+69
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
- Externally-defined type registration (CASSGO-43)
2626
- Add Query and Batch to ObservedQuery and ObservedBatch (CASSGO-73)
2727
- Add way to create HostInfo objects for testing purposes (CASSGO-71)
28+
- Add missing Context methods on Query and Batch (CASSGO-81)
2829

2930
### Changed
3031

session.go

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ func (s *Session) routingKeyInfo(ctx context.Context, stmt string, keyspace stri
719719
// Exec executes a batch operation and returns nil if successful
720720
// otherwise an error is returned describing the failure.
721721
func (b *Batch) Exec() error {
722-
iter := b.session.executeBatch(b, nil)
722+
iter := b.session.executeBatch(b, b.context)
723723
return iter.Close()
724724
}
725725

@@ -732,7 +732,7 @@ func (b *Batch) ExecContext(ctx context.Context) error {
732732

733733
// Iter executes a batch operation and returns an Iter object
734734
// 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) }
736736

737737
// IterContext executes a batch operation with the provided context and returns an Iter object
738738
// 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 {
766766
// ExecuteBatch executes a batch operation and returns nil if successful
767767
// otherwise an error is returned describing the failure.
768768
func (s *Session) ExecuteBatch(batch *Batch) error {
769-
iter := s.executeBatch(batch, nil)
769+
iter := s.executeBatch(batch, batch.context)
770770
return iter.Close()
771771
}
772772

@@ -786,7 +786,16 @@ func (s *Session) ExecuteBatchCAS(batch *Batch, dest ...interface{}) (applied bo
786786
// Further scans on the interator must also remember to include
787787
// the applied boolean as the first argument to *Iter.Scan
788788
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)
790799
if err := iter.checkErrAndNotFound(); err != nil {
791800
iter.Close()
792801
return false, nil, err
@@ -814,7 +823,14 @@ func (s *Session) MapExecuteBatchCAS(batch *Batch, dest map[string]interface{})
814823
// however it accepts a map rather than a list of arguments for the initial
815824
// scan.
816825
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)
818834
if err := iter.checkErrAndNotFound(); err != nil {
819835
iter.Close()
820836
return false, nil, err
@@ -1057,6 +1073,8 @@ func (q *Query) CustomPayload(customPayload map[string][]byte) *Query {
10571073
return q
10581074
}
10591075

1076+
// Deprecated: Context retrieval is deprecated. Pass context directly to execution methods
1077+
// like ExecContext or IterContext instead.
10601078
func (q *Query) Context() context.Context {
10611079
if q.context == nil {
10621080
return context.Background()
@@ -1272,7 +1290,7 @@ func isUseStatement(stmt string) bool {
12721290
// Iter executes the query and returns an iterator capable of iterating
12731291
// over all results.
12741292
func (q *Query) Iter() *Iter {
1275-
return q.IterContext(nil)
1293+
return q.IterContext(q.context)
12761294
}
12771295

12781296
// 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 {
12971315
// row into the map pointed at by m and discards the rest. If no rows
12981316
// were selected, ErrNotFound is returned.
12991317
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)
13011326
if err := iter.checkErrAndNotFound(); err != nil {
13021327
return err
13031328
}
@@ -1309,7 +1334,14 @@ func (q *Query) MapScan(m map[string]interface{}) error {
13091334
// row into the values pointed at by dest and discards the rest. If no rows
13101335
// were selected, ErrNotFound is returned.
13111336
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)
13131345
if err := iter.checkErrAndNotFound(); err != nil {
13141346
return err
13151347
}
@@ -1326,8 +1358,20 @@ func (q *Query) Scan(dest ...interface{}) error {
13261358
// SELECT * FROM. So using ScanCAS with INSERT is inherently prone to
13271359
// column mismatching. Use MapScanCAS to capture them safely.
13281360
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) {
13291373
q.disableSkipMetadata = true
1330-
iter := q.Iter()
1374+
iter := q.IterContext(ctx)
13311375
if err := iter.checkErrAndNotFound(); err != nil {
13321376
return false, err
13331377
}
@@ -1349,8 +1393,20 @@ func (q *Query) ScanCAS(dest ...interface{}) (applied bool, err error) {
13491393
// SELECT * FROM. So using ScanCAS with INSERT is inherently prone to
13501394
// column mismatching. MapScanCAS is added to capture them safely.
13511395
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) {
13521408
q.disableSkipMetadata = true
1353-
iter := q.Iter()
1409+
iter := q.IterContext(ctx)
13541410
if err := iter.checkErrAndNotFound(); err != nil {
13551411
return false, err
13561412
}
@@ -1834,6 +1890,8 @@ func (b *Batch) SetConsistency(c Consistency) {
18341890
b.Cons = c
18351891
}
18361892

1893+
// Deprecated: Context retrieval is deprecated. Pass context directly to execution methods
1894+
// like ExecContext or IterContext instead.
18371895
func (b *Batch) Context() context.Context {
18381896
if b.context == nil {
18391897
return context.Background()

0 commit comments

Comments
 (0)