cassandra-driver 1.0.0.beta.2-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.yardopts +4 -0
- data/README.md +125 -0
- data/lib/cassandra/auth/providers/password.rb +73 -0
- data/lib/cassandra/auth/providers.rb +16 -0
- data/lib/cassandra/auth.rb +97 -0
- data/lib/cassandra/client/batch.rb +212 -0
- data/lib/cassandra/client/client.rb +591 -0
- data/lib/cassandra/client/column_metadata.rb +54 -0
- data/lib/cassandra/client/connection_manager.rb +72 -0
- data/lib/cassandra/client/connector.rb +277 -0
- data/lib/cassandra/client/execute_options_decoder.rb +59 -0
- data/lib/cassandra/client/null_logger.rb +37 -0
- data/lib/cassandra/client/peer_discovery.rb +50 -0
- data/lib/cassandra/client/prepared_statement.rb +314 -0
- data/lib/cassandra/client/query_result.rb +230 -0
- data/lib/cassandra/client/request_runner.rb +71 -0
- data/lib/cassandra/client/result_metadata.rb +48 -0
- data/lib/cassandra/client/void_result.rb +78 -0
- data/lib/cassandra/client.rb +144 -0
- data/lib/cassandra/cluster/client.rb +768 -0
- data/lib/cassandra/cluster/connector.rb +244 -0
- data/lib/cassandra/cluster/control_connection.rb +425 -0
- data/lib/cassandra/cluster/metadata.rb +124 -0
- data/lib/cassandra/cluster/options.rb +42 -0
- data/lib/cassandra/cluster/registry.rb +198 -0
- data/lib/cassandra/cluster/schema/partitioners/murmur3.rb +47 -0
- data/lib/cassandra/cluster/schema/partitioners/ordered.rb +37 -0
- data/lib/cassandra/cluster/schema/partitioners/random.rb +37 -0
- data/lib/cassandra/cluster/schema/partitioners.rb +21 -0
- data/lib/cassandra/cluster/schema/replication_strategies/network_topology.rb +92 -0
- data/lib/cassandra/cluster/schema/replication_strategies/none.rb +39 -0
- data/lib/cassandra/cluster/schema/replication_strategies/simple.rb +44 -0
- data/lib/cassandra/cluster/schema/replication_strategies.rb +21 -0
- data/lib/cassandra/cluster/schema/type_parser.rb +138 -0
- data/lib/cassandra/cluster/schema.rb +340 -0
- data/lib/cassandra/cluster.rb +215 -0
- data/lib/cassandra/column.rb +92 -0
- data/lib/cassandra/compression/compressors/lz4.rb +72 -0
- data/lib/cassandra/compression/compressors/snappy.rb +66 -0
- data/lib/cassandra/compression.rb +66 -0
- data/lib/cassandra/driver.rb +111 -0
- data/lib/cassandra/errors.rb +79 -0
- data/lib/cassandra/execution/info.rb +51 -0
- data/lib/cassandra/execution/options.rb +80 -0
- data/lib/cassandra/execution/trace.rb +152 -0
- data/lib/cassandra/future.rb +675 -0
- data/lib/cassandra/host.rb +79 -0
- data/lib/cassandra/keyspace.rb +133 -0
- data/lib/cassandra/listener.rb +87 -0
- data/lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb +149 -0
- data/lib/cassandra/load_balancing/policies/round_robin.rb +132 -0
- data/lib/cassandra/load_balancing/policies/token_aware.rb +119 -0
- data/lib/cassandra/load_balancing/policies/white_list.rb +90 -0
- data/lib/cassandra/load_balancing/policies.rb +19 -0
- data/lib/cassandra/load_balancing.rb +113 -0
- data/lib/cassandra/protocol/cql_byte_buffer.rb +307 -0
- data/lib/cassandra/protocol/cql_protocol_handler.rb +323 -0
- data/lib/cassandra/protocol/frame_decoder.rb +128 -0
- data/lib/cassandra/protocol/frame_encoder.rb +48 -0
- data/lib/cassandra/protocol/request.rb +38 -0
- data/lib/cassandra/protocol/requests/auth_response_request.rb +47 -0
- data/lib/cassandra/protocol/requests/batch_request.rb +76 -0
- data/lib/cassandra/protocol/requests/credentials_request.rb +47 -0
- data/lib/cassandra/protocol/requests/execute_request.rb +103 -0
- data/lib/cassandra/protocol/requests/options_request.rb +39 -0
- data/lib/cassandra/protocol/requests/prepare_request.rb +50 -0
- data/lib/cassandra/protocol/requests/query_request.rb +153 -0
- data/lib/cassandra/protocol/requests/register_request.rb +38 -0
- data/lib/cassandra/protocol/requests/startup_request.rb +49 -0
- data/lib/cassandra/protocol/requests/void_query_request.rb +24 -0
- data/lib/cassandra/protocol/response.rb +38 -0
- data/lib/cassandra/protocol/responses/auth_challenge_response.rb +41 -0
- data/lib/cassandra/protocol/responses/auth_success_response.rb +41 -0
- data/lib/cassandra/protocol/responses/authenticate_response.rb +41 -0
- data/lib/cassandra/protocol/responses/detailed_error_response.rb +60 -0
- data/lib/cassandra/protocol/responses/error_response.rb +50 -0
- data/lib/cassandra/protocol/responses/event_response.rb +39 -0
- data/lib/cassandra/protocol/responses/prepared_result_response.rb +64 -0
- data/lib/cassandra/protocol/responses/raw_rows_result_response.rb +43 -0
- data/lib/cassandra/protocol/responses/ready_response.rb +44 -0
- data/lib/cassandra/protocol/responses/result_response.rb +48 -0
- data/lib/cassandra/protocol/responses/rows_result_response.rb +139 -0
- data/lib/cassandra/protocol/responses/schema_change_event_response.rb +60 -0
- data/lib/cassandra/protocol/responses/schema_change_result_response.rb +57 -0
- data/lib/cassandra/protocol/responses/set_keyspace_result_response.rb +42 -0
- data/lib/cassandra/protocol/responses/status_change_event_response.rb +44 -0
- data/lib/cassandra/protocol/responses/supported_response.rb +41 -0
- data/lib/cassandra/protocol/responses/topology_change_event_response.rb +34 -0
- data/lib/cassandra/protocol/responses/void_result_response.rb +39 -0
- data/lib/cassandra/protocol/type_converter.rb +384 -0
- data/lib/cassandra/protocol.rb +93 -0
- data/lib/cassandra/reconnection/policies/constant.rb +48 -0
- data/lib/cassandra/reconnection/policies/exponential.rb +79 -0
- data/lib/cassandra/reconnection/policies.rb +20 -0
- data/lib/cassandra/reconnection.rb +49 -0
- data/lib/cassandra/result.rb +215 -0
- data/lib/cassandra/retry/policies/default.rb +47 -0
- data/lib/cassandra/retry/policies/downgrading_consistency.rb +71 -0
- data/lib/cassandra/retry/policies/fallthrough.rb +39 -0
- data/lib/cassandra/retry/policies.rb +21 -0
- data/lib/cassandra/retry.rb +142 -0
- data/lib/cassandra/session.rb +202 -0
- data/lib/cassandra/statement.rb +22 -0
- data/lib/cassandra/statements/batch.rb +95 -0
- data/lib/cassandra/statements/bound.rb +48 -0
- data/lib/cassandra/statements/prepared.rb +81 -0
- data/lib/cassandra/statements/simple.rb +58 -0
- data/lib/cassandra/statements/void.rb +33 -0
- data/lib/cassandra/statements.rb +23 -0
- data/lib/cassandra/table.rb +299 -0
- data/lib/cassandra/time_uuid.rb +142 -0
- data/lib/cassandra/util.rb +167 -0
- data/lib/cassandra/uuid.rb +104 -0
- data/lib/cassandra/version.rb +21 -0
- data/lib/cassandra.rb +428 -0
- data/lib/cassandra_murmur3.jar +0 -0
- metadata +211 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
#--
|
|
4
|
+
# Copyright 2013-2014 DataStax, Inc.
|
|
5
|
+
#
|
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
# you may not use this file except in compliance with the License.
|
|
8
|
+
# 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
|
+
module Cassandra
|
|
20
|
+
# Sessions are used for query execution. Each session tracks its current keyspace. A session should be reused as much as possible, however it is ok to create several independent session for interacting with different keyspaces in the same application.
|
|
21
|
+
class Session
|
|
22
|
+
extend Forwardable
|
|
23
|
+
|
|
24
|
+
# @!method keyspace
|
|
25
|
+
# Returns current keyspace
|
|
26
|
+
# @return [String] current keyspace
|
|
27
|
+
def_delegators :@client, :keyspace
|
|
28
|
+
|
|
29
|
+
# @private
|
|
30
|
+
def initialize(client, default_options, futures_factory)
|
|
31
|
+
@client = client
|
|
32
|
+
@options = default_options
|
|
33
|
+
@futures = futures_factory
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Executes a given statement and returns a future result
|
|
37
|
+
# @!method execute_async(statement, *args, options = {})
|
|
38
|
+
#
|
|
39
|
+
# @param statement [String, Cassandra::Statements::Simple,
|
|
40
|
+
# Cassandra::Statements::Bound, Cassandra::Statements::Prepared]
|
|
41
|
+
# statement to execute
|
|
42
|
+
# @param args [*Object] arguments to paramterized query or prepared
|
|
43
|
+
# statement
|
|
44
|
+
#
|
|
45
|
+
# @option options [Symbol] :consistency consistency level for the request.
|
|
46
|
+
# Must be one of {Cassandra::CONSISTENCIES}
|
|
47
|
+
# @option options [Integer] :page_size size of results page. You can page
|
|
48
|
+
# through results using {Cassandra::Result#next_page} or
|
|
49
|
+
# {Cassandra::Result#next_page_async}
|
|
50
|
+
# @option options [Boolean] :trace (false) whether to enable request tracing
|
|
51
|
+
# @option options [Numeric] :timeout (nil) if specified, it is a number of
|
|
52
|
+
# seconds after which to time out the request if it hasn't completed
|
|
53
|
+
# @option options [Symbol] :serial_consistency (nil) this option is only
|
|
54
|
+
# relevant for conditional updates and specifies a serial consistency to
|
|
55
|
+
# be used, one of {Cassandra::SERIAL_CONSISTENCIES}
|
|
56
|
+
#
|
|
57
|
+
# @see Cassandra.connect for description of options that can be specified
|
|
58
|
+
# on the cluster-level as well as default values chosen.
|
|
59
|
+
#
|
|
60
|
+
# @note Last argument will be treated as `options` if it is a {Hash}.
|
|
61
|
+
# Therefore, make sure to pass empty `options` when executing a statement
|
|
62
|
+
# with the last parameter required to be a map datatype.
|
|
63
|
+
#
|
|
64
|
+
# @return [Cassandra::Future<Cassandra::Result>]
|
|
65
|
+
def execute_async(statement, *args)
|
|
66
|
+
if args.last.is_a?(::Hash)
|
|
67
|
+
options = @options.override(args.pop)
|
|
68
|
+
else
|
|
69
|
+
options = @options
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
case statement
|
|
73
|
+
when ::String
|
|
74
|
+
@client.query(Statements::Simple.new(statement, *args), options)
|
|
75
|
+
when Statements::Simple
|
|
76
|
+
@client.query(statement, options)
|
|
77
|
+
when Statements::Prepared
|
|
78
|
+
@client.execute(statement.bind(*args), options)
|
|
79
|
+
when Statements::Bound
|
|
80
|
+
@client.execute(statement, options)
|
|
81
|
+
when Statements::Batch
|
|
82
|
+
@client.batch(statement, options)
|
|
83
|
+
else
|
|
84
|
+
@futures.error(::ArgumentError.new("unsupported statement #{statement.inspect}"))
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# A blocking wrapper around {Cassandra::Session#execute_async}
|
|
89
|
+
# @!method execute(statement, *args, options = {})
|
|
90
|
+
# @see Cassandra::Session#execute_async
|
|
91
|
+
# @see Cassandra::Future#get
|
|
92
|
+
#
|
|
93
|
+
# @return [Cassandra::Result] query result
|
|
94
|
+
# @raise [Cassandra::Errors::NoHostsAvailable] if none of the hosts can be reached
|
|
95
|
+
# @raise [Cassandra::Errors::QueryError] if Cassandra returns an error response
|
|
96
|
+
def execute(*args)
|
|
97
|
+
execute_async(*args).get
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Prepares a given statement and returns a future prepared statement
|
|
101
|
+
#
|
|
102
|
+
# @param statement [String, Cassandra::Statements::Simple] a statement to
|
|
103
|
+
# prepare
|
|
104
|
+
#
|
|
105
|
+
# @option options [Boolean] :trace (false) whether to enable request tracing
|
|
106
|
+
# @option options [Numeric] :timeout (nil) if specified, it is a number of
|
|
107
|
+
# seconds after which to time out the request if it hasn't completed
|
|
108
|
+
#
|
|
109
|
+
# @return [Cassandra::Future<Cassandra::Statements::Prepared>] future
|
|
110
|
+
# prepared statement
|
|
111
|
+
def prepare_async(statement, options = nil)
|
|
112
|
+
if options.is_a?(::Hash)
|
|
113
|
+
options = @options.override(options)
|
|
114
|
+
else
|
|
115
|
+
options = @options
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
case statement
|
|
119
|
+
when ::String
|
|
120
|
+
@client.prepare(statement, options)
|
|
121
|
+
when Statements::Simple
|
|
122
|
+
@client.prepare(statement.cql, options)
|
|
123
|
+
else
|
|
124
|
+
@futures.error(::ArgumentError.new("unsupported statement #{statement.inspect}"))
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# A blocking wrapper around {Cassandra::Session#prepare_async}
|
|
129
|
+
# @see Cassandra::Session#prepare_async
|
|
130
|
+
# @see Cassandra::Future#get
|
|
131
|
+
#
|
|
132
|
+
# @return [Cassandra::Statements::Prepared] prepared statement
|
|
133
|
+
# @raise [Cassandra::Errors::NoHostsAvailable] if none of the hosts can be reached
|
|
134
|
+
# @raise [Cassandra::Errors::QueryError] if Cassandra returns an error response
|
|
135
|
+
def prepare(*args)
|
|
136
|
+
prepare_async(*args).get
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# Returns a logged {Statements::Batch} instance and optionally yields it to
|
|
140
|
+
# a given block
|
|
141
|
+
# @yieldparam batch [Statements::Batch] a logged batch
|
|
142
|
+
# @return [Statements::Batch] a logged batch
|
|
143
|
+
def logged_batch(&block)
|
|
144
|
+
statement = Statements::Batch::Logged.new
|
|
145
|
+
yield(statement) if block_given?
|
|
146
|
+
statement
|
|
147
|
+
end
|
|
148
|
+
alias :batch :logged_batch
|
|
149
|
+
|
|
150
|
+
# Returns a unlogged {Statements::Batch} instance and optionally yields it
|
|
151
|
+
# to a given block
|
|
152
|
+
# @yieldparam batch [Statements::Batch] an unlogged batch
|
|
153
|
+
# @return [Statements::Batch] an unlogged batch
|
|
154
|
+
def unlogged_batch
|
|
155
|
+
statement = Statements::Batch::Unlogged.new
|
|
156
|
+
yield(statement) if block_given?
|
|
157
|
+
statement
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Returns a counter {Statements::Batch} instance and optionally yields it
|
|
161
|
+
# to a given block
|
|
162
|
+
# @yieldparam batch [Statements::Batch] a counter batch
|
|
163
|
+
# @return [Statements::Batch] a counter batch
|
|
164
|
+
def counter_batch
|
|
165
|
+
statement = Statements::Batch::Counter.new
|
|
166
|
+
yield(statement) if block_given?
|
|
167
|
+
statement
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Asynchronously closes current session
|
|
171
|
+
#
|
|
172
|
+
# @return [Cassandra::Future<Cassandra::Session>] a future that resolves to
|
|
173
|
+
# self once closed
|
|
174
|
+
def close_async
|
|
175
|
+
promise = @futures.promise
|
|
176
|
+
|
|
177
|
+
@client.close.on_complete do |f|
|
|
178
|
+
if f.resolved?
|
|
179
|
+
promise.fulfill(self)
|
|
180
|
+
else
|
|
181
|
+
f.on_failure {|e| promise.break(e)}
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
promise.future
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# Synchronously closes current session
|
|
189
|
+
#
|
|
190
|
+
# @return [self] this session
|
|
191
|
+
#
|
|
192
|
+
# @see Cassandra::Session#close_async
|
|
193
|
+
def close
|
|
194
|
+
close_async.get
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# @return [String] a CLI-friendly session representation
|
|
198
|
+
def inspect
|
|
199
|
+
"#<#{self.class.name}:0x#{self.object_id.to_s(16)}>"
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
#--
|
|
4
|
+
# Copyright 2013-2014 DataStax, Inc.
|
|
5
|
+
#
|
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
# you may not use this file except in compliance with the License.
|
|
8
|
+
# 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
|
+
module Cassandra
|
|
20
|
+
module Statement
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
#--
|
|
4
|
+
# Copyright 2013-2014 DataStax, Inc.
|
|
5
|
+
#
|
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
# you may not use this file except in compliance with the License.
|
|
8
|
+
# 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
|
+
module Cassandra
|
|
20
|
+
module Statements
|
|
21
|
+
# Batch statement groups several {Cassandra::Statement}. There are several types of Batch statements available:
|
|
22
|
+
# @see Cassandra::Session#batch
|
|
23
|
+
# @see Cassandra::Session#logged_batch
|
|
24
|
+
# @see Cassandra::Session#unlogged_batch
|
|
25
|
+
# @see Cassandra::Session#counter_batch
|
|
26
|
+
class Batch
|
|
27
|
+
# @private
|
|
28
|
+
class Logged < Batch
|
|
29
|
+
def type
|
|
30
|
+
:logged
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# @private
|
|
35
|
+
class Unlogged < Batch
|
|
36
|
+
def type
|
|
37
|
+
:unlogged
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# @private
|
|
42
|
+
class Counter < Batch
|
|
43
|
+
def type
|
|
44
|
+
:counter
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
include Statement
|
|
49
|
+
|
|
50
|
+
# @private
|
|
51
|
+
attr_reader :statements
|
|
52
|
+
|
|
53
|
+
# @private
|
|
54
|
+
def initialize
|
|
55
|
+
@statements = []
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Adds a statement to this batch
|
|
59
|
+
# @param statement [String, Cassandra::Statements::Simple,
|
|
60
|
+
# Cassandra::Statements::Prepared, Cassandra::Statements::Bound] statement to add
|
|
61
|
+
# @param args [*Object] arguments to paramterized query or prepared
|
|
62
|
+
# statement
|
|
63
|
+
# @return [self]
|
|
64
|
+
def add(statement, *args)
|
|
65
|
+
case statement
|
|
66
|
+
when String
|
|
67
|
+
@statements << Simple.new(statement, *args)
|
|
68
|
+
when Prepared
|
|
69
|
+
@statements << statement.bind(*args)
|
|
70
|
+
when Bound, Simple
|
|
71
|
+
@statements << statement
|
|
72
|
+
else
|
|
73
|
+
raise ::ArgumentError, "a batch can only consist of simple or prepared statements, #{statement.inspect} given"
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
self
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# A batch statement doesn't really have any cql of its own as it is composed of multiple different statements
|
|
80
|
+
# @return [nil] nothing
|
|
81
|
+
def cql
|
|
82
|
+
nil
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# @return [Symbol] one of `:logged`, `:unlogged` or `:counter`
|
|
86
|
+
def type
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# @return [String] a CLI-friendly batch statement representation
|
|
90
|
+
def inspect
|
|
91
|
+
"#<#{self.class.name}:0x#{self.object_id.to_s(16)} @type=#{type.inspect}>"
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
#--
|
|
4
|
+
# Copyright 2013-2014 DataStax, Inc.
|
|
5
|
+
#
|
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
# you may not use this file except in compliance with the License.
|
|
8
|
+
# 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
|
+
module Cassandra
|
|
20
|
+
module Statements
|
|
21
|
+
# a Bound statement is created using {Cassandra::Statements::Prepared#bind}
|
|
22
|
+
class Bound
|
|
23
|
+
include Statement
|
|
24
|
+
|
|
25
|
+
# @return [String] original cql used to prepare this statement
|
|
26
|
+
attr_reader :cql
|
|
27
|
+
# @return [Array<Object>] a list of positional parameters for the cql
|
|
28
|
+
attr_reader :params
|
|
29
|
+
# @private
|
|
30
|
+
attr_reader :params_metadata, :result_metadata, :keyspace, :partition_key
|
|
31
|
+
|
|
32
|
+
# @private
|
|
33
|
+
def initialize(cql, params_metadata, result_metadata, params, keyspace = nil, partition_key = nil)
|
|
34
|
+
@cql = cql
|
|
35
|
+
@params_metadata = params_metadata
|
|
36
|
+
@result_metadata = result_metadata
|
|
37
|
+
@params = params
|
|
38
|
+
@keyspace = keyspace
|
|
39
|
+
@partition_key = partition_key
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# @return [String] a CLI-friendly bound statement representation
|
|
43
|
+
def inspect
|
|
44
|
+
"#<#{self.class.name}:0x#{self.object_id.to_s(16)} @cql=#{@cql.inspect} @params=#{@params}>"
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
#--
|
|
4
|
+
# Copyright 2013-2014 DataStax, Inc.
|
|
5
|
+
#
|
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
# you may not use this file except in compliance with the License.
|
|
8
|
+
# 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
|
+
module Cassandra
|
|
20
|
+
module Statements
|
|
21
|
+
class Prepared
|
|
22
|
+
include Statement
|
|
23
|
+
|
|
24
|
+
# @return [String] original cql used to prepare this statement
|
|
25
|
+
attr_reader :cql
|
|
26
|
+
# @private
|
|
27
|
+
attr_reader :params_metadata
|
|
28
|
+
# @private
|
|
29
|
+
attr_reader :result_metadata
|
|
30
|
+
|
|
31
|
+
# @private
|
|
32
|
+
def initialize(cql, params_metadata, result_metadata, trace_id, keyspace, statement, options, hosts, consistency, retries, client, futures_factory, schema)
|
|
33
|
+
@cql = cql
|
|
34
|
+
@params_metadata = params_metadata
|
|
35
|
+
@result_metadata = result_metadata
|
|
36
|
+
@trace_id = trace_id
|
|
37
|
+
@keyspace = keyspace
|
|
38
|
+
@statement = statement
|
|
39
|
+
@options = options
|
|
40
|
+
@hosts = hosts
|
|
41
|
+
@consistency = consistency
|
|
42
|
+
@retries = retries
|
|
43
|
+
@client = client
|
|
44
|
+
@schema = schema
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Creates a statement bound with specific arguments
|
|
48
|
+
# @param args [*Object] arguments to bind, must contain the same number
|
|
49
|
+
# of parameters as the number of positional arguments (`?`) in the
|
|
50
|
+
# original cql passed to {Cassandra::Session#prepare}
|
|
51
|
+
# @return [Cassandra::Statements::Bound] bound statement
|
|
52
|
+
def bind(*args)
|
|
53
|
+
raise ::ArgumentError, "expecting exactly #{@params_metadata.size} bind parameters, #{args.size} given" if args.size != @params_metadata.size
|
|
54
|
+
|
|
55
|
+
return Bound.new(@cql, @params_metadata, @result_metadata, args) if @params_metadata.empty?
|
|
56
|
+
|
|
57
|
+
keyspace, table, _, _ = @params_metadata.first
|
|
58
|
+
return Bound.new(@cql, @params_metadata, @result_metadata, args, keyspace) unless keyspace && table
|
|
59
|
+
|
|
60
|
+
values = ::Hash.new
|
|
61
|
+
@params_metadata.zip(args) do |(keyspace, table, column, type), value|
|
|
62
|
+
values[column] = value
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
partition_key = @schema.create_partition_key(keyspace, table, values)
|
|
66
|
+
|
|
67
|
+
Bound.new(@cql, @params_metadata, @result_metadata, args, keyspace, partition_key)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# @return [Cassandra::Execution::Info] execution info for PREPARE request
|
|
71
|
+
def execution_info
|
|
72
|
+
@info ||= Execution::Info.new(@keyspace, @statement, @options, @hosts, @consistency, @retries, @trace_id ? Execution::Trace.new(@trace_id, @client) : nil)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# @return [String] a CLI-friendly prepared statement representation
|
|
76
|
+
def inspect
|
|
77
|
+
"#<#{self.class.name}:0x#{self.object_id.to_s(16)} @cql=#{@cql.inspect}>"
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
#--
|
|
4
|
+
# Copyright 2013-2014 DataStax, Inc.
|
|
5
|
+
#
|
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
# you may not use this file except in compliance with the License.
|
|
8
|
+
# 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
|
+
module Cassandra
|
|
20
|
+
module Statements
|
|
21
|
+
class Simple
|
|
22
|
+
include Statement
|
|
23
|
+
|
|
24
|
+
# @return [String] original cql used to prepare this statement
|
|
25
|
+
attr_reader :cql
|
|
26
|
+
# @return [Array<Object>] a list of positional parameters for the cql
|
|
27
|
+
attr_reader :params
|
|
28
|
+
|
|
29
|
+
# @param cql [String] a cql statement
|
|
30
|
+
# @param params [*Object] positional arguments for the query
|
|
31
|
+
#
|
|
32
|
+
# @raise [ArgumentError] if cql statement given is not a String
|
|
33
|
+
def initialize(cql, *params)
|
|
34
|
+
unless cql.is_a?(::String)
|
|
35
|
+
raise ::ArgumentError, "cql must be a string, #{cql.inspect} given"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
@cql = cql
|
|
39
|
+
@params = params
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# @return [String] a CLI-friendly simple statement representation
|
|
43
|
+
def inspect
|
|
44
|
+
"#<#{self.class.name}:0x#{self.object_id.to_s(16)} @cql=#{@cql.inspect} @params=#{@params.inspect}>"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# @param other [Object, Cassandra::Statements::Simple] object to compare
|
|
48
|
+
# @return [Boolean] whether the statements are equal
|
|
49
|
+
def eql?(other)
|
|
50
|
+
other.is_a?(Simple) &&
|
|
51
|
+
@cql == other.cql &&
|
|
52
|
+
@params == other.params
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
alias :== :eql?
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
#--
|
|
4
|
+
# Copyright 2013-2014 DataStax, Inc.
|
|
5
|
+
#
|
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
# you may not use this file except in compliance with the License.
|
|
8
|
+
# 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
|
+
module Cassandra
|
|
20
|
+
module Statements
|
|
21
|
+
# This statement is passed to {Cassandra::LoadBalancing::Policy#plan} when
|
|
22
|
+
# establishing connections and preparing statements
|
|
23
|
+
class Void
|
|
24
|
+
include Statement
|
|
25
|
+
|
|
26
|
+
# Returns nothing
|
|
27
|
+
# @return [nil] there is no cql for the void statement
|
|
28
|
+
def cql
|
|
29
|
+
nil
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
#--
|
|
4
|
+
# Copyright 2013-2014 DataStax, Inc.
|
|
5
|
+
#
|
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
# you may not use this file except in compliance with the License.
|
|
8
|
+
# 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
|
+
require 'cassandra/statements/batch'
|
|
20
|
+
require 'cassandra/statements/bound'
|
|
21
|
+
require 'cassandra/statements/prepared'
|
|
22
|
+
require 'cassandra/statements/simple'
|
|
23
|
+
require 'cassandra/statements/void'
|