From 02fe4d936f16bec3758cfc519ffb3b1a76c29e61 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Sun, 11 Sep 2022 18:03:40 -0700 Subject: [PATCH] Refactor queries --- lib/Db/TimelineQueryDay.php | 29 +++++++++++++---------------- lib/Db/TimelineQueryDays.php | 23 ++++++++++------------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/lib/Db/TimelineQueryDay.php b/lib/Db/TimelineQueryDay.php index 1569f97c..51e7f58d 100644 --- a/lib/Db/TimelineQueryDay.php +++ b/lib/Db/TimelineQueryDay.php @@ -27,11 +27,10 @@ trait TimelineQueryDay { } /** Get the base query builder for day */ - private function makeBaseQueryDay( + private function makeQueryDay( IQueryBuilder &$query, - string | null $user, - $whereFilecache, - int $dayid + int $dayid, + $whereFilecache ) { // Get all entries also present in filecache $query->select('m.fileid', 'f.etag', 'm.isvideo') @@ -41,13 +40,7 @@ trait TimelineQueryDay { $query->expr()->eq('f.fileid', 'm.fileid'), $whereFilecache )) - ->where($query->expr()->eq('m.dayid', $query->createNamedParameter($dayid, IQueryBuilder::PARAM_INT))); - - // Filter by user - // This won't be used when looking at e.g. a shared folder - if (!is_null($user)) { - $query->andWhere($query->expr()->eq('uid', $query->createNamedParameter($user))); - } + ->andWhere($query->expr()->eq('m.dayid', $query->createNamedParameter($dayid, IQueryBuilder::PARAM_INT))); // Group and sort by date taken $query->orderBy('m.datetaken', 'DESC'); @@ -65,10 +58,15 @@ trait TimelineQueryDay { string $user, int $dayId): array { + // Filter by path starting with timeline path $path = "files" . Exif::getPhotosPath($config, $user) . "%"; $query = $this->connection->getQueryBuilder(); - $pathConstraint = $query->expr()->like('f.path', $query->createNamedParameter($path)); - $this->makeBaseQueryDay($query, $user, $pathConstraint, $dayId); + $this->makeQueryDay($query, $dayId, $query->expr()->like( + 'f.path', $query->createNamedParameter($path) + )); + + // Filter by UID + $query->andWhere($query->expr()->eq('uid', $query->createNamedParameter($user))); $rows = $query->executeQuery()->fetchAll(); return $this->processDay($rows); @@ -84,11 +82,10 @@ trait TimelineQueryDay { int $dayId): array { $query = $this->connection->getQueryBuilder(); - $parentConstraint = $query->expr()->orX( + $this->makeQueryDay($query, $dayId, $query->expr()->orX( $query->expr()->eq('f.parent', $query->createNamedParameter($folderId, IQueryBuilder::PARAM_INT)), $query->expr()->eq('f.fileid', $query->createNamedParameter($folderId, IQueryBuilder::PARAM_INT)), - ); - $this->makeBaseQueryDay($query, null, $parentConstraint, $dayId); + )); $rows = $query->executeQuery()->fetchAll(); return $this->processDay($rows); diff --git a/lib/Db/TimelineQueryDays.php b/lib/Db/TimelineQueryDays.php index 5a163cde..5607b843 100644 --- a/lib/Db/TimelineQueryDays.php +++ b/lib/Db/TimelineQueryDays.php @@ -24,9 +24,8 @@ trait TimelineQueryDays { } /** Get the base query builder for days */ - private function makeBaseQueryDays( + private function makeQueryDays( IQueryBuilder &$query, - string | null $user, $whereFilecache ) { // Get all entries also present in filecache @@ -38,12 +37,6 @@ trait TimelineQueryDays { $whereFilecache )); - // Filter by user - // This won't be used when looking at e.g. a shared folder - if (!is_null($user)) { - $query->where($query->expr()->eq('uid', $query->createNamedParameter($user))); - } - // Group and sort by dayid $query->groupBy('m.dayid') ->orderBy('m.dayid', 'DESC'); @@ -59,10 +52,15 @@ trait TimelineQueryDays { IConfig &$config, string $user): array { + // Filter by path starting with timeline path $path = "files" . Exif::getPhotosPath($config, $user) . "%"; $query = $this->connection->getQueryBuilder(); - $pathConstraint = $query->expr()->like('f.path', $query->createNamedParameter($path)); - $this->makeBaseQueryDays($query, $user, $pathConstraint); + $this->makeQueryDays($query, $query->expr()->like( + 'f.path', $query->createNamedParameter($path) + )); + + // Filter by user + $query->andWhere($query->expr()->eq('uid', $query->createNamedParameter($user))); $rows = $query->executeQuery()->fetchAll(); return $this->processDays($rows); @@ -74,11 +72,10 @@ trait TimelineQueryDays { */ public function getDaysFolder(int $folderId) { $query = $this->connection->getQueryBuilder(); - $parentConstraint = $query->expr()->orX( + $this->makeQueryDays($query, $query->expr()->orX( $query->expr()->eq('f.parent', $query->createNamedParameter($folderId, IQueryBuilder::PARAM_INT)), $query->expr()->eq('f.fileid', $query->createNamedParameter($folderId, IQueryBuilder::PARAM_INT)), - ); - $this->makeBaseQueryDays($query, null, $parentConstraint); + )); $rows = $query->executeQuery()->fetchAll(); return $this->processDays($rows);