cassandra-cql 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,7 +16,7 @@ limitations under the License.
16
16
 
17
17
  module CassandraCQL; end;
18
18
  unless CassandraCQL.respond_to?(:CASSANDRA_VERSION)
19
- require "cassandra-cql/1.1"
19
+ require "cassandra-cql/1.2"
20
20
  end
21
21
 
22
22
  here = File.expand_path(File.dirname(__FILE__))
@@ -36,12 +36,10 @@ module CassandraCQL
36
36
  @cql_version = @options[:cql_version]
37
37
  @servers = servers
38
38
  connect!
39
- execute("USE #{@keyspace}")
40
39
  end
41
40
 
42
41
  def use_cql3?
43
- (@cql_version.nil? || @cql_version.split('.').first.to_i >= 3) &&
44
- CassandraCQL::Thrift::Client.method_defined?(:execute_cql3_query)
42
+ @use_cql3
45
43
  end
46
44
 
47
45
  def connect!
@@ -53,10 +51,18 @@ module CassandraCQL
53
51
 
54
52
  obj = self
55
53
  @connection.add_callback(:post_connect) do
56
- @connection.set_cql_version(@cql_version) if @cql_version
54
+ if @connection.describe_version >= '19.35.0' && (!@cql_version || @cql_version >= '3.0.0')
55
+ @use_cql3 = true
56
+ elsif @cql_version
57
+ @use_cql3 = false
58
+ @connection.set_cql_version(@cql_version)
59
+ else
60
+ @use_cql3 = false
61
+ end
57
62
  @connection.login(@auth_request) if @auth_request
58
63
  execute("USE #{@keyspace}")
59
64
  end
65
+ @connection.connect!
60
66
  end
61
67
 
62
68
  def disconnect!
@@ -72,19 +72,20 @@ module CassandraCQL
72
72
  def self.quote(obj, use_cql3=false)
73
73
  if obj.kind_of?(Array)
74
74
  obj.map { |member| quote(member, use_cql3) }.join(",")
75
+ elsif obj.kind_of?(Hash)
76
+ "{"+obj.map{ |key,val| "#{quote(cast_to_cql(key), use_cql3)}:#{quote(cast_to_cql(val), use_cql3)}" }.join(',')+"}"
75
77
  elsif obj.kind_of?(String)
76
78
  "'" + obj + "'"
77
- elsif obj.kind_of?(BigDecimal) and (!use_cql3 or CASSANDRA_VERSION.to_f < 1.2)
79
+ elsif obj.kind_of?(BigDecimal) and !use_cql3
78
80
  "'" + obj.to_s + "'"
79
81
  elsif obj.kind_of?(Numeric)
80
82
  obj.to_s
81
83
  elsif obj.kind_of?(SimpleUUID::UUID)
82
84
  obj.to_guid
83
- #elsif obj.kind_of?(TrueClass) or obj.kind_of?(FalseClass) and use_cql3 and CASSANDRA_VERSION.to_f == 1.2
84
- # obj.to_s
85
- elsif obj.kind_of?(TrueClass) or obj.kind_of?(FalseClass)
86
- #"'" + obj.to_s + "'"
85
+ elsif obj.kind_of?(TrueClass) or obj.kind_of?(FalseClass) and use_cql3
87
86
  obj.to_s
87
+ elsif obj.kind_of?(TrueClass) or obj.kind_of?(FalseClass)
88
+ "'" + obj.to_s + "'"
88
89
  else
89
90
  raise Error::UnescapableObject, "Unable to escape object of class #{obj.class}"
90
91
  end
@@ -93,6 +94,8 @@ module CassandraCQL
93
94
  def self.cast_to_cql(obj)
94
95
  if obj.kind_of?(Array)
95
96
  obj.map { |member| cast_to_cql(member) }
97
+ elsif obj.kind_of?(Hash)
98
+ obj
96
99
  elsif obj.kind_of?(Numeric)
97
100
  obj
98
101
  elsif obj.kind_of?(Date)
@@ -111,7 +114,7 @@ module CassandraCQL
111
114
  RUBY_VERSION >= "1.9" ? escape(obj.to_s.dup.force_encoding('ASCII-8BIT')) : escape(obj.to_s.dup)
112
115
  end
113
116
  end
114
-
117
+
115
118
  def self.sanitize(statement, bind_vars=[], use_cql3=false)
116
119
  # If there are no bind variables, return the statement unaltered
117
120
  return statement if bind_vars.empty?
@@ -120,7 +123,7 @@ module CassandraCQL
120
123
  expected_bind_vars = statement.count("?")
121
124
 
122
125
  raise Error::InvalidBindVariable, "Wrong number of bound variables (statement expected #{expected_bind_vars}, was #{bind_vars.size})" if expected_bind_vars != bind_vars.size
