From: Peter Rabbitson Date: Tue, 5 May 2009 11:49:26 +0000 (+0000) Subject: Deprecate -nest in favor of -paren X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d92d804b3d06c8110b12512b62333c6aec506993;p=scpubgit%2FQ-Branch.git Deprecate -nest in favor of -paren --- diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index 127e6ad..a569708 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -443,49 +443,54 @@ sub _where_HASHREF { sub _where_op_in_hash { my ($self, $op_str, $v) = @_; - $op_str =~ /^ (AND|OR|NEST) ( \_? \d* ) $/xi + $op_str =~ /^ (AND|OR|PAREN|NEST) ( \_? \d* ) $/xi or puke "unknown operator: -$op_str"; my $op = uc($1); # uppercase, remove trailing digits if ($2) { - belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. ' + belch 'Use of op_N modifiers is deprecated and will be removed in SQLA v2.0. ' . "You probably wanted ...-and => [ $op_str => COND1, $op_str => COND2 ... ]"; } + if ($op eq 'NEST') { + belch 'The -nest modifier is deprecated in favor of -paren and will be removed in SQLA v2.0'; + $op = 'PAREN'; + } + $self->_debug("OP(-$op) within hashref, recursing..."); $self->_SWITCH_refkind($v, { ARRAYREF => sub { - return $self->_where_ARRAYREF($v, $op eq 'NEST' ? '' : $op); + return $self->_where_ARRAYREF($v, $op eq 'PAREN' ? '' : $op); }, HASHREF => sub { if ($op eq 'OR') { return $self->_where_ARRAYREF([ map { $_ => $v->{$_} } (sort keys %$v) ], 'OR'); } - else { # NEST | AND + else { # PAREN | AND return $self->_where_HASHREF($v); } }, SCALARREF => sub { # literal SQL - $op eq 'NEST' - or puke "-$op => \\\$scalar not supported, use -nest => ..."; + $op eq 'PAREN' + or puke "-$op => \\\$scalar not supported, use -paren => ..."; return ($$v); }, ARRAYREFREF => sub { # literal SQL - $op eq 'NEST' - or puke "-$op => \\[..] not supported, use -nest => ..."; + $op eq 'PAREN' + or puke "-$op => \\[..] not supported, use -paren => ..."; return @{${$v}}; }, SCALAR => sub { # permissively interpreted as SQL - $op eq 'NEST' - or puke "-$op => 'scalar' not supported, use -nest => \\'scalar'"; - belch "literal SQL should be -nest => \\'scalar' " - . "instead of -nest => 'scalar' "; + $op eq 'PAREN' + or puke "-$op => 'scalar' not supported, use -paren => \\'scalar'"; + belch "literal SQL should be -paren => \\'scalar' " + . "instead of -paren => 'scalar' "; return ($v); }, @@ -1851,7 +1856,7 @@ This data structure would create the following: @bind = ('nwiger', 'pending', 'dispatched', 'robot', 'unassigned'); -There is also a special C<-nest> +There is also a special C<-paren> operator which adds an additional set of parens, to create a subquery. For example, to get something like this: @@ -1862,7 +1867,7 @@ You would do: my %where = ( user => 'nwiger', - -nest => [ workhrs => {'>', 20}, geo => 'ASIA' ], + -paren => [ workhrs => {'>', 20}, geo => 'ASIA' ], ); @@ -1873,7 +1878,7 @@ inside : my @where = ( -and => [ user => 'nwiger', - -nest => [ + -paren => [ -and => [workhrs => {'>', 20}, geo => 'ASIA' ], -and => [workhrs => {'<', 50}, geo => 'EURO' ] ], @@ -2026,7 +2031,7 @@ hash, like an EXISTS subquery : = $sql->select("t1", "*", {c1 => 1, c2 => \"> t0.c0"}); my %where = ( foo => 1234, - -nest => \["EXISTS ($sub_stmt)" => @sub_bind], + -paren => \["EXISTS ($sub_stmt)" => @sub_bind], ); which yields @@ -2049,7 +2054,7 @@ like for example fulltext expressions, geospatial expressions, NATIVE clauses, etc. Here is an example of a fulltext query in MySQL : my %where = ( - -nest => \["MATCH (col1, col2) AGAINST (?)" => qw/apples/] + -paren => \["MATCH (col1, col2) AGAINST (?)" => qw/apples/] ); Finally, here is an example where a subquery is used @@ -2060,7 +2065,7 @@ for expressing unary negation: $sub_stmt =~ s/^ where //i; # don't want "WHERE" in the subclause my %where = ( lname => {like => '%son%'}, - -nest => \["NOT ($sub_stmt)" => @sub_bind], + -paren => \["NOT ($sub_stmt)" => @sub_bind], ); This yields @@ -2318,7 +2323,7 @@ so I have no idea who they are! But the people I do know are: Mike Fragassi (enhancements to "BETWEEN" and "LIKE") Dan Kubb (support for "quote_char" and "name_sep") Guillermo Roditi (patch to cleanup "IN" and "BETWEEN", fix and tests for _order_by) - Laurent Dami (internal refactoring, multiple -nest, extensible list of special operators, literal SQL) + Laurent Dami (internal refactoring, multiple -paren, extensible list of special operators, literal SQL) Norbert Buchmuller (support for literal SQL in hashpair, misc. fixes & tests) Peter Rabbitson (rewrite of SQLA::Test, misc. fixes & tests) diff --git a/t/04modifiers.t b/t/04modifiers.t index dfc0f78..5815af3 100644 --- a/t/04modifiers.t +++ b/t/04modifiers.t @@ -10,7 +10,7 @@ use Data::Dumper; use SQL::Abstract; =begin -Test -and -or and -nest modifiers, assuming the following: +Test -and -or modifiers, assuming the following: * Modifiers are respected in both hashrefs and arrayrefs (with the obvious limitation of one modifier type per hahsref) @@ -299,29 +299,30 @@ my @and_or_tests = ( ); # modN and mod_N were a bad design decision - they go away in SQLA2, warn now -my @numbered_mods = ( +# -nest is renamed to -paren +my @backcompat_mods = ( { backcompat => { - -and => [a => 10, b => 11], - -and2 => [ c => 20, d => 21 ], + -and5 => [a => 10, b => 11], + -and_2 => [ c => 20, d => 21 ], -nest => [ x => 1 ], -nest2 => [ y => 2 ], - -or => { m => 7, n => 8 }, - -or2 => { m => 17, n => 18 }, + -or7 => { m => 7, n => 8 }, + -or_2 => { m => 17, n => 18 }, }, correct => { -and => [ -and => [a => 10, b => 11], -and => [ c => 20, d => 21 ], - -nest => [ x => 1 ], - -nest => [ y => 2 ], + -paren => [ x => 1 ], + -paren => [ y => 2 ], -or => { m => 7, n => 8 }, -or => { m => 17, n => 18 }, ] }, }, { backcompat => { - -and2 => [a => 10, b => 11], - -and_3 => [ c => 20, d => 21 ], + -and => [a => 10, b => 11], + -and2 => [ c => 20, d => 21 ], -nest2 => [ x => 1 ], -nest_3 => [ y => 2 ], -or2 => { m => 7, n => 8 }, @@ -330,48 +331,48 @@ my @numbered_mods = ( correct => [ -and => [ -and => [a => 10, b => 11], -and => [ c => 20, d => 21 ], - -nest => [ x => 1 ], - -nest => [ y => 2 ], + -paren => [ x => 1 ], + -paren => [ y => 2 ], -or => { m => 7, n => 8 }, -or => { m => 17, n => 18 }, ] ], }, ); -my @nest_tests = ( +my @paren_tests = ( { - where => {a => 1, -nest => [b => 2, c => 3]}, + where => {a => 1, -paren => [b => 2, c => 3]}, stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )', bind => [qw/2 3 1/], }, { - where => {a => 1, -nest => {b => 2, c => 3}}, + where => {a => 1, -paren => {b => 2, c => 3}}, stmt => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )', bind => [qw/2 3 1/], }, { - where => {a => 1, -or => {-nest => {b => 2, c => 3}}}, + where => {a => 1, -or => {-paren => {b => 2, c => 3}}}, stmt => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )', bind => [qw/2 3 1/], }, { - where => {a => 1, -or => {-nest => [b => 2, c => 3]}}, + where => {a => 1, -or => {-paren => [b => 2, c => 3]}}, stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )', bind => [qw/2 3 1/], }, { - where => {a => 1, -nest => {-or => {b => 2, c => 3}}}, + where => {a => 1, -paren => {-or => {b => 2, c => 3}}}, stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )', bind => [qw/2 3 1/], }, { - where => [a => 1, -nest => {b => 2, c => 3}, -nest => [d => 4, e => 5]], + where => [a => 1, -paren => {b => 2, c => 3}, -paren => [d => 4, e => 5]], stmt => 'WHERE ( ( a = ? OR ( b = ? AND c = ? ) OR ( d = ? OR e = ? ) ) )', bind => [qw/1 2 3 4 5/], }, ); -plan tests => @and_or_tests*3 + @numbered_mods*4 + @nest_tests*2; +plan tests => @and_or_tests*3 + @backcompat_mods*4 + @paren_tests*2; for my $case (@and_or_tests) { TODO: { @@ -397,7 +398,7 @@ for my $case (@and_or_tests) { } } -for my $case (@nest_tests) { +for my $case (@paren_tests) { TODO: { local $TODO = $case->{todo} if $case->{todo}; @@ -418,17 +419,26 @@ for my $case (@nest_tests) { } } - - -my $w_str = "\QUse of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0\E"; -for my $case (@numbered_mods) { +my $numw_str = "\QUse of op_N modifiers is deprecated and will be removed in SQLA v2.0\E"; +my $nestw_str = "\QThe -nest modifier is deprecated in favor of -paren and will be removed in SQLA v2.0\E"; +for my $case (@backcompat_mods) { TODO: { local $TODO = $case->{todo} if $case->{todo}; local $Data::Dumper::Terse = 1; - my @w; - local $SIG{__WARN__} = sub { push @w, @_ }; + my $w; + local $SIG{__WARN__} = sub { + if ($_[0] =~ /$numw_str/) { + push @{$w->{num}}, $_[0]; + } + elsif ($_[0] =~ /$nestw_str/) { + push @{$w->{nest}}, $_[0]; + } + else { + warn $_[0]; + } + }; my $sql = SQL::Abstract->new ($case->{args} || {}); lives_ok (sub { my ($old_s, @old_b) = $sql->where($case->{backcompat}); @@ -443,15 +453,8 @@ for my $case (@numbered_mods) { }; }); - ok (@w, 'Warnings were emitted about a mod_N construct'); - - my @non_match; - for (@w) { - push @non_match, $_ if ($_ !~ /$w_str/); - } - - is (@non_match, 0, 'All warnings match the deprecation message') - || diag join "\n", 'Rogue warnings:', @non_match; + is (@{$w->{num} || []}, 5, 'Correct number of warnings were emitted about a mod_N operator'); + is (@{$w->{nest} || []}, 2, 'Correct number of warnings were emitted about a -nest operator'); } }