From fb396fe4365ed0b2ed28973d360327f51574e07a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 16 Apr 2019 11:44:23 +0200 Subject: [PATCH] Use the querybuilder for the queries Signed-off-by: Joas Schilling --- lib/private/Repair/RemoveLinkShares.php | 83 +++++++++++++++---------- 1 file changed, 49 insertions(+), 34 deletions(-) diff --git a/lib/private/Repair/RemoveLinkShares.php b/lib/private/Repair/RemoveLinkShares.php index 084a65aa402..49850298d40 100644 --- a/lib/private/Repair/RemoveLinkShares.php +++ b/lib/private/Repair/RemoveLinkShares.php @@ -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(); } /** -- 2.39.5