Module: SQB::Ordering

Included in:
Delete, Select
Defined in:
lib/sqb/ordering.rb

Constant Summary collapse

VALID_ORDERS =
['ASC', 'DESC']

Instance Method Summary collapse

Instance Method Details

#no_order!Object

Remove all ordering for this query



34
35
36
# File 'lib/sqb/ordering.rb', line 34

def no_order!
  @orders = []
end

#order(column, direction = nil) ⇒ Query

Add an order column

Parameters:

  • column (String, Symbol, Hash)
  • direction (String) (defaults to: nil)

    ‘ASC’ or ‘DESC’ (default ‘ASC’)

Returns:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sqb/ordering.rb', line 11

def order(column, direction = nil)
  direction = direction ? direction.to_s.upcase : 'ASC'

  unless VALID_ORDERS.include?(direction)
    raise InvalidOrderDirectionError, "Invalid order direction #{direction}"
  end

  @orders ||= []

  with_table_and_column(column) do |table, column|
    @orders << [escape_and_join(table, column), direction].join(' ')
  end

  self
end

#order!(*args) ⇒ Object

Add an order replacing all previous ones



28
29
30
31
# File 'lib/sqb/ordering.rb', line 28

def order!(*args)
  @orders = []
  order(*args)
end