Module: CassandraMigrations::Cassandra::Queries
- Included in:
- CassandraMigrations::Cassandra
- Defined in:
- lib/cassandra_migrations/cassandra/queries.rb
Instance Method Summary collapse
- #delete!(table, selection, options = {}) ⇒ Object
- #select(table, options = {}) ⇒ Object
- #truncate!(table) ⇒ Object
- #update!(table, selection, value_hash, options = {}) ⇒ Object
- #write!(table, value_hash, options = {}) ⇒ Object
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, ={}) [:projection] = [:projection].to_s + ' ' if [:projection] execute("DELETE #{[: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, ={}) query_string = "SELECT #{[:projection] || '*'} FROM #{table}" if [:selection] query_string << " WHERE #{[:selection]}" end if [:order_by] query_string << " ORDER BY #{[:order_by]}" end if [:limit] query_string << " LIMIT #{[:limit]}" end if [: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, ={}) set_terms = [] value_hash.each do |column, value| set_terms << "#{column} = #{to_cql_value(column, value, table, )}" end query = "UPDATE #{table}" if [:ttl] query += " USING TTL #{[: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, ={}) columns = [] values = [] value_hash.each do |column, value| columns << column.to_s values << to_cql_value(column, value, table, ) end query = "INSERT INTO #{table} (#{columns.join(', ')}) VALUES (#{values.join(', ')})" if [:ttl] query += " USING TTL #{[:ttl]}" end execute(query) end |