1 package Data::Query::Renderer::SQL::Naive;
4 use SQL::ReservedWords;
5 use Data::Query::Constants qw(DQ_IDENTIFIER DQ_OPERATOR DQ_VALUE);
8 bless({ %{$_[1]||{}} }, (ref($_[0])||$_[0]))->BUILDALL;
13 $self->{reserved_ident_parts}
15 our $_DEFAULT_RESERVED ||= { map +($_ => 1), SQL::ReservedWords->words }
17 $self->{quote_chars}||=[''];
18 $self->{simple_ops}||=$self->_default_simple_ops;
22 sub _default_simple_ops {
24 (map +($_ => 'binop'), qw(= > < >= <=) ),
25 (map +($_ => 'unop'), (qw(NOT)) ),
26 (map +($_ => 'flatten'), qw(AND OR) ),
32 $self->_flatten_structure($self->_render(@_))
35 sub _flatten_structure {
36 my ($self, $struct) = @_;
41 elsif ($r eq 'ARRAY') {
42 my ($sql, @b) = @{$self->_flatten_structure($_)};
46 elsif ($r eq 'HASH') { push @bind, $_; () }
47 else { die "_flatten_structure can't handle ref type $r for $_" }
51 # I present this to permit strange people to easily supply a patch to lc()
52 # their keywords, as I have heard many desire to do, lest they infect me
53 # with whatever malady caused this desire by their continued proximity for
54 # want of such a feature.
56 sub _format_keyword { $_[1] }
59 $_[0]->${\"_render_${\lc($_[1]->{type})}"}($_[1]);
62 sub _render_identifier {
63 die "Unidentified identifier (SQL can no has \$_)"
64 unless my @i = @{$_[1]->{elements}};
65 # handle single or paired quote chars
66 my ($q1, $q2) = @{$_[0]->{quote_chars}}[0,-1];
67 my $always_quote = $_[0]->{always_quote};
68 my $res_check = $_[0]->{reserved_ident_parts};
71 $_[0]->{identifier_sep}||'.',
73 $_ eq '*' # Yes, this means you can't have a column just called '*'.
74 ? $_ # Yes, this is a feature. Go shoot the DBA if he disagrees.
75 : ( # reserved are stored uc, quote if non-word
76 $always_quote || $res_check->{+uc} || /\W/
88 sub _operator_type { 'SQL.Naive' }
90 sub _render_operator {
92 my $op = $dq->{operator};
93 unless (exists $op->{$self->_operator_type}) {
94 $op->{$self->_operator_type} = $self->_convert_op($dq);
96 if (my $op_type = $self->{simple_ops}{my $op_name = $op->{$self->_operator_type}}) {
97 return $self->${\"_handle_op_type_${op_type}"}($op_name, $dq);
99 die "Couldn't render operator ".$op->{$self->_operator_type};
102 sub _handle_op_type_binop {
103 my ($self, $op_name, $dq) = @_;
104 die "${op_name} registered as binary op but args contain "
105 .scalar(@{$dq->{args}})." entries"
106 unless @{$dq->{args}} == 2;
108 $self->_render($dq->{args}[0]),
110 $self->_render($dq->{args}[1]),
114 sub _handle_op_type_unop {
115 my ($self, $op_name, $dq) = @_;
116 die "${op_name} registered as unary op but args contain "
117 .scalar(@{$dq->{args}})." entries"
118 unless @{$dq->{args}} == 1;
121 $self->_render($dq->{args}[0]),
125 sub _handle_op_type_flatten {
126 my ($self, $op_name, $dq) = @_;
127 my @argq = @{$dq->{args}};
129 while (my $arg = shift @argq) {
131 unless ($arg->{type} eq DQ_OPERATOR) {
132 push @arg_final, $arg;
136 my $op = $arg->{operator};
137 unless (exists $op->{$self->_operator_type}) {
138 $op->{$self->_operator_type} = $self->_convert_op($arg);
141 if ($op->{$self->_operator_type} eq $op_name) {
142 unshift @argq, @{$arg->{args}};
144 push @arg_final, $arg;
147 my @sql = ('(', map +($self->_render($_), $op_name), @arg_final);
148 $sql[-1] = ')'; # replace final AND or whatever with )
153 my ($self, $dq) = @_;
154 if (my $perl_op = $dq->{'operator'}->{'Perl'}) {
156 $_ eq '==' and return '=';
157 $_ eq 'eq' and return '=';
158 $_ eq '!' and return 'NOT';
160 return uc $perl_op; # hope!
162 die "Can't convert non-perl op yet";
166 my ($self, $dq) = @_;
167 die "Empty select list" unless @{$dq->{select}};
169 # it is, in fact, completely valid for there to be nothing for us
170 # to project from since many databases handle 'SELECT 1;' fine
173 # we should perhaps validate that what we've been handed
174 # is an expression and possibly an identifier - at least a
175 # debugging mode that does such is almost certainly worthwhile;
176 # but for present I'm focusing on making this work.
177 my $e = $self->_render($_->{expr});
178 $_->{name} ? [ $e, 'AS', $self->_render($_->{name}), ',' ] : [ $e, ',' ]
181 # we put the commas inside the [] for each entry as a hint to the pretty
182 # printer downstreamso now we need to eliminate the comma from the last
183 # entry - we know there always is one due to the die guard at the top
188 $self->_format_keyword('SELECT'),
190 # if present this may be a bare FROM, a FROM+WHERE, or a FROM+WHERE+GROUP
191 # since we're the SELECT and therefore always come first, we don't care.
193 ? ($self->_format_keyword('FROM'), @{$self->_render($dq->{from})})
200 my ($self, $dq) = @_;
201 # FROM foo foo -> FROM foo
202 # FROM foo.bar bar -> FROM foo.bar
203 if ($dq->{alias}{type} eq DQ_IDENTIFIER) {
204 if ($dq->{alias}{elements}[-1] eq $dq->{as}) {
205 return $self->_render($dq->{alias});
209 $self->_render($dq->{alias}),
211 $self->_render_identifier({ elements => [ $dq->{as} ] })
215 sub _render_literal {
216 my ($self, $dq) = @_;
217 unless ($dq->{subtype} eq 'SQL') {
218 die "Can't render non-SQL literal";