cassandra-driver 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +13 -5
  2. data/README.md +18 -9
  3. data/ext/cassandra_murmur3/cassandra_murmur3.c +1 -1
  4. data/lib/cassandra.rb +5 -1
  5. data/lib/cassandra/address_resolution.rb +1 -1
  6. data/lib/cassandra/address_resolution/policies/ec2_multi_region.rb +1 -1
  7. data/lib/cassandra/address_resolution/policies/none.rb +1 -1
  8. data/lib/cassandra/auth.rb +1 -1
  9. data/lib/cassandra/auth/providers.rb +1 -1
  10. data/lib/cassandra/auth/providers/password.rb +1 -1
  11. data/lib/cassandra/cluster.rb +1 -1
  12. data/lib/cassandra/cluster/client.rb +33 -13
  13. data/lib/cassandra/cluster/connection_pool.rb +1 -1
  14. data/lib/cassandra/cluster/connector.rb +1 -36
  15. data/lib/cassandra/cluster/control_connection.rb +85 -9
  16. data/lib/cassandra/cluster/failed_connection.rb +1 -1
  17. data/lib/cassandra/cluster/metadata.rb +1 -1
  18. data/lib/cassandra/cluster/options.rb +15 -4
  19. data/lib/cassandra/cluster/registry.rb +1 -1
  20. data/lib/cassandra/cluster/schema.rb +76 -16
  21. data/lib/cassandra/cluster/schema/partitioners.rb +1 -1
  22. data/lib/cassandra/cluster/schema/partitioners/murmur3.rb +1 -1
  23. data/lib/cassandra/cluster/schema/partitioners/ordered.rb +1 -1
  24. data/lib/cassandra/cluster/schema/partitioners/random.rb +1 -1
  25. data/lib/cassandra/cluster/schema/replication_strategies.rb +1 -1
  26. data/lib/cassandra/cluster/schema/replication_strategies/network_topology.rb +1 -1
  27. data/lib/cassandra/cluster/schema/replication_strategies/none.rb +1 -1
  28. data/lib/cassandra/cluster/schema/replication_strategies/simple.rb +1 -1
  29. data/lib/cassandra/cluster/schema/type_parser.rb +35 -8
  30. data/lib/cassandra/column.rb +10 -14
  31. data/lib/cassandra/compression.rb +1 -1
  32. data/lib/cassandra/compression/compressors/lz4.rb +1 -1
  33. data/lib/cassandra/compression/compressors/snappy.rb +1 -1
  34. data/lib/cassandra/driver.rb +2 -2
  35. data/lib/cassandra/errors.rb +1 -1
  36. data/lib/cassandra/execution/info.rb +1 -1
  37. data/lib/cassandra/execution/options.rb +3 -2
  38. data/lib/cassandra/execution/trace.rb +1 -1
  39. data/lib/cassandra/executors.rb +1 -1
  40. data/lib/cassandra/future.rb +1 -1
  41. data/lib/cassandra/host.rb +1 -1
  42. data/lib/cassandra/keyspace.rb +55 -5
  43. data/lib/cassandra/listener.rb +1 -1
  44. data/lib/cassandra/load_balancing.rb +1 -1
  45. data/lib/cassandra/load_balancing/policies.rb +1 -1
  46. data/lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb +1 -1
  47. data/lib/cassandra/load_balancing/policies/round_robin.rb +1 -1
  48. data/lib/cassandra/load_balancing/policies/token_aware.rb +1 -1
  49. data/lib/cassandra/load_balancing/policies/white_list.rb +1 -1
  50. data/lib/cassandra/null_logger.rb +1 -1
  51. data/lib/cassandra/protocol.rb +6 -1
  52. data/lib/cassandra/protocol/coder.rb +319 -84
  53. data/lib/cassandra/protocol/cql_byte_buffer.rb +1 -1
  54. data/lib/cassandra/protocol/cql_protocol_handler.rb +24 -10
  55. data/lib/cassandra/protocol/request.rb +1 -1
  56. data/lib/cassandra/protocol/requests/auth_response_request.rb +1 -1
  57. data/lib/cassandra/protocol/requests/batch_request.rb +1 -1
  58. data/lib/cassandra/protocol/requests/credentials_request.rb +1 -1
  59. data/lib/cassandra/protocol/requests/execute_request.rb +1 -1
  60. data/lib/cassandra/protocol/requests/options_request.rb +1 -1
  61. data/lib/cassandra/protocol/requests/prepare_request.rb +1 -1
  62. data/lib/cassandra/protocol/requests/query_request.rb +5 -3
  63. data/lib/cassandra/protocol/requests/register_request.rb +1 -1
  64. data/lib/cassandra/protocol/requests/startup_request.rb +1 -1
  65. data/lib/cassandra/protocol/requests/void_query_request.rb +1 -1
  66. data/lib/cassandra/protocol/response.rb +1 -1
  67. data/lib/cassandra/protocol/responses/already_exists_error_response.rb +1 -1
  68. data/lib/cassandra/protocol/responses/auth_challenge_response.rb +1 -1
  69. data/lib/cassandra/protocol/responses/auth_success_response.rb +1 -1
  70. data/lib/cassandra/protocol/responses/authenticate_response.rb +1 -1
  71. data/lib/cassandra/protocol/responses/error_response.rb +1 -1
  72. data/lib/cassandra/protocol/responses/event_response.rb +1 -1
  73. data/lib/cassandra/protocol/responses/prepared_result_response.rb +1 -1
  74. data/lib/cassandra/protocol/responses/raw_rows_result_response.rb +9 -2
  75. data/lib/cassandra/protocol/responses/read_timeout_error_response.rb +1 -1
  76. data/lib/cassandra/protocol/responses/ready_response.rb +1 -1
  77. data/lib/cassandra/protocol/responses/result_response.rb +1 -1
  78. data/lib/cassandra/protocol/responses/rows_result_response.rb +1 -1
  79. data/lib/cassandra/protocol/responses/schema_change_event_response.rb +21 -6
  80. data/lib/cassandra/protocol/responses/schema_change_result_response.rb +18 -8
  81. data/lib/cassandra/protocol/responses/set_keyspace_result_response.rb +1 -1
  82. data/lib/cassandra/protocol/responses/status_change_event_response.rb +1 -1
  83. data/lib/cassandra/protocol/responses/supported_response.rb +1 -1
  84. data/lib/cassandra/protocol/responses/topology_change_event_response.rb +1 -1
  85. data/lib/cassandra/protocol/responses/unavailable_error_response.rb +1 -1
  86. data/lib/cassandra/protocol/responses/unprepared_error_response.rb +1 -1
  87. data/lib/cassandra/protocol/responses/void_result_response.rb +1 -1
  88. data/lib/cassandra/protocol/responses/write_timeout_error_response.rb +1 -1
  89. data/lib/cassandra/protocol/v1.rb +4 -2
  90. data/lib/cassandra/protocol/v3.rb +280 -0
  91. data/lib/cassandra/reconnection.rb +1 -1
  92. data/lib/cassandra/reconnection/policies.rb +1 -1
  93. data/lib/cassandra/reconnection/policies/constant.rb +1 -1
  94. data/lib/cassandra/reconnection/policies/exponential.rb +1 -1
  95. data/lib/cassandra/result.rb +1 -1
  96. data/lib/cassandra/retry.rb +1 -1
  97. data/lib/cassandra/retry/policies.rb +1 -1
  98. data/lib/cassandra/retry/policies/default.rb +1 -1
  99. data/lib/cassandra/retry/policies/downgrading_consistency.rb +1 -1
  100. data/lib/cassandra/retry/policies/fallthrough.rb +1 -1
  101. data/lib/cassandra/session.rb +14 -80
  102. data/lib/cassandra/statement.rb +1 -1
  103. data/lib/cassandra/statements.rb +1 -1
  104. data/lib/cassandra/statements/batch.rb +10 -25
  105. data/lib/cassandra/statements/bound.rb +1 -1
  106. data/lib/cassandra/statements/prepared.rb +24 -31
  107. data/lib/cassandra/statements/simple.rb +22 -66
  108. data/lib/cassandra/statements/void.rb +1 -1
  109. data/lib/cassandra/table.rb +36 -5
  110. data/lib/cassandra/time_uuid.rb +1 -1
  111. data/lib/cassandra/tuple.rb +124 -0
  112. data/lib/cassandra/types.rb +1406 -0
  113. data/lib/cassandra/udt.rb +420 -0
  114. data/lib/cassandra/util.rb +42 -64
  115. data/lib/cassandra/uuid.rb +1 -1
  116. data/lib/cassandra/uuid/generator.rb +1 -1
  117. data/lib/cassandra/version.rb +2 -2
  118. metadata +19 -15
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  #--
4
- # Copyright 2013-2014 DataStax, Inc.
4
+ # Copyright 2013-2015 DataStax, Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  #--
4
- # Copyright 2013-2014 DataStax, Inc.
4
+ # Copyright 2013-2015 DataStax, Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  #--
4
- # Copyright 2013-2014 DataStax, Inc.
4
+ # Copyright 2013-2015 DataStax, Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  #--
4
- # Copyright 2013-2014 DataStax, Inc.
4
+ # Copyright 2013-2015 DataStax, Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -33,14 +33,13 @@ module Cassandra
33
33
  @futures = futures_factory
