Sha256: 21d55ff8abef0ec9b6049744e50a67b5cd3da0fac95fd9dda2313e012566dec6

Contents?: true

Size: 1.29 KB

Versions: 11

Compression:

Stored size: 1.29 KB

Contents

require_relative '../prepared_statement'

module ActiveHouse
  module Querying
    module Where
      extend ActiveSupport::Concern

      included do
        private :build_where_query_part
      end

      def initial_values
        super.merge where: []
      end

      def build_where_query_part
        "WHERE\n" + values[:where].join(" AND\n") unless values[:where].empty?
      end

      def where!(*conditions)
        formatted_conditions = format_where_clauses(conditions)
        values[:where] = (values[:where] + formatted_conditions).uniq
        self
      end

      def where_not!(*conditions)
        formatted_conditions = format_where_clauses(conditions)
        negative_condition = "NOT (#{formatted_conditions.join(' AND ')})"
        values[:where] = (values[:where] + [negative_condition]).uniq
        self
      end

      def where(*conditions)
        dup.where!(*conditions)
      end

      def where_not(*conditions)
        dup.where_not!(*conditions)
      end

      def format_where_clauses(conditions)
        raise ArgumentError, 'wrong number of arguments' if conditions.empty?

        return [ActiveHouse::PreparedStatement.prepare_sql(*conditions)] if conditions.size > 1

        ActiveHouse::PreparedStatement.build_condition(conditions.first)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
active_house-0.8.0 lib/active_house/querying/where.rb
active_house-0.7.0 lib/active_house/querying/where.rb
active_house-0.6.6 lib/active_house/querying/where.rb
active_house-0.6.5 lib/active_house/querying/where.rb
active_house-0.6.4 lib/active_house/querying/where.rb
active_house-0.6.3 lib/active_house/querying/where.rb
active_house-0.6.2 lib/active_house/querying/where.rb
active_house-0.6.1 lib/active_house/querying/where.rb
active_house-0.6.0 lib/active_house/querying/where.rb
active_house-0.5.1 lib/active_house/querying/where.rb
active_house-0.5.0 lib/active_house/querying/where.rb