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 */ /** Get the base query builder for day */
private function makeBaseQueryDay( private function makeQueryDay(
IQueryBuilder &$query, IQueryBuilder &$query,
string | null $user, int $dayid,
$whereFilecache, $whereFilecache
int $dayid
) { ) {
// Get all entries also present in filecache // Get all entries also present in filecache
$query->select('m.fileid', 'f.etag', 'm.isvideo') $query->select('m.fileid', 'f.etag', 'm.isvideo')
@ -41,13 +40,7 @@ trait TimelineQueryDay {
$query->expr()->eq('f.fileid', 'm.fileid'), $query->expr()->eq('f.fileid', 'm.fileid'),
$whereFilecache $whereFilecache
)) ))
->where($query->expr()->eq('m.dayid', $query->createNamedParameter($dayid, IQueryBuilder::PARAM_INT))); ->andWhere($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)));
}
// Group and sort by date taken // Group and sort by date taken
$query->orderBy('m.datetaken', 'DESC'); $query->orderBy('m.datetaken', 'DESC');
@ -65,10 +58,15 @@ trait TimelineQueryDay {
string $user, string $user,
int $dayId): array { int $dayId): array {
// Filter by path starting with timeline path
$path = "files" . Exif::getPhotosPath($config, $user) . "%"; $path = "files" . Exif::getPhotosPath($config, $user) . "%";
$query = $this->connection->getQueryBuilder(); $query = $this->connection->getQueryBuilder();
$pathConstraint = $query->expr()->like('f.path', $query->createNamedParameter($path)); $this->makeQueryDay($query, $dayId, $query->expr()->like(
$this->makeBaseQueryDay($query, $user, $pathConstraint, $dayId); 'f.path', $query->createNamedParameter($path)
));
// Filter by UID
$query->andWhere($query->expr()->eq('uid', $query->createNamedParameter($user)));
$rows = $query->executeQuery()->fetchAll(); $rows = $query->executeQuery()->fetchAll();
return $this->processDay($rows); return $this->processDay($rows);
@ -84,11 +82,10 @@ trait TimelineQueryDay {
int $dayId): array { int $dayId): array {
$query = $this->connection->getQueryBuilder(); $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.parent', $query->createNamedParameter($folderId, IQueryBuilder::PARAM_INT)),
$query->expr()->eq('f.fileid', $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(); $rows = $query->executeQuery()->fetchAll();
return $this->processDay($rows); return $this->processDay($rows);

View File

@ -24,9 +24,8 @@ trait TimelineQueryDays {
} }
/** Get the base query builder for days */ /** Get the base query builder for days */
private function makeBaseQueryDays( private function makeQueryDays(
IQueryBuilder &$query, IQueryBuilder &$query,
string | null $user,
$whereFilecache $whereFilecache
) { ) {
// Get all entries also present in filecache // Get all entries also present in filecache
@ -38,12 +37,6 @@ trait TimelineQueryDays {
$whereFilecache $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 // Group and sort by dayid
$query->groupBy('m.dayid') $query->groupBy('m.dayid')
->orderBy('m.dayid', 'DESC'); ->orderBy('m.dayid', 'DESC');
@ -59,10 +52,15 @@ trait TimelineQueryDays {
IConfig &$config, IConfig &$config,
string $user): array { string $user): array {
// Filter by path starting with timeline path
$path = "files" . Exif::getPhotosPath($config, $user) . "%"; $path = "files" . Exif::getPhotosPath($config, $user) . "%";
$query = $this->connection->getQueryBuilder(); $query = $this->connection->getQueryBuilder();
$pathConstraint = $query->expr()->like('f.path', $query->createNamedParameter($path)); $this->makeQueryDays($query, $query->expr()->like(
$this->makeBaseQueryDays($query, $user, $pathConstraint); 'f.path', $query->createNamedParameter($path)
));
// Filter by user
$query->andWhere($query->expr()->eq('uid', $query->createNamedParameter($user)));
$rows = $query->executeQuery()->fetchAll(); $rows = $query->executeQuery()->fetchAll();
return $this->processDays($rows); return $this->processDays($rows);
@ -74,11 +72,10 @@ trait TimelineQueryDays {
*/ */
public function getDaysFolder(int $folderId) { public function getDaysFolder(int $folderId) {
$query = $this->connection->getQueryBuilder(); $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.parent', $query->createNamedParameter($folderId, IQueryBuilder::PARAM_INT)),
$query->expr()->eq('f.fileid', $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(); $rows = $query->executeQuery()->fetchAll();
return $this->processDays($rows); return $this->processDays($rows);