11# The translation pass turns Julia expressions into SQL expressions
2- # Frustratingly, primary tables are translated differently from secondary tables, so translate must propagate the `primary ` keyword
2+ # Frustratingly, _primary tables are translated differently from secondary tables, so translate must propagate the `_primary ` keyword
33struct SQLExpression
44 call:: Symbol
55 arguments:: Vector{Any}
66 SQLExpression (call, arguments... ) = new (call, Any[arguments... ])
77end
88
9- function translate (something:: Union{Char, AbstractString} ; primary = true )
9+ function nest (sql_expression)
10+ SQLExpression (:AS , SQLExpression (:FROM , sql_expression), :__TABLE__ )
11+ end
12+
13+ function translate (something:: Union{Char, AbstractString} ; _primary = true )
1014 repr (something)
1115end
1216
13- function translate (something; primary = true )
17+ function translate (something; _primary = true )
1418 something
1519end
16- function translate (source_row:: SourceRow ; primary = true )
20+ function translate (source_row:: SourceRow ; _primary = true )
1721 source_row. table_name
1822end
19- function translate (call:: Expr ; primary = true )
23+ function translate (call:: Expr ; _primary = true )
2024 arguments, keywords = split_call (call)
21- # TODO : figure out a way for users to pass a keyword named primary
22- translate_call (arguments... ; primary = primary, keywords... )
25+ translate_call (arguments... ; _primary = _primary, keywords... )
2326end
2427
2528# A 1-1 mapping between Julia functions and SQL functions
2629function translate_default (location, function_type, SQL_call)
2730 result = :(
28- function translate_call ($ function_type, arguments... ; primary = true )
31+ function translate_call ($ function_type, arguments... ; _primary = true )
2932 $ SQLExpression ($ SQL_call, $ map (
30- argument -> $ translate (argument; primary = primary ),
33+ argument -> $ translate (argument; _primary = _primary ),
3134 arguments
3235 )... )
3336 end
5861
5962@translate_default :: typeof (abs) :ABS
6063
61- function as (pair; primary = true )
64+ function as (pair; _primary = true )
6265 SQLExpression (:AS ,
63- translate (pair. second. code; primary = primary ),
66+ translate (pair. second. code; _primary = _primary ),
6467 pair. first
6568 )
6669end
6972
7073@translate_default :: typeof (char) :CHAR
7174
72- function translate_call (:: typeof (convert), :: Type{Int} , it; primary = true )
75+ function translate_call (:: typeof (convert), :: Type{Int} , it; _primary = true )
7376 SQLExpression (:UNICODE , translate (it))
7477end
7578
8184
8285@translate_default :: typeof (QueryOperators. drop) :OFFSET
8386
84- function translate_call (:: typeof (QueryOperators. filter), iterator, call, call_expression; primary = true )
87+ function translate_call (:: typeof (QueryOperators. filter), iterator, call, call_expression; _primary = true )
8588 SQLExpression (:WHERE ,
86- translate (iterator; primary = primary ),
87- translate (call (model_row (iterator)). code; primary = primary )
89+ translate (iterator; _primary = _primary ),
90+ translate (call (model_row (iterator)). code; _primary = _primary )
8891 )
8992end
9093
91- function translate_call (:: typeof (getproperty), source_tables:: Database , table_name; primary = true )
92- translated = translate (table_name; primary = primary )
93- if primary
94+ function translate_call (:: typeof (getproperty), source_tables:: Database , table_name; _primary = true )
95+ translated = translate (table_name; _primary = _primary )
96+ if _primary
9497 SQLExpression (:FROM , translated)
9598 else
9699 translated
97100 end
98101end
99- function translate_call (:: typeof (getproperty), source_row:: SourceRow , column_name; primary = true )
100- translated = translate (column_name; primary = primary )
101- if primary
102+ function translate_call (:: typeof (getproperty), source_row:: SourceRow , column_name; _primary = true )
103+ translated = translate (column_name; _primary = _primary )
104+ if _primary
102105 translated
103106 else
104107 SQLExpression (:., source_row. table_name, translated)
105108 end
106109end
107110
108- function translate_call (:: typeof (QueryOperators. groupby), ungrouped, group_function, group_function_expression, map_selector, map_function_expression; primary = true )
109- # TODO : map_selector
111+ function translate_call (:: typeof (QueryOperators. groupby), ungrouped, group_function, group_function_expression, map_selector, map_function_expression; _primary = true )
110112 model = model_row (ungrouped)
111113 SQLExpression (Symbol (" GROUP BY" ),
112- SQLExpression ( :AS , SQLExpression ( :FROM , translate_call (
114+ nest ( translate_call (
113115 QueryOperators. map,
114116 ungrouped,
115117 map_selector, map_function_expression,
116- primary = primary
117- )), :__TABLE__ ),
118- translate (group_function (model). code; primary = primary )
118+ _primary = _primary
119+ )),
120+ translate (group_function (model). code; _primary = _primary )
119121 )
120122end
121123
@@ -131,25 +133,25 @@ end
131133
132134@translate_default :: typeof (ismissing) Symbol (" IS NULL" )
133135
134- function translate_call (:: typeof (QueryOperators. join), source1, source2, key1, key1_expression, key2, key2_expression, combine, combine_expression; primary = true )
136+ function translate_call (:: typeof (QueryOperators. join), source1, source2, key1, key1_expression, key2, key2_expression, combine, combine_expression; _primary = true )
135137 model_row_1 = model_row (source1)
136138 model_row_2 = model_row (source2)
137139 SQLExpression (:SELECT ,
138140 SQLExpression (:ON ,
139141 SQLExpression (Symbol (" INNER JOIN" ),
140142 translate (source1),
141- # mark as not primary to suppress FROM
142- translate (source2; primary = false )
143+ # mark as not _primary to suppress FROM
144+ translate (source2; _primary = false )
143145 ),
144- # mark both as not primary to always be explicit about table
146+ # mark both as not _primary to always be explicit about table
145147 SQLExpression (:(= ),
146- translate (key1 (model_row_1). code; primary = false ),
147- translate (key2 (model_row_2). code; primary = false )
148+ translate (key1 (model_row_1). code; _primary = false ),
149+ translate (key2 (model_row_2). code; _primary = false )
148150 )
149151 ),
150- # mark both as not primary to always be explicit about table
152+ # mark both as not _primary to always be explicit about table
151153 Generator (
152- pair -> as (pair; primary = false ),
154+ pair -> as (pair; _primary = false ),
153155 pairs (combine (model_row_1, model_row_2))
154156 )...
155157 )
@@ -159,11 +161,15 @@ end
159161
160162@translate_default :: typeof (lowercase) :LOWER
161163
162- function translate_call (:: typeof (QueryOperators. map), select_table, call, call_expression; primary = true )
164+ function translate_call (:: typeof (QueryOperators. map), select_table, call, call_expression; _primary = true )
165+ inner = translate (select_table; _primary = _primary)
166+ if inner. call == :SELECT
167+ inner = nest (inner)
168+ end
163169 SQLExpression (
164- Symbol ( " SELECT" ), translate (select_table; primary = primary) ,
170+ : SELECT, inner ,
165171 Generator (
166- pair -> as (pair; primary = primary ),
172+ pair -> as (pair; _primary = _primary ),
167173 pairs (call (model_row (select_table)))
168174 )...
169175 )
@@ -175,48 +181,48 @@ end
175181
176182@translate_default :: typeof (min) :min
177183
178- translate_call (:: typeof (occursin), needle:: Regex , haystack; primary = true ) =
184+ translate_call (:: typeof (occursin), needle:: Regex , haystack; _primary = true ) =
179185 SQLExpression (
180186 :LIKE ,
181- translate (haystack; primary = primary ),
187+ translate (haystack; _primary = _primary ),
182188 # * => %, . => _
183- string ( ' " ' , replace (replace (needle. pattern, r" (?<!\\ )\.\* " => " %" ), r" (?<!\\ )\. " => " _" ), ' " ' )
189+ translate ( replace (replace (needle. pattern, r" (?<!\\ )\.\* " => " %" ), r" (?<!\\ )\. " => " _" ))
184190 )
185191
186- function translate_call (:: typeof (QueryOperators. orderby), unordered, key_function, key_function_expression; primary = true )
192+ function translate_call (:: typeof (QueryOperators. orderby), unordered, key_function, key_function_expression; _primary = true )
187193 SQLExpression (Symbol (" ORDER BY" ),
188- translate (unordered; primary = primary ),
189- translate (key_function (model_row (unordered)). code; primary = primary )
194+ translate (unordered; _primary = _primary ),
195+ translate (key_function (model_row (unordered)). code; _primary = _primary )
190196 )
191197end
192198
193- function translate_call (:: typeof (QueryOperators. orderby_descending), unordered, key_function, key_function_expression; primary = true )
199+ function translate_call (:: typeof (QueryOperators. orderby_descending), unordered, key_function, key_function_expression; _primary = true )
194200 SQLExpression (Symbol (" ORDER BY" ),
195- translate (unordered; primary = primary ),
201+ translate (unordered; _primary = _primary ),
196202 SQLExpression (:DESC ,
197- translate (key_function (model_row (unordered)). code; primary = primary )
203+ translate (key_function (model_row (unordered)). code; _primary = _primary )
198204 )
199205 )
200206end
201207
202208@translate_default :: typeof (repr) :QUOTE
203209
204- function translate_call (:: typeof (rand), :: Type{Int} ; primary = true , digits = 0 )
210+ function translate_call (:: typeof (rand), :: Type{Int} ; _primary = true , digits = 0 )
205211 SQLExpression (:RANDOM )
206212end
207213
208- function translate_call (:: typeof (replace), it, pair; primary = true )
214+ function translate_call (:: typeof (replace), it, pair; _primary = true )
209215 SQLExpression (:REPLACE ,
210- translate (it; primary = primary ),
211- translate (pair. first; primary = primary ),
212- translate (pair. second; primary = primary )
216+ translate (it; _primary = _primary ),
217+ translate (pair. first; _primary = _primary ),
218+ translate (pair. second; _primary = _primary )
213219 )
214220end
215221
216- function translate_call (:: typeof (round), it; primary = true , digits = 0 )
222+ function translate_call (:: typeof (round), it; _primary = true , digits = 0 )
217223 SQLExpression (:ROUND ,
218- translate (it; primary = primary ),
219- translate (digits; primary = primary )
224+ translate (it; _primary = _primary ),
225+ translate (digits; _primary = _primary )
220226 )
221227end
222228
@@ -230,18 +236,18 @@ end
230236
231237@translate_default :: typeof (QueryOperators. take) :LIMIT
232238
233- function translate_call (:: typeof (QueryOperators. thenby), unordered, key_function, key_function_expression; primary = true )
234- original = translate (unordered; primary = primary )
239+ function translate_call (:: typeof (QueryOperators. thenby), unordered, key_function, key_function_expression; _primary = true )
240+ original = translate (unordered; _primary = _primary )
235241 SQLExpression (original. call, original. arguments... ,
236- translate (key_function (model_row (unordered)). code; primary = primary )
242+ translate (key_function (model_row (unordered)). code; _primary = _primary )
237243 )
238244end
239245
240- function translate_call (:: typeof (QueryOperators. thenby_descending), unordered, key_function, key_function_expression; primary = true )
241- original = translate (unordered; primary = primary )
246+ function translate_call (:: typeof (QueryOperators. thenby_descending), unordered, key_function, key_function_expression; _primary = true )
247+ original = translate (unordered; _primary = _primary )
242248 SQLExpression (original. call, original. arguments... ,
243249 SQLExpression (:DESC ,
244- translate (key_function (model_row (unordered)). code; primary = primary )
250+ translate (key_function (model_row (unordered)). code; _primary = _primary )
245251 )
246252 )
247253end
250256
251257@translate_default :: typeof (type_of) :TYPEOF
252258
253- function translate_call (:: typeof (QueryOperators. unique), repeated, key_function, key_function_expression; primary = true )
254- result = translate (repeated; primary = primary )
259+ function translate_call (:: typeof (QueryOperators. unique), repeated, key_function, key_function_expression; _primary = true )
260+ result = translate (repeated; _primary = _primary )
255261 SQLExpression (Symbol (string (result. call, " DISTINCT" )), result. arguments... )
256262end
257263
0 commit comments