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
98 99 100 101 |
# File 'lib/cassandra_migrations/cassandra/queries.rb', line 98 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 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, ={}) query_string = "SELECT #{[:projection] || '*'} FROM #{table}" [:secondary_options] ||= {} 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 #Secondary options if [:page_size] [:secondary_options][:page_size] = [:page_size] end if [:consistency] [:secondary_options][:consistency] = [:consistency] end if [:trace] [:secondary_options][:trace] = [:trace] end if [:timeout] [:secondary_options][:timeout] = [:timeout] end if [:serial_consistency] [:secondary_options][:serial_consistency] = [:serial_consistency] end if [:paging_state] [:secondary_options][:paging_state] = [:paging_state] end if [:arguments] [:secondary_options][:arguments] = [:arguments] end if [:secondary_options].length > 0 execute(query_string, [: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, ={}) 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 |