From: Peter Rabbitson Date: Sun, 2 Jun 2013 13:38:35 +0000 (+0200) Subject: Stop differentiating between ORDER BY foo and ORDER BY foo ASC by default X-Git-Tag: v1.74~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FSQL-Abstract.git;a=commitdiff_plain;h=0c2de280869928d9ff1ee95f36a9a45318766990 Stop differentiating between ORDER BY foo and ORDER BY foo ASC by default --- diff --git a/Changes b/Changes index 40e994f..ca58b59 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,8 @@ Revision history for SQL::Abstract - Fix insufficient parenthesis unroll during operator comparison + - 'ORDER BY foo' and 'ORDER BY foo ASC' are now considered equal + by default (with a switch to reenable old behavior when necessary) revision 1.73 2012-07-10 ---------------------------- diff --git a/lib/SQL/Abstract/Test.pm b/lib/SQL/Abstract/Test.pm index 16631fe..202d130 100644 --- a/lib/SQL/Abstract/Test.pm +++ b/lib/SQL/Abstract/Test.pm @@ -16,6 +16,8 @@ my $sqlat = SQL::Abstract::Tree->new; our $case_sensitive = 0; our $parenthesis_significant = 0; +our $order_by_asc_significant = 0; + our $sql_differ; # keeps track of differing portion between SQLs our $tb = __PACKAGE__->builder; @@ -173,6 +175,11 @@ sub _eq_sql { $sqlat->_parenthesis_unroll($_) for $left, $right; } + # unroll ASC order by's + unless ($order_by_asc_significant) { + $sqlat->_strip_asc_from_order_by($_) for $left, $right; + } + if ( $left->[0] ne $right->[0] ) { $sql_differ = sprintf "OP [$left->[0]] != [$right->[0]] in\nleft: %s\nright: %s\n", $sqlat->unparse($left), @@ -326,6 +333,11 @@ If true, SQL comparison will preserve and report difference in nested parenthesis. Useful while testing C vs C. Defaults to false; +=head2 $order_by_asc_significant + +If true SQL comparison will consider C and +C to be different. Default is false; + =head2 $sql_differ When L returns false, the global variable diff --git a/lib/SQL/Abstract/Tree.pm b/lib/SQL/Abstract/Tree.pm index 54dfd62..81a360d 100644 --- a/lib/SQL/Abstract/Tree.pm +++ b/lib/SQL/Abstract/Tree.pm @@ -760,6 +760,30 @@ sub _parenthesis_unroll { } while ($changes); } +sub _strip_asc_from_order_by { + my ($self, $ast) = @_; + + return $ast if ( + ref $ast ne 'ARRAY' + or + $ast->[0] ne 'ORDER BY' + ); + + + my $to_replace; + + if (@{$ast->[1]} == 1 and $ast->[1][0][0] eq '-ASC') { + $to_replace = [ $ast->[1][0] ]; + } + elsif (@{$ast->[1]} == 1 and $ast->[1][0][0] eq '-LIST') { + $to_replace = [ grep { $_->[0] eq '-ASC' } @{$ast->[1][0][1]} ]; + } + + @$_ = @{$_->[1][0]} for @$to_replace; + + $ast; +} + sub format { my $self = shift; $self->unparse($self->parse($_[0]), $_[1]) } 1; diff --git a/t/10test.t b/t/10test.t index 19afd8f..f3835a1 100644 --- a/t/10test.t +++ b/t/10test.t @@ -592,6 +592,34 @@ my @sql_tests = ( ] }, + # order by + { + equal => 1, + statements => [ + q/SELECT * FROM foo ORDER BY bar/, + q/SELECT * FROM foo ORDER BY bar ASC/, + q/SELECT * FROM foo ORDER BY bar asc/, + ], + }, + { + equal => 1, + statements => [ + q/SELECT * FROM foo ORDER BY bar, baz ASC/, + q/SELECT * FROM foo ORDER BY bar ASC, baz/, + q/SELECT * FROM foo ORDER BY bar asc, baz ASC/, + q/SELECT * FROM foo ORDER BY bar, baz/, + ], + }, + { + equal => 0, + opts => { order_by_asc_significant => 1 }, + statements => [ + q/SELECT * FROM foo ORDER BY bar/, + q/SELECT * FROM foo ORDER BY bar ASC/, + q/SELECT * FROM foo ORDER BY bar desc/, + ], + }, + # list permutations { equal => 0,