Skip to content

Commit 7061478

Browse files
committed
* Refactored Statement classes and session.execute_async to make it possible to create new Statement classes (say in DSE) that the core driver can execute.
1 parent bed9ac8 commit 7061478

File tree

8 files changed

+42
-18
lines changed

8 files changed

+42
-18
lines changed

lib/cassandra/session.rb

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,13 @@ def execute_async(statement, options = nil)
8989

9090
case statement
9191
when ::String
92-
@client.query(Statements::Simple.new(statement,
93-
options.arguments,
94-
options.type_hints,
95-
options.idempotent?),
96-
options)
97-
when Statements::Simple
98-
@client.query(statement, options)
99-
when Statements::Prepared
100-
@client.execute(statement.bind(options.arguments), options)
101-
when Statements::Bound
102-
@client.execute(statement, options)
103-
when Statements::Batch
104-
Util.assert_not_empty(statement.statements) { 'batch cannot be empty' }
105-
@client.batch(statement, options)
92+
Statements::Simple.new(statement,
93+
options.arguments,
94+
options.type_hints,
95+
options.idempotent?).accept(@client,
96+
options)
97+
when Statement
98+
statement.accept(@client, options)
10699
else
107100
@futures.error(::ArgumentError.new("unsupported statement #{statement.inspect}"))
108101
end

lib/cassandra/statement.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,10 @@ module Statement
2323
def idempotent?
2424
!!@idempotent
2525
end
26+
27+
# @private
28+
def accept(client, options)
29+
raise NotImplementedError, "#{self.class} must implement :accept method"
30+
end
2631
end
2732
end

lib/cassandra/statements/batch.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ def add(statement, options = nil)
113113
self
114114
end
115115

116+
# @private
117+
def accept(client, options)
118+
Util.assert_not_empty(statements) { 'batch cannot be empty' }
119+
client.batch(self, options)
120+
end
121+
116122
# Determines whether or not the statement is safe to retry on timeout
117123
# Batches are idempotent only when all statements in a batch are.
118124
# @return [Boolean] whether the statement is safe to retry on timeout

lib/cassandra/statements/bound.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ def initialize(cql,
4646
@idempotent = idempotent
4747
end
4848

49+
# @private
50+
def accept(client, options)
51+
client.execute(self, options)
52+
end
53+
4954
# @return [String] a CLI-friendly bound statement representation
5055
def inspect
5156
"#<#{self.class.name}:0x#{object_id.to_s(16)} @cql=#{@cql.inspect} " \

lib/cassandra/statements/prepared.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ def execution_info
155155
nil)
156156
end
157157

158+
# @private
159+
def accept(client, options)
160+
client.execute(bind(options.arguments), options)
161+
end
162+
158163
# @return [String] a CLI-friendly prepared statement representation
159164
def inspect
160165
"#<#{self.class.name}:0x#{object_id.to_s(16)} @cql=#{@cql.inspect}>"

lib/cassandra/statements/simple.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ def initialize(cql, params = nil, type_hints = nil, idempotent = false)
9393
@idempotent = idempotent
9494
end
9595

96+
# @private
97+
def accept(client, options)
98+
client.query(self, options)
99+
end
100+
96101
# @return [String] a CLI-friendly simple statement representation
97102
def inspect
98103
"#<#{self.class.name}:0x#{object_id.to_s(16)} @cql=#{@cql.inspect} " \

lib/cassandra/statements/void.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ module Statements
2323
class Void
2424
include Statement
2525

26+
# @private
27+
def accept(client, options)
28+
nil
29+
end
30+
2631
# Returns nothing
2732
# @return [nil] there is no cql for the void statement
2833
def cql

spec/cassandra/session_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module Cassandra
3535
statement = double('simple statement')
3636

3737
expect(Statements::Simple).to receive(:new).once.with(cql, EMPTY_LIST, EMPTY_LIST, false).and_return(statement)
38-
expect(client).to receive(:query).once.with(statement, session_options).and_return(promise)
38+
expect(statement).to receive(:accept).once.with(client, session_options).and_return(promise)
3939
expect(session.execute_async(cql)).to eq(promise)
4040
end
4141
end
@@ -48,7 +48,7 @@ module Cassandra
4848
statement = double('simple statement')
4949

5050
expect(Statements::Simple).to receive(:new).once.with(cql, [1], [], false).and_return(statement)
51-
expect(client).to receive(:query).once.with(statement, session_options.override(arguments: [1])).and_return(promise)
51+
expect(statement).to receive(:accept).once.with(client, session_options.override(arguments: [1])).and_return(promise)
5252
expect(session.execute_async(cql, arguments: [1])).to eq(promise)
5353
end
5454
end
@@ -61,7 +61,7 @@ module Cassandra
6161
statement = double('simple statement')
6262

6363
expect(Statements::Simple).to receive(:new).once.with(cql, [1], [:int], false).and_return(statement)
64-
expect(client).to receive(:query).once.with(statement, session_options.override(arguments: [1], type_hints: [:int])).and_return(promise)
64+
expect(statement).to receive(:accept).once.with(client, session_options.override(arguments: [1], type_hints: [:int])).and_return(promise)
6565
expect(session.execute_async(cql, arguments: [1], type_hints: [:int])).to eq(promise)
6666
end
6767
end
@@ -75,7 +75,7 @@ module Cassandra
7575
statement = double('simple statement')
7676

7777
expect(Statements::Simple).to receive(:new).once.with(cql, EMPTY_LIST, EMPTY_LIST, false).and_return(statement)
78-
expect(client).to receive(:query).once.with(statement, session_options.override(options)).and_return(promise)
78+
expect(statement).to receive(:accept).once.with(client, session_options.override(options)).and_return(promise)
7979
expect(session.execute_async(cql, options)).to eq(promise)
8080
end
8181
end

0 commit comments

Comments
 (0)