Skip to content

Commit 1659496

Browse files
committed
merge OutsideTable and OutsideRow
1 parent c41958a commit 1659496

1 file changed

Lines changed: 38 additions & 49 deletions

File tree

src/QuerySQLite.jl

Lines changed: 38 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ partial_map(call, fixed, variables1, variables2) =
4141

4242
as_symbols(them) = map_unrolled(Symbol, (them...,))
4343

44-
4544
struct OutsideCode{Outside}
4645
outside::Outside
4746
code::Expr
@@ -96,30 +95,25 @@ struct OutsideTable{Outside}
9695
table_name::Symbol
9796
end
9897

99-
struct OutsideRow{Outside}
100-
outside::Outside
101-
table_name::Symbol
102-
end
103-
104-
OutsideRow(outside_table::OutsideTable) =
105-
OutsideRow(outside_table.outside, outside_table.table_name)
98+
OutsideTable(outside_table::OutsideTable) =
99+
OutsideTable(outside_table.outside, outside_table.table_name)
106100

107-
function unwrap!(outsides, outside_code::OutsideCode)
101+
function pop_outsides!(outsides, outside_code::OutsideCode)
108102
push!(outsides, outside_code.outside)
109103
outside_code.code
110104
end
111-
unwrap!(outsides, something) = something
112-
function one_outside(a_function, arguments...)
105+
pop_outsides!(outsides, something) = something
106+
function combine_outsides(a_function, arguments...)
113107
outsides = Set(Any[])
114-
unwrapped_arguments = partial_map(unwrap!, outsides, arguments)
108+
codes = partial_map(pop_outsides!, outsides, arguments)
115109
OutsideCode(
116110
if length(outsides) == 0
117111
error("No outside")
118112
elseif length(outsides) > 1
119113
error("Too many outsides")
120114
else
121115
first(outsides)
122-
end, Expr(:call, a_function, unwrapped_arguments...)
116+
end, Expr(:call, a_function, codes...)
123117
)
124118
end
125119