34
34
  end
35
35
 
36
- # @!method execute_async(statement, options = nil)
37
36
  # Executes a given statement and returns a future result
38
37
  #
39
38
  # @param statement [String, Cassandra::Statements::Simple,
40
39
  # Cassandra::Statements::Bound, Cassandra::Statements::Prepared]
41
40
  # statement to execute
42
41
  #
43
- # @param options [nil, Hash] a customizable set of options
42
+ # @param options [Hash] (nil) a customizable set of options
44
43
  #
45
44
  # @option options [Symbol] :consistency consistency level for the request.
46
45
  # Must be one of {Cassandra::CONSISTENCIES}
@@ -56,53 +55,23 @@ module Cassandra
56
55
  # @option options [String] :paging_state (nil) this option is used for
57
56
  # stateless paging, where result paging is resumed some time after the
58
57
  # initial request.
59
- # @option options [Array] :arguments (nil) positional arguments for the
60
- # statement.
61
- #
62
- # @overload execute_async(statement, *args, options = nil)
63
- # Executes a statement using the deprecated splat-style way of passing
64
- # positional arguments/
65
- #
66
- # @deprecated This style will soon be removed, use the `:arguments`
67
- # option to provide positional arguments instead.
68
- #
69
- # @param statement [String, Cassandra::Statements::Simple,
70
- # Cassandra::Statements::Bound, Cassandra::Statements::Prepared]
71
- # statement to execute
72
- #
73
- # @param args [*Object] **this style of positional arguments is
74
- # deprecated, please use the `:arguments` options instead** -
75
- # positional arguments to paramterized query or prepared statement.
76
- #
77
- # @param options [nil, Hash] a customizable set of options
78
- #
79
- # @note Last argument will be treated as `options` if it is a {Hash}.
80
- # Therefore, make sure to pass empty `options` when executing a
81
- # statement with the last parameter required to be a map datatype.
58
+ # @option options [Array, Hash] :arguments (nil) positional or named
59
+ # arguments for the statement.
82
60
  #
