Skip to content

Commit 900d937

Browse files
authored
Merge pull request #58891 from nextcloud/executeUpdateRemove
refactor(dbal): move from deprecated to modern calls
2 parents dfa09a0 + 149f9a1 commit 900d937

71 files changed

Lines changed: 364 additions & 405 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apps/dav/lib/CalDAV/CalDavBackend.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,8 +1778,12 @@ public function deleteCalendarObject($calendarId, $objectUri, $calendarType = se
17781778
}
17791779

17801780
if ($forceDeletePermanently || $this->config->getAppValue(Application::APP_ID, RetentionService::RETENTION_CONFIG_KEY) === '0') {
1781-
$stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ? AND `calendartype` = ?');
1782-
$stmt->execute([$calendarId, $objectUri, $calendarType]);
1781+
$qb = $this->db->getQueryBuilder();
1782+
$qb->delete('calendarobjects')
1783+
->where($qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId)))
1784+
->andWhere($qb->expr()->eq('uri', $qb->createNamedParameter($objectUri)))
1785+
->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType)))
1786+
->executeStatement();
17831787

17841788
$this->purgeProperties($calendarId, $data['id']);
17851789

apps/files/tests/Command/DeleteOrphanedFilesTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ public function testClearFiles(): void {
104104
$this->assertCount(1, $this->getFile($fileInfo->getId()), 'Asserts that file is still available');
105105
$this->assertEquals(1, $this->getMountsCount($numericStorageId), 'Asserts that mount is still available');
106106

107-
$deletedRows = $this->connection->executeUpdate('DELETE FROM `*PREFIX*storages` WHERE `id` = ?', [$storageId]);
107+
$qb = $this->connection->getQueryBuilder();
108+
$deletedRows = $qb->delete('storages')
109+
->where($qb->expr()->eq('id', $qb->createNamedParameter($storageId)))
110+
->executeStatement();
108111
$this->assertNotNull($deletedRows, 'Asserts that storage got deleted');
109112
$this->assertSame(1, $deletedRows, 'Asserts that storage got deleted');
110113

apps/files_sharing/tests/DeleteOrphanedSharesJobTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected function setUp(): void {
7575

7676
$this->connection = Server::get(IDBConnection::class);
7777
// clear occasional leftover shares from other tests
78-
$this->connection->executeUpdate('DELETE FROM `*PREFIX*share`');
78+
$this->connection->getQueryBuilder()->delete('share')->executeStatement();
7979

8080
$this->user1 = $this->getUniqueID('user1_');
8181
$this->user2 = $this->getUniqueID('user2_');
@@ -89,7 +89,7 @@ protected function setUp(): void {
8989
}
9090

9191
protected function tearDown(): void {
92-
$this->connection->executeUpdate('DELETE FROM `*PREFIX*share`');
92+
$this->connection->getQueryBuilder()->delete('share')->executeStatement();
9393

9494
$userManager = Server::get(IUserManager::class);
9595
$user1 = $userManager->get($this->user1);
@@ -108,7 +108,10 @@ protected function tearDown(): void {
108108

109109
private function getShares() {
110110
$shares = [];
111-
$result = $this->connection->executeQuery('SELECT * FROM `*PREFIX*share`');
111+
$result = $this->connection->getQueryBuilder()
112+
->select('*')
113+
->from('share')
114+
->executeQuery();
112115
while ($row = $result->fetchAssociative()) {
113116
$shares[] = $row;
114117
}

apps/files_sharing/tests/SharesReminderJobTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected function setUp(): void {
5555
$this->mailer = $this->createMock(IMailer::class);
5656

5757
// Clear occasional leftover shares from other tests
58-
$this->db->executeUpdate('DELETE FROM `*PREFIX*share`');
58+
$this->db->getQueryBuilder()->delete('share')->executeStatement();
5959

6060
$this->user1 = $this->getUniqueID('user1_');
6161
$this->user2 = $this->getUniqueID('user2_');
@@ -82,7 +82,7 @@ protected function setUp(): void {
8282
}
8383

8484
protected function tearDown(): void {
85-
$this->db->executeUpdate('DELETE FROM `*PREFIX*share`');
85+
$this->db->getQueryBuilder()->delete('share')->executeStatement();
8686

8787
$this->logout();
8888

apps/files_trashbin/tests/TrashbinTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ protected function tearDown(): void {
144144

145145
// clear trash table
146146
$connection = Server::get(IDBConnection::class);
147-
$connection->executeUpdate('DELETE FROM `*PREFIX*files_trash`');
147+
$connection->getQueryBuilder()->delete('files_trash')->executeStatement();
148148

149149
parent::tearDown();
150150
}

apps/user_ldap/lib/Mapping/AbstractMapping.php

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace OCA\User_LDAP\Mapping;
1010

1111
use Doctrine\DBAL\Exception;
12-
use OCP\DB\IPreparedStatement;
1312
use OCP\DB\QueryBuilder\IQueryBuilder;
1413
use OCP\IAppConfig;
1514
use OCP\ICache;
@@ -155,24 +154,6 @@ protected function getXbyY($fetchCol, $compareCol, $search) {
155154
}
156155
}
157156

158-
/**
159-
* Performs a DELETE or UPDATE query to the database.
160-
*
161-
* @param IPreparedStatement $statement
162-
* @param array $parameters
163-
* @return bool true if at least one row was modified, false otherwise
164-
*/
165-
protected function modify(IPreparedStatement $statement, $parameters) {
166-
try {
167-
$result = $statement->execute($parameters);
168-
$updated = $result->rowCount() > 0;
169-
$result->closeCursor();
170-
return $updated;
171-
} catch (Exception $e) {
172-
return false;
173-
}
174-
}
175-
176157
/**
177158
* Gets the LDAP DN based on the provided name.
178159
*/
@@ -200,13 +181,16 @@ public function getDNByName(string $name): string|false {
200181
*/
201182
public function setDNbyUUID($fdn, $uuid) {
202183
$oldDn = $this->getDnByUUID($uuid);
203-
$statement = $this->dbc->prepare('
204-
UPDATE `' . $this->getTableName() . '`
205-
SET `ldap_dn_hash` = ?, `ldap_dn` = ?
206-
WHERE `directory_uuid` = ?
207-
');
208-
209-
$r = $this->modify($statement, [$this->getDNHash($fdn), $fdn, $uuid]);
184+
$qb = $this->dbc->getQueryBuilder();
185+
try {
186+
$r = $qb->update($this->getTableName(false))
187+
->set('ldap_dn_hash', $qb->createNamedParameter($this->getDNHash($fdn)))
188+
->set('ldap_dn', $qb->createNamedParameter($fdn))
189+
->where($qb->expr()->eq('directory_uuid', $qb->createNamedParameter($uuid)))
190+
->executeStatement() > 0;
191+
} catch (Exception $e) {
192+
$r = false;
193+
}
210194
if ($r) {
211195
if (is_string($oldDn) && isset($this->cache[$oldDn])) {
212196
$userId = $this->cache[$oldDn];
@@ -232,15 +216,17 @@ public function setDNbyUUID($fdn, $uuid) {
232216
* @return bool
233217
*/
234218
public function setUUIDbyDN($uuid, $fdn): bool {
235-
$statement = $this->dbc->prepare('
236-
UPDATE `' . $this->getTableName() . '`
237-
SET `directory_uuid` = ?
238-
WHERE `ldap_dn_hash` = ?
239-
');
240-
241219
unset($this->cache[$fdn]);
242220

243-
return $this->modify($statement, [$uuid, $this->getDNHash($fdn)]);
221+
$qb = $this->dbc->getQueryBuilder();
222+
try {
223+
return $qb->update($this->getTableName(false))
224+
->set('directory_uuid', $qb->createNamedParameter($uuid))
225+
->where($qb->expr()->eq('ldap_dn_hash', $qb->createNamedParameter($this->getDNHash($fdn))))
226+
->executeStatement() > 0;
227+
} catch (Exception $e) {
228+
return false;
229+
}
244230
}
245231

246232
/**
@@ -340,21 +326,22 @@ public function getListOfIdsByDn(array $fdns): array {
340326
* @return string[]
341327
*/
342328
public function getNamesBySearch(string $search, string $prefixMatch = '', string $postfixMatch = ''): array {
343-
$statement = $this->dbc->prepare('
344-
SELECT `owncloud_name`
345-
FROM `' . $this->getTableName() . '`
346-
WHERE `owncloud_name` LIKE ?
347-
');
348-
329+
$qb = $this->dbc->getQueryBuilder();
349330
try {
350-
$res = $statement->execute([$prefixMatch . $this->dbc->escapeLikeParameter($search) . $postfixMatch]);
331+
$res = $qb->select('owncloud_name')
332+
->from($this->getTableName(false))
333+
->where($qb->expr()->like('owncloud_name', $qb->createNamedParameter(
334+
$prefixMatch . $this->dbc->escapeLikeParameter($search) . $postfixMatch
335+
)))
336+
->executeQuery();
351337
} catch (Exception $e) {
352338
return [];
353339
}
354340
$names = [];
355341
while ($row = $res->fetchAssociative()) {
356342
$names[] = $row['owncloud_name'];
357343
}
344+
$res->closeCursor();
358345
return $names;
359346
}
360347

@@ -450,17 +437,20 @@ public function map($fdn, $name, $uuid) {
450437
* @return bool
451438
*/
452439
public function unmap($name) {
453-
$statement = $this->dbc->prepare('
454-
DELETE FROM `' . $this->getTableName() . '`
455-
WHERE `owncloud_name` = ?');
456-
457440
$dn = array_search($name, $this->cache, true);
458441
if ($dn !== false) {
459442
unset($this->cache[$dn]);
460443
}
461444
$this->localNameToDnCache?->remove($name);
462445

463-
return $this->modify($statement, [$name]);
446+
$qb = $this->dbc->getQueryBuilder();
447+
try {
448+
return $qb->delete($this->getTableName(false))
449+
->where($qb->expr()->eq('owncloud_name', $qb->createNamedParameter($name)))
450+
->executeStatement() > 0;
451+
} catch (Exception $e) {
452+
return false;
453+
}
464454
}
465455

466456
/**

lib/private/Accounts/AccountManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ protected function getUser(IUser $user, bool $insertIfNotExists = true): array {
196196
->where($query->expr()->eq('uid', $query->createParameter('uid')))
197197
->setParameter('uid', $uid);
198198
$result = $query->executeQuery();
199-
$accountData = $result->fetchAll();
199+
$accountData = $result->fetchAllAssociative();
200200
$result->closeCursor();
201201

202202
if (empty($accountData)) {
@@ -233,7 +233,7 @@ public function searchUsers(string $property, array $values): array {
233233
$query->setParameter('values', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
234234
$result = $query->executeQuery();
235235

236-
while ($row = $result->fetch()) {
236+
while ($row = $result->fetchAssociative()) {
237237
$matches[$row['uid']] = $row['value'];
238238
}
239239
$result->closeCursor();

lib/private/AppConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,7 @@ private function loadConfig(?string $app = null, bool $lazy = false): void {
14271427
return;
14281428
}
14291429

1430-
$rows = $result->fetchAll();
1430+
$rows = $result->fetchAllAssociative();
14311431
foreach ($rows as $row) {
14321432
// most of the time, 'lazy' is not in the select because its value is already known
14331433
if ($this->migrationCompleted && $lazy && ((int)$row['lazy']) === 1) {

lib/private/Authentication/Token/PublicKeyTokenMapper.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function getToken(string $token): PublicKeyToken {
7575
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT)))
7676
->executeQuery();
7777

78-
$data = $result->fetch();
78+
$data = $result->fetchAssociative();
7979
$result->closeCursor();
8080
if ($data === false) {
8181
throw new DoesNotExistException('token does not exist');
@@ -97,7 +97,7 @@ public function getTokenById(int $id): PublicKeyToken {
9797
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT)))
9898
->executeQuery();
9999

100-
$data = $result->fetch();
100+
$data = $result->fetchAssociative();
101101
$result->closeCursor();
102102
if ($data === false) {
103103
throw new DoesNotExistException('token does not exist');
@@ -123,7 +123,7 @@ public function getTokenByUser(string $uid): array {
123123
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT)))
124124
->setMaxResults(1000);
125125
$result = $qb->executeQuery();
126-
$data = $result->fetchAll();
126+
$data = $result->fetchAllAssociative();
127127
$result->closeCursor();
128128

129129
$entities = array_map(function ($row) {
@@ -178,7 +178,7 @@ public function hasExpiredTokens(string $uid): bool {
178178
->setMaxResults(1);
179179

180180
$cursor = $qb->executeQuery();
181-
$data = $cursor->fetchAll();
181+
$data = $cursor->fetchAllAssociative();
182182
$cursor->closeCursor();
183183

184184
return count($data) === 1;
@@ -242,7 +242,7 @@ public function getFirstTokenForUser(string $userId): ?PublicKeyToken {
242242
->orderBy('id');
243243
$result = $qb->executeQuery();
244244

245-
$data = $result->fetch();
245+
$data = $result->fetchAssociative();
246246
$result->closeCursor();
247247
if ($data === false) {
248248
return null;

lib/private/Authentication/TwoFactorAuth/Db/ProviderUserAssignmentDao.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function getState(string $uid): array {
3838
->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
3939
$result = $query->executeQuery();
4040
$providers = [];
41-
foreach ($result->fetchAll() as $row) {
41+
foreach ($result->fetchAllAssociative() as $row) {
4242
$providers[(string)$row['provider_id']] = (int)$row['enabled'] === 1;
4343
}
4444
$result->closeCursor();
@@ -81,7 +81,7 @@ public function deleteByUser(string $uid): array {
8181
->from(self::TABLE_NAME)
8282
->where($qb1->expr()->eq('uid', $qb1->createNamedParameter($uid)));
8383
$selectResult = $selectQuery->executeQuery();
84-
$rows = $selectResult->fetchAll();
84+
$rows = $selectResult->fetchAllAssociative();
8585
$selectResult->closeCursor();
8686

8787
$qb2 = $this->conn->getQueryBuilder();

0 commit comments

Comments
 (0)