Database: start reworking the DBA to work with PDO

This commit is contained in:
2025-05-13 20:51:43 +02:00
parent 7faa59562d
commit 8dbf1dce7b
9 changed files with 391 additions and 506 deletions

View File

@@ -36,7 +36,7 @@ class Tag
$row = $db->queryAssoc('
SELECT *
FROM tags
WHERE id_tag = {int:id_tag}',
WHERE id_tag = :id_tag',
[
'id_tag' => $id_tag,
]);
@@ -55,7 +55,7 @@ class Tag
$row = $db->queryAssoc('
SELECT *
FROM tags
WHERE slug = {string:slug}',
WHERE slug = :slug',
[
'slug' => $slug,
]);
@@ -73,7 +73,7 @@ class Tag
SELECT *
FROM tags
ORDER BY ' . ($limit > 0 ? 'count
LIMIT {int:limit}' : 'tag'),
LIMIT :limit' : 'tag'),
[
'limit' => $limit,
]);
@@ -107,14 +107,14 @@ class Tag
$res = $db->query('
SELECT *
FROM tags
WHERE id_user_owner = {int:id_user_owner}
WHERE id_user_owner = :id_user_owner
ORDER BY tag',
[
'id_user_owner' => $id_user_owner,
]);
$objects = [];
while ($row = $db->fetch_assoc($res))
while ($row = $db->fetchAssoc($res))
$objects[$row['id_tag']] = new Tag($row);
return $objects;
@@ -125,9 +125,9 @@ class Tag
$rows = Registry::get('db')->queryAssocs('
SELECT *
FROM tags
WHERE id_parent = {int:id_parent} AND kind = {string:kind}
WHERE id_parent = :id_parent AND kind = :kind
ORDER BY tag ASC
LIMIT {int:offset}, {int:limit}',
LIMIT :offset, :limit',
[
'id_parent' => $id_parent,
'kind' => 'Album',
@@ -153,7 +153,7 @@ class Tag
FROM assets_tags AS at
LEFT JOIN assets AS a ON at.id_asset = a.id_asset
LEFT JOIN users AS u ON a.id_user_uploaded = u.id_user
WHERE at.id_tag = {int:id_tag}
WHERE at.id_tag = :id_tag
GROUP BY a.id_user_uploaded
ORDER BY u.first_name, u.surname',
[
@@ -166,9 +166,9 @@ class Tag
$rows = Registry::get('db')->queryAssocs('
SELECT *
FROM tags
WHERE id_parent = {int:id_parent} AND kind = {string:kind}
WHERE id_parent = :id_parent AND kind = :kind
ORDER BY tag ASC
LIMIT {int:offset}, {int:limit}',
LIMIT :offset, :limit',
[
'id_parent' => $id_parent,
'kind' => 'Person',
@@ -195,7 +195,7 @@ class Tag
WHERE id_tag IN(
SELECT id_tag
FROM assets_tags
WHERE id_asset = {int:id_asset}
WHERE id_asset = :id_asset
)
ORDER BY count DESC',
[
@@ -225,7 +225,7 @@ class Tag
WHERE id_tag IN(
SELECT id_tag
FROM posts_tags
WHERE id_post = {int:id_post}
WHERE id_post = :id_post
)
ORDER BY count DESC',
[
@@ -255,7 +255,7 @@ class Tag
FROM `assets_tags` AS at
WHERE at.id_tag = t.id_tag
)' . (!empty($id_tags) ? '
WHERE t.id_tag IN({array_int:id_tags})' : ''),
WHERE t.id_tag IN(@id_tags)' : ''),
['id_tags' => $id_tags]);
}
@@ -276,14 +276,14 @@ class Tag
INSERT IGNORE INTO tags
(id_parent, tag, slug, kind, description, count)
VALUES
({int:id_parent}, {string:tag}, {string:slug}, {string:kind}, {string:description}, {int:count})
(: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.');
$data['id_tag'] = $db->insert_id();
$data['id_tag'] = $db->insertId();
return $return_format === 'object' ? new Tag($data) : $data;
}
@@ -297,14 +297,15 @@ class Tag
return Registry::get('db')->query('
UPDATE tags
SET
id_parent = {int:id_parent},
id_asset_thumb = {int:id_asset_thumb},' . (isset($this->id_user_owner) ? '
id_user_owner = {int:id_user_owner},' : '') . '
tag = {string:tag},
slug = {string:slug},
description = {string:description},
count = {int:count}
WHERE id_tag = {int:id_tag}',
id_parent = :id_parent,
id_asset_thumb = :id_asset_thumb,' . (isset($this->id_user_owner) ? '
id_user_owner = :id_user_owner,' : '') . '
tag = :tag,
slug = :slug,
kind = :kind,
description = :description,
count = :count
WHERE id_tag = :id_tag',
get_object_vars($this));
}
@@ -312,9 +313,10 @@ class Tag
{
$db = Registry::get('db');
// Unlink any tagged assets
$res = $db->query('
DELETE FROM assets_tags
WHERE id_tag = {int:id_tag}',
WHERE id_tag = :id_tag',
[
'id_tag' => $this->id_tag,
]);
@@ -322,9 +324,10 @@ class Tag
if (!$res)
return false;
// Delete the actual tag
return $db->query('
DELETE FROM tags
WHERE id_tag = {int:id_tag}',
WHERE id_tag = :id_tag',
[
'id_tag' => $this->id_tag,
]);
@@ -336,15 +339,15 @@ class Tag
$new_id = $db->queryValue('
SELECT MAX(id_asset) as new_id
FROM assets_tags
WHERE id_tag = {int:id_tag}',
WHERE id_tag = :id_tag',
[
'id_tag' => $this->id_tag,
]);
return $db->query('
UPDATE tags
SET id_asset_thumb = {int:new_id}
WHERE id_tag = {int:id_tag}',
SET id_asset_thumb = :new_id
WHERE id_tag = :id_tag',
[
'new_id' => $new_id ?? 0,
'id_tag' => $this->id_tag,
@@ -359,7 +362,7 @@ class Tag
return Registry::get('db')->queryPair('
SELECT id_tag, tag
FROM tags
WHERE LOWER(tag) LIKE {string:tokens}
WHERE LOWER(tag) LIKE :tokens
ORDER BY tag ASC',
['tokens' => '%' . strtolower(implode('%', $tokens)) . '%']);
}
@@ -389,7 +392,7 @@ class Tag
return Registry::get('db')->queryPair('
SELECT id_tag, tag
FROM tags
WHERE tag = {string:tag}',
WHERE tag = :tag',
['tag' => $tag]);
}
@@ -401,7 +404,7 @@ class Tag
return Registry::get('db')->queryValue('
SELECT id_tag
FROM tags
WHERE slug = {string:slug}',
WHERE slug = :slug',
['slug' => $slug]);
}
@@ -410,7 +413,7 @@ class Tag
return Registry::get('db')->queryPair('
SELECT tag, id_tag
FROM tags
WHERE tag IN ({array_string:tags})',
WHERE tag IN (:tags)',
['tags' => $tags]);
}
@@ -422,7 +425,8 @@ class Tag
if (empty($kind))
$kind = 'Album';
$where[] = 'kind {raw:operator} {string:kind}';
$operator = $isAlbum ? '=' : '!=';
$where[] = 'kind ' . $operator . ' :kind';
$where = implode(' AND ', $where);
return Registry::get('db')->queryValue('
@@ -431,32 +435,32 @@ class Tag
WHERE ' . $where,
[
'kind' => $kind,
'operator' => $isAlbum ? '=' : '!=',
]);
}
public static function getOffset($offset, $limit, $order, $direction, $isAlbum = false)
{
assert(in_array($order, ['id_tag', 'tag', 'slug', 'count']));
$order = $order . ($direction === 'up' ? ' ASC' : ' DESC');
$operator = $isAlbum ? '=' : '!=';
$db = Registry::get('db');
$res = $db->query('
SELECT t.*, u.id_user, u.first_name, u.surname
FROM tags AS t
LEFT JOIN users AS u ON t.id_user_owner = u.id_user
WHERE kind {raw:operator} {string:album}
ORDER BY id_parent, {raw:order}
LIMIT {int:offset}, {int:limit}',
WHERE kind ' . $operator . ' :album
ORDER BY id_parent, ' . $order . '
LIMIT :offset, :limit',
[
'order' => $order . ($direction === 'up' ? ' ASC' : ' DESC'),
'offset' => $offset,
'limit' => $limit,
'album' => 'Album',
'operator' => $isAlbum ? '=' : '!=',
]);
$albums_by_parent = [];
while ($row = $db->fetch_assoc($res))
while ($row = $db->fetchAssoc($res))
{
if (!isset($albums_by_parent[$row['id_parent']]))
$albums_by_parent[$row['id_parent']] = [];