From 04728a1d607aa3e51b43ba87587853f2ee47ab12 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Thu, 13 Apr 2023 22:49:12 -0700 Subject: [PATCH] refactor: move CTE to trait Signed-off-by: Varun Patil --- lib/Db/TimelineQueryCTE.php | 79 ++++++++++++++++++++++++++++++++++++ lib/Db/TimelineQueryDays.php | 70 ++------------------------------ 2 files changed, 83 insertions(+), 66 deletions(-) create mode 100644 lib/Db/TimelineQueryCTE.php diff --git a/lib/Db/TimelineQueryCTE.php b/lib/Db/TimelineQueryCTE.php new file mode 100644 index 00000000..ba95c415 --- /dev/null +++ b/lib/Db/TimelineQueryCTE.php @@ -0,0 +1,79 @@ + '.archive'" : ''; + + return + "*PREFIX*cte_folders_all(fileid, name) AS ( + SELECT + f.fileid, + f.name + FROM + *PREFIX*filecache f + WHERE + f.fileid IN (:topFolderIds) + UNION ALL + SELECT + f.fileid, + f.name + FROM + *PREFIX*filecache f + INNER JOIN *PREFIX*cte_folders_all c + ON (f.parent = c.fileid + AND f.mimetype = (SELECT `id` FROM `*PREFIX*mimetypes` WHERE `mimetype` = 'httpd/unix-directory') + {$extraJoinOn} + ) + )"; + } + + /** CTE to get all folders recursively in the given top folders excluding archive */ + protected static function CTE_FOLDERS(): string + { + $cte = '*PREFIX*cte_folders AS ( + SELECT + fileid + FROM + *PREFIX*cte_folders_all + GROUP BY + fileid + )'; + + return self::bundleCTEs([self::CTE_FOLDERS_ALL(true), $cte]); + } + + /** CTE to get all archive folders recursively in the given top folders */ + protected static function CTE_FOLDERS_ARCHIVE(): string + { + $cte = '*PREFIX*cte_folders(fileid) AS ( + SELECT + cfa.fileid + FROM + *PREFIX*cte_folders_all cfa + WHERE + cfa.name = \'.archive\' + GROUP BY + cfa.fileid + UNION ALL + SELECT + f.fileid + FROM + *PREFIX*filecache f + INNER JOIN *PREFIX*cte_folders c + ON (f.parent = c.fileid) + )'; + + return self::bundleCTEs([self::CTE_FOLDERS_ALL(false), $cte]); + } + + protected static function bundleCTEs(array $ctes): string + { + return 'WITH RECURSIVE ' . implode(',', $ctes); + } +} diff --git a/lib/Db/TimelineQueryDays.php b/lib/Db/TimelineQueryDays.php index fc562709..30c6076a 100644 --- a/lib/Db/TimelineQueryDays.php +++ b/lib/Db/TimelineQueryDays.php @@ -8,72 +8,10 @@ use OCA\Memories\ClustersBackend; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; -const CTE_FOLDERS = // CTE to get all folders recursively in the given top folders excluding archive - 'WITH RECURSIVE *PREFIX*cte_folders_all(fileid) AS ( - SELECT - f.fileid - FROM - *PREFIX*filecache f - WHERE - f.fileid IN (:topFolderIds) - UNION ALL - SELECT - f.fileid - FROM - *PREFIX*filecache f - INNER JOIN *PREFIX*cte_folders_all c - ON (f.parent = c.fileid - AND f.mimetype = (SELECT `id` FROM `*PREFIX*mimetypes` WHERE `mimetype` = \'httpd/unix-directory\') - AND f.name <> \'.archive\' - ) - ), *PREFIX*cte_folders AS ( - SELECT - fileid - FROM - *PREFIX*cte_folders_all - GROUP BY - fileid - )'; - -const CTE_FOLDERS_ARCHIVE = // CTE to get all archive folders recursively in the given top folders - 'WITH RECURSIVE *PREFIX*cte_folders_all(fileid, name) AS ( - SELECT - f.fileid, - f.name - FROM - *PREFIX*filecache f - WHERE - f.fileid IN (:topFolderIds) - UNION ALL - SELECT - f.fileid, - f.name - FROM - *PREFIX*filecache f - INNER JOIN *PREFIX*cte_folders_all c - ON (f.parent = c.fileid - AND f.mimetype = (SELECT `id` FROM `*PREFIX*mimetypes` WHERE `mimetype` = \'httpd/unix-directory\') - ) - ), *PREFIX*cte_folders(fileid) AS ( - SELECT - cfa.fileid - FROM - *PREFIX*cte_folders_all cfa - WHERE - cfa.name = \'.archive\' - GROUP BY - cfa.fileid - UNION ALL - SELECT - f.fileid - FROM - *PREFIX*filecache f - INNER JOIN *PREFIX*cte_folders c - ON (f.parent = c.fileid) - )'; - trait TimelineQueryDays { + use TimelineQueryCTE; + protected IDBConnection $connection; /** @@ -187,8 +125,8 @@ trait TimelineQueryDays // Get SQL $CTE_SQL = \array_key_exists('cteFoldersArchive', $params) && $params['cteFoldersArchive'] - ? CTE_FOLDERS_ARCHIVE - : CTE_FOLDERS; + ? self::CTE_FOLDERS_ARCHIVE() + : self::CTE_FOLDERS(); // Add WITH clause if needed if (false !== strpos($sql, 'cte_folders')) {