Skip to content

Commit

Permalink
Fix SqlitePlatform to support quoted table name (#1025)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jul 12, 2022
1 parent 704a827 commit 899bb84
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 29 deletions.
4 changes: 2 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ parameters:

# fix https://github.com/phpstan/phpstan-deprecation-rules/issues/52 and https://github.com/phpstan/phpstan/issues/6444
-
message: '~^Call to method (getVarcharTypeDeclarationSQL|getClobTypeDeclarationSQL|getCreateTableSQL|getCurrentDatabaseExpression)\(\) of deprecated class Doctrine\\DBAL\\Platforms\\(PostgreSQLPlatform|SQLServerPlatform|AbstractPlatform):\nUse.+instead\.$~'
message: '~^Call to method (getVarcharTypeDeclarationSQL|getClobTypeDeclarationSQL|getCreateTableSQL|getCurrentDatabaseExpression|initializeDoctrineTypeMappings)\(\) of deprecated class Doctrine\\DBAL\\Platforms\\(PostgreSQLPlatform|SQLServerPlatform|AbstractPlatform):\nUse.+instead\.$~'
path: '*'
count: 5
count: 6

# TODO these rules are generated, this ignores should be fixed in the code
# for src/Schema/TestCase.php
Expand Down
17 changes: 10 additions & 7 deletions src/Persistence/Sql/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Result as DbalResult;

Expand Down Expand Up @@ -253,23 +254,25 @@ protected static function connectFromDbalDriverConnection(DbalDriverConnection $
$dbalConnection->_conn = $dbalDriverConnection;
}, null, \Doctrine\DBAL\Connection::class)();

