]> source.dussan.org Git - nextcloud-server.git/commitdiff
Use the querybuilder for the queries
authorJoas Schilling <coding@schilljs.com>
Tue, 16 Apr 2019 09:44:23 +0000 (11:44 +0200)
committerRoeland Jago Douma <roeland@famdouma.nl>
Tue, 16 Apr 2019 13:42:21 +0000 (15:42 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/Repair/RemoveLinkShares.php

index 084a65aa402cb54e65abce791883071d29e33c09..49850298d4057c68102a50d80cc20390e642aaa8 100644 (file)
@@ -26,6 +26,7 @@ namespace OC\Repair;
 
 use Doctrine\DBAL\Driver\Statement;
 use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\DB\QueryBuilder\IQueryBuilder;
 use OCP\IConfig;
 use OCP\IDBConnection;
 use OCP\IGroupManager;
@@ -98,27 +99,35 @@ class RemoveLinkShares implements IRepairStep {
         * @return int
         */
        private function getTotal(): int {
-               $sql = 'SELECT COUNT(*) AS `total`
-               FROM `*PREFIX*share`
-               WHERE `id` IN (
-                       SELECT `s1`.`id`
-                       FROM (
-                               SELECT *
-                               FROM `*PREFIX*share`
-                               WHERE `parent` IS NOT NULL
-                               AND `share_type` = 3
-                       ) AS s1
-                       JOIN `*PREFIX*share` AS s2
-                       ON `s1`.`parent` = `s2`.`id`
-                       WHERE (`s2`.`share_type` = 1 OR `s2`.`share_type` = 2)
-                       AND `s1`.`item_source` = `s2`.`item_source`
-               )';
-               $cursor = $this->connection->executeQuery($sql);
-               $data = $cursor->fetchAll();
-               $total = (int)$data[0]['total'];
-               $cursor->closeCursor();
-
-               return $total;
+               $subSubQuery = $this->connection->getQueryBuilder();
+               $subSubQuery->select('*')
+                       ->from('share')
+                       ->where($subSubQuery->expr()->isNotNull('parent'))
+                       ->andWhere($subSubQuery->expr()->eq('share_type', $subSubQuery->expr()->literal(3, IQueryBuilder::PARAM_INT)));
+
+               $subQuery = $this->connection->getQueryBuilder();
+               $subQuery->select('s1.id')
+                       ->from($subQuery->createFunction('(' . $subSubQuery->getSQL() . ')'), 's1')
+                       ->join(
+                               's1', 'share', 's2',
+                               $subQuery->expr()->eq('s1.parent', 's2.id')
+                       )
+                       ->where($subQuery->expr()->orX(
+                               $subQuery->expr()->eq('s2.share_type', $subQuery->expr()->literal(1, IQueryBuilder::PARAM_INT)),
+                               $subQuery->expr()->eq('s2.share_type', $subQuery->expr()->literal(2, IQueryBuilder::PARAM_INT))
+                       ))
+                       ->andWhere($subQuery->expr()->eq('s1.item_source', 's2.item_source'));
+
+               $query = $this->connection->getQueryBuilder();
+               $query->select($query->func()->count('*', 'total'))
+                       ->from('share')
+                       ->where($query->expr()->in('id', $query->createFunction('(' . $subQuery->getSQL() . ')')));
+
+               $result = $query->execute();
+               $data = $result->fetch();
+               $result->closeCursor();
+
+               return (int) $data['total'];
        }
 
        /**
@@ -127,19 +136,25 @@ class RemoveLinkShares implements IRepairStep {
         * @return \Doctrine\DBAL\Driver\Statement
         */
        private function getShares(): Statement {
-               $sql = 'SELECT `s1`.`id`, `s1`.`uid_owner`, `s1`.`uid_initiator`
-                       FROM (
-                               SELECT *
-                               FROM `*PREFIX*share`
-                               WHERE `parent` IS NOT NULL
-                               AND `share_type` = 3
-                       ) AS s1
-                       JOIN `*PREFIX*share` AS s2
-                       ON `s1`.`parent` = `s2`.`id`
-                       WHERE (`s2`.`share_type` = 1 OR `s2`.`share_type` = 2)
-                       AND `s1`.`item_source` = `s2`.`item_source`';
-               $cursor = $this->connection->executeQuery($sql);
-               return $cursor;
+               $subQuery = $this->connection->getQueryBuilder();
+               $subQuery->select('*')
+                       ->from('share')
+                       ->where($subQuery->expr()->isNotNull('parent'))
+                       ->andWhere($subQuery->expr()->eq('share_type', $subQuery->expr()->literal(3, IQueryBuilder::PARAM_INT)));
+
+               $query = $this->connection->getQueryBuilder();
+               $query->select('s1.id', 's1.uid_owner', 's1.uid_initiator')
+                       ->from($query->createFunction('(' . $subQuery->getSQL() . ')'), 's1')
+                       ->join(
+                               's1', 'share', 's2',
+                               $query->expr()->eq('s1.parent', 's2.id')
+                       )
+                       ->where($query->expr()->orX(
+                               $query->expr()->eq('s2.share_type', $query->expr()->literal(1, IQueryBuilder::PARAM_INT)),
+                               $query->expr()->eq('s2.share_type', $query->expr()->literal(2, IQueryBuilder::PARAM_INT))
+                       ))
+                       ->andWhere($query->expr()->eq('s1.item_source', 's2.item_source'));
+               return $query->execute();
        }
 
        /**