123
-
126
+
124
127
  statement.gsub(/\?/) {
125
128
  quote(cast_to_cql(bind_vars.shift), use_cql3)
126
129
  }
@@ -23,5 +23,7 @@ module CassandraCQL
23
23
  raise Error::CastException.new("Unable to convert bytes to Date", value)
24
24
  end
25
25
  end
26
+
27
+ TimestampType = DateType
26
28
  end
27
29
  end
@@ -15,5 +15,5 @@ limitations under the License.
15
15
  =end
16
16
 
17
17
  module CassandraCQL
18
- VERSION = "1.2.1"
18
+ VERSION = "1.2.2"
19
19
  end
@@ -25,14 +25,14 @@ describe "basic methods" do
25
25
  @row.column_values.size.should eq(@row.column_names.size)
26
26
  end
27
27
  end
28
-
28
+
29
29
  context "columns" do
30
30
  it "should equal the number of columns" do
31
31
  @row.column_names.size.should eq(@row.column_values.size)
32
32
  @row.columns.should eq(@row.column_names.size)
33
33
  end
34
34
  end
35
-
35
+
36
36
  context "checking casting" do
37
37
  it "should return column_values for to_a" do
38
38
  @row.to_a.should eq(@row.column_values)
@@ -83,7 +83,8 @@ describe "basic methods" do
83
83
  key text PRIMARY KEY,
84
84
  mylist LIST <int>,
85
85
  myset SET <varchar>,
86
- mymap MAP <uuid,timestamp>
86
+ mymap MAP <uuid,timestamp>,
87
+ myflattenmap MAP <uuid,timestamp>
87
88
  )
88
89
  CQL
89
90
 
@@ -93,8 +94,8 @@ describe "basic methods" do
93
94
  }
94
95
 
95
96
  @connection.execute(
96
- "INSERT INTO collections (key, mylist, myset, mymap) VALUES (?, [?], {?}, {?:?,?:?})",
97
- 'test', [1, 2, 3], ['some', 'set'], *@map.to_a.flatten)
97
+ "INSERT INTO collections (key, mylist, myset, mymap, myflattenmap) VALUES (?, [?], {?}, ?, {?:?,?:?})",
98
+ 'test', [1, 2, 3], ['some', 'set'], @map, *@map.to_a.flatten)
98
99
 
99
100
  @row = @connection.execute("SELECT * FROM collections WHERE key=?", "test").fetch
100
101
  end
@@ -10,9 +10,9 @@ Bundler.setup(:default, :test)
10
10
  require 'yaml'
11
11
  require 'rspec'
12
12
 
13
- CASSANDRA_VERSION = ENV['CASSANDRA_VERSION'] || '1.1' unless defined?(CASSANDRA_VERSION)
14
- CQL_VERSION = ENV['CQL_VERSION'] || '2.0.0'
15
- USE_CQL3 = CQL_VERSION.split('.').first.to_i == 3
13
+ CASSANDRA_VERSION = ENV['CASSANDRA_VERSION'] || '1.2' unless defined?(CASSANDRA_VERSION)
14
+ CQL_VERSION = ENV['CQL_VERSION'] || '3.0.0'
15
+ USE_CQL3 = CQL_VERSION.split('.').first.to_i == 3 && CASSANDRA_VERSION >= '1.2'
16
16
 
17
17
  require "cassandra-cql/#{CASSANDRA_VERSION}"
18
18
 
@@ -30,13 +30,13 @@ describe "execute" do
30
30
  stmt.execute([], :compression => true).should be_nil
31
31
  @connection.keyspace.should eq('system')
32
32
  end
33
-
33
+
34
34
  it "should set keyspace to nil when deleting keyspace" do
35
35
  @connection.execute("DROP KEYSPACE #{@connection.keyspace}").should be_nil
36
36
  @connection.keyspace.should be_nil
37
37
  end
38
38
  end
39
-
39
+
40
40
  context "when performing void-returning column_family operations" do
41
41
  before(:each) do
42
42
  @connection = setup_cassandra_connection
@@ -70,13 +70,21 @@ describe "escape" do
70
70
  end
71
71
 
72
72
  describe "quote" do
73
-
73
+
74
74
  context "with a string" do
75
75
  it "should add quotes" do
76
76
  Statement.quote("test", USE_CQL3).should eq("'test'")
77
77
  end
78
78
  end
79
79
 
80
+ context "with a JSON string" do
81
+ #regression test, conflict with Map collection
82
+ it "should add quotes" do
83
+ json = '{"foo":"test", "bar":1}'
84
+ Statement.quote(json, USE_CQL3).should eq("'#{json}'")
85
+ end
86
+ end
87
+
80
88
  context "with an integer" do
81
89
  it "should not add quotes" do
