Class: OrderQuery::SQL::Where

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

Overview

Build 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:



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

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

Instance Attribute Details

#pointObject (readonly)

Returns the value of attribute point.



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

def point
  @point
end

Instance Method Details

#build(side) ⇒ 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 (
      ... ))
    


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/order_query/sql/where.rb', line 22

def build(side)
  # generate pairs of terms such as sales < 5, sales = 5
  terms = @columns.map { |col|
    [where_side(col, side, true), where_tie(col)].reject { |x| x == WHERE_IDENTITY }
  }
  # group pairwise with OR, and nest with AND
  query = foldr_terms terms.map { |pair| join_terms 'OR'.freeze, *pair }, 'AND'.freeze
  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