Skip to content

Commit 21416b8

Browse files
committed
refactor(dbal): move to modern calls
Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>
1 parent 3904da9 commit 21416b8

File tree

72 files changed

+406
-427
lines changed

Some content is hidden

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

72 files changed

+406
-427
lines changed

apps/dav/lib/CalDAV/CalDavBackend.php

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

15721572
if ($forceDeletePermanently || $this->config->getAppValue(Application::APP_ID, RetentionService::RETENTION_CONFIG_KEY) === '0') {
1573-
$stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ? AND `calendartype` = ?');
1574-
$stmt->execute([$calendarId, $objectUri, $calendarType]);
1573+
$qb = $this->db->getQueryBuilder();
1574+
$qb->delete('calendarobjects')
1575+
->where($qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId)))
1576+
->andWhere($qb->expr()->eq('uri', $qb->createNamedParameter($objectUri)))
1577+
->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType)))
1578+
->executeStatement();
15751579

15761580
$this->purgeProperties($calendarId, $data['id']);
15771581

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->assertEquals(1, $this->getMountsCount($numericStorageId), 'Asserts that mount is still available');
105105

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

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

7575
$this->user1 = $this->getUniqueID('user1_');
7676
$this->user2 = $this->getUniqueID('user2_');
@@ -85,7 +85,7 @@ protected function setUp(): void {
8585
}
8686

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

