Skip to content

Commit

Permalink
fixup! Use generic table to store location data
Browse files Browse the repository at this point in the history
  • Loading branch information
artonge committed Feb 13, 2023
1 parent eda3f83 commit 540c329
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions lib/DB/Location/LocationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

class LocationMapper {
public const TABLE_NAME = 'photos_metadata';
public const METADATA_NAME = 'location';
public const METADATA_TYPE = 'location';

public function __construct(
private IDBConnection $connection,
Expand All @@ -47,7 +47,7 @@ public function findLocationsForUser(string $userId): array {
$rows = $qb->selectDistinct('value')
->from(self::TABLE_NAME)
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
->andWhere($qb->expr()->eq('name', $qb->createNamedParameter(self::METADATA_NAME)))
->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(self::METADATA_TYPE)))
->executeQuery()
->fetchAll();

Expand All @@ -62,7 +62,7 @@ public function findFilesForUserAndLocation(string $userId, int $locationId) {
->from(self::TABLE_NAME, 'm')
->leftJoin("m", "filecache", "f", $qb->expr()->eq("m.file_id", "f.fileid"))
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
->andWhere($qb->expr()->eq('m.name', $qb->createNamedParameter(self::METADATA_NAME)))
->andWhere($qb->expr()->eq('m.type', $qb->createNamedParameter(self::METADATA_TYPE)))
->andWhere($qb->expr()->eq('m.value', $qb->createNamedParameter($locationId)))
->executeQuery();

Expand All @@ -86,7 +86,7 @@ public function addLocationForFileAndUser(int $locationId, int $fileId, string $
$query->insert(self::TABLE_NAME)
->values([
"user_id" => $query->createNamedParameter($userId),
"name" => $query->createNamedParameter(self::METADATA_NAME),
"type" => $query->createNamedParameter(self::METADATA_TYPE),
"value" => $query->createNamedParameter($locationId),
"file_id" => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
])
Expand All @@ -103,15 +103,15 @@ public function updateLocationForFile(int $locationId, int $fileId): void {
$query->update(self::TABLE_NAME)
->set("value", $query->createNamedParameter($locationId))
->where($query->expr()->eq('file_id', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('name', $query->createNamedParameter(self::METADATA_NAME)))
->andWhere($query->expr()->eq('type', $query->createNamedParameter(self::METADATA_TYPE)))
->executeStatement();
}

public function removeLocationForFile(int $fileId, ?string $userId = null): void {
$query = $this->connection->getQueryBuilder();
$query->delete(self::TABLE_NAME)
->where($query->expr()->eq('file_id', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('name', $query->createNamedParameter(self::METADATA_NAME)));
->andWhere($query->expr()->eq('type', $query->createNamedParameter(self::METADATA_TYPE)));

if ($userId !== null) {
$query->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId)));
Expand All @@ -124,7 +124,7 @@ public function removeLocationForUser(string $userId): void {
$query = $this->connection->getQueryBuilder();
$query->delete(self::TABLE_NAME)
->where($query->expr()->eq('user_id', $query->createNamedParameter($userId)))
->andWhere($query->expr()->eq('name', $query->createNamedParameter(self::METADATA_NAME)))
->andWhere($query->expr()->eq('type', $query->createNamedParameter(self::METADATA_TYPE)))
->executeStatement();
}
}
4 changes: 2 additions & 2 deletions lib/Migration/Version20002Date20221012131022.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
$table->addColumn('file_id', Types::BIGINT, [
'notnull' => true,
]);
$table->addColumn('name', Types::STRING, [
$table->addColumn('type', Types::STRING, [
'notnull' => true,
'length' => 64,
]);
Expand All @@ -72,7 +72,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
]);

$table->setPrimaryKey(['id']);
$table->addUniqueConstraint(['user_id', 'file_id', 'name'], 'photos_metadata_unique_idx');
$table->addUniqueConstraint(['user_id', 'file_id', 'type'], 'photos_metadata_unique_idx');
}

if ($modified) {
Expand Down

0 comments on commit 540c329

Please sign in to comment.