-
- $want = CLOSE_PAREN | AGGREG;
- }
- elsif ( $current & AGGREG ) {
- $ea = $val;
- $want = KEYWORD | OPEN_PAREN;
- }
- elsif ( $current & KEYWORD ) {
- $key = $val;
- $want = OP;
- }
- elsif ( $current & OP ) {
- $op = $val;
- $want = VALUE;
- }
- elsif ( $current & VALUE ) {
- $value = $val;
-
- # Remove surrounding quotes from $key, $val
- # (in future, simplify as for($key,$val) { action on $_ })
- if ($key =~ /$re_delim/o) {
- substr($key,0,1) = "";
- substr($key,-1,1) = "";
- }
- if ($val =~ /$re_delim/o) {
- substr($val,0,1) = "";
- substr($val,-1,1) = "";
- }
- # Unescape escaped characters
- $key =~ s!\\(.)!$1!g;
- $val =~ s!\\(.)!$1!g;
- # print "$ea Key=[$key] op=[$op] val=[$val]\n";
-
-
- my $subkey = '';
- if ($key =~ /^(.+?)\.(.+)$/) {
- $key = $1;
- $subkey = $2;
- }
-
- my $class;
- if (exists $lcfields{lc $key}) {
- $key = $lcfields{lc $key};
- $class = $FIELD_METADATA{$key}->[0];
- }
- # no longer have a default, since CF's are now a real class, not fallthrough
- # fixme: "default class" is not Generic.
-
-
- die "Unknown field: $key" unless $class;
-
- $self->{_sql_localdepth} = 0;
- die "No such dispatch method: $class"
- unless exists $dispatch{$class};
- my $sub = $dispatch{$class} || die;;
- if ($can_bundle{$class} &&
- (!@bundle ||
- ($bundle[-1]->{dispatch} == $sub &&
- $bundle[-1]->{key} eq $key &&
- $bundle[-1]->{subkey} eq $subkey)))
- {
- push @bundle, {
- dispatch => $sub,
- key => $key,
- op => $op,
- val => $val,
- ea => $ea || "",
- subkey => $subkey,
- };
- } else {
- $self->_close_bundle(@bundle); @bundle = ();
- $sub->(
- $self,
- $key,
- $op,
- $val,
- SUBCLAUSE => "", # don't need anymore
- ENTRYAGGREGATOR => $ea || "",
- SUBKEY => $subkey,
- );
- }
-
- $self->{_sql_looking_at}{lc $key} = 1;
-
- ($ea,$key,$op,$value) = ("","","","");
-
- $want = CLOSE_PAREN | AGGREG;
- } else {
- die "I'm lost";
- }
-
- $last = $current;
- } # while
-
- $self->_close_bundle(@bundle); @bundle = ();
-
- die "Incomplete query"
- unless (($want | CLOSE_PAREN) || ($want | KEYWORD));
-
- die "Incomplete Query"
- unless ($last && ($last | CLOSE_PAREN) || ($last || VALUE));
-
- # This will never happen, because the parser will complain
- die "Mismatched parentheses"
- unless $depth == 0;
-
+ };
+ $callback{'EntryAggregator'} = sub { $ea = $_[0] || '' };
+ $callback{'Condition'} = sub {
+ my ($key, $op, $value) = @_;
+
+ # key has dot then it's compound variant and we have subkey
+ my $subkey = '';
+ ($key, $subkey) = ($1, $2) if $key =~ /^([^\.]+)\.(.+)$/;
+
+ # normalize key and get class (type)
+ my $class;
+ if (exists $lcfields{lc $key}) {
+ $key = $lcfields{lc $key};
+ $class = $FIELD_METADATA{$key}->[0];
+ }
+ die "Unknown field '$key' in '$string'" unless $class;
+
+ # replace __CurrentUser__ with id
+ $value = $self->CurrentUser->id if $value eq '__CurrentUser__';
+
+
+ unless( $dispatch{ $class } ) {
+ die "No dispatch method for class '$class'"
+ }
+ my $sub = $dispatch{ $class };
+
+ if ( $can_bundle{ $class }
+ && ( !@bundle
+ || ( $bundle[-1]->{dispatch} == $sub
+ && $bundle[-1]->{key} eq $key
+ && $bundle[-1]->{subkey} eq $subkey
+ )
+ )
+ )
+ {
+ push @bundle, {
+ dispatch => $sub,
+ key => $key,
+ op => $op,
+ val => $value,
+ ea => $ea,
+ subkey => $subkey,
+ };
+ }
+ else {
+ $self->_close_bundle(@bundle); @bundle = ();
+ $sub->( $self, $key, $op, $value,
+ SUBCLAUSE => '', # don't need anymore
+ ENTRYAGGREGATOR => $ea,
+ SUBKEY => $subkey,
+ );
+ }
+ $self->{_sql_looking_at}{lc $key} = 1;
+ $ea = '';
+ };
+ RT::SQL::Parse($string, \%callback);
+ $self->_close_bundle(@bundle); @bundle = ();