Get rid of day table

pull/37/head
Varun Patil 2022-08-16 02:47:49 +00:00
parent 83e6247716
commit b26c1d3108
2 changed files with 19 additions and 91 deletions

View File

@ -56,90 +56,40 @@ class Util {
$dateTaken = $this->getDateTaken($file);
$dayId = floor($dateTaken / 86400);
// Get existing entry
$sql = 'SELECT * FROM oc_polaroid WHERE
user_id = ? AND file_id = ?';
$res = $this->connection->executeQuery($sql, [
$user, $fileId,
]);
$erow = $res->fetch();
$exists = (bool)$erow;
// Insert or update file
if ($exists) {
$sql = 'UPDATE oc_polaroid SET
day_id = ?, date_taken = ?, is_video = ?
WHERE user_id = ? AND file_id = ?';
} else {
$sql = 'INSERT
INTO oc_polaroid (day_id, date_taken, is_video, user_id, file_id)
VALUES (?, ?, ?, ?, ?)';
}
$res = $this->connection->executeStatement($sql, [
$sql = 'INSERT
INTO oc_polaroid (day_id, date_taken, is_video, user_id, file_id)
VALUES (?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
day_id = ?, date_taken = ?, is_video = ?';
$this->connection->executeStatement($sql, [
$dayId, $dateTaken, $is_video,
$user, $fileId,
$dayId, $dateTaken, $is_video,
], [
\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_BOOL,
\PDO::PARAM_STR, \PDO::PARAM_INT,
\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_BOOL,
]);
// Change of day
$dayChange = ($exists && intval($erow['day_id']) != $dayId);
// Update day table
if (!$exists || $dayChange) {
$sql = 'INSERT
INTO oc_polaroid_day (user_id, day_id, count)
VALUES (?, ?, 1)
ON DUPLICATE KEY
UPDATE count = count + 1';
$this->connection->executeStatement($sql, [
$user, $dayId,
]);
if ($dayChange) {
$sql = 'UPDATE oc_polaroid_day SET
count = count - 1
WHERE user_id = ? AND day_id = ?';
$this->connection->executeStatement($sql, [
$user, $erow['day_id'],
], [
\PDO::PARAM_STR, \PDO::PARAM_INT,
]);
}
}
}
public function deleteFile(File $file) {
$sql = 'DELETE
FROM oc_polaroid
WHERE file_id = ?
RETURNING *';
$res = $this->connection->executeQuery($sql, [$file->getId()], [\PDO::PARAM_INT]);
$rows = $res->fetchAll();
foreach ($rows as $row) {
$dayId = $row['day_id'];
$userId = $row['user_id'];
$sql = 'UPDATE oc_polaroid_day
SET count = count - 1
WHERE user_id = ? AND day_id = ?';
$this->connection->executeStatement($sql, [$userId, $dayId], [
\PDO::PARAM_STR, \PDO::PARAM_INT,
]);
}
WHERE file_id = ?';
$this->connection->executeStatement($sql, [$file->getId()], [\PDO::PARAM_INT]);
}
public function getDays(
string $user,
): array {
$qb = $this->connection->getQueryBuilder();
$qb->select('day_id', 'count')
->from('polaroid_day')
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user)))
->orderBy('day_id', 'DESC');
$result = $qb->executeQuery();
$rows = $result->fetchAll();
$sql = 'SELECT day_id, COUNT(file_id) AS count
FROM `oc_polaroid`
WHERE user_id=?
GROUP BY day_id
ORDER BY day_id DESC';
$rows = $this->connection->executeQuery($sql, [$user], [
\PDO::PARAM_STR,
])->fetchAll();
return $rows;
}

View File

@ -48,32 +48,10 @@
$table->setPrimaryKey(['id']);
$table->addIndex(['user_id'], 'polaroid_user_id_index');
$table->addIndex(['day_id'], 'polaroid_day_id_index');
$table->addIndex(['user_id', 'day_id'], 'polaroid_ud_index');
$table->addUniqueIndex(['user_id', 'file_id'], 'polaroid_day_uf_ui');
}
if (!$schema->hasTable('polaroid_day')) {
$table = $schema->createTable('polaroid_day');
$table->addColumn('id', 'integer', [
'autoincrement' => true,
'notnull' => true,
]);
$table->addColumn('user_id', 'string', [
'notnull' => true,
'length' => 200,
]);
$table->addColumn('count', Types::INTEGER, [
'notnull' => true,
'default' => 0,
]);
$table->addColumn('day_id', Types::INTEGER, [
'notnull' => true,
]);
$table->setPrimaryKey(['id']);
$table->addIndex(['user_id'], 'polaroid_day_user_id_index');
$table->addUniqueIndex(['user_id', 'day_id'], 'polaroid_day_ud_ui');
}
return $schema;
}
}