Class: CassandraCQL::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra-cql/statement.rb

Constant Summary collapse

KS_CHANGE_RE =
/^use (\w+)/i
KS_DROP_RE =
/^drop keyspace (\w+)/i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handle, statement) ⇒ Statement

Returns a new instance of Statement.



30
31
32
33
# File 'lib/cassandra-cql/statement.rb', line 30

def initialize(handle, statement)
  @handle = handle
  prepare(statement)
end

Instance Attribute Details

#statementObject (readonly)

Returns the value of attribute statement.



28
29
30
# File 'lib/cassandra-cql/statement.rb', line 28

def statement
  @statement
end

Class Method Details

.cast_to_cql(obj) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cassandra-cql/statement.rb', line 93

def self.cast_to_cql(obj)
  if obj.kind_of?(Array)
    obj.map { |member| cast_to_cql(member) }
  elsif obj.kind_of?(Numeric)
    obj
  elsif obj.kind_of?(Date)
    obj.strftime('%Y-%m-%d')
  elsif obj.kind_of?(Time)
    (obj.to_f * 1000).to_i
  elsif obj.kind_of?(SimpleUUID::UUID)
    obj
  elsif obj.kind_of?(TrueClass) or obj.kind_of?(FalseClass)
    obj
  # There are corner cases where this is an invalid assumption but they are extremely rare.
  # The alternative is to make the user pack the data on their own .. let's not do that until we have to
  elsif obj.kind_of?(String) and Utility.binary_data?(obj)
    escape(obj.unpack('H*')[0])
  else
    RUBY_VERSION >= "1.9" ? escape(obj.to_s.dup.force_encoding('ASCII-8BIT')) : escape(obj.to_s.dup)
  end
end

.escape(obj) ⇒ Object



68
69
70
# File 'lib/cassandra-cql/statement.rb', line 68

def self.escape(obj)
  obj.gsub("'", "''")
end

.quote(obj, use_cql3 = false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cassandra-cql/statement.rb', line 72

def self.quote(obj, use_cql3=false)
  if obj.kind_of?(Array)
    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)
    "'" + 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 + "'"
    obj.to_s
  else
    raise Error::UnescapableObject, "Unable to escape object of class #{obj.class}"
  end
end

.sanitize(statement, bind_vars = [], use_cql3 = false) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cassandra-cql/statement.rb', line 115

def self.sanitize(statement, bind_vars=[], use_cql3=false)
  # If there are no bind variables, return the statement unaltered
  return statement if bind_vars.empty?

  bind_vars = bind_vars.dup
  expected_bind_vars = statement.count("?")

  raise Error::InvalidBindVariable, "Wrong number of bound variables (statement expected #{expected_bind_vars}, was #{bind_vars.size})" if expected_bind_vars != bind_vars.size

  statement.gsub(/\?/) {
    quote(cast_to_cql(bind_vars.shift), use_cql3)
  }
end

Instance Method Details

#execute(bind_vars = [], options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cassandra-cql/statement.rb', line 39

def execute(bind_vars=[], options={})
  sanitized_query = self.class.sanitize(@statement, bind_vars, @handle.use_cql3?)
  compression_type = CassandraCQL::Thrift::Compression::NONE
  if options[:compression]
    compression_type = CassandraCQL::Thrift::Compression::GZIP
    sanitized_query = Utility.compress(sanitized_query)
  end

  res = Result.new(@handle.execute_cql_query(sanitized_query, compression_type))

  # Change our keyspace if required
  if @statement =~ KS_CHANGE_RE
    @handle.keyspace = $1
  elsif @statement =~ KS_DROP_RE
    @handle.keyspace = nil
  end

  # We let ints be fetched for now because they'll probably be deprecated later
  if res.void?
    nil
  else
    res
  end
end

#finishObject



64
65
66
# File 'lib/cassandra-cql/statement.rb', line 64

def finish
  true
end

#prepare(statement) ⇒ Object



35
36
37
# File 'lib/cassandra-cql/statement.rb', line 35

def prepare(statement)
  @statement = statement
end