Sha256: 1f7386ae90f48c8d40f0792c63609c0cc078400539d3af6dcbc94073b890b5a2

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

#
# Copyright (c) 2019-present, Blue Marble Payroll, LLC
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#

require_relative 'query/field'
require_relative 'query/filters'
require_relative 'query/sorter'

module Dbee
  # This class is an abstration of a simplified SQL expression.  In DB terms:
  # - fields are the SELECT
  # - sorters are the ORDER BY
  # - limit is the TAKE
  # - filters are the WHERE
  class Query
    acts_as_hashable

    class NoFieldsError < StandardError; end

    attr_reader :fields, :filters, :limit, :sorters

    def initialize(fields:, filters: [], limit: nil, sorters: [])
      @fields = Field.array(fields)

      # If no fields were passed into a query then we will have no data to return.
      # Let's raise a hard error here and let the consumer deal with it since this may
      # have implications in downstream SQL generators.
      raise NoFieldsError if @fields.empty?

      @filters  = Filters.array(filters)
      @limit    = limit.to_s.empty? ? nil : limit.to_i
      @sorters  = Sorter.array(sorters)

      freeze
    end

    def ==(other)
      other.fields == fields &&
        other.filters == filters &&
        other.limit == limit &&
        other.sorters == sorters
    end
    alias eql? ==
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dbee-1.0.1 lib/dbee/query.rb
dbee-1.0.0 lib/dbee/query.rb
dbee-1.0.0.pre.alpha.3 lib/dbee/query.rb