Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add Query and Batch to ObservedQuery and ObservedBatch (CASSGO-73)
- Add way to create HostInfo objects for testing purposes (CASSGO-71)
- Add missing Context methods on Query and Batch (CASSGO-81)
- Update example and test code for 2.0 release (CASSGO-80)

### Changed

Expand Down
8 changes: 4 additions & 4 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ func TestBatch_WithNowInSeconds(t *testing.T) {
t.Fatal(err)
}

b := session.NewBatch(LoggedBatch)
b := session.Batch(LoggedBatch)
b.WithNowInSeconds(0)
b.Query("INSERT INTO batch_now_in_seconds (id, val) VALUES (?, ?) USING TTL 20", 1, "val")
if err := session.ExecuteBatch(b); err != nil {
if err := b.Exec(); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -140,10 +140,10 @@ func TestBatch_SetKeyspace(t *testing.T) {
ids := []int{1, 2}
texts := []string{"val1", "val2"}

b := session.NewBatch(LoggedBatch).SetKeyspace("gocql_keyspace_override_test")
b := session.Batch(LoggedBatch).SetKeyspace("gocql_keyspace_override_test")
b.Query("INSERT INTO batch_keyspace(id, value) VALUES (?, ?)", ids[0], texts[0])
b.Query("INSERT INTO batch_keyspace(id, value) VALUES (?, ?)", ids[1], texts[1])
err = session.ExecuteBatch(b)
err = b.Exec()
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 4 additions & 4 deletions cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3899,15 +3899,15 @@ func TestStmtCacheUsesOverriddenKeyspace(t *testing.T) {

// Inserting data via Batch to ensure that batches
// properly accounts for keyspace overriding
b1 := session.NewBatch(LoggedBatch)
b1 := session.Batch(LoggedBatch)
b1.Query(insertQuery, 1)
err = session.ExecuteBatch(b1)
err = b1.Exec()
require.NoError(t, err)

b2 := session.NewBatch(LoggedBatch)
b2 := session.Batch(LoggedBatch)
b2.SetKeyspace("gocql_test_stmt_cache")
b2.Query(insertQuery, 2)
err = session.ExecuteBatch(b2)
err = b2.Exec()
require.NoError(t, err)

var scannedID int
Expand Down
39 changes: 31 additions & 8 deletions example_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,46 @@ func Example_batch() {

ctx := context.Background()

b := session.Batch(gocql.UnloggedBatch).WithContext(ctx)
// Example 1: Simple batch using the Query() method - recommended approach
batch := session.Batch(gocql.LoggedBatch)
batch.Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 2, "1.2")
batch.Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 3, "1.3")

err = batch.ExecContext(ctx)
if err != nil {
log.Fatal(err)
}

// Example 2: Advanced batch usage with Entries for more control
b := session.Batch(gocql.UnloggedBatch)
b.Entries = append(b.Entries, gocql.BatchEntry{
Stmt: "INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)",
Args: []interface{}{1, 2, "1.2"},
Args: []interface{}{1, 4, "1.4"},
Idempotent: true,
})
b.Entries = append(b.Entries, gocql.BatchEntry{
Stmt: "INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)",
Args: []interface{}{1, 3, "1.3"},
Args: []interface{}{1, 5, "1.5"},
Idempotent: true,
})

err = b.Exec()
err = b.ExecContext(ctx)
if err != nil {
log.Fatal(err)
}

err = b.Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 4, "1.4").
Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 5, "1.5").
Exec()
// Example 3: Fluent style chaining
err = session.Batch(gocql.LoggedBatch).
Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 6, "1.6").
Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 7, "1.7").
ExecContext(ctx)
if err != nil {
log.Fatal(err)
}

scanner := session.Query("SELECT pk, ck, description FROM example.batches").Iter().Scanner()
// Verification: Display all inserted data
fmt.Println("All inserted data:")
scanner := session.Query("SELECT pk, ck, description FROM example.batches").IterContext(ctx).Scanner()
for scanner.Next() {
var pk, ck int32
var description string
Expand All @@ -83,8 +98,16 @@ func Example_batch() {
}
fmt.Println(pk, ck, description)
}

if err := scanner.Err(); err != nil {
log.Fatal(err)
}

// All inserted data:
// 1 2 1.2
// 1 3 1.3
// 1 4 1.4
// 1 5 1.5
// 1 6 1.6
// 1 7 1.7
}
2 changes: 1 addition & 1 deletion example_dynamic_columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Example_dynamicColumns() {
defer session.Close()

printQuery := func(ctx context.Context, session *gocql.Session, stmt string, values ...interface{}) error {
iter := session.Query(stmt, values...).WithContext(ctx).Iter()
iter := session.Query(stmt, values...).IterContext(ctx)
fmt.Println(stmt)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ',
0)
Expand Down
12 changes: 6 additions & 6 deletions example_lwt_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
gocql "github.com/apache/cassandra-gocql-driver/v2"
)