if ($dbalConnection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
if ($dbalConnection->getDatabasePlatform() instanceof SqlitePlatform) {
\Closure::bind(function () use ($dbalConnection) {
$dbalConnection->platform = new class() extends SqlitePlatform {
use Sqlite\PlatformTrait;
};
}, null, DbalConnection::class)();
} elseif ($dbalConnection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
\Closure::bind(function () use ($dbalConnection) {
$dbalConnection->platform = new class() extends \Doctrine\DBAL\Platforms\PostgreSQL94Platform { // @phpstan-ignore-line
use Postgresql\PlatformTrait;
};
}, null, DbalConnection::class)();
}

if ($dbalConnection->getDatabasePlatform() instanceof SQLServerPlatform) {
} elseif ($dbalConnection->getDatabasePlatform() instanceof SQLServerPlatform) {
\Closure::bind(function () use ($dbalConnection) {
$dbalConnection->platform = new class() extends \Doctrine\DBAL\Platforms\SQLServer2012Platform { // @phpstan-ignore-line
use Mssql\PlatformTrait;
};
}, null, DbalConnection::class)();
}

if ($dbalConnection->getDatabasePlatform() instanceof OraclePlatform) {
} elseif ($dbalConnection->getDatabasePlatform() instanceof OraclePlatform) {
\Closure::bind(function () use ($dbalConnection) {
$dbalConnection->platform = new class() extends OraclePlatform {
use Oracle\PlatformTrait;
Expand Down
8 changes: 8 additions & 0 deletions src/Persistence/Sql/Postgresql/PlatformTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public function getClobTypeDeclarationSQL(array $column)
return 'CITEXT';
}

protected function initializeDoctrineTypeMappings(): void
{
parent::initializeDoctrineTypeMappings();

// https://github.com/doctrine/dbal/pull/5495
$this->doctrineTypeMapping['citext'] = 'text';
}

public function getCurrentDatabaseExpression(bool $includeSchema = false): string
{
if ($includeSchema) {
Expand Down
38 changes: 38 additions & 0 deletions src/Persistence/Sql/Sqlite/PlatformTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Atk4\Data\Persistence\Sql\Sqlite;

use Doctrine\DBAL\Schema\Identifier;

trait PlatformTrait
{
// fix quoted table name support
// TODO submit a PR with fixed SqlitePlatform to DBAL

private function unquoteTableIdentifier(string $tableName): string
{
return (new Identifier($tableName))->getName();
}

public function getListTableConstraintsSQL($table)
{
return parent::getListTableConstraintsSQL($this->unquoteTableIdentifier($table));
}

public function getListTableColumnsSQL($table, $database = null)
{
return parent::getListTableColumnsSQL($this->unquoteTableIdentifier($table), $database);
}

public function getListTableIndexesSQL($table, $database = null)
{
return parent::getListTableIndexesSQL($this->unquoteTableIdentifier($table), $database);
}

public function getListTableForeignKeysSQL($table, $database = null)
{
return parent::getListTableForeignKeysSQL($this->unquoteTableIdentifier($table), $database);
}
}
5 changes: 1 addition & 4 deletions src/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,9 @@ public function refModel(Model $ourModel, array $defaults = []): Model
return $this->createTheirModel($defaults);
}

/** @var array<int|string, string> List of properties to show in var_dump. */
/** @var array<int|string, string> */
protected $__debug_fields = ['link', 'model', 'our_field', 'their_field'];

/**
* Returns array with useful debug info for var_dump.
*/
public function __debugInfo(): array
{
$arr = [];
Expand Down
4 changes: 2 additions & 2 deletions tests/Schema/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Schema\Identifier as DbalIdentifier;
use Doctrine\DBAL\Schema\Identifier;

class MigratorTest extends TestCase
{
Expand All @@ -37,7 +37,7 @@ protected function createDemoMigrator(string $table): Migrator
protected function isTableExist(string $table): bool
{
foreach ($this->createSchemaManager()->listTableNames() as $v) {
$vUnquoted = (new DbalIdentifier($v))->getName();
$vUnquoted = (new Identifier($v))->getName();
if ($vUnquoted === $table) {
return true;
}
Expand Down
50 changes: 36 additions & 14 deletions tests/Schema/TestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,44 @@

class TestCaseTest extends TestCase
{
public function testInit(): void
public function testGetSetDb(): void
{
$this->setDb($q = [
$this->assertSame([], $this->getDb([]));
$this->assertSame([], $this->getDb());

$dbData = [
'user' => [
['name' => 'John', 'surname' => 'Smith'],
['name' => 'Steve', 'surname' => 'Jobs'],
['name' => 'John', 'age' => '25'],
['name' => 'Steve', 'age' => '30'],
],
]);

$q2 = $this->getDb(['user']);

$this->setDb($q2);
$q3 = $this->getDb(['user']);

$this->assertSameExportUnordered($q2, $q3);

$this->assertSameExportUnordered($q, $this->getDb(['user'], true));
];
$dbDataWithId = array_map(function ($rows) {
$rowsWithId = [];
$id = 1;
foreach ($rows as $row) {
$rowsWithId[$id] = array_merge(['id' => (string) $id], $row);
++$id;
}

return $rowsWithId;
}, $dbData);

$this->setDb($dbData);
$dbDataGet1 = $this->getDb(['user']);
$this->assertSameExportUnordered($dbDataWithId, $dbDataGet1);
$this->assertSameExportUnordered($dbDataWithId, $this->getDb());
$this->assertSameExportUnordered($dbData, $this->getDb(null, true));

$this->setDb($dbData);
$dbDataGet2 = $this->getDb(['user']);
$this->assertSameExportUnordered($dbDataWithId, $dbDataGet2);
$this->assertSameExportUnordered($dbDataWithId, $this->getDb());
$this->assertSame($dbDataGet1, $dbDataGet2);

$this->setDb($dbDataGet1);
$dbDataGet3 = $this->getDb(['user']);
$this->assertSameExportUnordered($dbDataWithId, $dbDataGet3);
$this->assertSameExportUnordered($dbDataWithId, $this->getDb());
$this->assertSame($dbDataGet1, $dbDataGet3);
}
}

0 comments on commit 899bb84

Please sign in to comment.