Changeset 71 in bookmarks for trunk/lib/Bookmarks.pm
- Timestamp:
- 02/13/14 16:10:42 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Bookmarks.pm
r70 r71 2 2 3 3 use Moose; 4 4 5 use SQL::Interp qw{:all}; 5 6 use URI; 7 6 8 use Bookmark; 7 9 use Bookmarks::List; 10 use Bookmarks::Search; 8 11 9 12 has dbh => ( is => 'rw' ); … … 73 76 my $self = shift; 74 77 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); 79 79 80 80 # build the query 81 81 my @sql; 82 82 83 if (!ref $tags) { 84 $tags = [ $tags ]; 85 } 86 if (@$tags) { 83 if (@{ $search->tags }) { 87 84 my $intersect = 0; 88 for my $tag (@{ $ tags }) {85 for my $tag (@{ $search->tags }) { 89 86 push @sql, 'intersect' if $intersect; 90 87 push @sql, 'select resources.*, bookmarks.* from resources join bookmarks on resources.uri = bookmarks.uri'; … … 95 92 push @sql, 'select * from resources join bookmarks on resources.uri = bookmarks.uri'; 96 93 } 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; 99 97 } 100 98 push @sql, 'order by ctime desc'; 101 push @sql, ('limit', \$ limit) if $limit;99 push @sql, ('limit', \$search->limit) if $search->limit; 102 100 # 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; 104 102 105 103 my ($sql, @bind) = sql_interp(@sql); 106 #die $sql;107 104 108 105 my $sth_resource = $self->dbh->prepare($sql); … … 119 116 return Bookmarks::List->new({ 120 117 bookmarks => $self, 121 tags => $tags, 122 query => $query, 123 limit => $limit, 124 offset => $offset, 118 search => $search, 125 119 results => \@resources, 126 120 }); … … 147 141 my $self = shift; 148 142 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 154 145 my @sql; 155 146 156 147 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; 158 149 159 150 # build the where clause 160 if (@ $tags) {151 if (@{ $search->tags }) { 161 152 push @sql, 'where tags.uri in ('; 162 153 my $intersect = 0; 163 for my $tag (@{ $ tags }) {154 for my $tag (@{ $search->tags }) { 164 155 push @sql, 'intersect' if $intersect; 165 156 push @sql, 'select uri from tags where tag = ', \$tag; 166 157 $intersect++; 167 158 } 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; 172 164 } 173 165
Note: See TracChangeset
for help on using the changeset viewer.