@@ -90,22 +90,22 @@ function get_tables(outside)
9090end
9191export get_tables
9292
93- struct OutsideTable {Outside}
93+ struct OutsideRow {Outside}
9494 outside:: Outside
9595 table_name:: Symbol
9696end
9797
98- OutsideTable (outside_table :: OutsideTable ) =
99- OutsideTable (outside_table . outside, outside_table . table_name)
98+ OutsideRow (outside_row :: OutsideRow ) =
99+ OutsideRow (outside_row . outside, outside_row . table_name)
100100
101101function pop_outsides! (outsides, outside_code:: OutsideCode )
102102 push! (outsides, outside_code. outside)
103103 outside_code. code
104104end
105105pop_outsides! (outsides, something) = something
106- function combine_outsides (a_function, arguments ... )
106+ function combine_outsides (a_function, outside_codes ... )
107107 outsides = Set (Any[])
108- codes = partial_map (pop_outsides!, outsides, arguments )
108+ codes = partial_map (pop_outsides!, outsides, outside_codes )
109109 OutsideCode (
110110 if length (outsides) == 0
111111 error (" No outside" )
@@ -152,60 +152,60 @@ end
152152@code_instead (== ) OutsideCode Any
153153@code_instead (== ) Any OutsideCode
154154@code_instead (== ) OutsideCode OutsideCode
155- translate_call (:: typeof (== ), left, right) =
155+ translate_node (:: typeof (== ), left, right) =
156156 string (translate (left), " = " , translate (right))
157157
158158@code_instead (!= ) OutsideCode Any
159159@code_instead (!= ) Any OutsideCode
160160@code_instead (!= ) OutsideCode OutsideCode
161- translate_call (:: typeof (!= ), left, right) =
161+ translate_node (:: typeof (!= ), left, right) =
162162 string (translate (left), " <> " , translate (right))
163163
164164@code_instead (! ) OutsideCode
165- translate_call (:: typeof (! ), wrong) = string (" NOT " , translate (wrong))
165+ translate_node (:: typeof (! ), wrong) = string (" NOT " , translate (wrong))
166166
167167@code_instead (& ) OutsideCode Any
168168@code_instead (& ) Any OutsideCode
169169@code_instead (& ) OutsideCode OutsideCode
170- translate_call (:: typeof (& ), left, right) =
170+ translate_node (:: typeof (& ), left, right) =
171171 string (translate (left), " AND " , translate (right))
172172
173173@code_instead (| ) OutsideCode Any
174174@code_instead (| ) Any OutsideCode
175175@code_instead (| ) OutsideCode OutsideCode
176- translate_call (:: typeof (| ), left, right) =
176+ translate_node (:: typeof (| ), left, right) =
177177 string (translate (left), " OR " , translate (right))
178178
179179@code_instead backwards OutsideCode
180- translate_call (:: typeof (backwards), column) =
180+ translate_node (:: typeof (backwards), column) =
181181 string (translate (column), " DESC" )
182182
183183@code_instead coalesce OutsideCode Vararg{Any}
184184
185- translate_call (:: typeof (coalesce), arguments... ) =
185+ translate_node (:: typeof (coalesce), arguments... ) =
186186 string (" COALESCE(" , join (map_unrolled (translate, arguments... ), " , " ), " )" )
187187
188188@code_instead drop OutsideCode Integer
189- translate_call (:: typeof (drop), iterator, number) =
189+ translate_node (:: typeof (drop), iterator, number) =
190190 string (translate (iterator), " OFFSET " , number)
191191
192- get_column (outside_table , column_name) =
192+ get_column (outside_row , column_name) =
193193 OutsideCode (
194- outside_table . outside,
195- Expr (:call , getproperty, outside_table , column_name)
194+ outside_row . outside,
195+ Expr (:call , getproperty, outside_row , column_name)
196196 )
197- function get_model_call (:: typeof (getproperty), outside_tables:: OutsideTables , table_name)
197+ function make_model_row_node (:: typeof (getproperty), outside_tables:: OutsideTables , table_name)
198198 outside = outside_tables. outside
199199 column_names = get_column_names (outside, table_name)
200200 NamedTuple {column_names} (partial_map (
201201 get_column,
202- OutsideTable (outside, table_name),
202+ OutsideRow (outside, table_name),
203203 column_names
204204 ))
205205end
206- translate_call (:: typeof (getproperty), outside_tables:: OutsideTables , table_name) =
206+ translate_node (:: typeof (getproperty), outside_tables:: OutsideTables , table_name) =
207207 string (" SELECT * FROM " , table_name)
208- translate_call (:: typeof (getproperty), outside_table :: OutsideTable , column_name) =
208+ translate_node (:: typeof (getproperty), outside_row :: OutsideRow , column_name) =
209209 column_name
210210
211211"""
@@ -233,7 +233,7 @@ export if_else
233233@code_instead if_else OutsideCode Any OutsideCode
234234@code_instead if_else OutsideCode OutsideCode Any
235235@code_instead if_else OutsideCode OutsideCode OutsideCode
236- translate_call (:: typeof (if_else), test, right, wrong) = string (
236+ translate_node (:: typeof (if_else), test, right, wrong) = string (
237237 " CASE WHEN " ,
238238 translate (test),
239239 " THEN " ,
@@ -246,76 +246,76 @@ translate_call(::typeof(if_else), test, right, wrong) = string(
246246@code_instead in OutsideCode Any
247247@code_instead in Any OutsideCode
248248@code_instead in OutsideCode OutsideCode
249- translate_call (:: typeof (in), item, collection) =
249+ translate_node (:: typeof (in), item, collection) =
250250 string (translate (item), " IN " , collection)
251251
252252@code_instead isequal OutsideCode Any
253253@code_instead isequal Any OutsideCode
254254@code_instead isequal OutsideCode OutsideCode
255255
256- translate_call (:: typeof (isequal), left, right) =
256+ translate_node (:: typeof (isequal), left, right) =
257257 string (translate (left), " IS NOT DISTINCT FROM " , translate (right))
258258
259259@code_instead isless OutsideCode Any
260260@code_instead isless Any OutsideCode
261261@code_instead isless OutsideCode OutsideCode
262262
263- translate_call (:: typeof (isless), left, right) =
263+ translate_node (:: typeof (isless), left, right) =
264264 string (translate (left), " < " , translate (right))
265265
266266@code_instead ismissing OutsideCode
267- translate_call (:: typeof (ismissing), maybe) =
267+ translate_node (:: typeof (ismissing), maybe) =
268268 string (translate (maybe), " IS NULL" )
269269
270270@code_instead QueryOperators. drop OutsideCode Integer
271- translate_call (:: typeof (QueryOperators. drop), iterator, number) =
271+ translate_node (:: typeof (QueryOperators. drop), iterator, number) =
272272 string (translate (iterator), " OFFSET " , number)
273273
274274@code_instead QueryOperators. filter OutsideCode Any Expr
275- translate_call (:: typeof (QueryOperators. filter), iterator, call, call_expression) =
275+ translate_node (:: typeof (QueryOperators. filter), iterator, call, call_expression) =
276276 string (
277277 translate (iterator),
278278 " WHERE " ,
279- translate (get_code (call (get_model (iterator)). code))
279+ translate (get_code (call (make_model_row (iterator)). code))
280280 )
281281
282282@code_instead QueryOperators. orderby OutsideCode Any Expr
283- translate_call (:: typeof (QueryOperators. orderby), unordered, key_function, key_function_expression) = string (
283+ translate_node (:: typeof (QueryOperators. orderby), unordered, key_function, key_function_expression) = string (
284284 translate (unordered),
285285 " ORDER BY " ,
286- translate (get_code (key_function (get_model (unordered))))
286+ translate (get_code (key_function (make_model_row (unordered))))
287287)
288288@code_instead QueryOperators. thenby OutsideCode Any Expr
289- translate_call (:: typeof (QueryOperators. thenby), unordered, key_function, key_function_expression) = string (
289+ translate_node (:: typeof (QueryOperators. thenby), unordered, key_function, key_function_expression) = string (
290290 translate (unordered),
291291 " , " ,
292- translate (get_code (key_function (get_model (unordered))))
292+ translate (get_code (key_function (make_model_row (unordered))))
293293)
294294@code_instead QueryOperators. orderby_descending OutsideCode Any Expr
295- translate_call (:: typeof (QueryOperators. orderby_descending), unordered, key_function, key_function_expression) = string (
295+ translate_node (:: typeof (QueryOperators. orderby_descending), unordered, key_function, key_function_expression) = string (
296296 translate (unordered),
297297 " ORDER BY " ,
298- translate (get_code (key_function (get_model (unordered)))),
298+ translate (get_code (key_function (make_model_row (unordered)))),
299299 " DESC"
300300)
301301@code_instead QueryOperators. thenby_descending OutsideCode Any Expr
302- translate_call (:: typeof (QueryOperators. thenby_descending), unordered, key_function, key_function_expression) = string (
302+ translate_node (:: typeof (QueryOperators. thenby_descending), unordered, key_function, key_function_expression) = string (
303303 translate (unordered),
304304 " , " ,
305- translate (get_code (key_function (get_model (unordered)))),
305+ translate (get_code (key_function (make_model_row (unordered)))),
306306 " DESC"
307307)
308308
309309@code_instead QueryOperators. map OutsideCode Any Expr
310- get_model_call (:: typeof (QueryOperators. map), iterator, call, call_expression) =
311- call (get_model (iterator))
312- select_as (new_name_model :: Pair{Symbol, <: OutsideCode} ) =
313- string (translate (get_code (new_name_model . second)), " AS " , new_name_model . first)
314- function translate_call (:: typeof (QueryOperators. map), select_table, call, call_expression)
310+ make_model_row_node (:: typeof (QueryOperators. map), iterator, call, call_expression) =
311+ call (make_model_row (iterator))
312+ select_as (new_name_make_model_row :: Pair{Symbol, <: OutsideCode} ) =
313+ string (translate (get_code (new_name_make_model_row . second)), " AS " , new_name_make_model_row . first)
314+ function translate_node (:: typeof (QueryOperators. map), select_table, call, call_expression)
315315 if @capture select_table $ getproperty (outsidetables_OutsideTables, name_)
316316 string (
317317 " SELECT " ,
318- join (Generator (select_as, pairs (call (get_model (select_table)))), " , " ),
318+ join (Generator (select_as, pairs (call (make_model_row (select_table)))), " , " ),
319319 " FROM " ,
320320 name
321321 )
@@ -325,7 +325,7 @@ function translate_call(::typeof(QueryOperators.map), select_table, call, call_e
325325end
326326
327327@code_instead QueryOperators. take OutsideCode Any
328- translate_call (:: typeof (QueryOperators. take), iterator, number) =
328+ translate_node (:: typeof (QueryOperators. take), iterator, number) =
329329 if @capture iterator $ (QueryOperators. drop)(inneriterator_, offset_)
330330 string (
331331 translate (inneriterator),
@@ -339,9 +339,9 @@ translate_call(::typeof(QueryOperators.take), iterator, number) =
339339 end
340340
341341@code_instead QueryOperators. unique OutsideCode Any Expr
342- function translate_call (:: typeof (QueryOperators. unique), repeated, key_function, key_function_expression)
343- model = get_model (repeated)
344- if key_function (model ) != = model
342+ function translate_node (:: typeof (QueryOperators. unique), repeated, key_function, key_function_expression)
343+ the_make_model_row = make_model_row (repeated)
344+ if key_function (the_make_model_row ) != = the_make_model_row
345345 error (" Key functions not supported for unique" )
346346 else
347347 replace (translate (repeated), r" \b SELECT\b " => " SELECT DISTINCT" )
@@ -350,18 +350,18 @@ end
350350
351351@code_instead occursin AbstractString OutsideCode
352352@code_instead occursin Regex OutsideCode
353- translate_call (:: typeof (occursin), needle:: AbstractString , haystack) = string (
353+ translate_node (:: typeof (occursin), needle:: AbstractString , haystack) = string (
354354 translate (haystack),
355355 " LIKE '%" ,
356356 needle,
357357 " %'"
358358)
359- translate_call (:: typeof (occursin), needle:: Regex , haystack) = string (
359+ translate_node (:: typeof (occursin), needle:: Regex , haystack) = string (
360360 translate (haystack),
361361 " LIKE " ,
362362 replace (replace (needle. pattern, r" (?<!\\ )\.\* " => " %" ), r" (?<!\\ )\. " => " _" )
363363)
364- translate_call (:: typeof (occursin), needle, haystack) = string (
364+ translate_node (:: typeof (occursin), needle, haystack) = string (
365365 translate (haystack),
366366 " LIKE " ,
367367 translate (needle)
@@ -370,15 +370,15 @@ translate_call(::typeof(occursin), needle, haystack) = string(
370370@code_instead startswith OutsideCode Any
371371@code_instead startswith Any OutsideCode
372372@code_instead startswith OutsideCode OutsideCode
373- translate_call (:: typeof (startswith), full, prefix:: AbstractString ) = string (
373+ translate_node (:: typeof (startswith), full, prefix:: AbstractString ) = string (
374374 translate (full),
375375 " LIKE '" ,
376376 prefix,
377377 " %'"
378378)
379379
380380@code_instead take OutsideCode Integer
381- translate_call (:: typeof (take), iterator, number) =
381+ translate_node (:: typeof (take), iterator, number) =
382382 if @capture iterator $ drop (inneriterator_, offset_)
383383 string (
384384 translate (inneriterator),
@@ -392,42 +392,37 @@ translate_call(::typeof(take), iterator, number) =
392392 end
393393
394394# dispatch
395-
396- get_model_call (arbitrary_function, iterator, arguments... ) = get_model (iterator)
397-
398- get_model (code:: Expr ) =
399- if @capture code call_ (arguments__)
400- get_model_call (call, arguments... )
401- else
402- error (" Cannot build a get_model row for $code " )
403- end
404-
405- translate (something) = something
406- translate (outside_table:: OutsideTable ) = outside_table. table_name
407- translate (code:: Expr ) =
395+ nodes (code:: Expr ) =
408396 if @capture code call_ (arguments__)
409- translate_call ( if call === ifelse
397+ if call === ifelse
410398 if_else
411399 else
412400 call
413- end , arguments... )
401+ end , arguments...
414402 elseif @capture code left_ && right_
415- translate_call ( & , left, right)
403+ & , left, right
416404 elseif @capture code left_ || right_
417- translate_call ( | , left, right)
405+ | , left, right
418406 elseif @capture code if condition_ yes_ else no_ end
419- translate_call ( if_else, condition, left, right)
407+ if_else, condition, left, right
420408 else
421- error (" Cannot translate code $code " )
409+ error (" Cannot split call $code " )
422410 end
423411
412+ make_model_row_node (arbitrary_function, iterator, arguments... ) = make_model_row (iterator)
413+ make_model_row (code:: Expr ) = make_model_row_node (nodes (code)... )
414+
415+ translate (something) = something
416+ translate (outside_row:: OutsideRow ) = outside_row. table_name
417+ translate (code:: Expr ) = translate_node (nodes (code)... )
418+
424419# collect
425420query (outside_code:: OutsideCode ) = outside_code
426421
427422struct SQLiteCursor{Row}
428423 statement:: Stmt
429424 status:: RefValue{Cint}
430- cursor_row :: RefValue{Int}
425+ cursor_make_model_row :: RefValue{Int}
431426end
432427
433428eltype (:: SQLiteCursor{Row} ) where {Row} = Row
@@ -471,20 +466,20 @@ iterate(cursor::SQLiteCursor{Row}) where {Row} =
471466 nothing
472467 else
473468 named_tuple = generate_namedtuple (Row, cursor)
474- cursor. cursor_row [] = 1
469+ cursor. cursor_make_model_row [] = 1
475470 named_tuple, 1
476471 end
477472
478473iterate (cursor:: SQLiteCursor{Row} , state) where {Row} =
479- if state != cursor. cursor_row []
480- error (" State does not match SQLiteCursor row " )
474+ if state != cursor. cursor_make_model_row []
475+ error (" State does not match SQLiteCursor make_model_row " )
481476 else
482477 cursor. status[] = sqlite3_step (cursor. statement. handle)
483478 if isdone (cursor)
484479 nothing
485480 else
486481 named_tuple = generate_namedtuple (Row, cursor)
487- cursor. cursor_row [] = state + 1
482+ cursor. cursor_make_model_row [] = state + 1
488483 named_tuple, state + 1
489484 end
490485 end
0 commit comments