Class: CassandraObject::Adapters::CassandraSchemalessAdapter::QueryBuilder
- Inherits:
-
Object
- Object
- CassandraObject::Adapters::CassandraSchemalessAdapter::QueryBuilder
- Defined in:
- lib/cassandra_object/adapters/cassandra_schemaless_adapter.rb
Instance Method Summary collapse
-
#initialize(adapter, scope) ⇒ QueryBuilder
constructor
A new instance of QueryBuilder.
- #select_string ⇒ Object
- #to_query_async ⇒ Object
- #where_string_async(id) ⇒ Object
Constructor Details
#initialize(adapter, scope) ⇒ QueryBuilder
Returns a new instance of QueryBuilder.
9 10 11 12 |
# File 'lib/cassandra_object/adapters/cassandra_schemaless_adapter.rb', line 9 def initialize(adapter, scope) @adapter = adapter @scope = scope end |
Instance Method Details
#select_string ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/cassandra_object/adapters/cassandra_schemaless_adapter.rb', line 14 def select_string selected_values = @scope.select_values.select { |sv| sv == :column1 || sv == :values } if selected_values.any? (['KEY'] | selected_values) * ',' else '*' end end |
#to_query_async ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/cassandra_object/adapters/cassandra_schemaless_adapter.rb', line 23 def to_query_async # empty ids return nil if !@scope.id_values.present? && !@scope.where_values.present? && !@scope.is_all && !@scope.limit_value.present? if @scope.id_values.empty? str = [ "SELECT #{select_string} FROM #{@scope.klass.column_family}", where_string_async(nil) ] str << 'ALLOW FILTERING' if @scope.klass.allow_filtering return [] << str.delete_if(&:blank?) * ' ' end @scope.id_values.map do |id| str = [ "SELECT #{select_string} FROM #{@scope.klass.column_family}", where_string_async(id) ] str << 'ALLOW FILTERING' if @scope.klass.allow_filtering str.delete_if(&:blank?) * ' ' end end |
#where_string_async(id) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/cassandra_object/adapters/cassandra_schemaless_adapter.rb', line 45 def where_string_async(id) conditions = [] conditions << "#{@adapter.primary_key_column} = '#{id}'" if !id.nil? select_values = @scope.select_values.select { |sv| sv != :column1 } if select_values.size > 0 select_str = select_values.size > 1 ? "column1 IN (#{select_values.map { |sv| '?' }.join(',')})" : 'column1 = ?' conditions << select_str end conditions += @scope.where_values.select.each_with_index { |_, i| i.even? } return conditions.any? ? "WHERE #{conditions.join(' AND ')}" : nil end |