Skip to content

Commit 28b5f48

Browse files
committed
add missing Context variant methods
1 parent a9bfe80 commit 28b5f48

File tree

1 file changed

+60
-6
lines changed

1 file changed

+60
-6
lines changed

session.go

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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(nil, 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(nil, 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
@@ -1297,7 +1313,14 @@ func (q *Query) iterInternal(c *Conn, ctx context.Context) *Iter {
12971313
// row into the map pointed at by m and discards the rest. If no rows
12981314
// were selected, ErrNotFound is returned.
12991315
func (q *Query) MapScan(m map[string]interface{}) error {
1300-
iter := q.Iter()
1316+
return q.MapScanContext(nil, m)
1317+
}
1318+
1319+
// MapScanContext executes the query with the provided context, copies the columns of the first selected
1320+
// row into the map pointed at by m and discards the rest. If no rows
1321+
// were selected, ErrNotFound is returned.
1322+
func (q *Query) MapScanContext(ctx context.Context, m map[string]interface{}) error {
1323+
iter := q.IterContext(ctx)
13011324
if err := iter.checkErrAndNotFound(); err != nil {
13021325
return err
13031326
}
@@ -1309,7 +1332,14 @@ func (q *Query) MapScan(m map[string]interface{}) error {
13091332
// row into the values pointed at by dest and discards the rest. If no rows
13101333
// were selected, ErrNotFound is returned.
13111334
func (q *Query) Scan(dest ...interface{}) error {
1312-
iter := q.Iter()
1335+
return q.ScanContext(nil, dest...)
1336+
}
1337+
1338+
// ScanContext executes the query with the provided context, copies the columns of the first selected
1339+
// row into the values pointed at by dest and discards the rest. If no rows
1340+
// were selected, ErrNotFound is returned.
1341+
func (q *Query) ScanContext(ctx context.Context, dest ...interface{}) error {
1342+
iter := q.IterContext(ctx)
13131343
if err := iter.checkErrAndNotFound(); err != nil {
13141344
return err
13151345
}
@@ -1326,8 +1356,20 @@ func (q *Query) Scan(dest ...interface{}) error {
13261356
// SELECT * FROM. So using ScanCAS with INSERT is inherently prone to
13271357
// column mismatching. Use MapScanCAS to capture them safely.
13281358
func (q *Query) ScanCAS(dest ...interface{}) (applied bool, err error) {
1359+
return q.ScanCASContext(nil, dest...)
1360+
}
1361+
1362+
// ScanCASContext executes a lightweight transaction (i.e. an UPDATE or INSERT
1363+
// statement containing an IF clause) with the provided context. If the transaction fails because
1364+
// the existing values did not match, the previous values will be stored
1365+
// in dest.
1366+
//
1367+
// As for INSERT .. IF NOT EXISTS, previous values will be returned as if
1368+
// SELECT * FROM. So using ScanCAS with INSERT is inherently prone to
1369+
// column mismatching. Use MapScanCAS to capture them safely.
1370+
func (q *Query) ScanCASContext(ctx context.Context, dest ...interface{}) (applied bool, err error) {
13291371
q.disableSkipMetadata = true
1330-
iter := q.Iter()
1372+
iter := q.IterContext(ctx)
13311373
if err := iter.checkErrAndNotFound(); err != nil {
13321374
return false, err
13331375
}
@@ -1349,8 +1391,20 @@ func (q *Query) ScanCAS(dest ...interface{}) (applied bool, err error) {
13491391
// SELECT * FROM. So using ScanCAS with INSERT is inherently prone to
13501392
// column mismatching. MapScanCAS is added to capture them safely.
13511393
func (q *Query) MapScanCAS(dest map[string]interface{}) (applied bool, err error) {
1394+
return q.MapScanCASContext(nil, dest)
1395+
}
1396+
1397+
// MapScanCASContext executes a lightweight transaction (i.e. an UPDATE or INSERT
1398+
// statement containing an IF clause) with the provided context. If the transaction fails because
1399+
// the existing values did not match, the previous values will be stored
1400+
// in dest map.
1401+
//
1402+
// As for INSERT .. IF NOT EXISTS, previous values will be returned as if
1403+
// SELECT * FROM. So using ScanCAS with INSERT is inherently prone to
1404+
// column mismatching. MapScanCAS is added to capture them safely.
1405+
func (q *Query) MapScanCASContext(ctx context.Context, dest map[string]interface{}) (applied bool, err error) {
13521406
q.disableSkipMetadata = true
1353-
iter := q.Iter()
1407+
iter := q.IterContext(ctx)
13541408
if err := iter.checkErrAndNotFound(); err != nil {
13551409
return false, err
13561410
}

0 commit comments

Comments
 (0)