Skip to content

Commit ef8a295

Browse files
committed
Update examples for 2.0
1 parent a9bfe80 commit ef8a295

12 files changed

+646
-15
lines changed

example_batch_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func Example_batch() {
4949

5050
ctx := context.Background()
5151

52-
b := session.Batch(gocql.UnloggedBatch).WithContext(ctx)
52+
// Example 1: Basic batch usage with Entries
53+
b := session.Batch(gocql.UnloggedBatch)
5354
b.Entries = append(b.Entries, gocql.BatchEntry{
5455
Stmt: "INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)",
5556
Args: []interface{}{1, 2, "1.2"},
@@ -61,19 +62,22 @@ func Example_batch() {
6162
Idempotent: true,
6263
})
6364

64-
err = b.Exec()
65+
err = b.ExecContext(ctx)
6566
if err != nil {
6667
log.Fatal(err)
6768
}
6869

70+
// Example 2: Using the fluent Query() method
6971
err = b.Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 4, "1.4").
7072
Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 5, "1.5").
71-
Exec()
73+
ExecContext(ctx)
7274
if err != nil {
7375
log.Fatal(err)
7476
}
7577

76-
scanner := session.Query("SELECT pk, ck, description FROM example.batches").Iter().Scanner()
78+
// Verification: Display all inserted data
79+
fmt.Println("All inserted data:")
80+
scanner := session.Query("SELECT pk, ck, description FROM example.batches").IterContext(ctx).Scanner()
7781
for scanner.Next() {
7882
var pk, ck int32
7983
var description string
@@ -83,6 +87,12 @@ func Example_batch() {
8387
}
8488
fmt.Println(pk, ck, description)
8589
}
90+
91+
if err := scanner.Err(); err != nil {
92+
log.Fatal(err)
93+
}
94+
95+
// All inserted data:
8696
// 1 2 1.2
8797
// 1 3 1.3
8898
// 1 4 1.4

