Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WiP] refactor(db): Apply query prepared statements #48791

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions apps/dav/lib/BackgroundJob/CleanupInvitationTokenJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@

class CleanupInvitationTokenJob extends TimedJob {

/** @var IDBConnection */
private $db;

public function __construct(IDBConnection $db, ITimeFactory $time) {
public function __construct(
private IDBConnection $db,
ITimeFactory $time,
) {
parent::__construct($time);
$this->db = $db;

// Run once a day at off-peak time
$this->setInterval(24 * 60 * 60);
Expand All @@ -31,6 +30,6 @@ public function run($argument) {
$query->delete('calendar_invitations')
->where($query->expr()->lt('expiration',
$query->createNamedParameter($this->time->getTime())))
->execute();
->executeStatement();
}
}
53 changes: 18 additions & 35 deletions apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,6 @@

abstract class AbstractPrincipalBackend implements BackendInterface {

/** @var IDBConnection */
private $db;

/** @var IUserSession */
private $userSession;

/** @var IGroupManager */
private $groupManager;

private LoggerInterface $logger;

/** @var ProxyMapper */
private $proxyMapper;

/** @var string */
private $principalPrefix;

Expand All @@ -54,19 +40,16 @@
/** @var string */
private $cuType;

public function __construct(IDBConnection $dbConnection,
IUserSession $userSession,
IGroupManager $groupManager,
LoggerInterface $logger,
ProxyMapper $proxyMapper,
public function __construct(
private IDBConnection $dbConnection,
private IUserSession $userSession,
private IGroupManager $groupManager,
private LoggerInterface $logger,
private ProxyMapper $proxyMapper,
string $principalPrefix,
string $dbPrefix,
string $cuType) {
$this->db = $dbConnection;
$this->userSession = $userSession;
$this->groupManager = $groupManager;
$this->logger = $logger;
$this->proxyMapper = $proxyMapper;
string $cuType,
) {
$this->principalPrefix = $principalPrefix;
$this->dbTableName = 'calendar_' . $dbPrefix . 's';
$this->dbMetaDataTableName = $this->dbTableName . '_md';
Expand All @@ -93,15 +76,15 @@
$principals = [];

if ($prefixPath === $this->principalPrefix) {
$query = $this->db->getQueryBuilder();

Check failure on line 79 in apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedThisPropertyFetch

apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php:79:13: UndefinedThisPropertyFetch: Instance property OCA\DAV\CalDAV\ResourceBooking\AbstractPrincipalBackend::$db is not defined (see https://psalm.dev/041)
$query->select(['id', 'backend_id', 'resource_id', 'email', 'displayname'])
->from($this->dbTableName);
$stmt = $query->execute();
$stmt = $query->executeQuery();

$metaDataQuery = $this->db->getQueryBuilder();
$metaDataQuery->select([$this->dbForeignKeyName, 'key', 'value'])
->from($this->dbMetaDataTableName);
$metaDataStmt = $metaDataQuery->execute();
$metaDataStmt = $metaDataQuery->executeQuery();
$metaDataRows = $metaDataStmt->fetchAll(\PDO::FETCH_ASSOC);

$metaDataById = [];
Expand Down Expand Up @@ -147,12 +130,12 @@

[$backendId, $resourceId] = explode('-', $name, 2);

$query = $this->db->getQueryBuilder();

Check failure on line 133 in apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedThisPropertyFetch

apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php:133:12: UndefinedThisPropertyFetch: Instance property OCA\DAV\CalDAV\ResourceBooking\AbstractPrincipalBackend::$db is not defined (see https://psalm.dev/041)
$query->select(['id', 'backend_id', 'resource_id', 'email', 'displayname'])
->from($this->dbTableName)
->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId)))
->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resourceId)));
$stmt = $query->execute();
$stmt = $query->executeQuery();
$row = $stmt->fetch(\PDO::FETCH_ASSOC);

if (!$row) {
Expand All @@ -163,7 +146,7 @@
$metaDataQuery->select(['key', 'value'])
->from($this->dbMetaDataTableName)
->where($metaDataQuery->expr()->eq($this->dbForeignKeyName, $metaDataQuery->createNamedParameter($row['id'])));
$metaDataStmt = $metaDataQuery->execute();
$metaDataStmt = $metaDataQuery->executeQuery();
$metaDataRows = $metaDataStmt->fetchAll(\PDO::FETCH_ASSOC);
$metadata = [];

Expand All @@ -179,11 +162,11 @@
* @return string[]|null
*/
public function getPrincipalById($id): ?array {
$query = $this->db->getQueryBuilder();

Check failure on line 165 in apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedThisPropertyFetch

apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php:165:12: UndefinedThisPropertyFetch: Instance property OCA\DAV\CalDAV\ResourceBooking\AbstractPrincipalBackend::$db is not defined (see https://psalm.dev/041)
$query->select(['id', 'backend_id', 'resource_id', 'email', 'displayname'])
->from($this->dbTableName)
->where($query->expr()->eq('id', $query->createNamedParameter($id)));
$stmt = $query->execute();
$stmt = $query->executeQuery();
$row = $stmt->fetch(\PDO::FETCH_ASSOC);

if (!$row) {
Expand All @@ -194,7 +177,7 @@
$metaDataQuery->select(['key', 'value'])
->from($this->dbMetaDataTableName)
->where($metaDataQuery->expr()->eq($this->dbForeignKeyName, $metaDataQuery->createNamedParameter($row['id'])));
$metaDataStmt = $metaDataQuery->execute();
$metaDataStmt = $metaDataQuery->executeQuery();
$metaDataRows = $metaDataStmt->fetchAll(\PDO::FETCH_ASSOC);
$metadata = [];

Expand Down Expand Up @@ -238,12 +221,12 @@
foreach ($searchProperties as $prop => $value) {
switch ($prop) {
case '{http://sabredav.org/ns}email-address':
$query = $this->db->getQueryBuilder();

Check failure on line 224 in apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedThisPropertyFetch

apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php:224:15: UndefinedThisPropertyFetch: Instance property OCA\DAV\CalDAV\ResourceBooking\AbstractPrincipalBackend::$db is not defined (see https://psalm.dev/041)
$query->select(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions'])
->from($this->dbTableName)
->where($query->expr()->iLike('email', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($value) . '%')));

$stmt = $query->execute();
$stmt = $query->executeQuery();
$principals = [];
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
if (!$this->isAllowedToAccessResource($row, $usersGroups)) {
Expand All @@ -257,12 +240,12 @@
break;

case '{DAV:}displayname':
$query = $this->db->getQueryBuilder();

Check failure on line 243 in apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedThisPropertyFetch

apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php:243:15: UndefinedThisPropertyFetch: Instance property OCA\DAV\CalDAV\ResourceBooking\AbstractPrincipalBackend::$db is not defined (see https://psalm.dev/041)
$query->select(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions'])
->from($this->dbTableName)
->where($query->expr()->iLike('displayname', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($value) . '%')));

$stmt = $query->execute();
$stmt = $query->executeQuery();
$principals = [];
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
if (!$this->isAllowedToAccessResource($row, $usersGroups)) {
Expand Down Expand Up @@ -319,7 +302,7 @@
* @return IQueryBuilder
*/
private function getMetadataQuery(string $key): IQueryBuilder {
$query = $this->db->getQueryBuilder();

Check failure on line 305 in apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedThisPropertyFetch

apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php:305:12: UndefinedThisPropertyFetch: Instance property OCA\DAV\CalDAV\ResourceBooking\AbstractPrincipalBackend::$db is not defined (see https://psalm.dev/041)
$query->select([$this->dbForeignKeyName])
->from($this->dbMetaDataTableName)
->where($query->expr()->eq('key', $query->createNamedParameter($key)));
Expand All @@ -339,7 +322,7 @@
*/
private function searchPrincipalsByMetadataKey(string $key, string $value, array $usersGroups = []): array {
$query = $this->getMetadataQuery($key);
$query->andWhere($query->expr()->iLike('value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($value) . '%')));

Check failure on line 325 in apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedThisPropertyFetch

apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php:325:86: UndefinedThisPropertyFetch: Instance property OCA\DAV\CalDAV\ResourceBooking\AbstractPrincipalBackend::$db is not defined (see https://psalm.dev/041)
return $this->getRows($query, $usersGroups);
}

Expand All @@ -356,7 +339,7 @@
private function searchPrincipalsByRoomFeature(string $key, string $value, array $usersGroups = []): array {
$query = $this->getMetadataQuery($key);
foreach (explode(',', $value) as $v) {
$query->andWhere($query->expr()->iLike('value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($v) . '%')));

Check failure on line 342 in apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedThisPropertyFetch

apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php:342:87: UndefinedThisPropertyFetch: Instance property OCA\DAV\CalDAV\ResourceBooking\AbstractPrincipalBackend::$db is not defined (see https://psalm.dev/041)
}
return $this->getRows($query, $usersGroups);
}
Expand Down Expand Up @@ -425,12 +408,12 @@

if (str_starts_with($uri, 'mailto:')) {
$email = substr($uri, 7);
$query = $this->db->getQueryBuilder();

Check failure on line 411 in apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedThisPropertyFetch

apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php:411:13: UndefinedThisPropertyFetch: Instance property OCA\DAV\CalDAV\ResourceBooking\AbstractPrincipalBackend::$db is not defined (see https://psalm.dev/041)
$query->select(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions'])
->from($this->dbTableName)
->where($query->expr()->eq('email', $query->createNamedParameter($email)));

$stmt = $query->execute();
$stmt = $query->executeQuery();
$row = $stmt->fetch(\PDO::FETCH_ASSOC);

if (!$row) {
Expand All @@ -452,12 +435,12 @@
[, $name] = \Sabre\Uri\split($path);
[$backendId, $resourceId] = explode('-', $name, 2);

$query = $this->db->getQueryBuilder();

Check failure on line 438 in apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedThisPropertyFetch

apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php:438:13: UndefinedThisPropertyFetch: Instance property OCA\DAV\CalDAV\ResourceBooking\AbstractPrincipalBackend::$db is not defined (see https://psalm.dev/041)
$query->select(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions'])
->from($this->dbTableName)
->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId)))
->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resourceId)));
$stmt = $query->execute();
$stmt = $query->executeQuery();
$row = $stmt->fetch(\PDO::FETCH_ASSOC);

if (!$row) {
Expand Down
27 changes: 6 additions & 21 deletions apps/dav/lib/Migration/BuildSocialSearchIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,11 @@

class BuildSocialSearchIndex implements IRepairStep {

/** @var IDBConnection */
private $db;

/** @var IJobList */
private $jobList;

/** @var IConfig */
private $config;

/**
* @param IDBConnection $db
* @param IJobList $jobList
* @param IConfig $config
*/
public function __construct(IDBConnection $db,
IJobList $jobList,
IConfig $config) {
$this->db = $db;
$this->jobList = $jobList;
$this->config = $config;
public function __construct(
private IDBConnection $db,
private IJobList $jobList,
private IConfig $config,
) {
}

/**
Expand All @@ -56,7 +41,7 @@ public function run(IOutput $output) {
$query->select($query->func()->max('cardid'))
->from('cards_properties')
->where($query->expr()->eq('name', $query->createNamedParameter('X-SOCIALPROFILE')));
$maxId = (int)$query->execute()->fetchOne();
$maxId = (int)$query->executeQuery()->fetchOne();

if ($maxId === 0) {
return;
Expand Down
2 changes: 1 addition & 1 deletion apps/dav/lib/Migration/RefreshWebcalJobRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function run(IOutput $output) {
$query = $this->connection->getQueryBuilder();
$query->select(['principaluri', 'uri'])
->from('calendarsubscriptions');
$stmt = $query->execute();
$stmt = $query->executeQuery();

$count = 0;
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
Expand Down
16 changes: 6 additions & 10 deletions apps/dav/lib/Migration/RemoveDeletedUsersCalendarSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
use OCP\Migration\IRepairStep;

class RemoveDeletedUsersCalendarSubscriptions implements IRepairStep {
/** @var IDBConnection */
private $connection;

/** @var IUserManager */
private $userManager;

/** @var int */
private $progress = 0;
Expand All @@ -29,9 +24,10 @@ class RemoveDeletedUsersCalendarSubscriptions implements IRepairStep {

private const SUBSCRIPTIONS_CHUNK_SIZE = 1000;

public function __construct(IDBConnection $connection, IUserManager $userManager) {
$this->connection = $connection;
$this->userManager = $userManager;
public function __construct(
private IDBConnection $connection,
private IUserManager $userManager,
) {
}

/**
Expand Down Expand Up @@ -69,7 +65,7 @@ private function countSubscriptions(): int {
$query = $qb->select($qb->func()->count('*'))
->from('calendarsubscriptions');

$result = $query->execute();
$result = $query->executeQuery();
$count = $result->fetchOne();
$result->closeCursor();

Expand All @@ -92,7 +88,7 @@ private function checkSubscriptions(): void {
->setMaxResults(self::SUBSCRIPTIONS_CHUNK_SIZE)
->setFirstResult($this->progress);

$result = $query->execute();
$result = $query->executeQuery();
while ($row = $result->fetch()) {
$username = $this->getPrincipal($row['principaluri']);
if (!$this->userManager->userExists($username)) {
Expand Down
8 changes: 4 additions & 4 deletions apps/files/lib/Command/RepairTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function execute(InputInterface $input, OutputInterface $output): int {
'path' => $row['parent_path'] . '/' . $row['name'],
'storage' => $row['parent_storage'],
]);
$query->execute();
$query->executeStatement();
}
}
}
Expand All @@ -78,14 +78,14 @@ private function getFileId(int $storage, string $path) {
->from('filecache')
->where($query->expr()->eq('storage', $query->createNamedParameter($storage)))
->andWhere($query->expr()->eq('path_hash', $query->createNamedParameter(md5($path))));
return $query->execute()->fetch(\PDO::FETCH_COLUMN);
return $query->executeQuery()->fetch(\PDO::FETCH_COLUMN);
}

private function deleteById(int $fileId): void {
$query = $this->connection->getQueryBuilder();
$query->delete('filecache')
->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId)));
$query->execute();
$query->executeStatement();
}

private function findBrokenTreeBits(): array {
Expand All @@ -108,6 +108,6 @@ private function findBrokenTreeBits(): array {
$query->expr()->neq('f.storage', 'p.storage')
));

return $query->execute()->fetchAll();
return $query->executeQuery()->fetchAll();
}
}
2 changes: 1 addition & 1 deletion apps/files_external/lib/Command/Notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private function getStorageIds(int $mountId, string $path): array {
->innerJoin('m', 'filecache', 'f', $qb->expr()->eq('m.storage_id', 'f.storage'))
->where($qb->expr()->eq('mount_id', $qb->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('path_hash', $qb->createNamedParameter($pathHash, IQueryBuilder::PARAM_STR)))
->execute()
->executeQuery()
->fetchAll();
}

Expand Down
18 changes: 6 additions & 12 deletions apps/files_sharing/lib/Migration/SetPasswordColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@
*/
class SetPasswordColumn implements IRepairStep {

/** @var IDBConnection */
private $connection;

/** @var IConfig */
private $config;


public function __construct(IDBConnection $connection, IConfig $config) {
$this->connection = $connection;
$this->config = $config;
public function __construct(
private IDBConnection $connection,
private IConfig $config,
) {
}

/**
Expand All @@ -54,7 +48,7 @@ public function run(IOutput $output) {
->set('password', 'share_with')
->where($query->expr()->eq('share_type', $query->createNamedParameter(IShare::TYPE_LINK)))
->andWhere($query->expr()->isNotNull('share_with'));
$result = $query->execute();
$result = $query->executeStatement();

if ($result === 0) {
// No link updated, no need to run the second query
Expand All @@ -67,7 +61,7 @@ public function run(IOutput $output) {
->set('share_with', $clearQuery->createNamedParameter(null))
->where($clearQuery->expr()->eq('share_type', $clearQuery->createNamedParameter(IShare::TYPE_LINK)));

$clearQuery->execute();
$clearQuery->executeStatement();
}

protected function shouldRun() {
Expand Down
4 changes: 2 additions & 2 deletions apps/files_sharing/lib/ShareBackend/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function getChildren($itemSource) {
->where(
$qb->expr()->eq('mimetype', $qb->createNamedParameter('httpd/unix-directory'))
);
$result = $qb->execute();
$result = $qb->executeQuery();
$row = $result->fetch();
$result->closeCursor();

Expand All @@ -41,7 +41,7 @@ public function getChildren($itemSource) {
$qb->expr()->in('parent', $parents)
);

$result = $qb->execute();
$result = $qb->executeQuery();

$parents = [];
while ($file = $result->fetch()) {
Expand Down
2 changes: 1 addition & 1 deletion apps/settings/lib/Settings/Admin/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function cronMaxAge(): int {
->orderBy('last_checked', 'ASC')
->setMaxResults(1);

$result = $query->execute();
$result = $query->executeQuery();
if ($row = $result->fetch()) {
$maxAge = (int)$row['last_checked'];
} else {
Expand Down
13 changes: 6 additions & 7 deletions apps/user_ldap/lib/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
use OCP\IDBConnection;

class Helper {
private IConfig $config;
private IDBConnection $connection;

/** @var CappedMemoryCache<string> */
protected CappedMemoryCache $sanitizeDnCache;

public function __construct(IConfig $config,
IDBConnection $connection) {
$this->config = $config;
$this->connection = $connection;
public function __construct(
private IConfig $config,
private IDBConnection $connection,
) {
$this->sanitizeDnCache = new CappedMemoryCache(10000);
}

Expand Down Expand Up @@ -145,7 +144,7 @@ public function deleteServerConfiguration($prefix) {
$query->andWhere($query->expr()->notLike('configkey', $query->createNamedParameter('s%')));
}

$deletedRows = $query->execute();
$deletedRows = $query->executeStatement();
return $deletedRows !== 0;
}

Expand Down
Loading
Loading