memories/lib/Db/TimelineQuery.php

85 lines
2.3 KiB
PHP
Raw Normal View History

2022-08-20 02:53:21 +00:00
<?php
2022-10-19 17:10:36 +00:00
2022-08-20 02:53:21 +00:00
declare(strict_types=1);
namespace OCA\Memories\Db;
2022-10-27 16:19:25 +00:00
use OCP\DB\QueryBuilder\IQueryBuilder;
2022-08-20 02:53:21 +00:00
use OCP\IDBConnection;
2022-10-19 17:10:36 +00:00
class TimelineQuery
{
2022-10-26 23:20:28 +00:00
use TimelineQueryAlbums;
2022-09-11 02:05:04 +00:00
use TimelineQueryDays;
2022-10-19 17:10:36 +00:00
use TimelineQueryFaces;
2022-09-13 07:55:32 +00:00
use TimelineQueryFilters;
2022-10-06 21:37:18 +00:00
use TimelineQueryTags;
2022-09-11 02:05:04 +00:00
2022-09-09 07:31:42 +00:00
protected IDBConnection $connection;
2022-08-20 02:53:21 +00:00
2022-10-19 17:10:36 +00:00
public function __construct(IDBConnection $connection)
{
2022-09-09 07:31:42 +00:00
$this->connection = $connection;
}
2022-09-25 13:21:40 +00:00
2022-10-27 19:54:51 +00:00
public static function debugQuery(IQueryBuilder &$query, string $sql = '')
2022-10-27 16:19:25 +00:00
{
// Print the query and exit
$sql = empty($sql) ? $query->getSQL() : $sql;
$sql = str_replace('*PREFIX*', 'oc_', $sql);
2022-10-27 19:58:47 +00:00
$sql = self::replaceQueryParams($query, $sql);
2022-10-27 19:54:51 +00:00
echo "{$sql}";
exit;
}
public static function replaceQueryParams(IQueryBuilder &$query, string $sql)
{
$params = $query->getParameters();
2022-10-27 16:19:25 +00:00
foreach ($params as $key => $value) {
$sql = str_replace(':'.$key, $query->getConnection()->getDatabasePlatform()->quoteStringLiteral($value), $sql);
}
2022-10-27 19:54:51 +00:00
return $sql;
2022-10-27 16:19:25 +00:00
}
2022-10-28 21:26:56 +00:00
public function transformExtraFields(IQueryBuilder &$query, string $uid, array &$fields)
{
if (\in_array('basename', $fields, true)) {
$query->addSelect('f.name AS basename');
}
if (\in_array('mimetype', $fields, true)) {
$query->join('f', 'mimetypes', 'mimetypes', $query->expr()->eq('f.mimetype', 'mimetypes.id'));
$query->addSelect('mimetypes.mimetype');
}
}
2022-10-19 17:10:36 +00:00
public function getInfoById(int $id): array
{
2022-09-25 13:21:40 +00:00
$qb = $this->connection->getQueryBuilder();
2022-09-27 21:36:23 +00:00
$qb->select('fileid', 'dayid', 'datetaken')
2022-09-25 13:21:40 +00:00
->from('memories')
2022-10-19 17:10:36 +00:00
->where($qb->expr()->eq('fileid', $qb->createNamedParameter($id, \PDO::PARAM_INT)))
;
2022-09-25 13:21:40 +00:00
$result = $qb->executeQuery();
$row = $result->fetch();
$result->closeCursor();
2022-09-27 21:36:23 +00:00
$utcTs = 0;
2022-10-19 17:10:36 +00:00
2022-09-27 21:36:23 +00:00
try {
$utcDate = new \DateTime($row['datetaken'], new \DateTimeZone('UTC'));
$utcTs = $utcDate->getTimestamp();
2022-10-19 17:10:36 +00:00
} catch (\Throwable $e) {
}
2022-09-27 21:36:23 +00:00
2022-09-25 13:21:40 +00:00
return [
2022-10-19 17:15:14 +00:00
'fileid' => (int) $row['fileid'],
'dayid' => (int) $row['dayid'],
2022-09-27 21:36:23 +00:00
'datetaken' => $utcTs,
2022-09-25 13:21:40 +00:00
];
}
2022-10-19 17:10:36 +00:00
}