Skip to content

Commit 4ce6818

Browse files
committed
CASSGO-103: Expose GetSerialConsistency on Query and Batch
Return the serial consistency as *Consistency so callers can detect "not set" (nil) and avoid using the zero value ANY for serial operations, which is invalid and can cause panics. patch by tejaslodayadd; reviewed by millerjp, joao-r-reis for CASSGO-103.
1 parent f1e31a5 commit 4ce6818

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

session.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,15 @@ func (q *Query) SerialConsistency(cons Consistency) *Query {
12721272
return q
12731273
}
12741274

1275+
// GetSerialConsistency returns the currently configured serial consistency level
1276+
// for the query. If the serial consistency level has not been set, it returns nil.
1277+
func (q *Query) GetSerialConsistency() *Consistency {
1278+
if !q.serialCons.isSerial() {
1279+
return nil
1280+
}
1281+
return &q.serialCons
1282+
}
1283+
12751284
// PageState sets the paging state for the query to resume paging from a specific
12761285
// point in time. Setting this will disable to query paging for this query, and
12771286
// must be used for all subsequent pages.
@@ -2079,6 +2088,15 @@ func (b *Batch) SerialConsistency(cons Consistency) *Batch {
20792088
return b
20802089
}
20812090

2091+
// GetSerialConsistency returns the currently configured serial consistency level
2092+
// for the batch. If the serial consistency level has not been set, it returns nil.
2093+
func (b *Batch) GetSerialConsistency() *Consistency {
2094+
if !b.serialCons.isSerial() {
2095+
return nil
2096+
}
2097+
return &b.serialCons
2098+
}
2099+
20822100
// DefaultTimestamp will enable the with default timestamp flag on the query.
20832101
// If enable, this will replace the server side assigned
20842102
// timestamp as default timestamp. Note that a timestamp in the query itself

session_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ func TestQueryBasicAPI(t *testing.T) {
108108
t.Fatalf("expected Query.GetConsistency to return 'All', got '%s'", qry.GetConsistency())
109109
}
110110

111+
if sc := qry.GetSerialConsistency(); sc != nil {
112+
t.Fatalf("expected Query.GetSerialConsistency to return nil when not set, got '%s'", sc)
113+
}
114+
115+
qry.SerialConsistency(Serial)
116+
if sc := qry.GetSerialConsistency(); sc == nil || *sc != Serial {
117+
t.Fatalf("expected Query.GetSerialConsistency to return 'Serial', got '%v'", sc)
118+
}
119+
111120
trace := &traceWriter{}
112121
qry.Trace(trace)
113122
if qry.trace != trace {
@@ -194,6 +203,16 @@ func TestBatchBasicAPI(t *testing.T) {
194203
t.Fatalf("expected batch.GetConsistency() to return 'One', got '%s'", b.GetConsistency())
195204
}
196205

206+
// Test Serial Consistency
207+
if sc := b.GetSerialConsistency(); sc != nil {
208+
t.Fatalf("expected batch.GetSerialConsistency() to return nil when not set, got '%s'", sc)
209+
}
210+
211+
b.SerialConsistency(Serial)
212+
if sc := b.GetSerialConsistency(); sc == nil || *sc != Serial {
213+
t.Fatalf("expected batch.GetSerialConsistency() to return 'Serial', got '%v'", sc)
214+
}
215+
197216
trace := &traceWriter{}
198217
b.Trace(trace)
199218
if b.trace != trace {

0 commit comments

Comments
 (0)