module Clear::SQL::Query::OrderBy

Overview

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

Direct including types

Defined in:

clear/sql/query/order_by.cr

Instance Method Summary

Instance Method Detail

def clear_order_bys #

Remove all order by clauses


[View source]
def 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

[View source]
def 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

[View source]
def 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

[View source]
def order_by(**tuple) #

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

[View source]
def 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


[View source]