Changeset 20 in bookmarks for trunk/Bookmarks.pm


Ignore:
Timestamp:
05/21/13 17:55:34 (11 years ago)
Author:
peter
Message:
  • Bookmarks::get_resources() and Bookmarks::get_cotags() both accept arrayrefs as their tag parameters
  • can now use multiple ?tag query parameters in the request and it will search for the intersection of those tags
  • bkmk list takes 0 or more --tag command line options
  • display a checkbox-based form listing the search tags and available cotags at the below the list of bookmarks
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Bookmarks.pm

    r15 r20  
    5353    my $self = shift; 
    5454    my $params = shift; 
    55     my $tag = $params->{tag}; 
     55    my $tags = $params->{tag} || []; 
    5656    my $limit = $params->{limit}; 
    5757    my $offset = $params->{offset}; 
    5858 
    59     my ($sql, @bind) = sql_interp( 
    60         'select * from resources join bookmarks on resources.uri = bookmarks.uri', 
    61         ($tag   ? ('join tags on resources.uri = tags.uri where tags.tag =', \$tag) : ''), 
    62         'order by ctime desc', 
    63         ($limit ? ('limit', \$limit) : ()), 
    64         # an offset is only allowed if we have a limit clause 
    65         ($limit && $offset ? ('offset', \$offset) : ()), 
    66     ); 
     59    # build the query 
     60    my @sql; 
     61 
     62    if (!ref $tags) { 
     63        $tags = [ $tags ]; 
     64    } 
     65    if (@$tags) { 
     66        my $intersect = 0; 
     67        for my $tag (@{ $tags }) { 
     68            push @sql, 'intersect' if $intersect; 
     69            push @sql, 'select resources.*, bookmarks.* from resources join bookmarks on resources.uri = bookmarks.uri'; 
     70            push @sql, 'join tags on resources.uri = tags.uri where tags.tag =', \$tag; 
     71            $intersect++; 
     72        } 
     73    } else { 
     74        push @sql, 'select * from resources join bookmarks on resources.uri = bookmarks.uri'; 
     75    } 
     76    push @sql, 'order by ctime desc'; 
     77    push @sql, ('limit', \$limit) if $limit; 
     78    # an offset is only allowed if we have a limit clause 
     79    push @sql, ('offset', \$offset) if $limit && $offset; 
     80 
     81    my ($sql, @bind) = sql_interp(@sql); 
    6782 
    6883    my $sth_resource = $self->dbh->prepare($sql); 
     
    95110    my $self = shift; 
    96111    my $params = shift; 
    97     my $tag = $params->{tag}; 
    98     my $sth = $self->dbh->prepare('select tag, count(tag) as count from tags where tag != ? and uri in (select uri from tags where tag = ?) group by tag order by tag'); 
    99     $sth->execute($tag, $tag); 
     112    my $tags = $params->{tag} || []; 
     113    if (!ref $tags) { 
     114        $tags = [ $tags ]; 
     115    } 
     116    my @sql; 
     117 
     118    push @sql, 'select tag, count(tag) as count from tags'; 
     119    if (@$tags) { 
     120        push @sql, 'where uri in ('; 
     121        my $intersect = 0; 
     122        for my $tag (@{ $tags }) { 
     123            push @sql, 'intersect' if $intersect; 
     124            push @sql, 'select uri from tags where tag = ', \$tag; 
     125            $intersect++; 
     126        } 
     127        push @sql, ') and tag not in ', $tags, ''; 
     128    } 
     129    push @sql, 'group by tag order by tag'; 
     130 
     131    my ($sql, @bind) = sql_interp(@sql); 
     132    my $sth = $self->dbh->prepare($sql); 
     133    $sth->execute(@bind); 
    100134    return @{ $sth->fetchall_arrayref({}) }; 
    101135} 
Note: See TracChangeset for help on using the changeset viewer.