class CassandraStore::Relation
Attributes
distinct_value[RW]
limit_value[RW]
order_values[RW]
select_values[RW]
target[RW]
where_cql_values[RW]
where_values[RW]
Public Class Methods
new(target:)
click to toggle source
# File lib/cassandra_store/relation.rb, line 4 def initialize(target:) self.target = target end
Public Instance Methods
all()
click to toggle source
# File lib/cassandra_store/relation.rb, line 8 def all fresh end
count()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# File lib/cassandra_store/relation.rb, line 60 def distinct fresh.tap do |relation| relation.distinct_value = true end end
find_each(options = {}) { |record| ... }
click to toggle source
# File lib/cassandra_store/relation.rb, line 72 def find_each(options = {}) return enum_for(:find_each, options) unless block_given? find_in_batches options do |batch| batch.each do |record| yield record end end end
find_in_batches(batch_size: 1_000) { |records| ... }
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/cassandra_store/relation.rb, line 46 def limit(n) fresh.tap do |relation| relation.limit_value = n end end
order(hash = {})
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/cassandra_store/relation.rb, line 124 def to_a @records ||= find_each.to_a end
update_all(string_or_hash)
click to toggle source
# 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 = {})
click to toggle source
# 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 = {})
click to toggle source
# 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
Private Instance Methods
each_page(cql, page_size:) { |result| ... }
click to toggle source
# File lib/cassandra_store/relation.rb, line 146 def each_page(cql, page_size:) result = target.execute(cql, page_size: page_size) while result yield result result = result.next_page end end
fresh()
click to toggle source
# File lib/cassandra_store/relation.rb, line 140 def fresh dup.tap do |relation| relation.instance_variable_set(:@records, nil) end end
limit_clause()
click to toggle source
# File lib/cassandra_store/relation.rb, line 184 def limit_clause (limit_value ? "LIMIT #{limit_value.to_i}" : "").to_s end
load_record(row)
click to toggle source
# File lib/cassandra_store/relation.rb, line 130 def load_record(row) target.new.tap do |record| record.persisted! row.each do |key, value| record.write_raw_attribute(key, value) end end end
order_clause()
click to toggle source
# File lib/cassandra_store/relation.rb, line 180 def order_clause (order_values.presence ? "ORDER BY #{order_values.map { |column, value| "#{target.quote_column_name column} #{value}" }.join(", ")}" : "").to_s end
select_clause()
click to toggle source
# File lib/cassandra_store/relation.rb, line 156 def select_clause "#{distinct_value ? "DISTINCT" : ""} #{select_values.presence ? select_values.join(", ") : "*"}" end
where_clause()
click to toggle source
# File lib/cassandra_store/relation.rb, line 160 def where_clause return if where_values.blank? && where_cql_values.blank? constraints = [] Array(where_values).each do |hash| hash.each do |column, value| constraints << if value.is_a?(Array) || value.is_a?(Range) "#{target.quote_column_name column} IN (#{value.to_a.map { |v| target.quote_value v }.join(", ")})" else "#{target.quote_column_name column} = #{target.quote_value value}" end end end constraints += Array(where_cql_values) "WHERE #{constraints.join(" AND ")}" end