module

Clear::SQL::Query::OrderBy

Encode for:

ORDER BY expression [ASC | DESC | USING operator] [NULLS FIRST | NULLS LAST];

Current implementation:

[x] Multiple Order by clauses [x] ASC/DESC [x] NULLS FIRST / NULLS LAST [ ] NOT IMPLEMENTED: USING OPERATOR

Instance methods

clear_order_bys

Remove all order by clauses

Source
order_by(__tuple : NamedTuple)

Add multiple ORDER BY clause using a tuple:

query = Clear::SQL.select.from("users").order_by(id: :desc, name: {:asc, :nulls_last})
query.to_sql # > SELECT * FROM users ORDER BY "id" DESC, "name" ASC NULLS LAST
Source
order_by(expression : Symbol, direction : Symbol = :asc, nulls : Symbol | Nil = nil)

Add one ORDER BY clause

query = Clear::SQL.select.from("users").order_by(:id, :desc, nulls_last)
query.to_sql # > SELECT * FROM users ORDER BY "id" DESC NULLS LAST
Source
order_by(expression : String, direction : Symbol = :asc, nulls : Symbol | Nil = nil)

Add one ORDER BY clause

query = Clear::SQL.select.from("users").order_by(:id, :desc, nulls_last)
query.to_sql # > SELECT * FROM users ORDER BY "id" DESC NULLS LAST
Source
order_by

Add multiple ORDER BY clause using a tuple:

query = Clear::SQL.select.from("users").order_by(id: :desc, name: {:asc, :nulls_last})
query.to_sql # > SELECT * FROM users ORDER BY "id" DESC, "name" ASC NULLS LAST
Source
reverse_order_by

Flip over all order bys by switching the ASC direction to DESC and the NULLS FIRST to NULLS LAST

query = Clear::SQL.select.from("users").order_by(id: :desc, name: :asc, company: {:asc, :nulls_last})
query.reverse_order_by
query.to_sql # SELECT * FROM users ORDER BY "id" ASC, "name" DESC, "company" DESC NULLS FIRST

return self

Source

Nested types