Refactor queries

pull/37/head
Varun Patil 2022-09-11 18:03:40 -07:00
parent b8e5ee7f1b
commit 02fe4d936f
2 changed files with 23 additions and 29 deletions

View File

@ -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);

View File

@ -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);