83
61
  # @see Cassandra.cluster Options that can be specified on the cluster-level
84
62
  # and their default values.
85
63
  #
86
- # @note Positional arguments for simple statements are only supported on
64
+ # @note Positional arguments for simple statements are only supported
87
65
  # starting with Apache Cassandra 2.0 and above.
88
66
  #
67
+ # @note Named arguments for simple statements are only supported
68
+ # starting with Apache Cassandra 2.1 and above.
69
+ #
89
70
  # @return [Cassandra::Future<Cassandra::Result>]
90
71
  #
91
72
  # @see Cassandra::Session#execute A list of errors this future can be
92
73
  # resolved with
93
- def execute_async(statement, *args)
94
- options = nil
95
- options = args.pop if args.last.is_a?(::Hash)
96
-
97
- unless args.empty?
98
- ::Kernel.warn "[WARNING] Splat style (*args) positional arguments " \
99
- "are deprecated, use the :arguments option instead, " \
100
- "called from #{caller.first}"
101
-
102
- options ||= {}
103
- options[:arguments] = args
104
- end
105
-
74
+ def execute_async(statement, options = nil)
106
75
  if options
107
76
  options = @options.override(options)
108
77
  else
@@ -134,28 +103,7 @@ module Cassandra
134
103
  # Cassandra::Statements::Bound, Cassandra::Statements::Prepared]
135
104
  # statement to execute
136
105
  #
