Changeset 324


Ignore:
Timestamp:
08/10/09 23:03:51 (15 years ago)
Author:
nanardon
Message:
  • add an optimized version of search() in SQL base
File:
1 edited

Legend:

Unmodified
Added
Removed
  • LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/objects.pm

    r305 r324  
    364364} 
    365365 
     366 
     367sub search { 
     368    my ($class, $base, @filter) = @_; 
     369 
     370    if ($class->has_extended_attributes) { 
     371        my @sqlintersec; 
     372        my @bind; 
     373        while (my $item = shift(@filter)) { 
     374            # attr=foo => no extra white space ! 
     375            # \W is false, it is possible to have two char 
     376            my ($attr, $mode, $val) = $item =~ /^(\w+)(?:(\W)(.+))?$/ or next; 
     377            if (!$mode) { 
     378                $mode = '~'; 
     379                $val = shift(@filter); 
     380            } 
     381            push(@sqlintersec, 
     382                sprintf(q{select okey from %s where attr=? %s}, 
     383                    $base->db->quote_identifier($class->object_table . 
     384                        '_attributes'), 
     385                    sprintf(q{and lower(value) = ?}), 
     386                ) 
     387            ); 
     388            push(@bind, $base->get_field_name($class->type, $attr, 'r'), 
     389                lc($val));  
     390        } 
     391        my $sth = $base->db->prepare( 
     392            sprintf(q{ 
     393                select name from %s where ikey in ( 
     394                %s 
     395                ) 
     396                }, 
     397                $base->db->quote_identifier($class->object_table), 
     398                join("\n intersect\n", @sqlintersec), 
     399            ) 
     400        ); 
     401        $sth->execute(@bind); 
     402        my @results; 
     403        while (my $res = $sth->fetchrow_hashref) { 
     404            push(@results, $res->{name}); 
     405        } 
     406        return(@results); 
     407    } else { 
     408        my @bind; 
     409        my @where; 
     410        while (my $item = shift(@filter)) { 
     411            # attr=foo => no extra white space ! 
     412            # \W is false, it is possible to have two char 
     413            my ($attr, $mode, $val) = $item =~ /^(\w+)(?:(\W)(.+))?$/ or next; 
     414            if (!$mode) { 
     415                $mode = '~'; 
     416                $val = shift(@filter); 
     417            } 
     418            push(@where, sprintf("lower(%s) = ?", 
     419                   $base->db->quote_identifier($base->get_field_name($class->type, 
     420                           $attr, 'r')) 
     421               )); 
     422            push(@bind, lc($val));  
     423        } 
     424        my $sth = $base->db->prepare(sprintf(q{select name from %s where %s}, 
     425                $base->db->quote_identifier($class->object_table), 
     426                join(' and ', @where), 
     427            )); 
     428        $sth->execute(@bind); 
     429        my @results; 
     430        while (my $res = $sth->fetchrow_hashref) { 
     431            push(@results, $res->{name}); 
     432        } 
     433        return(@results); 
     434    } 
     435} 
     436 
    3664371; 
    367438 
Note: See TracChangeset for help on using the changeset viewer.