Skip to content

Commit

Permalink
backport DBAL 5517
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jul 21, 2022
1 parent 4d91d92 commit 4f7c4fe
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Persistence/Sql/Sqlite/PlatformTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

trait PlatformTrait
{
use PlatformTraitBackport5517 {
PlatformTraitBackport5517::getListTableConstraintsSQL as private __getListTableConstraintsSQL;
PlatformTraitBackport5517::getListTableColumnsSQL as private __getListTableColumnsSQL;
PlatformTraitBackport5517::getListTableIndexesSQL as private __getListTableIndexesSQL;
PlatformTraitBackport5517::getListTableForeignKeysSQL as private __getListTableForeignKeysSQL;
}

public function supportsForeignKeyConstraints(): bool
{
// backport https://github.com/doctrine/dbal/pull/5427, remove once DBAL 3.3.x support is dropped
Expand All @@ -32,22 +39,22 @@ private function unquoteTableIdentifier(string $tableName): string

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

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

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

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

public function getAlterTableSQL(TableDiff $diff): array
Expand Down
123 changes: 123 additions & 0 deletions src/Persistence/Sql/Sqlite/PlatformTraitBackport5517.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

declare(strict_types=1);

namespace Atk4\Data\Persistence\Sql\Sqlite;

use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Identifier;

/**
* Drop once https://github.com/doctrine/dbal/pull/5517 is merged and DBAL 3.3.x support is removed.
*/
trait PlatformTraitBackport5517
{
private function emulateSchemaNamespacing(string $tableName): string
{
return $tableName;
}

public function getForeignKeyDeclarationSQL(ForeignKeyConstraint $foreignKey)
{
return parent::getForeignKeyDeclarationSQL(new ForeignKeyConstraint(
$foreignKey->getQuotedLocalColumns($this),
$this->emulateSchemaNamespacing($foreignKey->getQuotedForeignTableName($this)),
$foreignKey->getQuotedForeignColumns($this),
$foreignKey->getName(),
$foreignKey->getOptions()
));
}

protected function _getCreateTableSQL($name, array $columns, array $options = [])
{
$name = $this->emulateSchemaNamespacing($name);
$queryFields = $this->getColumnDeclarationListSQL($columns);

if (isset($options['uniqueConstraints']) && !empty($options['uniqueConstraints'])) {
foreach ($options['uniqueConstraints'] as $constraintName => $definition) {
$queryFields .= ', ' . $this->getUniqueConstraintDeclarationSQL($constraintName, $definition);
}
}

$queryFields .= \Closure::bind(fn () => $this->getNonAutoincrementPrimaryKeyDefinition($columns, $options), $this, parent::class)();

if (isset($options['foreignKeys'])) {
foreach ($options['foreignKeys'] as $foreignKey) {
$queryFields .= ', ' . $this->getForeignKeyDeclarationSQL($foreignKey);
}
}

$tableComment = '';
if (isset($options['comment'])) {
$comment = trim($options['comment'], " '");

$tableComment = \Closure::bind(fn () => $this->getInlineTableCommentSQL($comment), $this, parent::class)();
}

$query = ['CREATE TABLE ' . $name . ' ' . $tableComment . '(' . $queryFields . ')'];

if (isset($options['alter']) && $options['alter'] === true) {
return $query;
}

if (isset($options['indexes']) && !empty($options['indexes'])) {
foreach ($options['indexes'] as $indexDef) {
$query[] = $this->getCreateIndexSQL($indexDef, $name);
}
}

if (isset($options['unique']) && !empty($options['unique'])) {
foreach ($options['unique'] as $indexDef) {
$query[] = $this->getCreateIndexSQL($indexDef, $name);
}
}

return $query;
}

public function getListTableConstraintsSQL($table)
{
$table = $this->emulateSchemaNamespacing($table);

return sprintf(
"SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name = %s AND sql NOT NULL ORDER BY name",
$this->quoteStringLiteral($table)
);
}

public function getListTableColumnsSQL($table, $database = null)
{
$table = $this->emulateSchemaNamespacing($table);

return sprintf('PRAGMA table_info(%s)', $this->quoteStringLiteral($table));
}

public function getListTableIndexesSQL($table, $database = null)
{
$table = $this->emulateSchemaNamespacing($table);

return sprintf('PRAGMA index_list(%s)', $this->quoteStringLiteral($table));
}

public function getTruncateTableSQL($tableName, $cascade = false)
{
$tableIdentifier = new Identifier($tableName);
$tableName = $this->emulateSchemaNamespacing($tableIdentifier->getQuotedName($this));

return 'DELETE FROM ' . $tableName;
}

public function getTemporaryTableName($tableName)
{
$tableName = $this->emulateSchemaNamespacing($tableName);

return $tableName;
}

public function getListTableForeignKeysSQL($table, $database = null)
{
$table = $this->emulateSchemaNamespacing($table);

return sprintf('PRAGMA foreign_key_list(%s)', $this->quoteStringLiteral($table));
}
}

0 comments on commit 4f7c4fe

Please sign in to comment.