Commit | Line | Data |
515523bc |
1 | package Data::Query::Renderer::SQL::Naive; |
2 | |
3 | use strictures 1; |
4 | use SQL::ReservedWords; |
bdb576cb |
5 | use Data::Query::Constants qw( |
7f462f86 |
6 | DQ_IDENTIFIER DQ_OPERATOR DQ_VALUE DQ_JOIN DQ_ALIAS DQ_ORDER |
bdb576cb |
7 | ); |
515523bc |
8 | |
9 | sub new { |
10 | bless({ %{$_[1]||{}} }, (ref($_[0])||$_[0]))->BUILDALL; |
11 | } |
12 | |
13 | sub BUILDALL { |
14 | my $self = shift; |
15 | $self->{reserved_ident_parts} |
16 | ||= ( |
17 | our $_DEFAULT_RESERVED ||= { map +($_ => 1), SQL::ReservedWords->words } |
18 | ); |
19 | $self->{quote_chars}||=['']; |
20 | $self->{simple_ops}||=$self->_default_simple_ops; |
21 | return $self; |
22 | } |
23 | |
24 | sub _default_simple_ops { |
25 | +{ |
f8343502 |
26 | (map +($_ => 'binop'), qw(= > < >= <= != LIKE), 'NOT LIKE' ), |
27 | (map +($_ => 'unop'), qw(NOT) ), |
28 | (map +($_ => 'unop_reverse'), ('IS NULL', 'IS NOT NULL')), |
515523bc |
29 | (map +($_ => 'flatten'), qw(AND OR) ), |
f8343502 |
30 | (map +($_ => 'in'), ('IN', 'NOT IN')), |
829906e0 |
31 | (map +($_ => 'between'), ('BETWEEN', 'NOT BETWEEN')), |
32 | (apply => 'apply'), |
515523bc |
33 | } |
34 | } |
35 | |
36 | sub render { |
37 | my $self = shift; |
38 | $self->_flatten_structure($self->_render(@_)) |
39 | } |
40 | |
41 | sub _flatten_structure { |
42 | my ($self, $struct) = @_; |
43 | my @bind; |
bdb576cb |
44 | [ do { |
45 | my @p = map { |
46 | my $r = ref; |
47 | if (!$r) { $_ } |
48 | elsif ($r eq 'ARRAY') { |
49 | my ($sql, @b) = @{$self->_flatten_structure($_)}; |
50 | push @bind, @b; |
51 | $sql; |
52 | } |
53 | elsif ($r eq 'HASH') { push @bind, $_; () } |
54 | else { die "_flatten_structure can't handle ref type $r for $_" } |
55 | } @$struct; |
56 | join '', map { |
57 | ($p[$_], (($p[$_+1]||',') eq ',') ? () : (' ')) |
58 | } 0 .. $#p; |
59 | }, |
60 | @bind |
61 | ]; |
515523bc |
62 | } |
63 | |
9c8fc055 |
64 | # I present this to permit strange people to easily supply a patch to lc() |
65 | # their keywords, as I have heard many desire to do, lest they infect me |
66 | # with whatever malady caused this desire by their continued proximity for |
67 | # want of such a feature. |
68 | |
69 | sub _format_keyword { $_[1] } |
70 | |
515523bc |
71 | sub _render { |
bdb576cb |
72 | $_[0]->${\"_render_${\(lc($_[1]->{type})||'broken')}"}($_[1]); |
73 | } |
74 | |
75 | sub _render_broken { |
76 | my ($self, $dq) = @_; |
77 | require Data::Dumper::Concise; |
78 | die "Broken DQ entry: ".Data::Dumper::Concise::Dumper($dq); |
515523bc |
79 | } |
80 | |
81 | sub _render_identifier { |
82 | die "Unidentified identifier (SQL can no has \$_)" |
83 | unless my @i = @{$_[1]->{elements}}; |
84 | # handle single or paired quote chars |
85 | my ($q1, $q2) = @{$_[0]->{quote_chars}}[0,-1]; |
86 | my $always_quote = $_[0]->{always_quote}; |
87 | my $res_check = $_[0]->{reserved_ident_parts}; |
88 | return [ |
89 | join |
90 | $_[0]->{identifier_sep}||'.', |
91 | map +( |
92 | $_ eq '*' # Yes, this means you can't have a column just called '*'. |
93 | ? $_ # Yes, this is a feature. Go shoot the DBA if he disagrees. |
94 | : ( # reserved are stored uc, quote if non-word |
95 | $always_quote || $res_check->{+uc} || /\W/ |
96 | ? $q1.$_.$q2 |
97 | : $_ |
98 | ) |
99 | ), @i |
100 | ]; |
101 | } |
102 | |
103 | sub _render_value { |
829906e0 |
104 | defined($_[1]->{value}) |
105 | ? [ '?', $_[1] ] |
106 | : [ 'NULL' ]; |
515523bc |
107 | } |
108 | |
12e6eab8 |
109 | sub _operator_type { 'SQL.Naive' } |
110 | |
515523bc |
111 | sub _render_operator { |
112 | my ($self, $dq) = @_; |
113 | my $op = $dq->{operator}; |
12e6eab8 |
114 | unless (exists $op->{$self->_operator_type}) { |
115 | $op->{$self->_operator_type} = $self->_convert_op($dq); |
515523bc |
116 | } |
f8343502 |
117 | my $op_name = $op->{$self->_operator_type}; |
118 | if (my $op_type = $self->{simple_ops}{$op_name}) { |
515523bc |
119 | return $self->${\"_handle_op_type_${op_type}"}($op_name, $dq); |
f8343502 |
120 | } elsif (my $meth = $self->can("_handle_op_special_${op_name}")) { |
121 | return $self->$meth($dq); |
515523bc |
122 | } |
f8343502 |
123 | #die "Couldn't render operator ".$op->{$self->_operator_type}; |
124 | $self->_handle_op_type_binop($op_name, $dq); |
515523bc |
125 | } |
126 | |
127 | sub _handle_op_type_binop { |
128 | my ($self, $op_name, $dq) = @_; |
129 | die "${op_name} registered as binary op but args contain " |
130 | .scalar(@{$dq->{args}})." entries" |
131 | unless @{$dq->{args}} == 2; |
132 | [ |
133 | $self->_render($dq->{args}[0]), |
134 | $op_name, |
135 | $self->_render($dq->{args}[1]), |
136 | ] |
137 | } |
138 | |
139 | sub _handle_op_type_unop { |
140 | my ($self, $op_name, $dq) = @_; |
141 | die "${op_name} registered as unary op but args contain " |
142 | .scalar(@{$dq->{args}})." entries" |
143 | unless @{$dq->{args}} == 1; |
144 | [ |
f8343502 |
145 | '(', |
146 | $op_name, |
147 | $self->_render($dq->{args}[0]), |
148 | ')', |
149 | ] |
150 | } |
151 | |
152 | sub _handle_op_type_unop_reverse { |
153 | my ($self, $op_name, $dq) = @_; |
154 | die "${op_name} registered as unary op but args contain " |
155 | .scalar(@{$dq->{args}})." entries" |
156 | unless @{$dq->{args}} == 1; |
157 | [ |
515523bc |
158 | $self->_render($dq->{args}[0]), |
f8343502 |
159 | $op_name, |
515523bc |
160 | ] |
161 | } |
162 | |
163 | sub _handle_op_type_flatten { |
164 | my ($self, $op_name, $dq) = @_; |
165 | my @argq = @{$dq->{args}}; |
166 | my @arg_final; |
167 | while (my $arg = shift @argq) { |
168 | |
169 | unless ($arg->{type} eq DQ_OPERATOR) { |
170 | push @arg_final, $arg; |
171 | next; |
172 | } |
173 | |
174 | my $op = $arg->{operator}; |
12e6eab8 |
175 | unless (exists $op->{$self->_operator_type}) { |
176 | $op->{$self->_operator_type} = $self->_convert_op($arg); |
515523bc |
177 | } |
178 | |
12e6eab8 |
179 | if ($op->{$self->_operator_type} eq $op_name) { |
515523bc |
180 | unshift @argq, @{$arg->{args}}; |
181 | } else { |
182 | push @arg_final, $arg; |
183 | } |
184 | } |
185 | my @sql = ('(', map +($self->_render($_), $op_name), @arg_final); |
186 | $sql[-1] = ')'; # replace final AND or whatever with ) |
187 | \@sql; |
188 | } |
189 | |
f8343502 |
190 | sub _handle_op_type_in { |
191 | my ($self, $op, $dq) = @_; |
192 | my ($lhs, @in) = @{$dq->{args}}; |
193 | my @rhs = ('(', map +($self->_render($_), ','), @in); |
194 | $rhs[-1] = ')'; |
195 | [ $self->_render($lhs), $op, @rhs ]; |
196 | } |
197 | |
198 | sub _handle_op_type_between { |
199 | my ($self, $op_name, $dq) = @_; |
200 | my ($lhs, $rhs1, $rhs2) = (map $self->_render($_), @{$dq->{args}}); |
201 | [ $lhs, $op_name, $rhs1, 'AND', $rhs2 ]; |
202 | } |
203 | |
829906e0 |
204 | sub _handle_op_type_apply { |
205 | my ($self, $op_name, $dq) = @_; |
206 | my ($func, @args) = @{$dq->{args}}; |
207 | die "Function name must be identifier" |
208 | unless $func->{type} eq DQ_IDENTIFIER; |
209 | my $ident = $self->_render($func)->[0]; |
210 | [ |
211 | "$ident(", |
212 | (map $self->_render($_), @args), |
213 | ')' |
214 | ] |
215 | } |
216 | |
515523bc |
217 | sub _convert_op { |
218 | my ($self, $dq) = @_; |
219 | if (my $perl_op = $dq->{'operator'}->{'Perl'}) { |
220 | for ($perl_op) { |
221 | $_ eq '==' and return '='; |
222 | $_ eq 'eq' and return '='; |
223 | $_ eq '!' and return 'NOT'; |
224 | } |
225 | return uc $perl_op; # hope! |
226 | } |
227 | die "Can't convert non-perl op yet"; |
228 | } |
9c8fc055 |
229 | |
230 | sub _render_select { |
231 | my ($self, $dq) = @_; |
232 | die "Empty select list" unless @{$dq->{select}}; |
233 | |
234 | # it is, in fact, completely valid for there to be nothing for us |
235 | # to project from since many databases handle 'SELECT 1;' fine |
236 | |
bdb576cb |
237 | my @select = map [ |
238 | ($_->{type} eq DQ_ALIAS |
7f462f86 |
239 | ? $self->_render_alias($_, $self->_format_keyword('AS')) |
bdb576cb |
240 | : $self->_render($_) |
241 | ), |
242 | ',' |
243 | ], @{$dq->{select}}; |
9c8fc055 |
244 | |
245 | # we put the commas inside the [] for each entry as a hint to the pretty |
bdb576cb |
246 | # printer downstream so now we need to eliminate the comma from the last |
9c8fc055 |
247 | # entry - we know there always is one due to the die guard at the top |
248 | |
249 | pop @{$select[-1]}; |
250 | |
251 | return [ |
252 | $self->_format_keyword('SELECT'), |
253 | \@select, |
254 | # if present this may be a bare FROM, a FROM+WHERE, or a FROM+WHERE+GROUP |
255 | # since we're the SELECT and therefore always come first, we don't care. |
2cf0bb42 |
256 | ($dq->{from} |
257 | ? ($self->_format_keyword('FROM'), @{$self->_render($dq->{from})}) |
258 | : () |
259 | ) |
260 | ]; |
261 | } |
262 | |
263 | sub _render_alias { |
bdb576cb |
264 | my ($self, $dq, $as) = @_; |
2cf0bb42 |
265 | # FROM foo foo -> FROM foo |
266 | # FROM foo.bar bar -> FROM foo.bar |
267 | if ($dq->{alias}{type} eq DQ_IDENTIFIER) { |
268 | if ($dq->{alias}{elements}[-1] eq $dq->{as}) { |
269 | return $self->_render($dq->{alias}); |
270 | } |
271 | } |
272 | return [ |
273 | $self->_render($dq->{alias}), |
bdb576cb |
274 | $as || ' ', |
2cf0bb42 |
275 | $self->_render_identifier({ elements => [ $dq->{as} ] }) |
276 | ]; |
277 | } |
278 | |
279 | sub _render_literal { |
280 | my ($self, $dq) = @_; |
281 | unless ($dq->{subtype} eq 'SQL') { |
282 | die "Can't render non-SQL literal"; |
283 | } |
284 | return [ |
7f462f86 |
285 | $dq->{literal}, @{$dq->{values}||[]} |
9c8fc055 |
286 | ]; |
287 | } |
288 | |
bdb576cb |
289 | sub _render_join { |
290 | my ($self, $dq) = @_; |
291 | my ($left, $right) = @{$dq->{join}}; |
292 | die "No support for ON yet" if $dq->{on}; |
293 | die "No support for LEFT/RIGHT yet" if $dq->{outer}; |
294 | [ $self->_render($left), ',', $self->_render($right) ]; |
295 | } |
296 | |
7f462f86 |
297 | sub _render_where { |
298 | my ($self, $dq) = @_; |
299 | my ($from, $where) = @{$dq}{qw(from where)}; |
300 | [ |
301 | ($from ? $self->_render($from) : ()), |
302 | $self->_format_keyword('WHERE'), |
303 | $self->_render($where) |
304 | ] |
305 | } |
306 | |
307 | sub _render_order { |
308 | my ($self, $dq) = @_; |
309 | my @ret = ( |
310 | $self->_format_keyword('ORDER BY'), |
311 | $self->_render($dq->{by}), |
312 | ($dq->{direction} ? $self->_format_keyword($dq->{direction}) : ()) |
313 | ); |
314 | my $from; |
315 | while ($from = $dq->{from}) { |
316 | last unless $from->{type} eq DQ_ORDER; |
317 | $dq = $from; |
318 | push @ret, ( |
319 | ',', |
320 | $self->_render($dq->{by}), |
321 | ($dq->{direction} ? $self->_format_keyword($dq->{direction}) : ()) |
322 | ); |
323 | } |
324 | unshift @ret, $self->_render($from) if $from; |
325 | \@ret; |
326 | } |
327 | |
515523bc |
328 | 1; |