Commit | Line | Data |
515523bc |
1 | package Data::Query::Renderer::SQL::Naive; |
2 | |
3 | use strictures 1; |
87867dad |
4 | |
5 | sub intersperse { my $i = shift; my @i = map +($_, $i), @_; pop @i; @i } |
6 | |
515523bc |
7 | use SQL::ReservedWords; |
6b45ffe4 |
8 | use Data::Query::ExprHelpers; |
515523bc |
9 | |
8b4289c8 |
10 | use Moo; |
6b45ffe4 |
11 | no warnings; |
12 | use warnings; |
515523bc |
13 | |
8b4289c8 |
14 | has reserved_ident_parts => ( |
15 | is => 'ro', default => sub { |
16 | our $_DEFAULT_RESERVED ||= { map +($_ => 1), SQL::ReservedWords->words } |
17 | } |
18 | ); |
19 | |
20 | has quote_chars => (is => 'ro', default => sub { [''] }); |
21 | |
4cea30e6 |
22 | has identifier_sep => (is => 'ro', default => sub { '.' }); |
23 | |
8b4289c8 |
24 | has simple_ops => (is => 'ro', builder => '_default_simple_ops'); |
515523bc |
25 | |
4cea30e6 |
26 | has lc_keywords => (is => 'ro', default => sub { 0 }); |
27 | |
28 | has always_quote => (is => 'ro', default => sub { 0 }); |
29 | |
07b0f7e0 |
30 | has collapse_aliases => (is => 'ro', default => sub { 1 }); |
31 | |
515523bc |
32 | sub _default_simple_ops { |
33 | +{ |
f8343502 |
34 | (map +($_ => 'binop'), qw(= > < >= <= != LIKE), 'NOT LIKE' ), |
35 | (map +($_ => 'unop'), qw(NOT) ), |
36 | (map +($_ => 'unop_reverse'), ('IS NULL', 'IS NOT NULL')), |
515523bc |
37 | (map +($_ => 'flatten'), qw(AND OR) ), |
f8343502 |
38 | (map +($_ => 'in'), ('IN', 'NOT IN')), |
829906e0 |
39 | (map +($_ => 'between'), ('BETWEEN', 'NOT BETWEEN')), |
40 | (apply => 'apply'), |
515523bc |
41 | } |
42 | } |
43 | |
44 | sub render { |
45 | my $self = shift; |
46 | $self->_flatten_structure($self->_render(@_)) |
47 | } |
48 | |
49 | sub _flatten_structure { |
50 | my ($self, $struct) = @_; |
51 | my @bind; |
bdb576cb |
52 | [ do { |
53 | my @p = map { |
54 | my $r = ref; |
55 | if (!$r) { $_ } |
56 | elsif ($r eq 'ARRAY') { |
57 | my ($sql, @b) = @{$self->_flatten_structure($_)}; |
58 | push @bind, @b; |
59 | $sql; |
60 | } |
61 | elsif ($r eq 'HASH') { push @bind, $_; () } |
62 | else { die "_flatten_structure can't handle ref type $r for $_" } |
63 | } @$struct; |
64 | join '', map { |
65 | ($p[$_], (($p[$_+1]||',') eq ',') ? () : (' ')) |
66 | } 0 .. $#p; |
67 | }, |
68 | @bind |
69 | ]; |
515523bc |
70 | } |
71 | |
04d30ecb |
72 | # I presented this to permit strange people to easily supply a patch to lc() |
9c8fc055 |
73 | # their keywords, as I have heard many desire to do, lest they infect me |
74 | # with whatever malady caused this desire by their continued proximity for |
75 | # want of such a feature. |
04d30ecb |
76 | # |
77 | # Then I realised that SQL::Abstract compatibility work required it. |
78 | # |
79 | # FEH. |
9c8fc055 |
80 | |
4cea30e6 |
81 | sub _format_keyword { $_[0]->lc_keywords ? lc($_[1]) : $_[1] } |
9c8fc055 |
82 | |
515523bc |
83 | sub _render { |
bdb576cb |
84 | $_[0]->${\"_render_${\(lc($_[1]->{type})||'broken')}"}($_[1]); |
85 | } |
86 | |
87 | sub _render_broken { |
88 | my ($self, $dq) = @_; |
89 | require Data::Dumper::Concise; |
90 | die "Broken DQ entry: ".Data::Dumper::Concise::Dumper($dq); |
515523bc |
91 | } |
92 | |
93 | sub _render_identifier { |
94 | die "Unidentified identifier (SQL can no has \$_)" |
95 | unless my @i = @{$_[1]->{elements}}; |
96 | # handle single or paired quote chars |
8b4289c8 |
97 | my ($q1, $q2) = @{$_[0]->quote_chars}[0,-1]; |
4cea30e6 |
98 | my $always_quote = $_[0]->always_quote; |
8b4289c8 |
99 | my $res_check = $_[0]->reserved_ident_parts; |
515523bc |
100 | return [ |
101 | join |
4cea30e6 |
102 | $_[0]->identifier_sep, |
515523bc |
103 | map +( |
104 | $_ eq '*' # Yes, this means you can't have a column just called '*'. |
105 | ? $_ # Yes, this is a feature. Go shoot the DBA if he disagrees. |
106 | : ( # reserved are stored uc, quote if non-word |
713f29aa |
107 | ($always_quote and $q1) || $res_check->{+uc} || /\W/ |
515523bc |
108 | ? $q1.$_.$q2 |
109 | : $_ |
110 | ) |
111 | ), @i |
112 | ]; |
113 | } |
114 | |
115 | sub _render_value { |
a5c0a2d5 |
116 | [ '?', $_[1] ] |
515523bc |
117 | } |
118 | |
12e6eab8 |
119 | sub _operator_type { 'SQL.Naive' } |
120 | |
515523bc |
121 | sub _render_operator { |
122 | my ($self, $dq) = @_; |
123 | my $op = $dq->{operator}; |
12e6eab8 |
124 | unless (exists $op->{$self->_operator_type}) { |
125 | $op->{$self->_operator_type} = $self->_convert_op($dq); |
515523bc |
126 | } |
f8343502 |
127 | my $op_name = $op->{$self->_operator_type}; |
d17f527f |
128 | if (my $op_type = $self->simple_ops->{$op_name}) { |
515523bc |
129 | return $self->${\"_handle_op_type_${op_type}"}($op_name, $dq); |
f8343502 |
130 | } elsif (my $meth = $self->can("_handle_op_special_${op_name}")) { |
131 | return $self->$meth($dq); |
515523bc |
132 | } |
6e055841 |
133 | if (my $argc = @{$dq->{args}}) { |
134 | if ($argc == 1) { |
135 | return $self->_handle_op_type_unop($op_name, $dq); |
136 | } elsif ($argc == 2) { |
137 | return $self->_handle_op_type_binop($op_name, $dq); |
138 | } |
139 | } |
140 | die "Unsure how to handle ${op_name}"; |
515523bc |
141 | } |
142 | |
410dc8ab |
143 | sub _maybe_parenthesise { |
144 | my ($self, $dq) = @_; |
6b45ffe4 |
145 | for ($dq) { |
146 | return is_Select() || is_Slice() |
410dc8ab |
147 | ? [ '(', $self->_render($dq), ')' ] |
6b45ffe4 |
148 | : $self->_render($dq); |
149 | } |
410dc8ab |
150 | } |
151 | |
515523bc |
152 | sub _handle_op_type_binop { |
153 | my ($self, $op_name, $dq) = @_; |
154 | die "${op_name} registered as binary op but args contain " |
155 | .scalar(@{$dq->{args}})." entries" |
156 | unless @{$dq->{args}} == 2; |
157 | [ |
410dc8ab |
158 | $self->_maybe_parenthesise($dq->{args}[0]), |
515523bc |
159 | $op_name, |
410dc8ab |
160 | $self->_maybe_parenthesise($dq->{args}[1]), |
515523bc |
161 | ] |
162 | } |
163 | |
164 | sub _handle_op_type_unop { |
165 | my ($self, $op_name, $dq) = @_; |
166 | die "${op_name} registered as unary op but args contain " |
167 | .scalar(@{$dq->{args}})." entries" |
168 | unless @{$dq->{args}} == 1; |
169 | [ |
f8343502 |
170 | '(', |
171 | $op_name, |
172 | $self->_render($dq->{args}[0]), |
173 | ')', |
174 | ] |
175 | } |
176 | |
177 | sub _handle_op_type_unop_reverse { |
178 | my ($self, $op_name, $dq) = @_; |
179 | die "${op_name} registered as unary op but args contain " |
180 | .scalar(@{$dq->{args}})." entries" |
181 | unless @{$dq->{args}} == 1; |
182 | [ |
515523bc |
183 | $self->_render($dq->{args}[0]), |
f8343502 |
184 | $op_name, |
515523bc |
185 | ] |
186 | } |
187 | |
188 | sub _handle_op_type_flatten { |
189 | my ($self, $op_name, $dq) = @_; |
190 | my @argq = @{$dq->{args}}; |
191 | my @arg_final; |
192 | while (my $arg = shift @argq) { |
193 | |
6b45ffe4 |
194 | unless (is_Operator($arg)) { |
515523bc |
195 | push @arg_final, $arg; |
196 | next; |
197 | } |
198 | |
199 | my $op = $arg->{operator}; |
12e6eab8 |
200 | unless (exists $op->{$self->_operator_type}) { |
201 | $op->{$self->_operator_type} = $self->_convert_op($arg); |
515523bc |
202 | } |
203 | |
12e6eab8 |
204 | if ($op->{$self->_operator_type} eq $op_name) { |
515523bc |
205 | unshift @argq, @{$arg->{args}}; |
206 | } else { |
207 | push @arg_final, $arg; |
208 | } |
209 | } |
9d8243ac |
210 | [ '(', |
211 | intersperse( |
212 | $self->_format_keyword($op_name), |
410dc8ab |
213 | map $self->_maybe_parenthesise($_), @arg_final |
9d8243ac |
214 | ), |
215 | ')' |
216 | ]; |
515523bc |
217 | } |
218 | |
f8343502 |
219 | sub _handle_op_type_in { |
220 | my ($self, $op, $dq) = @_; |
221 | my ($lhs, @in) = @{$dq->{args}}; |
9d8243ac |
222 | [ $self->_render($lhs), |
223 | $op, |
224 | '(', |
225 | intersperse(',', map $self->_render($_), @in), |
226 | ')' |
227 | ]; |
f8343502 |
228 | } |
229 | |
230 | sub _handle_op_type_between { |
231 | my ($self, $op_name, $dq) = @_; |
f3f803da |
232 | my @args = @{$dq->{args}}; |
233 | if (@args == 3) { |
410dc8ab |
234 | my ($lhs, $rhs1, $rhs2) = (map $self->_maybe_parenthesise($_), @args); |
839687b2 |
235 | [ '(', $lhs, $op_name, $rhs1, 'AND', $rhs2, ')' ]; |
6b45ffe4 |
236 | } elsif (@args == 2 and is_Literal $args[1]->{type}) { |
f3f803da |
237 | my ($lhs, $rhs) = (map $self->_render($_), @args); |
839687b2 |
238 | [ '(', $lhs, $op_name, $rhs, ')' ]; |
f3f803da |
239 | } else { |
240 | die "Invalid args for between: ${\scalar @args} given"; |
241 | } |
f8343502 |
242 | } |
243 | |
829906e0 |
244 | sub _handle_op_type_apply { |
245 | my ($self, $op_name, $dq) = @_; |
246 | my ($func, @args) = @{$dq->{args}}; |
247 | die "Function name must be identifier" |
6b45ffe4 |
248 | unless is_Identifier $func; |
54a46537 |
249 | my $ident = do { |
250 | # The problem we have here is that built-ins can't be quoted, generally. |
251 | # I rather wonder if things like MAX(...) need to -not- be handled as |
252 | # an apply and instead of something else, maybe a parenop type - but |
253 | # as an explicitly Naive renderer this seems like a reasonable answer. |
254 | local @{$self}{qw(reserved_ident_parts always_quote)}; |
255 | $self->_render_identifier($func)->[0]; |
256 | }; |
829906e0 |
257 | [ |
258 | "$ident(", |
410dc8ab |
259 | intersperse(',', map $self->_maybe_parenthesise($_), @args), |
829906e0 |
260 | ')' |
261 | ] |
262 | } |
263 | |
515523bc |
264 | sub _convert_op { |
265 | my ($self, $dq) = @_; |
266 | if (my $perl_op = $dq->{'operator'}->{'Perl'}) { |
267 | for ($perl_op) { |
268 | $_ eq '==' and return '='; |
269 | $_ eq 'eq' and return '='; |
270 | $_ eq '!' and return 'NOT'; |
271 | } |
272 | return uc $perl_op; # hope! |
273 | } |
274 | die "Can't convert non-perl op yet"; |
275 | } |
9c8fc055 |
276 | |
277 | sub _render_select { |
278 | my ($self, $dq) = @_; |
279 | die "Empty select list" unless @{$dq->{select}}; |
280 | |
281 | # it is, in fact, completely valid for there to be nothing for us |
282 | # to project from since many databases handle 'SELECT 1;' fine |
283 | |
9d8243ac |
284 | my @select = intersperse(',', |
6b45ffe4 |
285 | map +(is_Alias() |
9d8243ac |
286 | ? $self->_render_alias($_, $self->_format_keyword('AS')) |
287 | : $self->_render($_)), @{$dq->{select}} |
288 | ); |
9c8fc055 |
289 | |
290 | return [ |
291 | $self->_format_keyword('SELECT'), |
292 | \@select, |
293 | # if present this may be a bare FROM, a FROM+WHERE, or a FROM+WHERE+GROUP |
294 | # since we're the SELECT and therefore always come first, we don't care. |
2cf0bb42 |
295 | ($dq->{from} |
296 | ? ($self->_format_keyword('FROM'), @{$self->_render($dq->{from})}) |
297 | : () |
0446ca9c |
298 | ), |
2cf0bb42 |
299 | ]; |
300 | } |
301 | |
302 | sub _render_alias { |
bdb576cb |
303 | my ($self, $dq, $as) = @_; |
2cf0bb42 |
304 | # FROM foo foo -> FROM foo |
305 | # FROM foo.bar bar -> FROM foo.bar |
07b0f7e0 |
306 | if ($self->collapse_aliases) { |
6b45ffe4 |
307 | if (is_Identifier(my $from = $dq->{from})) { |
308 | if ($from->{elements}[-1] eq $dq->{to}) { |
309 | return $self->_render($from); |
07b0f7e0 |
310 | } |
2cf0bb42 |
311 | } |
312 | } |
6b45ffe4 |
313 | return [ |
410dc8ab |
314 | $self->_maybe_parenthesise($dq->{from}), |
bdb576cb |
315 | $as || ' ', |
07b0f7e0 |
316 | $self->_render_identifier({ elements => [ $dq->{to} ] }) |
2cf0bb42 |
317 | ]; |
318 | } |
319 | |
320 | sub _render_literal { |
321 | my ($self, $dq) = @_; |
322 | unless ($dq->{subtype} eq 'SQL') { |
323 | die "Can't render non-SQL literal"; |
324 | } |
e8428681 |
325 | if (defined($dq->{literal})) { |
5edb4362 |
326 | return [ |
327 | $dq->{literal}, @{$dq->{values}||[]} |
328 | ]; |
329 | } elsif ($dq->{parts}) { |
330 | return [ map $self->_render($_), @{$dq->{parts}} ]; |
331 | } else { |
332 | die "Invalid SQL literal - neither 'literal' nor 'parts' found"; |
333 | } |
9c8fc055 |
334 | } |
335 | |
bdb576cb |
336 | sub _render_join { |
337 | my ($self, $dq) = @_; |
07b0f7e0 |
338 | my ($left, $right) = @{$dq}{qw(left right)}; |
2f95eef1 |
339 | my $join = do { |
340 | if ($dq->{outer}) { |
341 | $self->_format_keyword(uc($dq->{outer}).' JOIN'); |
342 | } elsif ($dq->{on}) { |
343 | $self->_format_keyword('JOIN'); |
344 | } else { |
345 | ',' |
346 | } |
347 | }; |
f16c57e9 |
348 | my $rhs = $self->_render($right); |
2f95eef1 |
349 | [ |
f16c57e9 |
350 | $self->_render($left), $join, |
6b45ffe4 |
351 | (is_Join($right) ? ('(', $rhs, ')') : $rhs), |
2f95eef1 |
352 | ($dq->{on} |
353 | ? ($self->_format_keyword('ON'), $self->_render($dq->{on})) |
354 | : ()) |
355 | ]; |
bdb576cb |
356 | } |
357 | |
7f462f86 |
358 | sub _render_where { |
359 | my ($self, $dq) = @_; |
360 | my ($from, $where) = @{$dq}{qw(from where)}; |
6b45ffe4 |
361 | my $keyword = (is_Group($from) ? 'HAVING' : 'WHERE'); |
7f462f86 |
362 | [ |
363 | ($from ? $self->_render($from) : ()), |
a1042388 |
364 | $self->_format_keyword($keyword), |
7f462f86 |
365 | $self->_render($where) |
366 | ] |
367 | } |
368 | |
369 | sub _render_order { |
370 | my ($self, $dq) = @_; |
371 | my @ret = ( |
372 | $self->_format_keyword('ORDER BY'), |
373 | $self->_render($dq->{by}), |
b6b6113e |
374 | ($dq->{reverse} |
375 | ? $self->_format_keyword('DESC') |
0446ca9c |
376 | : ()) |
7f462f86 |
377 | ); |
378 | my $from; |
379 | while ($from = $dq->{from}) { |
6b45ffe4 |
380 | last unless is_Order $from; |
7f462f86 |
381 | $dq = $from; |
382 | push @ret, ( |
383 | ',', |
384 | $self->_render($dq->{by}), |
b6b6113e |
385 | ($dq->{reverse} |
386 | ? $self->_format_keyword('DESC') |
0446ca9c |
387 | : ()) |
7f462f86 |
388 | ); |
389 | } |
390 | unshift @ret, $self->_render($from) if $from; |
391 | \@ret; |
392 | } |
393 | |
a1042388 |
394 | sub _render_group { |
395 | my ($self, $dq) = @_; |
396 | # this could also squash like order does. but I dunno whether that should |
397 | # move somewhere else just yet. |
398 | my @ret = ( |
399 | ($dq->{from} ? $self->_render($dq->{from}) : ()), |
a010d224 |
400 | (@{$dq->{by}} |
401 | ? ( |
402 | $self->_format_keyword('GROUP BY'), |
403 | intersperse(',', map $self->_render($_), @{$dq->{by}}) |
404 | ) |
405 | : ()) |
a1042388 |
406 | ); |
407 | \@ret; |
408 | } |
409 | |
dc657ce1 |
410 | sub _render_delete { |
411 | my ($self, $dq) = @_; |
87867dad |
412 | my ($target, $where) = @{$dq}{qw(target where)}; |
413 | [ $self->_format_keyword('DELETE FROM'), |
414 | $self->_render($target), |
415 | ($where |
416 | ? ($self->_format_keyword('WHERE'), $self->_render($where)) |
417 | : ()) |
418 | ]; |
419 | } |
420 | |
421 | sub _render_update { |
422 | my ($self, $dq) = @_; |
423 | my ($target, $set, $where) = @{$dq}{qw(target set where)}; |
a5c0a2d5 |
424 | unless ($set) { |
425 | die "Must have set key - names+value keys not yet tested"; |
426 | my ($names, $value) = @{$dq}{qw(names value)}; |
427 | die "Must have names and value or set" unless $names and $value; |
428 | die "names and value must be same size" unless @$names == @$value; |
429 | $set = [ map [ $names->[$_], $value->[$_] ], 0..$#$names ]; |
430 | } |
87867dad |
431 | my @rendered_set = intersperse( |
432 | ',', map [ intersperse('=', map $self->_render($_), @$_) ], @{$set} |
433 | ); |
434 | [ $self->_format_keyword('UPDATE'), |
435 | $self->_render($target), |
436 | $self->_format_keyword('SET'), |
437 | @rendered_set, |
438 | ($where |
439 | ? ($self->_format_keyword('WHERE'), $self->_render($where)) |
440 | : ()) |
441 | ]; |
dc657ce1 |
442 | } |
443 | |
a5c0a2d5 |
444 | sub _render_insert { |
445 | my ($self, $dq) = @_; |
446 | my ($target, $names, $values, $returning) |
447 | = @{$dq}{qw(target names values returning)}; |
448 | unless ($values) { |
449 | die "Must have values key - sets key not yet implemented"; |
450 | } |
451 | [ $self->_format_keyword('INSERT INTO'), |
452 | $self->_render($target), |
453 | ($names |
454 | ? ('(', intersperse(',', map $self->_render($_), @$names), ')') |
455 | : ()), |
456 | $self->_format_keyword('VALUES'), |
457 | intersperse(',', |
458 | map [ '(', intersperse(',', map $self->_render($_), @$_), ')' ], |
459 | @$values |
460 | ), |
461 | ($returning |
462 | ? ($self->_format_keyword('RETURNING'), |
463 | intersperse(',', map $self->_render($_), @$returning)) |
464 | : ()), |
465 | ]; |
466 | } |
467 | |
515523bc |
468 | 1; |