From b26c1d31084ed29681ae4ec239347c3ea396491e Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Tue, 16 Aug 2022 02:47:49 +0000 Subject: [PATCH] Get rid of day table --- lib/Db/Util.php | 86 ++++--------------- .../Version000000Date20220812163631.php | 24 +----- 2 files changed, 19 insertions(+), 91 deletions(-) diff --git a/lib/Db/Util.php b/lib/Db/Util.php index 5050c628..528e65ec 100644 --- a/lib/Db/Util.php +++ b/lib/Db/Util.php @@ -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; } diff --git a/lib/Migration/Version000000Date20220812163631.php b/lib/Migration/Version000000Date20220812163631.php index 4b335e36..c06e1a04 100644 --- a/lib/Migration/Version000000Date20220812163631.php +++ b/lib/Migration/Version000000Date20220812163631.php @@ -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; } } \ No newline at end of file