Removed an unnecessary 'use'.
[dbsrgits/SQL-Abstract.git] / lib / SQL / Abstract / Test.pm
CommitLineData
fffe6900 1package SQL::Abstract::Test; # see doc at end of file
2
3use strict;
4use warnings;
5aad8cf3 5use base qw/Test::Builder::Module Exporter/;
fffe6900 6use Data::Dumper;
7use Carp;
4abea32b 8use Test::Builder;
9use Test::Deep qw(eq_deeply);
fffe6900 10
11our @EXPORT_OK = qw/&is_same_sql_bind &eq_sql &eq_bind
12 $case_sensitive $sql_differ/;
13
14our $case_sensitive = 0;
15our $sql_differ; # keeps track of differing portion between SQLs
5aad8cf3 16our $tb = __PACKAGE__->builder;
fffe6900 17
25823711 18# Parser states for _recurse_parse()
19use constant {
20 PARSE_TOP_LEVEL => 0,
21 PARSE_IN_EXPR => 1,
22 PARSE_IN_PARENS => 2,
23};
24
25# These SQL keywords always signal end of the current expression (except inside
26# of a parenthesized subexpression).
27# Format: A list of strings that will be compiled to extended syntax (ie.
28# /.../x) regexes, without capturing parentheses. They will be automatically
29# anchored to word boundaries to match the whole token).
30my @expression_terminator_sql_keywords = (
31 'FROM',
32 '(?:
33 (?:
34 (?: \b (?: LEFT | RIGHT | FULL ) \s+ )?
35 (?: \b (?: CROSS | INNER | OUTER ) \s+ )?
36 )?
37 JOIN
38 )',
39 'ON',
40 'WHERE',
41 'GROUP \s+ BY',
42 'HAVING',
43 'ORDER \s+ BY',
44 'LIMIT',
45 'OFFSET',
46 'FOR',
47 'UNION',
48 'INTERSECT',
49 'EXCEPT',
50);
51
52my $tokenizer_re_str = join('|',
53 map { '\b' . $_ . '\b' }
54 @expression_terminator_sql_keywords, 'AND', 'OR'
55);
56
57my $tokenizer_re = qr/
58 \s*
59 (
60 \(
61 |
62 \)
63 |
64 $tokenizer_re_str
65 )
66 \s*
67/xi;
68
69
fffe6900 70sub is_same_sql_bind {
71 my ($sql1, $bind_ref1, $sql2, $bind_ref2, $msg) = @_;
72
73 # compare
25823711 74 my $same_sql = eq_sql($sql1, $sql2);
fffe6900 75 my $same_bind = eq_bind($bind_ref1, $bind_ref2);
76
a6daa642 77 # call Test::Builder::ok
5aad8cf3 78 $tb->ok($same_sql && $same_bind, $msg);
fffe6900 79
80 # add debugging info
81 if (!$same_sql) {
5aad8cf3 82 $tb->diag("SQL expressions differ\n"
fffe6900 83 ." got: $sql1\n"
84 ."expected: $sql2\n"
5aad8cf3 85 ."differing in :\n$sql_differ\n"
86 );
fffe6900 87 }
88 if (!$same_bind) {
5aad8cf3 89 $tb->diag("BIND values differ\n"
fffe6900 90 ." got: " . Dumper($bind_ref1)
91 ."expected: " . Dumper($bind_ref2)
5aad8cf3 92 );
fffe6900 93 }
94}
95
fffe6900 96sub eq_bind {
97 my ($bind_ref1, $bind_ref2) = @_;
fffe6900 98
4abea32b 99 return eq_deeply($bind_ref1, $bind_ref2);
fffe6900 100}
101
102sub eq_sql {
25823711 103 my ($sql1, $sql2) = @_;
104
105 # parse
106 my $tree1 = parse($sql1);
107 my $tree2 = parse($sql2);
108
109 return _eq_sql($tree1, $tree2);
110}
111
112sub _eq_sql {
fffe6900 113 my ($left, $right) = @_;
114
115 # ignore top-level parentheses
116 while ($left->[0] eq 'PAREN') {$left = $left->[1] }
117 while ($right->[0] eq 'PAREN') {$right = $right->[1]}
118
119 # if operators are different
120 if ($left->[0] ne $right->[0]) {
121 $sql_differ = sprintf "OP [$left->[0]] != [$right->[0]] in\nleft: %s\nright: %s\n",
122 unparse($left),
123 unparse($right);
124 return 0;
125 }
126 # elsif operators are identical, compare operands
127 else {
128 if ($left->[0] eq 'EXPR' ) { # unary operator
129 (my $l = " $left->[1] " ) =~ s/\s+/ /g;
130 (my $r = " $right->[1] ") =~ s/\s+/ /g;
131 my $eq = $case_sensitive ? $l eq $r : uc($l) eq uc($r);
132 $sql_differ = "[$left->[1]] != [$right->[1]]\n" if not $eq;
133 return $eq;
134 }
135 else { # binary operator
25823711 136 return _eq_sql($left->[1][0], $right->[1][0]) # left operand
137 && _eq_sql($left->[1][1], $right->[1][1]); # right operand
fffe6900 138 }
139 }
140}
141
142
143sub parse {
144 my $s = shift;
145
25823711 146 # tokenize string, and remove all optional whitespace
147 my $tokens = [];
148 foreach my $token (split $tokenizer_re, $s) {
149 $token =~ s/\s+/ /g;
150 $token =~ s/\s+([^\w\s])/$1/g;
151 $token =~ s/([^\w\s])\s+/$1/g;
152 push @$tokens, $token if $token !~ /^$/;
153 }
fffe6900 154
25823711 155 my $tree = _recurse_parse($tokens, PARSE_TOP_LEVEL);
fffe6900 156 return $tree;
157}
158
159sub _recurse_parse {
25823711 160 my ($tokens, $state) = @_;
fffe6900 161
162 my $left;
163 while (1) { # left-associative parsing
164
165 my $lookahead = $tokens->[0];
25823711 166 return $left if !defined($lookahead)
167 || ($state == PARSE_IN_PARENS && $lookahead eq ')')
168 || ($state == PARSE_IN_EXPR && grep { $lookahead =~ /^$_$/xi }
169 '\)', @expression_terminator_sql_keywords
170 );
fffe6900 171
172 my $token = shift @$tokens;
173
174 # nested expression in ()
175 if ($token eq '(') {
25823711 176 my $right = _recurse_parse($tokens, PARSE_IN_PARENS);
fffe6900 177 $token = shift @$tokens or croak "missing ')'";
178 $token eq ')' or croak "unexpected token : $token";
179 $left = $left ? [CONCAT => [$left, [PAREN => $right]]]
180 : [PAREN => $right];
181 }
182 # AND/OR
183 elsif ($token eq 'AND' || $token eq 'OR') {
25823711 184 my $right = _recurse_parse($tokens, PARSE_IN_EXPR);
fffe6900 185 $left = [$token => [$left, $right]];
186 }
25823711 187 # expression terminator keywords (as they start a new expression)
188 elsif (grep { $token =~ /^$_$/xi } @expression_terminator_sql_keywords) {
189 my $right = _recurse_parse($tokens, PARSE_IN_EXPR);
190 $left = $left ? [CONCAT => [$left, [CONCAT => [[EXPR => $token], [PAREN => $right]]]]]
191 : [CONCAT => [[EXPR => $token], [PAREN => $right]]];
192 }
fffe6900 193 # leaf expression
194 else {
195 $left = $left ? [CONCAT => [$left, [EXPR => $token]]]
196 : [EXPR => $token];
197 }
198 }
199}
200
201
202
203sub unparse {
204 my $tree = shift;
205 my $dispatch = {
206 EXPR => sub {$tree->[1] },
207 PAREN => sub {"(" . unparse($tree->[1]) . ")" },
208 CONCAT => sub {join " ", map {unparse($_)} @{$tree->[1]}},
209 AND => sub {join " AND ", map {unparse($_)} @{$tree->[1]}},
210 OR => sub {join " OR ", map {unparse($_)} @{$tree->[1]}},
211 };
212 $dispatch->{$tree->[0]}->();
213}
214
215
2161;
217
218
219__END__
220
221=head1 NAME
222
223SQL::Abstract::Test - Helper function for testing SQL::Abstract
224
225=head1 SYNOPSIS
226
227 use SQL::Abstract;
228 use Test::More;
5aad8cf3 229 use SQL::Abstract::Test import => ['is_same_sql_bind'];
fffe6900 230
231 my ($sql, @bind) = SQL::Abstract->new->select(%args);
232 is_same_sql_bind($given_sql, \@given_bind,
233 $expected_sql, \@expected_bind, $test_msg);
234
235=head1 DESCRIPTION
236
237This module is only intended for authors of tests on
238L<SQL::Abstract|SQL::Abstract> and related modules;
239it exports functions for comparing two SQL statements
240and their bound values.
241
242The SQL comparison is performed on I<abstract syntax>,
243ignoring differences in spaces or in levels of parentheses.
244Therefore the tests will pass as long as the semantics
245is preserved, even if the surface syntax has changed.
246
247B<Disclaimer> : this is only a half-cooked semantic equivalence;
248parsing is simple-minded, and comparison of SQL abstract syntax trees
249ignores commutativity or associativity of AND/OR operators, Morgan
250laws, etc.
251
252=head1 FUNCTIONS
253
254=head2 is_same_sql_bind
255
256 is_same_sql_bind($given_sql, \@given_bind,
257 $expected_sql, \@expected_bind, $test_msg);
258
259Compares given and expected pairs of C<($sql, \@bind)>, and calls
a6daa642 260L<Test::Builder/ok> on the result, with C<$test_msg> as message. If the
fffe6900 261test fails, a detailed diagnostic is printed. For clients which use
a6daa642 262L<Test::Build>, this is the only function that needs to be
fffe6900 263imported.
264
265=head2 eq_sql
266
267 my $is_same = eq_sql($given_sql, $expected_sql);
268
269Compares the abstract syntax of two SQL statements. If the result is
270false, global variable L</sql_differ> will contain the SQL portion
271where a difference was encountered; this is useful for printing diagnostics.
272
273=head2 eq_bind
274
275 my $is_same = eq_sql(\@given_bind, \@expected_bind);
276
277Compares two lists of bind values, taking into account
278the fact that some of the values may be
279arrayrefs (see L<SQL::Abstract/bindtype>).
280
281=head1 GLOBAL VARIABLES
282
283=head2 case_sensitive
284
285If true, SQL comparisons will be case-sensitive. Default is false;
286
287=head2 sql_differ
288
289When L</eq_sql> returns false, the global variable
290C<$sql_differ> contains the SQL portion
291where a difference was encountered.
292
293
294=head1 SEE ALSO
295
a6daa642 296L<SQL::Abstract>, L<Test::More>, L<Test::Builder>.
fffe6900 297
25823711 298=head1 AUTHORS
fffe6900 299
300Laurent Dami, E<lt>laurent.dami AT etat geneve chE<gt>
301
25823711 302Norbert Buchmuller <norbi@nix.hu>
303
fffe6900 304=head1 COPYRIGHT AND LICENSE
305
306Copyright 2008 by Laurent Dami.
307
308This library is free software; you can redistribute it and/or modify
309it under the same terms as Perl itself.