Class: CassandraStore::Relation
- Inherits:
-
Object
- Object
- CassandraStore::Relation
- Defined in:
- lib/cassandra_store/relation.rb
Instance Attribute Summary collapse
-
#distinct_value ⇒ Object
Returns the value of attribute distinct_value.
-
#limit_value ⇒ Object
Returns the value of attribute limit_value.
-
#order_values ⇒ Object
Returns the value of attribute order_values.
-
#select_values ⇒ Object
Returns the value of attribute select_values.
-
#target ⇒ Object
Returns the value of attribute target.
-
#where_cql_values ⇒ Object
Returns the value of attribute where_cql_values.
-
#where_values ⇒ Object
Returns the value of attribute where_values.
Instance Method Summary collapse
- #all ⇒ Object
- #count ⇒ Object
- #delete_all ⇒ Object
- #delete_in_batches ⇒ Object
- #distinct ⇒ Object
- #find_each(options = {}) ⇒ Object
- #find_in_batches(batch_size: 1_000) ⇒ Object
- #first(n = 1) ⇒ Object
-
#initialize(target:) ⇒ Relation
constructor
A new instance of Relation.
- #limit(n) ⇒ Object
- #order(hash = {}) ⇒ Object
- #select(*columns) ⇒ Object
- #to_a ⇒ Object
- #update_all(string_or_hash) ⇒ Object
- #where(hash = {}) ⇒ Object
- #where_cql(string, args = {}) ⇒ Object
Constructor Details
#initialize(target:) ⇒ Relation
Returns a new instance of Relation.
4 5 6 |
# File 'lib/cassandra_store/relation.rb', line 4 def initialize(target:) self.target = target end |
Instance Attribute Details
#distinct_value ⇒ Object
Returns the value of attribute distinct_value.
2 3 4 |
# File 'lib/cassandra_store/relation.rb', line 2 def distinct_value @distinct_value end |
#limit_value ⇒ Object
Returns the value of attribute limit_value.
2 3 4 |
# File 'lib/cassandra_store/relation.rb', line 2 def limit_value @limit_value end |
#order_values ⇒ Object
Returns the value of attribute order_values.
2 3 4 |
# File 'lib/cassandra_store/relation.rb', line 2 def order_values @order_values end |
#select_values ⇒ Object
Returns the value of attribute select_values.
2 3 4 |
# File 'lib/cassandra_store/relation.rb', line 2 def select_values @select_values end |
#target ⇒ Object
Returns the value of attribute target.
2 3 4 |
# File 'lib/cassandra_store/relation.rb', line 2 def target @target end |
#where_cql_values ⇒ Object
Returns the value of attribute where_cql_values.
2 3 4 |
# File 'lib/cassandra_store/relation.rb', line 2 def where_cql_values @where_cql_values end |
#where_values ⇒ Object
Returns the value of attribute where_values.
2 3 4 |
# File 'lib/cassandra_store/relation.rb', line 2 def where_values @where_values end |
Instance Method Details
#all ⇒ Object
8 9 10 |
# File 'lib/cassandra_store/relation.rb', line 8 def all fresh end |
#count ⇒ Object
118 119 120 121 122 |
# File 'lib/cassandra_store/relation.rb', line 118 def count cql = "SELECT COUNT(*) FROM #{target.quote_table_name target.table_name} #{where_clause}" target.execute(cql).first["count"] end |
#delete_all ⇒ Object
100 101 102 103 104 |
# File 'lib/cassandra_store/relation.rb', line 100 def delete_all target.execute("DELETE FROM #{target.quote_table_name target.table_name} #{where_clause}") true end |
#delete_in_batches ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/cassandra_store/relation.rb', line 106 def delete_in_batches find_in_batches do |records| records.each do |record| where_clause = target.key_columns.map { |column, _| "#{target.quote_column_name column} = #{target.quote_value record.read_raw_attribute(column)}" }.join(" AND ") target.execute "DELETE FROM #{target.quote_table_name target.table_name} WHERE #{where_clause}" end end true end |
#distinct ⇒ Object
60 61 62 63 64 |
# File 'lib/cassandra_store/relation.rb', line 60 def distinct fresh.tap do |relation| relation.distinct_value = true end end |
#find_each(options = {}) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/cassandra_store/relation.rb', line 72 def find_each( = {}) return enum_for(:find_each, ) unless block_given? find_in_batches do |batch| batch.each do |record| yield record end end end |
#find_in_batches(batch_size: 1_000) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/cassandra_store/relation.rb', line 82 def find_in_batches(batch_size: 1_000) return enum_for(:find_in_batches, batch_size: batch_size) unless block_given? each_page "SELECT #{select_clause} FROM #{target.quote_table_name target.table_name} #{where_clause} #{order_clause} #{limit_clause}", page_size: batch_size do |result| records = [] result.each do |row| records << if select_values.present? row else load_record(row) end end yield(records) unless records.empty? end end |
#first(n = 1) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/cassandra_store/relation.rb', line 52 def first(n = 1) result = limit(n).to_a return result.first if n == 1 result end |
#limit(n) ⇒ Object
46 47 48 49 50 |
# File 'lib/cassandra_store/relation.rb', line 46 def limit(n) fresh.tap do |relation| relation.limit_value = n end end |
#order(hash = {}) ⇒ Object
40 41 42 43 44 |
# File 'lib/cassandra_store/relation.rb', line 40 def order(hash = {}) fresh.tap do |relation| relation.order_values = (relation.order_values || {}).merge(hash) end end |
#select(*columns) ⇒ Object
66 67 68 69 70 |
# File 'lib/cassandra_store/relation.rb', line 66 def select(*columns) fresh.tap do |relation| relation.select_values = (relation.select_values || []) + columns end end |
#to_a ⇒ Object
124 125 126 |
# File 'lib/cassandra_store/relation.rb', line 124 def to_a @records ||= find_each.to_a end |
#update_all(string_or_hash) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/cassandra_store/relation.rb', line 30 def update_all(string_or_hash) if string_or_hash.is_a?(Hash) target.execute("UPDATE #{target.quote_table_name target.table_name} SET #{string_or_hash.map { |column, value| "#{target.quote_column_name column} = #{target.quote_value value}" }.join(", ")} #{where_clause}") else target.execute("UPDATE #{target.quote_table_name target.table_name} SET #{string_or_hash} #{where_clause}") end true end |
#where(hash = {}) ⇒ Object
12 13 14 15 16 |
# File 'lib/cassandra_store/relation.rb', line 12 def where(hash = {}) fresh.tap do |relation| relation.where_values = (relation.where_values || []) + [hash] end end |
#where_cql(string, args = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/cassandra_store/relation.rb', line 18 def where_cql(string, args = {}) fresh.tap do |relation| str = string args.each do |key, value| str.gsub!(":#{key}", target.quote_value(value)) end relation.where_cql_values = (relation.where_cql_values || []) + [str] end end |