diff --git a/apps/dav/lib/Migration/CalDAVRemoveEmptyValue.php b/apps/dav/lib/Migration/CalDAVRemoveEmptyValue.php index cbce847a298..c7f57dcb117 100644 --- a/apps/dav/lib/Migration/CalDAVRemoveEmptyValue.php +++ b/apps/dav/lib/Migration/CalDAVRemoveEmptyValue.php @@ -5,7 +5,6 @@ */ namespace OCA\DAV\Migration; -use Doctrine\DBAL\Platforms\OraclePlatform; use OCA\DAV\CalDAV\CalDavBackend; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; @@ -75,7 +74,7 @@ class CalDAVRemoveEmptyValue implements IRepairStep { } protected function getInvalidObjects($pattern) { - if ($this->db->getDatabasePlatform() instanceof OraclePlatform) { + if ($this->db->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) { $rows = []; $chunkSize = 500; $query = $this->db->getQueryBuilder(); diff --git a/apps/settings/tests/SetupChecks/SupportedDatabaseTest.php b/apps/settings/tests/SetupChecks/SupportedDatabaseTest.php index 123bb9b1e88..0ba1621c5fe 100644 --- a/apps/settings/tests/SetupChecks/SupportedDatabaseTest.php +++ b/apps/settings/tests/SetupChecks/SupportedDatabaseTest.php @@ -8,7 +8,6 @@ declare(strict_types=1); */ namespace OCA\Settings\Tests; -use Doctrine\DBAL\Platforms\SQLitePlatform; use OCA\Settings\SetupChecks\SupportedDatabase; use OCP\IDBConnection; use OCP\IL10N; @@ -41,8 +40,7 @@ class SupportedDatabaseTest extends TestCase { } public function testPass(): void { - $platform = $this->connection->getDatabasePlatform(); - if ($platform instanceof SQLitePlatform) { + if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_SQLITE) { /** SQlite always gets a warning */ $this->assertEquals(SetupResult::WARNING, $this->check->run()->getSeverity()); } else { diff --git a/apps/user_ldap/lib/Mapping/AbstractMapping.php b/apps/user_ldap/lib/Mapping/AbstractMapping.php index 7dced1ca7c3..9b92960d354 100644 --- a/apps/user_ldap/lib/Mapping/AbstractMapping.php +++ b/apps/user_ldap/lib/Mapping/AbstractMapping.php @@ -8,9 +8,9 @@ namespace OCA\User_LDAP\Mapping; use Doctrine\DBAL\Exception; -use Doctrine\DBAL\Platforms\SQLitePlatform; use OCP\DB\IPreparedStatement; use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; use Psr\Log\LoggerInterface; /** @@ -216,7 +216,7 @@ abstract class AbstractMapping { public function getListOfIdsByDn(array $fdns): array { $totalDBParamLimit = 65000; $sliceSize = 1000; - $maxSlices = $this->dbc->getDatabasePlatform() instanceof SQLitePlatform ? 9 : $totalDBParamLimit / $sliceSize; + $maxSlices = $this->dbc->getDatabaseProvider() === IDBConnection::PLATFORM_SQLITE ? 9 : $totalDBParamLimit / $sliceSize; $results = []; $slice = 1; diff --git a/core/Command/Db/ConvertFilecacheBigInt.php b/core/Command/Db/ConvertFilecacheBigInt.php index 7e47e3fa753..0375823c36c 100644 --- a/core/Command/Db/ConvertFilecacheBigInt.php +++ b/core/Command/Db/ConvertFilecacheBigInt.php @@ -5,11 +5,11 @@ */ namespace OC\Core\Command\Db; -use Doctrine\DBAL\Platforms\SQLitePlatform; use Doctrine\DBAL\Types\Type; use OC\DB\Connection; use OC\DB\SchemaWrapper; use OCP\DB\Types; +use OCP\IDBConnection; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -53,7 +53,7 @@ class ConvertFilecacheBigInt extends Command { protected function execute(InputInterface $input, OutputInterface $output): int { $schema = new SchemaWrapper($this->connection); - $isSqlite = $this->connection->getDatabasePlatform() instanceof SQLitePlatform; + $isSqlite = $this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_SQLITE; $updates = []; $tables = static::getColumnsByTable(); diff --git a/core/Command/Db/ConvertMysqlToMB4.php b/core/Command/Db/ConvertMysqlToMB4.php index 679cdd5f616..618e15de3f4 100644 --- a/core/Command/Db/ConvertMysqlToMB4.php +++ b/core/Command/Db/ConvertMysqlToMB4.php @@ -5,7 +5,6 @@ */ namespace OC\Core\Command\Db; -use Doctrine\DBAL\Platforms\MySQLPlatform; use OC\DB\MySqlTools; use OC\Migration\ConsoleOutput; use OC\Repair\Collation; @@ -34,7 +33,7 @@ class ConvertMysqlToMB4 extends Command { } protected function execute(InputInterface $input, OutputInterface $output): int { - if (!$this->connection->getDatabasePlatform() instanceof MySQLPlatform) { + if ($this->connection->getDatabaseProvider() !== IDBConnection::PLATFORM_MYSQL) { $output->writeln("This command is only valid for MySQL/MariaDB databases."); return 1; } diff --git a/lib/private/AllConfig.php b/lib/private/AllConfig.php index d05fe440202..58eee772fbf 100644 --- a/lib/private/AllConfig.php +++ b/lib/private/AllConfig.php @@ -6,7 +6,6 @@ */ namespace OC; -use Doctrine\DBAL\Platforms\OraclePlatform; use OCP\Cache\CappedMemoryCache; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IConfig; @@ -470,7 +469,7 @@ class AllConfig implements IConfig { $this->fixDIInit(); $qb = $this->connection->getQueryBuilder(); - $configValueColumn = ($this->connection->getDatabasePlatform() instanceof OraclePlatform) + $configValueColumn = ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) ? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR) : 'configvalue'; $result = $qb->select('userid') @@ -509,7 +508,7 @@ class AllConfig implements IConfig { } $qb = $this->connection->getQueryBuilder(); - $configValueColumn = ($this->connection->getDatabasePlatform() instanceof OraclePlatform) + $configValueColumn = ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) ? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR) : 'configvalue'; diff --git a/lib/private/BackgroundJob/JobList.php b/lib/private/BackgroundJob/JobList.php index 61c48b0eab2..cec487fbe7b 100644 --- a/lib/private/BackgroundJob/JobList.php +++ b/lib/private/BackgroundJob/JobList.php @@ -7,7 +7,6 @@ */ namespace OC\BackgroundJob; -use Doctrine\DBAL\Platforms\MySQLPlatform; use OCP\AppFramework\QueryException; use OCP\AppFramework\Utility\ITimeFactory; use OCP\AutoloadNotAllowedException; @@ -98,7 +97,7 @@ class JobList implements IJobList { // Add galera safe delete chunking if using mysql // Stops us hitting wsrep_max_ws_rows when large row counts are deleted - if ($this->connection->getDatabasePlatform() instanceof MySQLPlatform) { + if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_MYSQL) { // Then use chunked delete $max = IQueryBuilder::MAX_ROW_DELETION; diff --git a/lib/private/DB/Connection.php b/lib/private/DB/Connection.php index 6b261827125..425d034fd07 100644 --- a/lib/private/DB/Connection.php +++ b/lib/private/DB/Connection.php @@ -18,6 +18,7 @@ use Doctrine\DBAL\Exception\ConnectionLost; use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\MySQLPlatform; use Doctrine\DBAL\Platforms\OraclePlatform; +use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Platforms\SQLitePlatform; use Doctrine\DBAL\Result; use Doctrine\DBAL\Schema\Schema; @@ -27,6 +28,7 @@ use OC\SystemConfig; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Diagnostics\IEventLogger; use OCP\Diagnostics\IQueryLogger; +use OCP\IDBConnection; use OCP\IRequestId; use OCP\PreConditionNotMetException; use OCP\Profiler\IProfiler; @@ -715,4 +717,19 @@ class Connection extends PrimaryReadReplicaConnection { private function getConnectionName(): string { return $this->isConnectedToPrimary() ? 'primary' : 'replica'; } + + public function getDatabaseProvider(): string { + $platform = $this->getDatabasePlatform(); + if ($platform instanceof MySQLPlatform) { + return IDBConnection::PLATFORM_MYSQL; + } elseif ($platform instanceof OraclePlatform) { + return IDBConnection::PLATFORM_ORACLE; + } elseif ($platform instanceof PostgreSQLPlatform) { + return IDBConnection::PLATFORM_POSTGRES; + } elseif ($platform instanceof SQLitePlatform) { + return IDBConnection::PLATFORM_SQLITE; + } else { + throw new \Exception('Database ' . $platform::class . ' not supported'); + } + } } diff --git a/lib/private/DB/ConnectionAdapter.php b/lib/private/DB/ConnectionAdapter.php index 61bb37766d2..52e2ab3261e 100644 --- a/lib/private/DB/ConnectionAdapter.php +++ b/lib/private/DB/ConnectionAdapter.php @@ -238,18 +238,10 @@ class ConnectionAdapter implements IDBConnection { return $this->inner; } + /** + * @return self::PLATFORM_MYSQL|self::PLATFORM_ORACLE|self::PLATFORM_POSTGRES|self::PLATFORM_SQLITE + */ public function getDatabaseProvider(): string { - $platform = $this->inner->getDatabasePlatform(); - if ($platform instanceof MySQLPlatform) { - return IDBConnection::PLATFORM_MYSQL; - } elseif ($platform instanceof OraclePlatform) { - return IDBConnection::PLATFORM_ORACLE; - } elseif ($platform instanceof PostgreSQLPlatform) { - return IDBConnection::PLATFORM_POSTGRES; - } elseif ($platform instanceof SQLitePlatform) { - return IDBConnection::PLATFORM_SQLITE; - } else { - throw new \Exception('Database ' . $platform::class . ' not supported'); - } + return $this->inner->getDatabaseProvider(); } } diff --git a/lib/private/DB/MigrationService.php b/lib/private/DB/MigrationService.php index feee88fa81d..acf060f348d 100644 --- a/lib/private/DB/MigrationService.php +++ b/lib/private/DB/MigrationService.php @@ -6,8 +6,6 @@ */ namespace OC\DB; -use Doctrine\DBAL\Platforms\OraclePlatform; -use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\SchemaException; @@ -20,6 +18,7 @@ use OCP\AppFramework\App; use OCP\AppFramework\QueryException; use OCP\DB\ISchemaWrapper; use OCP\DB\Types; +use OCP\IDBConnection; use OCP\Migration\IMigrationStep; use OCP\Migration\IOutput; use OCP\Server; @@ -601,7 +600,7 @@ class MigrationService { $indexName = strtolower($primaryKey->getName()); $isUsingDefaultName = $indexName === 'primary'; - if ($this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform) { + if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_POSTGRES) { $defaultName = $table->getName() . '_pkey'; $isUsingDefaultName = strtolower($defaultName) === $indexName; @@ -611,7 +610,7 @@ class MigrationService { return $sequence->getName() !== $sequenceName; }); } - } elseif ($this->connection->getDatabasePlatform() instanceof OraclePlatform) { + } elseif ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) { $defaultName = $table->getName() . '_seq'; $isUsingDefaultName = strtolower($defaultName) === $indexName; } diff --git a/lib/private/DB/QueryBuilder/QueryBuilder.php b/lib/private/DB/QueryBuilder/QueryBuilder.php index d071c942bc8..93a2120ec9a 100644 --- a/lib/private/DB/QueryBuilder/QueryBuilder.php +++ b/lib/private/DB/QueryBuilder/QueryBuilder.php @@ -109,20 +109,12 @@ class QueryBuilder implements IQueryBuilder { * @return \OCP\DB\QueryBuilder\IExpressionBuilder */ public function expr() { - if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) { - return new OCIExpressionBuilder($this->connection, $this); - } - if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_POSTGRES) { - return new PgSqlExpressionBuilder($this->connection, $this); - } - if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_MYSQL) { - return new MySqlExpressionBuilder($this->connection, $this); - } - if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_SQLITE) { - return new SqliteExpressionBuilder($this->connection, $this); - } - - return new ExpressionBuilder($this->connection, $this); + return match($this->connection->getDatabaseProvider()) { + IDBConnection::PLATFORM_ORACLE => new OCIExpressionBuilder($this->connection, $this), + IDBConnection::PLATFORM_POSTGRES => new PgSqlExpressionBuilder($this->connection, $this), + IDBConnection::PLATFORM_MYSQL => new MySqlExpressionBuilder($this->connection, $this), + IDBConnection::PLATFORM_SQLITE => new SqliteExpressionBuilder($this->connection, $this), + }; } /** diff --git a/lib/private/Repair/Collation.php b/lib/private/Repair/Collation.php index 0affb3b1ca9..a01a684151b 100644 --- a/lib/private/Repair/Collation.php +++ b/lib/private/Repair/Collation.php @@ -8,7 +8,6 @@ namespace OC\Repair; use Doctrine\DBAL\Exception\DriverException; -use Doctrine\DBAL\Platforms\MySQLPlatform; use OCP\IConfig; use OCP\IDBConnection; use OCP\Migration\IOutput; @@ -50,7 +49,7 @@ class Collation implements IRepairStep { * Fix mime types */ public function run(IOutput $output) { - if (!$this->connection->getDatabasePlatform() instanceof MySQLPlatform) { + if ($this->connection->getDatabaseProvider() !== IDBConnection::PLATFORM_MYSQL) { $output->info('Not a mysql database -> nothing to do'); return; } diff --git a/lib/public/IDBConnection.php b/lib/public/IDBConnection.php index 75440b7428a..25823bc33e7 100644 --- a/lib/public/IDBConnection.php +++ b/lib/public/IDBConnection.php @@ -343,7 +343,7 @@ interface IDBConnection { * Returns the database provider name * @link https://github.com/nextcloud/server/issues/30877 * @since 28.0.0 - * @return IDBConnection::PLATFORM_* + * @return self::PLATFORM_MYSQL|self::PLATFORM_ORACLE|self::PLATFORM_POSTGRES|self::PLATFORM_SQLITE */ public function getDatabaseProvider(): string; } diff --git a/tests/lib/DB/MigrationsTest.php b/tests/lib/DB/MigrationsTest.php index 05dbc13e372..e1432e59964 100644 --- a/tests/lib/DB/MigrationsTest.php +++ b/tests/lib/DB/MigrationsTest.php @@ -8,8 +8,6 @@ namespace Test\DB; -use Doctrine\DBAL\Platforms\OraclePlatform; -use Doctrine\DBAL\Platforms\PostgreSqlPlatform; use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Exception\TableDoesNotExist; use Doctrine\DBAL\Schema\ForeignKeyConstraint; @@ -326,9 +324,9 @@ class MigrationsTest extends \Test\TestCase { public function testEnsureOracleConstraintsValidWithPrimaryKeyDefault() { $defaultName = 'PRIMARY'; - if ($this->db->getDatabasePlatform() instanceof PostgreSqlPlatform) { + if ($this->db->getDatabaseProvider() === IDBConnection::PLATFORM_POSTGRES) { $defaultName = \str_repeat('a', 26) . '_' . \str_repeat('b', 30) . '_seq'; - } elseif ($this->db->getDatabasePlatform() instanceof OraclePlatform) { + } elseif ($this->db->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) { $defaultName = \str_repeat('a', 26) . '_seq'; } @@ -407,9 +405,9 @@ class MigrationsTest extends \Test\TestCase { $this->expectException(\InvalidArgumentException::class); $defaultName = 'PRIMARY'; - if ($this->db->getDatabasePlatform() instanceof PostgreSqlPlatform) { + if ($this->db->getDatabaseProvider() === IDBConnection::PLATFORM_POSTGRES) { $defaultName = \str_repeat('a', 27) . '_' . \str_repeat('b', 30) . '_seq'; - } elseif ($this->db->getDatabasePlatform() instanceof OraclePlatform) { + } elseif ($this->db->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) { $defaultName = \str_repeat('a', 27) . '_seq'; } diff --git a/tests/lib/DB/MigratorTest.php b/tests/lib/DB/MigratorTest.php index a486a39ae3d..36343e3fc47 100644 --- a/tests/lib/DB/MigratorTest.php +++ b/tests/lib/DB/MigratorTest.php @@ -10,8 +10,6 @@ namespace Test\DB; use Doctrine\DBAL\Exception; use Doctrine\DBAL\ParameterType; -use Doctrine\DBAL\Platforms\OraclePlatform; -use Doctrine\DBAL\Platforms\SQLitePlatform; use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\SchemaConfig; use OC\DB\Migrator; @@ -21,7 +19,7 @@ use OC\DB\TDoctrineParameterTypeMap; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\Types; use OCP\IConfig; -use OCP\Security\ISecureRandom; +use OCP\IDBConnection; /** * Class MigratorTest @@ -60,12 +58,10 @@ class MigratorTest extends \Test\TestCase { } private function getMigrator(): Migrator { - $platform = $this->connection->getDatabasePlatform(); - $random = \OC::$server->get(ISecureRandom::class); $dispatcher = \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class); - if ($platform instanceof SQLitePlatform) { + if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_SQLITE) { return new SQLiteMigrator($this->connection, $this->config, $dispatcher); - } elseif ($platform instanceof OraclePlatform) { + } elseif ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) { return new OracleMigrator($this->connection, $this->config, $dispatcher); } return new Migrator($this->connection, $this->config, $dispatcher); @@ -304,7 +300,7 @@ class MigratorTest extends \Test\TestCase { $migrator = $this->getMigrator(); $migrator->migrate($startSchema); - if ($oracleThrows && $this->connection->getDatabasePlatform() instanceof OraclePlatform) { + if ($oracleThrows && $this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) { // Oracle can not store false|empty string in notnull columns $this->expectException(\Doctrine\DBAL\Exception\NotNullConstraintViolationException::class); } diff --git a/tests/lib/DB/OCPostgreSqlPlatformTest.php b/tests/lib/DB/OCPostgreSqlPlatformTest.php index 4f83e866a7c..3ed420df501 100644 --- a/tests/lib/DB/OCPostgreSqlPlatformTest.php +++ b/tests/lib/DB/OCPostgreSqlPlatformTest.php @@ -7,7 +7,7 @@ namespace Test\DB; -use Doctrine\DBAL\Platforms\PostgreSQL100Platform; +use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Types\Types; @@ -24,7 +24,7 @@ use Doctrine\DBAL\Types\Types; */ class OCPostgreSqlPlatformTest extends \Test\TestCase { public function testAlterBigint() { - $platform = new PostgreSQL100Platform(); + $platform = new PostgreSQLPlatform(); $sourceSchema = new Schema(); $targetSchema = new Schema(); diff --git a/tests/lib/Files/Cache/CacheTest.php b/tests/lib/Files/Cache/CacheTest.php index faecbf54491..a36607eb965 100644 --- a/tests/lib/Files/Cache/CacheTest.php +++ b/tests/lib/Files/Cache/CacheTest.php @@ -7,12 +7,12 @@ namespace Test\Files\Cache; -use Doctrine\DBAL\Platforms\MySqlPlatform; use OC\Files\Cache\Cache; use OC\Files\Search\SearchComparison; use OC\Files\Search\SearchQuery; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Search\ISearchComparison; +use OCP\IDBConnection; use OCP\IUser; class LongId extends \OC\Files\Storage\Temporary { @@ -142,7 +142,7 @@ class CacheTest extends \Test\TestCase { if (strpos($folder, 'F09F9890')) { // 4 byte UTF doesn't work on mysql $params = \OC::$server->get(\OC\DB\Connection::class)->getParams(); - if (\OC::$server->getDatabaseConnection()->getDatabasePlatform() instanceof MySqlPlatform && $params['charset'] !== 'utf8mb4') { + if (\OC::$server->getDatabaseConnection()->getDatabaseProvider() === IDBConnection::PLATFORM_MYSQL && $params['charset'] !== 'utf8mb4') { $this->markTestSkipped('MySQL doesn\'t support 4 byte UTF-8'); } } diff --git a/tests/lib/Repair/RepairCollationTest.php b/tests/lib/Repair/RepairCollationTest.php index 5f5e0737f77..6d3946b2a85 100644 --- a/tests/lib/Repair/RepairCollationTest.php +++ b/tests/lib/Repair/RepairCollationTest.php @@ -7,7 +7,6 @@ namespace Test\Repair; -use Doctrine\DBAL\Platforms\MySqlPlatform; use OC\Repair\Collation; use OCP\IDBConnection; use OCP\Migration\IOutput; @@ -61,7 +60,7 @@ class RepairCollationTest extends TestCase { $this->connection = \OC::$server->get(IDBConnection::class); $this->logger = $this->createMock(LoggerInterface::class); $this->config = \OC::$server->getConfig(); - if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) { + if ($this->connection->getDatabaseProvider() !== IDBConnection::PLATFORM_MYSQL) { $this->markTestSkipped("Test only relevant on MySql"); }