Skip to content

Commit df7fa41

Browse files
authored
added ScanQueryCtx (#24)
1 parent 945d3c2 commit df7fa41

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

cassandra.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Cassandra interface {
1616
ExecuteQuery(string, ...interface{}) error
1717
ExecuteBatch(gocql.BatchType, []string, [][]interface{}) error
1818
ExecuteUnloggedBatch([]string, [][]interface{}) error
19+
ScanQueryCtx(ctx context.Context, queryString string, queryParams []interface{}, outParams ...interface{}) error
1920
ScanQuery(string, []interface{}, ...interface{}) error
2021
ScanCASQuery(string, []interface{}, ...interface{}) (bool, error)
2122
IterQuery(string, []interface{}, ...interface{}) func() (int, bool, error)
@@ -169,9 +170,9 @@ func (c *cassandra) ExecuteUnloggedBatch(queries []string, params [][]interface{
169170
return c.ExecuteBatch(gocql.UnloggedBatch, queries, params)
170171
}
171172

172-
// ScanQuery executes a provided SELECT query at the configured read consistency level.
173-
func (c *cassandra) ScanQuery(queryString string, queryParams []interface{}, outParams ...interface{}) error {
174-
if err := c.Query(c.rcl, queryString, queryParams...).Scan(outParams...); err != nil {
173+
// ScanQueryCtx executes a provided SELECT query at the configured read consistency level.
174+
func (c *cassandra) ScanQueryCtx(ctx context.Context, queryString string, queryParams []interface{}, outParams ...interface{}) error {
175+
if err := c.Query(c.rcl, queryString, queryParams...).WithContext(ctx).Scan(outParams...); err != nil {
175176
if err == gocql.ErrNotFound {
176177
return NotFound
177178
}
@@ -180,6 +181,11 @@ func (c *cassandra) ScanQuery(queryString string, queryParams []interface{}, out
180181
return nil
181182
}
182183

184+
// ScanQuery executes a provided SELECT query at the configured read consistency level.
185+
func (c *cassandra) ScanQuery(queryString string, queryParams []interface{}, outParams ...interface{}) error {
186+
return c.ScanQueryCtx(context.Background(), queryString, queryParams, outParams...)
187+
}
188+
183189
// ScanCASQuery executes a lightweight transaction (an UPDATE or INSERT statement containing an IF clause)
184190
// at the configured write consistency level.
185191
func (c *cassandra) ScanCASQuery(queryString string, queryParams []interface{}, outParams ...interface{}) (bool, error) {

cassandra_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,29 @@ func (s *CassandraSuite) TestScanQueryError(c *C) {
119119
c.Assert(err, NotNil)
120120
}
121121

122+
func (s *CassandraSuite) TestScanQueryCtxSuccess(c *C) {
123+
_ = s.cassandra.ExecuteQuery("insert into test (field) values (1)")
124+
var field int
125+
err := s.cassandra.ScanQueryCtx(context.Background(), "select * from test", []interface{}{}, &field)
126+
c.Assert(err, IsNil)
127+
c.Assert(field, Equals, 1)
128+
}
129+
130+
func (s *CassandraSuite) TestScanQueryCtxError(c *C) {
131+
var field int
132+
err := s.cassandra.ScanQueryCtx(context.Background(), "select * from test where field = 999", []interface{}{}, &field)
133+
c.Assert(err, Equals, NotFound)
134+
}
135+
136+
func (s *CassandraSuite) TestScanQueryCtxCanceled(c *C) {
137+
ctx, cancel := context.WithCancel(context.Background())
138+
cancel()
139+
140+
var field int
141+
err := s.cassandra.ScanQueryCtx(ctx, "select * from unknown", []interface{}{}, &field)
142+
c.Assert(err, NotNil)
143+
}
144+
122145
func (s *CassandraSuite) TestScanCASQuerySuccess(c *C) {
123146
var field int
124147
applied, err := s.cassandra.ScanCASQuery("insert into test (field) values (3) if not exists", []interface{}{}, &field)

testutils.go

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func (c *TestErrorCassandra) ExecuteUnloggedBatch(queries []string, params [][]i
3737
return fmt.Errorf("Error during ExecuteUnloggedBatch")
3838
}
3939

40+
func (c *TestErrorCassandra) ScanQueryCtx(_ context.Context, queryString string, queryParams []interface{}, outParams ...interface{}) error {
41+
return fmt.Errorf("Error during ScanQueryCtx")
42+
}
43+
4044
func (c *TestErrorCassandra) ScanQuery(queryString string, queryParams []interface{}, outParams ...interface{}) error {
4145
return fmt.Errorf("Error during ScanQuery")
4246
}

0 commit comments

Comments
 (0)