Class: OrderQuery::SQL::Where

Inherits:
Object
  • Object
show all
Defined in:
lib/order_query/sql/where.rb

Overview

Builds where clause for searching around a record in an order space.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(point) ⇒ Where

Returns a new instance of Where.

Parameters:



10
11
12
13
# File 'lib/order_query/sql/where.rb', line 10

def initialize(point)
  @point   = point
  @columns = point.space.columns
end

Instance Attribute Details

#pointObject (readonly)

Returns the value of attribute point.



7
8
9
# File 'lib/order_query/sql/where.rb', line 7

def point
  @point
end

Instance Method Details

#build(side, strict = true) ⇒ query, parameters

Join column pairs with OR, and nest within each other with AND

Parameters:

  • side (:before or :after)

Returns:

  • (query, parameters)

    WHERE columns matching records strictly before / after this one.

    sales < 5 OR sales = 5 AND (

    invoice < 3 OR
    invoices = 3 AND (
      ... ))
    


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/order_query/sql/where.rb', line 25

def build(side, strict = true)
  # generate pairs of terms such as sales < 5, sales = 5
  terms = @columns.map.with_index do |col, i|
    be_strict = i != @columns.size - 1 ? true : strict
    [where_side(col, side, be_strict), where_tie(col)].reject do |x|
      x == WHERE_IDENTITY
    end
  end
  # group pairwise with OR, and nest with AND
  query = foldr_terms terms.map { |pair| join_terms 'OR', *pair }, 'AND'
  if ::OrderQuery.wrap_top_level_or
    # wrap in a redundant AND clause for performance
    query = wrap_top_level_or query, terms, side
  end
  query
end