Changeset 71 in bookmarks for trunk/lib/Bookmarks.pm


Ignore:
Timestamp:
02/13/14 16:10:42 (10 years ago)
Author:
peter
Message:
  • added a Bookmarks::Search class that encapsulates the query, tags, limit, and offset values for a given search
  • Bookmarks::List now has a search attribute that takes a Bookmarks::Search object instead of the individual attributes from the search
  • Bookmarks::get_cotags() takes a Bookmarks::Search object as its search parameter instead of discrete tags and query parameters
  • changed the name of the Bookmarks::get_bookmarks() parameter from tag to tags to align with the rest of the backend APIs
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Bookmarks.pm

    r70 r71  
    22 
    33use Moose; 
     4 
    45use SQL::Interp qw{:all}; 
    56use URI; 
     7 
    68use Bookmark; 
    79use Bookmarks::List; 
     10use Bookmarks::Search; 
    811 
    912has dbh      => ( is => 'rw' ); 
     
    7376    my $self = shift; 
    7477    my $params = shift; 
    75     my $query = $params->{query}; 
    76     my $tags = $params->{tag} || []; 
    77     my $limit = $params->{limit}; 
    78     my $offset = $params->{offset}; 
     78    my $search = Bookmarks::Search->new($params); 
    7979 
    8080    # build the query 
    8181    my @sql; 
    8282 
    83     if (!ref $tags) { 
    84         $tags = [ $tags ]; 
    85     } 
    86     if (@$tags) { 
     83    if (@{ $search->tags }) { 
    8784        my $intersect = 0; 
    88         for my $tag (@{ $tags }) { 
     85        for my $tag (@{ $search->tags }) { 
    8986            push @sql, 'intersect' if $intersect; 
    9087            push @sql, 'select resources.*, bookmarks.* from resources join bookmarks on resources.uri = bookmarks.uri'; 
     
    9592        push @sql, 'select * from resources join bookmarks on resources.uri = bookmarks.uri'; 
    9693    } 
    97     if ($query) { 
    98         push @sql, (@$tags ? 'and' : 'where'), 'title like', \"%$query%"; 
     94    if ($search->query) { 
     95        my $fuzzy_match = '%' . $search->query . '%'; 
     96        push @sql, (@{ $search->tags } ? 'and' : 'where'), 'title like', \$fuzzy_match; 
    9997    } 
    10098    push @sql, 'order by ctime desc'; 
    101     push @sql, ('limit', \$limit) if $limit; 
     99    push @sql, ('limit', \$search->limit) if $search->limit; 
    102100    # an offset is only allowed if we have a limit clause 
    103     push @sql, ('offset', \$offset) if $limit && $offset; 
     101    push @sql, ('offset', \$search->offset) if $search->limit && $search->offset; 
    104102 
    105103    my ($sql, @bind) = sql_interp(@sql); 
    106     #die $sql; 
    107104 
    108105    my $sth_resource = $self->dbh->prepare($sql); 
     
    119116    return Bookmarks::List->new({ 
    120117        bookmarks => $self, 
    121         tags      => $tags, 
    122         query     => $query, 
    123         limit     => $limit, 
    124         offset    => $offset, 
     118        search    => $search, 
    125119        results   => \@resources, 
    126120    }); 
     
    147141    my $self = shift; 
    148142    my $params = shift; 
    149     my $query = $params->{query}; 
    150     my $tags = $params->{tag} || []; 
    151     if (!ref $tags) { 
    152         $tags = [ $tags ]; 
    153     } 
     143    my $search = $params->{search}; 
     144 
    154145    my @sql; 
    155146 
    156147    push @sql, 'select tag, count(tag) as count from tags'; 
    157     push @sql, 'join resources on tags.uri = resources.uri' if $query; 
     148    push @sql, 'join resources on tags.uri = resources.uri' if $search->query; 
    158149 
    159150    # build the where clause 
    160     if (@$tags) { 
     151    if (@{ $search->tags }) { 
    161152        push @sql, 'where tags.uri in ('; 
    162153        my $intersect = 0; 
    163         for my $tag (@{ $tags }) { 
     154        for my $tag (@{ $search->tags }) { 
    164155            push @sql, 'intersect' if $intersect; 
    165156            push @sql, 'select uri from tags where tag = ', \$tag; 
    166157            $intersect++; 
    167158        } 
    168         push @sql, ') and tag not in ', $tags, ''; 
    169     } 
    170     if ($query) { 
    171         push @sql, (@$tags ? 'and' : 'where'), 'title like', \"%$query%"; 
     159        push @sql, ') and tag not in ', $search->tags, ''; 
     160    } 
     161    if ($search->query) { 
     162        my $fuzzy_match = '%' . $search->query . '%'; 
     163        push @sql, (@{ $search->tags } ? 'and' : 'where'), 'title like', \$fuzzy_match; 
    172164    } 
    173165 
Note: See TracChangeset for help on using the changeset viewer.