cassandra-driver 0.1.0.alpha1 → 1.0.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. data/.yardopts +4 -0
  2. data/README.md +117 -0
  3. data/lib/cassandra.rb +320 -0
  4. data/lib/cassandra/auth.rb +97 -0
  5. data/lib/cassandra/auth/providers.rb +16 -0
  6. data/lib/cassandra/auth/providers/password.rb +73 -0
  7. data/lib/cassandra/client.rb +144 -0
  8. data/lib/cassandra/client/batch.rb +212 -0
  9. data/lib/cassandra/client/client.rb +591 -0
  10. data/lib/cassandra/client/column_metadata.rb +54 -0
  11. data/lib/cassandra/client/connection_manager.rb +72 -0
  12. data/lib/cassandra/client/connector.rb +272 -0
  13. data/lib/cassandra/client/execute_options_decoder.rb +59 -0
  14. data/lib/cassandra/client/null_logger.rb +37 -0
  15. data/lib/cassandra/client/peer_discovery.rb +50 -0
  16. data/lib/cassandra/client/prepared_statement.rb +314 -0
  17. data/lib/cassandra/client/query_result.rb +230 -0
  18. data/lib/cassandra/client/request_runner.rb +71 -0
  19. data/lib/cassandra/client/result_metadata.rb +48 -0
  20. data/lib/cassandra/client/void_result.rb +78 -0
  21. data/lib/cassandra/cluster.rb +191 -0
  22. data/lib/cassandra/cluster/client.rb +767 -0
  23. data/lib/cassandra/cluster/connector.rb +231 -0
  24. data/lib/cassandra/cluster/control_connection.rb +420 -0
  25. data/lib/cassandra/cluster/options.rb +40 -0
  26. data/lib/cassandra/cluster/registry.rb +181 -0
  27. data/lib/cassandra/cluster/schema.rb +321 -0
  28. data/lib/cassandra/cluster/schema/type_parser.rb +138 -0
  29. data/lib/cassandra/column.rb +92 -0
  30. data/lib/cassandra/compression.rb +66 -0
  31. data/lib/cassandra/compression/compressors/lz4.rb +72 -0
  32. data/lib/cassandra/compression/compressors/snappy.rb +66 -0
  33. data/lib/cassandra/driver.rb +86 -0
  34. data/lib/cassandra/errors.rb +79 -0
  35. data/lib/cassandra/execution/info.rb +51 -0
  36. data/lib/cassandra/execution/options.rb +77 -0
  37. data/lib/cassandra/execution/trace.rb +152 -0
  38. data/lib/cassandra/future.rb +675 -0
  39. data/lib/cassandra/host.rb +75 -0
  40. data/lib/cassandra/keyspace.rb +120 -0
  41. data/lib/cassandra/listener.rb +87 -0
  42. data/lib/cassandra/load_balancing.rb +112 -0
  43. data/lib/cassandra/load_balancing/policies.rb +18 -0
  44. data/lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb +149 -0
  45. data/lib/cassandra/load_balancing/policies/round_robin.rb +95 -0
  46. data/lib/cassandra/load_balancing/policies/white_list.rb +90 -0
  47. data/lib/cassandra/protocol.rb +93 -0
  48. data/lib/cassandra/protocol/cql_byte_buffer.rb +307 -0
  49. data/lib/cassandra/protocol/cql_protocol_handler.rb +323 -0
  50. data/lib/cassandra/protocol/frame_decoder.rb +128 -0
  51. data/lib/cassandra/protocol/frame_encoder.rb +48 -0
  52. data/lib/cassandra/protocol/request.rb +38 -0
  53. data/lib/cassandra/protocol/requests/auth_response_request.rb +47 -0
  54. data/lib/cassandra/protocol/requests/batch_request.rb +76 -0
  55. data/lib/cassandra/protocol/requests/credentials_request.rb +47 -0
  56. data/lib/cassandra/protocol/requests/execute_request.rb +103 -0
  57. data/lib/cassandra/protocol/requests/options_request.rb +39 -0
  58. data/lib/cassandra/protocol/requests/prepare_request.rb +50 -0
  59. data/lib/cassandra/protocol/requests/query_request.rb +153 -0
  60. data/lib/cassandra/protocol/requests/register_request.rb +38 -0
  61. data/lib/cassandra/protocol/requests/startup_request.rb +49 -0
  62. data/lib/cassandra/protocol/requests/void_query_request.rb +24 -0
  63. data/lib/cassandra/protocol/response.rb +38 -0
  64. data/lib/cassandra/protocol/responses/auth_challenge_response.rb +41 -0
  65. data/lib/cassandra/protocol/responses/auth_success_response.rb +41 -0
  66. data/lib/cassandra/protocol/responses/authenticate_response.rb +41 -0
  67. data/lib/cassandra/protocol/responses/detailed_error_response.rb +60 -0
  68. data/lib/cassandra/protocol/responses/error_response.rb +50 -0
  69. data/lib/cassandra/protocol/responses/event_response.rb +39 -0
  70. data/lib/cassandra/protocol/responses/prepared_result_response.rb +64 -0
  71. data/lib/cassandra/protocol/responses/raw_rows_result_response.rb +43 -0
  72. data/lib/cassandra/protocol/responses/ready_response.rb +44 -0
  73. data/lib/cassandra/protocol/responses/result_response.rb +48 -0
  74. data/lib/cassandra/protocol/responses/rows_result_response.rb +139 -0
  75. data/lib/cassandra/protocol/responses/schema_change_event_response.rb +60 -0
  76. data/lib/cassandra/protocol/responses/schema_change_result_response.rb +57 -0
  77. data/lib/cassandra/protocol/responses/set_keyspace_result_response.rb +42 -0
  78. data/lib/cassandra/protocol/responses/status_change_event_response.rb +44 -0
  79. data/lib/cassandra/protocol/responses/supported_response.rb +41 -0
  80. data/lib/cassandra/protocol/responses/topology_change_event_response.rb +34 -0
  81. data/lib/cassandra/protocol/responses/void_result_response.rb +39 -0
  82. data/lib/cassandra/protocol/type_converter.rb +384 -0
  83. data/lib/cassandra/reconnection.rb +49 -0
  84. data/lib/cassandra/reconnection/policies.rb +20 -0
  85. data/lib/cassandra/reconnection/policies/constant.rb +48 -0
  86. data/lib/cassandra/reconnection/policies/exponential.rb +79 -0
  87. data/lib/cassandra/result.rb +215 -0
  88. data/lib/cassandra/retry.rb +142 -0
  89. data/lib/cassandra/retry/policies.rb +21 -0
  90. data/lib/cassandra/retry/policies/default.rb +47 -0
  91. data/lib/cassandra/retry/policies/downgrading_consistency.rb +71 -0
  92. data/lib/cassandra/retry/policies/fallthrough.rb +39 -0
  93. data/lib/cassandra/session.rb +195 -0
  94. data/lib/cassandra/statement.rb +22 -0
  95. data/lib/cassandra/statements.rb +23 -0
  96. data/lib/cassandra/statements/batch.rb +95 -0
  97. data/lib/cassandra/statements/bound.rb +46 -0
  98. data/lib/cassandra/statements/prepared.rb +59 -0
  99. data/lib/cassandra/statements/simple.rb +58 -0
  100. data/lib/cassandra/statements/void.rb +33 -0
  101. data/lib/cassandra/table.rb +254 -0
  102. data/lib/cassandra/time_uuid.rb +141 -0
  103. data/lib/cassandra/util.rb +169 -0
  104. data/lib/cassandra/uuid.rb +104 -0
  105. data/lib/cassandra/version.rb +17 -1
  106. metadata +134 -8
