Module: CassandraMigrations::Cassandra::Queries

Included in:
CassandraMigrations::Cassandra
Defined in:
lib/cassandra_migrations/cassandra/queries.rb

Instance Method Summary collapse

Instance Method Details

#delete!(table, selection, options = {}) ⇒ Object



98
99
100
101
# File 'lib/cassandra_migrations/cassandra/queries.rb', line 98

def delete!(table, selection, options={})
  options[:projection] = options[:projection].to_s + ' ' if options[:projection]
  execute("DELETE #{options[:projection]}FROM #{table} WHERE #{selection}")
end

#select(table, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cassandra_migrations/cassandra/queries.rb', line 42

def select(table, options={})
  query_string = "SELECT #{options[:projection] || '*'} FROM #{table}"
  options[:secondary_options] ||= {}

  if options[:selection]
    query_string << " WHERE #{options[:selection]}"
  end

  if options[:order_by]
    query_string << " ORDER BY #{options[:order_by]}"
  end

  if options[:limit]
    query_string << " LIMIT #{options[:limit]}"
  end

  if options[:allow_filtering]
    query_string << " ALLOW FILTERING"
  end

  #Secondary options
  if options[:page_size]
    options[:secondary_options][:page_size] = options[:page_size]
  end

  if options[:consistency]
    options[:secondary_options][:consistency] = options[:consistency]
  end

  if options[:trace]
    options[:secondary_options][:trace] = options[:trace]
  end

  if options[:timeout]
    options[:secondary_options][:timeout] = options[:timeout]
  end

  if options[:serial_consistency]
    options[:secondary_options][:serial_consistency] = options[:serial_consistency]
  end

  if options[:paging_state]
    options[:secondary_options][:paging_state] = options[:paging_state]
  end

  if options[:arguments]
    options[:secondary_options][:arguments] = options[:arguments]
  end

  if options[:secondary_options].length > 0
    execute(query_string, options[:secondary_options])
  else
    execute(query_string)
  end
end

#truncate!(table) ⇒ Object



103
104
105
# File 'lib/cassandra_migrations/cassandra/queries.rb', line 103

def truncate!(table)
  execute("TRUNCATE #{table}")
end

#update!(table, selection, value_hash, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cassandra_migrations/cassandra/queries.rb', line 25

def update!(table, selection, value_hash, options={})
  set_terms = []
  value_hash.each do |column, value|
    set_terms << "#{column} = #{to_cql_value(column, value, table, options)}"
  end

  query = "UPDATE #{table}"

  if options[:ttl]
    query += " USING TTL #{options[:ttl]}"
  end

  query += " SET #{set_terms.join(', ')} WHERE #{selection}"

  execute(query)
end

#write!(table, value_hash, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cassandra_migrations/cassandra/queries.rb', line 7

def write!(table, value_hash, options={})
  columns = []
  values = []

  value_hash.each do |column, value|
    columns << column.to_s
    values << to_cql_value(column, value, table, options)
  end

  query = "INSERT INTO #{table} (#{columns.join(', ')}) VALUES (#{values.join(', ')})"

  if options[:ttl]
    query += " USING TTL #{options[:ttl]}"
  end

  execute(query)
end