// ExampleSession_MapExecuteBatchCAS demonstrates how to execute a batch lightweight transaction.
func ExampleSession_MapExecuteBatchCAS() {
// ExampleBatch_MapExecCAS demonstrates how to execute a batch lightweight transaction.
func ExampleBatch_MapExecCAS() {
/* The example assumes the following CQL was used to setup the keyspace:
create keyspace example with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
create table example.my_lwt_batch_table(pk text, ck text, version int, value text, PRIMARY KEY(pk, ck));
Expand All @@ -50,13 +50,13 @@ func ExampleSession_MapExecuteBatchCAS() {
ctx := context.Background()

err = session.Query("INSERT INTO example.my_lwt_batch_table (pk, ck, version, value) VALUES (?, ?, ?, ?)",
"pk1", "ck1", 1, "a").WithContext(ctx).Exec()
"pk1", "ck1", 1, "a").ExecContext(ctx)
if err != nil {
log.Fatal(err)
}

err = session.Query("INSERT INTO example.my_lwt_batch_table (pk, ck, version, value) VALUES (?, ?, ?, ?)",
"pk1", "ck2", 1, "A").WithContext(ctx).Exec()
"pk1", "ck2", 1, "A").ExecContext(ctx)
if err != nil {
log.Fatal(err)
}
Expand All @@ -72,7 +72,7 @@ func ExampleSession_MapExecuteBatchCAS() {
Args: []interface{}{"B", "pk1", "ck2", ck2Version},
})
m := make(map[string]interface{})
applied, iter, err := b.WithContext(ctx).MapExecCAS(m)
applied, iter, err := b.MapExecCASContext(ctx, m)
if err != nil {
log.Fatal(err)
}
Expand All @@ -91,7 +91,7 @@ func ExampleSession_MapExecuteBatchCAS() {

printState := func() {
scanner := session.Query("SELECT ck, value FROM example.my_lwt_batch_table WHERE pk = ?", "pk1").
WithContext(ctx).Iter().Scanner()
IterContext(ctx).Scanner()
for scanner.Next() {
var ck, value string
err = scanner.Scan(&ck, &value)
Expand Down
14 changes: 7 additions & 7 deletions example_lwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,37 @@ func ExampleQuery_MapScanCAS() {
ctx := context.Background()

err = session.Query("INSERT INTO example.my_lwt_table (pk, version, value) VALUES (?, ?, ?)",
1, 1, "a").WithContext(ctx).Exec()
1, 1, "a").ExecContext(ctx)
if err != nil {
log.Fatal(err)
}
m := make(map[string]interface{})
applied, err := session.Query("UPDATE example.my_lwt_table SET value = ? WHERE pk = ? IF version = ?",
"b", 1, 0).WithContext(ctx).MapScanCAS(m)
"b", 1, 0).MapScanCASContext(ctx, m)
if err != nil {
log.Fatal(err)
}
fmt.Println(applied, m)

var value string
err = session.Query("SELECT value FROM example.my_lwt_table WHERE pk = ?", 1).WithContext(ctx).
Scan(&value)
err = session.Query("SELECT value FROM example.my_lwt_table WHERE pk = ?", 1).
ScanContext(ctx, &value)
if err != nil {
log.Fatal(err)
}
fmt.Println(value)

m = make(map[string]interface{})
applied, err = session.Query("UPDATE example.my_lwt_table SET value = ? WHERE pk = ? IF version = ?",
"b", 1, 1).WithContext(ctx).MapScanCAS(m)
"b", 1, 1).MapScanCASContext(ctx, m)
if err != nil {
log.Fatal(err)
}
fmt.Println(applied, m)

var value2 string
err = session.Query("SELECT value FROM example.my_lwt_table WHERE pk = ?", 1).WithContext(ctx).
Scan(&value2)
err = session.Query("SELECT value FROM example.my_lwt_table WHERE pk = ?", 1).
ScanContext(ctx, &value2)
if err != nil {
log.Fatal(err)
}
Expand Down
10 changes: 5 additions & 5 deletions example_marshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,20 @@ func Example_marshalerUnmarshaler() {
patch: 3,
}
err = session.Query("INSERT INTO example.my_marshaler_table (pk, value) VALUES (?, ?)",
1, value).WithContext(ctx).Exec()
1, value).ExecContext(ctx)
if err != nil {
log.Fatal(err)
}
var stringValue string
err = session.Query("SELECT value FROM example.my_marshaler_table WHERE pk = 1").WithContext(ctx).
Scan(&stringValue)
err = session.Query("SELECT value FROM example.my_marshaler_table WHERE pk = 1").
ScanContext(ctx, &stringValue)
if err != nil {
log.Fatal(err)
}
fmt.Println(stringValue)
var unmarshaledValue MyMarshaler
err = session.Query("SELECT value FROM example.my_marshaler_table WHERE pk = 1").WithContext(ctx).
Scan(&unmarshaledValue)
err = session.Query("SELECT value FROM example.my_marshaler_table WHERE pk = 1").
ScanContext(ctx, &unmarshaledValue)
if err != nil {
log.Fatal(err)
}
Expand Down
Loading