This is an automated email from the ASF dual-hosted git repository.

joaoreis pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/cassandra-gocql-driver.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 0094af0a Deprecate Session.ExecuteBatch() and move "execute batch" 
methods to Batch type
0094af0a is described below

commit 0094af0afacc90f5a853dc6c75c211137b149b80
Author: tengu-alt <olexandr.luzh...@gmail.com>
AuthorDate: Thu Apr 24 14:32:24 2025 +0300

    Deprecate Session.ExecuteBatch() and move "execute batch" methods to Batch 
type
    
    Session.ExecuteBatch() was deprecated and similar 
methods(MapExecuteBatchCAS,ExecuteBatchCAS)
    were deprecated and moved to Batch type
    
    patch by Oleksandr Luzhniy; reviewed by João Reis, Bohdan Siryk, for 
CASSGO-57
---
 CHANGELOG.md              |  2 ++
 cassandra_test.go         | 38 +++++++++++++++++++-------------------
 doc.go                    | 12 ++++++------
 example_batch_test.go     |  2 +-
 example_lwt_batch_test.go |  2 +-
 integration_test.go       |  2 +-
 session.go                | 26 ++++++++++++++++++++++----
 session_test.go           |  4 ++--
 8 files changed, 54 insertions(+), 34 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index ffaa6629..8de4745d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -52,6 +52,8 @@ and this project adheres to [Semantic 
Versioning](https://semver.org/spec/v2.0.0
 
 - Refactoring hostpool package test and Expose HostInfo creation (CASSGO-59)
 
+- Move "execute batch" methods to Batch type (CASSGO-57)
+
 ### Fixed
 - Cassandra version unmarshal fix (CASSGO-49)
 
diff --git a/cassandra_test.go b/cassandra_test.go
index 46909902..5a8e1ff4 100644
--- a/cassandra_test.go
+++ b/cassandra_test.go
@@ -457,7 +457,7 @@ func TestCAS(t *testing.T) {
 
        successBatch := session.Batch(LoggedBatch)
        successBatch.Query("INSERT INTO cas_table (title, revid, last_modified) 
VALUES (?, ?, ?) IF NOT EXISTS", title, revid, modified)
-       if applied, _, err := session.ExecuteBatchCAS(successBatch, &titleCAS, 
&revidCAS, &modifiedCAS); err != nil {
+       if applied, _, err := successBatch.ExecCAS(&titleCAS, &revidCAS, 
&modifiedCAS); err != nil {
                t.Fatal("insert:", err)
        } else if !applied {
                t.Fatalf("insert should have been applied: title=%v revID=%v 
modified=%v", titleCAS, revidCAS, modifiedCAS)
@@ -466,7 +466,7 @@ func TestCAS(t *testing.T) {
        successBatch = session.Batch(LoggedBatch)
        successBatch.Query("INSERT INTO cas_table (title, revid, last_modified) 
VALUES (?, ?, ?) IF NOT EXISTS", title+"_foo", revid, modified)
        casMap := make(map[string]interface{})
-       if applied, _, err := session.MapExecuteBatchCAS(successBatch, casMap); 
err != nil {
+       if applied, _, err := successBatch.MapExecCAS(casMap); err != nil {
                t.Fatal("insert:", err)
        } else if !applied {
                t.Fatal("insert should have been applied")
@@ -474,7 +474,7 @@ func TestCAS(t *testing.T) {
 
        failBatch := session.Batch(LoggedBatch)
        failBatch.Query("INSERT INTO cas_table (title, revid, last_modified) 
VALUES (?, ?, ?) IF NOT EXISTS", title, revid, modified)
-       if applied, _, err := session.ExecuteBatchCAS(successBatch, &titleCAS, 
&revidCAS, &modifiedCAS); err != nil {
+       if applied, _, err := successBatch.ExecCAS(&titleCAS, &revidCAS, 
&modifiedCAS); err != nil {
                t.Fatal("insert:", err)
        } else if applied {
                t.Fatalf("insert should have not been applied: title=%v 
revID=%v modified=%v", titleCAS, revidCAS, modifiedCAS)
@@ -483,14 +483,14 @@ func TestCAS(t *testing.T) {
        insertBatch := session.Batch(LoggedBatch)
        insertBatch.Query("INSERT INTO cas_table (title, revid, last_modified) 
VALUES ('_foo', 2c3af400-73a4-11e5-9381-29463d90c3f0, TOTIMESTAMP(NOW()))")
        insertBatch.Query("INSERT INTO cas_table (title, revid, last_modified) 
VALUES ('_foo', 3e4ad2f1-73a4-11e5-9381-29463d90c3f0, TOTIMESTAMP(NOW()))")
-       if err := session.ExecuteBatch(insertBatch); err != nil {
+       if err := insertBatch.Exec(); err != nil {
                t.Fatal("insert:", err)
        }
 
        failBatch = session.Batch(LoggedBatch)
        failBatch.Query("UPDATE cas_table SET last_modified = 
TOTIMESTAMP(NOW()) WHERE title='_foo' AND 
revid=2c3af400-73a4-11e5-9381-29463d90c3f0 IF 
last_modified=TOTIMESTAMP(NOW());")
        failBatch.Query("UPDATE cas_table SET last_modified = 
TOTIMESTAMP(NOW()) WHERE title='_foo' AND 
revid=3e4ad2f1-73a4-11e5-9381-29463d90c3f0 IF 
last_modified=TOTIMESTAMP(NOW());")
-       if applied, iter, err := session.ExecuteBatchCAS(failBatch, &titleCAS, 
&revidCAS, &modifiedCAS); err != nil {
+       if applied, iter, err := failBatch.ExecCAS(&titleCAS, &revidCAS, 
&modifiedCAS); err != nil {
                t.Fatal("insert:", err)
        } else if applied {
                t.Fatalf("insert should have not been applied: title=%v 
revID=%v modified=%v", titleCAS, revidCAS, modifiedCAS)
@@ -521,20 +521,20 @@ func TestCAS(t *testing.T) {
        notCASBatch := session.Batch(LoggedBatch)
        notCASBatch.Query("INSERT INTO cas_table (title, revid, last_modified) 
VALUES (?, ?, ?)", title+"_baz", revid, modified)
        casMap = make(map[string]interface{})
-       if _, _, err := session.MapExecuteBatchCAS(notCASBatch, casMap); err != 
ErrNotFound {
+       if _, _, err := notCASBatch.MapExecCAS(casMap); err != ErrNotFound {
                t.Fatal("insert should have returned not found:", err)
        }
 
        notCASBatch = session.Batch(LoggedBatch)
        notCASBatch.Query("INSERT INTO cas_table (title, revid, last_modified) 
VALUES (?, ?, ?)", title+"_baz", revid, modified)
        casMap = make(map[string]interface{})
-       if _, _, err := session.ExecuteBatchCAS(notCASBatch, &revidCAS); err != 
ErrNotFound {
+       if _, _, err := notCASBatch.ExecCAS(&revidCAS); err != ErrNotFound {
                t.Fatal("insert should have returned not found:", err)
        }
 
        failBatch = session.Batch(LoggedBatch)
        failBatch.Query("UPDATE cas_table SET last_modified = 
TOTIMESTAMP(NOW()) WHERE title='_foo' AND 
revid=3e4ad2f1-73a4-11e5-9381-29463d90c3f0 IF last_modified = ?", modified)
-       if _, _, err := session.ExecuteBatchCAS(failBatch, new(bool)); err == 
nil {
+       if _, _, err := failBatch.ExecCAS(new(bool)); err == nil {
                t.Fatal("update should have errored")
        }
        // make sure MapScanCAS does not panic when MapScan fails
@@ -550,7 +550,7 @@ func TestCAS(t *testing.T) {
        failBatch.Query("UPDATE cas_table SET last_modified = 
TOTIMESTAMP(NOW()) WHERE title='_foo' AND 
revid=3e4ad2f1-73a4-11e5-9381-29463d90c3f0 IF last_modified = ?", modified)
        casMap = make(map[string]interface{})
        casMap["last_modified"] = false
-       if _, _, err := session.MapExecuteBatchCAS(failBatch, casMap); err == 
nil {
+       if _, _, err := failBatch.MapExecCAS(casMap); err == nil {
                t.Fatal("update should have errored")
        }
 }
@@ -752,7 +752,7 @@ func TestBatch(t *testing.T) {
                batch.Query(`INSERT INTO batch_table (id) VALUES (?)`, i)
        }
 
-       if err := session.ExecuteBatch(batch); err != nil {
+       if err := batch.Exec(); err != nil {
                t.Fatal("execute batch:", err)
        }
 
@@ -788,7 +788,7 @@ func TestUnpreparedBatch(t *testing.T) {
                batch.Query(`UPDATE batch_unprepared SET c = c + 1 WHERE id = 
1`)
        }
 
-       if err := session.ExecuteBatch(batch); err != nil {
+       if err := batch.Exec(); err != nil {
                t.Fatal("execute batch:", err)
        }
 
@@ -824,8 +824,8 @@ func TestBatchLimit(t *testing.T) {
        for i := 0; i < 65537; i++ {
                batch.Query(`INSERT INTO batch_table2 (id) VALUES (?)`, i)
        }
-       if err := session.ExecuteBatch(batch); err != ErrTooManyStmts {
-               t.Fatal("gocql attempted to execute a batch larger than the 
support limit of statements.")
+       if err := batch.Exec(); err != ErrTooManyStmts {
+               t.Fatalf("gocql attempted to execute a batch larger than the 
support limit of statements: expected %v, got %v", ErrTooManyStmts, err)
        }
 
 }
@@ -876,7 +876,7 @@ func TestTooManyQueryArgs(t *testing.T) {
 
        batch := session.Batch(UnloggedBatch)
        batch.Query("INSERT INTO too_many_query_args (id, value) VALUES (?, 
?)", 1, 2, 3)
-       err = session.ExecuteBatch(batch)
+       err = batch.Exec()
 
        if err == nil {
                t.Fatal("'`INSERT INTO too_many_query_args (id, value) VALUES 
(?, ?)`, 1, 2, 3' should return an error")
@@ -908,7 +908,7 @@ func TestNotEnoughQueryArgs(t *testing.T) {
 
        batch := session.Batch(UnloggedBatch)
        batch.Query("INSERT INTO not_enough_query_args (id, cluster, value) 
VALUES (?, ?, ?)", 1, 2)
-       err = session.ExecuteBatch(batch)
+       err = batch.Exec()
 
        if err == nil {
                t.Fatal("'`INSERT INTO not_enough_query_args (id, cluster, 
value) VALUES (?, ?, ?)`, 1, 2' should return an error")
@@ -1534,7 +1534,7 @@ func TestBatchQueryInfo(t *testing.T) {
        batch := session.Batch(LoggedBatch)
        batch.Bind("INSERT INTO batch_query_info (id, cluster, value) VALUES 
(?, ?,?)", write)
 
-       if err := session.ExecuteBatch(batch); err != nil {
+       if err := batch.Exec(); err != nil {
                t.Fatalf("batch insert into batch_query_info failed, err '%v'", 
err)
        }
 
@@ -2047,7 +2047,7 @@ func TestBatchStats(t *testing.T) {
        b.Query("INSERT INTO batchStats (id) VALUES (?)", 1)
        b.Query("INSERT INTO batchStats (id) VALUES (?)", 2)
 
-       if err := session.ExecuteBatch(b); err != nil {
+       if err := b.Exec(); err != nil {
                t.Fatalf("query failed. %v", err)
        } else {
                if b.Attempts() < 1 {
@@ -2104,7 +2104,7 @@ func TestBatchObserve(t *testing.T) {
                batch.Query(fmt.Sprintf(`INSERT INTO batch_observe_table 
(id,other) VALUES (?,%d)`, i), i)
        }
 
-       if err := session.ExecuteBatch(batch); err != nil {
+       if err := batch.Exec(); err != nil {
                t.Fatal("execute batch:", err)
        }
        if observedBatch == nil {
@@ -3408,7 +3408,7 @@ func TestUnsetColBatch(t *testing.T) {
        b.Query("INSERT INTO gocql_test.batchUnsetInsert(id, my_int, my_text) 
VALUES (?,?,?)", 1, UnsetValue, "")
        b.Query("INSERT INTO gocql_test.batchUnsetInsert(id, my_int, my_text) 
VALUES (?,?,?)", 2, 2, UnsetValue)
 
-       if err := session.ExecuteBatch(b); err != nil {
+       if err := b.Exec(); err != nil {
                t.Fatalf("query failed. %v", err)
        } else {
                if b.Attempts() < 1 {
diff --git a/doc.go b/doc.go
index 109a8503..5685625b 100644
--- a/doc.go
+++ b/doc.go
@@ -311,7 +311,7 @@
 //
 // The CQL protocol supports sending batches of DML statements 
(INSERT/UPDATE/DELETE) and so does gocql.
 // Use Session.Batch to create a new batch and then fill-in details of 
individual queries.
-// Then execute the batch with Session.ExecuteBatch.
+// Then execute the batch with Batch.Exec.
 //
 // Logged batches ensure atomicity, either all or none of the operations in 
the batch will succeed, but they have
 // overhead to ensure this property.
@@ -329,8 +329,8 @@
 // It is also possible to pass entire BEGIN BATCH .. APPLY BATCH statement to 
Query.Exec.
 // There are differences how those are executed.
 // BEGIN BATCH statement passed to Query.Exec is prepared as a whole in a 
single statement.
-// Session.ExecuteBatch prepares individual statements in the batch.
-// If you have variable-length batches using the same statement, using 
Session.ExecuteBatch is more efficient.
+// Batch.Exec prepares individual statements in the batch.
+// If you have variable-length batches using the same statement, using 
Batch.Exec is more efficient.
 //
 // See Example_batch for an example.
 //
@@ -340,9 +340,9 @@
 // INSERT/UPDATE .. IF statement) and reading its result. See example for 
Query.MapScanCAS.
 //
 // Multiple-statement lightweight transactions can be executed as a logged 
batch that contains at least one conditional
-// statement. All the conditions must return true for the batch to be applied. 
You can use Session.ExecuteBatchCAS and
-// Session.MapExecuteBatchCAS when executing the batch to learn about the 
result of the LWT. See example for
-// Session.MapExecuteBatchCAS.
+// statement. All the conditions must return true for the batch to be applied. 
You can use Batch.ExecCAS and
+// Batch.MapExecCAS when executing the batch to learn about the result of the 
LWT. See example for
+// Batch.MapExecCAS.
 //
 // # Retries and speculative execution
 //
diff --git a/example_batch_test.go b/example_batch_test.go
index b27085cc..b5de6be4 100644
--- a/example_batch_test.go
+++ b/example_batch_test.go
@@ -61,7 +61,7 @@ func Example_batch() {
                Idempotent: true,
        })
 
-       err = session.ExecuteBatch(b)
+       err = b.Exec()
        if err != nil {
                log.Fatal(err)
        }
diff --git a/example_lwt_batch_test.go b/example_lwt_batch_test.go
index c3cc8383..8f6399a9 100644
--- a/example_lwt_batch_test.go
+++ b/example_lwt_batch_test.go
@@ -72,7 +72,7 @@ func ExampleSession_MapExecuteBatchCAS() {
                        Args: []interface{}{"B", "pk1", "ck2", ck2Version},
                })
                m := make(map[string]interface{})
-               applied, iter, err := 
session.MapExecuteBatchCAS(b.WithContext(ctx), m)
+               applied, iter, err := b.WithContext(ctx).MapExecCAS(m)
                if err != nil {
                        log.Fatal(err)
                }
diff --git a/integration_test.go b/integration_test.go
index 61ffbf50..ccc5939d 100644
--- a/integration_test.go
+++ b/integration_test.go
@@ -221,7 +221,7 @@ func TestCustomPayloadMessages(t *testing.T) {
        b := session.Batch(LoggedBatch)
        b.CustomPayload = customPayload
        b.Query("INSERT INTO testCustomPayloadMessages(id,value) VALUES(1, 1)")
-       if err := session.ExecuteBatch(b); err != nil {
+       if err := b.Exec(); err != nil {
                t.Fatalf("query failed. %v", err)
        }
 }
diff --git a/session.go b/session.go
index ed1a078d..df626c28 100644
--- a/session.go
+++ b/session.go
@@ -775,6 +775,7 @@ func (s *Session) executeBatch(batch *Batch) *Iter {
        return iter
 }
 
+// Deprecated: use Batch.Exec instead.
 // ExecuteBatch executes a batch operation and returns nil if successful
 // otherwise an error is returned describing the failure.
 func (s *Session) ExecuteBatch(batch *Batch) error {
@@ -782,13 +783,23 @@ func (s *Session) ExecuteBatch(batch *Batch) error {
        return iter.Close()
 }
 
+// Deprecated: use Batch.ExecCAS instead
 // ExecuteBatchCAS executes a batch operation and returns true if successful 
and
 // an iterator (to scan additional rows if more than one conditional statement)
 // was sent.
 // Further scans on the interator must also remember to include
 // the applied boolean as the first argument to *Iter.Scan
 func (s *Session) ExecuteBatchCAS(batch *Batch, dest ...interface{}) (applied 
bool, iter *Iter, err error) {
-       iter = s.executeBatch(batch)
+       return batch.ExecCAS(dest...)
+}
+
+// ExecCAS executes a batch operation and returns true if successful and
+// an iterator (to scan additional rows if more than one conditional statement)
+// was sent.
+// Further scans on the interator must also remember to include
+// the applied boolean as the first argument to *Iter.Scan
+func (b *Batch) ExecCAS(dest ...interface{}) (applied bool, iter *Iter, err 
error) {
+       iter = b.session.executeBatch(b)
        if err := iter.checkErrAndNotFound(); err != nil {
                iter.Close()
                return false, nil, err
@@ -804,11 +815,19 @@ func (s *Session) ExecuteBatchCAS(batch *Batch, dest 
...interface{}) (applied bo
        return applied, iter, iter.err
 }
 
+// Deprecated: use Batch.MapExecCAS instead
 // MapExecuteBatchCAS executes a batch operation much like ExecuteBatchCAS,
 // however it accepts a map rather than a list of arguments for the initial
 // scan.
 func (s *Session) MapExecuteBatchCAS(batch *Batch, dest 
map[string]interface{}) (applied bool, iter *Iter, err error) {
-       iter = s.executeBatch(batch)
+       return batch.MapExecCAS(dest)
+}
+
+// MapExecCAS executes a batch operation much like ExecuteBatchCAS,
+// however it accepts a map rather than a list of arguments for the initial
+// scan.
+func (b *Batch) MapExecCAS(dest map[string]interface{}) (applied bool, iter 
*Iter, err error) {
+       iter = b.session.executeBatch(b)
        if err := iter.checkErrAndNotFound(); err != nil {
                iter.Close()
                return false, nil, err
@@ -1828,9 +1847,8 @@ type Batch struct {
        routingInfo *queryRoutingInfo
 }
 
+// Deprecated: use Session.Batch instead
 // NewBatch creates a new batch operation using defaults defined in the cluster
-//
-// Deprecated: use session.Batch instead
 func (s *Session) NewBatch(typ BatchType) *Batch {
        return s.Batch(typ)
 }
diff --git a/session_test.go b/session_test.go
index 8633f995..25950270 100644
--- a/session_test.go
+++ b/session_test.go
@@ -98,7 +98,7 @@ func TestSessionAPI(t *testing.T) {
 
        testBatch := s.Batch(LoggedBatch)
        testBatch.Query("test")
-       err := s.ExecuteBatch(testBatch)
+       err := testBatch.Exec()
 
        if err != ErrNoConnections {
                t.Fatalf("expected session.ExecuteBatch to return '%v', got 
'%v'", ErrNoConnections, err)
@@ -111,7 +111,7 @@ func TestSessionAPI(t *testing.T) {
        //Should just return cleanly
        s.Close()
 
-       err = s.ExecuteBatch(testBatch)
+       err = testBatch.Exec()
        if err != ErrSessionClosed {
                t.Fatalf("expected session.ExecuteBatch to return '%v', got 
'%v'", ErrSessionClosed, err)
        }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org
For additional commands, e-mail: commits-h...@cassandra.apache.org

Reply via email to