Skip to content

Commit

Permalink
enh(db): provide database providers via API
Browse files Browse the repository at this point in the history
To avoid leaking internals (OC), wrap the getDatabasePlatform and provide the
associated constants

fixes #30877

Signed-off-by: Anna Larch <anna@nextcloud.com>
  • Loading branch information
miaulalala committed Sep 19, 2023
1 parent 8157d97 commit 56419d9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions apps/settings/lib/Controller/CheckSetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ protected function wasEmailTestSuccessful(): bool {

protected function hasValidTransactionIsolationLevel(): bool {
try {
if ($this->db->getDatabasePlatform() instanceof SqlitePlatform) {
if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_SQLITE) {
return true;
}

Expand Down Expand Up @@ -839,7 +839,7 @@ protected function hasBigIntConversionPendingColumns(): array {
];

$schema = new SchemaWrapper($this->db);
$isSqlite = $this->db->getDatabasePlatform() instanceof SqlitePlatform;
$isSqlite = $this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_SQLITE;
$pendingColumns = [];

foreach ($tables as $tableName => $columns) {
Expand Down
19 changes: 19 additions & 0 deletions lib/private/DB/ConnectionAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

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 @@ -242,4 +246,19 @@ public function migrateToSchema(Schema $toSchema): void {
public function getInner(): Connection {
return $this->inner;
}

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');
}
}
}
20 changes: 20 additions & 0 deletions lib/public/IDBConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@
* @since 6.0.0
*/
interface IDBConnection {
/* @since 28.0.0 */
public const PLATFORM_MYSQL = 'mysql';

/* @since 28.0.0 */
public const PLATFORM_ORACLE = 'oracle';

/* @since 28.0.0 */
public const PLATFORM_POSTGRES = 'postgres';

/* @since 28.0.0 */
public const PLATFORM_SQLITE = 'sqlite';

/**
* Gets the QueryBuilder for the connection.
*
Expand Down Expand Up @@ -339,4 +351,12 @@ public function createSchema(): Schema;
* @since 13.0.0
*/
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_*
*/
public function getDatabaseProvider(): string;
}

0 comments on commit 56419d9

Please sign in to comment.