137
- # @param options [nil, Hash] a customizable set of options
138
- #
139
- # @overload execute(statement, *args, options = nil)
140
- # Executes a statement using the deprecated splat-style way of passing
141
- # positional arguments/
142
- #
143
- # @deprecated This style will soon be removed, use the `:arguments`
144
- # option to provide positional arguments instead.
145
- #
146
- # @param statement [String, Cassandra::Statements::Simple,
147
- # Cassandra::Statements::Bound, Cassandra::Statements::Prepared]
148
- # statement to execute
149
- #
150
- # @param args [*Object] **this style of positional arguments is
151
- # deprecated, please use the `:arguments` options instead** -
152
- # positional arguments to paramterized query or prepared statement.
153
- #
154
- # @param options [nil, Hash] a customizable set of options
155
- #
156
- # @note Last argument will be treated as `options` if it is a {Hash}.
157
- # Therefore, make sure to pass empty `options` when executing a
158
- # statement with the last parameter required to be a map datatype.
106
+ # @param options [Hash] (nil) a customizable set of options
159
107
  #
160
108
  # @see Cassandra::Session#execute_async
161
109
  # @see Cassandra::Future#get
@@ -166,24 +114,8 @@ module Cassandra
166
114
  # @raise [Cassandra::Errors::ValidationError] if Cassandra fails to validate
167
115
  # @raise [Cassandra::Errors::TimeoutError] if request has not completed
168
116
  # within the `:timeout` given
169
- def execute(statement, *args)
170
- options = nil
171
- options = args.pop if args.last.is_a?(::Hash)
172
-
173
- unless args.empty?
174
- ::Kernel.warn "[WARNING] Splat style (*args) positional arguments " \
175
- "are deprecated, use the :arguments option instead, " \
176
- "called from #{caller.first}"
177
-
178
- options ||= {}
179
- options[:arguments] = args
180
- end
181
-
182
- if options
183
- execute_async(statement, options).get
184
- else
185
- execute_async(statement).get
186
- end
117
+ def execute(statement, options = nil)
118
+ execute_async(statement, options).get
187
119
  end
188
120
 
189
121
  # Prepares a given statement and returns a future prepared statement
@@ -191,6 +123,8 @@ module Cassandra
191
123
  # @param statement [String, Cassandra::Statements::Simple] a statement to
192
124
  # prepare
193
125
  #
126
+ # @param options [Hash] (nil) a customizable set of options
127
+ #
194
128
  # @option options [Boolean] :trace (false) whether to enable request tracing
195
129
  # @option options [Numeric] :timeout (nil) if specified, it is a number of
196
130
  # seconds after which to time out the request if it hasn't completed
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  #--
4
- # Copyright 2013-2014 DataStax, Inc.
4
+ # Copyright 2013-2015 DataStax, Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  #--
4
- # Copyright 2013-2014 DataStax, Inc.
4
+ # Copyright 2013-2015 DataStax, Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  #--
4
- # Copyright 2013-2014 DataStax, Inc.
4
+ # Copyright 2013-2015 DataStax, Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -55,39 +55,24 @@ module Cassandra
55
55
  @statements = []
56
56
  end
57
57
 
58
- # @!method add(statement, args)
59
58
  # Adds a statement to this batch.
60
59
  #
61
60
  # @param statement [String, Cassandra::Statements::Simple,
62
61
  # Cassandra::Statements::Prepared, Cassandra::Statements::Bound]
63
62
  # statement to add.
64
- # @param args [Array] positional arguments to bind, must contain
65
- # the same number of parameters as the number of positional (`?`)
66
- # markers in the original CQL passed to {Cassandra::Session#prepare}
63
+ # @param args [Array, Hash] (nil) positional or named arguments to bind,
64
+ # must contain the same number of parameters as the number of positional
65
+ # (`?`) or named (`:name`) markers in the CQL passed.
67
66
  #
68
- # @overload add(statement, *args)
69
- # Adds a statement to this batch using the deprecated splat-style way of
70
- # passing positional arguments
67
+ # @note Positional arguments for simple statements are only supported
68
+ # starting with Apache Cassandra 2.0 and above.
71
69
  #
72
- # @deprecated Please pass a single {Array} of positional arguments, the
73
- # `*args` style is deprecated.
74
- #
75
- # @param statement [String, Cassandra::Statements::Simple,
76
- # Cassandra::Statements::Prepared, Cassandra::Statements::Bound]
77
- # statement to add.
78
- # @param args [*Object] **this style of positional arguments is
79
- # deprecated, please pass a single {Array} instead** - arguments to
80
- # paramterized query or prepared statement
70
+ # @note Named arguments for simple statements are only supported
71
+ # starting with Apache Cassandra 2.1 and above.
81
72
  #
