add html profile
[dbsrgits/SQL-Abstract.git] / lib / SQL / Abstract / Tree.pm
CommitLineData
01dd4e4f 1package SQL::Abstract::Tree;
2
3use strict;
4use warnings;
5use Carp;
6
1536de15 7
1536de15 8use base 'Class::Accessor::Grouped';
9
10__PACKAGE__->mk_group_accessors( simple => $_ ) for qw(
11 newline indent_string indent_amount colormap indentmap
12);
13
01dd4e4f 14# Parser states for _recurse_parse()
15use constant PARSE_TOP_LEVEL => 0;
16use constant PARSE_IN_EXPR => 1;
17use constant PARSE_IN_PARENS => 2;
18use constant PARSE_RHS => 3;
19
20# These SQL keywords always signal end of the current expression (except inside
21# of a parenthesized subexpression).
22# Format: A list of strings that will be compiled to extended syntax (ie.
23# /.../x) regexes, without capturing parentheses. They will be automatically
24# anchored to word boundaries to match the whole token).
25my @expression_terminator_sql_keywords = (
26 'SELECT',
27 'FROM',
28 '(?:
29 (?:
30 (?: \b (?: LEFT | RIGHT | FULL ) \s+ )?
31 (?: \b (?: CROSS | INNER | OUTER ) \s+ )?
32 )?
33 JOIN
34 )',
35 'ON',
36 'WHERE',
37 'EXISTS',
38 'GROUP \s+ BY',
39 'HAVING',
40 'ORDER \s+ BY',
41 'LIMIT',
42 'OFFSET',
43 'FOR',
44 'UNION',
45 'INTERSECT',
46 'EXCEPT',
47 'RETURNING',
48);
49
50# These are binary operator keywords always a single LHS and RHS
51# * AND/OR are handled separately as they are N-ary
52# * so is NOT as being unary
53# * BETWEEN without paranthesis around the ANDed arguments (which
54# makes it a non-binary op) is detected and accomodated in
55# _recurse_parse()
56my $stuff_around_mathops = qr/[\w\s\`\'\"\)]/;
57my @binary_op_keywords = (
58 ( map
59 {
60 ' ^ ' . quotemeta ($_) . "(?= \$ | $stuff_around_mathops ) ",
61 " (?<= $stuff_around_mathops)" . quotemeta ($_) . "(?= \$ | $stuff_around_mathops ) ",
62 }
63 (qw/< > != <> = <= >=/)
64 ),
65 ( map
66 { '\b (?: NOT \s+)?' . $_ . '\b' }
67 (qw/IN BETWEEN LIKE/)
68 ),
69);
70
71my $tokenizer_re_str = join("\n\t|\n",
72 ( map { '\b' . $_ . '\b' } @expression_terminator_sql_keywords, 'AND', 'OR', 'NOT'),
73 @binary_op_keywords,
74);
75
76my $tokenizer_re = qr/ \s* ( $tokenizer_re_str | \( | \) | \? ) \s* /xi;
77
78sub _binary_op_keywords { @binary_op_keywords }
79
7e5600e9 80my %indents = (
81 select => 0,
82 where => 1,
83 from => 1,
84);
85
75c3a063 86my %profiles = (
87 console => {
1536de15 88 indent_string => ' ',
75c3a063 89 indent_amount => 2,
1536de15 90 newline => "\n",
3be357b0 91 colormap => {},
7e5600e9 92 indentmap => { %indents },
3be357b0 93 },
94 console_monochrome => {
95 indent_string => ' ',
96 indent_amount => 2,
97 newline => "\n",
98 colormap => {},
7e5600e9 99 indentmap => { %indents },
100 },
101 html => {
102 indent_string => '&nbsp;',
103 indent_amount => 2,
104 newline => "<br />\n",
105 colormap => {
106 select => ['<span class="select">', '</span>'],
107 where => ['<span class="where">', '</span>'],
108 from => ['<span class="from">', '</span>'],
1536de15 109 },
7e5600e9 110 indentmap => { %indents },
75c3a063 111 },
112 none => {
1536de15 113 colormap => {},
114 indentmap => {},
75c3a063 115 },
116);
117
3be357b0 118eval {
119 require Term::ANSIColor;
120 $profiles{console}->{colormap} = {
121 select => [Term::ANSIColor::color('red'), Term::ANSIColor::color('reset')],
122 where => [Term::ANSIColor::color('green'), Term::ANSIColor::color('reset')],
123 from => [Term::ANSIColor::color('cyan'), Term::ANSIColor::color('reset')],
124 };
125};
126
75c3a063 127sub new {
128 my ($class, $args) = @_;
129
130 my $profile = delete $args->{profile} || 'none';
131 my $data = {%{$profiles{$profile}}, %{$args||{}}};
132
133 bless $data, $class
134}
d695b0ad 135
01dd4e4f 136sub parse {
d695b0ad 137 my ($self, $s) = @_;
01dd4e4f 138
139 # tokenize string, and remove all optional whitespace
140 my $tokens = [];
141 foreach my $token (split $tokenizer_re, $s) {
142 push @$tokens, $token if (length $token) && ($token =~ /\S/);
143 }
144
d695b0ad 145 my $tree = $self->_recurse_parse($tokens, PARSE_TOP_LEVEL);
01dd4e4f 146 return $tree;
147}
148
149sub _recurse_parse {
d695b0ad 150 my ($self, $tokens, $state) = @_;
01dd4e4f 151
152 my $left;
153 while (1) { # left-associative parsing
154
155 my $lookahead = $tokens->[0];
156 if ( not defined($lookahead)
157 or
158 ($state == PARSE_IN_PARENS && $lookahead eq ')')
159 or
160 ($state == PARSE_IN_EXPR && grep { $lookahead =~ /^ $_ $/xi } ('\)', @expression_terminator_sql_keywords ) )
161 or
162 ($state == PARSE_RHS && grep { $lookahead =~ /^ $_ $/xi } ('\)', @expression_terminator_sql_keywords, @binary_op_keywords, 'AND', 'OR', 'NOT' ) )
163 ) {
164 return $left;
165 }
166
167 my $token = shift @$tokens;
168
169 # nested expression in ()
170 if ($token eq '(' ) {
d695b0ad 171 my $right = $self->_recurse_parse($tokens, PARSE_IN_PARENS);
172 $token = shift @$tokens or croak "missing closing ')' around block " . $self->unparse($right);
173 $token eq ')' or croak "unexpected token '$token' terminating block " . $self->unparse($right);
01dd4e4f 174
175 $left = $left ? [@$left, [PAREN => [$right] ]]
176 : [PAREN => [$right] ];
177 }
178 # AND/OR
179 elsif ($token =~ /^ (?: OR | AND ) $/xi ) {
180 my $op = uc $token;
d695b0ad 181 my $right = $self->_recurse_parse($tokens, PARSE_IN_EXPR);
01dd4e4f 182
183 # Merge chunks if logic matches
184 if (ref $right and $op eq $right->[0]) {
185 $left = [ (shift @$right ), [$left, map { @$_ } @$right] ];
186 }
187 else {
188 $left = [$op => [$left, $right]];
189 }
190 }
191 # binary operator keywords
192 elsif (grep { $token =~ /^ $_ $/xi } @binary_op_keywords ) {
193 my $op = uc $token;
d695b0ad 194 my $right = $self->_recurse_parse($tokens, PARSE_RHS);
01dd4e4f 195
196 # A between with a simple LITERAL for a 1st RHS argument needs a
197 # rerun of the search to (hopefully) find the proper AND construct
198 if ($op eq 'BETWEEN' and $right->[0] eq 'LITERAL') {
199 unshift @$tokens, $right->[1][0];
d695b0ad 200 $right = $self->_recurse_parse($tokens, PARSE_IN_EXPR);
01dd4e4f 201 }
202
203 $left = [$op => [$left, $right] ];
204 }
205 # expression terminator keywords (as they start a new expression)
206 elsif (grep { $token =~ /^ $_ $/xi } @expression_terminator_sql_keywords ) {
207 my $op = uc $token;
d695b0ad 208 my $right = $self->_recurse_parse($tokens, PARSE_IN_EXPR);
01dd4e4f 209 $left = $left ? [ $left, [$op => [$right] ]]
210 : [ $op => [$right] ];
211 }
212 # NOT (last as to allow all other NOT X pieces first)
213 elsif ( $token =~ /^ not $/ix ) {
214 my $op = uc $token;
d695b0ad 215 my $right = $self->_recurse_parse ($tokens, PARSE_RHS);
01dd4e4f 216 $left = $left ? [ @$left, [$op => [$right] ]]
217 : [ $op => [$right] ];
218
219 }
220 # literal (eat everything on the right until RHS termination)
221 else {
d695b0ad 222 my $right = $self->_recurse_parse ($tokens, PARSE_RHS);
223 $left = $left ? [ $left, [LITERAL => [join ' ', $token, $self->unparse($right)||()] ] ]
224 : [ LITERAL => [join ' ', $token, $self->unparse($right)||()] ];
01dd4e4f 225 }
226 }
227}
228
d695b0ad 229sub format_keyword {
230 my ($self, $keyword) = @_;
231
1536de15 232 if (my $around = $self->colormap->{lc $keyword}) {
d695b0ad 233 $keyword = "$around->[0]$keyword$around->[1]";
234 }
235
236 return $keyword
237}
238
a24cc3a0 239sub whitespace {
240 my ($self, $keyword, $depth) = @_;
e171c446 241
242 my $before = '';
1536de15 243 my $after = ' ';
244 if (defined $self->indentmap->{lc $keyword}) {
245 $before = $self->newline . $self->indent($depth + $self->indentmap->{lc $keyword});
a24cc3a0 246 }
1a67e3a0 247 $before = '' if $depth == 0 and lc $keyword eq 'select';
e171c446 248 return [$before, $after];
a24cc3a0 249}
250
1536de15 251sub indent { ($_[0]->indent_string||'') x ( ( $_[0]->indent_amount || 0 ) * $_[1] ) }
a24cc3a0 252
0569a14f 253sub _is_select {
254 my $tree = shift;
255 $tree = $tree->[0] while ref $tree;
256
ca8aec12 257 defined $tree && lc $tree eq 'select';
0569a14f 258}
259
01dd4e4f 260sub unparse {
a24cc3a0 261 my ($self, $tree, $depth) = @_;
262
e171c446 263 $depth ||= 0;
01dd4e4f 264
265 if (not $tree ) {
266 return '';
267 }
a24cc3a0 268
269 my $car = $tree->[0];
270 my $cdr = $tree->[1];
271
272 if (ref $car) {
e171c446 273 return join ('', map $self->unparse($_, $depth), @$tree);
01dd4e4f 274 }
a24cc3a0 275 elsif ($car eq 'LITERAL') {
276 return $cdr->[0];
01dd4e4f 277 }
a24cc3a0 278 elsif ($car eq 'PAREN') {
e171c446 279 return '(' .
a24cc3a0 280 join(' ',
0569a14f 281 map $self->unparse($_, $depth + 2), @{$cdr}) .
1536de15 282 (_is_select($cdr)?( $self->newline||'' ).$self->indent($depth + 1):'') . ')';
01dd4e4f 283 }
a24cc3a0 284 elsif ($car eq 'OR' or $car eq 'AND' or (grep { $car =~ /^ $_ $/xi } @binary_op_keywords ) ) {
e171c446 285 return join (" $car ", map $self->unparse($_, $depth), @{$cdr});
01dd4e4f 286 }
287 else {
a24cc3a0 288 my ($l, $r) = @{$self->whitespace($car, $depth)};
e171c446 289 return sprintf "$l%s %s$r", $self->format_keyword($car), $self->unparse($cdr, $depth);
01dd4e4f 290 }
291}
292
d695b0ad 293sub format { my $self = shift; $self->unparse($self->parse(@_)) }
01dd4e4f 294
2951;
296
3be357b0 297=pod
298
299=head1 SYNOPSIS
300
301 my $sqla_tree = SQL::Abstract::Tree->new({ profile => 'console' });
302
303 print $sqla_tree->format('SELECT * FROM foo WHERE foo.a > 2');
304
305 # SELECT *
306 # FROM foo
307 # WHERE foo.a > 2
308