9090
$userManager = Server::get(IUserManager::class);
9191
$user1 = $userManager->get($this->user1);
@@ -104,7 +104,10 @@ protected function tearDown(): void {
104104

105105
private function getShares() {
106106
$shares = [];
107-
$result = $this->connection->executeQuery('SELECT * FROM `*PREFIX*share`');
107+
$result = $this->connection->getQueryBuilder()
108+
->select('*')
109+
->from('share')
110+
->executeQuery();
108111
while ($row = $result->fetchAssociative()) {
109112
$shares[] = $row;
110113
}

apps/files_sharing/tests/SharesReminderJobTest.php

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

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

5656
$this->user1 = $this->getUniqueID('user1_');
5757
$this->user2 = $this->getUniqueID('user2_');
@@ -78,7 +78,7 @@ protected function setUp(): void {
7878
}
7979

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

8383
$userManager = Server::get(IUserManager::class);
8484
$user1 = $userManager->get($this->user1);

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 & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace OCA\User_LDAP\Mapping;
99

1010
use Doctrine\DBAL\Exception;
11-
use OCP\DB\IPreparedStatement;
1211
use OCP\DB\QueryBuilder\IQueryBuilder;
1312
use OCP\IAppConfig;
1413
use OCP\ICache;
@@ -154,23 +153,6 @@ protected function getXbyY($fetchCol, $compareCol, $search) {
154153
}
155154
}
156155

157-
/**
158-
* Performs a DELETE or UPDATE query to the database.
159-
*
160-
* @param IPreparedStatement $statement
161-
* @param array $parameters
162-
* @return bool true if at least one row was modified, false otherwise
163-
*/
164-
protected function modify(IPreparedStatement $statement, $parameters) {
165-
try {
166-
$result = $statement->execute($parameters);
167-
$updated = $result->rowCount() > 0;
168-
$result->closeCursor();
169-
return $updated;
170-
} catch (Exception $e) {
171-
return false;
172-
}
173-
}
174156

175157
/**
176158
* Gets the LDAP DN based on the provided name.
@@ -199,13 +181,16 @@ public function getDNByName(string $name): string|false {
199181
*/
200182
public function setDNbyUUID($fdn, $uuid) {
201183
$oldDn = $this->getDnByUUID($uuid);
202-
$statement = $this->dbc->prepare('
203-
UPDATE `' . $this->getTableName() . '`
204-
SET `ldap_dn_hash` = ?, `ldap_dn` = ?
205-
WHERE `directory_uuid` = ?
206-
');
207-
208-
$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+
}
209194
if ($r) {
210195
if (is_string($oldDn) && isset($this->cache[$oldDn])) {
211196
$userId = $this->cache[$oldDn];
@@ -231,15 +216,17 @@ public function setDNbyUUID($fdn, $uuid) {
231216
* @return bool
232217
*/
233218
public function setUUIDbyDN($uuid, $fdn): bool {
234-
$statement = $this->dbc->prepare('
235-
UPDATE `' . $this->getTableName() . '`
236-
SET `directory_uuid` = ?
237-
WHERE `ldap_dn_hash` = ?
238-
');
239-
240219
unset($this->cache[$fdn]);
241220

242-
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+
}
243230
}
244231

245232
/**
@@ -333,21 +320,22 @@ public function getListOfIdsByDn(array $fdns): array {
333320
* @return string[]
334321
*/
335322
public function getNamesBySearch(string $search, string $prefixMatch = '', string $postfixMatch = ''): array {
336-
$statement = $this->dbc->prepare('
337-
SELECT `owncloud_name`
338-
FROM `' . $this->getTableName() . '`
339-
WHERE `owncloud_name` LIKE ?
340-
');
341-
323+
$qb = $this->dbc->getQueryBuilder();
342324
try {
343-
$res = $statement->execute([$prefixMatch . $this->dbc->escapeLikeParameter($search) . $postfixMatch]);
325+
$res = $qb->select('owncloud_name')
326+
->from($this->getTableName(false))
327+
->where($qb->expr()->like('owncloud_name', $qb->createNamedParameter(
328+
$prefixMatch . $this->dbc->escapeLikeParameter($search) . $postfixMatch
329+
)))
330+
->executeQuery();
344331
} catch (Exception $e) {
345332
return [];
346333
}
347334
$names = [];
348335
while ($row = $res->fetchAssociative()) {
349336
$names[] = $row['owncloud_name'];
350337
}
338+
$res->closeCursor();
351339
return $names;
352340
}
353341

@@ -443,17 +431,20 @@ public function map($fdn, $name, $uuid) {
443431
* @return bool
444432
*/
445433
public function unmap($name) {
446-
$statement = $this->dbc->prepare('
447-
DELETE FROM `' . $this->getTableName() . '`
448-
WHERE `owncloud_name` = ?');
449-
450434
$dn = array_search($name, $this->cache, true);
451435
if ($dn !== false) {
452436
unset($this->cache[$dn]);
453437
}
454438
$this->localNameToDnCache?->remove($name);
455439

456-
return $this->modify($statement, [$name]);
440+
$qb = $this->dbc->getQueryBuilder();
441+
try {
442+
return $qb->delete($this->getTableName(false))
443+
->where($qb->expr()->eq('owncloud_name', $qb->createNamedParameter($name)))
444+
->executeStatement() > 0;
445+
} catch (Exception $e) {
446+
return false;
447+
}
457448
}
458449

459450
/**

lib/base.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ private static function printUpgradePage(\OC\SystemConfig $systemConfig): void {
269269
$result = $qb->select($qb->func()->count('*', 'user_count'))
270270
->from('ldap_user_mapping')
271271
->executeQuery();
272-
$row = $result->fetch();
272+
$row = $result->fetchAssociative();
273273
$result->closeCursor();
274274

275275
$tooBig = ($row['user_count'] > 50);
@@ -280,7 +280,7 @@ private static function printUpgradePage(\OC\SystemConfig $systemConfig): void {
280280
$result = $qb->select($qb->func()->count('*', 'user_count'))
281281
->from('user_saml_users')
282282
->executeQuery();
283-
$row = $result->fetch();
283+
$row = $result->fetchAssociative();
284284
$result->closeCursor();
285285

286286
$tooBig = ($row['user_count'] > 50);

lib/private/Accounts/AccountManager.php

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

203203
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
@@ -1360,7 +1360,7 @@ private function loadConfig(?string $app = null, bool $lazy = false): void {
13601360
}
13611361

13621362
$result = $qb->executeQuery();
1363-
$rows = $result->fetchAll();
1363+
$rows = $result->fetchAllAssociative();
13641364
foreach ($rows as $row) {
13651365
// most of the time, 'lazy' is not in the select because its value is already known
13661366
if ($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;

0 commit comments

Comments
 (0)