Sha256: d47e7ed6aa036a8cc42a6e959a5cdd5b5f62028c4369129ba00f5671ff149c5a

Contents?: true

Size: 1.08 KB

Versions: 11

Compression:

Stored size: 1.08 KB

Contents

require_relative '../prepared_statement'
require 'active_support/core_ext/array/wrap'

module ActiveHouse
  module Querying
    module Having
      extend ActiveSupport::Concern

      included do
        private :build_having_query_part, :format_having
      end

      def build_having_query_part
        "HAVING\n" + values[:having].join(" AND\n") unless values[:having].empty?
      end

      def initial_values
        super.merge having: []
      end

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

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

      def format_having(conditions)
        raise ArgumentError, 'wrong number of arguments' if conditions.empty?
        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/having.rb
active_house-0.7.0 lib/active_house/querying/having.rb
active_house-0.6.6 lib/active_house/querying/having.rb
active_house-0.6.5 lib/active_house/querying/having.rb
active_house-0.6.4 lib/active_house/querying/having.rb
active_house-0.6.3 lib/active_house/querying/having.rb
active_house-0.6.2 lib/active_house/querying/having.rb
active_house-0.6.1 lib/active_house/querying/having.rb
active_house-0.6.0 lib/active_house/querying/having.rb
active_house-0.5.1 lib/active_house/querying/having.rb
active_house-0.5.0 lib/active_house/querying/having.rb