Skip to content

Commit c80ae66

Browse files
solracsfCarlSchwan
authored andcommitted
refactor(dbal): move to modern calls
Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent 0302d60 commit c80ae66

69 files changed

Lines changed: 360 additions & 401 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
@@ -1761,8 +1761,12 @@ public function deleteCalendarObject($calendarId, $objectUri, $calendarType = se
17611761
}
17621762

17631763
if ($forceDeletePermanently || $this->config->getAppValue(Application::APP_ID, RetentionService::RETENTION_CONFIG_KEY) === '0') {
1764-
$stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ? AND `calendartype` = ?');
1765-
$stmt->execute([$calendarId, $objectUri, $calendarType]);
1764+
$qb = $this->db->getQueryBuilder();
1765+
$qb->delete('calendarobjects')
1766+
->where($qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId)))
1767+
->andWhere($qb->expr()->eq('uri', $qb->createNamedParameter($objectUri)))
1768+
->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType)))
1769+
->executeStatement();
17661770

17671771
$this->purgeProperties($calendarId, $data['id']);
17681772

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
@@ -74,7 +74,7 @@ protected function setUp(): void {
7474

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

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

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

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

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

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
@@ -143,7 +143,7 @@ protected function tearDown(): void {
143143

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

148148
parent::tearDown();
149149
}

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
@@ -1420,7 +1420,7 @@ private function loadConfig(?string $app = null, bool $lazy = false): void {
14201420
return;
14211421
}
14221422

1423-
$rows = $result->fetchAll();
1423+
$rows = $result->fetchAllAssociative();
14241424
foreach ($rows as $row) {
14251425
// most of the time, 'lazy' is not in the select because its value is already known
14261426
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
@@ -76,7 +76,7 @@ public function getToken(string $token): PublicKeyToken {
7676
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT)))
7777
->executeQuery();
7878

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

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

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

181181
$cursor = $qb->executeQuery();
182-
$data = $cursor->fetchAll();
182+
$data = $cursor->fetchAllAssociative();
183183
$cursor->closeCursor();
184184

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

246-
$data = $result->fetch();
246+
$data = $result->fetchAssociative();
247247
$result->closeCursor();
248248
if ($data === false) {
249249
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)