Use querybuilder for TimelineQuery

pull/37/head
Varun Patil 2022-09-10 19:05:04 -07:00
parent 91f329b7b8
commit 48dc8c1a55
3 changed files with 185 additions and 120 deletions

View File

@ -3,132 +3,15 @@ declare(strict_types=1);
namespace OCA\Memories\Db;
use OCA\Memories\Exif;
use OCP\IConfig;
use OCP\IDBConnection;
class TimelineQuery {
use TimelineQueryDays;
use TimelineQueryDay;
protected IDBConnection $connection;
public function __construct(IDBConnection $connection) {
$this->connection = $connection;
}
/**
* Process the days response
* @param array $days
*/
private function processDays(&$days) {
foreach($days as &$row) {
$row["dayid"] = intval($row["dayid"]);
$row["count"] = intval($row["count"]);
}
return $days;
}
/**
* Get the days response from the database for the timeline
* @param IConfig $config
* @param string $userId
*/
public function getDays(
IConfig &$config,
string $user): array {
$sql = 'SELECT `*PREFIX*memories`.`dayid`, COUNT(`*PREFIX*memories`.`fileid`) AS count
FROM `*PREFIX*memories`
INNER JOIN `*PREFIX*filecache`
ON `*PREFIX*filecache`.`fileid` = `*PREFIX*memories`.`fileid`
AND `*PREFIX*filecache`.`path` LIKE ?
WHERE uid=?
GROUP BY `*PREFIX*memories`.`dayid`
ORDER BY `*PREFIX*memories`.`dayid` DESC';
$path = "files" . Exif::getPhotosPath($config, $user) . "%";
$rows = $this->connection->executeQuery($sql, [$path, $user], [
\PDO::PARAM_STR, \PDO::PARAM_STR,
])->fetchAll();
return $this->processDays($rows);
}
/**
* Get the days response from the database for one folder
* @param int $folderId
*/
public function getDaysFolder(int $folderId) {
$sql = 'SELECT `*PREFIX*memories`.`dayid`, COUNT(`*PREFIX*memories`.`fileid`) AS count
FROM `*PREFIX*memories`
INNER JOIN `*PREFIX*filecache`
ON `*PREFIX*filecache`.`fileid` = `*PREFIX*memories`.`fileid`
AND (`*PREFIX*filecache`.`parent`=? OR `*PREFIX*filecache`.`fileid`=?)
GROUP BY dayid
ORDER BY dayid DESC';
$rows = $this->connection->executeQuery($sql, [$folderId, $folderId], [
\PDO::PARAM_INT, \PDO::PARAM_INT,
])->fetchAll();
return $this->processDays($rows);
}
/**
* Process the single day response
* @param array $day
*/
private function processDay(&$day) {
foreach($day as &$row) {
$row["fileid"] = intval($row["fileid"]);
$row["isvideo"] = intval($row["isvideo"]);
if (!$row["isvideo"]) {
unset($row["isvideo"]);
}
}
return $day;
}
/**
* Get a day response from the database for the timeline
* @param IConfig $config
* @param string $userId
* @param int $dayId
*/
public function getDay(
IConfig &$config,
string $user,
int $dayId): array {
$sql = 'SELECT `*PREFIX*memories`.`fileid`, *PREFIX*filecache.etag, `*PREFIX*memories`.`isvideo`
FROM *PREFIX*memories
INNER JOIN *PREFIX*filecache
ON `*PREFIX*filecache`.`fileid` = `*PREFIX*memories`.`fileid`
AND `*PREFIX*filecache`.`path` LIKE ?
WHERE `*PREFIX*memories`.`uid` = ? AND `*PREFIX*memories`.`dayid` = ?
ORDER BY `*PREFIX*memories`.`datetaken` DESC';
$path = "files" . Exif::getPhotosPath($config, $user) . "%";
$rows = $this->connection->executeQuery($sql, [$path, $user, $dayId], [
\PDO::PARAM_STR, \PDO::PARAM_STR, \PDO::PARAM_INT,
])->fetchAll();
return $this->processDay($rows);
}
/**
* Get a day response from the database for one folder
* @param int $folderId
* @param int $dayId
*/
public function getDayFolder(
int $folderId,
int $dayId): array {
$sql = 'SELECT `*PREFIX*memories`.`fileid`, `*PREFIX*filecache`.`etag`, `*PREFIX*memories`.`isvideo`
FROM `*PREFIX*memories`
INNER JOIN `*PREFIX*filecache`
ON `*PREFIX*filecache`.`fileid` = `*PREFIX*memories`.`fileid`
AND (`*PREFIX*filecache`.`parent`=? OR `*PREFIX*filecache`.`fileid`=?)
WHERE `*PREFIX*memories`.`dayid`=?
ORDER BY `*PREFIX*memories`.`datetaken` DESC';
$rows = $this->connection->executeQuery($sql, [$folderId, $folderId, $dayId], [
\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT,
])->fetchAll();
return $this->processDay($rows);
}
}

