1
0
forked from Public/pics

Refactor fragile SQLite regex rewrites into driver-aware query methods

Move the ON DUPLICATE KEY UPDATE and UPDATE...AS alias SQL rewrites out
of Database::rewriteForSQLite() and into Tag::createNew() and
Tag::recount() as driver-aware branches via a new Database::getDriver()
method. This keeps dialect-specific SQL explicit at the call site rather
than buried in fragile regex transforms.

Also fix Tag::getAlbums() and Tag::getPeople() failing on SQLite when
id_parent is NULL by adding an IS NULL fallback for root-level queries.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-14 15:40:46 +01:00
parent a4d453792d
commit d788391f4a
2 changed files with 48 additions and 35 deletions

View File

@@ -122,10 +122,12 @@ class Tag
public static function getAlbums($id_parent = 0, $offset = 0, $limit = 24, $return_format = 'array')
{
$parent_clause = empty($id_parent) ? '(id_parent = :id_parent OR id_parent IS NULL)' : 'id_parent = :id_parent';
$rows = Registry::get('db')->queryAssocs('
SELECT *
FROM tags
WHERE id_parent = :id_parent AND kind = :kind
WHERE ' . $parent_clause . ' AND kind = :kind
ORDER BY tag ASC
LIMIT :offset, :limit',
[
@@ -163,10 +165,12 @@ class Tag
public static function getPeople($id_parent = 0, $offset = 0, $limit = 24, $return_format = 'array')
{
$parent_clause = empty($id_parent) ? '(id_parent = :id_parent OR id_parent IS NULL)' : 'id_parent = :id_parent';
$rows = Registry::get('db')->queryAssocs('
SELECT *
FROM tags
WHERE id_parent = :id_parent AND kind = :kind
WHERE ' . $parent_clause . ' AND kind = :kind
ORDER BY tag ASC
LIMIT :offset, :limit',
[
@@ -249,7 +253,21 @@ class Tag
public static function recount(array $id_tags = [])
{
return Registry::get('db')->query('
$db = Registry::get('db');
if ($db->getDriver() === 'sqlite')
{
return $db->query('
UPDATE tags SET count = (
SELECT COUNT(*)
FROM `assets_tags` AS at
WHERE at.id_tag = tags.id_tag
)' . (!empty($id_tags) ? '
WHERE tags.id_tag IN(@id_tags)' : ''),
['id_tags' => $id_tags]);
}
return $db->query('
UPDATE tags AS t SET count = (
SELECT COUNT(*)
FROM `assets_tags` AS at
@@ -272,13 +290,26 @@ class Tag
if (!isset($data['count']))
$data['count'] = 0;
$res = $db->query('
INSERT IGNORE INTO tags
(id_parent, tag, slug, kind, description, count)
VALUES
(:id_parent, :tag, :slug, :kind, :description, :count)
ON DUPLICATE KEY UPDATE count = count + 1',
$data);
if ($db->getDriver() === 'sqlite')
{
$res = $db->query('
INSERT INTO tags
(id_parent, tag, slug, kind, description, count)
VALUES
(:id_parent, :tag, :slug, :kind, :description, :count)
ON CONFLICT(slug) DO UPDATE SET count = count + 1',
$data);
}
else
{
$res = $db->query('
INSERT IGNORE INTO tags
(id_parent, tag, slug, kind, description, count)
VALUES
(:id_parent, :tag, :slug, :kind, :description, :count)
ON DUPLICATE KEY UPDATE count = count + 1',
$data);
}
if (!$res)
throw new Exception('Could not create the requested tag.');