@@ -141,7 +135,7 @@ function code_instead(location, a_function, types...)
141135
Expr(:block,
142136
location,
143137
Expr(:call,
144-
one_outside,
138+
combine_outsides,
145139
a_function,
146140
map_unrolled(maybe_splat, arguments, types)...
147141
)
@@ -195,14 +189,25 @@ translate_call(::typeof(coalesce), arguments...) =
195189
translate_call(::typeof(drop), iterator, number) =
196190
string(translate(iterator), " OFFSET ", number)
197191

198-
change_row(::typeof(getproperty), outside_tables::OutsideTables, table_name) =
199-
model_row(OutsideTable(outside_tables.outside, table_name))
192+
get_column(outside_table, column_name) =
193+
OutsideCode(
194+
outside_table.outside,
195+
Expr(:call, getproperty, outside_table, column_name)
196+
)
197+
function get_model_call(::typeof(getproperty), outside_tables::OutsideTables, table_name)
198+
outside = outside_tables.outside
199+
column_names = get_column_names(outside, table_name)
200+
NamedTuple{column_names}(partial_map(
201+
get_column,
202+
OutsideTable(outside, table_name),
203+
column_names
204+
))
205+
end
200206
translate_call(::typeof(getproperty), outside_tables::OutsideTables, table_name) =
201-
translate(OutsideTable(outside_tables.outside, table_name))
202-
translate_call(::typeof(getproperty), outside_row::OutsideRow, column_name) =
207+
string("SELECT * FROM ", table_name)
208+
translate_call(::typeof(getproperty), outside_table::OutsideTable, column_name) =
203209
column_name
204210

205-
206211
"""
207212
if_else(switch, yes, no)
208213
@@ -271,46 +276,46 @@ translate_call(::typeof(QueryOperators.filter), iterator, call, call_expression)
271276
string(
272277
translate(iterator),
273278
" WHERE ",
274-
translate(get_code(call(model_row(iterator)).code))
279+
translate(get_code(call(get_model(iterator)).code))
275280
)
276281

277282
@code_instead QueryOperators.orderby OutsideCode Any Expr
278283
translate_call(::typeof(QueryOperators.orderby), unordered, key_function, key_function_expression) = string(
279284
translate(unordered),
280285
" ORDER BY ",
281-
translate(get_code(key_function(model_row(unordered))))
286+
translate(get_code(key_function(get_model(unordered))))
282287
)
283288
@code_instead QueryOperators.thenby OutsideCode Any Expr
284289
translate_call(::typeof(QueryOperators.thenby), unordered, key_function, key_function_expression) = string(
285290
translate(unordered),
286291
", ",
287-
translate(get_code(key_function(model_row(unordered))))
292+
translate(get_code(key_function(get_model(unordered))))
288293
)
289294
@code_instead QueryOperators.orderby_descending OutsideCode Any Expr
290295
translate_call(::typeof(QueryOperators.orderby_descending), unordered, key_function, key_function_expression) = string(
291296
translate(unordered),
292297
" ORDER BY ",
293-
translate(get_code(key_function(model_row(unordered)))),
298+
translate(get_code(key_function(get_model(unordered)))),
294299
" DESC"
295300
)
296301
@code_instead QueryOperators.thenby_descending OutsideCode Any Expr
297302
translate_call(::typeof(QueryOperators.thenby_descending), unordered, key_function, key_function_expression) = string(
298303
translate(unordered),
299304
", ",
300-
translate(get_code(key_function(model_row(unordered)))),
305+
translate(get_code(key_function(get_model(unordered)))),
301306
"DESC"
302307
)
303308

304309
@code_instead QueryOperators.map OutsideCode Any Expr
305-
change_row(::typeof(QueryOperators.map), iterator, call, call_expression) =
306-
call(model_row(iterator))
310+
get_model_call(::typeof(QueryOperators.map), iterator, call, call_expression) =
311+
call(get_model(iterator))
307312
select_as(new_name_model::Pair{Symbol, <: OutsideCode}) =
308313
string(translate(get_code(new_name_model.second)), " AS ", new_name_model.first)
309314
function translate_call(::typeof(QueryOperators.map), select_table, call, call_expression)
310315
if @capture select_table $getproperty(outsidetables_OutsideTables, name_)
311316
string(
312317
"SELECT ",
313-
join(Generator(select_as, pairs(call(model_row(select_table)))), ", "),
318+
join(Generator(select_as, pairs(call(get_model(select_table)))), ", "),
314319
" FROM ",
315320
name
316321
)
@@ -335,7 +340,7 @@ translate_call(::typeof(QueryOperators.take), iterator, number) =
335340

336341
@code_instead QueryOperators.unique OutsideCode Any Expr
337342
function translate_call(::typeof(QueryOperators.unique), repeated, key_function, key_function_expression)
338-
model = model_row(repeated)
343+
model = get_model(repeated)
339344
if key_function(model) !== model
340345
error("Key functions not supported for unique")
341346
else
@@ -362,22 +367,6 @@ translate_call(::typeof(occursin), needle, haystack) = string(
362367
translate(needle)
363368
)
364369

365-
make_outside_column(outside_table, column_name) =
366-
OutsideCode(
367-
outside_table.outside,
368-
Expr(:call, getproperty, OutsideRow(outside_table), column_name)
369-
)
370-
function model_row(outside_table::OutsideTable)
371-
column_names = get_column_names(outside_table.outside, outside_table.table_name)
372-
NamedTuple{column_names}(partial_map(
373-
make_outside_column,
374-
outside_table,
375-
column_names
376-
))
377-
end
378-
translate(outside_table::OutsideTable) =
379-
string("SELECT * FROM ", outside_table.table_name)
380-
381370
@code_instead startswith OutsideCode Any
382371
@code_instead startswith Any OutsideCode
383372
@code_instead startswith OutsideCode OutsideCode
@@ -404,17 +393,17 @@ translate_call(::typeof(take), iterator, number) =
404393

405394
# dispatch
406395

407-
change_row(arbitrary_function, iterator, arguments...) = model_row(iterator)
396+
get_model_call(arbitrary_function, iterator, arguments...) = get_model(iterator)
408397

409-
model_row(code::Expr) =
398+
get_model(code::Expr) =
410399
if @capture code call_(arguments__)
411-
change_row(call, arguments...)
400+
get_model_call(call, arguments...)
412401
else
413-
error("Cannot build a model_row row for $code")
402+
error("Cannot build a get_model row for $code")
414403
end
415404

416405
translate(something) = something
417-
translate(outside_row::OutsideRow) = outside_row.table_name
406+
translate(outside_table::OutsideTable) = outside_table.table_name
418407
translate(code::Expr) =
419408
if @capture code call_(arguments__)
420409
translate_call(if call === ifelse

0 commit comments

Comments
 (0)