memories/lib/Db/TimelineQuery.php

94 lines
2.5 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;
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.isvideo', 'm.video_duration', 'm.datetaken', 'm.dayid', 'm.w', 'm.h', 'm.liveid',
'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;
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 root(?TimelineRoot $override = null): TimelineRoot
{
if (null !== $override) {
$this->_root = $override;
}
if (null === $this->_root) {
$this->_root = new TimelineRoot();
$this->_root->populate();
}
if (!$this->_rootEmptyAllowed && $this->_root->isEmpty()) {
throw new \Exception('No valid root folder found (.nomedia?)');
}
return $this->_root;
}
public function allowEmptyRoot(bool $value = true)
{
$this->_rootEmptyAllowed = $value;
}
public function getBuilder()
{
return $this->connection->getQueryBuilder();
}
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)
{
$params = $query->getParameters();
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(',', $value);
} elseif (\is_bool($value)) {
$value = $value ? '1' : '0';
} elseif (null === $value) {
$value = 'NULL';
}
$value = $query->getConnection()->getDatabasePlatform()->quoteStringLiteral($value);
$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
}