cvsuser     02/03/12 09:02:11

  Modified:    P5EEx/Blue/P5EEx/Blue/Repository DBI.pm
  Log:
  add support for the 'matches' operator
  
  Revision  Changes    Path
  1.8       +37 -15    p5ee/P5EEx/Blue/P5EEx/Blue/Repository/DBI.pm
  
  Index: DBI.pm
  ===================================================================
  RCS file: /cvs/public/p5ee/P5EEx/Blue/P5EEx/Blue/Repository/DBI.pm,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -w -r1.7 -r1.8
  --- DBI.pm    2 Mar 2002 03:07:46 -0000       1.7
  +++ DBI.pm    12 Mar 2002 17:02:11 -0000      1.8
  @@ -1,13 +1,13 @@
   
   ######################################################################
  -## File: $Id: DBI.pm,v 1.7 2002/03/02 03:07:46 spadkins Exp $
  +## File: $Id: DBI.pm,v 1.8 2002/03/12 17:02:11 spadkins Exp $
   ######################################################################
   
   use P5EEx::Blue::P5EE;
   use P5EEx::Blue::Repository;
   
   package P5EEx::Blue::Repository::DBI;
  -$VERSION = do { my @r=(q$Revision: 1.7 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r};
  +$VERSION = do { my @r=(q$Revision: 1.8 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r};
   
   @ISA = ( "P5EEx::Blue::Repository" );
   
  @@ -345,10 +345,11 @@
   
   sub mk_where_clause {
       my ($self, $table, $params, $paramvalues) = @_;
  -    my ($where, $column, $colstr, $value, $colnum, $sqlop, $column_def, $quoted);
  +    my ($where, $column, $colstr, $value, $colnum, $repop, $sqlop, $column_def, 
$quoted);
       my $tabcols = $self->{table}{$table}{column};
  -    my %op = (
  +    my %sqlop = (
           'contains' => 'like',
  +        'matches'  => 'like',
           'eq'       => '=',
           'ne'       => '!=',
           'le'       => '<=',
  @@ -367,29 +368,42 @@
               $colstr = $params->[$colnum];
               $column = $colstr;
               $sqlop = "=";
  +            $repop = "";
               # check if $column contains an embedded operation, i.e. "name.eq", 
"name.contains"
               if ($colstr =~ /^(.*)\.([^.]+)$/) {
  -                if ($op{$2}) {
  +                $repop = $2;
  +                if ($sqlop{$repop}) {
                       $column = $1;
  -                    $sqlop = $op{$2};
  +                    $sqlop = $sqlop{$repop};
                   }
               }
               $column_def = $tabcols->{$column};
               next if (!defined $column_def);  # skip if the column is unknown
               if (! defined $paramvalues->{$colstr}) {
  -                $value = "?";   # TODO: make this work with the "contains" operator
  +                $value = "?";   # TODO: make this work with the "contains/matches" 
operators
               }
               else {
                   $value = $paramvalues->{$colstr};
  +
                   if ($value =~ s/^!expr!//) {
                       $quoted = 0;
                   }
                   else {
                       $quoted = (defined $column_def->{quoted}) ? 
($column_def->{quoted}) : ($value !~ /^-?[0-9.]+$/);
                   }
  -                if ($quoted) {
  +
  +                if ($repop eq "contains") {
  +                    $value =~ s/'/\\'/g;
  +                    $value = "'%$value%'";
  +                }
  +                elsif ($repop eq "matches") {
  +                    $value =~ s/\*/%/g;
                       $value =~ s/'/\\'/g;
  -                    $value = ($sqlop eq "like") ? "'%$value%'" : "'$value'";
  +                    $value = "'$value'";
  +                }
  +                elsif ($quoted) {
  +                    $value =~ s/'/\\'/g;
  +                    $value = "'$value'";
                   }
               }
               $where .= ($colnum == 0) ? "where $column $sqlop $value\n" : "  and 
$column $sqlop $value\n";
  @@ -657,8 +671,9 @@
   
       #print $self->{context}->dump(), "\n";
   
  -    my %op = (
  +    my %sqlop = (
           'contains' => 'like',
  +        'matches'  => 'like',
           'eq'       => '=',
           'ne'       => '!=',
           'le'       => '<=',
  @@ -668,7 +683,7 @@
           'in'       => 'in',
       );
   
  -    my ($where_condition, @join_conditions, @criteria_conditions, $param, $sqlop, 
$paramvalue);
  +    my ($where_condition, @join_conditions, @criteria_conditions, $param, $repop, 
$sqlop, $paramvalue);
       if (!defined $params && ref($paramvalues) eq "HASH") {
           $params = [ keys %$paramvalues ];
       }
  @@ -696,11 +711,13 @@
           }
   
           $sqlop = "=";
  +        $repop = "";
           # check if $param contains an embedded operation, i.e. "name.eq", 
"name.contains"
           if ($param =~ /^(.*)\.([^.]+)$/) {
  -            if ($op{$2}) {
  +            $repop = $2;
  +            if ($sqlop{$repop}) {
                   $column = $1;
  -                $sqlop = $op{$2};
  +                $sqlop = $sqlop{$repop};
               }
           }
   
  @@ -721,7 +738,7 @@
           next if (!defined $column_def);  # skip if the column is unknown
   
           if (! defined $paramvalues->{$param}) {
  -            $paramvalue = "?";   # TODO: make this work with the "contains" operator
  +            $paramvalue = "?";   # TODO: make this work with the "contains/matches" 
operators
           }
           else {
               $paramvalue = $paramvalues->{$param};
  @@ -736,9 +753,14 @@
                   $quoted = (defined $column_def->{quoted}) ? ($column_def->{quoted}) 
: ($paramvalue !~ /^-?[0-9.]+$/);
               }
   
  -            if ($sqlop eq "like") {
  +            if ($repop eq "contains") {
                   $paramvalue =~ s/'/\\'/g;
                   $paramvalue = "'%$paramvalue%'";
  +            }
  +            elsif ($repop eq "matches") {
  +                $paramvalue =~ s/\*/%/g;
  +                $paramvalue =~ s/'/\\'/g;
  +                $paramvalue = "'$paramvalue'";
               }
               elsif ($sqlop eq "in" || $sqlop eq "=") {
                   if ($quoted) {
  
  
  


Reply via email to