@@ -0,0 +1,21 @@
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/retry/policies/default'
20
+ require 'cassandra/retry/policies/downgrading_consistency'
21
+ require 'cassandra/retry/policies/fallthrough'
@@ -0,0 +1,47 @@
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 Retry
21
+ module Policies
22
+ class Default
23
+ include Policy
24
+
25
+ def read_timeout(statement, consistency, required, received, retrieved, retries)
26
+ return reraise if retries > 0
27
+
28
+ if received >= required && !retrieved
29
+ try_again(consistency)
30
+ else
31
+ reraise
32
+ end
33
+ end
34
+
35
+ def write_timeout(statement, consistency, type, required, received, retries)
36
+ return reraise if retries > 0
37
+
38
+ type == :batch_log ? try_again(consistency) : reraise
39
+ end
40
+
41
+ def unavailable(statement, consistency, required, alive, retries)
42
+ reraise
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,71 @@
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 Retry
21
+ module Policies
22
+ class DowngradingConsistency
23
+ include Policy
24
+
25
+ def read_timeout(statement, consistency, required, received, retrieved, retries)
26
+ return reraise if retries > 0 || SERIAL_CONSISTENCIES.include?(consistency)
27
+ return max_likely_to_work(consistency, required, received) if received < required
28
+
29
+ retrieved ? reraise : try_again(consistency)
30
+ end
31
+
32
+ def write_timeout(statement, consistency, type, required, received, retries)
33
+ return reraise if retries > 0
34
+
35
+ case type
36
+ when :simple, :batch
37
+ ignore
38
+ when :unlogged_batch
39
+ max_likely_to_work(consistency, required, received)
40
+ when :batch_log
41
+ try_again(consistency)
42
+ else
43
+ reraise
44
+ end
45
+ end
46
+
47
+ def unavailable(statement, consistency, required, alive, retries)
48
+ return reraise if retries > 0
49
+
50
+ max_likely_to_work(consistency, required, alive)
51
+ end
52
+
53
+ private
54
+
55
+ def max_likely_to_work(consistency, required, received)
56
+ if consistency == :all && required > 1 && received >= (required.to_f / 2).floor + 1
57
+ try_again(:quorum)
58
+ elsif received >= 3
59
+ try_again(:three)
60
+ elsif received >= 2
61
+ try_again(:two)
62
+ elsif received >= 1
63
+ try_again(:one)
64
+ else
65
+ reraise
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,39 @@
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 Retry
21
+ module Policies
22
+ class Fallthrough
23
+ include Policy
24
+
25
+ def read_timeout(statement, consistency, required, received, retrieved, retries)
26
+ reraise
27
+ end
28
+
29
+ def write_timeout(statement, consistency, type, required, received, retries)
30
+ reraise
31
+ end
32
+
33
+ def unavailable(statement, consistency, required, alive, retries)
34
+ reraise
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,195 @@
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
+ # @private
23
+ def initialize(client, default_options, futures_factory)
24
+ @client = client
25
+ @options = default_options
26
+ @futures = futures_factory
27
+ end
28
+
29
+ # Executes a given statement and returns a future result
30
+ # @!method execute_async(statement, *args, options = {})
31
+ #
32
+ # @param statement [String, Cassandra::Statements::Simple,
33
+ # Cassandra::Statements::Bound, Cassandra::Statements::Prepared]
34
+ # statement to execute
35
+ # @param args [*Object] arguments to paramterized query or prepared
36
+ # statement
37
+ #
38
+ # @option options [Symbol] :consistency consistency level for the request.
39
+ # Must be one of {Cassandra::CONSISTENCIES}
40
+ # @option options [Integer] :page_size size of results page. You can page
41
+ # through results using {Cassandra::Result#next_page} or
42
+ # {Cassandra::Result#next_page_async}
43
+ # @option options [Boolean] :trace (false) whether to enable request tracing
44
+ # @option options [Numeric] :timeout (nil) if specified, it is a number of
45
+ # seconds after which to time out the request if it hasn't completed
46
+ # @option options [Symbol] :serial_consistency (nil) this option is only
47
+ # relevant for conditional updates and specifies a serial consistency to
48
+ # be used, one of {Cassandra::SERIAL_CONSISTENCIES}
49
+ #
50
+ # @see Cassandra.connect for description of options that can be specified
51
+ # on the cluster-level as well as default values chosen.
52
+ #
53
+ # @note Last argument will be treated as `options` if it is a {Hash}.
54
+ # Therefore, make sure to pass empty `options` when executing a statement
55
+ # with the last parameter required to be a map datatype.
56
+ #
57
+ # @return [Cassandra::Future<Cassandra::Result>]
58
+ def execute_async(statement, *args)
59
+ if args.last.is_a?(::Hash)
60
+ options = @options.override(args.pop)
61
+ else
62
+ options = @options
63
+ end
64
+
65
+ case statement
66
+ when ::String
67
+ @client.query(Statements::Simple.new(statement, *args), options)
68
+ when Statements::Simple
69
+ @client.query(statement, options)
70
+ when Statements::Prepared
71
+ @client.execute(statement.bind(*args), options)
72
+ when Statements::Bound
73
+ @client.execute(statement, options)
74
+ when Statements::Batch
75
+ @client.batch(statement, options)
76
+ else
77
+ @futures.error(::ArgumentError.new("unsupported statement #{statement.inspect}"))
78
+ end
79
+ end
80
+
81
+ # A blocking wrapper around {Cassandra::Session#execute_async}
82
+ # @!method execute(statement, *args, options = {})
83
+ # @see Cassandra::Session#execute_async
84
+ # @see Cassandra::Future#get
85
+ #
86
+ # @return [Cassandra::Result] query result
87
+ # @raise [Cassandra::Errors::NoHostsAvailable] if none of the hosts can be reached
88
+ # @raise [Cassandra::Errors::QueryError] if Cassandra returns an error response
89
+ def execute(*args)
90
+ execute_async(*args).get
91
+ end
92
+
93
+ # Prepares a given statement and returns a future prepared statement
94
+ #
95
+ # @param statement [String, Cassandra::Statements::Simple] a statement to
96
+ # prepare
97
+ #
98
+ # @option options [Boolean] :trace (false) whether to enable request tracing
99
+ # @option options [Numeric] :timeout (nil) if specified, it is a number of
100
+ # seconds after which to time out the request if it hasn't completed
101
+ #
102
+ # @return [Cassandra::Future<Cassandra::Statements::Prepared>] future
103
+ # prepared statement
104
+ def prepare_async(statement, options = nil)
105
+ if options.is_a?(::Hash)
106
+ options = @options.override(options)
107
+ else
108
+ options = @options
109
+ end
110
+
111
+ case statement
112
+ when ::String
113
+ @client.prepare(statement, options)
114
+ when Statements::Simple
115
+ @client.prepare(statement.cql, options)
116
+ else
117
+ @futures.error(::ArgumentError.new("unsupported statement #{statement.inspect}"))
118
+ end
119
+ end
120
+
121
+ # A blocking wrapper around {Cassandra::Session#prepare_async}
122
+ # @see Cassandra::Session#prepare_async
123
+ # @see Cassandra::Future#get
124
+ #
125
+ # @return [Cassandra::Statements::Prepared] prepared statement
126
+ # @raise [Cassandra::Errors::NoHostsAvailable] if none of the hosts can be reached
127
+ # @raise [Cassandra::Errors::QueryError] if Cassandra returns an error response
128
+ def prepare(*args)
129
+ prepare_async(*args).get
130
+ end
131
+
132
+ # Returns a logged {Statements::Batch} instance and optionally yields it to
133
+ # a given block
134
+ # @yieldparam batch [Statements::Batch] a logged batch
135
+ # @return [Statements::Batch] a logged batch
136
+ def logged_batch(&block)
137
+ statement = Statements::Batch::Logged.new
138
+ yield(statement) if block_given?
139
+ statement
140
+ end
141
+ alias :batch :logged_batch
142
+
143
+ # Returns a unlogged {Statements::Batch} instance and optionally yields it
144
+ # to a given block
145
+ # @yieldparam batch [Statements::Batch] an unlogged batch
146
+ # @return [Statements::Batch] an unlogged batch
147
+ def unlogged_batch
148
+ statement = Statements::Batch::Unlogged.new
149
+ yield(statement) if block_given?
150
+ statement
151
+ end
152
+
153
+ # Returns a counter {Statements::Batch} instance and optionally yields it
154
+ # to a given block
155
+ # @yieldparam batch [Statements::Batch] a counter batch
156
+ # @return [Statements::Batch] a counter batch
157
+ def counter_batch
158
+ statement = Statements::Batch::Counter.new
159
+ yield(statement) if block_given?
160
+ statement
161
+ end
162
+
163
+ # Asynchronously closes current session
164
+ #
165
+ # @return [Cassandra::Future<Cassandra::Session>] a future that resolves to
166
+ # self once closed
167
+ def close_async
168
+ promise = @futures.promise
169
+
170
+ @client.close.on_complete do |f|
171
+ if f.resolved?
172
+ promise.fulfill(self)
173
+ else
174
+ f.on_failure {|e| promise.break(e)}
175
+ end
176
+ end
177
+
178
+ promise.future
179
+ end
180
+
181
+ # Synchronously closes current session
182
+ #
183
+ # @return [self] this session
184
+ #
185
+ # @see Cassandra::Session#close_async
186
+ def close
187
+ close_async.get
188
+ end
189
+
190
+ # @return [String] a CLI-friendly session representation
191
+ def inspect
192
+ "#<#{self.class.name}:0x#{self.object_id.to_s(16)}>"
193
+ end
194
+ end
195
+ 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,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'
@@ -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