Class: CassandraCQL::Statement

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

Direct Known Subclasses

V08::Statement, V10::Statement, V11::Statement

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cassandra-cql/statement.rb', line 84

def self.cast_to_cql(obj)
  if obj.kind_of?(Array)
    obj.map { |member| cast_to_cql(member) }
  elsif obj.kind_of?(Fixnum) or obj.kind_of?(Float)
    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?(UUID)
    obj.to_guid
  # 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) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cassandra-cql/statement.rb', line 72

def self.quote(obj)
  if obj.kind_of?(Array)
    obj.map { |member| quote(member) }.join(",")
  elsif obj.kind_of?(String)
    "'" + obj + "'"
  elsif obj.kind_of?(Fixnum) or obj.kind_of?(Float)
    obj
  else
    raise Error::UnescapableObject, "Unable to escape object of class #{obj.class}"
  end
end

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



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cassandra-cql/statement.rb', line 104

def self.sanitize(statement, bind_vars=[])
  bind_vars = bind_vars.dup
  expected_bind_vars = statement.count("?")

  return statement if expected_bind_vars == 0 and bind_vars.empty?
  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))
  }
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)
  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