From: Lebbeous Fogle-Weekley Date: Thu, 29 Dec 2011 16:29:15 +0000 (-0500) Subject: Two changes: X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=6587a4baccb03f5450c9ad11c3b7161e1b5bff40;p=working%2FEvergreen.git Two changes: 1) move canonicalization stuff into O/A/Storage/QueryParser 2) no longer require config (filters, operators, etc) to be passed in with query to canonicalize Signed-off-by: Lebbeous Fogle-Weekley --- diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/metabib.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/metabib.pm index 1467c43db4..35816beb9a 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/metabib.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/metabib.pm @@ -10,7 +10,7 @@ use OpenSRF::Utils::JSON; use Data::Dumper; use Digest::MD5 qw/md5_hex/; -#use OpenILS::Application::Storage::Driver::Pg::QueryParser; +use OpenILS::Application::Storage::QueryParser; my $log = 'OpenSRF::Utils::Logger'; @@ -2776,98 +2776,12 @@ __PACKAGE__->register_method( api_level => 1, ); - -sub _abstract_query2str_filter { - my $f = shift; - - return sprintf( - "%s%s(%s)", - $f->{negate} ? $qpconfig->{operators}{disallowed} : "", - $f->{name}, - join(",", @{$f->{args}}) - ); -} - -sub _abstract_query2str_modifier { - my $f = shift; - - return $qpconfig->{operators}{modifier} . $f; -} - -# This should produce an equivalent query to the original, given an -# abstract_query with a qp config. -sub abstract_query2str_impl { - my ($abstract_query, $depth, $qpconfig) = @_; - - my $gs = $qpconfig->{operators}{group_start}; - my $ge = $qpconfig->{operators}{group_end}; - my $and = $qpconfig->{operators}{and}; - my $or = $qpconfig->{operators}{or}; - - my $q = ""; - $q .= $gs if $abstract_query->{type} and $abstract_query->{type} eq "query_plan" and $depth; - - if (exists $abstract_query->{type}) { - if ($abstract_query->{type} eq 'query_plan') { - $q .= join(" ", map { _abstract_query2str_filter($_) } @{$abstract_query->{filters}}) if - exists $abstract_query->{filters}; - $q .= " "; - - $q .= join(" ", map { _abstract_query2str_modifier($_) } @{$abstract_query->{modifiers}}) if - exists $abstract_query->{modifiers}; - } elsif ($abstract_query->{type} eq 'node') { - if ($abstract_query->{alias}) { - $q .= " " . $abstract_query->{alias}; - $q .= "|$_" foreach @{$abstract_query->{alias_fields}}; - } else { - $q .= " " . $abstract_query->{class}; - $q .= "|$_" foreach @{$abstract_query->{fields}}; - } - $q .= ":"; - } elsif ($abstract_query->{type} eq 'atom') { - my $prefix = $abstract_query->{prefix} || ''; - $prefix = $qpconfig->{operators}{disallowed} if $prefix eq '!'; - $q .= $prefix . - ($abstract_query->{content} || '') . - ($abstract_query->{suffix} || ''); - } elsif ($abstract_query->{type} eq 'facet') { - # facet syntax [ # ] is hardcoded I guess? - my $prefix = $abstract_query->{negate} ? $qpconfig->{operators}{disallowed} : ''; - $q .= $prefix . $abstract_query->{name} . "[" . - join(" # ", @{$abstract_query->{values}}) . "]"; - } - } - - if (exists $abstract_query->{children}) { - my $op = (keys(%{$abstract_query->{children}}))[0]; - $q .= join( - " " . ($op eq '&' ? $and : $or) . " ", - map { - abstract_query2str_impl($_, $depth + 1, $qpconfig) - } @{$abstract_query->{children}{$op}} - ); - } elsif ($abstract_query->{'&'} or $abstract_query->{'|'}) { - my $op = (keys(%{$abstract_query}))[0]; - $q .= join( - " " . ($op eq '&' ? $and : $or) . " ", - map { - abstract_query2str_impl($_, $depth + 1, $qpconfig) - } @{$abstract_query->{$op}} - ); - } - $q .= " "; - - $q .= $ge if $abstract_query->{type} and $abstract_query->{type} eq "query_plan" and $depth; - - return $q; -} - # Takes an abstract query object and recursively turns it back into a string # for QueryParser. sub abstract_query2str { my ($self, $conn, $query) = @_; - return abstract_query2str_impl($query, 0, $query->{config}); + return QueryParser::Canonicalize::abstract_query2str_impl($query, 0); } __PACKAGE__->register_method( diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/QueryParser.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/QueryParser.pm index 25b1fe86ce..07d19c07e5 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/QueryParser.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/QueryParser.pm @@ -826,6 +826,98 @@ sub find_arrays_in_abstract { } #------------------------------- +package QueryParser::Canonicalize; # not OO + +sub _abstract_query2str_filter { + my $f = shift; + my $qpconfig = $parser_config{QueryParser}; + + return sprintf( + "%s%s(%s)", + $f->{negate} ? $qpconfig->{operators}{disallowed} : "", + $f->{name}, + join(",", @{$f->{args}}) + ); +} + +sub _abstract_query2str_modifier { + my $f = shift; + my $qpconfig = $parser_config{QueryParser}; + + return $qpconfig->{operators}{modifier} . $f; +} + +# This should produce an equivalent query to the original, given an +# abstract_query. +sub abstract_query2str_impl { + my ($abstract_query, $depth) = @_; + + my $qpconfig = $parser_config{QueryParser}; + + my $gs = $qpconfig->{operators}{group_start}; + my $ge = $qpconfig->{operators}{group_end}; + my $and = $qpconfig->{operators}{and}; + my $or = $qpconfig->{operators}{or}; + + my $q = ""; + $q .= $gs if $abstract_query->{type} and $abstract_query->{type} eq "query_plan" and $depth; + + if (exists $abstract_query->{type}) { + if ($abstract_query->{type} eq 'query_plan') { + $q .= join(" ", map { _abstract_query2str_filter($_) } @{$abstract_query->{filters}}) if + exists $abstract_query->{filters}; + $q .= " "; + + $q .= join(" ", map { _abstract_query2str_modifier($_) } @{$abstract_query->{modifiers}}) if + exists $abstract_query->{modifiers}; + } elsif ($abstract_query->{type} eq 'node') { + if ($abstract_query->{alias}) { + $q .= " " . $abstract_query->{alias}; + $q .= "|$_" foreach @{$abstract_query->{alias_fields}}; + } else { + $q .= " " . $abstract_query->{class}; + $q .= "|$_" foreach @{$abstract_query->{fields}}; + } + $q .= ":"; + } elsif ($abstract_query->{type} eq 'atom') { + my $prefix = $abstract_query->{prefix} || ''; + $prefix = $qpconfig->{operators}{disallowed} if $prefix eq '!'; + $q .= $prefix . + ($abstract_query->{content} || '') . + ($abstract_query->{suffix} || ''); + } elsif ($abstract_query->{type} eq 'facet') { + # facet syntax [ # ] is hardcoded I guess? + my $prefix = $abstract_query->{negate} ? $qpconfig->{operators}{disallowed} : ''; + $q .= $prefix . $abstract_query->{name} . "[" . + join(" # ", @{$abstract_query->{values}}) . "]"; + } + } + + if (exists $abstract_query->{children}) { + my $op = (keys(%{$abstract_query->{children}}))[0]; + $q .= join( + " " . ($op eq '&' ? $and : $or) . " ", + map { + abstract_query2str_impl($_, $depth + 1) + } @{$abstract_query->{children}{$op}} + ); + } elsif ($abstract_query->{'&'} or $abstract_query->{'|'}) { + my $op = (keys(%{$abstract_query}))[0]; + $q .= join( + " " . ($op eq '&' ? $and : $or) . " ", + map { + abstract_query2str_impl($_, $depth + 1) + } @{$abstract_query->{$op}} + ); + } + $q .= " "; + + $q .= $ge if $abstract_query->{type} and $abstract_query->{type} eq "query_plan" and $depth; + + return $q; +} + +#------------------------------- package QueryParser::query_plan; sub QueryParser {