82
73
  # @return [self]
83
- def add(statement, *args)
84
- if args.one? && args.first.is_a?(::Array)
85
- args = args.first
86
- elsif !args.empty?
87
- ::Kernel.warn "[WARNING] Splat style (*args) positional arguments " \
88
- "are deprecated, pass an Array instead - called " \
89
- "from #{caller.first}"
90
- end
74
+ def add(statement, args = nil)
75
+ args ||= EMPTY_LIST
91
76
 
92
77
  case statement
93
78
  when String
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  #--
4
- # Copyright 2013-2014 DataStax, Inc.
4
+ # Copyright 2013-2015 DataStax, Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  #--
4
- # Copyright 2013-2014 DataStax, Inc.
4
+ # Copyright 2013-2015 DataStax, Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@
18
18
 
19
19
  module Cassandra
20
20
  module Statements
21
+ # A prepared statement is created by calling {Cassandra::Session#prepare}
22
+ # or {Cassandra::Session#prepare_async}.
21
23
  class Prepared
22
24
  include Statement
23
25
 
@@ -42,45 +44,36 @@ module Cassandra
42
44
  @schema = schema
43
45
  end
44
46
 
45
- # @!method bind(args)
46
47
  # Creates a statement bound with specific arguments
47
48
  #
48
- # @param args [Array] positional arguments to bind, must contain the same
49
- # number of parameters as the number of positional (`?`) markers in the
50
- # original CQL passed to {Cassandra::Session#prepare}
51
- #
52
- # @note Positional arguments are only supported on Apache Cassandra 2.1
53
- # and above.
54
- #
55
- # @overload bind(*args)
56
- # Creates a statement bound with specific arguments using the
57
- # deprecated splat-style way of passing positional arguments.
58
- #
59
- # @deprecated Please pass a single {Array} of positional arguments, the
60
- # `*args` style is deprecated.
61
- #
62
- # @param args [*Object] **this style of positional arguments is
63
- # deprecated, please pass a single {Array} instead** - positional
64
- # arguments to bind, must contain the same number of parameters as
65
- # the number of positional argument markers (`?`) in the CQL passed
66
- # to {Cassandra::Session#prepare}.
49
+ # @param args [Array, Hash] (nil) positional or named arguments to bind,
50
+ # must contain the same number of parameters as the number of positional
51
+ # (`?`) or named (`:name`) markers in the original CQL passed to
52
+ # {Cassandra::Session#prepare}
67
53
  #
68
54
  # @return [Cassandra::Statements::Bound] bound statement
69
- def bind(*args)
70
- if args.one? && args.first.is_a?(::Array)
71
- args = args.first
55
+ def bind(args = nil)
56
+ if args
57
+ Util.assert_instance_of_one_of([::Array, ::Hash], args) { "args must be an Array or a Hash, #{args.inspect} given" }
72
58
  else
73
- unless args.empty?
74
- ::Kernel.warn "[WARNING] Splat style (*args) positional " \
75
- "arguments are deprecated, pass an Array instead " \
76
- "- called from #{caller.first}"
77
- end
59
+ args = EMPTY_LIST
78
60
  end
79
61
 
80
- Util.assert_equal(@params_metadata.size, args.size) { "expecting exactly #{@params_metadata.size} bind parameters, #{args.size} given" }
62
+ if args.is_a?(Hash)
63
+ args = @params_metadata.map do |(_, _, name, type)|
64
+ unless args.has_key?(name)
65
+ name = name.to_sym
66
+ raise ::ArgumentError, "argument :#{name} must be present in #{args.inspect}, but isn't" unless args.has_key?(name)
67
+ end
68
+
69
+ args[name]
70
+ end
71
+ else
72
+ Util.assert_equal(@params_metadata.size, args.size) { "expecting exactly #{@params_metadata.size} bind parameters, #{args.size} given" }
73
+ end
81
74
 
82
75
  params_types = @params_metadata.each_with_index.map do |(_, _, name, type), i|
83
- Util.assert_type(type, args[i]) { "argument for #{name.inspect} must be #{type.inspect}, #{args[i]} given" }
76
+ Util.assert_type(type, args[i]) { "argument for #{name.inspect} must be #{type}, #{args[i]} given" }
84
77
  type