View File

@ -0,0 +1,96 @@
<?php
declare(strict_types=1);
namespace OCA\Memories\Db;
use OCA\Memories\Exif;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\DB\QueryBuilder\IQueryBuilder;
trait TimelineQueryDay {
protected IDBConnection $connection;
/**
* Process the single day response
* @param array $day
*/
private function processDay(&$day) {
foreach($day as &$row) {
$row["fileid"] = intval($row["fileid"]);
$row["isvideo"] = intval($row["isvideo"]);
if (!$row["isvideo"]) {
unset($row["isvideo"]);
}
}
return $day;
}
/** Get the base query builder for day */
private function makeBaseQueryDay(
IQueryBuilder &$query,
string | null $user,
$whereFilecache,
int $dayid
) {
// Get all entries also present in filecache
$query->select('m.fileid', 'f.etag', 'm.isvideo')
->from('memories', 'm')
->innerJoin('m', 'filecache', 'f',
$query->expr()->andX(
$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)));
}
// Group and sort by date taken
$query->orderBy('m.datetaken', 'DESC');
return $query;
}
/**
* Get a day response from the database for the timeline
* @param IConfig $config
* @param string $userId
* @param int $dayId
*/
public function getDay(
IConfig &$config,
string $user,
int $dayId): array {
$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);
$rows = $query->executeQuery()->fetchAll();
return $this->processDay($rows);
}
/**
* Get a day response from the database for one folder
* @param int $folderId
* @param int $dayId
*/
public function getDayFolder(
int $folderId,
int $dayId): array {
$query = $this->connection->getQueryBuilder();
$parentConstraint = $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

@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
namespace OCA\Memories\Db;
use OCA\Memories\Exif;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\DB\QueryBuilder\IQueryBuilder;
trait TimelineQueryDays {
protected IDBConnection $connection;
/**
* Process the days response
* @param array $days
*/
private function processDays(&$days) {
foreach($days as &$row) {
$row["dayid"] = intval($row["dayid"]);
$row["count"] = intval($row["count"]);
}
return $days;
}
/** Get the base query builder for days */
private function makeBaseQueryDays(
IQueryBuilder &$query,
string | null $user,
$whereFilecache
) {
// Get all entries also present in filecache
$query->select('m.dayid', $query->func()->count('m.fileid', 'count'))
->from('memories', 'm')
->innerJoin('m', 'filecache', 'f',
$query->expr()->andX(
$query->expr()->eq('f.fileid', 'm.fileid'),
$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');
return $query;
}
/**
* Get the days response from the database for the timeline
* @param IConfig $config
* @param string $userId
*/
public function getDays(
IConfig &$config,
string $user): array {
$path = "files" . Exif::getPhotosPath($config, $user) . "%";
$query = $this->connection->getQueryBuilder();
$pathConstraint = $query->expr()->like('f.path', $query->createNamedParameter($path));
$this->makeBaseQueryDays($query, $user, $pathConstraint);
$rows = $query->executeQuery()->fetchAll();
return $this->processDays($rows);
}
/**
* Get the days response from the database for one folder
* @param int $folderId
*/
public function getDaysFolder(int $folderId) {
$query = $this->connection->getQueryBuilder();
$parentConstraint = $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);
}
}