example_dynamic_columns_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func Example_dynamicColumns() {
5656
defer session.Close()
5757

5858
printQuery := func(ctx context.Context, session *gocql.Session, stmt string, values ...interface{}) error {
59-
iter := session.Query(stmt, values...).WithContext(ctx).Iter()
59+
iter := session.Query(stmt, values...).IterContext(ctx)
6060
fmt.Println(stmt)
6161
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ',
6262
0)

example_lwt_batch_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ func ExampleSession_MapExecuteBatchCAS() {
5050
ctx := context.Background()
5151

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

5858
err = session.Query("INSERT INTO example.my_lwt_batch_table (pk, ck, version, value) VALUES (?, ?, ?, ?)",
59-
"pk1", "ck2", 1, "A").WithContext(ctx).Exec()
59+
"pk1", "ck2", 1, "A").ExecContext(ctx)
6060
if err != nil {
6161
log.Fatal(err)
6262
}
@@ -91,7 +91,7 @@ func ExampleSession_MapExecuteBatchCAS() {
9191

9292
printState := func() {
9393
scanner := session.Query("SELECT ck, value FROM example.my_lwt_batch_table WHERE pk = ?", "pk1").
94-
WithContext(ctx).Iter().Scanner()
94+
IterContext(ctx).Scanner()
9595
for scanner.Next() {
9696
var ck, value string
9797
err = scanner.Scan(&ck, &value)

example_lwt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func ExampleQuery_MapScanCAS() {
5050
ctx := context.Background()
5151

5252
err = session.Query("INSERT INTO example.my_lwt_table (pk, version, value) VALUES (?, ?, ?)",
53-
1, 1, "a").WithContext(ctx).Exec()
53+
1, 1, "a").ExecContext(ctx)
5454
if err != nil {
5555
log.Fatal(err)
5656
}

example_marshaler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func Example_marshalerUnmarshaler() {
9595
patch: 3,
9696
}
9797
err = session.Query("INSERT INTO example.my_marshaler_table (pk, value) VALUES (?, ?)",
98-
1, value).WithContext(ctx).Exec()
98+
1, value).ExecContext(ctx)
9999
if err != nil {
100100
log.Fatal(err)
101101
}

example_setkeyspace_test.go

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package gocql_test
20+
21+
import (
22+
"context"
23+
"fmt"
24+
"log"
25+
26+
gocql "github.com/apache/cassandra-gocql-driver/v2"
27+
)
28+
29+
// Example_setKeyspace demonstrates the SetKeyspace method that allows
30+
// specifying keyspace per query, available with Protocol 5+ (Cassandra 4.0+).
31+
//
32+
// This example shows the complete keyspace precedence hierarchy:
33+
// 1. Keyspace in CQL query string (keyspace.table) - HIGHEST precedence
34+
// 2. SetKeyspace() method - MIDDLE precedence
35+
// 3. Default session keyspace - LOWEST precedence
36+
func Example_setKeyspace() {
37+
/* The example assumes the following CQL was used to setup the keyspaces:
38+
create keyspace example with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
39+
create keyspace example2 with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
40+
create keyspace example3 with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
41+
create table example.users(id int, name text, PRIMARY KEY(id));
42+
create table example2.users(id int, name text, PRIMARY KEY(id));
43+
create table example3.users(id int, name text, PRIMARY KEY(id));
44+
*/
45+
cluster := gocql.NewCluster("localhost:9042")
46+
cluster.ProtoVersion = 5 // SetKeyspace requires Protocol 5+, available in Cassandra 4.0+
47+
cluster.Keyspace = "example" // Set a default keyspace
48+
session, err := cluster.CreateSession()
49+
if err != nil {
50+
log.Fatal(err)
51+
}
52+
defer session.Close()
53+
54+
ctx := context.Background()
55+
56+
// Example 1: Keyspace Precedence Hierarchy Demonstration
57+
fmt.Println("Demonstrating complete keyspace precedence hierarchy:")
58+
fmt.Println("1. Keyspace in CQL (keyspace.table) - HIGHEST")
59+
fmt.Println("2. SetKeyspace() method - MIDDLE")
60+
fmt.Println("3. Default session keyspace - LOWEST")
61+
fmt.Println()
62+
63+
// Insert test data
64+
// Default keyspace (example) - lowest precedence
65+
err = session.Query("INSERT INTO users (id, name) VALUES (?, ?)").
66+
Bind(1, "Alice").
67+
ExecContext(ctx)
68+
if err != nil {
69+
log.Fatal(err)
70+
}
71+
72+
// SetKeyspace overrides default - middle precedence
73+
err = session.Query("INSERT INTO users (id, name) VALUES (?, ?)").
74+
SetKeyspace("example2").
75+
Bind(1, "Bob").
76+
ExecContext(ctx)
77+
if err != nil {
78+
log.Fatal(err)
79+
}
80+
81+
// Fully qualified table name - highest precedence
82+
err = session.Query("INSERT INTO example3.users (id, name) VALUES (?, ?)").
83+
Bind(1, "Charlie").
84+
ExecContext(ctx)
85+
if err != nil {
86+
log.Fatal(err)
87+
}
88+
89+
// Example 2: Fully qualified table names override SetKeyspace
90+
fmt.Println("Example 2: Fully qualified table names take precedence over SetKeyspace:")
91+
92+
// This query sets keyspace to "example2" via SetKeyspace, but the fully qualified
93+
// table name "example3.users" takes precedence - query will target example3
94+
err = session.Query("INSERT INTO example3.users (id, name) VALUES (?, ?)").
95+
SetKeyspace("example2"). // This is IGNORED because CQL has "example3.users"
96+
Bind(2, "Diana").
97+
ExecContext(ctx)
98+
if err != nil {
99+
log.Fatal(err)
100+
}
101+
fmt.Println("Inserted Diana into example3.users despite SetKeyspace(\"example2\")")
102+
103+
// Verify data went to example3, not example2
104+
var count int
105+
iter := session.Query("SELECT COUNT(*) FROM users").
106+
SetKeyspace("example2").
107+
IterContext(ctx)
108+
if iter.Scan(&count) {
109+
fmt.Printf("Count in example2: %d (only Bob)\n", count)
110+
}
111+
if err := iter.Close(); err != nil {
112+
log.Fatal(err)
113+
}
114+
115+
iter = session.Query("SELECT COUNT(*) FROM users").
116+
SetKeyspace("example3").
117+
IterContext(ctx)
118+
if iter.Scan(&count) {
119+
fmt.Printf("Count in example3: %d (Charlie and Diana)\n", count)
120+
}
121+
if err := iter.Close(); err != nil {
122+
log.Fatal(err)
123+
}
124+
125+
// Example 3: SetKeyspace overrides default keyspace
126+
fmt.Println("\nExample 3: SetKeyspace overrides default keyspace:")
127+
128+
// Query using default keyspace (no SetKeyspace)
129+
var id int
130+
var name string
131+
iter = session.Query("SELECT id, name FROM users WHERE id = ?", 1).
132+
IterContext(ctx) // Uses default keyspace "example"
133+
if iter.Scan(&id, &name) {
134+
fmt.Printf("Default keyspace (example): ID %d, Name %s\n", id, name)
135+
}
136+
if err := iter.Close(); err != nil {
137+
log.Fatal(err)
138+
}
139+
140+
// SetKeyspace overrides default
141+
iter = session.Query("SELECT id, name FROM users WHERE id = ?", 1).
142+
SetKeyspace("example2"). // Override default keyspace
143+
IterContext(ctx)
144+
if iter.Scan(&id, &name) {
145+
fmt.Printf("SetKeyspace override (example2): ID %d, Name %s\n", id, name)
146+
}
147+
if err := iter.Close(); err != nil {
148+
log.Fatal(err)
149+
}
150+
151+
// Example 4: Mixed query patterns in one workflow
152+
fmt.Println("\nExample 4: Using all precedence levels in one workflow:")
153+
154+
// Query from default keyspace
155+
iter = session.Query("SELECT name FROM users WHERE id = 1").IterContext(ctx)
156+
if iter.Scan(&name) {
157+
fmt.Printf("Default (example): %s\n", name)
158+
}
159+
iter.Close()
160+
161+
// Query using SetKeyspace
162+
iter = session.Query("SELECT name FROM users WHERE id = 1").
163+
SetKeyspace("example2").IterContext(ctx)
164+
if iter.Scan(&name) {
165+
fmt.Printf("SetKeyspace (example2): %s\n", name)
166+
}
167+
iter.Close()
168+
169+
// Query using fully qualified table name (ignores both default and SetKeyspace)
170+
iter = session.Query("SELECT name FROM example3.users WHERE id = 1").
171+
SetKeyspace("example2"). // This is ignored due to qualified table name
172+
IterContext(ctx)
173+
if iter.Scan(&name) {
174+
fmt.Printf("Qualified name (example3): %s\n", name)
175+
}
176+
iter.Close()
177+
178+
// Demonstrating complete keyspace precedence hierarchy:
179+
// 1. Keyspace in CQL (keyspace.table) - HIGHEST
180+
// 2. SetKeyspace() method - MIDDLE
181+
// 3. Default session keyspace - LOWEST
182+
//
183+
// Example 2: Fully qualified table names take precedence over SetKeyspace:
184+
// Inserted Diana into example3.users despite SetKeyspace("example2")
185+
// Count in example2: 1 (only Bob)
186+
// Count in example3: 2 (Charlie and Diana)
187+
//
188+
// Example 3: SetKeyspace overrides default keyspace:
189+
// Default keyspace (example): ID 1, Name Alice
190+
// SetKeyspace override (example2): ID 1, Name Bob
191+
//
192+
// Example 4: Using all precedence levels in one workflow:
193+
// Default (example): Alice
194+
// SetKeyspace (example2): Bob
195+
// Qualified name (example3): Charlie
196+
}

0 commit comments

Comments
 (0)