/
order.jl
52 lines (40 loc) · 1.12 KB
/
order.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# ORDER BY clause.
mutable struct OrderClause <: AbstractSQLClause
over::Union{SQLClause, Nothing}
by::Vector{SQLClause}
OrderClause(;
over = nothing,
by = SQLClause[]) =
new(over, by)
end
OrderClause(by...; over = nothing) =
OrderClause(over = over, by = SQLClause[by...])
"""
ORDER(; over = nothing, by = [])
ORDER(by...; over = nothing)
An `ORDER BY` clause.
# Examples
```jldoctest
julia> c = FROM(:person) |>
ORDER(:year_of_birth) |>
SELECT(:person_id);
julia> print(render(c))
SELECT "person_id"
FROM "person"
ORDER BY "year_of_birth"
```
"""
ORDER(args...; kws...) =
OrderClause(args...; kws...) |> SQLClause
dissect(scr::Symbol, ::typeof(ORDER), pats::Vector{Any}) =
dissect(scr, OrderClause, pats)
function PrettyPrinting.quoteof(c::OrderClause, ctx::QuoteContext)
ex = Expr(:call, nameof(ORDER))
append!(ex.args, quoteof(c.by, ctx))
if c.over !== nothing
ex = Expr(:call, :|>, quoteof(c.over, ctx), ex)
end
ex
end
rebase(c::OrderClause, c′) =
OrderClause(over = rebase(c.over, c′), by = c.by)