have query use EXISTS rather than IN

preferably we would have a table with image tag counts, but this works for now
This commit is contained in:
Daku 2014-02-24 01:50:31 +00:00
parent 790cb0d7a9
commit 320a92289b

View File

@ -51,7 +51,7 @@ class TagCategories extends Extension {
public function onSearchTermParse(SearchTermParseEvent $event) { public function onSearchTermParse(SearchTermParseEvent $event) {
$matches = array(); $matches = array();
if(preg_match("/^(.+)tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])([0-9]+)$/", $event->term, $matches)) { if(preg_match("/^(.+)tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])([0-9]+)$/i", $event->term, $matches)) {
global $database; global $database;
$type = $matches[1]; $type = $matches[1];
$cmp = ltrim($matches[2], ":") ?: "="; $cmp = ltrim($matches[2], ":") ?: "=";
@ -59,15 +59,15 @@ class TagCategories extends Extension {
$types = $database->get_col('SELECT category FROM image_tag_categories'); $types = $database->get_col('SELECT category FROM image_tag_categories');
if(in_array($type, $types)) { if(in_array($type, $types)) {
$event->add_querylet(new Querylet("images.id IN ( $event->add_querylet(
SELECT * FROM ( new Querylet("EXISTS (
SELECT it.image_id SELECT 1
FROM image_tags it FROM image_tags it
LEFT JOIN tags t ON it.tag_id = t.id LEFT JOIN tags t ON it.tag_id = t.id
GROUP BY image_id WHERE images.id = it.image_id
HAVING SUM(CASE WHEN t.tag LIKE '$type:%' THEN 1 ELSE 0 END) $cmp $count GROUP BY image_id
) AS subquery HAVING SUM(CASE WHEN t.tag LIKE '$type:%' THEN 1 ELSE 0 END) $cmp $count
)")); )"));
} }
} }
} }