Commit | Line | Data |
01dd4e4f |
1 | package SQL::Abstract::Tree; |
2 | |
3 | use strict; |
4 | use warnings; |
b3b79607 |
5 | no warnings 'qw'; |
01dd4e4f |
6 | use Carp; |
7 | |
0769ac0e |
8 | use Hash::Merge qw//; |
9 | |
10 | use base 'Class::Accessor::Grouped'; |
11 | |
12 | __PACKAGE__->mk_group_accessors( simple => $_ ) for qw( |
13 | newline indent_string indent_amount colormap indentmap fill_in_placeholders |
14 | placeholder_surround |
15 | ); |
2fed0b4b |
16 | |
bc482085 |
17 | my $merger = Hash::Merge->new; |
18 | |
19 | $merger->specify_behavior({ |
2fed0b4b |
20 | SCALAR => { |
21 | SCALAR => sub { $_[1] }, |
22 | ARRAY => sub { [ $_[0], @{$_[1]} ] }, |
23 | HASH => sub { $_[1] }, |
24 | }, |
25 | ARRAY => { |
26 | SCALAR => sub { $_[1] }, |
27 | ARRAY => sub { $_[1] }, |
28 | HASH => sub { $_[1] }, |
29 | }, |
30 | HASH => { |
31 | SCALAR => sub { $_[1] }, |
32 | ARRAY => sub { [ values %{$_[0]}, @{$_[1]} ] }, |
33 | HASH => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) }, |
34 | }, |
0769ac0e |
35 | }, 'SQLA::Tree Behavior' ); |
1536de15 |
36 | |
0769ac0e |
37 | my $op_look_ahead = '(?: (?= [\s\)\(\;] ) | \z)'; |
b3b79607 |
38 | my $op_look_behind = '(?: (?<= [\,\s\)\(] ) | \A )'; |
39 | |
0769ac0e |
40 | my $quote_left = qr/[\`\'\"\[]/; |
41 | my $quote_right = qr/[\`\'\"\]]/; |
01dd4e4f |
42 | |
4e914a7c |
43 | my $placeholder_re = qr/(?: \? | \$\d+ )/x; |
44 | |
01dd4e4f |
45 | # These SQL keywords always signal end of the current expression (except inside |
46 | # of a parenthesized subexpression). |
0769ac0e |
47 | # Format: A list of strings that will be compiled to extended syntax ie. |
01dd4e4f |
48 | # /.../x) regexes, without capturing parentheses. They will be automatically |
0769ac0e |
49 | # anchored to op boundaries (excluding quotes) to match the whole token. |
50 | my @expression_start_keywords = ( |
01dd4e4f |
51 | 'SELECT', |
7853a177 |
52 | 'UPDATE', |
53 | 'INSERT \s+ INTO', |
54 | 'DELETE \s+ FROM', |
3d910890 |
55 | 'FROM', |
7853a177 |
56 | 'SET', |
01dd4e4f |
57 | '(?: |
58 | (?: |
0769ac0e |
59 | (?: (?: LEFT | RIGHT | FULL ) \s+ )? |
60 | (?: (?: CROSS | INNER | OUTER ) \s+ )? |
01dd4e4f |
61 | )? |
62 | JOIN |
63 | )', |
64 | 'ON', |
65 | 'WHERE', |
efc991a0 |
66 | '(?: DEFAULT \s+ )? VALUES', |
01dd4e4f |
67 | 'EXISTS', |
68 | 'GROUP \s+ BY', |
69 | 'HAVING', |
70 | 'ORDER \s+ BY', |
c0eaa9fd |
71 | 'SKIP', |
72 | 'FIRST', |
01dd4e4f |
73 | 'LIMIT', |
74 | 'OFFSET', |
75 | 'FOR', |
76 | 'UNION', |
77 | 'INTERSECT', |
78 | 'EXCEPT', |
820bb1f5 |
79 | 'BEGIN \s+ WORK', |
80 | 'COMMIT', |
81 | 'ROLLBACK \s+ TO \s+ SAVEPOINT', |
82 | 'ROLLBACK', |
83 | 'SAVEPOINT', |
84 | 'RELEASE \s+ SAVEPOINT', |
01dd4e4f |
85 | 'RETURNING', |
8d0dd7dc |
86 | 'ROW_NUMBER \s* \( \s* \) \s+ OVER', |
01dd4e4f |
87 | ); |
88 | |
b3b79607 |
89 | my $expr_start_re = join ("\n\t|\n", @expression_start_keywords ); |
90 | $expr_start_re = qr/ $op_look_behind (?i: $expr_start_re ) $op_look_ahead /x; |
0769ac0e |
91 | |
01dd4e4f |
92 | # These are binary operator keywords always a single LHS and RHS |
93 | # * AND/OR are handled separately as they are N-ary |
94 | # * so is NOT as being unary |
95 | # * BETWEEN without paranthesis around the ANDed arguments (which |
96 | # makes it a non-binary op) is detected and accomodated in |
97 | # _recurse_parse() |
01dd4e4f |
98 | |
0769ac0e |
99 | # this will be included in the $binary_op_re, the distinction is interesting during |
100 | # testing as one is tighter than the other, plus mathops have different look |
101 | # ahead/behind (e.g. "x"="y" ) |
102 | my @math_op_keywords = (qw/ < > != <> = <= >= /); |
103 | my $math_re = join ("\n\t|\n", map |
104 | { "(?: (?<= [\\w\\s] | $quote_right ) | \\A )" . quotemeta ($_) . "(?: (?= [\\w\\s] | $quote_left ) | \\z )" } |
105 | @math_op_keywords |
01dd4e4f |
106 | ); |
b7b0f832 |
107 | $math_re = qr/$math_re/x; |
0769ac0e |
108 | |
109 | sub _math_op_re { $math_re } |
110 | |
111 | |
112 | my $binary_op_re = '(?: NOT \s+)? (?:' . join ('|', qw/IN BETWEEN R?LIKE/) . ')'; |
b3b79607 |
113 | $binary_op_re = join "\n\t|\n", |
114 | "$op_look_behind (?i: $binary_op_re ) $op_look_ahead", |
115 | $math_re, |
116 | $op_look_behind . 'IS (?:\s+ NOT)?' . "(?= \\s+ NULL \\b | $op_look_ahead )", |
117 | ; |
b7b0f832 |
118 | $binary_op_re = qr/$binary_op_re/x; |
0769ac0e |
119 | |
120 | sub _binary_op_re { $binary_op_re } |
121 | |
b3b79607 |
122 | my $all_known_re = join("\n\t|\n", |
123 | $expr_start_re, |
0769ac0e |
124 | $binary_op_re, |
125 | "$op_look_behind (?i: AND|OR|NOT ) $op_look_ahead", |
b3b79607 |
126 | (map { quotemeta $_ } qw/, ( ) */), |
4e914a7c |
127 | $placeholder_re, |
0769ac0e |
128 | ); |
01dd4e4f |
129 | |
b3b79607 |
130 | $all_known_re = qr/$all_known_re/x; |
131 | |
132 | #this one *is* capturing for the split below |
133 | # splits on whitespace if all else fails |
134 | my $tokenizer_re = qr/ \s* ( $all_known_re ) \s* | \s+ /x; |
135 | |
136 | # Parser states for _recurse_parse() |
137 | use constant PARSE_TOP_LEVEL => 0; |
138 | use constant PARSE_IN_EXPR => 1; |
139 | use constant PARSE_IN_PARENS => 2; |
140 | use constant PARSE_IN_FUNC => 3; |
141 | use constant PARSE_RHS => 4; |
142 | |
143 | my $expr_term_re = qr/ ^ (?: $expr_start_re | \) ) $/x; |
144 | my $rhs_term_re = qr/ ^ (?: $expr_term_re | $binary_op_re | (?i: AND | OR | NOT | \, ) ) $/x; |
4e914a7c |
145 | my $func_start_re = qr/^ (?: \* | $placeholder_re | \( ) $/x; |
01dd4e4f |
146 | |
7e5600e9 |
147 | my %indents = ( |
7853a177 |
148 | select => 0, |
149 | update => 0, |
150 | 'insert into' => 0, |
151 | 'delete from' => 0, |
3d910890 |
152 | from => 1, |
91916220 |
153 | where => 0, |
7853a177 |
154 | join => 1, |
155 | 'left join' => 1, |
156 | on => 2, |
91916220 |
157 | 'group by' => 0, |
158 | 'order by' => 0, |
7853a177 |
159 | set => 1, |
160 | into => 1, |
91916220 |
161 | values => 1, |
c0eaa9fd |
162 | limit => 1, |
163 | offset => 1, |
164 | skip => 1, |
165 | first => 1, |
7e5600e9 |
166 | ); |
167 | |
75c3a063 |
168 | my %profiles = ( |
169 | console => { |
84c65032 |
170 | fill_in_placeholders => 1, |
9d11f0d4 |
171 | placeholder_surround => ['?/', ''], |
1536de15 |
172 | indent_string => ' ', |
75c3a063 |
173 | indent_amount => 2, |
1536de15 |
174 | newline => "\n", |
3be357b0 |
175 | colormap => {}, |
7e5600e9 |
176 | indentmap => { %indents }, |
aafbf833 |
177 | |
178 | eval { require Term::ANSIColor } |
179 | ? do { |
180 | my $c = \&Term::ANSIColor::color; |
181 | ( |
09931431 |
182 | placeholder_surround => [q(') . $c->('black on_magenta'), $c->('reset') . q(')], |
aafbf833 |
183 | colormap => { |
820bb1f5 |
184 | 'begin work' => [$c->('black on_white'), $c->('reset')], |
185 | commit => [$c->('black on_white'), $c->('reset')], |
186 | rollback => [$c->('black on_white'), $c->('reset')], |
187 | savepoint => [$c->('black on_white'), $c->('reset')], |
188 | 'rollback to savepoint' => [$c->('black on_white'), $c->('reset')], |
189 | 'release savepoint' => [$c->('black on_white'), $c->('reset')], |
190 | |
aafbf833 |
191 | select => [$c->('red'), $c->('reset')], |
192 | 'insert into' => [$c->('red'), $c->('reset')], |
193 | update => [$c->('red'), $c->('reset')], |
194 | 'delete from' => [$c->('red'), $c->('reset')], |
195 | |
196 | set => [$c->('cyan'), $c->('reset')], |
197 | from => [$c->('cyan'), $c->('reset')], |
198 | |
199 | where => [$c->('green'), $c->('reset')], |
200 | values => [$c->('yellow'), $c->('reset')], |
201 | |
202 | join => [$c->('magenta'), $c->('reset')], |
203 | 'left join' => [$c->('magenta'), $c->('reset')], |
204 | on => [$c->('blue'), $c->('reset')], |
205 | |
206 | 'group by' => [$c->('yellow'), $c->('reset')], |
207 | 'order by' => [$c->('yellow'), $c->('reset')], |
c0eaa9fd |
208 | |
209 | skip => [$c->('green'), $c->('reset')], |
210 | first => [$c->('green'), $c->('reset')], |
211 | limit => [$c->('green'), $c->('reset')], |
212 | offset => [$c->('green'), $c->('reset')], |
aafbf833 |
213 | } |
214 | ); |
215 | } : (), |
3be357b0 |
216 | }, |
217 | console_monochrome => { |
84c65032 |
218 | fill_in_placeholders => 1, |
9d11f0d4 |
219 | placeholder_surround => ['?/', ''], |
3be357b0 |
220 | indent_string => ' ', |
221 | indent_amount => 2, |
222 | newline => "\n", |
223 | colormap => {}, |
7e5600e9 |
224 | indentmap => { %indents }, |
225 | }, |
226 | html => { |
84c65032 |
227 | fill_in_placeholders => 1, |
9d11f0d4 |
228 | placeholder_surround => ['<span class="placeholder">', '</span>'], |
7e5600e9 |
229 | indent_string => ' ', |
230 | indent_amount => 2, |
231 | newline => "<br />\n", |
232 | colormap => { |
7853a177 |
233 | select => ['<span class="select">' , '</span>'], |
234 | 'insert into' => ['<span class="insert-into">' , '</span>'], |
235 | update => ['<span class="select">' , '</span>'], |
236 | 'delete from' => ['<span class="delete-from">' , '</span>'], |
c0eaa9fd |
237 | |
238 | set => ['<span class="set">', '</span>'], |
7853a177 |
239 | from => ['<span class="from">' , '</span>'], |
c0eaa9fd |
240 | |
241 | where => ['<span class="where">' , '</span>'], |
242 | values => ['<span class="values">', '</span>'], |
243 | |
7853a177 |
244 | join => ['<span class="join">' , '</span>'], |
c0eaa9fd |
245 | 'left join' => ['<span class="left-join">','</span>'], |
7853a177 |
246 | on => ['<span class="on">' , '</span>'], |
c0eaa9fd |
247 | |
7853a177 |
248 | 'group by' => ['<span class="group-by">', '</span>'], |
249 | 'order by' => ['<span class="order-by">', '</span>'], |
c0eaa9fd |
250 | |
251 | skip => ['<span class="skip">', '</span>'], |
252 | first => ['<span class="first">', '</span>'], |
253 | limit => ['<span class="limit">', '</span>'], |
254 | offset => ['<span class="offset">', '</span>'], |
820bb1f5 |
255 | |
256 | 'begin work' => ['<span class="begin-work">', '</span>'], |
257 | commit => ['<span class="commit">', '</span>'], |
258 | rollback => ['<span class="rollback">', '</span>'], |
259 | savepoint => ['<span class="savepoint">', '</span>'], |
260 | 'rollback to savepoint' => ['<span class="rollback-to-savepoint">', '</span>'], |
261 | 'release savepoint' => ['<span class="release-savepoint">', '</span>'], |
1536de15 |
262 | }, |
7e5600e9 |
263 | indentmap => { %indents }, |
75c3a063 |
264 | }, |
265 | none => { |
1536de15 |
266 | colormap => {}, |
267 | indentmap => {}, |
75c3a063 |
268 | }, |
269 | ); |
270 | |
271 | sub new { |
2fed0b4b |
272 | my $class = shift; |
273 | my $args = shift || {}; |
75c3a063 |
274 | |
275 | my $profile = delete $args->{profile} || 'none'; |
bc482085 |
276 | my $data = $merger->merge( $profiles{$profile}, $args ); |
75c3a063 |
277 | |
278 | bless $data, $class |
279 | } |
d695b0ad |
280 | |
01dd4e4f |
281 | sub parse { |
d695b0ad |
282 | my ($self, $s) = @_; |
01dd4e4f |
283 | |
284 | # tokenize string, and remove all optional whitespace |
285 | my $tokens = []; |
286 | foreach my $token (split $tokenizer_re, $s) { |
b3b79607 |
287 | push @$tokens, $token if ( |
288 | defined $token |
289 | and |
290 | length $token |
09931431 |
291 | and |
b3b79607 |
292 | $token =~ /\S/ |
293 | ); |
01dd4e4f |
294 | } |
b3b79607 |
295 | $self->_recurse_parse($tokens, PARSE_TOP_LEVEL); |
01dd4e4f |
296 | } |
297 | |
298 | sub _recurse_parse { |
d695b0ad |
299 | my ($self, $tokens, $state) = @_; |
01dd4e4f |
300 | |
301 | my $left; |
302 | while (1) { # left-associative parsing |
303 | |
304 | my $lookahead = $tokens->[0]; |
305 | if ( not defined($lookahead) |
306 | or |
307 | ($state == PARSE_IN_PARENS && $lookahead eq ')') |
308 | or |
b3b79607 |
309 | ($state == PARSE_IN_EXPR && $lookahead =~ $expr_term_re ) |
0769ac0e |
310 | or |
b3b79607 |
311 | ($state == PARSE_RHS && $lookahead =~ $rhs_term_re ) |
01dd4e4f |
312 | or |
b3b79607 |
313 | ($state == PARSE_IN_FUNC && $lookahead !~ $func_start_re) # if there are multiple values - the parenthesis will switch the $state |
01dd4e4f |
314 | ) { |
0769ac0e |
315 | return $left||(); |
01dd4e4f |
316 | } |
317 | |
318 | my $token = shift @$tokens; |
319 | |
320 | # nested expression in () |
321 | if ($token eq '(' ) { |
d695b0ad |
322 | my $right = $self->_recurse_parse($tokens, PARSE_IN_PARENS); |
323 | $token = shift @$tokens or croak "missing closing ')' around block " . $self->unparse($right); |
324 | $token eq ')' or croak "unexpected token '$token' terminating block " . $self->unparse($right); |
01dd4e4f |
325 | |
0769ac0e |
326 | $left = $left ? [$left, [PAREN => [$right||()] ]] |
327 | : [PAREN => [$right||()] ]; |
01dd4e4f |
328 | } |
b3b79607 |
329 | # AND/OR and LIST (,) |
330 | elsif ($token =~ /^ (?: OR | AND | \, ) $/xi ) { |
331 | my $op = ($token eq ',') ? 'LIST' : uc $token; |
332 | |
d695b0ad |
333 | my $right = $self->_recurse_parse($tokens, PARSE_IN_EXPR); |
01dd4e4f |
334 | |
335 | # Merge chunks if logic matches |
336 | if (ref $right and $op eq $right->[0]) { |
b3b79607 |
337 | $left = [ (shift @$right ), [$left||(), map { @$_ } @$right] ]; |
01dd4e4f |
338 | } |
339 | else { |
b3b79607 |
340 | $left = [$op => [ $left||(), $right||() ]]; |
01dd4e4f |
341 | } |
342 | } |
343 | # binary operator keywords |
a1e204f4 |
344 | elsif ( $token =~ /^ $binary_op_re $ /x ) { |
01dd4e4f |
345 | my $op = uc $token; |
d695b0ad |
346 | my $right = $self->_recurse_parse($tokens, PARSE_RHS); |
01dd4e4f |
347 | |
348 | # A between with a simple LITERAL for a 1st RHS argument needs a |
349 | # rerun of the search to (hopefully) find the proper AND construct |
350 | if ($op eq 'BETWEEN' and $right->[0] eq 'LITERAL') { |
351 | unshift @$tokens, $right->[1][0]; |
d695b0ad |
352 | $right = $self->_recurse_parse($tokens, PARSE_IN_EXPR); |
01dd4e4f |
353 | } |
354 | |
355 | $left = [$op => [$left, $right] ]; |
356 | } |
357 | # expression terminator keywords (as they start a new expression) |
b3b79607 |
358 | elsif ( $token =~ / ^ $expr_start_re $ /x ) { |
01dd4e4f |
359 | my $op = uc $token; |
d695b0ad |
360 | my $right = $self->_recurse_parse($tokens, PARSE_IN_EXPR); |
efc991a0 |
361 | $left = $left ? [ $left, [$op => [$right||()] ]] |
362 | : [ $op => [$right||()] ]; |
01dd4e4f |
363 | } |
0769ac0e |
364 | # NOT |
365 | elsif ( $token =~ /^ NOT $/ix ) { |
01dd4e4f |
366 | my $op = uc $token; |
d695b0ad |
367 | my $right = $self->_recurse_parse ($tokens, PARSE_RHS); |
01dd4e4f |
368 | $left = $left ? [ @$left, [$op => [$right] ]] |
369 | : [ $op => [$right] ]; |
370 | |
371 | } |
4e914a7c |
372 | elsif ( $token =~ $placeholder_re) { |
373 | $left = $left ? [ $left, [ PLACEHOLDER => [ $token ] ] ] |
374 | : [ PLACEHOLDER => [ $token ] ]; |
375 | } |
b3b79607 |
376 | # we're now in "unknown token" land - start eating tokens until |
377 | # we see something familiar |
01dd4e4f |
378 | else { |
b3b79607 |
379 | my $right; |
380 | |
381 | # check if the current token is an unknown op-start |
382 | if (@$tokens and $tokens->[0] =~ $func_start_re) { |
383 | $right = [ $token => [ $self->_recurse_parse($tokens, PARSE_IN_FUNC) || () ] ]; |
384 | } |
385 | else { |
386 | $right = [ LITERAL => [ $token ] ]; |
387 | } |
388 | |
389 | $left = $left ? [ $left, $right ] |
390 | : $right; |
01dd4e4f |
391 | } |
392 | } |
393 | } |
394 | |
d695b0ad |
395 | sub format_keyword { |
396 | my ($self, $keyword) = @_; |
397 | |
1536de15 |
398 | if (my $around = $self->colormap->{lc $keyword}) { |
d695b0ad |
399 | $keyword = "$around->[0]$keyword$around->[1]"; |
400 | } |
401 | |
402 | return $keyword |
403 | } |
404 | |
728f26a2 |
405 | my %starters = ( |
406 | select => 1, |
407 | update => 1, |
408 | 'insert into' => 1, |
409 | 'delete from' => 1, |
410 | ); |
411 | |
f2ab166a |
412 | sub pad_keyword { |
a24cc3a0 |
413 | my ($self, $keyword, $depth) = @_; |
e171c446 |
414 | |
415 | my $before = ''; |
1536de15 |
416 | if (defined $self->indentmap->{lc $keyword}) { |
417 | $before = $self->newline . $self->indent($depth + $self->indentmap->{lc $keyword}); |
a24cc3a0 |
418 | } |
728f26a2 |
419 | $before = '' if $depth == 0 and defined $starters{lc $keyword}; |
e4570c8e |
420 | return [$before, '']; |
a24cc3a0 |
421 | } |
422 | |
1536de15 |
423 | sub indent { ($_[0]->indent_string||'') x ( ( $_[0]->indent_amount || 0 ) * $_[1] ) } |
a24cc3a0 |
424 | |
a97eb57c |
425 | sub _is_key { |
426 | my ($self, $tree) = @_; |
0569a14f |
427 | $tree = $tree->[0] while ref $tree; |
428 | |
a97eb57c |
429 | defined $tree && defined $self->indentmap->{lc $tree}; |
0569a14f |
430 | } |
431 | |
9d11f0d4 |
432 | sub fill_in_placeholder { |
fb272e73 |
433 | my ($self, $bindargs) = @_; |
434 | |
435 | if ($self->fill_in_placeholders) { |
ad46269d |
436 | my $val = shift @{$bindargs} || ''; |
9d11f0d4 |
437 | my ($left, $right) = @{$self->placeholder_surround}; |
fb272e73 |
438 | $val =~ s/\\/\\\\/g; |
439 | $val =~ s/'/\\'/g; |
ad46269d |
440 | return qq($left$val$right) |
fb272e73 |
441 | } |
442 | return '?' |
443 | } |
444 | |
3a247d23 |
445 | # FIXME - terrible name for a user facing API |
01dd4e4f |
446 | sub unparse { |
3a247d23 |
447 | my ($self, $tree, $bindargs) = @_; |
448 | $self->_unparse($tree, [@{$bindargs||[]}], 0); |
449 | } |
a24cc3a0 |
450 | |
3a247d23 |
451 | sub _unparse { |
452 | my ($self, $tree, $bindargs, $depth) = @_; |
01dd4e4f |
453 | |
0769ac0e |
454 | if (not $tree or not @$tree) { |
01dd4e4f |
455 | return ''; |
456 | } |
a24cc3a0 |
457 | |
0769ac0e |
458 | my ($car, $cdr) = @{$tree}[0,1]; |
459 | |
460 | if (! defined $car or (! ref $car and ! defined $cdr) ) { |
461 | require Data::Dumper; |
462 | Carp::confess( sprintf ( "Internal error - malformed branch at depth $depth:\n%s", |
463 | Data::Dumper::Dumper($tree) |
464 | ) ); |
465 | } |
a24cc3a0 |
466 | |
467 | if (ref $car) { |
3a247d23 |
468 | return join (' ', map $self->_unparse($_, $bindargs, $depth), @$tree); |
01dd4e4f |
469 | } |
a24cc3a0 |
470 | elsif ($car eq 'LITERAL') { |
471 | return $cdr->[0]; |
01dd4e4f |
472 | } |
4e914a7c |
473 | elsif ($car eq 'PLACEHOLDER') { |
474 | return $self->fill_in_placeholder($bindargs); |
475 | } |
a24cc3a0 |
476 | elsif ($car eq 'PAREN') { |
e4570c8e |
477 | return sprintf ('(%s)', |
478 | join (' ', map { $self->_unparse($_, $bindargs, $depth + 2) } @{$cdr} ) |
479 | . |
480 | ($self->_is_key($cdr) |
481 | ? ( $self->newline||'' ) . $self->indent($depth + 1) |
482 | : '' |
483 | ) |
484 | ); |
01dd4e4f |
485 | } |
0769ac0e |
486 | elsif ($car eq 'AND' or $car eq 'OR' or $car =~ / ^ $binary_op_re $ /x ) { |
3a247d23 |
487 | return join (" $car ", map $self->_unparse($_, $bindargs, $depth), @{$cdr}); |
01dd4e4f |
488 | } |
b3b79607 |
489 | elsif ($car eq 'LIST' ) { |
3a247d23 |
490 | return join (', ', map $self->_unparse($_, $bindargs, $depth), @{$cdr}); |
b3b79607 |
491 | } |
01dd4e4f |
492 | else { |
f2ab166a |
493 | my ($l, $r) = @{$self->pad_keyword($car, $depth)}; |
3a247d23 |
494 | return sprintf "$l%s %s$r", $self->format_keyword($car), $self->_unparse($cdr, $bindargs, $depth); |
01dd4e4f |
495 | } |
496 | } |
497 | |
fb272e73 |
498 | sub format { my $self = shift; $self->unparse($self->parse($_[0]), $_[1]) } |
01dd4e4f |
499 | |
500 | 1; |
501 | |
3be357b0 |
502 | =pod |
503 | |
504 | =head1 SYNOPSIS |
505 | |
506 | my $sqla_tree = SQL::Abstract::Tree->new({ profile => 'console' }); |
507 | |
508 | print $sqla_tree->format('SELECT * FROM foo WHERE foo.a > 2'); |
509 | |
510 | # SELECT * |
511 | # FROM foo |
512 | # WHERE foo.a > 2 |
513 | |
6b1bf9f8 |
514 | =head1 METHODS |
515 | |
516 | =head2 new |
517 | |
518 | my $sqla_tree = SQL::Abstract::Tree->new({ profile => 'console' }); |
519 | |
c22f502d |
520 | $args = { |
521 | profile => 'console', # predefined profile to use (default: 'none') |
522 | fill_in_placeholders => 1, # true for placeholder population |
9d11f0d4 |
523 | placeholder_surround => # The strings that will be wrapped around |
524 | [GREEN, RESET], # populated placeholders if the above is set |
c22f502d |
525 | indent_string => ' ', # the string used when indenting |
526 | indent_amount => 2, # how many of above string to use for a single |
527 | # indent level |
528 | newline => "\n", # string for newline |
529 | colormap => { |
530 | select => [RED, RESET], # a pair of strings defining what to surround |
531 | # the keyword with for colorization |
532 | # ... |
533 | }, |
534 | indentmap => { |
535 | select => 0, # A zero means that the keyword will start on |
536 | # a new line |
537 | from => 1, # Any other positive integer means that after |
538 | on => 2, # said newline it will get that many indents |
539 | # ... |
540 | }, |
541 | } |
542 | |
543 | Returns a new SQL::Abstract::Tree object. All arguments are optional. |
544 | |
545 | =head3 profiles |
546 | |
547 | There are four predefined profiles, C<none>, C<console>, C<console_monochrome>, |
548 | and C<html>. Typically a user will probably just use C<console> or |
549 | C<console_monochrome>, but if something about a profile bothers you, merely |
550 | use the profile and override the parts that you don't like. |
551 | |
6b1bf9f8 |
552 | =head2 format |
553 | |
c22f502d |
554 | $sqlat->format('SELECT * FROM bar WHERE x = ?', [1]) |
555 | |
556 | Takes C<$sql> and C<\@bindargs>. |
6b1bf9f8 |
557 | |
1a3cc911 |
558 | Returns a formatting string based on the string passed in |
ee4227a7 |
559 | |
560 | =head2 parse |
561 | |
562 | $sqlat->parse('SELECT * FROM bar WHERE x = ?') |
563 | |
564 | Returns a "tree" representing passed in SQL. Please do not depend on the |
565 | structure of the returned tree. It may be stable at some point, but not yet. |
566 | |
567 | =head2 unparse |
568 | |
569 | $sqlat->parse($tree_structure, \@bindargs) |
570 | |
571 | Transform "tree" into SQL, applying various transforms on the way. |
572 | |
573 | =head2 format_keyword |
574 | |
575 | $sqlat->format_keyword('SELECT') |
576 | |
577 | Currently this just takes a keyword and puts the C<colormap> stuff around it. |
578 | Later on it may do more and allow for coderef based transforms. |
579 | |
f2ab166a |
580 | =head2 pad_keyword |
ee4227a7 |
581 | |
f2ab166a |
582 | my ($before, $after) = @{$sqlat->pad_keyword('SELECT')}; |
ee4227a7 |
583 | |
584 | Returns whitespace to be inserted around a keyword. |
9d11f0d4 |
585 | |
586 | =head2 fill_in_placeholder |
587 | |
588 | my $value = $sqlat->fill_in_placeholder(\@bindargs) |
589 | |
590 | Removes last arg from passed arrayref and returns it, surrounded with |
591 | the values in placeholder_surround, and then surrounded with single quotes. |
f2ab166a |
592 | |
593 | =head2 indent |
594 | |
595 | Returns as many indent strings as indent amounts times the first argument. |
596 | |
597 | =head1 ACCESSORS |
598 | |
599 | =head2 colormap |
600 | |
601 | See L</new> |
602 | |
603 | =head2 fill_in_placeholders |
604 | |
605 | See L</new> |
606 | |
607 | =head2 indent_amount |
608 | |
609 | See L</new> |
610 | |
611 | =head2 indent_string |
612 | |
613 | See L</new> |
614 | |
615 | =head2 indentmap |
616 | |
617 | See L</new> |
618 | |
619 | =head2 newline |
620 | |
621 | See L</new> |
622 | |
623 | =head2 placeholder_surround |
624 | |
625 | See L</new> |
626 | |