82
90
  Statement.quote(15, USE_CQL3).should eq("15")
@@ -92,24 +100,31 @@ describe "quote" do
92
100
 
93
101
  context "with a big decimal" do
94
102
  let :big_decimal do
95
- BigDecimal.new('129182739481237481341234123411.1029348102934810293481039')
103
+ BigDecimal.new('129182739481237481341234123411.1029348102934810293481039')
96
104
  end
97
- let :result do
105
+ let :result do
98
106
  '0.1291827394812374813412341234111029348102934810293481039E30'
99
107
  end
100
-
108
+
101
109
  it "should add quotes", cql_version: '2.0.0' do
102
110
  Statement.quote(BigDecimal.new(big_decimal), USE_CQL3).should eq("'#{result}'")
103
111
  end
104
112
 
105
113
  it "should not add quotes", cql_version: '3.0.0' do
106
114
  Statement.quote(big_decimal, USE_CQL3).should eq(result)
107
- end
115
+ end
108
116
  end
109
117
  context "with a boolean" do
110
- it "should not add quotes" do
111
- Statement.quote(true, USE_CQL3).should eq("true")
112
- Statement.quote(false, USE_CQL3).should eq("false")
118
+ if USE_CQL3
119
+ it "should not add quotes" do
120
+ Statement.quote(true, USE_CQL3).should eq("true")
121
+ Statement.quote(false, USE_CQL3).should eq("false")
122
+ end
123
+ else
124
+ it "should add quotes" do
125
+ Statement.quote(true, USE_CQL3).should eq("'true'")
126
+ Statement.quote(false, USE_CQL3).should eq("'false'")
127
+ end
113
128
  end
114
129
  end
115
130
 
@@ -131,7 +146,7 @@ describe "cast_to_cql" do
131
146
  Time.at(long / 1000.0).to_f.should be_within(0.001).of(ts.to_f)
132
147
  end
133
148
  end
134
-
149
+
135
150
  context "with a Date object" do
136
151
  it "should return a corresponding Time object" do
137
152
  date = Date.today << 1
@@ -139,15 +154,15 @@ describe "cast_to_cql" do
139
154
  str.should eq(date.strftime('%Y-%m-%d'))
140
155
  end
141
156
  end
142
-
157
+
143
158
  context "with a Fixnum object" do
144
159
  it "should return the same object" do
145
160
  Statement.cast_to_cql(15).should eq(15)
146
161
  end
147
162
  end
148
-
163
+
149
164
  context "with a UUID object" do
150
- it "should return the a guid" do
165
+ it "should return the same object" do
151
166
  uuid = UUID.new
152
167
  guid = Statement.cast_to_cql(uuid)
153
168
  guid.should eq(uuid)
@@ -155,7 +170,7 @@ describe "cast_to_cql" do
155
170
  end
156
171
 
157
172
  context "with a SimpleUUID::UUID object" do
158
- it "should return the guid" do
173
+ it "should return the same object" do
159
174
  uuid = SimpleUUID::UUID.new
160
175
  guid = Statement.cast_to_cql(uuid)
161
176
  guid.should eq(uuid)
@@ -170,7 +185,7 @@ describe "cast_to_cql" do
170
185
  new_str.object_id.should_not eq(str.object_id)
171
186
  end
172
187
  end
173
-
188
+
174
189
  context "with a String with quotes" do
175
190
  it "should return a quoted version" do
176
191
  str = "This is a ' string"
@@ -179,7 +194,7 @@ describe "cast_to_cql" do
179
194
  new_str.should eq(Statement.escape(str))
180
195
  end
181
196
  end
182
-
197
+
183
198
  context "with binary data" do
184
199
  it "should return an unpacked version" do
185
200
  bytes = "binary\x00"
@@ -189,14 +204,14 @@ describe "cast_to_cql" do
189
204
  [new_data].pack('H*').should eq(bytes)
190
205
  end
191
206
  end
192
-
207
+
193
208
  context "with an array of Fixnums" do
194
209
  it "should equal itself" do
195
210
  arr = [1, 2, 3]
196
211
  Statement.cast_to_cql(arr).should eq(arr)
197
212
  end
198
213
  end
199
-
214
+
200
215
  context "with an array of Strings" do
201
216
  it "should return quoted versions of itself" do
202
217
  arr = ["test", "'"]
@@ -213,7 +228,7 @@ describe "sanitize" do
213
228
  Statement.sanitize("use keyspace", [], USE_CQL3).should eq("use keyspace")
214
229
  end
215
230
  end
216
-
231
+
217
232
  context "when expecting bind vars" do
218
233
  it "should raise an exception with bind variable mismatch" do
219
234
  expect {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cassandra-cql
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-22 00:00:00.000000000 Z
12
+ date: 2014-01-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler