Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix(db): Promote the use of getDatabaseProvider to reduce the impag…
…e of removed upstream platforms

Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Jul 19, 2024
commit 829f2b9bc7290c0b1b1a9db373ee26199c9bcc4d
3 changes: 1 addition & 2 deletions apps/dav/lib/Migration/CalDAVRemoveEmptyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,7 +74,7 @@ public function run(IOutput $output) {
}

protected function getInvalidObjects($pattern) {
if ($this->db->getDatabasePlatform() instanceof OraclePlatform) {
if ($this->db->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) {
$rows = [];
$chunkSize = 500;
$query = $this->db->getQueryBuilder();
Expand Down
4 changes: 1 addition & 3 deletions apps/settings/tests/SetupChecks/SupportedDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
namespace OCA\Settings\Tests;

use Doctrine\DBAL\Platforms\SqlitePlatform;
use OCA\Settings\SetupChecks\SupportedDatabase;
use OCP\IDBConnection;
use OCP\IL10N;
Expand Down Expand Up @@ -41,8 +40,7 @@ protected function setUp(): void {
}

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 {
Expand Down
4 changes: 2 additions & 2 deletions apps/user_ldap/lib/Mapping/AbstractMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -216,7 +216,7 @@ protected function collectResultsFromListOfIdsQuery(IQueryBuilder $qb, array &$r
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;
Expand Down
4 changes: 2 additions & 2 deletions core/Command/Db/ConvertFilecacheBigInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,7 +53,7 @@ public static function getColumnsByTable(): array {

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();
Expand Down
3 changes: 1 addition & 2 deletions core/Command/Db/ConvertMysqlToMB4.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,7 +33,7 @@ protected function configure() {
}

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;
}
Expand Down
3 changes: 1 addition & 2 deletions core/Migrations/Version13000Date20170718121200.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
namespace OC\Core\Migrations;

use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\IDBConnection;
Expand Down Expand Up @@ -238,7 +237,7 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op
$table->addIndex(['name'], 'fs_name_hash');
$table->addIndex(['mtime'], 'fs_mtime');
$table->addIndex(['size'], 'fs_size');
if (!$schema->getDatabasePlatform() instanceof PostgreSQL94Platform) {
if ($this->connection->getDatabaseProvider() !== IDBConnection::PLATFORM_POSTGRES) {
$table->addIndex(['storage', 'path'], 'fs_storage_path_prefix', [], ['lengths' => [null, 64]]);
}
}
Expand Down
5 changes: 2 additions & 3 deletions lib/private/AllConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
namespace OC;

use Doctrine\DBAL\Platforms\OraclePlatform;
use OCP\Cache\CappedMemoryCache;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
Expand Down Expand Up @@ -470,7 +469,7 @@ public function getUsersForUserValue($appName, $key, $value) {
$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')
Expand Down Expand Up @@ -509,7 +508,7 @@ public function getUsersForUserValueCaseInsensitive($appName, $key, $value) {
}

$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';

Expand Down
3 changes: 1 addition & 2 deletions lib/private/BackgroundJob/JobList.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/
namespace OC\BackgroundJob;

use Doctrine\DBAL\Platforms\MySQLPlatform;
use OCP\AppFramework\QueryException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\AutoloadNotAllowedException;
Expand Down Expand Up @@ -98,7 +97,7 @@ public function remove($job, $argument = null): void {

// 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;

Expand Down
20 changes: 20 additions & 0 deletions lib/private/DB/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Doctrine\DBAL\Exception\ConnectionLost;
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;
Expand All @@ -25,6 +26,7 @@
use OC\SystemConfig;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Diagnostics\IEventLogger;
use OCP\IDBConnection;
use OCP\IRequestId;
use OCP\PreConditionNotMetException;
use OCP\Profiler\IProfiler;
Expand Down Expand Up @@ -729,4 +731,22 @@ private function reconnectIfNeeded(): void {
private function getConnectionName(): string {
return $this->isConnectedToPrimary() ? 'primary' : 'replica';
}

/**
* @return IDBConnection::PLATFORM_MYSQL|IDBConnection::PLATFORM_ORACLE|IDBConnection::PLATFORM_POSTGRES|IDBConnection::PLATFORM_SQLITE
*/
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');
}
}
}
20 changes: 4 additions & 16 deletions lib/private/DB/ConnectionAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\Schema;
use OC\DB\Exceptions\DbalException;
use OCP\DB\IPreparedStatement;
Expand Down Expand Up @@ -230,18 +226,10 @@ public function getInner(): Connection {
return $this->inner;
}

/**
* @return self::PLATFORM_MYSQL|self::PLATFORM_ORACLE|self::PLATFORM_POSTGRES|self::PLATFORM_SQLITE

Check failure

Code scanning / Psalm

MoreSpecificReturnType

The declared return type ''mysql'|'oracle'|'postgres'|'sqlite'' for OC\DB\ConnectionAdapter::getDatabaseProvider is more specific than the inferred return type 'string'
*/
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();

Check failure

Code scanning / Psalm

LessSpecificReturnStatement

The type 'string' is more general than the declared return type ''mysql'|'oracle'|'postgres'|'sqlite'' for OC\DB\ConnectionAdapter::getDatabaseProvider
}
}
9 changes: 4 additions & 5 deletions lib/private/DB/MigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@
*/
namespace OC\DB;

use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaException;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Types;
use OC\App\InfoParser;
use OC\IntegrityCheck\Helpers\AppLocator;
use OC\Migration\SimpleOutput;
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;
Expand Down Expand Up @@ -599,7 +598,7 @@ public function ensureOracleConstraints(Schema $sourceSchema, Schema $targetSche
$indexName = strtolower($primaryKey->getName());
$isUsingDefaultName = $indexName === 'primary';

if ($this->connection->getDatabasePlatform() instanceof PostgreSQL94Platform) {
if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_POSTGRES) {
$defaultName = $table->getName() . '_pkey';
$isUsingDefaultName = strtolower($defaultName) === $indexName;

Expand All @@ -609,7 +608,7 @@ public function ensureOracleConstraints(Schema $sourceSchema, Schema $targetSche
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;
}
Expand Down
42 changes: 12 additions & 30 deletions lib/private/DB/QueryBuilder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
*/
namespace OC\DB\QueryBuilder;

use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Query\QueryException;
use OC\DB\ConnectionAdapter;
use OC\DB\Exceptions\DbalException;
use OC\DB\QueryBuilder\ExpressionBuilder\ExpressionBuilder;
use OC\DB\QueryBuilder\ExpressionBuilder\MySqlExpressionBuilder;
use OC\DB\QueryBuilder\ExpressionBuilder\OCIExpressionBuilder;
use OC\DB\QueryBuilder\ExpressionBuilder\PgSqlExpressionBuilder;
Expand Down Expand Up @@ -96,20 +91,12 @@ public function automaticTablePrefix($enabled) {
* @return \OCP\DB\QueryBuilder\IExpressionBuilder
*/
public function expr() {
if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) {
return new OCIExpressionBuilder($this->connection, $this);
}
if ($this->connection->getDatabasePlatform() instanceof PostgreSQL94Platform) {
return new PgSqlExpressionBuilder($this->connection, $this);
}
if ($this->connection->getDatabasePlatform() instanceof MySQLPlatform) {
return new MySqlExpressionBuilder($this->connection, $this);
}
if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
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),
};
}

/**
Expand All @@ -129,17 +116,12 @@ public function expr() {
* @return \OCP\DB\QueryBuilder\IFunctionBuilder
*/
public function func() {
if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) {
return new OCIFunctionBuilder($this->connection, $this, $this->helper);
}
if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
return new SqliteFunctionBuilder($this->connection, $this, $this->helper);
}
if ($this->connection->getDatabasePlatform() instanceof PostgreSQL94Platform) {
return new PgSqlFunctionBuilder($this->connection, $this, $this->helper);
}

return new FunctionBuilder($this->connection, $this, $this->helper);
return match($this->connection->getDatabaseProvider()) {
IDBConnection::PLATFORM_ORACLE => new OCIFunctionBuilder($this->connection, $this, $this->helper),
IDBConnection::PLATFORM_POSTGRES => new PgSqlFunctionBuilder($this->connection, $this, $this->helper),
IDBConnection::PLATFORM_MYSQL => new FunctionBuilder($this->connection, $this, $this->helper),
IDBConnection::PLATFORM_SQLITE => new SqliteFunctionBuilder($this->connection, $this, $this->helper),
};
}

/**
Expand Down
3 changes: 1 addition & 2 deletions lib/private/Repair/Collation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,7 +49,7 @@ public function getName() {
* 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;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/public/IDBConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ public function quote($input, $type = IQueryBuilder::PARAM_STR);
*
* @return \Doctrine\DBAL\Platforms\AbstractPlatform The database platform.
* @since 8.0.0
* @deprecated 30.0.0 Please use {@see self::getDatabaseProvider()} and compare to self::PLATFORM_* constants
*/
public function getDatabasePlatform();

Expand Down Expand Up @@ -341,7 +342,7 @@ public function migrateToSchema(Schema $toSchema): void;
* 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;
}
Loading