inline build_accurate_search_querylet

This commit is contained in:
Shish 2020-02-05 00:16:47 +00:00
parent 188d809ee7
commit 1a07f84622

View file

@ -859,8 +859,9 @@ class Image
?string $order=null, ?string $order=null,
?int $limit=null, ?int $limit=null,
?int $offset=null ?int $offset=null
): Querylet ): Querylet {
{ global $database;
list($tag_conditions, $img_conditions) = self::terms_to_conditions($tags); list($tag_conditions, $img_conditions) = self::terms_to_conditions($tags);
$positive_tag_count = 0; $positive_tag_count = 0;
@ -932,49 +933,6 @@ class Image
// more than one positive tag, or more than zero negative tags // more than one positive tag, or more than zero negative tags
else { else {
$query = Image::build_accurate_search_querylet($tag_conditions);
}
/*
* Merge all the image metadata searches into one generic querylet
* and append to the base querylet with "AND blah"
*/
if (!empty($img_conditions)) {
$n = 0;
$img_sql = "";
$img_vars = [];
foreach ($img_conditions as $iq) {
if ($n++ > 0) {
$img_sql .= " AND";
}
if (!$iq->positive) {
$img_sql .= " NOT";
}
$img_sql .= " (" . $iq->qlet->sql . ")";
$img_vars = array_merge($img_vars, $iq->qlet->variables);
}
$query->append_sql(" AND ");
$query->append(new Querylet($img_sql, $img_vars));
}
if (!is_null($order)) {
$query->append(new Querylet(" ORDER BY ".$order));
}
if (!is_null($limit)) {
$query->append(new Querylet(" LIMIT :limit ", ["limit" => $limit]));
$query->append(new Querylet(" OFFSET :offset ", ["offset" => $offset]));
}
return $query;
}
/**
* #param TagQuerylet[] $tag_conditions
*/
private static function build_accurate_search_querylet(array $tag_conditions): Querylet
{
global $database;
$positive_tag_id_array = []; $positive_tag_id_array = [];
$positive_wildcard_id_array = []; $positive_wildcard_id_array = [];
$negative_tag_id_array = []; $negative_tag_id_array = [];
@ -1051,23 +1009,54 @@ class Image
} }
$sub_query .= " GROUP BY it.image_id "; $sub_query .= " GROUP BY it.image_id ";
$sql = " $query = new Querylet("
SELECT images.* SELECT images.*
FROM images INNER JOIN ( FROM images
$sub_query INNER JOIN ($sub_query) a on a.image_id = images.id
) a on a.image_id = images.id ");
";
} elseif (!empty($negative_tag_id_array)) { } elseif (!empty($negative_tag_id_array)) {
$negative_tag_id_list = join(', ', $negative_tag_id_array); $negative_tag_id_list = join(', ', $negative_tag_id_array);
$sql = " $query = new Querylet("
SELECT images.* SELECT images.*
FROM images LEFT JOIN image_tags negative ON negative.image_id = images.id AND negative.tag_id in ($negative_tag_id_list) FROM images
LEFT JOIN image_tags negative ON negative.image_id = images.id AND negative.tag_id in ($negative_tag_id_list)
WHERE negative.image_id IS NULL WHERE negative.image_id IS NULL
"; ");
} else { } else {
throw new SCoreException("No criteria specified"); throw new SCoreException("No criteria specified");
} }
}
return new Querylet($sql); /*
* Merge all the image metadata searches into one generic querylet
* and append to the base querylet with "AND blah"
*/
if (!empty($img_conditions)) {
$n = 0;
$img_sql = "";
$img_vars = [];
foreach ($img_conditions as $iq) {
if ($n++ > 0) {
$img_sql .= " AND";
}
if (!$iq->positive) {
$img_sql .= " NOT";
}
$img_sql .= " (" . $iq->qlet->sql . ")";
$img_vars = array_merge($img_vars, $iq->qlet->variables);
}
$query->append_sql(" AND ");
$query->append(new Querylet($img_sql, $img_vars));
}
if (!is_null($order)) {
$query->append(new Querylet(" ORDER BY ".$order));
}
if (!is_null($limit)) {
$query->append(new Querylet(" LIMIT :limit ", ["limit" => $limit]));
$query->append(new Querylet(" OFFSET :offset ", ["offset" => $offset]));
}
return $query;
} }
} }