Skip to content

Common Patterns

Alex edited this page Jul 29, 2018 · 2 revisions

SQL operator

Structure:

  • Array
    • first element - Symbol that begins from _ - :_nullif
    • arguments
[:_count, [:_nullif, :row, [:_params, 'mrgl']]] # TODO: param?
# => ['COUNT(NULLIF(row, $1))', ['mrgl']]

Detailed Operators description.

Columns

Possible structures:

  • String
    • if it locted in the Hash with table alias, table alias will be added to it
    • if there is no table alias it will be returned to the builder as it is.
  • Symbol - just parsed to string
  • Hash
    • key - table alias||name
    • value - columns
  • SQL operator
  • Sub Query
  • Array - holder of any possible structures
['name', {u: ['role', 'u.phone, m.rating', :field]}]
# => 'name, u.role, u.phone, m.rating, u.field'

Detailed Select description

Tables

Possible structures:

  • String || Symbol
  • SQL operator
  • Array
    • if there is no Array inside it will be joined structure.join ' '
    • Array with any tables structure.
['users u', [:_values, ...], [:masters, :m]]
'users u, (VALUES...), masters m'

Conditions

Not finished yet... Column can be described as :u__name => 'u.name' or :name Possible structures:

  • String passes as it is
  • Hash represent sql = or IN if value is Array. TODO: IS in case of nil or false
    • Symbol - column name
    • Array - only as value! will be placed in querry params ($1).
    • String - will be placed in querry params
  • Array
    • first element - Symbol operator representation, by default :and
      • eq - =
      • or, and
      • in
      • not
      • like, ilike
      • reg - ~, reg_i - ~*, reg_f - !~, reg_fi - !~*
    • arguments
  • Array - holder of any possible structures
[[:not, 'z = 13'],
 [:or, 'mrgl = 2', 'rgl = 22'],
 [:or, 'rgl = 12', 'zgl = lol']]
# => 'NOT z = 13 AND (mrgl = 2 OR rgl = 22) AND (rgl = 12 OR zgl = lol)'
[{ role: 'manager', id: [0, 1, 153] },
 [:not, [:like, :u__name, 'Aeonax']]]
#=> 'role = $1 AND id IN ($2) AND NOT u.name LIKE $3', ['manager', [0, 1, 153], 'Aeonax']

Sub Query

Possible structures:

  • Constructor instance
  • Hash with :select value