Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to disable schema emulation on SQLite #5517

Merged
merged 1 commit into from
Jul 21, 2022
Merged
Changes from all commits
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
40 changes: 31 additions & 9 deletions src/Platforms/SqlitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
*/
class SqlitePlatform extends AbstractPlatform
{
private bool $schemaEmulationEnabled = true;

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -328,14 +330,34 @@ protected function _getCommonIntegerTypeDeclarationSQL(array $column)
return ! empty($column['unsigned']) ? ' UNSIGNED' : '';
}

/**
* Disables schema emulation.
*
* Schema emulation is enabled by default to maintain backwards compatibility.
mvorisek marked this conversation as resolved.
Show resolved Hide resolved
* Disable it to opt-in to the behavior of DBAL 4.
*
* @deprecated Will be removed in DBAL 4.0.
*/
public function disableSchemaEmulation(): void
{
$this->schemaEmulationEnabled = false;
}

private function emulateSchemaNamespacing(string $tableName): string
{
return $this->schemaEmulationEnabled
? str_replace('.', '__', $tableName)
: $tableName;
}

/**
* {@inheritDoc}
*/
public function getForeignKeyDeclarationSQL(ForeignKeyConstraint $foreignKey)
{
return parent::getForeignKeyDeclarationSQL(new ForeignKeyConstraint(
$foreignKey->getQuotedLocalColumns($this),
str_replace('.', '__', $foreignKey->getQuotedForeignTableName($this)),
$this->emulateSchemaNamespacing($foreignKey->getQuotedForeignTableName($this)),
$foreignKey->getQuotedForeignColumns($this),
$foreignKey->getName(),
$foreignKey->getOptions()
Expand All @@ -347,7 +369,7 @@ public function getForeignKeyDeclarationSQL(ForeignKeyConstraint $foreignKey)
*/
protected function _getCreateTableSQL($name, array $columns, array $options = [])
{
$name = str_replace('.', '__', $name);
mvorisek marked this conversation as resolved.
Show resolved Hide resolved
$name = $this->emulateSchemaNamespacing($name);
$queryFields = $this->getColumnDeclarationListSQL($columns);

if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
Expand Down Expand Up @@ -461,7 +483,7 @@ public function getClobTypeDeclarationSQL(array $column)
*/
public function getListTableConstraintsSQL($table)
{
$table = str_replace('.', '__', $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",
Expand All @@ -474,7 +496,7 @@ public function getListTableConstraintsSQL($table)
*/
public function getListTableColumnsSQL($table, $database = null)
{
$table = str_replace('.', '__', $table);
$table = $this->emulateSchemaNamespacing($table);

return sprintf('PRAGMA table_info(%s)', $this->quoteStringLiteral($table));
}
Expand All @@ -484,7 +506,7 @@ public function getListTableColumnsSQL($table, $database = null)
*/
public function getListTableIndexesSQL($table, $database = null)
{
$table = str_replace('.', '__', $table);
$table = $this->emulateSchemaNamespacing($table);

return sprintf('PRAGMA index_list(%s)', $this->quoteStringLiteral($table));
}
Expand Down Expand Up @@ -586,7 +608,7 @@ public function getName()
public function getTruncateTableSQL($tableName, $cascade = false)
{
$tableIdentifier = new Identifier($tableName);
$tableName = str_replace('.', '__', $tableIdentifier->getQuotedName($this));
$tableName = $this->emulateSchemaNamespacing($tableIdentifier->getQuotedName($this));

return 'DELETE FROM ' . $tableName;
}
Expand Down Expand Up @@ -792,7 +814,7 @@ public function getBlobTypeDeclarationSQL(array $column)
*/
public function getTemporaryTableName($tableName)
{
$tableName = str_replace('.', '__', $tableName);
$tableName = $this->emulateSchemaNamespacing($tableName);

return $tableName;
}
Expand All @@ -816,7 +838,7 @@ public function canEmulateSchemas()
'SqlitePlatform::canEmulateSchemas() is deprecated.'
);

return true;
return $this->schemaEmulationEnabled;
}

/**
Expand Down Expand Up @@ -881,7 +903,7 @@ public function getCreateTableSQL(Table $table, $createFlags = null)
*/
public function getListTableForeignKeysSQL($table, $database = null)
{
$table = str_replace('.', '__', $table);
$table = $this->emulateSchemaNamespacing($table);

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