Skip to content

Commit

Permalink
Refactor OC\Server::getDatabaseConnection
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Summers <18727110+summersab@users.noreply.github.com>
  • Loading branch information
summersab committed Aug 30, 2023
1 parent 613cd16 commit 6866e69
Show file tree
Hide file tree
Showing 53 changed files with 141 additions and 107 deletions.
4 changes: 3 additions & 1 deletion core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

use OCP\IDBConnection;
use Psr\Log\LoggerInterface;

$application->add(new \Stecman\Component\Symfony\Console\BashCompletion\CompletionCommand());
Expand Down Expand Up @@ -107,7 +109,7 @@
$application->add(\OC::$server->get(OC\Core\Command\Info\Space::class));

$application->add(new OC\Core\Command\Db\ConvertType(\OC::$server->getConfig(), new \OC\DB\ConnectionFactory(\OC::$server->getSystemConfig())));
$application->add(new OC\Core\Command\Db\ConvertMysqlToMB4(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection(), \OC::$server->getURLGenerator(), \OC::$server->get(LoggerInterface::class)));
$application->add(new OC\Core\Command\Db\ConvertMysqlToMB4(\OC::$server->getConfig(), \OC::$server->get(IDBConnection::class), \OC::$server->getURLGenerator(), \OC::$server->get(LoggerInterface::class)));
$application->add(new OC\Core\Command\Db\ConvertFilecacheBigInt(\OC::$server->get(\OC\DB\Connection::class)));
$application->add(\OCP\Server::get(\OC\Core\Command\Db\AddMissingColumns::class));
$application->add(\OCP\Server::get(\OC\Core\Command\Db\AddMissingIndices::class));
Expand Down
4 changes: 2 additions & 2 deletions lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function __construct(IStorage $storage) {

$this->storageCache = new Storage($storage);
$this->mimetypeLoader = \OC::$server->getMimeTypeLoader();
$this->connection = \OC::$server->getDatabaseConnection();
$this->connection = \OC::$server->get(IDBConnection::class);
$this->eventDispatcher = \OC::$server->get(IEventDispatcher::class);
$this->querySearchHelper = \OCP\Server::get(QuerySearchHelper::class);
}
Expand Down Expand Up @@ -1070,7 +1070,7 @@ public function getPathById($id) {
* @deprecated use getPathById() instead
*/
public static function getById($id) {
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query = \OC::$server->get(IDBConnection::class)->getQueryBuilder();
$query->select('path', 'storage')
->from('filecache')
->where($query->expr()->eq('fileid', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
Expand Down
13 changes: 7 additions & 6 deletions lib/private/Files/Cache/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\Storage\IStorage;
use OCP\IDBConnection;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -55,7 +56,7 @@ class Storage {
*/
public static function getGlobalCache() {
if (is_null(self::$globalCache)) {
self::$globalCache = new StorageGlobal(\OC::$server->getDatabaseConnection());
self::$globalCache = new StorageGlobal(\OC::$server->get(IDBConnection::class));
}
return self::$globalCache;
}
Expand All @@ -76,7 +77,7 @@ public function __construct($storage, $isAvailable = true) {
if ($row = self::getStorageById($this->storageId)) {
$this->numericId = (int)$row['numeric_id'];
} else {
$connection = \OC::$server->getDatabaseConnection();
$connection = \OC::$server->get(IDBConnection::class);
$available = $isAvailable ? 1 : 0;
if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId, 'available' => $available])) {
$this->numericId = $connection->lastInsertId('*PREFIX*storages');
Expand Down Expand Up @@ -174,7 +175,7 @@ public function setAvailability($isAvailable, int $delay = 0) {
\OC::$server->get(LoggerInterface::class)->info('Storage with ' . $this->storageId . ' marked as unavailable', ['app' => 'lib']);
}

$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query = \OC::$server->get(IDBConnection::class)->getQueryBuilder();
$query->update('storages')
->set('available', $query->createNamedParameter($available))
->set('last_checked', $query->createNamedParameter(time() + $delay))
Expand All @@ -201,13 +202,13 @@ public static function remove($storageId) {
$storageId = self::adjustStorageId($storageId);
$numericId = self::getNumericStorageId($storageId);

$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query = \OC::$server->get(IDBConnection::class)->getQueryBuilder();
$query->delete('storages')
->where($query->expr()->eq('id', $query->createNamedParameter($storageId)));
$query->execute();

if (!is_null($numericId)) {
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query = \OC::$server->get(IDBConnection::class)->getQueryBuilder();
$query->delete('filecache')
->where($query->expr()->eq('storage', $query->createNamedParameter($numericId)));
$query->execute();
Expand All @@ -220,7 +221,7 @@ public static function remove($storageId) {
* @param int $mountId
*/
public static function cleanByMountId(int $mountId) {
$db = \OC::$server->getDatabaseConnection();
$db = \OC::$server->get(IDBConnection::class);

try {
$db->beginTransaction();
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Files/Cache/Wrapper/CacheJail.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCP\Files\Search\ISearchBinaryOperator;
use OCP\Files\Search\ISearchComparison;
use OCP\Files\Search\ISearchOperator;
use OCP\IDBConnection;

/**
* Jail to a subdirectory of the wrapped cache
Expand All @@ -52,7 +53,7 @@ class CacheJail extends CacheWrapper {
public function __construct($cache, $root) {
parent::__construct($cache);
$this->root = $root;
$this->connection = \OC::$server->getDatabaseConnection();
$this->connection = \OC::$server->get(IDBConnection::class);
$this->mimetypeLoader = \OC::$server->getMimeTypeLoader();

if ($cache instanceof CacheJail) {
Expand Down
5 changes: 3 additions & 2 deletions lib/private/Files/Storage/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
use OCP\Files\Storage\ILockingStorage;
use OCP\Files\Storage\IStorage;
use OCP\Files\Storage\IWriteStreamStorage;
use OCP\IDBConnection;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -382,7 +383,7 @@ public function getPropagator($storage = null) {
}
if (!isset($storage->propagator)) {
$config = \OC::$server->getSystemConfig();
$storage->propagator = new Propagator($storage, \OC::$server->getDatabaseConnection(), ['appdata_' . $config->getValue('instanceid')]);
$storage->propagator = new Propagator($storage, \OC::$server->get(IDBConnection::class), ['appdata_' . $config->getValue('instanceid')]);
}
return $storage->propagator;
}
Expand Down Expand Up @@ -536,7 +537,7 @@ public function verifyPath($path, $fileName) {
throw new InvalidDirectoryException();
}

if (!\OC::$server->getDatabaseConnection()->supports4ByteText()) {
if (!\OC::$server->get(IDBConnection::class)->supports4ByteText()) {
// verify database - e.g. mysql only 3-byte chars
if (preg_match('%(?:
\xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Files/Storage/Home.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace OC\Files\Storage;

use OC\Files\Cache\HomePropagator;
use OCP\IDBConnection;

/**
* Specialized version of Local storage for home directory usage
Expand Down Expand Up @@ -83,7 +84,7 @@ public function getPropagator($storage = null) {
$storage = $this;
}
if (!isset($this->propagator)) {
$this->propagator = new HomePropagator($storage, \OC::$server->getDatabaseConnection());
$this->propagator = new HomePropagator($storage, \OC::$server->get(IDBConnection::class));
}
return $this->propagator;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Files/Storage/Wrapper/Jail.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCP\Files\Storage\IStorage;
use OCP\Files\Storage\IWriteStreamStorage;
use OCP\Lock\ILockingProvider;
use OCP\IDBConnection;

/**
* Jail to a subdirectory of the wrapped storage
Expand Down Expand Up @@ -513,7 +514,7 @@ public function getPropagator($storage = null) {
if (!$storage) {
$storage = $this;
}
$this->propagator = new JailPropagator($storage, \OC::$server->getDatabaseConnection());
$this->propagator = new JailPropagator($storage, \OC::$server->get(IDBConnection::class));
return $this->propagator;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/private/Group/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function __construct(IDBConnection $dbConn = null) {
*/
private function fixDI() {
if ($this->dbConn === null) {
$this->dbConn = \OC::$server->getDatabaseConnection();
$this->dbConn = \OC::$server->get(IDBConnection::class);
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/private/Group/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use OCP\Group\Events\GroupCreatedEvent;
use OCP\GroupInterface;
use OCP\ICacheFactory;
use OCP\IDBConnection;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
Expand Down Expand Up @@ -425,7 +426,7 @@ public function getSubAdmin() {
$this->subAdmin = new \OC\SubAdmin(
$this->userManager,
$this,
\OC::$server->getDatabaseConnection(),
\OC::$server->get(IDBConnection::class),
$this->dispatcher
);
}
Expand Down
17 changes: 9 additions & 8 deletions lib/private/Repair.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Collaboration\Resources\IManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use OC\DB\Connection;
Expand Down Expand Up @@ -174,10 +175,10 @@ public function addStep($repairStep) {
*/
public static function getRepairSteps(): array {
return [
new Collation(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class), \OC::$server->getDatabaseConnection(), false),
new RepairMimeTypes(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
new Collation(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class), \OC::$server->get(IDBConnection::class), false),
new RepairMimeTypes(\OC::$server->getConfig(), \OC::$server->get(IDBConnection::class)),
new CleanTags(\OC::$server->get(IDBConnection::class), \OC::$server->getUserManager()),
new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->get(IDBConnection::class)),
new MoveUpdaterStepFile(\OC::$server->getConfig()),
new MoveAvatars(
\OC::$server->getJobList(),
Expand All @@ -189,16 +190,16 @@ public static function getRepairSteps(): array {
\OC::$server->getConfig()
),
new MigrateOauthTables(\OC::$server->get(Connection::class)),
new FixMountStorages(\OC::$server->getDatabaseConnection()),
new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
new FixMountStorages(\OC::$server->get(IDBConnection::class)),
new UpdateLanguageCodes(\OC::$server->get(IDBConnection::class), \OC::$server->getConfig()),
new AddLogRotateJob(\OC::$server->getJobList()),
new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OCP\Server::get(JSCombiner::class)),
\OCP\Server::get(ClearGeneratedAvatarCache::class),
new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()),
new AddCleanupUpdaterBackupsJob(\OC::$server->getJobList()),
new CleanupCardDAVPhotoCache(\OC::$server->getConfig(), \OC::$server->getAppDataDir('dav-photocache'), \OC::$server->get(LoggerInterface::class)),
new AddClenupLoginFlowV2BackgroundJob(\OC::$server->getJobList()),
new RemoveLinkShares(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig(), \OC::$server->getGroupManager(), \OC::$server->getNotificationManager(), \OCP\Server::get(ITimeFactory::class)),
new RemoveLinkShares(\OC::$server->get(IDBConnection::class), \OC::$server->getConfig(), \OC::$server->getGroupManager(), \OC::$server->getNotificationManager(), \OCP\Server::get(ITimeFactory::class)),
new ClearCollectionsAccessCache(\OC::$server->getConfig(), \OCP\Server::get(IManager::class)),
\OCP\Server::get(ResetGeneratedAvatarFlag::class),
\OCP\Server::get(EncryptionLegacyCipher::class),
Expand All @@ -223,7 +224,7 @@ public static function getRepairSteps(): array {
*/
public static function getExpensiveRepairSteps() {
return [
new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()),
new OldGroupMembershipShares(\OC::$server->get(IDBConnection::class), \OC::$server->getGroupManager()),
\OC::$server->get(ValidatePhoneNumber::class),
];
}
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Share/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ public static function getItems($itemType, ?string $item = null, ?int $shareType
// Remove root from file source paths if retrieving own shared items
if (isset($uidOwner) && isset($row['path'])) {
if (isset($row['parent'])) {
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query = \OC::$server->get(IDBConnection::class)->getQueryBuilder();
$query->select('file_target')
->from('share')
->where($query->expr()->eq('id', $query->createNamedParameter($row['parent'])));
Expand Down
9 changes: 5 additions & 4 deletions lib/private/Share20/ProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use OCA\Talk\Share\RoomShareProvider;
use OCP\Defaults;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IDBConnection;
use OCP\IServerContainer;
use OCP\Share\IManager;
use OCP\Share\IProviderFactory;
Expand Down Expand Up @@ -96,7 +97,7 @@ public function registerProvider(string $shareProviderClass): void {
protected function defaultShareProvider() {
if ($this->defaultProvider === null) {
$this->defaultProvider = new DefaultShareProvider(
$this->serverContainer->getDatabaseConnection(),
$this->serverContainer->get(IDBConnection::class),
$this->serverContainer->getUserManager(),
$this->serverContainer->getGroupManager(),
$this->serverContainer->getLazyRootFolder(),
Expand Down Expand Up @@ -150,7 +151,7 @@ protected function federatedShareProvider() {
);

$this->federatedProvider = new FederatedShareProvider(
$this->serverContainer->getDatabaseConnection(),
$this->serverContainer->get(IDBConnection::class),
$addressHandler,
$notifications,
$tokenHandler,
Expand Down Expand Up @@ -187,7 +188,7 @@ protected function getShareByMailProvider() {

$this->shareByMailProvider = new ShareByMailProvider(
$this->serverContainer->getConfig(),
$this->serverContainer->getDatabaseConnection(),
$this->serverContainer->get(IDBConnection::class),
$this->serverContainer->getSecureRandom(),
$this->serverContainer->getUserManager(),
$this->serverContainer->getLazyRootFolder(),
Expand Down Expand Up @@ -229,7 +230,7 @@ protected function getShareByCircleProvider() {

if ($this->shareByCircleProvider === null) {
$this->shareByCircleProvider = new \OCA\Circles\ShareByCircleProvider(
$this->serverContainer->getDatabaseConnection(),
$this->serverContainer->get(IDBConnection::class),
$this->serverContainer->getSecureRandom(),
$this->serverContainer->getUserManager(),
$this->serverContainer->getLazyRootFolder(),
Expand Down
5 changes: 3 additions & 2 deletions lib/private/SystemTag/ManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace OC\SystemTag;

use OCP\EventDispatcher\IEventDispatcher;
use OCP\IDBConnection;
use OCP\IServerContainer;
use OCP\SystemTag\ISystemTagManager;
use OCP\SystemTag\ISystemTagManagerFactory;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function __construct(IServerContainer $serverContainer) {
*/
public function getManager(): ISystemTagManager {
return new SystemTagManager(
$this->serverContainer->getDatabaseConnection(),
$this->serverContainer->get(IDBConnection::class),
$this->serverContainer->getGroupManager(),
$this->serverContainer->get(IEventDispatcher::class),
);
Expand All @@ -78,7 +79,7 @@ public function getManager(): ISystemTagManager {
*/
public function getObjectMapper(): ISystemTagObjectMapper {
return new SystemTagObjectMapper(
$this->serverContainer->getDatabaseConnection(),
$this->serverContainer->get(IDBConnection::class),
$this->getManager(),
$this->serverContainer->get(IEventDispatcher::class),
);
Expand Down
2 changes: 1 addition & 1 deletion lib/private/User/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function __construct($eventDispatcher = null, $table = 'users') {
*/
private function fixDI() {
if ($this->dbConn === null) {
$this->dbConn = \OC::$server->getDatabaseConnection();
$this->dbConn = \OC::$server->get(IDBConnection::class);
}
}

Expand Down
9 changes: 5 additions & 4 deletions lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IGroup;
use OCP\IUser;
use OCP\IUserBackend;
Expand Down Expand Up @@ -536,7 +537,7 @@ public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = fa
* @since 12.0.0
*/
public function countDisabledUsers(): int {
$queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$queryBuilder = \OC::$server->get(IDBConnection::class)->getQueryBuilder();
$queryBuilder->select($queryBuilder->func()->count('*'))
->from('preferences')
->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core')))
Expand Down Expand Up @@ -565,7 +566,7 @@ public function countDisabledUsers(): int {
* @since 14.0.0
*/
public function countDisabledUsersOfGroups(array $groups): int {
$queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$queryBuilder = \OC::$server->get(IDBConnection::class)->getQueryBuilder();
$queryBuilder->select($queryBuilder->createFunction('COUNT(DISTINCT ' . $queryBuilder->getColumnName('uid') . ')'))
->from('preferences', 'p')
->innerJoin('p', 'group_user', 'g', $queryBuilder->expr()->eq('p.userid', 'g.uid'))
Expand Down Expand Up @@ -594,7 +595,7 @@ public function countDisabledUsersOfGroups(array $groups): int {
* @since 11.0.0
*/
public function countSeenUsers() {
$queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$queryBuilder = \OC::$server->get(IDBConnection::class)->getQueryBuilder();
$queryBuilder->select($queryBuilder->func()->count('*'))
->from('preferences')
->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login')))
Expand Down Expand Up @@ -644,7 +645,7 @@ public function callForSeenUsers(\Closure $callback) {
* @return string[] with user ids
*/
private function getSeenUserIds($limit = null, $offset = null) {
$queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$queryBuilder = \OC::$server->get(IDBConnection::class)->getQueryBuilder();
$queryBuilder->select(['userid'])
->from('preferences')
->where($queryBuilder->expr()->eq(
Expand Down
3 changes: 2 additions & 1 deletion tests/Test/Repair/Owncloud/UpdateLanguageCodesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OC\Repair\Owncloud\UpdateLanguageCodes;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use Test\TestCase;

Expand All @@ -46,7 +47,7 @@ class UpdateLanguageCodesTest extends TestCase {
protected function setUp(): void {
parent::setUp();

$this->connection = \OC::$server->getDatabaseConnection();
$this->connection = \OC::$server->get(IDBConnection::class);
$this->config = $this->createMock(IConfig::class);
}

Expand Down
Loading

0 comments on commit 6866e69

Please sign in to comment.