From b4737747b6c3cee921872b34e49873dc7438b706 Mon Sep 17 00:00:00 2001 From: Git'Fellow <12234510+solracsf@users.noreply.github.com> Date: Wed, 16 Oct 2024 10:41:21 +0200 Subject: [PATCH] chore(db): Correctly apply query types --- .../files/lib/Command/DeleteOrphanedFiles.php | 6 +- apps/workflowengine/lib/Manager.php | 38 ++++++------ .../Repair/OldGroupMembershipShares.php | 4 +- lib/private/Repair/RepairInvalidShares.php | 27 ++++----- lib/private/SubAdmin.php | 58 ++++++------------- 5 files changed, 53 insertions(+), 80 deletions(-) diff --git a/apps/files/lib/Command/DeleteOrphanedFiles.php b/apps/files/lib/Command/DeleteOrphanedFiles.php index 4bbee0b45f4f2..845d0bea1df1a 100644 --- a/apps/files/lib/Command/DeleteOrphanedFiles.php +++ b/apps/files/lib/Command/DeleteOrphanedFiles.php @@ -147,11 +147,11 @@ private function cleanupOrphanedMounts(): int { $deletedInLastChunk = self::CHUNK_SIZE; while ($deletedInLastChunk === self::CHUNK_SIZE) { $deletedInLastChunk = 0; - $result = $query->execute(); - while ($row = $result->fetch()) { + $result = $query->executeQuery(); + while ($row = $result->fetchAssociative()) { $deletedInLastChunk++; $deletedEntries += $deleteQuery->setParameter('storageid', (int)$row['storage_id']) - ->execute(); + ->executeStatement(); } $result->closeCursor(); } diff --git a/apps/workflowengine/lib/Manager.php b/apps/workflowengine/lib/Manager.php index f03f84a33c01d..9a124a14fcb1d 100644 --- a/apps/workflowengine/lib/Manager.php +++ b/apps/workflowengine/lib/Manager.php @@ -99,9 +99,9 @@ public function getAllConfiguredEvents() { ->where($query->expr()->neq('events', $query->createNamedParameter('[]'), IQueryBuilder::PARAM_STR)) ->groupBy('class', 'entity', $query->expr()->castColumn('events', IQueryBuilder::PARAM_STR)); - $result = $query->execute(); + $result = $query->executeQuery(); $operations = []; - while ($row = $result->fetch()) { + while ($row = $result->fetchAssociative()) { $eventNames = \json_decode($row['events']); $operation = $row['class']; @@ -145,10 +145,10 @@ public function getAllConfiguredScopesForOperation(string $operationClass): arra ->where($query->expr()->eq('o.class', $query->createParameter('operationClass'))); $query->setParameters(['operationClass' => $operationClass]); - $result = $query->execute(); + $result = $query->executeQuery(); $scopesByOperation[$operationClass] = []; - while ($row = $result->fetch()) { + while ($row = $result->fetchAssociative()) { $scope = new ScopeContext($row['type'], $row['value']); if (!$operation->isAvailableForScope((int)$row['type'])) { @@ -180,10 +180,10 @@ public function getAllOperations(ScopeContext $scopeContext): array { } $query->setParameters(['scope' => $scopeContext->getScope(), 'scopeId' => $scopeContext->getScopeId()]); - $result = $query->execute(); + $result = $query->executeQuery(); $this->operations[$scopeContext->getHash()] = []; - while ($row = $result->fetch()) { + while ($row = $result->fetchAssociative()) { try { /** @var IOperation $operation */ $operation = $this->container->query($row['class']); @@ -221,8 +221,8 @@ protected function getOperation($id) { $query->select('*') ->from('flow_operations') ->where($query->expr()->eq('id', $query->createNamedParameter($id))); - $result = $query->execute(); - $row = $result->fetch(); + $result = $query->executeQuery(); + $row = $result->fetchAssociative(); $result->closeCursor(); if ($row) { @@ -250,7 +250,7 @@ protected function insertOperation( 'entity' => $query->createNamedParameter($entity), 'events' => $query->createNamedParameter(json_encode($events)) ]); - $query->execute(); + $query->executeStatement(); $this->cacheFactory->createDistributed('flow')->remove('events'); @@ -313,7 +313,7 @@ protected function canModify(int $id, ScopeContext $scopeContext):bool { } $qb->setParameters(['scope' => $scopeContext->getScope(), 'scopeId' => $scopeContext->getScopeId()]); - $result = $qb->execute(); + $result = $qb->executeQuery(); $operations = []; while (($opId = $result->fetchOne()) !== false) { @@ -365,7 +365,7 @@ public function updateOperation( ->set('entity', $query->createNamedParameter($entity)) ->set('events', $query->createNamedParameter(json_encode($events))) ->where($query->expr()->eq('id', $query->createNamedParameter($id))); - $query->execute(); + $query->executeStatement(); $this->connection->commit(); } catch (Exception $e) { $this->connection->rollBack(); @@ -393,12 +393,12 @@ public function deleteOperation($id, ScopeContext $scopeContext) { $this->connection->beginTransaction(); $result = (bool)$query->delete('flow_operations') ->where($query->expr()->eq('id', $query->createNamedParameter($id))) - ->execute(); + ->executeStatement(); if ($result) { $qb = $this->connection->getQueryBuilder(); $result &= (bool)$qb->delete('flow_operations_scope') ->where($qb->expr()->eq('operation_id', $qb->createNamedParameter($id))) - ->execute(); + ->executeStatement(); } $this->connection->commit(); } catch (Exception $e) { @@ -537,9 +537,9 @@ public function getChecks(array $checkIds) { $query->select('*') ->from('flow_checks') ->where($query->expr()->in('id', $query->createNamedParameter($checkIds, IQueryBuilder::PARAM_INT_ARRAY))); - $result = $query->execute(); + $result = $query->executeQuery(); - while ($row = $result->fetch()) { + while ($row = $result->fetchAssociative()) { $this->checks[(int)$row['id']] = $row; $checks[(int)$row['id']] = $row; } @@ -568,9 +568,9 @@ protected function addCheck($class, $operator, $value) { $query->select('id') ->from('flow_checks') ->where($query->expr()->eq('hash', $query->createNamedParameter($hash))); - $result = $query->execute(); + $result = $query->executeQuery(); - if ($row = $result->fetch()) { + if ($row = $result->fetchAssociative()) { $result->closeCursor(); return (int)$row['id']; } @@ -583,7 +583,7 @@ protected function addCheck($class, $operator, $value) { 'value' => $query->createNamedParameter($value), 'hash' => $query->createNamedParameter($hash), ]); - $query->execute(); + $query->executeStatement(); return $query->getLastInsertId(); } @@ -597,7 +597,7 @@ protected function addScope(int $operationId, ScopeContext $scope): void { 'type' => $query->createNamedParameter($scope->getScope()), 'value' => $query->createNamedParameter($scope->getScopeId()), ]); - $insertQuery->execute(); + $insertQuery->executeStatement(); } public function formatOperation(array $operation): array { diff --git a/lib/private/Repair/OldGroupMembershipShares.php b/lib/private/Repair/OldGroupMembershipShares.php index 027f179596c4a..dae2b7f597210 100644 --- a/lib/private/Repair/OldGroupMembershipShares.php +++ b/lib/private/Repair/OldGroupMembershipShares.php @@ -66,8 +66,8 @@ public function run(IOutput $output) { $deleteQuery->delete('share') ->where($query->expr()->eq('id', $deleteQuery->createParameter('share'))); - $result = $query->execute(); - while ($row = $result->fetch()) { + $result = $query->executeQuery(); + while ($row = $result->fetchAssociative()) { if (!$this->isMember($row['group'], $row['user'])) { $deletedEntries += $deleteQuery->setParameter('share', (int)$row['id']) ->execute(); diff --git a/lib/private/Repair/RepairInvalidShares.php b/lib/private/Repair/RepairInvalidShares.php index 71e6359da5b54..1f4d6771a6370 100644 --- a/lib/private/Repair/RepairInvalidShares.php +++ b/lib/private/Repair/RepairInvalidShares.php @@ -9,6 +9,8 @@ use OCP\Migration\IOutput; use OCP\Migration\IRepairStep; +use OCP\IConfig; +use OCP\IDBConnection; /** * Repairs shares with invalid data @@ -16,19 +18,10 @@ class RepairInvalidShares implements IRepairStep { public const CHUNK_SIZE = 200; - /** @var \OCP\IConfig */ - protected $config; - - /** @var \OCP\IDBConnection */ - protected $connection; - - /** - * @param \OCP\IConfig $config - * @param \OCP\IDBConnection $connection - */ - public function __construct($config, $connection) { - $this->connection = $connection; - $this->config = $config; + public function __construct( + protected IConfig $config; + protected IDBConnection $connection, + ) { } public function getName() { @@ -49,7 +42,7 @@ private function adjustFileSharePermissions(IOutput $out) { ->where($builder->expr()->eq('item_type', $builder->expr()->literal('file'))) ->andWhere($builder->expr()->neq('permissions', $permsFunc)); - $updatedEntries = $builder->execute(); + $updatedEntries = $builder->executeStatement(); if ($updatedEntries > 0) { $out->info('Fixed file share permissions for ' . $updatedEntries . ' shares'); } @@ -77,11 +70,11 @@ private function removeSharesNonExistingParent(IOutput $out) { $deletedInLastChunk = self::CHUNK_SIZE; while ($deletedInLastChunk === self::CHUNK_SIZE) { $deletedInLastChunk = 0; - $result = $query->execute(); - while ($row = $result->fetch()) { + $result = $query->executeQuery(); + while ($row = $result->fetchAssociative()) { $deletedInLastChunk++; $deletedEntries += $deleteQuery->setParameter('parent', (int)$row['parent']) - ->execute(); + ->executeStatement(); } $result->closeCursor(); } diff --git a/lib/private/SubAdmin.php b/lib/private/SubAdmin.php index 335e901a321b6..d75f3d8325f0c 100644 --- a/lib/private/SubAdmin.php +++ b/lib/private/SubAdmin.php @@ -19,32 +19,12 @@ use OCP\IUserManager; class SubAdmin extends PublicEmitter implements ISubAdmin { - /** @var IUserManager */ - private $userManager; - - /** @var IGroupManager */ - private $groupManager; - - /** @var IDBConnection */ - private $dbConn; - - /** @var IEventDispatcher */ - private $eventDispatcher; - - /** - * @param IUserManager $userManager - * @param IGroupManager $groupManager - * @param IDBConnection $dbConn - */ - public function __construct(IUserManager $userManager, - IGroupManager $groupManager, - IDBConnection $dbConn, - IEventDispatcher $eventDispatcher) { - $this->userManager = $userManager; - $this->groupManager = $groupManager; - $this->dbConn = $dbConn; - $this->eventDispatcher = $eventDispatcher; - + public function __construct( + private IUserManager $userManager, + private IGroupManager $groupManager, + private IDBConnection $dbConn, + private IEventDispatcher $eventDispatcher + ) { $this->userManager->listen('\OC\User', 'postDelete', function ($user) { $this->post_deleteUser($user); }); @@ -66,7 +46,7 @@ public function createSubAdmin(IUser $user, IGroup $group): void { 'gid' => $qb->createNamedParameter($group->getGID()), 'uid' => $qb->createNamedParameter($user->getUID()) ]) - ->execute(); + ->executeStatement(); /** @deprecated 21.0.0 - use type SubAdminAddedEvent instead */ $this->emit('\OC\SubAdmin', 'postCreateSubAdmin', [$user, $group]); @@ -85,7 +65,7 @@ public function deleteSubAdmin(IUser $user, IGroup $group): void { $qb->delete('group_admin') ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID()))) ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) - ->execute(); + ->executeStatement(); /** @deprecated 21.0.0 - use type SubAdminRemovedEvent instead */ $this->emit('\OC\SubAdmin', 'postDeleteSubAdmin', [$user, $group]); @@ -123,10 +103,10 @@ public function getSubAdminsGroupIds(IUser $user): array { $result = $qb->select('gid') ->from('group_admin') ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) - ->execute(); + ->executeQuery(); $groups = []; - while ($row = $result->fetch()) { + while ($row = $result->fetchAssociative()) { $groups[] = $row['gid']; } $result->closeCursor(); @@ -156,10 +136,10 @@ public function getGroupsSubAdmins(IGroup $group): array { $result = $qb->select('uid') ->from('group_admin') ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID()))) - ->execute(); + ->executeQuery(); $users = []; - while ($row = $result->fetch()) { + while ($row = $result->fetchAssociative()) { $user = $this->userManager->get($row['uid']); if (!is_null($user)) { $users[] = $user; @@ -179,10 +159,10 @@ public function getAllSubAdmins(): array { $result = $qb->select('*') ->from('group_admin') - ->execute(); + ->executeQuery(); $subadmins = []; - while ($row = $result->fetch()) { + while ($row = $result->fetchAssociative()) { $user = $this->userManager->get($row['uid']); $group = $this->groupManager->get($row['gid']); if (!is_null($user) && !is_null($group)) { @@ -213,9 +193,9 @@ public function isSubAdminOfGroup(IUser $user, IGroup $group): bool { ->from('group_admin') ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID()))) ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) - ->execute(); + ->executeQuery(); - $fetch = $result->fetch(); + $fetch = $result->fetchAssociative(); $result->closeCursor(); $result = !empty($fetch) ? true : false; @@ -244,7 +224,7 @@ public function isSubAdmin(IUser $user): bool { ->from('group_admin') ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) ->setMaxResults(1) - ->execute(); + ->executeQuery(); $isSubAdmin = $result->fetch(); $result->closeCursor(); @@ -284,7 +264,7 @@ private function post_deleteUser(IUser $user) { $qb->delete('group_admin') ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) - ->execute(); + ->executeStatement(); } /** @@ -296,6 +276,6 @@ private function post_deleteGroup(IGroup $group) { $qb->delete('group_admin') ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID()))) - ->execute(); + ->executeStatement(); } }