memories/lib/Db/TimelineQuery.php

83 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;
use OCP\IRequest;
2022-08-20 02:53:21 +00:00
2022-10-19 17:10:36 +00:00
class TimelineQuery
{
2022-09-11 02:05:04 +00:00
use TimelineQueryDays;
2022-09-13 07:55:32 +00:00
use TimelineQueryFilters;
2022-11-07 04:48:10 +00:00
use TimelineQueryFolders;
2022-11-22 11:31:31 +00:00
use TimelineQueryLivePhoto;
2023-02-08 21:53:38 +00:00
use TimelineQueryMap;
use TimelineQueryNativeX;
2023-03-10 17:30:56 +00:00
use TimelineQuerySingleItem;
2022-09-11 02:05:04 +00:00
2023-03-10 17:30:56 +00:00
public const TIMELINE_SELECT = [
'm.datetaken', 'm.dayid',
'm.w', 'm.h', 'm.liveid',
'm.isvideo', 'm.video_duration',
'f.etag', 'f.name AS basename',
'mimetypes.mimetype',
2023-03-10 17:30:56 +00:00
];
2022-09-09 07:31:42 +00:00
protected IDBConnection $connection;
protected IRequest $request;
protected ?TimelineRoot $_root = null; // cache
protected bool $_rootEmptyAllowed = false;
2022-08-20 02:53:21 +00:00
public function __construct(IDBConnection $connection, IRequest $request)
2022-10-19 17:10:36 +00:00
{
2022-09-09 07:31:42 +00:00
$this->connection = $connection;
$this->request = $request;
2022-09-09 07:31:42 +00:00
}
2022-09-25 13:21:40 +00:00
public function allowEmptyRoot(bool $value = true): void
{
$this->_rootEmptyAllowed = $value;
}
public function getBuilder(): IQueryBuilder
{
return $this->connection->getQueryBuilder();
}
/**
* @return never
*/
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; // only for debugging, so this is okay
2022-10-27 19:54:51 +00:00
}
public static function replaceQueryParams(IQueryBuilder &$query, string $sql): string
2022-10-27 19:54:51 +00:00
{
$params = $query->getParameters();
$platform = $query->getConnection()->getDatabasePlatform();
2022-10-27 16:19:25 +00:00
foreach ($params as $key => $value) {
2023-02-05 20:39:46 +00:00
if (\is_array($value)) {
$value = implode(',', array_map(static fn ($v) => $platform->quoteStringLiteral($v), $value));
2023-02-05 20:39:46 +00:00
} elseif (\is_bool($value)) {
$value = $platform->quoteStringLiteral($value ? '1' : '0');
2023-02-05 20:39:46 +00:00
} elseif (null === $value) {
$value = $platform->quoteStringLiteral('NULL');
2023-02-05 20:39:46 +00:00
}
$sql = str_replace(':'.$key, $value, $sql);
2022-10-27 16:19:25 +00:00
}
2022-10-27 19:54:51 +00:00
return $sql;
2022-10-27 16:19:25 +00:00
}
2022-10-19 17:10:36 +00:00
}