1 package Data::Query::Renderer::SQL::Naive;
4 use SQL::ReservedWords;
5 use Data::Query::Constants qw(
6 DQ_IDENTIFIER DQ_OPERATOR DQ_VALUE DQ_JOIN DQ_ALIAS DQ_ORDER DQ_LITERAL
10 bless({ %{$_[1]||{}} }, (ref($_[0])||$_[0]))->BUILDALL;
15 $self->{reserved_ident_parts}
17 our $_DEFAULT_RESERVED ||= { map +($_ => 1), SQL::ReservedWords->words }
19 $self->{quote_chars}||=[''];
20 $self->{simple_ops}||=$self->_default_simple_ops;
24 sub _default_simple_ops {
26 (map +($_ => 'binop'), qw(= > < >= <= != LIKE), 'NOT LIKE' ),
27 (map +($_ => 'unop'), qw(NOT) ),
28 (map +($_ => 'unop_reverse'), ('IS NULL', 'IS NOT NULL')),
29 (map +($_ => 'flatten'), qw(AND OR) ),
30 (map +($_ => 'in'), ('IN', 'NOT IN')),
31 (map +($_ => 'between'), ('BETWEEN', 'NOT BETWEEN')),
38 $self->_flatten_structure($self->_render(@_))
41 sub _flatten_structure {
42 my ($self, $struct) = @_;
48 elsif ($r eq 'ARRAY') {
49 my ($sql, @b) = @{$self->_flatten_structure($_)};
53 elsif ($r eq 'HASH') { push @bind, $_; () }
54 else { die "_flatten_structure can't handle ref type $r for $_" }
57 ($p[$_], (($p[$_+1]||',') eq ',') ? () : (' '))
64 # I presented 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.
69 # Then I realised that SQL::Abstract compatibility work required it.
73 sub _format_keyword { $_[0]->{lc_keywords} ? lc($_[1]) : $_[1] }
76 $_[0]->${\"_render_${\(lc($_[1]->{type})||'broken')}"}($_[1]);
81 require Data::Dumper::Concise;
82 die "Broken DQ entry: ".Data::Dumper::Concise::Dumper($dq);
85 sub _render_identifier {
86 die "Unidentified identifier (SQL can no has \$_)"
87 unless my @i = @{$_[1]->{elements}};
88 # handle single or paired quote chars
89 my ($q1, $q2) = @{$_[0]->{quote_chars}}[0,-1];
90 my $always_quote = $_[0]->{always_quote};
91 my $res_check = $_[0]->{reserved_ident_parts};
94 $_[0]->{identifier_sep}||'.',
96 $_ eq '*' # Yes, this means you can't have a column just called '*'.
97 ? $_ # Yes, this is a feature. Go shoot the DBA if he disagrees.
98 : ( # reserved are stored uc, quote if non-word
99 $always_quote || $res_check->{+uc} || /\W/
108 defined($_[1]->{value})
113 sub _operator_type { 'SQL.Naive' }
115 sub _render_operator {
116 my ($self, $dq) = @_;
117 my $op = $dq->{operator};
118 unless (exists $op->{$self->_operator_type}) {
119 $op->{$self->_operator_type} = $self->_convert_op($dq);
121 my $op_name = $op->{$self->_operator_type};
122 if (my $op_type = $self->{simple_ops}{$op_name}) {
123 return $self->${\"_handle_op_type_${op_type}"}($op_name, $dq);
124 } elsif (my $meth = $self->can("_handle_op_special_${op_name}")) {
125 return $self->$meth($dq);
127 if (my $argc = @{$dq->{args}}) {
129 return $self->_handle_op_type_unop($op_name, $dq);
130 } elsif ($argc == 2) {
131 return $self->_handle_op_type_binop($op_name, $dq);
134 die "Unsure how to handle ${op_name}";
137 sub _handle_op_type_binop {
138 my ($self, $op_name, $dq) = @_;
139 die "${op_name} registered as binary op but args contain "
140 .scalar(@{$dq->{args}})." entries"
141 unless @{$dq->{args}} == 2;
143 $self->_render($dq->{args}[0]),
145 $self->_render($dq->{args}[1]),
149 sub _handle_op_type_unop {
150 my ($self, $op_name, $dq) = @_;
151 die "${op_name} registered as unary op but args contain "
152 .scalar(@{$dq->{args}})." entries"
153 unless @{$dq->{args}} == 1;
157 $self->_render($dq->{args}[0]),
162 sub _handle_op_type_unop_reverse {
163 my ($self, $op_name, $dq) = @_;
164 die "${op_name} registered as unary op but args contain "
165 .scalar(@{$dq->{args}})." entries"
166 unless @{$dq->{args}} == 1;
168 $self->_render($dq->{args}[0]),
173 sub _handle_op_type_flatten {
174 my ($self, $op_name, $dq) = @_;
175 my @argq = @{$dq->{args}};
177 while (my $arg = shift @argq) {
179 unless ($arg->{type} eq DQ_OPERATOR) {
180 push @arg_final, $arg;
184 my $op = $arg->{operator};
185 unless (exists $op->{$self->_operator_type}) {
186 $op->{$self->_operator_type} = $self->_convert_op($arg);
189 if ($op->{$self->_operator_type} eq $op_name) {
190 unshift @argq, @{$arg->{args}};
192 push @arg_final, $arg;
195 my @sql = ('(', map +($self->_render($_), $op_name), @arg_final);
196 $sql[-1] = ')'; # replace final AND or whatever with )
200 sub _handle_op_type_in {
201 my ($self, $op, $dq) = @_;
202 my ($lhs, @in) = @{$dq->{args}};
203 my @rhs = ('(', map +($self->_render($_), ','), @in);
205 [ $self->_render($lhs), $op, @rhs ];
208 sub _handle_op_type_between {
209 my ($self, $op_name, $dq) = @_;
210 my @args = @{$dq->{args}};
212 my ($lhs, $rhs1, $rhs2) = (map $self->_render($_), @args);
213 [ '(', $lhs, $op_name, $rhs1, 'AND', $rhs2, ')' ];
214 } elsif (@args == 2 and $args[1]->{type} eq DQ_LITERAL) {
215 my ($lhs, $rhs) = (map $self->_render($_), @args);
216 [ '(', $lhs, $op_name, $rhs, ')' ];
218 die "Invalid args for between: ${\scalar @args} given";
222 sub _handle_op_type_apply {
223 my ($self, $op_name, $dq) = @_;
224 my ($func, @args) = @{$dq->{args}};
225 die "Function name must be identifier"
226 unless $func->{type} eq DQ_IDENTIFIER;
228 # The problem we have here is that built-ins can't be quoted, generally.
229 # I rather wonder if things like MAX(...) need to -not- be handled as
230 # an apply and instead of something else, maybe a parenop type - but
231 # as an explicitly Naive renderer this seems like a reasonable answer.
232 local @{$self}{qw(reserved_ident_parts always_quote)};
233 $self->_render_identifier($func)->[0];
237 (map $self->_render($_), @args),
243 my ($self, $dq) = @_;
244 if (my $perl_op = $dq->{'operator'}->{'Perl'}) {
246 $_ eq '==' and return '=';
247 $_ eq 'eq' and return '=';
248 $_ eq '!' and return 'NOT';
250 return uc $perl_op; # hope!
252 die "Can't convert non-perl op yet";
256 my ($self, $dq) = @_;
257 die "Empty select list" unless @{$dq->{select}};
259 # it is, in fact, completely valid for there to be nothing for us
260 # to project from since many databases handle 'SELECT 1;' fine
263 ($_->{type} eq DQ_ALIAS
264 ? $self->_render_alias($_, $self->_format_keyword('AS'))
270 # we put the commas inside the [] for each entry as a hint to the pretty
271 # printer downstream so now we need to eliminate the comma from the last
272 # entry - we know there always is one due to the die guard at the top
277 $self->_format_keyword('SELECT'),
279 # if present this may be a bare FROM, a FROM+WHERE, or a FROM+WHERE+GROUP
280 # since we're the SELECT and therefore always come first, we don't care.
282 ? ($self->_format_keyword('FROM'), @{$self->_render($dq->{from})})
289 my ($self, $dq, $as) = @_;
290 # FROM foo foo -> FROM foo
291 # FROM foo.bar bar -> FROM foo.bar
292 if ($dq->{alias}{type} eq DQ_IDENTIFIER) {
293 if ($dq->{alias}{elements}[-1] eq $dq->{as}) {
294 return $self->_render($dq->{alias});
298 $self->_render($dq->{alias}),
300 $self->_render_identifier({ elements => [ $dq->{as} ] })
304 sub _render_literal {
305 my ($self, $dq) = @_;
306 unless ($dq->{subtype} eq 'SQL') {
307 die "Can't render non-SQL literal";
309 if ($dq->{literal}) {
311 $dq->{literal}, @{$dq->{values}||[]}
313 } elsif ($dq->{parts}) {
314 return [ map $self->_render($_), @{$dq->{parts}} ];
316 die "Invalid SQL literal - neither 'literal' nor 'parts' found";
321 my ($self, $dq) = @_;
322 my ($left, $right) = @{$dq->{join}};
323 die "No support for ON yet" if $dq->{on};
324 die "No support for LEFT/RIGHT yet" if $dq->{outer};
325 [ $self->_render($left), ',', $self->_render($right) ];
329 my ($self, $dq) = @_;
330 my ($from, $where) = @{$dq}{qw(from where)};
332 ($from ? $self->_render($from) : ()),
333 $self->_format_keyword('WHERE'),
334 $self->_render($where)
339 my ($self, $dq) = @_;
341 $self->_format_keyword('ORDER BY'),
342 $self->_render($dq->{by}),
343 ($dq->{direction} ? $self->_format_keyword($dq->{direction}) : ())
346 while ($from = $dq->{from}) {
347 last unless $from->{type} eq DQ_ORDER;
351 $self->_render($dq->{by}),
352 ($dq->{direction} ? $self->_format_keyword($dq->{direction}) : ())
355 unshift @ret, $self->_render($from) if $from;