Skip to content

Commit c41958a

Browse files
committed
simplify API
1 parent a0c7864 commit c41958a

2 files changed

Lines changed: 36 additions & 31 deletions

File tree

src/QuerySQLite.jl

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ struct OutsideCode{Outside}
4747
code::Expr
4848
end
4949

50+
get_code(outside_code::OutsideCode) = outside_code.code
51+
5052
"""
5153
get_table_names(outside)::Tuple{Symbol}
5254
@@ -65,15 +67,29 @@ get_column_names(outside::DB, table_name) =
6567
as_symbols(columns(outside, String(table_name)).name)
6668
export get_column_names
6769

68-
"""
69-
abstract type OutsideTables{Outside} end
7070

71-
`Outside` must support [`get_table_names`](@ref), [`get_column_names`](@ref), and [`submit_to`](@ref).
72-
"""
7371
struct OutsideTables{Outside}
7472
outside::Outside
7573
end
76-
export OutsideTables
74+
75+
get_table(outside, table_name::Symbol) =
76+
OutsideCode(
77+
outside,
78+
Expr(:call, getproperty, OutsideTables(outside), table_name)
79+
)
80+
81+
"""
82+
get_tables(outside)
83+
84+
`outside` must support [`get_table_names`](@ref).
85+
"""
86+
function get_tables(outside)
87+
table_names = get_table_names(outside)
88+
NamedTuple{table_names}(
89+
partial_map(get_table, outside, table_names)
90+
)
91+
end
92+
export get_tables
7793

7894
struct OutsideTable{Outside}
7995
outside::Outside
@@ -88,21 +104,6 @@ end
88104
OutsideRow(outside_table::OutsideTable) =
89105
OutsideRow(outside_table.outside, outside_table.table_name)
90106

91-
make_outside_table(outside_tables, table_name) =
92-
OutsideCode(
93-
outside_tables.outside,
94-
Expr(:call, getproperty, outside_tables, table_name)
95-
)
96-
97-
function NamedTuple(outside_tables::OutsideTables)
98-
table_names = get_table_names(outside_tables.outside)
99-
NamedTuple{table_names}(partial_map(
100-
make_outside_table,
101-
outside_tables,
102-
table_names
103-
))
104-
end
105-
106107
function unwrap!(outsides, outside_code::OutsideCode)
107108
push!(outsides, outside_code.outside)
108109
outside_code.code
@@ -196,9 +197,12 @@ translate_call(::typeof(drop), iterator, number) =
196197

197198
change_row(::typeof(getproperty), outside_tables::OutsideTables, table_name) =
198199
model_row(OutsideTable(outside_tables.outside, table_name))
200+
translate_call(::typeof(getproperty), outside_tables::OutsideTables, table_name) =
201+
translate(OutsideTable(outside_tables.outside, table_name))
199202
translate_call(::typeof(getproperty), outside_row::OutsideRow, column_name) =
200203
column_name
201204

205+
202206
"""
203207
if_else(switch, yes, no)
204208
@@ -267,40 +271,41 @@ translate_call(::typeof(QueryOperators.filter), iterator, call, call_expression)
267271
string(
268272
translate(iterator),
269273
" WHERE ",
270-
translate(call(model_row(iterator)).code)
274+
translate(get_code(call(model_row(iterator)).code))
271275
)
272276

273277
@code_instead QueryOperators.orderby OutsideCode Any Expr
274278
translate_call(::typeof(QueryOperators.orderby), unordered, key_function, key_function_expression) = string(
275279
translate(unordered),
276280
" ORDER BY ",
277-
translate(key_function(model_row(unordered)).code)
281+
translate(get_code(key_function(model_row(unordered))))
278282
)
279283
@code_instead QueryOperators.thenby OutsideCode Any Expr
280284
translate_call(::typeof(QueryOperators.thenby), unordered, key_function, key_function_expression) = string(
281285
translate(unordered),
282286
", ",
283-
translate(key_function(model_row(unordered)).code)
287+
translate(get_code(key_function(model_row(unordered))))
284288
)
285289
@code_instead QueryOperators.orderby_descending OutsideCode Any Expr
286290
translate_call(::typeof(QueryOperators.orderby_descending), unordered, key_function, key_function_expression) = string(
287291
translate(unordered),
288292
" ORDER BY ",
289-
translate(key_function(model_row(unordered)).code),
293+
translate(get_code(key_function(model_row(unordered)))),
290294
" DESC"
291295
)
292296
@code_instead QueryOperators.thenby_descending OutsideCode Any Expr
293297
translate_call(::typeof(QueryOperators.thenby_descending), unordered, key_function, key_function_expression) = string(
294298
translate(unordered),
295299
", ",
296-
translate(key_function(model_row(unordered)).code),
300+
translate(get_code(key_function(model_row(unordered)))),
297301
"DESC"
298302
)
299303

300304
@code_instead QueryOperators.map OutsideCode Any Expr
301-
change_row(::typeof(QueryOperators.map), iterator, call, call_expression) = call(model_row(iterator))
305+
change_row(::typeof(QueryOperators.map), iterator, call, call_expression) =
306+
call(model_row(iterator))
302307
select_as(new_name_model::Pair{Symbol, <: OutsideCode}) =
303-
string(translate(new_name_model.second.code), " AS ", new_name_model.first)
308+
string(translate(get_code(new_name_model.second)), " AS ", new_name_model.first)
304309
function translate_call(::typeof(QueryOperators.map), select_table, call, call_expression)
305310
if @capture select_table $getproperty(outsidetables_OutsideTables, name_)
306311
string(
@@ -409,7 +414,7 @@ model_row(code::Expr) =
409414
end
410415

411416
translate(something) = something
412-
417+
translate(outside_row::OutsideRow) = outside_row.table_name
413418
translate(code::Expr) =
414419
if @capture code call_(arguments__)
415420
translate_call(if call === ifelse
@@ -517,7 +522,7 @@ name_and_type(handle, column_number, nullable = true, strict_types = true) =
517522

518523
function getiterator(outside_code::OutsideCode)
519524
# TODO REVIEW
520-
statement = Stmt(outside_code.outside, translate(outside_code.code))
525+
statement = Stmt(outside_code.outside, String(translate(outside_code.code)))
521526
# bind!(statement, values)
522527
status = execute!(statement)
523528
handle = statement.handle

test/runtests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using SQLite: DB
55
using QueryTables
66

77
filename = joinpath(@__DIR__, "Chinook_Sqlite.sqlite")
8-
database = NamedTuple(OutsideTables(DB(filename)))
8+
database = get_tables(DB(filename))
99

1010
@testset "QuerySQLite" begin
1111

@@ -23,7 +23,7 @@ database = NamedTuple(OutsideTables(DB(filename)))
2323
@test database.Customer |>
2424
@map({_.City}) |>
2525
@unique() |>
26-
collect |>
26+
collect |>
2727
length == 53
2828

2929
@test database.Track |>

0 commit comments

Comments
 (0)