Class: Dbee::Query

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/dbee/query.rb,
lib/dbee/query/field.rb,
lib/dbee/query/filters.rb,
lib/dbee/query/sorters.rb,
lib/dbee/query/filters/base.rb,
lib/dbee/query/sorters/base.rb,
lib/dbee/query/filters/equals.rb,
lib/dbee/query/filters/contains.rb,
lib/dbee/query/filters/less_than.rb,
lib/dbee/query/sorters/ascending.rb,
lib/dbee/query/filters/not_equals.rb,
lib/dbee/query/sorters/descending.rb,
lib/dbee/query/filters/not_contain.rb,
lib/dbee/query/filters/starts_with.rb,
lib/dbee/query/filters/greater_than.rb,
lib/dbee/query/filters/not_start_with.rb,
lib/dbee/query/filters/less_than_or_equal_to.rb,
lib/dbee/query/filters/greater_than_or_equal_to.rb

Overview

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

Defined Under Namespace

Classes: Field, Filters, NoFieldsError, Sorters

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields:, filters: [], limit: nil, sorters: []) ⇒ Query

Returns a new instance of Query.

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dbee/query.rb', line 34

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).uniq
  @limit    = limit.to_s.empty? ? nil : limit.to_i
  @sorters  = Sorters.array(sorters).uniq

  freeze
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



26
27
28
# File 'lib/dbee/query.rb', line 26

def fields
  @fields
end

#filtersObject (readonly)

Returns the value of attribute filters.



26
27
28
# File 'lib/dbee/query.rb', line 26

def filters
  @filters
end

#limitObject (readonly)

Returns the value of attribute limit.



26
27
28
# File 'lib/dbee/query.rb', line 26

def limit
  @limit
end

#sortersObject (readonly)

Returns the value of attribute sorters.



26
27
28
# File 'lib/dbee/query.rb', line 26

def sorters
  @sorters
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



49
50
51
52
53
54
55
# File 'lib/dbee/query.rb', line 49

def ==(other)
  other.instance_of?(self.class) &&
    other.sorted_fields == sorted_fields &&
    other.sorted_filters == sorted_filters &&
    other.sorted_sorters == sorted_sorters &&
    other.limit == limit
end

#key_chainObject



58
59
60
# File 'lib/dbee/query.rb', line 58

def key_chain
  KeyChain.new(key_paths)
end