5 use List::Util qw(sum);
11 # WHERE condition - equal
15 q/SELECT foo FROM bar WHERE a = 1/,
16 q/SELECT foo FROM bar WHERE a=1/,
17 q/SELECT foo FROM bar WHERE (a = 1)/,
18 q/SELECT foo FROM bar WHERE (a=1)/,
19 q/SELECT foo FROM bar WHERE ( a = 1 )/,
44 q/SELECT foo FROM bar WHERE ((a = 1))/,
45 q/SELECT foo FROM bar WHERE ( (a = 1) )/,
46 q/SELECT foo FROM bar WHERE ( ( a = 1 ) )/,
52 q/SELECT foo FROM bar WHERE a = 1 AND b = 1/,
53 q/SELECT foo FROM bar WHERE (a = 1) AND (b = 1)/,
54 q/SELECT foo FROM bar WHERE ((a = 1) AND (b = 1))/,
55 q/SELECT foo FROM bar WHERE (a = 1 AND b = 1)/,
56 q/SELECT foo FROM bar WHERE ((a = 1 AND b = 1))/,
57 q/SELECT foo FROM bar WHERE (((a = 1) AND (b = 1)))/,
103 q/SELECT foo FROM bar WHERE a = 1 AND b = 1 AND c = 1/,
104 q/SELECT foo FROM bar WHERE (a = 1 AND b = 1) AND c = 1/,
105 q/SELECT foo FROM bar WHERE a = 1 AND (b = 1 AND c = 1)/,
106 q/SELECT foo FROM bar WHERE ((((a = 1))) AND (b = 1 AND c = 1))/,
112 q/SELECT foo FROM bar WHERE a = 1 OR b = 1 OR c = 1/,
113 q/SELECT foo FROM bar WHERE (a = 1 OR b = 1) OR c = 1/,
114 q/SELECT foo FROM bar WHERE a = 1 OR (b = 1 OR c = 1)/,
115 q/SELECT foo FROM bar WHERE a = 1 OR ((b = 1 OR (c = 1)))/,
121 q/SELECT foo FROM bar WHERE (a = 1) AND (b = 1 OR c = 1 OR d = 1) AND (e = 1 AND f = 1)/,
122 q/SELECT foo FROM bar WHERE a = 1 AND (b = 1 OR c = 1 OR d = 1) AND e = 1 AND (f = 1)/,
123 q/SELECT foo FROM bar WHERE ( ((a = 1) AND ( b = 1 OR (c = 1 OR d = 1) )) AND ((e = 1)) AND f = 1) /,
127 # WHERE condition - different
131 q/SELECT foo FROM bar WHERE a = 1/,
132 q/SELECT quux FROM bar WHERE a = 1/,
133 q/SELECT foo FROM quux WHERE a = 1/,
134 q/FOOBAR foo FROM bar WHERE a = 1/,
136 q/SELECT foo FROM bar WHERE a = 2/,
137 q/SELECT foo FROM bar WHERE a < 1/,
138 q/SELECT foo FROM bar WHERE b = 1/,
139 q/SELECT foo FROM bar WHERE (c = 1)/,
140 q/SELECT foo FROM bar WHERE (d = 1)/,
142 q/SELECT foo FROM bar WHERE a = 1 AND quux/,
143 q/SELECT foo FROM bar WHERE a = 1 GROUP BY foo/,
144 q/SELECT foo FROM bar WHERE a = 1 ORDER BY foo/,
145 q/SELECT foo FROM bar WHERE a = 1 LIMIT 1/,
146 q/SELECT foo FROM bar WHERE a = 1 OFFSET 1/,
147 q/SELECT foo FROM bar JOIN quux WHERE a = 1/,
148 q/SELECT foo FROM bar JOIN quux ON a = 1 WHERE a = 1/,
154 q/SELECT foo FROM bar WHERE a = 1 AND b = 1/,
155 q/SELECT quux FROM bar WHERE a = 1 AND b = 1/,
156 q/SELECT foo FROM quux WHERE a = 1 AND b = 1/,
157 q/FOOBAR foo FROM bar WHERE a = 1 AND b = 1/,
159 q/SELECT foo FROM bar WHERE a = 2 AND b = 1/,
160 q/SELECT foo FROM bar WHERE a = 3 AND (b = 1)/,
161 q/SELECT foo FROM bar WHERE (a = 4) AND b = 1/,
162 q/SELECT foo FROM bar WHERE (a = 5) AND (b = 1)/,
163 q/SELECT foo FROM bar WHERE ((a = 6) AND (b = 1))/,
164 q/SELECT foo FROM bar WHERE ((a = 7) AND (b = 1))/,
166 q/SELECT foo FROM bar WHERE a = 1 AND b = 2/,
167 q/SELECT foo FROM bar WHERE a = 1 AND (b = 3)/,
168 q/SELECT foo FROM bar WHERE (a = 1) AND b = 4/,
169 q/SELECT foo FROM bar WHERE (a = 1) AND (b = 5)/,
170 q/SELECT foo FROM bar WHERE ((a = 1) AND (b = 6))/,
171 q/SELECT foo FROM bar WHERE ((a = 1) AND (b = 7))/,
173 q/SELECT foo FROM bar WHERE a < 1 AND b = 1/,
174 q/SELECT foo FROM bar WHERE b = 1 AND b = 1/,
175 q/SELECT foo FROM bar WHERE (c = 1) AND b = 1/,
176 q/SELECT foo FROM bar WHERE (d = 1) AND b = 1/,
178 q/SELECT foo FROM bar WHERE a = 1 AND b = 1 AND quux/,
179 q/SELECT foo FROM bar WHERE a = 1 AND b = 1 GROUP BY foo/,
180 q/SELECT foo FROM bar WHERE a = 1 AND b = 1 ORDER BY foo/,
181 q/SELECT foo FROM bar WHERE a = 1 AND b = 1 LIMIT 1/,
182 q/SELECT foo FROM bar WHERE a = 1 AND b = 1 OFFSET 1/,
183 q/SELECT foo FROM bar JOIN quux WHERE a = 1 AND b = 1/,
184 q/SELECT foo FROM bar JOIN quux ON a = 1 WHERE a = 1 AND b = 1/,
190 q/SELECT foo FROM bar WHERE a = 1 AND b = 1 OR c = 1/,
191 q/SELECT foo FROM bar WHERE (a = 1 AND b = 1) OR c = 1/,
192 q/SELECT foo FROM bar WHERE a = 1 AND (b = 1 OR c = 1)/,
198 q/SELECT foo FROM bar WHERE a = 1 OR b = 1 AND c = 1/,
199 q/SELECT foo FROM bar WHERE (a = 1 OR b = 1) AND c = 1/,
200 q/SELECT foo FROM bar WHERE a = 1 OR (b = 1 AND c = 1)/,
206 # BETWEEN with/without parenthesis around itself/RHS is a sticky business
207 # if I made a mistake here, simply rewrite the special BETWEEN handling in
211 q/SELECT foo FROM bar WHERE ( completion_date BETWEEN ? AND ? AND status = ? )/,
212 q/SELECT foo FROM bar WHERE completion_date BETWEEN (? AND ?) AND status = ?/,
213 q/SELECT foo FROM bar WHERE ( (completion_date BETWEEN (? AND ?) ) AND status = ? )/,
214 q/SELECT foo FROM bar WHERE ( (completion_date BETWEEN (? AND ? AND status = ?) ) )/,
218 # JOIN condition - equal
222 q/SELECT foo FROM bar JOIN baz ON a = 1 WHERE x = 1/,
223 q/SELECT foo FROM bar JOIN baz ON a=1 WHERE x = 1/,
224 q/SELECT foo FROM bar JOIN baz ON (a = 1) WHERE x = 1/,
225 q/SELECT foo FROM bar JOIN baz ON (a=1) WHERE x = 1/,
226 q/SELECT foo FROM bar JOIN baz ON ( a = 1 ) WHERE x = 1/,
263 q/SELECT foo FROM bar JOIN baz ON ((a = 1)) WHERE x = 1/,
264 q/SELECT foo FROM bar JOIN baz ON ( (a = 1) ) WHERE x = 1/,
265 q/SELECT foo FROM bar JOIN baz ON ( ( a = 1 ) ) WHERE x = 1/,
271 q/SELECT foo FROM bar JOIN baz ON a = 1 AND b = 1 WHERE x = 1/,
272 q/SELECT foo FROM bar JOIN baz ON (a = 1) AND (b = 1) WHERE x = 1/,
273 q/SELECT foo FROM bar JOIN baz ON ((a = 1) AND (b = 1)) WHERE x = 1/,
274 q/SELECT foo FROM bar JOIN baz ON (a = 1 AND b = 1) WHERE x = 1/,
275 q/SELECT foo FROM bar JOIN baz ON ((a = 1 AND b = 1)) WHERE x = 1/,
276 q/SELECT foo FROM bar JOIN baz ON (((a = 1) AND (b = 1))) WHERE x = 1/,
336 # JOIN condition - different
340 q/SELECT foo FROM bar JOIN quux ON a = 1 WHERE quuux/,
341 q/SELECT quux FROM bar JOIN quux ON a = 1 WHERE quuux/,
342 q/SELECT foo FROM quux JOIN quux ON a = 1 WHERE quuux/,
343 q/FOOBAR foo FROM bar JOIN quux ON a = 1 WHERE quuux/,
345 q/SELECT foo FROM bar JOIN quux ON a = 2 WHERE quuux/,
346 q/SELECT foo FROM bar JOIN quux ON a < 1 WHERE quuux/,
347 q/SELECT foo FROM bar JOIN quux ON b = 1 WHERE quuux/,
348 q/SELECT foo FROM bar JOIN quux ON (c = 1) WHERE quuux/,
349 q/SELECT foo FROM bar JOIN quux ON (d = 1) WHERE quuux/,
351 q/SELECT foo FROM bar JOIN quux ON a = 1 AND quuux/,
352 q/SELECT foo FROM bar JOIN quux ON a = 1 GROUP BY foo/,
353 q/SELECT foo FROM bar JOIN quux ON a = 1 ORDER BY foo/,
354 q/SELECT foo FROM bar JOIN quux ON a = 1 LIMIT 1/,
355 q/SELECT foo FROM bar JOIN quux ON a = 1 OFFSET 1/,
356 q/SELECT foo FROM bar JOIN quux ON a = 1 JOIN quuux/,
357 q/SELECT foo FROM bar JOIN quux ON a = 1 JOIN quuux ON a = 1/,
363 q/SELECT foo FROM bar JOIN quux ON a = 1 AND b = 1 WHERE quuux/,
364 q/SELECT quux FROM bar JOIN quux ON a = 1 AND b = 1 WHERE quuux/,
365 q/SELECT foo FROM quux JOIN quux ON a = 1 AND b = 1 WHERE quuux/,
366 q/FOOBAR foo FROM bar JOIN quux ON a = 1 AND b = 1 WHERE quuux/,
368 q/SELECT foo FROM bar JOIN quux ON a = 2 AND b = 1 WHERE quuux/,
369 q/SELECT foo FROM bar JOIN quux ON a = 3 AND (b = 1) WHERE quuux/,
370 q/SELECT foo FROM bar JOIN quux ON (a = 4) AND b = 1 WHERE quuux/,
371 q/SELECT foo FROM bar JOIN quux ON (a = 5) AND (b = 1) WHERE quuux/,
372 q/SELECT foo FROM bar JOIN quux ON ((a = 6) AND (b = 1)) WHERE quuux/,
373 q/SELECT foo FROM bar JOIN quux ON ((a = 7) AND (b = 1)) WHERE quuux/,
375 q/SELECT foo FROM bar JOIN quux ON a = 1 AND b = 2 WHERE quuux/,
376 q/SELECT foo FROM bar JOIN quux ON a = 1 AND (b = 3) WHERE quuux/,
377 q/SELECT foo FROM bar JOIN quux ON (a = 1) AND b = 4 WHERE quuux/,
378 q/SELECT foo FROM bar JOIN quux ON (a = 1) AND (b = 5) WHERE quuux/,
379 q/SELECT foo FROM bar JOIN quux ON ((a = 1) AND (b = 6)) WHERE quuux/,
380 q/SELECT foo FROM bar JOIN quux ON ((a = 1) AND (b = 7)) WHERE quuux/,
382 q/SELECT foo FROM bar JOIN quux ON a < 1 AND b = 1 WHERE quuux/,
383 q/SELECT foo FROM bar JOIN quux ON b = 1 AND b = 1 WHERE quuux/,
384 q/SELECT foo FROM bar JOIN quux ON (c = 1) AND b = 1 WHERE quuux/,
385 q/SELECT foo FROM bar JOIN quux ON (d = 1) AND b = 1 WHERE quuux/,
387 q/SELECT foo FROM bar JOIN quux ON a = 1 AND b = 1 AND quuux/,
388 q/SELECT foo FROM bar JOIN quux ON a = 1 AND b = 1 GROUP BY foo/,
389 q/SELECT foo FROM bar JOIN quux ON a = 1 AND b = 1 ORDER BY foo/,
390 q/SELECT foo FROM bar JOIN quux ON a = 1 AND b = 1 LIMIT 1/,
391 q/SELECT foo FROM bar JOIN quux ON a = 1 AND b = 1 OFFSET 1/,
392 q/SELECT foo FROM bar JOIN quux JOIN quuux ON a = 1 AND b = 1/,
393 q/SELECT foo FROM bar JOIN quux ON a = 1 JOIN quuux ON a = 1 AND b = 1/,
397 # DISTINCT ON (...) not confused with JOIN ON (...)
401 q/SELECT DISTINCT ON (foo, quux) foo, quux FROM bar WHERE a = 1/,
402 q/SELECT DISTINCT ON (foo, quux) foo, quux FROM bar WHERE a=1/,
403 q/SELECT DISTINCT ON (foo, quux) foo, quux FROM bar WHERE (a = 1)/,
404 q/SELECT DISTINCT ON (foo, quux) foo, quux FROM bar WHERE (a=1)/,
405 q/SELECT DISTINCT ON (foo, quux) foo, quux FROM bar WHERE ( a = 1 )/,
407 SELECT DISTINCT ON (foo, quux)
416 SELECT DISTINCT ON (foo, quux)
425 SELECT DISTINCT ON (foo, quux)
433 q/SELECT DISTINCT ON (foo, quux) foo, quux FROM bar WHERE ((a = 1))/,
434 q/SELECT DISTINCT ON (foo, quux) foo, quux FROM bar WHERE ( (a = 1) )/,
435 q/SELECT DISTINCT ON (foo, quux) foo, quux FROM bar WHERE ( ( a = 1 ) )/,
443 q/SELECT * FROM (SELECT * FROM bar WHERE b = 1) AS foo WHERE a = 1/,
444 q/SELECT * FROM (SELECT * FROM bar WHERE b = 1) AS foo WHERE (a = 1)/,
445 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 1)) AS foo WHERE a = 1/,
446 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 1)) AS foo WHERE (a = 1)/,
452 q/SELECT * FROM (SELECT * FROM bar WHERE b = 1 AND c = 1) AS foo WHERE a = 1/,
453 q/SELECT * FROM (SELECT * FROM bar WHERE b = 1 AND (c = 1)) AS foo WHERE a = 1/,
454 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 1) AND c = 1) AS foo WHERE a = 1/,
455 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 1) AND (c = 1)) AS foo WHERE a = 1/,
456 q/SELECT * FROM (SELECT * FROM bar WHERE ((b = 1) AND (c = 1))) AS foo WHERE a = 1/,
458 q/SELECT * FROM (SELECT * FROM bar WHERE b = 1 AND c = 1) AS foo WHERE (a = 1)/,
459 q/SELECT * FROM (SELECT * FROM bar WHERE b = 1 AND (c = 1)) AS foo WHERE (a = 1)/,
460 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 1) AND c = 1) AS foo WHERE (a = 1)/,
461 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 1) AND (c = 1)) AS foo WHERE (a = 1)/,
462 q/SELECT * FROM (SELECT * FROM bar WHERE ((b = 1) AND (c = 1))) AS foo WHERE (a = 1)/,
466 # subselects - different
470 q/SELECT * FROM (SELECT * FROM bar WHERE b = 1) AS foo WHERE a = 1/,
471 q/SELECT * FROM (SELECT * FROM bar WHERE b = 1) AS foo WHERE a = 2/,
472 q/SELECT * FROM (SELECT * FROM bar WHERE b = 1) AS foo WHERE (a = 3)/,
473 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 1)) AS foo WHERE a = 4/,
474 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 1)) AS foo WHERE (a = 5)/,
475 q/SELECT * FROM (SELECT * FROM bar WHERE b = 2) AS foo WHERE a = 1/,
476 q/SELECT * FROM (SELECT * FROM bar WHERE b = 3) AS foo WHERE (a = 1)/,
477 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 4)) AS foo WHERE a = 1/,
478 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 5)) AS foo WHERE (a = 1)/,
484 q/SELECT * FROM (SELECT * FROM bar WHERE b = 1 AND c = 1) AS foo WHERE a = 1/,
485 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 1) AND c = 2) AS foo WHERE a = 1/,
486 q/SELECT * FROM (SELECT * FROM bar WHERE b = 1 AND (c = 3)) AS foo WHERE a = 1/,
487 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 1) AND (c = 4)) AS foo WHERE a = 1/,
488 q/SELECT * FROM (SELECT * FROM bar WHERE ((b = 1) AND (c = 5))) AS foo WHERE a = 1/,
490 q/SELECT * FROM (SELECT * FROM bar WHERE b = 1 AND c = 6) AS foo WHERE (a = 1)/,
491 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 1) AND c = 7) AS foo WHERE (a = 1)/,
492 q/SELECT * FROM (SELECT * FROM bar WHERE b = 1 AND (c = 8)) AS foo WHERE (a = 1)/,
493 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 1) AND (c = 9)) AS foo WHERE (a = 1)/,
494 q/SELECT * FROM (SELECT * FROM bar WHERE ((b = 1) AND (c = 10))) AS foo WHERE (a = 1)/,
496 q/SELECT * FROM (SELECT * FROM bar WHERE b = 1 AND c = 1) AS foo WHERE a = 2/,
497 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 1) AND c = 2) AS foo WHERE a = 2/,
498 q/SELECT * FROM (SELECT * FROM bar WHERE b = 1 AND (c = 3)) AS foo WHERE a = 2/,
499 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 1) AND (c = 4)) AS foo WHERE a = 2/,
500 q/SELECT * FROM (SELECT * FROM bar WHERE ((b = 1) AND (c = 5))) AS foo WHERE a = 2/,
502 q/SELECT * FROM (SELECT * FROM bar WHERE b = 1 AND c = 6) AS foo WHERE (a = 2)/,
503 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 1) AND c = 7) AS foo WHERE (a = 2)/,
504 q/SELECT * FROM (SELECT * FROM bar WHERE b = 1 AND (c = 8)) AS foo WHERE (a = 2)/,
505 q/SELECT * FROM (SELECT * FROM bar WHERE (b = 1) AND (c = 9)) AS foo WHERE (a = 2)/,
506 q/SELECT * FROM (SELECT * FROM bar WHERE ((b = 1) AND (c = 10))) AS foo WHERE (a = 2)/,
592 { foo => 42, bar => 1 },
593 { foo => 42, bar => 1 },
594 { foo => '42', bar => 1 },
598 # blessed object - equal
602 bless(\(local $_ = 42), 'Life::Universe::Everything'),
603 bless(\(local $_ = 42), 'Life::Universe::Everything'),
609 bless([42], 'Life::Universe::Everything'),
610 bless([42], 'Life::Universe::Everything'),
616 bless({ answer => 42 }, 'Life::Universe::Everything'),
617 bless({ answer => 42 }, 'Life::Universe::Everything'),
621 # complex data structure - equal
625 [42, { foo => 'bar', quux => [1, 2, \3, { quux => [4, 5] } ] }, 8 ],
626 [42, { foo => 'bar', quux => [1, 2, \3, { quux => [4, 5] } ] }, 8 ],
641 # scalarref - different
651 # arrayref - different
661 # hashref - different
682 # complex data structure - different
686 [42, { foo => 'bar', quux => [1, 2, \3, { quux => [4, 5] } ] }, 8 ],
687 [43, { foo => 'bar', quux => [1, 2, \3, { quux => [4, 5] } ] }, 8 ],
688 [42, { foo => 'baz', quux => [1, 2, \3, { quux => [4, 5] } ] }, 8 ],
689 [42, { bar => 'bar', quux => [1, 2, \3, { quux => [4, 5] } ] }, 8 ],
690 [42, { foo => 'bar', quuux => [1, 2, \3, { quux => [4, 5] } ] }, 8 ],
691 [42, { foo => 'bar', quux => [0, 1, 2, \3, { quux => [4, 5] } ] }, 8 ],
692 [42, { foo => 'bar', quux => [1, 2, 3, { quux => [4, 5] } ] }, 8 ],
693 [42, { foo => 'bar', quux => [1, 2, \4, { quux => [4, 5] } ] }, 8 ],
694 [42, { foo => 'bar', quux => [1, 2, \3, { quuux => [4, 5] } ] }, 8 ],
695 [42, { foo => 'bar', quux => [1, 2, \3, { quux => [4, 5, 6] } ] }, 8 ],
696 [42, { foo => 'bar', quux => [1, 2, \3, { quux => 4 } ] }, 8 ],
697 [42, { foo => 'bar', quux => [1, 2, \3, { quux => [4, 5], quuux => 1 } ] }, 8 ],
698 [42, { foo => 'bar', quux => [1, 2, \3, { quux => [4, 5] } ] }, 8, 9 ],
705 map { $_ * ($_ - 1) / 2 }
706 map { scalar @{$_->{statements}} }
710 map { $_ * ($_ - 1) / 2 }
711 map { scalar @{$_->{bindvals}} }
716 use_ok('SQL::Abstract::Test', import => [qw(
717 eq_sql_bind eq_sql eq_bind is_same_sql_bind
720 for my $test (@sql_tests) {
721 my $statements = $test->{statements};
722 while (@$statements) {
723 my $sql1 = shift @$statements;
724 foreach my $sql2 (@$statements) {
725 my $equal = eq_sql($sql1, $sql2);
727 local $TODO = $test->{todo} if $test->{todo};
729 if ($test->{equal}) {
730 ok($equal, "equal SQL expressions should have been considered equal");
732 ok(!$equal, "different SQL expressions should have been considered not equal");
735 if ($equal ^ $test->{equal}) {
744 for my $test (@bind_tests) {
745 my $bindvals = $test->{bindvals};
747 my $bind1 = shift @$bindvals;
748 foreach my $bind2 (@$bindvals) {
749 my $equal = eq_bind($bind1, $bind2);
750 if ($test->{equal}) {
751 ok($equal, "equal bind values considered equal");
753 ok(!$equal, "different bind values considered not equal");
756 if ($equal ^ $test->{equal}) {
757 diag("bind1: " . Dumper($bind1));
758 diag("bind2: " . Dumper($bind2));
765 "SELECT * FROM foo WHERE id = ?", [42],
766 "SELECT * FROM foo WHERE (id = ?)", [42],
768 "eq_sql_bind considers equal SQL expressions and bind values equal"
773 "SELECT * FROM foo WHERE id = ?", [42],
774 "SELECT * FROM foo WHERE (id = ?)", [0],
776 "eq_sql_bind considers equal SQL expressions and different bind values different"
780 "SELECT * FROM foo WHERE id = ?", [42],
781 "SELECT * FROM bar WHERE (id = ?)", [42],
783 "eq_sql_bind considers different SQL expressions and equal bind values different"