queryverse / QuerySQLite.jl
1
# Dummy row
2
struct SourceRow{Source}
3 1
    source::Source
4
    table_name::Symbol
5
end
6

7
# The model row pass will build and modify a model of a row
8
function model_row(call)
9 1
    arguments, keywords = split_call(call)
10 1
    model_row_call(arguments...; keywords...)
11
end
12

13
# Very few functions modify the row model, so leave the row unchanged by default
14
function model_row_call(arbitrary_function, iterator, arguments...)
15 1
    model_row(iterator)
16
end
17

18
# Create a model row when getproperty is called on a Database
19
function get_column(source_row, column_name)
20 0
    SourceCode(source_row.source,
21
        Expr(:call, getproperty, source_row, column_name)
22
    )
23
end
24

25
function model_row_call(::typeof(getproperty), source_tables::Database, table_name)
26 1
    source = get_source(source_tables)
27 1
    column_names = get_column_names(source, table_name)
28 1
    source_row = SourceRow(source, table_name)
29 1
    NamedTuple{column_names}(
30
        map(column_names) do column_name
31
        get_column(source_row, column_name)
32
    end
33
    )
34
end
35

36
# Map is the only function which directly modifies the model row
37
function model_row_call(::typeof(QueryOperators.map), iterator, call, call_expression)
38 1
    call(model_row(iterator))
39
end
40

41
# Grouped rows have their own dedicated model type
42
struct GroupRow{Group,Row}
43 1
    group::Group
44
    row::Row
45
end
46

47
function QueryOperators.key(group_of_rows::GroupRow)
48 1
    getfield(group_of_rows, :group)
49
end
50

51
function getproperty(group_of_rows::GroupRow, column_name::Symbol)
52 1
    getproperty(getfield(group_of_rows, :row), column_name)
53
end
54

55
function length(group_of_rows::GroupRow)
56 0
    length(first(getfield(group_of_rows, :row)))
57
end
58

59
function model_row_call(::typeof(QueryOperators.groupby), ungrouped, group_function, group_function_expression, map_selector, map_function_expression)
60 1
    model = map_selector(model_row(ungrouped))
61 1
    GroupRow(group_function(model), model)
62
end

Read our documentation on viewing source code .

Loading