default to cassandra 1.2, and use cql3 transport methods automatically

also, don't execute USE in initialize; the after connect callback will
do that; it would start a cql query before it knew which version
to speak

third, fix quoting for cassandra 1.1

finally, DateType was renamed to TimestampType, so alias the
cast class
This commit is contained in:
Cody Cutrer 2013-10-16 13:03:31 -06:00
parent d0d11b8188
commit b73ccb982e
6 changed files with 28 additions and 14 deletions

View File

@ -16,7 +16,7 @@ limitations under the License.
module CassandraCQL; end;
unless CassandraCQL.respond_to?(:CASSANDRA_VERSION)
require "cassandra-cql/1.1"
require "cassandra-cql/1.2"
end
here = File.expand_path(File.dirname(__FILE__))

View File

@ -36,12 +36,10 @@ module CassandraCQL
@cql_version = @options[:cql_version]
@servers = servers
connect!
execute("USE #{@keyspace}")
end
def use_cql3?
(@cql_version.nil? || @cql_version.split('.').first.to_i >= 3) &&
CassandraCQL::Thrift::Client.method_defined?(:execute_cql3_query)
@use_cql3
end
def connect!
@ -53,10 +51,18 @@ module CassandraCQL
obj = self
@connection.add_callback(:post_connect) do
@connection.set_cql_version(@cql_version) if @cql_version
if @connection.describe_version >= '19.35.0' && (!@cql_version || @cql_version >= '3.0.0')
@use_cql3 = true
elsif @cql_version
@use_cql3 = false
@connection.set_cql_version(@cql_version)
else
@use_cql3 = false
end
@connection.login(@auth_request) if @auth_request
execute("USE #{@keyspace}")
end
@connection.connect!
end
def disconnect!

View File

@ -74,17 +74,16 @@ module CassandraCQL
obj.map { |member| quote(member, use_cql3) }.join(",")
elsif obj.kind_of?(String)
"'" + obj + "'"
elsif obj.kind_of?(BigDecimal) and (!use_cql3 or CASSANDRA_VERSION.to_f < 1.2)
elsif obj.kind_of?(BigDecimal) and !use_cql3
"'" + obj.to_s + "'"
elsif obj.kind_of?(Numeric)
obj.to_s
elsif obj.kind_of?(SimpleUUID::UUID)
obj.to_guid
#elsif obj.kind_of?(TrueClass) or obj.kind_of?(FalseClass) and use_cql3 and CASSANDRA_VERSION.to_f == 1.2
# obj.to_s
elsif obj.kind_of?(TrueClass) or obj.kind_of?(FalseClass)
#"'" + obj.to_s + "'"
elsif obj.kind_of?(TrueClass) or obj.kind_of?(FalseClass) and use_cql3
obj.to_s
elsif obj.kind_of?(TrueClass) or obj.kind_of?(FalseClass)
"'" + obj.to_s + "'"
else
raise Error::UnescapableObject, "Unable to escape object of class #{obj.class}"
end

View File

@ -23,5 +23,7 @@ module CassandraCQL
raise Error::CastException.new("Unable to convert bytes to Date", value)
end
end
TimestampType = DateType
end
end

View File

@ -12,7 +12,7 @@ require 'rspec'
CASSANDRA_VERSION = ENV['CASSANDRA_VERSION'] || '1.1' unless defined?(CASSANDRA_VERSION)
CQL_VERSION = ENV['CQL_VERSION'] || '2.0.0'
USE_CQL3 = CQL_VERSION.split('.').first.to_i == 3
USE_CQL3 = CQL_VERSION.split('.').first.to_i == 3 && CASSANDRA_VERSION >= '1.2'
require "cassandra-cql/#{CASSANDRA_VERSION}"

View File

@ -107,9 +107,16 @@ describe "quote" do
end
end
context "with a boolean" do
it "should not add quotes" do
Statement.quote(true, USE_CQL3).should eq("true")
Statement.quote(false, USE_CQL3).should eq("false")
if USE_CQL3
it "should not add quotes" do
Statement.quote(true, USE_CQL3).should eq("true")
Statement.quote(false, USE_CQL3).should eq("false")
end
else
it "should add quotes" do
Statement.quote(true, USE_CQL3).should eq("'true'")
Statement.quote(false, USE_CQL3).should eq("'false'")
end
end
end