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, |
2867f4f5 |
157 | having => 0, |
91916220 |
158 | 'group by' => 0, |
159 | 'order by' => 0, |
7853a177 |
160 | set => 1, |
161 | into => 1, |
91916220 |
162 | values => 1, |
c0eaa9fd |
163 | limit => 1, |
164 | offset => 1, |
165 | skip => 1, |
166 | first => 1, |
7e5600e9 |
167 | ); |
168 | |
75c3a063 |
169 | my %profiles = ( |
170 | console => { |
84c65032 |
171 | fill_in_placeholders => 1, |
9d11f0d4 |
172 | placeholder_surround => ['?/', ''], |
1536de15 |
173 | indent_string => ' ', |
75c3a063 |
174 | indent_amount => 2, |
1536de15 |
175 | newline => "\n", |
3be357b0 |
176 | colormap => {}, |
6d388c84 |
177 | indentmap => \%indents, |
aafbf833 |
178 | |
179 | eval { require Term::ANSIColor } |
180 | ? do { |
181 | my $c = \&Term::ANSIColor::color; |
6d388c84 |
182 | |
183 | my $red = [$c->('red') , $c->('reset')]; |
184 | my $cyan = [$c->('cyan') , $c->('reset')]; |
185 | my $green = [$c->('green') , $c->('reset')]; |
186 | my $yellow = [$c->('yellow') , $c->('reset')]; |
187 | my $blue = [$c->('blue') , $c->('reset')]; |
188 | my $magenta = [$c->('magenta'), $c->('reset')]; |
189 | my $b_o_w = [$c->('black on_white'), $c->('reset')]; |
aafbf833 |
190 | ( |
09931431 |
191 | placeholder_surround => [q(') . $c->('black on_magenta'), $c->('reset') . q(')], |
aafbf833 |
192 | colormap => { |
6d388c84 |
193 | 'begin work' => $b_o_w, |
194 | commit => $b_o_w, |
195 | rollback => $b_o_w, |
196 | savepoint => $b_o_w, |
197 | 'rollback to savepoint' => $b_o_w, |
198 | 'release savepoint' => $b_o_w, |
199 | |
200 | select => $red, |
201 | 'insert into' => $red, |
202 | update => $red, |
203 | 'delete from' => $red, |
204 | |
205 | set => $cyan, |
206 | from => $cyan, |
207 | |
208 | where => $green, |
209 | values => $yellow, |
210 | |
211 | join => $magenta, |
212 | 'left join' => $magenta, |
213 | on => $blue, |
214 | |
215 | 'group by' => $yellow, |
2867f4f5 |
216 | having => $yellow, |
6d388c84 |
217 | 'order by' => $yellow, |
218 | |
219 | skip => $green, |
220 | first => $green, |
221 | limit => $green, |
222 | offset => $green, |
aafbf833 |
223 | } |
224 | ); |
225 | } : (), |
3be357b0 |
226 | }, |
227 | console_monochrome => { |
84c65032 |
228 | fill_in_placeholders => 1, |
9d11f0d4 |
229 | placeholder_surround => ['?/', ''], |
3be357b0 |
230 | indent_string => ' ', |
231 | indent_amount => 2, |
232 | newline => "\n", |
233 | colormap => {}, |
6d388c84 |
234 | indentmap => \%indents, |
7e5600e9 |
235 | }, |
236 | html => { |
84c65032 |
237 | fill_in_placeholders => 1, |
9d11f0d4 |
238 | placeholder_surround => ['<span class="placeholder">', '</span>'], |
7e5600e9 |
239 | indent_string => ' ', |
240 | indent_amount => 2, |
241 | newline => "<br />\n", |
242 | colormap => { |
7853a177 |
243 | select => ['<span class="select">' , '</span>'], |
244 | 'insert into' => ['<span class="insert-into">' , '</span>'], |
245 | update => ['<span class="select">' , '</span>'], |
246 | 'delete from' => ['<span class="delete-from">' , '</span>'], |
c0eaa9fd |
247 | |
248 | set => ['<span class="set">', '</span>'], |
7853a177 |
249 | from => ['<span class="from">' , '</span>'], |
c0eaa9fd |
250 | |
251 | where => ['<span class="where">' , '</span>'], |
252 | values => ['<span class="values">', '</span>'], |
253 | |
7853a177 |
254 | join => ['<span class="join">' , '</span>'], |
c0eaa9fd |
255 | 'left join' => ['<span class="left-join">','</span>'], |
7853a177 |
256 | on => ['<span class="on">' , '</span>'], |
c0eaa9fd |
257 | |
7853a177 |
258 | 'group by' => ['<span class="group-by">', '</span>'], |
2867f4f5 |
259 | having => ['<span class="having">', '</span>'], |
7853a177 |
260 | 'order by' => ['<span class="order-by">', '</span>'], |
c0eaa9fd |
261 | |
262 | skip => ['<span class="skip">', '</span>'], |
263 | first => ['<span class="first">', '</span>'], |
264 | limit => ['<span class="limit">', '</span>'], |
265 | offset => ['<span class="offset">', '</span>'], |
820bb1f5 |
266 | |
267 | 'begin work' => ['<span class="begin-work">', '</span>'], |
268 | commit => ['<span class="commit">', '</span>'], |
269 | rollback => ['<span class="rollback">', '</span>'], |
270 | savepoint => ['<span class="savepoint">', '</span>'], |
271 | 'rollback to savepoint' => ['<span class="rollback-to-savepoint">', '</span>'], |
272 | 'release savepoint' => ['<span class="release-savepoint">', '</span>'], |
1536de15 |
273 | }, |
6d388c84 |
274 | indentmap => \%indents, |
75c3a063 |
275 | }, |
276 | none => { |
1536de15 |
277 | colormap => {}, |
278 | indentmap => {}, |
75c3a063 |
279 | }, |
280 | ); |
281 | |
282 | sub new { |
2fed0b4b |
283 | my $class = shift; |
284 | my $args = shift || {}; |
75c3a063 |
285 | |
286 | my $profile = delete $args->{profile} || 'none'; |
bc482085 |
287 | my $data = $merger->merge( $profiles{$profile}, $args ); |
75c3a063 |
288 | |
289 | bless $data, $class |
290 | } |
d695b0ad |
291 | |
01dd4e4f |
292 | sub parse { |
d695b0ad |
293 | my ($self, $s) = @_; |
01dd4e4f |
294 | |
295 | # tokenize string, and remove all optional whitespace |
296 | my $tokens = []; |
297 | foreach my $token (split $tokenizer_re, $s) { |
b3b79607 |
298 | push @$tokens, $token if ( |
299 | defined $token |
300 | and |
301 | length $token |
09931431 |
302 | and |
b3b79607 |
303 | $token =~ /\S/ |
304 | ); |
01dd4e4f |
305 | } |
b3b79607 |
306 | $self->_recurse_parse($tokens, PARSE_TOP_LEVEL); |
01dd4e4f |
307 | } |
308 | |
309 | sub _recurse_parse { |
d695b0ad |
310 | my ($self, $tokens, $state) = @_; |
01dd4e4f |
311 | |
312 | my $left; |
313 | while (1) { # left-associative parsing |
314 | |
315 | my $lookahead = $tokens->[0]; |
316 | if ( not defined($lookahead) |
317 | or |
318 | ($state == PARSE_IN_PARENS && $lookahead eq ')') |
319 | or |
b3b79607 |
320 | ($state == PARSE_IN_EXPR && $lookahead =~ $expr_term_re ) |
0769ac0e |
321 | or |
b3b79607 |
322 | ($state == PARSE_RHS && $lookahead =~ $rhs_term_re ) |
01dd4e4f |
323 | or |
b3b79607 |
324 | ($state == PARSE_IN_FUNC && $lookahead !~ $func_start_re) # if there are multiple values - the parenthesis will switch the $state |
01dd4e4f |
325 | ) { |
0769ac0e |
326 | return $left||(); |
01dd4e4f |
327 | } |
328 | |
329 | my $token = shift @$tokens; |
330 | |
331 | # nested expression in () |
332 | if ($token eq '(' ) { |
d695b0ad |
333 | my $right = $self->_recurse_parse($tokens, PARSE_IN_PARENS); |
334 | $token = shift @$tokens or croak "missing closing ')' around block " . $self->unparse($right); |
335 | $token eq ')' or croak "unexpected token '$token' terminating block " . $self->unparse($right); |
01dd4e4f |
336 | |
0769ac0e |
337 | $left = $left ? [$left, [PAREN => [$right||()] ]] |
338 | : [PAREN => [$right||()] ]; |
01dd4e4f |
339 | } |
b3b79607 |
340 | # AND/OR and LIST (,) |
341 | elsif ($token =~ /^ (?: OR | AND | \, ) $/xi ) { |
342 | my $op = ($token eq ',') ? 'LIST' : uc $token; |
343 | |
d695b0ad |
344 | my $right = $self->_recurse_parse($tokens, PARSE_IN_EXPR); |
01dd4e4f |
345 | |
346 | # Merge chunks if logic matches |
347 | if (ref $right and $op eq $right->[0]) { |
b3b79607 |
348 | $left = [ (shift @$right ), [$left||(), map { @$_ } @$right] ]; |
01dd4e4f |
349 | } |
350 | else { |
b3b79607 |
351 | $left = [$op => [ $left||(), $right||() ]]; |
01dd4e4f |
352 | } |
353 | } |
354 | # binary operator keywords |
a1e204f4 |
355 | elsif ( $token =~ /^ $binary_op_re $ /x ) { |
01dd4e4f |
356 | my $op = uc $token; |
d695b0ad |
357 | my $right = $self->_recurse_parse($tokens, PARSE_RHS); |
01dd4e4f |
358 | |
359 | # A between with a simple LITERAL for a 1st RHS argument needs a |
360 | # rerun of the search to (hopefully) find the proper AND construct |
361 | if ($op eq 'BETWEEN' and $right->[0] eq 'LITERAL') { |
362 | unshift @$tokens, $right->[1][0]; |
d695b0ad |
363 | $right = $self->_recurse_parse($tokens, PARSE_IN_EXPR); |
01dd4e4f |
364 | } |
365 | |
366 | $left = [$op => [$left, $right] ]; |
367 | } |
368 | # expression terminator keywords (as they start a new expression) |
b3b79607 |
369 | elsif ( $token =~ / ^ $expr_start_re $ /x ) { |
01dd4e4f |
370 | my $op = uc $token; |
d695b0ad |
371 | my $right = $self->_recurse_parse($tokens, PARSE_IN_EXPR); |
efc991a0 |
372 | $left = $left ? [ $left, [$op => [$right||()] ]] |
373 | : [ $op => [$right||()] ]; |
01dd4e4f |
374 | } |
0769ac0e |
375 | # NOT |
376 | elsif ( $token =~ /^ NOT $/ix ) { |
01dd4e4f |
377 | my $op = uc $token; |
d695b0ad |
378 | my $right = $self->_recurse_parse ($tokens, PARSE_RHS); |
01dd4e4f |
379 | $left = $left ? [ @$left, [$op => [$right] ]] |
380 | : [ $op => [$right] ]; |
381 | |
382 | } |
4e914a7c |
383 | elsif ( $token =~ $placeholder_re) { |
384 | $left = $left ? [ $left, [ PLACEHOLDER => [ $token ] ] ] |
385 | : [ PLACEHOLDER => [ $token ] ]; |
386 | } |
b3b79607 |
387 | # we're now in "unknown token" land - start eating tokens until |
388 | # we see something familiar |
01dd4e4f |
389 | else { |
b3b79607 |
390 | my $right; |
391 | |
392 | # check if the current token is an unknown op-start |
393 | if (@$tokens and $tokens->[0] =~ $func_start_re) { |
394 | $right = [ $token => [ $self->_recurse_parse($tokens, PARSE_IN_FUNC) || () ] ]; |
395 | } |
396 | else { |
397 | $right = [ LITERAL => [ $token ] ]; |
398 | } |
399 | |
400 | $left = $left ? [ $left, $right ] |
401 | : $right; |
01dd4e4f |
402 | } |
403 | } |
404 | } |
405 | |
d695b0ad |
406 | sub format_keyword { |
407 | my ($self, $keyword) = @_; |
408 | |
1536de15 |
409 | if (my $around = $self->colormap->{lc $keyword}) { |
d695b0ad |
410 | $keyword = "$around->[0]$keyword$around->[1]"; |
411 | } |
412 | |
413 | return $keyword |
414 | } |
415 | |
728f26a2 |
416 | my %starters = ( |
417 | select => 1, |
418 | update => 1, |
419 | 'insert into' => 1, |
420 | 'delete from' => 1, |
421 | ); |
422 | |
f2ab166a |
423 | sub pad_keyword { |
a24cc3a0 |
424 | my ($self, $keyword, $depth) = @_; |
e171c446 |
425 | |
426 | my $before = ''; |
1536de15 |
427 | if (defined $self->indentmap->{lc $keyword}) { |
428 | $before = $self->newline . $self->indent($depth + $self->indentmap->{lc $keyword}); |
a24cc3a0 |
429 | } |
728f26a2 |
430 | $before = '' if $depth == 0 and defined $starters{lc $keyword}; |
e4570c8e |
431 | return [$before, '']; |
a24cc3a0 |
432 | } |
433 | |
1536de15 |
434 | sub indent { ($_[0]->indent_string||'') x ( ( $_[0]->indent_amount || 0 ) * $_[1] ) } |
a24cc3a0 |
435 | |
a97eb57c |
436 | sub _is_key { |
437 | my ($self, $tree) = @_; |
0569a14f |
438 | $tree = $tree->[0] while ref $tree; |
439 | |
a97eb57c |
440 | defined $tree && defined $self->indentmap->{lc $tree}; |
0569a14f |
441 | } |
442 | |
9d11f0d4 |
443 | sub fill_in_placeholder { |
fb272e73 |
444 | my ($self, $bindargs) = @_; |
445 | |
446 | if ($self->fill_in_placeholders) { |
ad46269d |
447 | my $val = shift @{$bindargs} || ''; |
9d11f0d4 |
448 | my ($left, $right) = @{$self->placeholder_surround}; |
fb272e73 |
449 | $val =~ s/\\/\\\\/g; |
450 | $val =~ s/'/\\'/g; |
ad46269d |
451 | return qq($left$val$right) |
fb272e73 |
452 | } |
453 | return '?' |
454 | } |
455 | |
3a247d23 |
456 | # FIXME - terrible name for a user facing API |
01dd4e4f |
457 | sub unparse { |
3a247d23 |
458 | my ($self, $tree, $bindargs) = @_; |
459 | $self->_unparse($tree, [@{$bindargs||[]}], 0); |
460 | } |
a24cc3a0 |
461 | |
3a247d23 |
462 | sub _unparse { |
463 | my ($self, $tree, $bindargs, $depth) = @_; |
01dd4e4f |
464 | |
0769ac0e |
465 | if (not $tree or not @$tree) { |
01dd4e4f |
466 | return ''; |
467 | } |
a24cc3a0 |
468 | |
0769ac0e |
469 | my ($car, $cdr) = @{$tree}[0,1]; |
470 | |
471 | if (! defined $car or (! ref $car and ! defined $cdr) ) { |
472 | require Data::Dumper; |
473 | Carp::confess( sprintf ( "Internal error - malformed branch at depth $depth:\n%s", |
474 | Data::Dumper::Dumper($tree) |
475 | ) ); |
476 | } |
a24cc3a0 |
477 | |
478 | if (ref $car) { |
3a247d23 |
479 | return join (' ', map $self->_unparse($_, $bindargs, $depth), @$tree); |
01dd4e4f |
480 | } |
a24cc3a0 |
481 | elsif ($car eq 'LITERAL') { |
482 | return $cdr->[0]; |
01dd4e4f |
483 | } |
4e914a7c |
484 | elsif ($car eq 'PLACEHOLDER') { |
485 | return $self->fill_in_placeholder($bindargs); |
486 | } |
a24cc3a0 |
487 | elsif ($car eq 'PAREN') { |
e4570c8e |
488 | return sprintf ('(%s)', |
489 | join (' ', map { $self->_unparse($_, $bindargs, $depth + 2) } @{$cdr} ) |
490 | . |
491 | ($self->_is_key($cdr) |
492 | ? ( $self->newline||'' ) . $self->indent($depth + 1) |
493 | : '' |
494 | ) |
495 | ); |
01dd4e4f |
496 | } |
0769ac0e |
497 | elsif ($car eq 'AND' or $car eq 'OR' or $car =~ / ^ $binary_op_re $ /x ) { |
3a247d23 |
498 | return join (" $car ", map $self->_unparse($_, $bindargs, $depth), @{$cdr}); |
01dd4e4f |
499 | } |
b3b79607 |
500 | elsif ($car eq 'LIST' ) { |
3a247d23 |
501 | return join (', ', map $self->_unparse($_, $bindargs, $depth), @{$cdr}); |
b3b79607 |
502 | } |
01dd4e4f |
503 | else { |
f2ab166a |
504 | my ($l, $r) = @{$self->pad_keyword($car, $depth)}; |
3a247d23 |
505 | return sprintf "$l%s %s$r", $self->format_keyword($car), $self->_unparse($cdr, $bindargs, $depth); |
01dd4e4f |
506 | } |
507 | } |
508 | |
fb272e73 |
509 | sub format { my $self = shift; $self->unparse($self->parse($_[0]), $_[1]) } |
01dd4e4f |
510 | |
511 | 1; |
512 | |
3be357b0 |
513 | =pod |
514 | |
515 | =head1 SYNOPSIS |
516 | |
517 | my $sqla_tree = SQL::Abstract::Tree->new({ profile => 'console' }); |
518 | |
519 | print $sqla_tree->format('SELECT * FROM foo WHERE foo.a > 2'); |
520 | |
521 | # SELECT * |
522 | # FROM foo |
523 | # WHERE foo.a > 2 |
524 | |
6b1bf9f8 |
525 | =head1 METHODS |
526 | |
527 | =head2 new |
528 | |
529 | my $sqla_tree = SQL::Abstract::Tree->new({ profile => 'console' }); |
530 | |
c22f502d |
531 | $args = { |
532 | profile => 'console', # predefined profile to use (default: 'none') |
533 | fill_in_placeholders => 1, # true for placeholder population |
9d11f0d4 |
534 | placeholder_surround => # The strings that will be wrapped around |
535 | [GREEN, RESET], # populated placeholders if the above is set |
c22f502d |
536 | indent_string => ' ', # the string used when indenting |
537 | indent_amount => 2, # how many of above string to use for a single |
538 | # indent level |
539 | newline => "\n", # string for newline |
540 | colormap => { |
541 | select => [RED, RESET], # a pair of strings defining what to surround |
542 | # the keyword with for colorization |
543 | # ... |
544 | }, |
545 | indentmap => { |
546 | select => 0, # A zero means that the keyword will start on |
547 | # a new line |
548 | from => 1, # Any other positive integer means that after |
549 | on => 2, # said newline it will get that many indents |
550 | # ... |
551 | }, |
552 | } |
553 | |
554 | Returns a new SQL::Abstract::Tree object. All arguments are optional. |
555 | |
556 | =head3 profiles |
557 | |
558 | There are four predefined profiles, C<none>, C<console>, C<console_monochrome>, |
559 | and C<html>. Typically a user will probably just use C<console> or |
560 | C<console_monochrome>, but if something about a profile bothers you, merely |
561 | use the profile and override the parts that you don't like. |
562 | |
6b1bf9f8 |
563 | =head2 format |
564 | |
c22f502d |
565 | $sqlat->format('SELECT * FROM bar WHERE x = ?', [1]) |
566 | |
567 | Takes C<$sql> and C<\@bindargs>. |
6b1bf9f8 |
568 | |
1a3cc911 |
569 | Returns a formatting string based on the string passed in |
ee4227a7 |
570 | |
571 | =head2 parse |
572 | |
573 | $sqlat->parse('SELECT * FROM bar WHERE x = ?') |
574 | |
575 | Returns a "tree" representing passed in SQL. Please do not depend on the |
576 | structure of the returned tree. It may be stable at some point, but not yet. |
577 | |
578 | =head2 unparse |
579 | |
580 | $sqlat->parse($tree_structure, \@bindargs) |
581 | |
582 | Transform "tree" into SQL, applying various transforms on the way. |
583 | |
584 | =head2 format_keyword |
585 | |
586 | $sqlat->format_keyword('SELECT') |
587 | |
588 | Currently this just takes a keyword and puts the C<colormap> stuff around it. |
589 | Later on it may do more and allow for coderef based transforms. |
590 | |
f2ab166a |
591 | =head2 pad_keyword |
ee4227a7 |
592 | |
f2ab166a |
593 | my ($before, $after) = @{$sqlat->pad_keyword('SELECT')}; |
ee4227a7 |
594 | |
595 | Returns whitespace to be inserted around a keyword. |
9d11f0d4 |
596 | |
597 | =head2 fill_in_placeholder |
598 | |
599 | my $value = $sqlat->fill_in_placeholder(\@bindargs) |
600 | |
601 | Removes last arg from passed arrayref and returns it, surrounded with |
602 | the values in placeholder_surround, and then surrounded with single quotes. |
f2ab166a |
603 | |
604 | =head2 indent |
605 | |
606 | Returns as many indent strings as indent amounts times the first argument. |
607 | |
608 | =head1 ACCESSORS |
609 | |
610 | =head2 colormap |
611 | |
612 | See L</new> |
613 | |
614 | =head2 fill_in_placeholders |
615 | |
616 | See L</new> |
617 | |
618 | =head2 indent_amount |
619 | |
620 | See L</new> |
621 | |
622 | =head2 indent_string |
623 | |
624 | See L</new> |
625 | |
626 | =head2 indentmap |
627 | |
628 | See L</new> |
629 | |
630 | =head2 newline |
631 | |
632 | See L</new> |
633 | |
634 | =head2 placeholder_surround |
635 | |
636 | See L</new> |
637 | |