/
limit.jl
79 lines (62 loc) · 2 KB
/
limit.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
# Truncating.
mutable struct LimitNode <: TabularNode
over::Union{SQLNode, Nothing}
offset::Union{Int, Nothing}
limit::Union{Int, Nothing}
LimitNode(; over = nothing, offset = nothing, limit = nothing) =
new(over, offset, limit)
end
LimitNode(limit; over = nothing, offset = nothing) =
LimitNode(over = over, offset = offset, limit = limit)
LimitNode(offset, limit; over = nothing) =
LimitNode(over = over, offset = offset, limit = limit)
LimitNode(range::UnitRange; over = nothing) =
LimitNode(over = over, offset = first(range) - 1, limit = length(range))
"""
Limit(; over = nothing, offset = nothing, limit = nothing)
Limit(limit; over = nothing, offset = nothing)
Limit(offset, limit; over = nothing)
Limit(start:stop; over = nothing)
The `Limit` node skips the first `offset` rows and then emits the next `limit`
rows.
To make the output deterministic, `Limit` must be applied directly after
an [`Order`](@ref) node.
The `Limit` node is translated to a query with a `LIMIT` or a `FETCH` clause:
```sql
SELECT ...
FROM \$over
OFFSET \$offset ROWS
FETCH NEXT \$limit ROWS ONLY
```
# Examples
*Show the oldest patient.*
```jldoctest
julia> person = SQLTable(:person, columns = [:person_id, :year_of_birth]);
julia> q = From(:person) |>
Order(Get.year_of_birth) |>
Limit(1);
julia> print(render(q, tables = [person]))
SELECT
"person_1"."person_id",
"person_1"."year_of_birth"
FROM "person" AS "person_1"
ORDER BY "person_1"."year_of_birth"
FETCH FIRST 1 ROW ONLY
```
"""
Limit(args...; kws...) =
LimitNode(args...; kws...) |> SQLNode
const funsql_limit = Limit
dissect(scr::Symbol, ::typeof(Limit), pats::Vector{Any}) =
dissect(scr, LimitNode, pats)
function PrettyPrinting.quoteof(n::LimitNode, ctx::QuoteContext)
ex = Expr(:call, nameof(Limit))
if n.offset !== nothing
push!(ex.args, n.offset)
end
push!(ex.args, n.limit)
if n.over !== nothing
ex = Expr(:call, :|>, quoteof(n.over, ctx), ex)
end
ex
end