From bcc9b612c1e1c299d6c460ce0ea97ce3b3a0dc96 Mon Sep 17 00:00:00 2001 From: shish Date: Mon, 23 Jul 2007 00:52:24 +0000 Subject: [PATCH] if a tag has no matches, error git-svn-id: file:///home/shish/svn/shimmie2/trunk@354 7f39781d-f577-437e-ae19-be835c7a54ca --- core/database.class.php | 52 +++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/core/database.class.php b/core/database.class.php index 4ce701c6..9fc69df5 100644 --- a/core/database.class.php +++ b/core/database.class.php @@ -171,27 +171,43 @@ class Database { $s_tag_list = join(', ', $s_tag_array); $tag_id_array = array(); + $tags_ok = true; foreach($tag_search->variables as $tag) { - $tag_id_array = array_merge($tag_id_array, $this->db->GetCol("SELECT id FROM tags WHERE tag LIKE ?", array($tag))); + $tag_ids = $this->db->GetCol("SELECT id FROM tags WHERE tag LIKE ?", array($tag)); + $tag_id_array = array_merge($tag_id_array, $tag_ids); + $tags_ok = count($tag_ids) > 0; + if(!$tags_ok) break; } - $tag_id_list = join(', ', $tag_id_array); + if($tags_ok) { + $tag_id_list = join(', ', $tag_id_array); - $subquery = new Querylet(" - SELECT images.*, SUM({$tag_search->sql}) AS score - FROM images - LEFT JOIN image_tags ON image_tags.image_id = images.id - JOIN tags ON image_tags.tag_id = tags.id - WHERE tags.id IN ({$tag_id_list}) - GROUP BY images.id - HAVING score = ?", - array_merge( - $tag_search->variables, - array($positive_tag_count) - ) - ); - $query = new Querylet(" - SELECT *, UNIX_TIMESTAMP(posted) AS posted_timestamp - FROM ({$subquery->sql}) AS images ", $subquery->variables); + $subquery = new Querylet(" + SELECT images.*, SUM({$tag_search->sql}) AS score + FROM images + LEFT JOIN image_tags ON image_tags.image_id = images.id + JOIN tags ON image_tags.tag_id = tags.id + WHERE tags.id IN ({$tag_id_list}) + GROUP BY images.id + HAVING score = ?", + array_merge( + $tag_search->variables, + array($positive_tag_count) + ) + ); + $query = new Querylet(" + SELECT *, UNIX_TIMESTAMP(posted) AS posted_timestamp + FROM ({$subquery->sql}) AS images ", $subquery->variables); + } + else { + # there are no results, "where 1=0" should shortcut things + $query = new Querylet(" + SELECT images.* + FROM images + LEFT JOIN image_tags ON image_tags.image_id = images.id + JOIN tags ON image_tags.tag_id = tags.id + WHERE 1=0 + "); + } if(strlen($img_search->sql) > 0) { $query->append_sql("WHERE 1=1 ");