85
78
  end
86
79
 
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  #--
4
- # Copyright 2013-2014 DataStax, Inc.
4
+ # Copyright 2013-2015 DataStax, Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -29,45 +29,39 @@ module Cassandra
29
29
  # @private
30
30
  attr_reader :params_types
31
31
 
32
- # @!method initialize(cql, params)
32
+ # @private
33
+ attr_reader :params_names
34
+
33
35
  # @param cql [String] a cql statement
34
- # @param params [Array] positional arguments for the query
36
+ # @param params [Array] (nil) positional arguments for the query
35
37
  #
36
- # @note Positional arguments for simple statements are only supported on
38
+ # @note Positional arguments for simple statements are only supported
37
39
  # starting with Apache Cassandra 2.0 and above.
38
40
  #
39
- # @overload initialize(cql, *params)
40
- # Uses the deprecated splat-style way of passing positional arguments.
41
- #
42
- # @deprecated Please pass a single {Array} of positional arguments, the
43
- # `*params` style is deprecated.
44
- #
45
- # @param cql [String] a cql statement
46
- # @param params [*Object] **this style of positional arguments is
47
- # deprecated, please pass a single {Array} instead** - positional
48
- # arguments for the query
41
+ # @note Named arguments for simple statements are only supported
42
+ # starting with Apache Cassandra 2.1 and above.
49
43
  #
50
44
  # @raise [ArgumentError] if cql statement given is not a String
51
- def initialize(cql, *params)
52
- unless cql.is_a?(::String)
53
- raise ::ArgumentError, "cql must be a string, #{cql.inspect} given"
54
- end
45
+ def initialize(cql, params = nil)
46
+ Util.assert_instance_of(::String, cql) { "cql must be a string, #{cql.inspect} given" }
55
47
 
56
- if params.one? && params.first.is_a?(::Array)
57
- params = params.first
58
- else
59
- unless params.empty?
60
- ::Kernel.warn "[WARNING] Splat style (*params) positional " \
61
- "arguments are deprecated, pass an Array instead " \
62
- "- called from #{caller.first}"
48
+ params ||= EMPTY_LIST
49
+
50
+ if params.is_a?(::Hash)
51
+ params_names = []
52
+ params = params.each_with_object([]) do |(name, value), params|
53
+ params_names << name
54
+ params << value
63
55
  end
56
+ else
57
+ Util.assert_instance_of(::Array, params) { "params must be an Array or a Hash, #{params.inspect} given" }
58
+ params_names = EMPTY_LIST
64
59
  end
65
60
 
66
- params_types = params.map {|value| guess_type(value)}
67
-
68
61
  @cql = cql
69
62
  @params = params
70
- @params_types = params_types
63
+ @params_types = params.map {|value| Util.guess_type(value)}
64
+ @params_names = params_names
71
65
  end
72
66
 
73
67
  # @return [String] a CLI-friendly simple statement representation
@@ -84,44 +78,6 @@ module Cassandra
84
78
  end
85
79
 
86
80
  alias :== :eql?
87
-
88
- private
89
-
90
- # @private
91
- @@type_guesses = {
92
- ::String => :varchar,
93
- ::Fixnum => :bigint,
94
- ::Float => :double,
95
- ::Bignum => :varint,
96
- ::BigDecimal => :decimal,
97
- ::TrueClass => :boolean,
98
- ::FalseClass => :boolean,
99
- ::NilClass => :bigint,
100
- Uuid => :uuid,
101
- TimeUuid => :uuid,
102
- ::IPAddr => :inet,
103
- ::Time => :timestamp,
104
- ::Hash => :map,
105
- ::Array => :list,
106
- ::Set => :set,
107
- }.freeze
108
-
109
- def guess_type(value)
110
- type = @@type_guesses[value.class]
111
-
112
- raise ::ArgumentError, "Unable to guess the type of the argument: #{value.inspect}" unless type
113
-
114
- if type == :map
115
- pair = value.first
116
- [type, guess_type(pair[0]), guess_type(pair[1])]
117
- elsif type == :list
118
- [type, guess_type(value.first)]
119
- elsif type == :set
120
- [type, guess_type(value.first)]
121
- else
122
- type
123
- end
124
- end
125
81
  end
126
82
  end
127
83
  end