Sha256: d805aeb6d2c6a663eb0001ae011dff854ee48a32b6fa88f59f92ccc7939ab766

Contents?: true

Size: 1.2 KB

Versions: 8

Compression:

Stored size: 1.2 KB

Contents

require 'cassanity/operator'
require 'cassanity/operators/eq'
require 'cassanity/operators/gt'
require 'cassanity/operators/gte'
require 'cassanity/operators/lt'
require 'cassanity/operators/lte'
require 'cassanity/range'

module Cassanity
  module ArgumentGenerators
    class WhereClause

      # Internal
      def call(args = {})
        where = args[:where]
        cql = ''
        return [cql] if where.nil? || where.empty?

        variables, wheres = [], []

        where.each do |key, value|
          case value
          when Array
            wheres << "#{key} IN (?)"
            variables << value
          when Range
            start, finish = value.begin, value.end
            end_operator = value.exclude_end? ? '<' : '<='
            wheres << "#{key} >= ?"
            wheres << "#{key} #{end_operator} ?"
            variables << start
            variables << finish
          when Cassanity::Operator
            wheres << "#{key} #{value.symbol} ?"
            variables << value.value
          else
            wheres << "\"#{key}\" = ?"
            variables << value
          end
        end

        cql << " WHERE #{wheres.join(' AND ')}"

        [cql, *variables]
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
cassanity-0.6.0 lib/cassanity/argument_generators/where_clause.rb
cassanity-0.6.0.beta5 lib/cassanity/argument_generators/where_clause.rb
cassanity-0.6.0.beta4 lib/cassanity/argument_generators/where_clause.rb
cassanity-0.6.0.beta3 lib/cassanity/argument_generators/where_clause.rb
cassanity-0.6.0.beta2 lib/cassanity/argument_generators/where_clause.rb
cassanity-0.6.0.beta1 lib/cassanity/argument_generators/where_clause.rb
cassanity-0.5.1 lib/cassanity/argument_generators/where_clause.rb
cassanity-0.5.0 lib/cassanity/argument_generators/where_clause.rb