Subject: | issue with parsing in SQL::Statement |
Date: | Wed, 16 Sep 2015 19:54:58 +0000 |
To: | "bug-SQL-Statement [...] rt.cpan.org" <bug-SQL-Statement [...] rt.cpan.org> |
From: | "Robertson, Jason V" <jason.v.robertson [...] intel.com> |
Hi,
When I parse this SQL:
my $sql = "SELECT * FROM sometable WHERE pkey='a' AND pk2='b' AND pk3='c' AND (step LIKE '%hello%' OR step LIKE '%xyz%') OR (step = 'asd' OR step2 = 'dasd')";
I get:
SQL = SELECT * FROM registry_processcorner WHERE pkey='a' AND pk2='b' AND pk3='c' AND (step LIKE '%hello%' OR step LIKE '%xyz%') OR (step = 'asd' OR step2 = 'dasd')
Use of uninitialized value in substitution iterator at /p/coeenv/proj_tools/hdkperlmod/jvrobert_sbox/lib/lib64/site_perl/SQL/Parser.pm line 1691.
Oddly, it works fine if I change my SQL to use lower-case 'and' and 'or'.
Looking at the code a bit it looks like it's using " AND " as some sort of marker. I can sort of fix the problem by changing the group_ands function to use " AND " four spaces in front, not one) to mark completion, but this still seems a bit fragile.
sub group_ands
{
my $str = shift;
my $and_preds = shift || [];
return ( $str, $and_preds )
unless $str =~ / AND / and $str =~ / OR /;
return $str, $and_preds
unless ( $str =~ /^(.*?) AND (.*)$/i );
my ( $front, $back ) = ( $1, $2 );
my $index = scalar @$and_preds;
$front = $1
if ( $front =~ /^.* OR (.*)$/i );
$back = $1
if ( $back =~ /^(.*?) (OR|AND) .*$/i );
my $newpred = "$front AND $back";
push @$and_preds, $newpred;
$str =~ s/\Q$newpred/~$index~/i;
return group_ands( $str, $and_preds );
}