/
as.jl
80 lines (59 loc) · 2.07 KB
/
as.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# AS wrapper.
mutable struct AsNode <: AbstractSQLNode
over::Union{SQLNode, Nothing}
name::Symbol
AsNode(;
over = nothing,
name::Union{Symbol, AbstractString}) =
new(over, Symbol(name))
end
AsNode(name; over = nothing) =
AsNode(over = over, name = name)
"""
As(; over = nothing, name)
As(name; over = nothing)
name => over
In a scalar context, `As` specifies the name of the output column. When
applied to tabular data, `As` wraps the data in a nested record.
The arrow operator (`=>`) is a shorthand notation for `As`.
# Examples
*Show all patient IDs.*
```jldoctest
julia> person = SQLTable(:person, columns = [:person_id, :year_of_birth]);
julia> q = From(:person) |> Select(:id => Get.person_id);
julia> print(render(q, tables = [person]))
SELECT "person_1"."person_id" AS "id"
FROM "person" AS "person_1"
```
*Show all patients together with their state of residence.*
```jldoctest
julia> person = SQLTable(:person, columns = [:person_id, :year_of_birth, :location_id]);
julia> location = SQLTable(:location, columns = [:location_id, :state]);
julia> q = From(:person) |>
Join(From(:location) |> As(:location),
on = Get.location_id .== Get.location.location_id) |>
Select(Get.person_id, Get.location.state);
julia> print(render(q, tables = [person, location]))
SELECT
"person_1"."person_id",
"location_1"."state"
FROM "person" AS "person_1"
JOIN "location" AS "location_1" ON ("person_1"."location_id" = "location_1"."location_id")
```
"""
As(args...; kws...) =
AsNode(args...; kws...) |> SQLNode
const funsql_as = As
dissect(scr::Symbol, ::typeof(As), pats::Vector{Any}) =
dissect(scr, AsNode, pats)
Base.convert(::Type{AbstractSQLNode}, p::Pair{<:Union{Symbol, AbstractString}}) =
AsNode(name = first(p), over = convert(SQLNode, last(p)))
function PrettyPrinting.quoteof(n::AsNode, ctx::QuoteContext)
ex = Expr(:call, nameof(As), quoteof(n.name))
if n.over !== nothing
ex = Expr(:call, :|>, quoteof(n.over, ctx), ex)
end
ex
end
label(n::AsNode) =
n.name