Module: Card::Query::SqlStatement::Order

Included in:
Card::Query::SqlStatement
Defined in:
lib/card/query/sql_statement/order.rb

Overview

build ORDER BY clause

Instance Method Summary collapse

Instance Method Details

#orderObject



19
20
21
22
23
# File 'lib/card/query/sql_statement/order.rb', line 19

def order
  full_syntax do
    "ORDER BY #{order_directives.join ', '}"
  end
end

#order_asObject



47
48
49
50
51
52
# File 'lib/card/query/sql_statement/order.rb', line 47

def order_as
  field = yield
  return field unless (as = @mods[:sort_as])

  "CAST(#{field} AS #{cast_type(safe_sql(as))})"
end

#order_configObject



62
63
64
# File 'lib/card/query/sql_statement/order.rb', line 62

def order_config
  @mods[:sort].blank? ? "update" : @mods[:sort]
end

#order_dir(order_key) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/card/query/sql_statement/order.rb', line 54

def order_dir order_key
  if @mods[:dir].blank?
    DEFAULT_ORDER_DIRS[order_key.to_sym] || "asc"
  else
    safe_sql @mods[:dir]
  end
end

#order_directive(order_key) ⇒ Object



31
32
33
34
35
# File 'lib/card/query/sql_statement/order.rb', line 31

def order_directive order_key
  field = order_field order_key
  @fields += ", #{field}"
  "#{field} #{order_dir order_key}"
end

#order_directivesObject



25
26
27
28
29
# File 'lib/card/query/sql_statement/order.rb', line 25

def order_directives
  Array.wrap(order_config).map do |order_key|
    order_directive order_key
  end
end

#order_field(order_key) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/card/query/sql_statement/order.rb', line 37

def order_field order_key
  order_as do
    if (field = ORDER_MAP[order_key])
      "#{@query.table_alias}.#{field}"
    else
      safe_sql order_key
    end
  end
end