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



64
65
66
67
# File 'lib/cassandra_migrations/cassandra/queries.rb', line 64

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
# File 'lib/cassandra_migrations/cassandra/queries.rb', line 42

def select(table, options={})
  query_string = "SELECT #{options[:projection] || '*'} FROM #{table}"
  
  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
    
  execute(query_string)
end

#truncate!(table) ⇒ Object



69
70
71
# File 'lib/cassandra_migrations/cassandra/queries.rb', line 69

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