updated-packages

This commit is contained in:
RafficMohammed
2023-01-08 00:13:22 +05:30
parent 3ff7df7487
commit da241bacb6
12659 changed files with 563377 additions and 510538 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -2,13 +2,14 @@
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use function array_merge;
use function count;
use function current;
@@ -22,10 +23,7 @@ use function strtoupper;
class DB2Platform extends AbstractPlatform
{
/**
* {@inheritdoc}
*/
public function getCharMaxLength() : int
public function getCharMaxLength(): int
{
return 254;
}
@@ -49,22 +47,22 @@ class DB2Platform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getVarcharTypeDeclarationSQL(array $field)
public function getVarcharTypeDeclarationSQL(array $column)
{
// for IBM DB2, the CHAR max length is less than VARCHAR default length
if (! isset($field['length']) && ! empty($field['fixed'])) {
$field['length'] = $this->getCharMaxLength();
if (! isset($column['length']) && ! empty($column['fixed'])) {
$column['length'] = $this->getCharMaxLength();
}
return parent::getVarcharTypeDeclarationSQL($field);
return parent::getVarcharTypeDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
public function getBlobTypeDeclarationSQL(array $field)
public function getBlobTypeDeclarationSQL(array $column)
{
// todo blob(n) with $field['length'];
// todo blob(n) with $column['length'];
return 'BLOB(1M)';
}
@@ -97,7 +95,7 @@ class DB2Platform extends AbstractPlatform
*/
public function isCommentedDoctrineType(Type $doctrineType)
{
if ($doctrineType->getName() === Type::BOOLEAN) {
if ($doctrineType->getName() === Types::BOOLEAN) {
// We require a commented boolean type in order to distinguish between boolean and smallint
// as both (have to) map to the same native type.
return true;
@@ -126,9 +124,9 @@ class DB2Platform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getClobTypeDeclarationSQL(array $field)
public function getClobTypeDeclarationSQL(array $column)
{
// todo clob(n) with $field['length'];
// todo clob(n) with $column['length'];
return 'CLOB(1M)';
}
@@ -143,7 +141,7 @@ class DB2Platform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getBooleanTypeDeclarationSQL(array $columnDef)
public function getBooleanTypeDeclarationSQL(array $column)
{
return 'SMALLINT';
}
@@ -151,34 +149,34 @@ class DB2Platform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getIntegerTypeDeclarationSQL(array $columnDef)
public function getIntegerTypeDeclarationSQL(array $column)
{
return 'INTEGER' . $this->_getCommonIntegerTypeDeclarationSQL($columnDef);
return 'INTEGER' . $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
public function getBigIntTypeDeclarationSQL(array $columnDef)
public function getBigIntTypeDeclarationSQL(array $column)
{
return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($columnDef);
return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
public function getSmallIntTypeDeclarationSQL(array $columnDef)
public function getSmallIntTypeDeclarationSQL(array $column)
{
return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($columnDef);
return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
protected function _getCommonIntegerTypeDeclarationSQL(array $column)
{
$autoinc = '';
if (! empty($columnDef['autoincrement'])) {
if (! empty($column['autoincrement'])) {
$autoinc = ' GENERATED BY DEFAULT AS IDENTITY';
}
@@ -232,9 +230,9 @@ class DB2Platform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTimeTypeDeclarationSQL(array $column)
{
if (isset($fieldDeclaration['version']) && $fieldDeclaration['version'] === true) {
if (isset($column['version']) && $column['version'] === true) {
return 'TIMESTAMP(0) WITH DEFAULT';
}
@@ -244,7 +242,7 @@ class DB2Platform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getDateTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTypeDeclarationSQL(array $column)
{
return 'DATE';
}
@@ -252,7 +250,7 @@ class DB2Platform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getTimeTypeDeclarationSQL(array $column)
{
return 'TIME';
}
@@ -342,7 +340,7 @@ class DB2Platform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getListTableIndexesSQL($table, $currentDatabase = null)
public function getListTableIndexesSQL($table, $database = null)
{
$table = $this->quoteStringLiteral($table);
@@ -416,17 +414,17 @@ class DB2Platform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getCreateDatabaseSQL($database)
public function getCreateDatabaseSQL($name)
{
return 'CREATE DATABASE ' . $database;
return 'CREATE DATABASE ' . $name;
}
/**
* {@inheritDoc}
*/
public function getDropDatabaseSQL($database)
public function getDropDatabaseSQL($name)
{
return 'DROP DATABASE ' . $database;
return 'DROP DATABASE ' . $name;
}
/**
@@ -483,25 +481,27 @@ class DB2Platform extends AbstractPlatform
public function getIndexDeclarationSQL($name, Index $index)
{
// Index declaration in statements like CREATE TABLE is not supported.
throw DBALException::notSupported(__METHOD__);
throw Exception::notSupported(__METHOD__);
}
/**
* {@inheritDoc}
*/
protected function _getCreateTableSQL($tableName, array $columns, array $options = [])
protected function _getCreateTableSQL($name, array $columns, array $options = [])
{
$indexes = [];
if (isset($options['indexes'])) {
$indexes = $options['indexes'];
}
$options['indexes'] = [];
$sqls = parent::_getCreateTableSQL($tableName, $columns, $options);
$sqls = parent::_getCreateTableSQL($name, $columns, $options);
foreach ($indexes as $definition) {
$sqls[] = $this->getCreateIndexSQL($definition, $tableName);
$sqls[] = $this->getCreateIndexSQL($definition, $name);
}
return $sqls;
}
@@ -524,7 +524,8 @@ class DB2Platform extends AbstractPlatform
$queryPart = 'ADD COLUMN ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnDef);
// Adding non-nullable columns to a table requires a default value to be specified.
if (! empty($columnDef['notnull']) &&
if (
! empty($columnDef['notnull']) &&
! isset($columnDef['default']) &&
empty($columnDef['autoincrement'])
) {
@@ -571,7 +572,7 @@ class DB2Platform extends AbstractPlatform
}
}
$this->gatherAlterColumnSQL($diff->fromTable, $columnDiff, $sql, $queryParts);
$this->gatherAlterColumnSQL($diff->getName($this), $columnDiff, $sql, $queryParts);
}
foreach ($diff->renamedColumns as $oldColumnName => $column) {
@@ -599,8 +600,14 @@ class DB2Platform extends AbstractPlatform
$sql = array_merge($sql, $commentsSQL);
if ($diff->newName !== false) {
$sql[] = 'RENAME TABLE ' . $diff->getName($this)->getQuotedName($this) . ' TO ' . $diff->getNewName()->getQuotedName($this);
$newName = $diff->getNewName();
if ($newName !== false) {
$sql[] = sprintf(
'RENAME TABLE %s TO %s',
$diff->getName($this)->getQuotedName($this),
$newName->getQuotedName($this)
);
}
$sql = array_merge(
@@ -616,13 +623,17 @@ class DB2Platform extends AbstractPlatform
/**
* Gathers the table alteration SQL for a given column diff.
*
* @param Table $table The table to gather the SQL for.
* @param Identifier $table The table to gather the SQL for.
* @param ColumnDiff $columnDiff The column diff to evaluate.
* @param string[] $sql The sequence of table alteration statements to fill.
* @param mixed[] $queryParts The sequence of column alteration clauses to fill.
*/
private function gatherAlterColumnSQL(Table $table, ColumnDiff $columnDiff, array &$sql, array &$queryParts)
{
private function gatherAlterColumnSQL(
Identifier $table,
ColumnDiff $columnDiff,
array &$sql,
array &$queryParts
): void {
$alterColumnClauses = $this->getAlterColumnClausesSQL($columnDiff);
if (empty($alterColumnClauses)) {
@@ -663,7 +674,8 @@ class DB2Platform extends AbstractPlatform
$clauses = [];
if ($columnDiff->hasChanged('type') ||
if (
$columnDiff->hasChanged('type') ||
$columnDiff->hasChanged('length') ||
$columnDiff->hasChanged('precision') ||
$columnDiff->hasChanged('scale') ||
@@ -701,21 +713,23 @@ class DB2Platform extends AbstractPlatform
foreach ($diff->removedIndexes as $remKey => $remIndex) {
foreach ($diff->addedIndexes as $addKey => $addIndex) {
if ($remIndex->getColumns() === $addIndex->getColumns()) {
if ($remIndex->isPrimary()) {
$sql[] = 'ALTER TABLE ' . $table . ' DROP PRIMARY KEY';
} elseif ($remIndex->isUnique()) {
$sql[] = 'ALTER TABLE ' . $table . ' DROP UNIQUE ' . $remIndex->getQuotedName($this);
} else {
$sql[] = $this->getDropIndexSQL($remIndex, $table);
}
$sql[] = $this->getCreateIndexSQL($addIndex, $table);
unset($diff->removedIndexes[$remKey], $diff->addedIndexes[$addKey]);
break;
if ($remIndex->getColumns() !== $addIndex->getColumns()) {
continue;
}
if ($remIndex->isPrimary()) {
$sql[] = 'ALTER TABLE ' . $table . ' DROP PRIMARY KEY';
} elseif ($remIndex->isUnique()) {
$sql[] = 'ALTER TABLE ' . $table . ' DROP UNIQUE ' . $remIndex->getQuotedName($this);
} else {
$sql[] = $this->getDropIndexSQL($remIndex, $table);
}
$sql[] = $this->getCreateIndexSQL($addIndex, $table);
unset($diff->removedIndexes[$remKey], $diff->addedIndexes[$addKey]);
break;
}
}
@@ -740,27 +754,27 @@ class DB2Platform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getDefaultValueDeclarationSQL($field)
public function getDefaultValueDeclarationSQL($column)
{
if (! empty($field['autoincrement'])) {
if (! empty($column['autoincrement'])) {
return '';
}
if (isset($field['version']) && $field['version']) {
if ((string) $field['type'] !== 'DateTime') {
$field['default'] = '1';
if (isset($column['version']) && $column['version']) {
if ((string) $column['type'] !== 'DateTime') {
$column['default'] = '1';
}
}
return parent::getDefaultValueDeclarationSQL($field);
return parent::getDefaultValueDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
public function getEmptyIdentityInsertSQL($tableName, $identifierColumnName)
public function getEmptyIdentityInsertSQL($quotedTableName, $quotedIdentifierColumnName)
{
return 'INSERT INTO ' . $tableName . ' (' . $identifierColumnName . ') VALUES (DEFAULT)';
return 'INSERT INTO ' . $quotedTableName . ' (' . $quotedIdentifierColumnName . ') VALUES (DEFAULT)';
}
/**
@@ -821,13 +835,13 @@ class DB2Platform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getSubstringExpression($value, $from, $length = null)
public function getSubstringExpression($string, $start, $length = null)
{
if ($length === null) {
return 'SUBSTR(' . $value . ', ' . $from . ')';
return 'SUBSTR(' . $string . ', ' . $start . ')';
}
return 'SUBSTR(' . $value . ', ' . $from . ', ' . $length . ')';
return 'SUBSTR(' . $string . ', ' . $start . ', ' . $length . ')';
}
/**
@@ -850,6 +864,8 @@ class DB2Platform extends AbstractPlatform
* {@inheritDoc}
*
* DB2 returns all column names in SQL result sets in uppercase.
*
* @deprecated
*/
public function getSQLResultCasing($column)
{
@@ -893,4 +909,17 @@ class DB2Platform extends AbstractPlatform
{
return Keywords\DB2Keywords::class;
}
public function getListTableCommentsSQL(string $table): string
{
return sprintf(
<<<'SQL'
SELECT REMARKS
FROM SYSIBM.SYSTABLES
WHERE NAME = UPPER( %s )
SQL
,
$this->quoteStringLiteral($table)
);
}
}

View File

@@ -22,6 +22,9 @@ final class DateIntervalUnit
public const YEAR = 'YEAR';
/**
* @codeCoverageIgnore
*/
private function __construct()
{
}

View File

@@ -8,6 +8,7 @@ use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\BinaryType;
use InvalidArgumentException;
use function array_merge;
use function array_unique;
use function array_values;
@@ -23,6 +24,8 @@ use function trim;
/**
* Drizzle platform
*
* @deprecated
*/
class DrizzlePlatform extends AbstractPlatform
{
@@ -47,9 +50,7 @@ class DrizzlePlatform extends AbstractPlatform
*/
public function getConcatExpression()
{
$args = func_get_args();
return 'CONCAT(' . implode(', ', (array) $args) . ')';
return 'CONCAT(' . implode(', ', func_get_args()) . ')';
}
/**
@@ -73,7 +74,7 @@ class DrizzlePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getBooleanTypeDeclarationSQL(array $field)
public function getBooleanTypeDeclarationSQL(array $column)
{
return 'BOOLEAN';
}
@@ -81,18 +82,18 @@ class DrizzlePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getIntegerTypeDeclarationSQL(array $field)
public function getIntegerTypeDeclarationSQL(array $column)
{
return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
protected function _getCommonIntegerTypeDeclarationSQL(array $column)
{
$autoinc = '';
if (! empty($columnDef['autoincrement'])) {
if (! empty($column['autoincrement'])) {
$autoinc = ' AUTO_INCREMENT';
}
@@ -102,17 +103,17 @@ class DrizzlePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getBigIntTypeDeclarationSQL(array $field)
public function getBigIntTypeDeclarationSQL(array $column)
{
return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
public function getSmallIntTypeDeclarationSQL(array $field)
public function getSmallIntTypeDeclarationSQL(array $column)
{
return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
@@ -156,7 +157,7 @@ class DrizzlePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getClobTypeDeclarationSQL(array $field)
public function getClobTypeDeclarationSQL(array $column)
{
return 'TEXT';
}
@@ -164,7 +165,7 @@ class DrizzlePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getBlobTypeDeclarationSQL(array $field)
public function getBlobTypeDeclarationSQL(array $column)
{
return 'BLOB';
}
@@ -188,7 +189,7 @@ class DrizzlePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
protected function _getCreateTableSQL($tableName, array $columns, array $options = [])
protected function _getCreateTableSQL($name, array $columns, array $options = [])
{
$queryFields = $this->getColumnDeclarationListSQL($columns);
@@ -217,7 +218,7 @@ class DrizzlePlatform extends AbstractPlatform
$query .= 'TEMPORARY ';
}
$query .= 'TABLE ' . $tableName . ' (' . $queryFields . ') ';
$query .= 'TABLE ' . $name . ' (' . $queryFields . ') ';
$query .= $this->buildTableOptions($options);
$query .= $this->buildPartitionOptions($options);
@@ -225,7 +226,7 @@ class DrizzlePlatform extends AbstractPlatform
if (isset($options['foreignKeys'])) {
foreach ((array) $options['foreignKeys'] as $definition) {
$sql[] = $this->getCreateForeignKeySQL($definition, $tableName);
$sql[] = $this->getCreateForeignKeySQL($definition, $name);
}
}
@@ -330,14 +331,17 @@ class DrizzlePlatform extends AbstractPlatform
$databaseSQL = 'DATABASE()';
}
return 'SELECT COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT, IS_NULLABLE, IS_AUTO_INCREMENT, CHARACTER_MAXIMUM_LENGTH, COLUMN_DEFAULT,' .
' NUMERIC_PRECISION, NUMERIC_SCALE, COLLATION_NAME' .
return 'SELECT COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT, IS_NULLABLE, IS_AUTO_INCREMENT,' .
' CHARACTER_MAXIMUM_LENGTH, COLUMN_DEFAULT, NUMERIC_PRECISION, NUMERIC_SCALE, COLLATION_NAME' .
' FROM DATA_DICTIONARY.COLUMNS' .
' WHERE TABLE_SCHEMA=' . $databaseSQL . ' AND TABLE_NAME = ' . $this->quoteStringLiteral($table);
}
/**
* {@inheritDoc}
* @param string $table
* @param string|null $database
*
* @return string
*/
public function getListTableForeignKeysSQL($table, $database = null)
{
@@ -347,9 +351,11 @@ class DrizzlePlatform extends AbstractPlatform
$databaseSQL = 'DATABASE()';
}
return 'SELECT CONSTRAINT_NAME, CONSTRAINT_COLUMNS, REFERENCED_TABLE_NAME, REFERENCED_TABLE_COLUMNS, UPDATE_RULE, DELETE_RULE' .
' FROM DATA_DICTIONARY.FOREIGN_KEYS' .
' WHERE CONSTRAINT_SCHEMA=' . $databaseSQL . ' AND CONSTRAINT_TABLE=' . $this->quoteStringLiteral($table);
return 'SELECT CONSTRAINT_NAME, CONSTRAINT_COLUMNS, REFERENCED_TABLE_NAME, REFERENCED_TABLE_COLUMNS,'
. ' UPDATE_RULE, DELETE_RULE'
. ' FROM DATA_DICTIONARY.FOREIGN_KEYS'
. ' WHERE CONSTRAINT_SCHEMA=' . $databaseSQL
. ' AND CONSTRAINT_TABLE=' . $this->quoteStringLiteral($table);
}
/**
@@ -363,9 +369,12 @@ class DrizzlePlatform extends AbstractPlatform
$databaseSQL = 'DATABASE()';
}
return "SELECT INDEX_NAME AS 'key_name', COLUMN_NAME AS 'column_name', IS_USED_IN_PRIMARY AS 'primary', IS_UNIQUE=0 AS 'non_unique'" .
' FROM DATA_DICTIONARY.INDEX_PARTS' .
' WHERE TABLE_SCHEMA=' . $databaseSQL . ' AND TABLE_NAME=' . $this->quoteStringLiteral($table);
return "SELECT INDEX_NAME AS 'key_name',"
. " COLUMN_NAME AS 'column_name',"
. " IS_USED_IN_PRIMARY AS 'primary',"
. " IS_UNIQUE=0 AS 'non_unique'"
. ' FROM DATA_DICTIONARY.INDEX_PARTS'
. ' WHERE TABLE_SCHEMA=' . $databaseSQL . ' AND TABLE_NAME=' . $this->quoteStringLiteral($table);
}
/**
@@ -418,13 +427,17 @@ class DrizzlePlatform extends AbstractPlatform
} elseif (is_string($index)) {
$indexName = $index;
} else {
throw new InvalidArgumentException('DrizzlePlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
throw new InvalidArgumentException(
__METHOD__ . '() expects $index parameter to be string or ' . Index::class . '.'
);
}
if ($table instanceof Table) {
$table = $table->getQuotedName($this);
} elseif (! is_string($table)) {
throw new InvalidArgumentException('DrizzlePlatform::getDropIndexSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
throw new InvalidArgumentException(
__METHOD__ . '() expects $table parameter to be string or ' . Table::class . '.'
);
}
if ($index instanceof Index && $index->isPrimary()) {
@@ -437,7 +450,9 @@ class DrizzlePlatform extends AbstractPlatform
}
/**
* {@inheritDoc}
* @param string $table
*
* @return string
*/
protected function getDropPrimaryKeySQL($table)
{
@@ -447,9 +462,9 @@ class DrizzlePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTimeTypeDeclarationSQL(array $column)
{
if (isset($fieldDeclaration['version']) && $fieldDeclaration['version'] === true) {
if (isset($column['version']) && $column['version'] === true) {
return 'TIMESTAMP';
}
@@ -459,7 +474,7 @@ class DrizzlePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getTimeTypeDeclarationSQL(array $column)
{
return 'TIME';
}
@@ -467,7 +482,7 @@ class DrizzlePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getDateTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTypeDeclarationSQL(array $column)
{
return 'DATE';
}
@@ -480,8 +495,10 @@ class DrizzlePlatform extends AbstractPlatform
$columnSql = [];
$queryParts = [];
if ($diff->newName !== false) {
$queryParts[] = 'RENAME TO ' . $diff->getNewName()->getQuotedName($this);
$newName = $diff->getNewName();
if ($newName !== false) {
$queryParts[] = 'RENAME TO ' . $newName->getQuotedName($this);
}
foreach ($diff->addedColumns as $column) {
@@ -489,9 +506,11 @@ class DrizzlePlatform extends AbstractPlatform
continue;
}
$columnArray = $column->toArray();
$columnArray['comment'] = $this->getColumnComment($column);
$queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
$columnArray = array_merge($column->toArray(), [
'comment' => $this->getColumnComment($column),
]);
$queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
}
foreach ($diff->removedColumns as $column) {
@@ -513,7 +532,8 @@ class DrizzlePlatform extends AbstractPlatform
// Do not generate column alteration clause if type is binary and only fixed property has changed.
// Drizzle only supports binary type columns with variable length.
// Avoids unnecessary table alteration statements.
if ($columnArray['type'] instanceof BinaryType &&
if (
$columnArray['type'] instanceof BinaryType &&
$columnDiff->hasChanged('fixed') &&
count($columnDiff->changedProperties) === 1
) {
@@ -543,8 +563,10 @@ class DrizzlePlatform extends AbstractPlatform
if (! $this->onSchemaAlterTable($diff, $tableSql)) {
if (count($queryParts) > 0) {
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . implode(', ', $queryParts);
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this)
. ' ' . implode(', ', $queryParts);
}
$sql = array_merge(
$this->getPreAlterTableIndexForeignKeySQL($diff),
$sql,
@@ -563,7 +585,9 @@ class DrizzlePlatform extends AbstractPlatform
if ($table instanceof Table) {
$table = $table->getQuotedName($this);
} elseif (! is_string($table)) {
throw new InvalidArgumentException('getDropTableSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
throw new InvalidArgumentException(
__METHOD__ . '() expects $table parameter to be string or ' . Table::class . '.'
);
}
return 'DROP TEMPORARY TABLE ' . $table;
@@ -576,7 +600,7 @@ class DrizzlePlatform extends AbstractPlatform
{
if (is_array($item)) {
foreach ($item as $key => $value) {
if (! is_bool($value) && ! is_numeric($item)) {
if (! is_bool($value) && ! is_numeric($value)) {
continue;
}

View File

@@ -8,11 +8,13 @@ use function strtoupper;
/**
* Abstract interface for a SQL reserved keyword dictionary.
*
* @psalm-consistent-constructor
*/
abstract class KeywordList
{
/** @var string[]|null */
private $keywords = null;
private $keywords;
/**
* Checks if the given word is a keyword of this dialect/vendor platform.

View File

@@ -9,10 +9,7 @@ namespace Doctrine\DBAL\Platforms\Keywords;
*/
final class MariaDb102Keywords extends MySQLKeywords
{
/**
* {@inheritdoc}
*/
public function getName() : string
public function getName(): string
{
return 'MariaDb102';
}
@@ -20,7 +17,7 @@ final class MariaDb102Keywords extends MySQLKeywords
/**
* {@inheritdoc}
*/
protected function getKeywords() : array
protected function getKeywords(): array
{
return [
'ACCESSIBLE',

View File

@@ -28,6 +28,7 @@ class MySQL80Keywords extends MySQL57Keywords
$keywords = array_merge($keywords, [
'ADMIN',
'ARRAY',
'CUBE',
'CUME_DIST',
'DENSE_RANK',
@@ -40,7 +41,9 @@ class MySQL80Keywords extends MySQL57Keywords
'JSON_TABLE',
'LAG',
'LAST_VALUE',
'LATERAL',
'LEAD',
'MEMBER',
'NTH_VALUE',
'NTILE',
'OF',

View File

@@ -9,10 +9,7 @@ namespace Doctrine\DBAL\Platforms\Keywords;
*/
class PostgreSQL100Keywords extends PostgreSQL94Keywords
{
/**
* {@inheritdoc}
*/
public function getName() : string
public function getName(): string
{
return 'PostgreSQL100';
}

View File

@@ -9,6 +9,7 @@ use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Visitor\Visitor;
use function implode;
use function str_replace;

View File

@@ -2,40 +2,34 @@
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
/**
* Provides the behavior, features and SQL dialect of the MariaDB 10.2 (10.2.7 GA) database platform.
*
* Note: Should not be used with versions prior to 10.2.7.
*/
final class MariaDb1027Platform extends MySqlPlatform
class MariaDb1027Platform extends MySqlPlatform
{
/**
* {@inheritdoc}
*
* @link https://mariadb.com/kb/en/library/json-data-type/
*/
public function getJsonTypeDeclarationSQL(array $field) : string
public function getJsonTypeDeclarationSQL(array $column): string
{
return 'LONGTEXT';
}
/**
* {@inheritdoc}
*/
protected function getReservedKeywordsClass() : string
protected function getReservedKeywordsClass(): string
{
return Keywords\MariaDb102Keywords::class;
}
/**
* {@inheritdoc}
*/
protected function initializeDoctrineTypeMappings() : void
protected function initializeDoctrineTypeMappings(): void
{
parent::initializeDoctrineTypeMappings();
$this->doctrineTypeMapping['json'] = Type::JSON;
$this->doctrineTypeMapping['json'] = Types::JSON;
}
}

View File

@@ -4,7 +4,7 @@ namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
/**
* Provides the behavior, features and SQL dialect of the MySQL 5.7 (5.7.9 GA) database platform.
@@ -22,7 +22,7 @@ class MySQL57Platform extends MySqlPlatform
/**
* {@inheritdoc}
*/
public function getJsonTypeDeclarationSQL(array $field)
public function getJsonTypeDeclarationSQL(array $column)
{
return 'JSON';
}
@@ -66,6 +66,6 @@ class MySQL57Platform extends MySqlPlatform
{
parent::initializeDoctrineTypeMappings();
$this->doctrineTypeMapping['json'] = Type::JSON;
$this->doctrineTypeMapping['json'] = Types::JSON;
}
}

View File

@@ -11,6 +11,7 @@ use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types\BlobType;
use Doctrine\DBAL\Types\TextType;
use InvalidArgumentException;
use function array_diff_key;
use function array_merge;
use function array_unique;
@@ -148,17 +149,16 @@ class MySqlPlatform extends AbstractPlatform
* Two approaches to listing the table indexes. The information_schema is
* preferred, because it doesn't cause problems with SQL keywords such as "order" or "table".
*/
public function getListTableIndexesSQL($table, $currentDatabase = null)
public function getListTableIndexesSQL($table, $database = null)
{
if ($currentDatabase) {
$currentDatabase = $this->quoteStringLiteral($currentDatabase);
$table = $this->quoteStringLiteral($table);
if ($database) {
$database = $this->quoteStringLiteral($database);
$table = $this->quoteStringLiteral($table);
return 'SELECT TABLE_NAME AS `Table`, NON_UNIQUE AS Non_Unique, INDEX_NAME AS Key_name, ' .
'SEQ_IN_INDEX AS Seq_in_index, COLUMN_NAME AS Column_Name, COLLATION AS Collation, ' .
'CARDINALITY AS Cardinality, SUB_PART AS Sub_Part, PACKED AS Packed, ' .
'NULLABLE AS `Null`, INDEX_TYPE AS Index_Type, COMMENT AS Comment ' .
'FROM information_schema.STATISTICS WHERE TABLE_NAME = ' . $table . ' AND TABLE_SCHEMA = ' . $currentDatabase .
return 'SELECT NON_UNIQUE AS Non_Unique, INDEX_NAME AS Key_name, COLUMN_NAME AS Column_Name,' .
' SUB_PART AS Sub_Part, INDEX_TYPE AS Index_Type' .
' FROM information_schema.STATISTICS WHERE TABLE_NAME = ' . $table .
' AND TABLE_SCHEMA = ' . $database .
' ORDER BY SEQ_IN_INDEX ASC';
}
@@ -176,7 +176,10 @@ class MySqlPlatform extends AbstractPlatform
}
/**
* {@inheritDoc}
* @param string $table
* @param string|null $database
*
* @return string
*/
public function getListTableForeignKeysSQL($table, $database = null)
{
@@ -195,10 +198,9 @@ class MySqlPlatform extends AbstractPlatform
$databaseNameSql = $database ?? 'DATABASE()';
$sql .= ' AND k.table_schema = ' . $databaseNameSql . ' /*!50116 AND c.constraint_schema = ' . $databaseNameSql . ' */';
$sql .= ' AND k.`REFERENCED_COLUMN_NAME` is not NULL';
return $sql;
return $sql . ' AND k.table_schema = ' . $databaseNameSql
. ' /*!50116 AND c.constraint_schema = ' . $databaseNameSql . ' */'
. ' AND k.`REFERENCED_COLUMN_NAME` is not NULL';
}
/**
@@ -243,10 +245,10 @@ class MySqlPlatform extends AbstractPlatform
*
* {@inheritDoc}
*/
public function getClobTypeDeclarationSQL(array $field)
public function getClobTypeDeclarationSQL(array $column)
{
if (! empty($field['length']) && is_numeric($field['length'])) {
$length = $field['length'];
if (! empty($column['length']) && is_numeric($column['length'])) {
$length = $column['length'];
if ($length <= static::LENGTH_LIMIT_TINYTEXT) {
return 'TINYTEXT';
@@ -267,9 +269,9 @@ class MySqlPlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTimeTypeDeclarationSQL(array $column)
{
if (isset($fieldDeclaration['version']) && $fieldDeclaration['version'] === true) {
if (isset($column['version']) && $column['version'] === true) {
return 'TIMESTAMP';
}
@@ -279,7 +281,7 @@ class MySqlPlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getDateTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTypeDeclarationSQL(array $column)
{
return 'DATE';
}
@@ -287,7 +289,7 @@ class MySqlPlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getTimeTypeDeclarationSQL(array $column)
{
return 'TIME';
}
@@ -295,21 +297,21 @@ class MySqlPlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getBooleanTypeDeclarationSQL(array $field)
public function getBooleanTypeDeclarationSQL(array $column)
{
return 'TINYINT(1)';
}
/**
* Obtain DBMS specific SQL code portion needed to set the COLLATION
* of a field declaration to be used in statements like CREATE TABLE.
* of a column declaration to be used in statements like CREATE TABLE.
*
* @deprecated Deprecated since version 2.5, Use {@link self::getColumnCollationDeclarationSQL()} instead.
*
* @param string $collation name of the collation
*
* @return string DBMS specific SQL code portion needed to set the COLLATION
* of a field declaration.
* of a column declaration.
*/
public function getCollationFieldDeclaration($collation)
{
@@ -381,7 +383,7 @@ class MySqlPlatform extends AbstractPlatform
' ORDER BY ORDINAL_POSITION ASC';
}
public function getListTableMetadataSQL(string $table, ?string $database = null) : string
public function getListTableMetadataSQL(string $table, ?string $database = null): string
{
return sprintf(
<<<'SQL'
@@ -414,7 +416,7 @@ SQL
/**
* {@inheritDoc}
*/
protected function _getCreateTableSQL($tableName, array $columns, array $options = [])
protected function _getCreateTableSQL($name, array $columns, array $options = [])
{
$queryFields = $this->getColumnDeclarationListSQL($columns);
@@ -443,7 +445,7 @@ SQL
$query .= 'TEMPORARY ';
}
$query .= 'TABLE ' . $tableName . ' (' . $queryFields . ') ';
$query .= 'TABLE ' . $name . ' (' . $queryFields . ') ';
$query .= $this->buildTableOptions($options);
$query .= $this->buildPartitionOptions($options);
@@ -457,7 +459,7 @@ SQL
// Propagate foreign key constraints only for InnoDB.
if (isset($options['foreignKeys']) && $engine === 'INNODB') {
foreach ((array) $options['foreignKeys'] as $definition) {
$sql[] = $this->getCreateForeignKeySQL($definition, $tableName);
$sql[] = $this->getCreateForeignKeySQL($definition, $name);
}
}
@@ -467,14 +469,14 @@ SQL
/**
* {@inheritdoc}
*/
public function getDefaultValueDeclarationSQL($field)
public function getDefaultValueDeclarationSQL($column)
{
// Unset the default value if the given field definition does not allow default values.
if ($field['type'] instanceof TextType || $field['type'] instanceof BlobType) {
$field['default'] = null;
// Unset the default value if the given column definition does not allow default values.
if ($column['type'] instanceof TextType || $column['type'] instanceof BlobType) {
$column['default'] = null;
}
return parent::getDefaultValueDeclarationSQL($field);
return parent::getDefaultValueDeclarationSQL($column);
}
/**
@@ -504,7 +506,7 @@ SQL
$options['collate'] = $options['charset'] . '_unicode_ci';
}
$tableOptions[] = sprintf('COLLATE %s', $options['collate']);
$tableOptions[] = $this->getColumnCollationDeclarationSQL($options['collate']);
// Engine
if (! isset($options['engine'])) {
@@ -520,9 +522,7 @@ SQL
// Comment
if (isset($options['comment'])) {
$comment = trim($options['comment'], " '");
$tableOptions[] = sprintf('COMMENT = %s ', $this->quoteStringLiteral($comment));
$tableOptions[] = sprintf('COMMENT = %s ', $this->quoteStringLiteral($options['comment']));
}
// Row format
@@ -554,8 +554,10 @@ SQL
{
$columnSql = [];
$queryParts = [];
if ($diff->newName !== false) {
$queryParts[] = 'RENAME TO ' . $diff->getNewName()->getQuotedName($this);
$newName = $diff->getNewName();
if ($newName !== false) {
$queryParts[] = 'RENAME TO ' . $newName->getQuotedName($this);
}
foreach ($diff->addedColumns as $column) {
@@ -563,9 +565,11 @@ SQL
continue;
}
$columnArray = $column->toArray();
$columnArray['comment'] = $this->getColumnComment($column);
$queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
$columnArray = array_merge($column->toArray(), [
'comment' => $this->getColumnComment($column),
]);
$queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
}
foreach ($diff->removedColumns as $column) {
@@ -585,7 +589,8 @@ SQL
$columnArray = $column->toArray();
// Don't propagate default value changes for unsupported column types.
if ($columnDiff->hasChanged('default') &&
if (
$columnDiff->hasChanged('default') &&
count($columnDiff->changedProperties) === 1 &&
($columnArray['type'] instanceof TextType || $columnArray['type'] instanceof BlobType)
) {
@@ -613,6 +618,17 @@ SQL
$keyColumns = array_unique(array_values($diff->addedIndexes['primary']->getColumns()));
$queryParts[] = 'ADD PRIMARY KEY (' . implode(', ', $keyColumns) . ')';
unset($diff->addedIndexes['primary']);
} elseif (isset($diff->changedIndexes['primary'])) {
// Necessary in case the new primary key includes a new auto_increment column
foreach ($diff->changedIndexes['primary']->getColumns() as $columnName) {
if (isset($diff->addedColumns[$columnName]) && $diff->addedColumns[$columnName]->getAutoincrement()) {
$keyColumns = array_unique(array_values($diff->changedIndexes['primary']->getColumns()));
$queryParts[] = 'DROP PRIMARY KEY';
$queryParts[] = 'ADD PRIMARY KEY (' . implode(', ', $keyColumns) . ')';
unset($diff->changedIndexes['primary']);
break;
}
}
}
$sql = [];
@@ -620,8 +636,10 @@ SQL
if (! $this->onSchemaAlterTable($diff, $tableSql)) {
if (count($queryParts) > 0) {
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . implode(', ', $queryParts);
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' '
. implode(', ', $queryParts);
}
$sql = array_merge(
$this->getPreAlterTableIndexForeignKeySQL($diff),
$sql,
@@ -648,25 +666,27 @@ SQL
$sql = array_merge($sql, $this->getPreAlterTableAlterPrimaryKeySQL($diff, $remIndex));
foreach ($diff->addedIndexes as $addKey => $addIndex) {
if ($remIndex->getColumns() === $addIndex->getColumns()) {
$indexClause = 'INDEX ' . $addIndex->getName();
if ($addIndex->isPrimary()) {
$indexClause = 'PRIMARY KEY';
} elseif ($addIndex->isUnique()) {
$indexClause = 'UNIQUE INDEX ' . $addIndex->getName();
}
$query = 'ALTER TABLE ' . $table . ' DROP INDEX ' . $remIndex->getName() . ', ';
$query .= 'ADD ' . $indexClause;
$query .= ' (' . $this->getIndexFieldDeclarationListSQL($addIndex) . ')';
$sql[] = $query;
unset($diff->removedIndexes[$remKey], $diff->addedIndexes[$addKey]);
break;
if ($remIndex->getColumns() !== $addIndex->getColumns()) {
continue;
}
$indexClause = 'INDEX ' . $addIndex->getName();
if ($addIndex->isPrimary()) {
$indexClause = 'PRIMARY KEY';
} elseif ($addIndex->isUnique()) {
$indexClause = 'UNIQUE INDEX ' . $addIndex->getName();
}
$query = 'ALTER TABLE ' . $table . ' DROP INDEX ' . $remIndex->getName() . ', ';
$query .= 'ADD ' . $indexClause;
$query .= ' (' . $this->getIndexFieldDeclarationListSQL($addIndex) . ')';
$sql[] = $query;
unset($diff->removedIndexes[$remKey], $diff->addedIndexes[$addKey]);
break;
}
}
@@ -845,10 +865,14 @@ SQL
*/
protected function getPostAlterTableRenameIndexForeignKeySQL(TableDiff $diff)
{
$sql = [];
$tableName = $diff->newName !== false
? $diff->getNewName()->getQuotedName($this)
: $diff->getName($this)->getQuotedName($this);
$sql = [];
$newName = $diff->getNewName();
if ($newName !== false) {
$tableName = $newName->getQuotedName($this);
} else {
$tableName = $diff->getName($this)->getQuotedName($this);
}
foreach ($this->getRemainingForeignKeyConstraintsRequiringRenamedIndexes($diff) as $foreignKey) {
if (in_array($foreignKey, $diff->changedForeignKeys, true)) {
@@ -881,41 +905,41 @@ SQL
/**
* {@inheritDoc}
*/
public function getIntegerTypeDeclarationSQL(array $field)
public function getIntegerTypeDeclarationSQL(array $column)
{
return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
public function getBigIntTypeDeclarationSQL(array $field)
public function getBigIntTypeDeclarationSQL(array $column)
{
return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
public function getSmallIntTypeDeclarationSQL(array $field)
public function getSmallIntTypeDeclarationSQL(array $column)
{
return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
* {@inheritdoc}
*/
public function getFloatDeclarationSQL(array $field)
public function getFloatDeclarationSQL(array $column)
{
return 'DOUBLE PRECISION' . $this->getUnsignedDeclaration($field);
return 'DOUBLE PRECISION' . $this->getUnsignedDeclaration($column);
}
/**
* {@inheritdoc}
*/
public function getDecimalTypeDeclarationSQL(array $columnDef)
public function getDecimalTypeDeclarationSQL(array $column)
{
return parent::getDecimalTypeDeclarationSQL($columnDef) . $this->getUnsignedDeclaration($columnDef);
return parent::getDecimalTypeDeclarationSQL($column) . $this->getUnsignedDeclaration($column);
}
/**
@@ -933,14 +957,30 @@ SQL
/**
* {@inheritDoc}
*/
protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
protected function _getCommonIntegerTypeDeclarationSQL(array $column)
{
$autoinc = '';
if (! empty($columnDef['autoincrement'])) {
if (! empty($column['autoincrement'])) {
$autoinc = ' AUTO_INCREMENT';
}
return $this->getUnsignedDeclaration($columnDef) . $autoinc;
return $this->getUnsignedDeclaration($column) . $autoinc;
}
/**
* {@inheritDoc}
*/
public function getColumnCharsetDeclarationSQL($charset)
{
return 'CHARACTER SET ' . $charset;
}
/**
* {@inheritDoc}
*/
public function getColumnCollationDeclarationSQL($collation)
{
return 'COLLATE ' . $this->quoteSingleIdentifier($collation);
}
/**
@@ -952,6 +992,7 @@ SQL
if ($foreignKey->hasOption('match')) {
$query .= ' MATCH ' . $foreignKey->getOption('match');
}
$query .= parent::getAdvancedForeignKeyOptionsSQL($foreignKey);
return $query;
@@ -967,13 +1008,17 @@ SQL
} elseif (is_string($index)) {
$indexName = $index;
} else {
throw new InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
throw new InvalidArgumentException(
__METHOD__ . '() expects $index parameter to be string or ' . Index::class . '.'
);
}
if ($table instanceof Table) {
$table = $table->getQuotedName($this);
} elseif (! is_string($table)) {
throw new InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
throw new InvalidArgumentException(
__METHOD__ . '() expects $table parameter to be string or ' . Table::class . '.'
);
}
if ($index instanceof Index && $index->isPrimary()) {
@@ -1093,7 +1138,9 @@ SQL
if ($table instanceof Table) {
$table = $table->getQuotedName($this);
} elseif (! is_string($table)) {
throw new InvalidArgumentException('getDropTemporaryTableSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
throw new InvalidArgumentException(
__METHOD__ . '() expects $table parameter to be string or ' . Table::class . '.'
);
}
return 'DROP TEMPORARY TABLE ' . $table;
@@ -1108,10 +1155,10 @@ SQL
*
* {@inheritDoc}
*/
public function getBlobTypeDeclarationSQL(array $field)
public function getBlobTypeDeclarationSQL(array $column)
{
if (! empty($field['length']) && is_numeric($field['length'])) {
$length = $field['length'];
if (! empty($column['length']) && is_numeric($column['length'])) {
$length = $column['length'];
if ($length <= static::LENGTH_LIMIT_TINYBLOB) {
return 'TINYBLOB';
@@ -1147,10 +1194,7 @@ SQL
return TransactionIsolationLevel::REPEATABLE_READ;
}
/**
* {@inheritdoc}
*/
public function supportsColumnLengthIndexes() : bool
public function supportsColumnLengthIndexes(): bool
{
return true;
}

View File

@@ -2,7 +2,7 @@
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Index;
@@ -11,7 +11,9 @@ use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types\BinaryType;
use Doctrine\Deprecations\Deprecation;
use InvalidArgumentException;
use function array_merge;
use function count;
use function explode;
@@ -37,29 +39,33 @@ class OraclePlatform extends AbstractPlatform
*
* @param string $identifier
*
* @throws DBALException
* @return void
*
* @throws Exception
*/
public static function assertValidIdentifier($identifier)
{
if (! preg_match('(^(([a-zA-Z]{1}[a-zA-Z0-9_$#]{0,})|("[^"]+"))$)', $identifier)) {
throw new DBALException('Invalid Oracle identifier');
throw new Exception('Invalid Oracle identifier');
}
}
/**
* {@inheritDoc}
*/
public function getSubstringExpression($value, $position, $length = null)
public function getSubstringExpression($string, $start, $length = null)
{
if ($length !== null) {
return sprintf('SUBSTR(%s, %d, %d)', $value, $position, $length);
return sprintf('SUBSTR(%s, %d, %d)', $string, $start, $length);
}
return sprintf('SUBSTR(%s, %d)', $value, $position);
return sprintf('SUBSTR(%s, %d)', $string, $start);
}
/**
* {@inheritDoc}
* @param string $type
*
* @return string
*/
public function getNowExpression($type = 'timestamp')
{
@@ -201,9 +207,13 @@ class OraclePlatform extends AbstractPlatform
{
if ($sequence->getCache() === 0) {
return ' NOCACHE';
} elseif ($sequence->getCache() === 1) {
}
if ($sequence->getCache() === 1) {
return ' NOCACHE';
} elseif ($sequence->getCache() > 1) {
}
if ($sequence->getCache() > 1) {
return ' CACHE ' . $sequence->getCache();
}
@@ -213,9 +223,9 @@ class OraclePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getSequenceNextValSQL($sequenceName)
public function getSequenceNextValSQL($sequence)
{
return 'SELECT ' . $sequenceName . '.nextval FROM DUAL';
return 'SELECT ' . $sequence . '.nextval FROM DUAL';
}
/**
@@ -234,11 +244,14 @@ class OraclePlatform extends AbstractPlatform
switch ($level) {
case TransactionIsolationLevel::READ_UNCOMMITTED:
return 'READ UNCOMMITTED';
case TransactionIsolationLevel::READ_COMMITTED:
return 'READ COMMITTED';
case TransactionIsolationLevel::REPEATABLE_READ:
case TransactionIsolationLevel::SERIALIZABLE:
return 'SERIALIZABLE';
default:
return parent::_getTransactionIsolationLevelSQL($level);
}
@@ -247,7 +260,7 @@ class OraclePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getBooleanTypeDeclarationSQL(array $field)
public function getBooleanTypeDeclarationSQL(array $column)
{
return 'NUMBER(1)';
}
@@ -255,7 +268,7 @@ class OraclePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getIntegerTypeDeclarationSQL(array $field)
public function getIntegerTypeDeclarationSQL(array $column)
{
return 'NUMBER(10)';
}
@@ -263,7 +276,7 @@ class OraclePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getBigIntTypeDeclarationSQL(array $field)
public function getBigIntTypeDeclarationSQL(array $column)
{
return 'NUMBER(20)';
}
@@ -271,7 +284,7 @@ class OraclePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getSmallIntTypeDeclarationSQL(array $field)
public function getSmallIntTypeDeclarationSQL(array $column)
{
return 'NUMBER(5)';
}
@@ -279,7 +292,7 @@ class OraclePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTimeTypeDeclarationSQL(array $column)
{
return 'TIMESTAMP(0)';
}
@@ -287,7 +300,7 @@ class OraclePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTimeTzTypeDeclarationSQL(array $column)
{
return 'TIMESTAMP(0) WITH TIME ZONE';
}
@@ -295,7 +308,7 @@ class OraclePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getDateTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTypeDeclarationSQL(array $column)
{
return 'DATE';
}
@@ -303,7 +316,7 @@ class OraclePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getTimeTypeDeclarationSQL(array $column)
{
return 'DATE';
}
@@ -311,7 +324,7 @@ class OraclePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
protected function _getCommonIntegerTypeDeclarationSQL(array $column)
{
return '';
}
@@ -344,7 +357,7 @@ class OraclePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getClobTypeDeclarationSQL(array $field)
public function getClobTypeDeclarationSQL(array $column)
{
return 'CLOB';
}
@@ -372,28 +385,30 @@ class OraclePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
protected function _getCreateTableSQL($table, array $columns, array $options = [])
protected function _getCreateTableSQL($name, array $columns, array $options = [])
{
$indexes = $options['indexes'] ?? [];
$options['indexes'] = [];
$sql = parent::_getCreateTableSQL($table, $columns, $options);
$sql = parent::_getCreateTableSQL($name, $columns, $options);
foreach ($columns as $name => $column) {
foreach ($columns as $columnName => $column) {
if (isset($column['sequence'])) {
$sql[] = $this->getCreateSequenceSQL($column['sequence']);
}
if (! isset($column['autoincrement']) || ! $column['autoincrement'] &&
(! isset($column['autoinc']) || ! $column['autoinc'])) {
if (
! isset($column['autoincrement']) || ! $column['autoincrement'] &&
(! isset($column['autoinc']) || ! $column['autoinc'])
) {
continue;
}
$sql = array_merge($sql, $this->getCreateAutoincrementSql($name, $table));
$sql = array_merge($sql, $this->getCreateAutoincrementSql($columnName, $name));
}
if (isset($indexes) && ! empty($indexes)) {
foreach ($indexes as $index) {
$sql[] = $this->getCreateIndexSQL($index, $table);
$sql[] = $this->getCreateIndexSQL($index, $name);
}
}
@@ -405,7 +420,7 @@ class OraclePlatform extends AbstractPlatform
*
* @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaOracleReader.html
*/
public function getListTableIndexesSQL($table, $currentDatabase = null)
public function getListTableIndexesSQL($table, $database = null)
{
$table = $this->normalizeIdentifier($table);
$table = $this->quoteStringLiteral($table->getName());
@@ -494,14 +509,17 @@ class OraclePlatform extends AbstractPlatform
$idx = new Index($autoincrementIdentifierName, [$quotedName], true, true);
$sql[] = 'DECLARE
$sql[] = "DECLARE
constraints_Count NUMBER;
BEGIN
SELECT COUNT(CONSTRAINT_NAME) INTO constraints_Count FROM USER_CONSTRAINTS WHERE TABLE_NAME = \'' . $unquotedTableName . '\' AND CONSTRAINT_TYPE = \'P\';
IF constraints_Count = 0 OR constraints_Count = \'\' THEN
EXECUTE IMMEDIATE \'' . $this->getCreateConstraintSQL($idx, $quotedTableName) . '\';
SELECT COUNT(CONSTRAINT_NAME) INTO constraints_Count
FROM USER_CONSTRAINTS
WHERE TABLE_NAME = '" . $unquotedTableName . "'
AND CONSTRAINT_TYPE = 'P';
IF constraints_Count = 0 OR constraints_Count = '' THEN
EXECUTE IMMEDIATE '" . $this->getCreateConstraintSQL($idx, $quotedTableName) . "';
END IF;
END;';
END;";
$sequenceName = $this->getIdentitySequenceName(
$tableIdentifier->isQuoted() ? $quotedTableName : $unquotedTableName,
@@ -575,6 +593,22 @@ END;';
return $identifier->isQuoted() ? $identifier : new Identifier(strtoupper($name));
}
/**
* Adds suffix to identifier,
*
* if the new string exceeds max identifier length,
* keeps $suffix, cuts from $identifier as much as the part exceeding.
*/
private function addSuffix(string $identifier, string $suffix): string
{
$maxPossibleLengthWithoutSuffix = $this->getMaxIdentifierLength() - strlen($suffix);
if (strlen($identifier) > $maxPossibleLengthWithoutSuffix) {
$identifier = substr($identifier, 0, $maxPossibleLengthWithoutSuffix);
}
return $identifier . $suffix;
}
/**
* Returns the autoincrement primary key identifier name for the given table identifier.
*
@@ -587,7 +621,7 @@ END;';
*/
private function getAutoincrementIdentifierName(Identifier $table)
{
$identifierName = $table->getName() . '_AI_PK';
$identifierName = $this->addSuffix($table->getName(), '_AI_PK');
return $table->isQuoted()
? $this->quoteSingleIdentifier($identifierName)
@@ -753,9 +787,9 @@ SQL
/**
* {@inheritDoc}
*/
public function getDropDatabaseSQL($database)
public function getDropDatabaseSQL($name)
{
return 'DROP USER ' . $database . ' CASCADE';
return 'DROP USER ' . $name . ' CASCADE';
}
/**
@@ -789,7 +823,8 @@ SQL
}
if (count($fields)) {
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ADD (' . implode(', ', $fields) . ')';
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this)
. ' ADD (' . implode(', ', $fields) . ')';
}
$fields = [];
@@ -803,7 +838,8 @@ SQL
// Do not generate column alteration clause if type is binary and only fixed property has changed.
// Oracle only supports binary type columns with variable length.
// Avoids unnecessary table alteration statements.
if ($column->getType() instanceof BinaryType &&
if (
$column->getType() instanceof BinaryType &&
$columnDiff->hasChanged('fixed') &&
count($columnDiff->changedProperties) === 1
) {
@@ -837,7 +873,8 @@ SQL
}
if (count($fields)) {
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' MODIFY (' . implode(', ', $fields) . ')';
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this)
. ' MODIFY (' . implode(', ', $fields) . ')';
}
foreach ($diff->renamedColumns as $oldColumnName => $column) {
@@ -861,7 +898,8 @@ SQL
}
if (count($fields)) {
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' DROP (' . implode(', ', $fields) . ')';
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this)
. ' DROP (' . implode(', ', $fields) . ')';
}
$tableSql = [];
@@ -869,8 +907,14 @@ SQL
if (! $this->onSchemaAlterTable($diff, $tableSql)) {
$sql = array_merge($sql, $commentsSQL);
if ($diff->newName !== false) {
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' RENAME TO ' . $diff->getNewName()->getQuotedName($this);
$newName = $diff->getNewName();
if ($newName !== false) {
$sql[] = sprintf(
'ALTER TABLE %s RENAME TO %s',
$diff->getName($this)->getQuotedName($this),
$newName->getQuotedName($this)
);
}
$sql = array_merge(
@@ -886,26 +930,26 @@ SQL
/**
* {@inheritdoc}
*/
public function getColumnDeclarationSQL($name, array $field)
public function getColumnDeclarationSQL($name, array $column)
{
if (isset($field['columnDefinition'])) {
$columnDef = $this->getCustomTypeDeclarationSQL($field);
if (isset($column['columnDefinition'])) {
$columnDef = $this->getCustomTypeDeclarationSQL($column);
} else {
$default = $this->getDefaultValueDeclarationSQL($field);
$default = $this->getDefaultValueDeclarationSQL($column);
$notnull = '';
if (isset($field['notnull'])) {
$notnull = $field['notnull'] ? ' NOT NULL' : ' NULL';
if (isset($column['notnull'])) {
$notnull = $column['notnull'] ? ' NOT NULL' : ' NULL';
}
$unique = isset($field['unique']) && $field['unique'] ?
$unique = isset($column['unique']) && $column['unique'] ?
' ' . $this->getUniqueFieldDeclarationSQL() : '';
$check = isset($field['check']) && $field['check'] ?
' ' . $field['check'] : '';
$check = isset($column['check']) && $column['check'] ?
' ' . $column['check'] : '';
$typeDecl = $field['type']->getSQLDeclaration($field, $this);
$typeDecl = $column['type']->getSQLDeclaration($column, $this);
$columnDef = $typeDecl . $default . $notnull . $unique . $check;
}
@@ -927,9 +971,17 @@ SQL
/**
* {@inheritDoc}
*
* @deprecated
*/
public function prefersSequences()
{
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4229',
'AbstractPlatform::prefersSequences() is deprecated without replacement and removed in DBAL 3.0'
);
return true;
}
@@ -949,7 +1001,7 @@ SQL
$table = new Identifier($tableName);
// No usage of column name to preserve BC compatibility with <2.5
$identitySequenceName = $table->getName() . '_SEQ';
$identitySequenceName = $this->addSuffix($table->getName(), '_SEQ');
if ($table->isQuoted()) {
$identitySequenceName = '"' . $identitySequenceName . '"';
@@ -1014,6 +1066,8 @@ SQL
* {@inheritDoc}
*
* Oracle returns all column names in SQL result sets in uppercase.
*
* @deprecated
*/
public function getSQLResultCasing($column)
{
@@ -1054,9 +1108,17 @@ SQL
/**
* {@inheritDoc}
*
* @deprecated
*/
public function fixSchemaElementName($schemaElementName)
{
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4132',
'AbstractPlatform::fixSchemaElementName is deprecated with no replacement and removed in DBAL 3.0'
);
if (strlen($schemaElementName) > 30) {
// Trim it
return substr($schemaElementName, 0, 30);
@@ -1083,9 +1145,17 @@ SQL
/**
* {@inheritDoc}
*
* @deprecated
*/
public function supportsForeignKeyOnUpdate()
{
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4229',
'AbstractPlatform::supportsForeignKeyOnUpdate() is deprecated without replacement and removed in DBAL 3.0'
);
return false;
}
@@ -1168,8 +1238,31 @@ SQL
/**
* {@inheritDoc}
*/
public function getBlobTypeDeclarationSQL(array $field)
public function getBlobTypeDeclarationSQL(array $column)
{
return 'BLOB';
}
public function getListTableCommentsSQL(string $table, ?string $database = null): string
{
$tableCommentsName = 'user_tab_comments';
$ownerCondition = '';
if ($database !== null && $database !== '/') {
$tableCommentsName = 'all_tab_comments';
$ownerCondition = ' AND owner = ' . $this->quoteStringLiteral(
$this->normalizeIdentifier($database)->getName()
);
}
return sprintf(
<<<'SQL'
SELECT comments FROM %s WHERE table_name = %s%s
SQL
,
$tableCommentsName,
$this->quoteStringLiteral($this->normalizeIdentifier($table)->getName()),
$ownerCondition
);
}
}

View File

@@ -11,15 +11,15 @@ use Doctrine\DBAL\Platforms\Keywords\PostgreSQL100Keywords;
*/
class PostgreSQL100Platform extends PostgreSQL94Platform
{
/**
* {@inheritdoc}
*/
protected function getReservedKeywordsClass() : string
protected function getReservedKeywordsClass(): string
{
return PostgreSQL100Keywords::class;
}
public function getListSequencesSQL($database) : string
/**
* {@inheritDoc}
*/
public function getListSequencesSQL($database): string
{
return 'SELECT sequence_name AS relname,
sequence_schema AS schemaname,

View File

@@ -6,6 +6,8 @@ use function explode;
/**
* Provides the behavior, features and SQL dialect of the PostgreSQL 9.1 database platform.
*
* @deprecated Use PostgreSQL 9.4 or newer
*/
class PostgreSQL91Platform extends PostgreSqlPlatform
{
@@ -41,6 +43,8 @@ class PostgreSQL91Platform extends PostgreSqlPlatform
$sql = parent::getListTableColumnsSQL($table, $database);
$parts = explode('AS complete_type,', $sql, 2);
return $parts[0] . 'AS complete_type, (SELECT tc.collcollate FROM pg_catalog.pg_collation tc WHERE tc.oid = a.attcollation) AS collation,' . $parts[1];
return $parts[0] . 'AS complete_type, '
. '(SELECT tc.collcollate FROM pg_catalog.pg_collation tc WHERE tc.oid = a.attcollation) AS collation,'
. $parts[1];
}
}

View File

@@ -2,18 +2,21 @@
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use function sprintf;
/**
* Provides the behavior, features and SQL dialect of the PostgreSQL 9.2 database platform.
*
* @deprecated Use PostgreSQL 9.4 or newer
*/
class PostgreSQL92Platform extends PostgreSQL91Platform
{
/**
* {@inheritdoc}
*/
public function getJsonTypeDeclarationSQL(array $field)
public function getJsonTypeDeclarationSQL(array $column)
{
return 'JSON';
}
@@ -21,13 +24,13 @@ class PostgreSQL92Platform extends PostgreSQL91Platform
/**
* {@inheritdoc}
*/
public function getSmallIntTypeDeclarationSQL(array $field)
public function getSmallIntTypeDeclarationSQL(array $column)
{
if (! empty($field['autoincrement'])) {
if (! empty($column['autoincrement'])) {
return 'SMALLSERIAL';
}
return parent::getSmallIntTypeDeclarationSQL($field);
return parent::getSmallIntTypeDeclarationSQL($column);
}
/**
@@ -53,7 +56,7 @@ class PostgreSQL92Platform extends PostgreSQL91Platform
{
parent::initializeDoctrineTypeMappings();
$this->doctrineTypeMapping['json'] = Type::JSON;
$this->doctrineTypeMapping['json'] = Types::JSON;
}
/**

View File

@@ -2,7 +2,7 @@
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
/**
* Provides the behavior, features and SQL dialect of the PostgreSQL 9.4 database platform.
@@ -12,9 +12,9 @@ class PostgreSQL94Platform extends PostgreSQL92Platform
/**
* {@inheritdoc}
*/
public function getJsonTypeDeclarationSQL(array $field)
public function getJsonTypeDeclarationSQL(array $column)
{
if (! empty($field['jsonb'])) {
if (! empty($column['jsonb'])) {
return 'JSONB';
}
@@ -36,6 +36,6 @@ class PostgreSQL94Platform extends PostgreSQL92Platform
{
parent::initializeDoctrineTypeMappings();
$this->doctrineTypeMapping['jsonb'] = Type::JSON;
$this->doctrineTypeMapping['jsonb'] = Types::JSON;
}
}

View File

@@ -14,7 +14,9 @@ use Doctrine\DBAL\Types\BinaryType;
use Doctrine\DBAL\Types\BlobType;
use Doctrine\DBAL\Types\IntegerType;
use Doctrine\DBAL\Types\Type;
use Doctrine\Deprecations\Deprecation;
use UnexpectedValueException;
use function array_diff;
use function array_merge;
use function array_unique;
@@ -35,6 +37,8 @@ use function trim;
/**
* PostgreSqlPlatform.
*
* @deprecated Use PostgreSQL 9.4 or newer
*
* @todo Rename: PostgreSQLPlatform
*/
class PostgreSqlPlatform extends AbstractPlatform
@@ -69,6 +73,8 @@ class PostgreSqlPlatform extends AbstractPlatform
* Enables use of 'true'/'false' or otherwise 1 and 0 instead.
*
* @param bool $flag
*
* @return void
*/
public function setUseBooleanTrueFalseStrings($flag)
{
@@ -78,13 +84,13 @@ class PostgreSqlPlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getSubstringExpression($value, $from, $length = null)
public function getSubstringExpression($string, $start, $length = null)
{
if ($length === null) {
return 'SUBSTRING(' . $value . ' FROM ' . $from . ')';
return 'SUBSTRING(' . $string . ' FROM ' . $start . ')';
}
return 'SUBSTRING(' . $value . ' FROM ' . $from . ' FOR ' . $length . ')';
return 'SUBSTRING(' . $string . ' FROM ' . $start . ' FOR ' . $length . ')';
}
/**
@@ -111,7 +117,8 @@ class PostgreSqlPlatform extends AbstractPlatform
if ($startPos !== false) {
$str = $this->getSubstringExpression($str, $startPos);
return 'CASE WHEN (POSITION(' . $substr . ' IN ' . $str . ') = 0) THEN 0 ELSE (POSITION(' . $substr . ' IN ' . $str . ') + ' . ($startPos-1) . ') END';
return 'CASE WHEN (POSITION(' . $substr . ' IN ' . $str . ') = 0) THEN 0'
. ' ELSE (POSITION(' . $substr . ' IN ' . $str . ') + ' . ($startPos - 1) . ') END';
}
return 'POSITION(' . $substr . ' IN ' . $str . ')';
@@ -204,9 +211,17 @@ class PostgreSqlPlatform extends AbstractPlatform
/**
* {@inheritDoc}
*
* @deprecated
*/
public function prefersSequences()
{
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4229',
'AbstractPlatform::prefersSequences() is deprecated without replacement and removed in DBAL 3.0'
);
return true;
}
@@ -277,7 +292,10 @@ class PostgreSqlPlatform extends AbstractPlatform
}
/**
* {@inheritDoc}
* @param string $table
* @param string|null $database
*
* @return string
*/
public function getListTableForeignKeysSQL($table, $database = null)
{
@@ -340,7 +358,7 @@ SQL
*
* @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html
*/
public function getListTableIndexesSQL($table, $currentDatabase = null)
public function getListTableIndexesSQL($table, $database = null)
{
return 'SELECT quote_ident(relname) as relname, pg_index.indisunique, pg_index.indisprimary,
pg_index.indkey, pg_index.indrelid,
@@ -349,7 +367,8 @@ SQL
WHERE oid IN (
SELECT indexrelid
FROM pg_index si, pg_class sc, pg_namespace sn
WHERE ' . $this->getTableWhereClause($table, 'sc', 'sn') . ' AND sc.oid=si.indrelid AND sc.relnamespace = sn.oid
WHERE ' . $this->getTableWhereClause($table, 'sc', 'sn') . '
AND sc.oid=si.indrelid AND sc.relnamespace = sn.oid
) AND pg_index.indexrelid = oid';
}
@@ -367,7 +386,7 @@ SQL
[$schema, $table] = explode('.', $table);
$schema = $this->quoteStringLiteral($schema);
} else {
$schema = "ANY(string_to_array((select replace(replace(setting,'\"\$user\"',user),' ','') from pg_catalog.pg_settings where name = 'search_path'),','))";
$schema = 'ANY(current_schemas(false))';
}
$table = new Identifier($table);
@@ -432,6 +451,8 @@ SQL
*
* This is useful to force DROP DATABASE operations which could fail because of active connections.
*
* @deprecated
*
* @param string $database The name of the database to disallow new connections for.
*
* @return string
@@ -446,6 +467,8 @@ SQL
*
* This is useful to force DROP DATABASE operations which could fail because of active connections.
*
* @deprecated
*
* @param string $database The name of the database to close currently active connections for.
*
* @return string
@@ -475,7 +498,8 @@ SQL
$query .= ' NOT DEFERRABLE';
}
if (($foreignKey->hasOption('feferred') && $foreignKey->getOption('feferred') !== false)
if (
($foreignKey->hasOption('feferred') && $foreignKey->getOption('feferred') !== false)
|| ($foreignKey->hasOption('deferred') && $foreignKey->getOption('deferred') !== false)
) {
$query .= ' INITIALLY DEFERRED';
@@ -526,7 +550,6 @@ SQL
}
foreach ($diff->changedColumns as $columnDiff) {
/** @var $columnDiff \Doctrine\DBAL\Schema\ColumnDiff */
if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
continue;
}
@@ -538,7 +561,12 @@ SQL
$oldColumnName = $columnDiff->getOldColumnName()->getQuotedName($this);
$column = $columnDiff->column;
if ($columnDiff->hasChanged('type') || $columnDiff->hasChanged('precision') || $columnDiff->hasChanged('scale') || $columnDiff->hasChanged('fixed')) {
if (
$columnDiff->hasChanged('type')
|| $columnDiff->hasChanged('precision')
|| $columnDiff->hasChanged('scale')
|| $columnDiff->hasChanged('fixed')
) {
$type = $column->getType();
// SERIAL/BIGSERIAL are not "real" types and we can't alter a column to that type
@@ -569,7 +597,8 @@ SQL
$seqName = $this->getIdentitySequenceName($diff->name, $oldColumnName);
$sql[] = 'CREATE SEQUENCE ' . $seqName;
$sql[] = "SELECT setval('" . $seqName . "', (SELECT MAX(" . $oldColumnName . ') FROM ' . $diff->getName($this)->getQuotedName($this) . '))';
$sql[] = "SELECT setval('" . $seqName . "', (SELECT MAX(" . $oldColumnName . ') FROM '
. $diff->getName($this)->getQuotedName($this) . '))';
$query = 'ALTER ' . $oldColumnName . " SET DEFAULT nextval('" . $seqName . "')";
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query;
} else {
@@ -582,7 +611,10 @@ SQL
$newComment = $this->getColumnComment($column);
$oldComment = $this->getOldColumnComment($columnDiff);
if ($columnDiff->hasChanged('comment') || ($columnDiff->fromColumn !== null && $oldComment !== $newComment)) {
if (
$columnDiff->hasChanged('comment')
|| ($columnDiff->fromColumn !== null && $oldComment !== $newComment)
) {
$commentsSQL[] = $this->getCommentOnColumnSQL(
$diff->getName($this)->getQuotedName($this),
$column->getQuotedName($this),
@@ -594,7 +626,8 @@ SQL
continue;
}
$query = 'ALTER ' . $oldColumnName . ' TYPE ' . $column->getType()->getSQLDeclaration($column->toArray(), $this);
$query = 'ALTER ' . $oldColumnName . ' TYPE '
. $column->getType()->getSQLDeclaration($column->toArray(), $this);
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query;
}
@@ -614,8 +647,14 @@ SQL
if (! $this->onSchemaAlterTable($diff, $tableSql)) {
$sql = array_merge($sql, $commentsSQL);
if ($diff->newName !== false) {
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' RENAME TO ' . $diff->getNewName()->getQuotedName($this);
$newName = $diff->getNewName();
if ($newName !== false) {
$sql[] = sprintf(
'ALTER TABLE %s RENAME TO %s',
$diff->getName($this)->getQuotedName($this),
$newName->getQuotedName($this)
);
}
$sql = array_merge(
@@ -632,9 +671,8 @@ SQL
* Checks whether a given column diff is a logically unchanged binary type column.
*
* Used to determine whether a column alteration for a binary type column can be skipped.
* Doctrine's {@link \Doctrine\DBAL\Types\BinaryType} and {@link \Doctrine\DBAL\Types\BlobType}
* are mapped to the same database column type on this platform as this platform
* does not have a native VARBINARY/BINARY column type. Therefore the {@link \Doctrine\DBAL\Schema\Comparator}
* Doctrine's {@link BinaryType} and {@link BlobType} are mapped to the same database column type on this platform
* as this platform does not have a native VARBINARY/BINARY column type. Therefore the comparator
* might detect differences for binary type columns which do not have to be propagated
* to database as there actually is no difference at database level.
*
@@ -766,7 +804,7 @@ SQL
/**
* {@inheritDoc}
*/
protected function _getCreateTableSQL($tableName, array $columns, array $options = [])
protected function _getCreateTableSQL($name, array $columns, array $options = [])
{
$queryFields = $this->getColumnDeclarationListSQL($columns);
@@ -775,19 +813,19 @@ SQL
$queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
}
$query = 'CREATE TABLE ' . $tableName . ' (' . $queryFields . ')';
$query = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')';
$sql = [$query];
if (isset($options['indexes']) && ! empty($options['indexes'])) {
foreach ($options['indexes'] as $index) {
$sql[] = $this->getCreateIndexSQL($index, $tableName);
$sql[] = $this->getCreateIndexSQL($index, $name);
}
}
if (isset($options['foreignKeys'])) {
foreach ((array) $options['foreignKeys'] as $definition) {
$sql[] = $this->getCreateForeignKeySQL($definition, $tableName);
$sql[] = $this->getCreateForeignKeySQL($definition, $name);
}
}
@@ -815,7 +853,7 @@ SQL
}
if (is_bool($value) || is_numeric($value)) {
return $callback($value ? true : false);
return $callback((bool) $value);
}
if (! is_string($value)) {
@@ -874,12 +912,15 @@ SQL
return $this->doConvertBooleans(
$item,
static function ($boolean) {
if ($boolean === null) {
/**
* @param mixed $value
*/
static function ($value) {
if ($value === null) {
return 'NULL';
}
return $boolean === true ? 'true' : 'false';
return $value === true ? 'true' : 'false';
}
);
}
@@ -895,8 +936,11 @@ SQL
return $this->doConvertBooleans(
$item,
static function ($boolean) {
return $boolean === null ? null : (int) $boolean;
/**
* @param mixed $value
*/
static function ($value) {
return $value === null ? null : (int) $value;
}
);
}
@@ -906,7 +950,7 @@ SQL
*/
public function convertFromBoolean($item)
{
if (in_array(strtolower($item), $this->booleanLiterals['false'], true)) {
if ($item !== null && in_array(strtolower($item), $this->booleanLiterals['false'], true)) {
return false;
}
@@ -916,9 +960,9 @@ SQL
/**
* {@inheritDoc}
*/
public function getSequenceNextValSQL($sequenceName)
public function getSequenceNextValSQL($sequence)
{
return "SELECT NEXTVAL('" . $sequenceName . "')";
return "SELECT NEXTVAL('" . $sequence . "')";
}
/**
@@ -933,7 +977,7 @@ SQL
/**
* {@inheritDoc}
*/
public function getBooleanTypeDeclarationSQL(array $field)
public function getBooleanTypeDeclarationSQL(array $column)
{
return 'BOOLEAN';
}
@@ -941,9 +985,9 @@ SQL
/**
* {@inheritDoc}
*/
public function getIntegerTypeDeclarationSQL(array $field)
public function getIntegerTypeDeclarationSQL(array $column)
{
if (! empty($field['autoincrement'])) {
if (! empty($column['autoincrement'])) {
return 'SERIAL';
}
@@ -953,9 +997,9 @@ SQL
/**
* {@inheritDoc}
*/
public function getBigIntTypeDeclarationSQL(array $field)
public function getBigIntTypeDeclarationSQL(array $column)
{
if (! empty($field['autoincrement'])) {
if (! empty($column['autoincrement'])) {
return 'BIGSERIAL';
}
@@ -965,7 +1009,7 @@ SQL
/**
* {@inheritDoc}
*/
public function getSmallIntTypeDeclarationSQL(array $field)
public function getSmallIntTypeDeclarationSQL(array $column)
{
return 'SMALLINT';
}
@@ -973,7 +1017,7 @@ SQL
/**
* {@inheritDoc}
*/
public function getGuidTypeDeclarationSQL(array $field)
public function getGuidTypeDeclarationSQL(array $column)
{
return 'UUID';
}
@@ -981,7 +1025,7 @@ SQL
/**
* {@inheritDoc}
*/
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTimeTypeDeclarationSQL(array $column)
{
return 'TIMESTAMP(0) WITHOUT TIME ZONE';
}
@@ -989,7 +1033,7 @@ SQL
/**
* {@inheritDoc}
*/
public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTimeTzTypeDeclarationSQL(array $column)
{
return 'TIMESTAMP(0) WITH TIME ZONE';
}
@@ -997,7 +1041,7 @@ SQL
/**
* {@inheritDoc}
*/
public function getDateTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTypeDeclarationSQL(array $column)
{
return 'DATE';
}
@@ -1005,7 +1049,7 @@ SQL
/**
* {@inheritDoc}
*/
public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getTimeTypeDeclarationSQL(array $column)
{
return 'TIME(0) WITHOUT TIME ZONE';
}
@@ -1023,7 +1067,7 @@ SQL
/**
* {@inheritDoc}
*/
protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
protected function _getCommonIntegerTypeDeclarationSQL(array $column)
{
return '';
}
@@ -1048,7 +1092,7 @@ SQL
/**
* {@inheritDoc}
*/
public function getClobTypeDeclarationSQL(array $field)
public function getClobTypeDeclarationSQL(array $column)
{
return 'TEXT';
}
@@ -1065,6 +1109,8 @@ SQL
* {@inheritDoc}
*
* PostgreSQL returns all column names in SQL result sets in lowercase.
*
* @deprecated
*/
public function getSQLResultCasing($column)
{
@@ -1193,7 +1239,7 @@ SQL
/**
* {@inheritDoc}
*/
public function getBlobTypeDeclarationSQL(array $field)
public function getBlobTypeDeclarationSQL(array $column)
{
return 'BYTEA';
}
@@ -1201,28 +1247,29 @@ SQL
/**
* {@inheritdoc}
*/
public function getDefaultValueDeclarationSQL($field)
public function getDefaultValueDeclarationSQL($column)
{
if ($this->isSerialField($field)) {
if ($this->isSerialColumn($column)) {
return '';
}
return parent::getDefaultValueDeclarationSQL($field);
return parent::getDefaultValueDeclarationSQL($column);
}
/**
* @param mixed[] $field
* @param mixed[] $column
*/
private function isSerialField(array $field) : bool
private function isSerialColumn(array $column): bool
{
return $field['autoincrement'] ?? false === true && isset($field['type'])
&& $this->isNumericType($field['type']);
return isset($column['type'], $column['autoincrement'])
&& $column['autoincrement'] === true
&& $this->isNumericType($column['type']);
}
/**
* Check whether the type of a column is changed in a way that invalidates the default value for the column
*/
private function typeChangeBreaksDefaultValue(ColumnDiff $columnDiff) : bool
private function typeChangeBreaksDefaultValue(ColumnDiff $columnDiff): bool
{
if (! $columnDiff->fromColumn) {
return $columnDiff->hasChanged('type');
@@ -1236,13 +1283,28 @@ SQL
&& ! ($oldTypeIsNumeric && $newTypeIsNumeric && $columnDiff->column->getAutoincrement());
}
private function isNumericType(Type $type) : bool
private function isNumericType(Type $type): bool
{
return $type instanceof IntegerType || $type instanceof BigIntType;
}
private function getOldColumnComment(ColumnDiff $columnDiff) : ?string
private function getOldColumnComment(ColumnDiff $columnDiff): ?string
{
return $columnDiff->fromColumn ? $this->getColumnComment($columnDiff->fromColumn) : null;
}
public function getListTableMetadataSQL(string $table, ?string $schema = null): string
{
if ($schema !== null) {
$table = $schema . '.' . $table;
}
return sprintf(
<<<'SQL'
SELECT obj_description(%s::regclass) AS table_comment;
SQL
,
$this->quoteStringLiteral($table)
);
}
}

View File

@@ -5,6 +5,8 @@ namespace Doctrine\DBAL\Platforms;
/**
* The SQLAnywhere11Platform provides the behavior, features and SQL dialect of the
* SAP Sybase SQL Anywhere 11 database platform.
*
* @deprecated Support for SQLAnywhere will be removed in 3.0.
*/
class SQLAnywhere11Platform extends SQLAnywherePlatform
{

View File

@@ -8,6 +8,8 @@ use Doctrine\DBAL\Schema\Sequence;
/**
* The SQLAnywhere12Platform provides the behavior, features and SQL dialect of the
* SAP Sybase SQL Anywhere 12 database platform.
*
* @deprecated Support for SQLAnywhere will be removed in 3.0.
*/
class SQLAnywhere12Platform extends SQLAnywhere11Platform
{
@@ -42,7 +44,7 @@ class SQLAnywhere12Platform extends SQLAnywhere11Platform
/**
* {@inheritdoc}
*/
public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTimeTzTypeDeclarationSQL(array $column)
{
return 'TIMESTAMP WITH TIME ZONE';
}
@@ -70,9 +72,9 @@ class SQLAnywhere12Platform extends SQLAnywhere11Platform
/**
* {@inheritdoc}
*/
public function getSequenceNextValSQL($sequenceName)
public function getSequenceNextValSQL($sequence)
{
return 'SELECT ' . $sequenceName . '.NEXTVAL';
return 'SELECT ' . $sequence . '.NEXTVAL';
}
/**

View File

@@ -8,6 +8,8 @@ use UnexpectedValueException;
/**
* The SQLAnywhere16Platform provides the behavior, features and SQL dialect of the
* SAP Sybase SQL Anywhere 16 database platform.
*
* @deprecated Support for SQLAnywhere will be removed in 3.0.
*/
class SQLAnywhere16Platform extends SQLAnywhere12Platform
{

View File

@@ -2,7 +2,7 @@
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
@@ -13,17 +13,20 @@ use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\Deprecations\Deprecation;
use InvalidArgumentException;
use function array_merge;
use function array_unique;
use function array_values;
use function assert;
use function count;
use function explode;
use function func_get_args;
use function get_class;
use function implode;
use function is_string;
use function preg_replace;
use function preg_match;
use function sprintf;
use function strlen;
use function strpos;
@@ -33,6 +36,8 @@ use function substr;
/**
* The SQLAnywherePlatform provides the behavior, features and SQL dialect of the
* SAP Sybase SQL Anywhere 10 database platform.
*
* @deprecated Support for SQLAnywhere will be removed in 3.0.
*/
class SQLAnywherePlatform extends AbstractPlatform
{
@@ -48,7 +53,7 @@ class SQLAnywherePlatform extends AbstractPlatform
{
switch (true) {
case $lockMode === LockMode::NONE:
return $fromClause . ' WITH (NOLOCK)';
return $fromClause;
case $lockMode === LockMode::PESSIMISTIC_READ:
return $fromClause . ' WITH (UPDLOCK)';
@@ -68,6 +73,12 @@ class SQLAnywherePlatform extends AbstractPlatform
*/
public function fixSchemaElementName($schemaElementName)
{
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4132',
'AbstractPlatform::fixSchemaElementName is deprecated with no replacement and removed in DBAL 3.0'
);
$maxIdentifierLength = $this->getMaxIdentifierLength();
if (strlen($schemaElementName) > $maxIdentifierLength) {
@@ -184,9 +195,11 @@ class SQLAnywherePlatform extends AbstractPlatform
$sql = array_merge($sql, $commentsSQL);
if ($diff->newName !== false) {
$newName = $diff->getNewName();
if ($newName !== false) {
$sql[] = $this->getAlterTableClause($diff->getName($this)) . ' ' .
$this->getAlterTableRenameTableClause($diff->getNewName());
$this->getAlterTableRenameTableClause($newName);
}
$sql = array_merge(
@@ -294,11 +307,11 @@ class SQLAnywherePlatform extends AbstractPlatform
/**
* {@inheritdoc}
*/
public function getBigIntTypeDeclarationSQL(array $columnDef)
public function getBigIntTypeDeclarationSQL(array $column)
{
$columnDef['integer_type'] = 'BIGINT';
$column['integer_type'] = 'BIGINT';
return $this->_getCommonIntegerTypeDeclarationSQL($columnDef);
return $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
@@ -320,7 +333,7 @@ class SQLAnywherePlatform extends AbstractPlatform
/**
* {@inheritdoc}
*/
public function getBlobTypeDeclarationSQL(array $field)
public function getBlobTypeDeclarationSQL(array $column)
{
return 'LONG BINARY';
}
@@ -333,9 +346,9 @@ class SQLAnywherePlatform extends AbstractPlatform
* Otherwise by just omitting the NOT NULL clause,
* SQL Anywhere will declare them NOT NULL nonetheless.
*/
public function getBooleanTypeDeclarationSQL(array $columnDef)
public function getBooleanTypeDeclarationSQL(array $column)
{
$nullClause = isset($columnDef['notnull']) && (bool) $columnDef['notnull'] === false ? ' NULL' : '';
$nullClause = isset($column['notnull']) && (bool) $column['notnull'] === false ? ' NULL' : '';
return 'BIT' . $nullClause;
}
@@ -343,7 +356,7 @@ class SQLAnywherePlatform extends AbstractPlatform
/**
* {@inheritdoc}
*/
public function getClobTypeDeclarationSQL(array $field)
public function getClobTypeDeclarationSQL(array $column)
{
return 'TEXT';
}
@@ -370,7 +383,7 @@ class SQLAnywherePlatform extends AbstractPlatform
*/
public function getConcatExpression()
{
return 'STRING(' . implode(', ', (array) func_get_args()) . ')';
return 'STRING(' . implode(', ', func_get_args()) . ')';
}
/**
@@ -393,11 +406,11 @@ class SQLAnywherePlatform extends AbstractPlatform
/**
* {@inheritdoc}
*/
public function getCreateDatabaseSQL($database)
public function getCreateDatabaseSQL($name)
{
$database = new Identifier($database);
$name = new Identifier($name);
return "CREATE DATABASE '" . $database->getName() . "'";
return "CREATE DATABASE '" . $name->getName() . "'";
}
/**
@@ -495,7 +508,7 @@ class SQLAnywherePlatform extends AbstractPlatform
/**
* {@inheritdoc}
*/
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTimeTypeDeclarationSQL(array $column)
{
return 'DATETIME';
}
@@ -511,7 +524,7 @@ class SQLAnywherePlatform extends AbstractPlatform
/**
* {@inheritdoc}
*/
public function getDateTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTypeDeclarationSQL(array $column)
{
return 'DATE';
}
@@ -527,11 +540,11 @@ class SQLAnywherePlatform extends AbstractPlatform
/**
* {@inheritdoc}
*/
public function getDropDatabaseSQL($database)
public function getDropDatabaseSQL($name)
{
$database = new Identifier($database);
$name = new Identifier($name);
return "DROP DATABASE '" . $database->getName() . "'";
return "DROP DATABASE '" . $name->getName() . "'";
}
/**
@@ -545,7 +558,7 @@ class SQLAnywherePlatform extends AbstractPlatform
if (! is_string($index)) {
throw new InvalidArgumentException(
'SQLAnywherePlatform::getDropIndexSQL() expects $index parameter to be string or ' . Index::class . '.'
__METHOD__ . '() expects $index parameter to be string or ' . Index::class . '.'
);
}
@@ -559,7 +572,7 @@ class SQLAnywherePlatform extends AbstractPlatform
if (! is_string($table)) {
throw new InvalidArgumentException(
'SQLAnywherePlatform::getDropIndexSQL() expects $table parameter to be string or ' . Index::class . '.'
__METHOD__ . '() expects $table parameter to be string or ' . Index::class . '.'
);
}
@@ -625,15 +638,16 @@ class SQLAnywherePlatform extends AbstractPlatform
switch ((int) $type) {
case self::FOREIGN_KEY_MATCH_SIMPLE:
return 'SIMPLE';
break;
case self::FOREIGN_KEY_MATCH_FULL:
return 'FULL';
break;
case self::FOREIGN_KEY_MATCH_SIMPLE_UNIQUE:
return 'UNIQUE SIMPLE';
break;
case self::FOREIGN_KEY_MATCH_FULL_UNIQUE:
return 'UNIQUE FULL';
default:
throw new InvalidArgumentException('Invalid foreign key match type: ' . $type);
}
@@ -673,7 +687,7 @@ class SQLAnywherePlatform extends AbstractPlatform
/**
* {@inheritdoc}
*/
public function getGuidTypeDeclarationSQL(array $field)
public function getGuidTypeDeclarationSQL(array $column)
{
return 'UNIQUEIDENTIFIER';
}
@@ -684,17 +698,17 @@ class SQLAnywherePlatform extends AbstractPlatform
public function getIndexDeclarationSQL($name, Index $index)
{
// Index declaration in statements like CREATE TABLE is not supported.
throw DBALException::notSupported(__METHOD__);
throw Exception::notSupported(__METHOD__);
}
/**
* {@inheritdoc}
*/
public function getIntegerTypeDeclarationSQL(array $columnDef)
public function getIntegerTypeDeclarationSQL(array $column)
{
$columnDef['integer_type'] = 'INT';
$column['integer_type'] = 'INT';
return $this->_getCommonIntegerTypeDeclarationSQL($columnDef);
return $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
@@ -869,7 +883,7 @@ SQL
/**
* {@inheritdoc}
*/
public function getListTableIndexesSQL($table, $currentDatabase = null)
public function getListTableIndexesSQL($table, $database = null)
{
$user = '';
@@ -1034,11 +1048,11 @@ SQL
/**
* {@inheritdoc}
*/
public function getSmallIntTypeDeclarationSQL(array $columnDef)
public function getSmallIntTypeDeclarationSQL(array $column)
{
$columnDef['integer_type'] = 'SMALLINT';
$column['integer_type'] = 'SMALLINT';
return $this->_getCommonIntegerTypeDeclarationSQL($columnDef);
return $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
@@ -1083,13 +1097,13 @@ SQL
/**
* {@inheritdoc}
*/
public function getSubstringExpression($value, $from, $length = null)
public function getSubstringExpression($string, $start, $length = null)
{
if ($length === null) {
return 'SUBSTRING(' . $value . ', ' . $from . ')';
return 'SUBSTRING(' . $string . ', ' . $start . ')';
}
return 'SUBSTRING(' . $value . ', ' . $from . ', ' . $length . ')';
return 'SUBSTRING(' . $string . ', ' . $start . ', ' . $length . ')';
}
/**
@@ -1111,7 +1125,7 @@ SQL
/**
* {@inheritdoc}
*/
public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getTimeTypeDeclarationSQL(array $column)
{
return 'TIME';
}
@@ -1119,14 +1133,16 @@ SQL
/**
* {@inheritdoc}
*/
public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = false)
public function getTrimExpression($str, $mode = TrimMode::UNSPECIFIED, $char = false)
{
if (! $char) {
switch ($pos) {
switch ($mode) {
case TrimMode::LEADING:
return $this->getLtrimExpression($str);
case TrimMode::TRAILING:
return $this->getRtrimExpression($str);
default:
return 'TRIM(' . $str . ')';
}
@@ -1134,14 +1150,17 @@ SQL
$pattern = "'%[^' + " . $char . " + ']%'";
switch ($pos) {
switch ($mode) {
case TrimMode::LEADING:
return 'SUBSTR(' . $str . ', PATINDEX(' . $pattern . ', ' . $str . '))';
case TrimMode::TRAILING:
return 'REVERSE(SUBSTR(REVERSE(' . $str . '), PATINDEX(' . $pattern . ', REVERSE(' . $str . '))))';
default:
return 'REVERSE(SUBSTR(REVERSE(SUBSTR(' . $str . ', PATINDEX(' . $pattern . ', ' . $str . '))), ' .
'PATINDEX(' . $pattern . ', REVERSE(SUBSTR(' . $str . ', PATINDEX(' . $pattern . ', ' . $str . '))))))';
'PATINDEX(' . $pattern . ', ' .
'REVERSE(SUBSTR(' . $str . ', PATINDEX(' . $pattern . ', ' . $str . '))))))';
}
}
@@ -1227,18 +1246,18 @@ SQL
/**
* {@inheritdoc}
*/
protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
protected function _getCommonIntegerTypeDeclarationSQL(array $column)
{
$unsigned = ! empty($columnDef['unsigned']) ? 'UNSIGNED ' : '';
$autoincrement = ! empty($columnDef['autoincrement']) ? ' IDENTITY' : '';
$unsigned = ! empty($column['unsigned']) ? 'UNSIGNED ' : '';
$autoincrement = ! empty($column['autoincrement']) ? ' IDENTITY' : '';
return $unsigned . $columnDef['integer_type'] . $autoincrement;
return $unsigned . $column['integer_type'] . $autoincrement;
}
/**
* {@inheritdoc}
*/
protected function _getCreateTableSQL($tableName, array $columns, array $options = [])
protected function _getCreateTableSQL($name, array $columns, array $options = [])
{
$columnListSql = $this->getColumnDeclarationListSQL($columns);
$indexSql = [];
@@ -1250,9 +1269,9 @@ SQL
}
if (! empty($options['indexes'])) {
/** @var Index $index */
foreach ((array) $options['indexes'] as $index) {
$indexSql[] = $this->getCreateIndexSQL($index, $tableName);
assert($index instanceof Index);
$indexSql[] = $this->getCreateIndexSQL($index, $name);
}
}
@@ -1263,7 +1282,8 @@ SQL
$flags = ' CLUSTERED ';
}
$columnListSql .= ', PRIMARY KEY' . $flags . ' (' . implode(', ', array_unique(array_values((array) $options['primary']))) . ')';
$columnListSql .= ', PRIMARY KEY' . $flags
. ' (' . implode(', ', array_unique(array_values((array) $options['primary']))) . ')';
}
if (! empty($options['foreignKeys'])) {
@@ -1272,7 +1292,7 @@ SQL
}
}
$query = 'CREATE TABLE ' . $tableName . ' (' . $columnListSql;
$query = 'CREATE TABLE ' . $name . ' (' . $columnListSql;
$check = $this->getCheckDeclarationSQL($columns);
if (! empty($check)) {
@@ -1291,13 +1311,17 @@ SQL
{
switch ($level) {
case TransactionIsolationLevel::READ_UNCOMMITTED:
return 0;
return '0';
case TransactionIsolationLevel::READ_COMMITTED:
return 1;
return '1';
case TransactionIsolationLevel::REPEATABLE_READ:
return 2;
return '2';
case TransactionIsolationLevel::SERIALIZABLE:
return 3;
return '3';
default:
throw new InvalidArgumentException('Invalid isolation level:' . $level);
}
@@ -1308,25 +1332,26 @@ SQL
*/
protected function doModifyLimitQuery($query, $limit, $offset)
{
$limitOffsetClause = '';
$limitOffsetClause = $this->getTopClauseSQL($limit, $offset);
if ($limit > 0) {
$limitOffsetClause = 'TOP ' . $limit . ' ';
if ($limitOffsetClause === '') {
return $query;
}
if (! preg_match('/^\s*(SELECT\s+(DISTINCT\s+)?)(.*)/i', $query, $matches)) {
return $query;
}
return $matches[1] . $limitOffsetClause . ' ' . $matches[3];
}
private function getTopClauseSQL(?int $limit, ?int $offset): string
{
if ($offset > 0) {
if ($limit === 0) {
$limitOffsetClause = 'TOP ALL ';
}
$limitOffsetClause .= 'START AT ' . ($offset + 1) . ' ';
return sprintf('TOP %s START AT %d', $limit ?? 'ALL', $offset + 1);
}
if ($limitOffsetClause) {
return preg_replace('/^\s*(SELECT\s+(DISTINCT\s+)?)/i', '\1' . $limitOffsetClause, $query);
}
return $query;
return $limit === null ? '' : 'TOP ' . $limit;
}
/**
@@ -1380,8 +1405,8 @@ SQL
if (! $constraint->isPrimary() && ! $constraint->isUnique()) {
throw new InvalidArgumentException(
'Can only create primary, unique or foreign key constraint declarations, no common index declarations ' .
'with getTableConstraintDeclarationSQL().'
'Can only create primary, unique or foreign key constraint declarations, no common index declarations'
. ' with getTableConstraintDeclarationSQL().'
);
}
@@ -1404,7 +1429,8 @@ SQL
}
if ($constraint->isPrimary()) {
return $sql . 'PRIMARY KEY ' . $flags . '(' . $this->getIndexFieldDeclarationListSQL($constraintColumns) . ')';
return $sql . 'PRIMARY KEY ' . $flags
. '(' . $this->getIndexFieldDeclarationListSQL($constraintColumns) . ')';
}
return $sql . 'UNIQUE ' . $flags . '(' . $this->getIndexFieldDeclarationListSQL($constraintColumns) . ')';

View File

@@ -10,6 +10,8 @@ use Doctrine\DBAL\Schema\Table;
* On top of SQL Server 2008 the following functionality is added:
*
* - Create tables with the FEDERATED ON syntax.
*
* @deprecated
*/
class SQLAzurePlatform extends SQLServer2008Platform
{

View File

@@ -15,6 +15,8 @@ namespace Doctrine\DBAL\Platforms;
* NVARCHAR(max) replace the old TEXT, NTEXT and IMAGE types. See
* {@link http://www.sql-server-helper.com/faq/sql-server-2005-varchar-max-p01.aspx}
* for more information.
*
* @deprecated Use SQL Server 2012 or newer
*/
class SQLServer2005Platform extends SQLServerPlatform
{
@@ -29,7 +31,7 @@ class SQLServer2005Platform extends SQLServerPlatform
/**
* {@inheritDoc}
*/
public function getClobTypeDeclarationSQL(array $field)
public function getClobTypeDeclarationSQL(array $column)
{
return 'VARCHAR(MAX)';
}

View File

@@ -7,6 +7,8 @@ namespace Doctrine\DBAL\Platforms;
*
* Differences to SQL Server 2005 and before are that a new DATETIME2 type was
* introduced that has a higher precision.
*
* @deprecated Use SQL Server 2012 or newer
*/
class SQLServer2008Platform extends SQLServer2005Platform
{
@@ -17,13 +19,14 @@ class SQLServer2008Platform extends SQLServer2005Platform
{
// "sysdiagrams" table must be ignored as it's internal SQL Server table for Database Diagrams
// Category 2 must be ignored as it is "MS SQL Server 'pseudo-system' object[s]" for replication
return "SELECT name, SCHEMA_NAME (uid) AS schema_name FROM sysobjects WHERE type = 'U' AND name != 'sysdiagrams' AND category != 2 ORDER BY name";
return 'SELECT name, SCHEMA_NAME (uid) AS schema_name FROM sysobjects'
. " WHERE type = 'U' AND name != 'sysdiagrams' AND category != 2 ORDER BY name";
}
/**
* {@inheritDoc}
*/
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTimeTypeDeclarationSQL(array $column)
{
// 3 - microseconds precision length
// http://msdn.microsoft.com/en-us/library/ms187819.aspx
@@ -33,7 +36,7 @@ class SQLServer2008Platform extends SQLServer2005Platform
/**
* {@inheritDoc}
*/
public function getDateTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTypeDeclarationSQL(array $column)
{
return 'DATE';
}
@@ -41,7 +44,7 @@ class SQLServer2008Platform extends SQLServer2005Platform
/**
* {@inheritDoc}
*/
public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getTimeTypeDeclarationSQL(array $column)
{
return 'TIME(0)';
}
@@ -49,7 +52,7 @@ class SQLServer2008Platform extends SQLServer2005Platform
/**
* {@inheritDoc}
*/
public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTimeTzTypeDeclarationSQL(array $column)
{
return 'DATETIMEOFFSET(6)';
}
@@ -110,7 +113,7 @@ class SQLServer2008Platform extends SQLServer2005Platform
return Keywords\SQLServer2008Keywords::class;
}
protected function getLikeWildcardCharacters() : string
protected function getLikeWildcardCharacters(): string
{
return parent::getLikeWildcardCharacters() . '[]^';
}

View File

@@ -3,11 +3,13 @@
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Schema\Sequence;
use const PREG_OFFSET_CAPTURE;
use function preg_match;
use function preg_match_all;
use function substr_count;
use const PREG_OFFSET_CAPTURE;
/**
* Platform to ensure compatibility of Doctrine with Microsoft SQL Server 2012 version.
*
@@ -66,9 +68,9 @@ class SQLServer2012Platform extends SQLServer2008Platform
/**
* {@inheritdoc}
*/
public function getSequenceNextValSQL($sequenceName)
public function getSequenceNextValSQL($sequence)
{
return 'SELECT NEXT VALUE FOR ' . $sequenceName;
return 'SELECT NEXT VALUE FOR ' . $sequence;
}
/**
@@ -99,18 +101,7 @@ class SQLServer2012Platform extends SQLServer2008Platform
}
// Queries using OFFSET... FETCH MUST have an ORDER BY clause
// Find the position of the last instance of ORDER BY and ensure it is not within a parenthetical statement
// but can be in a newline
$matches = [];
$matchesCount = preg_match_all('/[\\s]+order\\s+by\\s/im', $query, $matches, PREG_OFFSET_CAPTURE);
$orderByPos = false;
if ($matchesCount > 0) {
$orderByPos = $matches[0][($matchesCount - 1)][1];
}
if ($orderByPos === false
|| substr_count($query, '(', $orderByPos) - substr_count($query, ')', $orderByPos)
) {
if ($this->shouldAddOrderBy($query)) {
if (preg_match('/^SELECT\s+DISTINCT/im', $query)) {
// SQL Server won't let us order by a non-selected column in a DISTINCT query,
// so we have to do this madness. This says, order by the first column in the
@@ -140,4 +131,35 @@ class SQLServer2012Platform extends SQLServer2008Platform
return $query;
}
/**
* @param string $query
*/
private function shouldAddOrderBy($query): bool
{
// Find the position of the last instance of ORDER BY and ensure it is not within a parenthetical statement
// but can be in a newline
$matches = [];
$matchesCount = preg_match_all('/[\\s]+order\\s+by\\s/im', $query, $matches, PREG_OFFSET_CAPTURE);
if ($matchesCount === 0) {
return true;
}
// ORDER BY instance may be in a subquery after ORDER BY
// e.g. SELECT col1 FROM test ORDER BY (SELECT col2 from test ORDER BY col2)
// if in the searched query ORDER BY clause was found where
// number of open parentheses after the occurrence of the clause is equal to
// number of closed brackets after the occurrence of the clause,
// it means that ORDER BY is included in the query being checked
while ($matchesCount > 0) {
$orderByPos = $matches[0][--$matchesCount][1];
$openBracketsCount = substr_count($query, '(', $orderByPos);
$closedBracketsCount = substr_count($query, ')', $orderByPos);
if ($openBracketsCount === $closedBracketsCount) {
return false;
}
}
return true;
}
}

View File

@@ -10,8 +10,8 @@ use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types;
use InvalidArgumentException;
use function array_merge;
use function array_unique;
use function array_values;
@@ -26,7 +26,6 @@ use function is_bool;
use function is_numeric;
use function is_string;
use function preg_match;
use function preg_replace;
use function sprintf;
use function str_replace;
use function stripos;
@@ -40,6 +39,8 @@ use function substr_count;
/**
* The SQLServerPlatform provides the behavior, features and SQL dialect of the
* Microsoft SQL Server database platform.
*
* @deprecated Use SQL Server 2012 or newer
*/
class SQLServerPlatform extends AbstractPlatform
{
@@ -214,7 +215,9 @@ class SQLServerPlatform extends AbstractPlatform
if ($index instanceof Index) {
$index = $index->getQuotedName($this);
} elseif (! is_string($index)) {
throw new InvalidArgumentException('AbstractPlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
throw new InvalidArgumentException(
__METHOD__ . '() expects $index parameter to be string or ' . Index::class . '.'
);
}
if (! isset($table)) {
@@ -226,13 +229,12 @@ class SQLServerPlatform extends AbstractPlatform
}
return sprintf(
<<<SQL
IF EXISTS (SELECT * FROM sysobjects WHERE name = '%s')
ALTER TABLE %s DROP CONSTRAINT %s
ELSE
DROP INDEX %s ON %s
SQL
,
"
IF EXISTS (SELECT * FROM sysobjects WHERE name = '%s')
ALTER TABLE %s DROP CONSTRAINT %s
ELSE
DROP INDEX %s ON %s
",
$index,
$table,
$index,
@@ -244,11 +246,16 @@ SQL
/**
* {@inheritDoc}
*/
protected function _getCreateTableSQL($tableName, array $columns, array $options = [])
protected function _getCreateTableSQL($name, array $columns, array $options = [])
{
$defaultConstraintsSql = [];
$commentsSql = [];
$tableComment = $options['comment'] ?? null;
if ($tableComment !== null) {
$commentsSql[] = $this->getCommentOnTableSQL($name, $tableComment);
}
// @todo does other code breaks because of this?
// force primary keys to be not null
foreach ($columns as &$column) {
@@ -258,15 +265,15 @@ SQL
// Build default constraints SQL statements.
if (isset($column['default'])) {
$defaultConstraintsSql[] = 'ALTER TABLE ' . $tableName .
' ADD' . $this->getDefaultConstraintDeclarationSQL($tableName, $column);
$defaultConstraintsSql[] = 'ALTER TABLE ' . $name .
' ADD' . $this->getDefaultConstraintDeclarationSQL($name, $column);
}
if (empty($column['comment']) && ! is_numeric($column['comment'])) {
continue;
}
$commentsSql[] = $this->getCreateColumnCommentSQL($tableName, $column['name'], $column['comment']);
$commentsSql[] = $this->getCreateColumnCommentSQL($name, $column['name'], $column['comment']);
}
$columnListSql = $this->getColumnDeclarationListSQL($columns);
@@ -282,28 +289,31 @@ SQL
if (isset($options['primary_index']) && $options['primary_index']->hasFlag('nonclustered')) {
$flags = ' NONCLUSTERED';
}
$columnListSql .= ', PRIMARY KEY' . $flags . ' (' . implode(', ', array_unique(array_values($options['primary']))) . ')';
$columnListSql .= ', PRIMARY KEY' . $flags
. ' (' . implode(', ', array_unique(array_values($options['primary']))) . ')';
}
$query = 'CREATE TABLE ' . $tableName . ' (' . $columnListSql;
$query = 'CREATE TABLE ' . $name . ' (' . $columnListSql;
$check = $this->getCheckDeclarationSQL($columns);
if (! empty($check)) {
$query .= ', ' . $check;
}
$query .= ')';
$sql = [$query];
if (isset($options['indexes']) && ! empty($options['indexes'])) {
foreach ($options['indexes'] as $index) {
$sql[] = $this->getCreateIndexSQL($index, $tableName);
$sql[] = $this->getCreateIndexSQL($index, $name);
}
}
if (isset($options['foreignKeys'])) {
foreach ((array) $options['foreignKeys'] as $definition) {
$sql[] = $this->getCreateForeignKeySQL($definition, $tableName);
$sql[] = $this->getCreateForeignKeySQL($definition, $name);
}
}
@@ -315,12 +325,19 @@ SQL
*/
public function getCreatePrimaryKeySQL(Index $index, $table)
{
$flags = '';
if ($index->hasFlag('nonclustered')) {
$flags = ' NONCLUSTERED';
if ($table instanceof Table) {
$identifier = $table->getQuotedName($this);
} else {
$identifier = $table;
}
return 'ALTER TABLE ' . $table . ' ADD PRIMARY KEY' . $flags . ' (' . $this->getIndexFieldDeclarationListSQL($index) . ')';
$sql = 'ALTER TABLE ' . $identifier . ' ADD PRIMARY KEY';
if ($index->hasFlag('nonclustered')) {
$sql .= ' NONCLUSTERED';
}
return $sql . ' (' . $this->getIndexFieldDeclarationListSQL($index) . ')';
}
/**
@@ -334,9 +351,9 @@ SQL
* as column comments are stored in the same property there when
* specifying a column's "Description" attribute.
*
* @param string $tableName The quoted table name to which the column belongs.
* @param string $columnName The quoted column name to create the comment for.
* @param string $comment The column's comment.
* @param string $tableName The quoted table name to which the column belongs.
* @param string $columnName The quoted column name to create the comment for.
* @param string|null $comment The column's comment.
*
* @return string
*/
@@ -466,12 +483,15 @@ SQL
}
$columnDef = $column->toArray();
$queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnDef);
$addColumnSql = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnDef);
if (isset($columnDef['default'])) {
$queryParts[] = $this->getAlterTableAddDefaultConstraintClause($diff->name, $column);
$addColumnSql .= ' CONSTRAINT ' .
$this->generateDefaultConstraintName($diff->name, $column->getQuotedName($this)) .
$this->getDefaultValueDeclarationSQL($columnDef);
}
$queryParts[] = $addColumnSql;
$comment = $this->getColumnComment($column);
if (empty($comment) && ! is_numeric($comment)) {
@@ -514,7 +534,7 @@ SQL
);
} elseif ($hasFromComment && ! $hasComment) {
$commentsSql[] = $this->getDropColumnCommentSQL($diff->name, $column->getQuotedName($this));
} elseif ($hasComment) {
} elseif (! $hasFromComment && $hasComment) {
$commentsSql[] = $this->getCreateColumnCommentSQL(
$diff->name,
$column->getQuotedName($this),
@@ -539,10 +559,12 @@ SQL
$columnDef = $column->toArray();
$queryParts[] = 'ALTER COLUMN ' .
$this->getColumnDeclarationSQL($column->getQuotedName($this), $columnDef);
$queryParts[] = 'ALTER COLUMN ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnDef);
if (! isset($columnDef['default']) || (! $requireDropDefaultConstraint && ! $columnDiff->hasChanged('default'))) {
if (
! isset($columnDef['default'])
|| (! $requireDropDefaultConstraint && ! $columnDiff->hasChanged('default'))
) {
continue;
}
@@ -556,7 +578,7 @@ SQL
$oldColumnName = new Identifier($oldColumnName);
$sql[] = "sp_RENAME '" .
$sql[] = "sp_rename '" .
$diff->getName($this)->getQuotedName($this) . '.' . $oldColumnName->getQuotedName($this) .
"', '" . $column->getQuotedName($this) . "', 'COLUMN'";
@@ -584,8 +606,10 @@ SQL
$sql = array_merge($sql, $commentsSql);
if ($diff->newName !== false) {
$sql[] = "sp_RENAME '" . $diff->getName($this)->getQuotedName($this) . "', '" . $diff->getNewName()->getName() . "'";
$newName = $diff->getNewName();
if ($newName !== false) {
$sql[] = "sp_rename '" . $diff->getName($this)->getQuotedName($this) . "', '" . $newName->getName() . "'";
/**
* Rename table's default constraints names
@@ -598,10 +622,10 @@ SQL
$sql[] = "DECLARE @sql NVARCHAR(MAX) = N''; " .
"SELECT @sql += N'EXEC sp_rename N''' + dc.name + ''', N''' " .
"+ REPLACE(dc.name, '" . $this->generateIdentifierName($diff->name) . "', " .
"'" . $this->generateIdentifierName($diff->newName) . "') + ''', ''OBJECT'';' " .
"'" . $this->generateIdentifierName($newName->getName()) . "') + ''', ''OBJECT'';' " .
'FROM sys.default_constraints dc ' .
'JOIN sys.tables tbl ON dc.parent_object_id = tbl.object_id ' .
"WHERE tbl.name = '" . $diff->getNewName()->getName() . "';" .
"WHERE tbl.name = '" . $newName->getName() . "';" .
'EXEC sp_executesql @sql';
}
@@ -691,9 +715,9 @@ SQL
* as column comments are stored in the same property there when
* specifying a column's "Description" attribute.
*
* @param string $tableName The quoted table name to which the column belongs.
* @param string $columnName The quoted column name to alter the comment for.
* @param string $comment The column's comment.
* @param string $tableName The quoted table name to which the column belongs.
* @param string $columnName The quoted column name to alter the comment for.
* @param string|null $comment The column's comment.
*
* @return string
*/
@@ -764,7 +788,7 @@ SQL
protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
{
return [sprintf(
"EXEC sp_RENAME N'%s.%s', N'%s', N'INDEX'",
"EXEC sp_rename N'%s.%s', N'%s', N'INDEX'",
$tableName,
$oldIndexName,
$index->getQuotedName($this)
@@ -799,10 +823,10 @@ SQL
$level2Name = null
) {
return 'EXEC sp_addextendedproperty ' .
'N' . $this->quoteStringLiteral($name) . ', N' . $this->quoteStringLiteral($value) . ', ' .
'N' . $this->quoteStringLiteral($level0Type) . ', ' . $level0Name . ', ' .
'N' . $this->quoteStringLiteral($level1Type) . ', ' . $level1Name . ', ' .
'N' . $this->quoteStringLiteral($level2Type) . ', ' . $level2Name;
'N' . $this->quoteStringLiteral($name) . ', N' . $this->quoteStringLiteral((string) $value) . ', ' .
'N' . $this->quoteStringLiteral((string) $level0Type) . ', ' . $level0Name . ', ' .
'N' . $this->quoteStringLiteral((string) $level1Type) . ', ' . $level1Name . ', ' .
'N' . $this->quoteStringLiteral((string) $level2Type) . ', ' . $level2Name;
}
/**
@@ -831,9 +855,9 @@ SQL
) {
return 'EXEC sp_dropextendedproperty ' .
'N' . $this->quoteStringLiteral($name) . ', ' .
'N' . $this->quoteStringLiteral($level0Type) . ', ' . $level0Name . ', ' .
'N' . $this->quoteStringLiteral($level1Type) . ', ' . $level1Name . ', ' .
'N' . $this->quoteStringLiteral($level2Type) . ', ' . $level2Name;
'N' . $this->quoteStringLiteral((string) $level0Type) . ', ' . $level0Name . ', ' .
'N' . $this->quoteStringLiteral((string) $level1Type) . ', ' . $level1Name . ', ' .
'N' . $this->quoteStringLiteral((string) $level2Type) . ', ' . $level2Name;
}
/**
@@ -863,10 +887,10 @@ SQL
$level2Name = null
) {
return 'EXEC sp_updateextendedproperty ' .
'N' . $this->quoteStringLiteral($name) . ', N' . $this->quoteStringLiteral($value) . ', ' .
'N' . $this->quoteStringLiteral($level0Type) . ', ' . $level0Name . ', ' .
'N' . $this->quoteStringLiteral($level1Type) . ', ' . $level1Name . ', ' .
'N' . $this->quoteStringLiteral($level2Type) . ', ' . $level2Name;
'N' . $this->quoteStringLiteral($name) . ', N' . $this->quoteStringLiteral((string) $value) . ', ' .
'N' . $this->quoteStringLiteral((string) $level0Type) . ', ' . $level0Name . ', ' .
'N' . $this->quoteStringLiteral((string) $level1Type) . ', ' . $level1Name . ', ' .
'N' . $this->quoteStringLiteral((string) $level2Type) . ', ' . $level2Name;
}
/**
@@ -921,7 +945,10 @@ SQL
}
/**
* {@inheritDoc}
* @param string $table
* @param string|null $database
*
* @return string
*/
public function getListTableForeignKeysSQL($table, $database = null)
{
@@ -945,7 +972,7 @@ SQL
/**
* {@inheritDoc}
*/
public function getListTableIndexesSQL($table, $currentDatabase = null)
public function getListTableIndexesSQL($table, $database = null)
{
return "SELECT idx.name AS key_name,
col.name AS column_name,
@@ -1045,10 +1072,10 @@ SQL
/**
* {@inheritDoc}
*/
public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = false)
public function getTrimExpression($str, $mode = TrimMode::UNSPECIFIED, $char = false)
{
if (! $char) {
switch ($pos) {
switch ($mode) {
case TrimMode::LEADING:
$trimFn = 'LTRIM';
break;
@@ -1064,26 +1091,20 @@ SQL
return $trimFn . '(' . $str . ')';
}
/** Original query used to get those expressions
declare @c varchar(100) = 'xxxBarxxx', @trim_char char(1) = 'x';
declare @pat varchar(10) = '%[^' + @trim_char + ']%';
select @c as string
, @trim_char as trim_char
, stuff(@c, 1, patindex(@pat, @c) - 1, null) as trim_leading
, reverse(stuff(reverse(@c), 1, patindex(@pat, reverse(@c)) - 1, null)) as trim_trailing
, reverse(stuff(reverse(stuff(@c, 1, patindex(@pat, @c) - 1, null)), 1, patindex(@pat, reverse(stuff(@c, 1, patindex(@pat, @c) - 1, null))) - 1, null)) as trim_both;
*/
$pattern = "'%[^' + " . $char . " + ']%'";
if ($pos === TrimMode::LEADING) {
if ($mode === TrimMode::LEADING) {
return 'stuff(' . $str . ', 1, patindex(' . $pattern . ', ' . $str . ') - 1, null)';
}
if ($pos === TrimMode::TRAILING) {
return 'reverse(stuff(reverse(' . $str . '), 1, patindex(' . $pattern . ', reverse(' . $str . ')) - 1, null))';
if ($mode === TrimMode::TRAILING) {
return 'reverse(stuff(reverse(' . $str . '), 1, '
. 'patindex(' . $pattern . ', reverse(' . $str . ')) - 1, null))';
}
return 'reverse(stuff(reverse(stuff(' . $str . ', 1, patindex(' . $pattern . ', ' . $str . ') - 1, null)), 1, patindex(' . $pattern . ', reverse(stuff(' . $str . ', 1, patindex(' . $pattern . ', ' . $str . ') - 1, null))) - 1, null))';
return 'reverse(stuff(reverse(stuff(' . $str . ', 1, patindex(' . $pattern . ', ' . $str . ') - 1, null)), 1, '
. 'patindex(' . $pattern . ', reverse(stuff(' . $str . ', 1, patindex(' . $pattern . ', ' . $str
. ') - 1, null))) - 1, null))';
}
/**
@@ -1115,13 +1136,13 @@ SQL
/**
* {@inheritDoc}
*/
public function getSubstringExpression($value, $from, $length = null)
public function getSubstringExpression($string, $start, $length = null)
{
if ($length !== null) {
return 'SUBSTRING(' . $value . ', ' . $from . ', ' . $length . ')';
return 'SUBSTRING(' . $string . ', ' . $start . ', ' . $length . ')';
}
return 'SUBSTRING(' . $value . ', ' . $from . ', LEN(' . $value . ') - ' . $from . ' + 1)';
return 'SUBSTRING(' . $string . ', ' . $start . ', LEN(' . $string . ') - ' . $start . ' + 1)';
}
/**
@@ -1143,41 +1164,57 @@ SQL
/**
* {@inheritDoc}
*/
public function getIntegerTypeDeclarationSQL(array $field)
public function getIntegerTypeDeclarationSQL(array $column)
{
return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
public function getBigIntTypeDeclarationSQL(array $field)
public function getBigIntTypeDeclarationSQL(array $column)
{
return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
public function getSmallIntTypeDeclarationSQL(array $field)
public function getSmallIntTypeDeclarationSQL(array $column)
{
return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
public function getGuidTypeDeclarationSQL(array $field)
public function getGuidTypeDeclarationSQL(array $column)
{
return 'UNIQUEIDENTIFIER';
}
/**
* {@inheritDoc}
*/
public function getAsciiStringTypeDeclarationSQL(array $column): string
{
$length = $column['length'] ?? null;
if (! isset($column['fixed'])) {
return sprintf('VARCHAR(%d)', $length ?? 255);
}
return sprintf('CHAR(%d)', $length ?? 255);
}
/**
* {@inheritDoc}
*/
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
{
return $fixed ? ($length ? 'NCHAR(' . $length . ')' : 'CHAR(255)') : ($length ? 'NVARCHAR(' . $length . ')' : 'NVARCHAR(255)');
return $fixed
? ($length ? 'NCHAR(' . $length . ')' : 'CHAR(255)')
: ($length ? 'NVARCHAR(' . $length . ')' : 'NVARCHAR(255)');
}
/**
@@ -1199,7 +1236,7 @@ SQL
/**
* {@inheritDoc}
*/
public function getClobTypeDeclarationSQL(array $field)
public function getClobTypeDeclarationSQL(array $column)
{
return 'VARCHAR(MAX)';
}
@@ -1207,15 +1244,15 @@ SQL
/**
* {@inheritDoc}
*/
protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
protected function _getCommonIntegerTypeDeclarationSQL(array $column)
{
return ! empty($columnDef['autoincrement']) ? ' IDENTITY' : '';
return ! empty($column['autoincrement']) ? ' IDENTITY' : '';
}
/**
* {@inheritDoc}
*/
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTimeTypeDeclarationSQL(array $column)
{
return 'DATETIME';
}
@@ -1223,7 +1260,7 @@ SQL
/**
* {@inheritDoc}
*/
public function getDateTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTypeDeclarationSQL(array $column)
{
return 'DATETIME';
}
@@ -1231,7 +1268,7 @@ SQL
/**
* {@inheritDoc}
*/
public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getTimeTypeDeclarationSQL(array $column)
{
return 'DATETIME';
}
@@ -1239,7 +1276,7 @@ SQL
/**
* {@inheritDoc}
*/
public function getBooleanTypeDeclarationSQL(array $field)
public function getBooleanTypeDeclarationSQL(array $column)
{
return 'BIT';
}
@@ -1270,9 +1307,11 @@ SQL
// Even if the TOP n is very large, the use of a CTE will
// allow the SQL Server query planner to optimize it so it doesn't
// actually scan the entire range covered by the TOP clause.
$selectPattern = '/^(\s*SELECT\s+(?:DISTINCT\s+)?)(.*)$/im';
$replacePattern = sprintf('$1%s $2', $top);
$query = preg_replace($selectPattern, $replacePattern, $query);
if (! preg_match('/^(\s*SELECT\s+(?:DISTINCT\s+)?)(.*)$/is', $query, $matches)) {
return $query;
}
$query = $matches[1] . $top . ' ' . $matches[2];
if (stristr($query, 'ORDER BY')) {
// Inner order by is not valid in SQL Server for our purposes
@@ -1339,6 +1378,7 @@ SQL
$query = substr($query, 0, $orderByPos) . substr($query, $currentPosition - 1);
$offset = $orderByPos;
}
return $query;
}
@@ -1389,7 +1429,7 @@ SQL
{
if (is_array($item)) {
foreach ($item as $key => $value) {
if (! is_bool($value) && ! is_numeric($item)) {
if (! is_bool($value) && ! is_numeric($value)) {
continue;
}
@@ -1536,7 +1576,7 @@ SQL
{
switch (true) {
case $lockMode === LockMode::NONE:
return $fromClause . ' WITH (NOLOCK)';
return $fromClause;
case $lockMode === LockMode::PESSIMISTIC_READ:
return $fromClause . ' WITH (HOLDLOCK, ROWLOCK)';
@@ -1570,7 +1610,7 @@ SQL
*/
public function quoteSingleIdentifier($str)
{
return '[' . str_replace(']', '][', $str) . ']';
return '[' . str_replace(']', ']]', $str) . ']';
}
/**
@@ -1586,63 +1626,33 @@ SQL
/**
* {@inheritDoc}
*/
public function getBlobTypeDeclarationSQL(array $field)
public function getBlobTypeDeclarationSQL(array $column)
{
return 'VARBINARY(MAX)';
}
/**
* {@inheritDoc}
*/
public function getDefaultValueDeclarationSQL($field)
{
if (! isset($field['default'])) {
return empty($field['notnull']) ? ' NULL' : '';
}
if (! isset($field['type'])) {
return " DEFAULT '" . $field['default'] . "'";
}
$type = $field['type'];
if ($type instanceof Types\PhpIntegerMappingType) {
return ' DEFAULT ' . $field['default'];
}
if ($type instanceof Types\PhpDateTimeMappingType && $field['default'] === $this->getCurrentTimestampSQL()) {
return ' DEFAULT ' . $this->getCurrentTimestampSQL();
}
if ($type instanceof Types\BooleanType) {
return " DEFAULT '" . $this->convertBooleans($field['default']) . "'";
}
return " DEFAULT '" . $field['default'] . "'";
}
/**
* {@inheritdoc}
*
* Modifies column declaration order as it differs in Microsoft SQL Server.
*/
public function getColumnDeclarationSQL($name, array $field)
public function getColumnDeclarationSQL($name, array $column)
{
if (isset($field['columnDefinition'])) {
$columnDef = $this->getCustomTypeDeclarationSQL($field);
if (isset($column['columnDefinition'])) {
$columnDef = $this->getCustomTypeDeclarationSQL($column);
} else {
$collation = isset($field['collation']) && $field['collation'] ?
' ' . $this->getColumnCollationDeclarationSQL($field['collation']) : '';
$collation = isset($column['collation']) && $column['collation'] ?
' ' . $this->getColumnCollationDeclarationSQL($column['collation']) : '';
$notnull = isset($field['notnull']) && $field['notnull'] ? ' NOT NULL' : '';
$notnull = isset($column['notnull']) && $column['notnull'] ? ' NOT NULL' : '';
$unique = isset($field['unique']) && $field['unique'] ?
$unique = isset($column['unique']) && $column['unique'] ?
' ' . $this->getUniqueFieldDeclarationSQL() : '';
$check = isset($field['check']) && $field['check'] ?
' ' . $field['check'] : '';
$check = isset($column['check']) && $column['check'] ?
' ' . $column['check'] : '';
$typeDecl = $field['type']->getSQLDeclaration($field, $this);
$typeDecl = $column['type']->getSQLDeclaration($column, $this);
$columnDef = $typeDecl . $collation . $notnull . $unique . $check;
}
@@ -1676,4 +1686,33 @@ SQL
return strtoupper(dechex(crc32($identifier->getName())));
}
protected function getCommentOnTableSQL(string $tableName, ?string $comment): string
{
return sprintf(
"
EXEC sys.sp_addextendedproperty @name=N'MS_Description',
@value=N%s, @level0type=N'SCHEMA', @level0name=N'dbo',
@level1type=N'TABLE', @level1name=N%s
",
$this->quoteStringLiteral((string) $comment),
$this->quoteStringLiteral($tableName)
);
}
public function getListTableMetadataSQL(string $table): string
{
return sprintf(
"
SELECT
p.value AS [table_comment]
FROM
sys.tables AS tbl
INNER JOIN sys.extended_properties AS p ON p.major_id=tbl.object_id AND p.minor_id=0 AND p.class=1
WHERE
(tbl.name=N%s and SCHEMA_NAME(tbl.schema_id)=N'dbo' and p.name=N'MS_Description')
",
$this->quoteStringLiteral($table)
);
}
}

View File

@@ -2,7 +2,7 @@
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Constraint;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
@@ -12,6 +12,7 @@ use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types;
use function array_merge;
use function array_unique;
use function array_values;
@@ -23,6 +24,7 @@ use function str_replace;
use function strlen;
use function strpos;
use function strtolower;
use function trim;
/**
* The SqlitePlatform class describes the specifics and dialects of the SQLite
@@ -54,15 +56,19 @@ class SqlitePlatform extends AbstractPlatform
}
/**
* {@inheritDoc}
* @param string $type
*
* @return string
*/
public function getNowExpression($type = 'timestamp')
{
switch ($type) {
case 'time':
return 'time(\'now\')';
case 'date':
return 'date(\'now\')';
case 'timestamp':
default:
return 'datetime(\'now\')';
@@ -72,11 +78,11 @@ class SqlitePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = false)
public function getTrimExpression($str, $mode = TrimMode::UNSPECIFIED, $char = false)
{
$trimChar = $char !== false ? (', ' . $char) : '';
$trimChar = $char !== false ? ', ' . $char : '';
switch ($pos) {
switch ($mode) {
case TrimMode::LEADING:
$trimFn = 'LTRIM';
break;
@@ -97,13 +103,13 @@ class SqlitePlatform extends AbstractPlatform
*
* SQLite only supports the 2 parameter variant of this function
*/
public function getSubstringExpression($value, $position, $length = null)
public function getSubstringExpression($string, $start, $length = null)
{
if ($length !== null) {
return 'SUBSTR(' . $value . ', ' . $position . ', ' . $length . ')';
return 'SUBSTR(' . $string . ', ' . $start . ', ' . $length . ')';
}
return 'SUBSTR(' . $value . ', ' . $position . ', LENGTH(' . $value . '))';
return 'SUBSTR(' . $string . ', ' . $start . ', LENGTH(' . $string . '))';
}
/**
@@ -128,26 +134,25 @@ class SqlitePlatform extends AbstractPlatform
case DateIntervalUnit::MINUTE:
case DateIntervalUnit::HOUR:
return 'DATETIME(' . $date . ",'" . $operator . $interval . ' ' . $unit . "')";
default:
switch ($unit) {
case DateIntervalUnit::WEEK:
$interval *= 7;
$unit = DateIntervalUnit::DAY;
break;
case DateIntervalUnit::QUARTER:
$interval *= 3;
$unit = DateIntervalUnit::MONTH;
break;
}
if (! is_numeric($interval)) {
$interval = "' || " . $interval . " || '";
}
return 'DATE(' . $date . ",'" . $operator . $interval . ' ' . $unit . "')";
}
switch ($unit) {
case DateIntervalUnit::WEEK:
$interval *= 7;
$unit = DateIntervalUnit::DAY;
break;
case DateIntervalUnit::QUARTER:
$interval *= 3;
$unit = DateIntervalUnit::MONTH;
break;
}
if (! is_numeric($interval)) {
$interval = "' || " . $interval . " || '";
}
return 'DATE(' . $date . ",'" . $operator . $interval . ' ' . $unit . "')";
}
/**
@@ -165,11 +170,13 @@ class SqlitePlatform extends AbstractPlatform
{
switch ($level) {
case TransactionIsolationLevel::READ_UNCOMMITTED:
return 0;
return '0';
case TransactionIsolationLevel::READ_COMMITTED:
case TransactionIsolationLevel::REPEATABLE_READ:
case TransactionIsolationLevel::SERIALIZABLE:
return 1;
return '1';
default:
return parent::_getTransactionIsolationLevelSQL($level);
}
@@ -194,7 +201,7 @@ class SqlitePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getBooleanTypeDeclarationSQL(array $field)
public function getBooleanTypeDeclarationSQL(array $column)
{
return 'BOOLEAN';
}
@@ -202,67 +209,71 @@ class SqlitePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getIntegerTypeDeclarationSQL(array $field)
public function getIntegerTypeDeclarationSQL(array $column)
{
return 'INTEGER' . $this->_getCommonIntegerTypeDeclarationSQL($field);
return 'INTEGER' . $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
public function getBigIntTypeDeclarationSQL(array $field)
public function getBigIntTypeDeclarationSQL(array $column)
{
// SQLite autoincrement is implicit for INTEGER PKs, but not for BIGINT fields.
if (! empty($field['autoincrement'])) {
return $this->getIntegerTypeDeclarationSQL($field);
// SQLite autoincrement is implicit for INTEGER PKs, but not for BIGINT columns
if (! empty($column['autoincrement'])) {
return $this->getIntegerTypeDeclarationSQL($column);
}
return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
* {@inheritDoc}
* @param array<string, mixed> $column
*
* @return string
*/
public function getTinyIntTypeDeclarationSql(array $field)
public function getTinyIntTypeDeclarationSql(array $column)
{
// SQLite autoincrement is implicit for INTEGER PKs, but not for TINYINT fields.
if (! empty($field['autoincrement'])) {
return $this->getIntegerTypeDeclarationSQL($field);
// SQLite autoincrement is implicit for INTEGER PKs, but not for TINYINT columns
if (! empty($column['autoincrement'])) {
return $this->getIntegerTypeDeclarationSQL($column);
}
return 'TINYINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
return 'TINYINT' . $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
public function getSmallIntTypeDeclarationSQL(array $field)
public function getSmallIntTypeDeclarationSQL(array $column)
{
// SQLite autoincrement is implicit for INTEGER PKs, but not for SMALLINT fields.
if (! empty($field['autoincrement'])) {
return $this->getIntegerTypeDeclarationSQL($field);
// SQLite autoincrement is implicit for INTEGER PKs, but not for SMALLINT columns
if (! empty($column['autoincrement'])) {
return $this->getIntegerTypeDeclarationSQL($column);
}
return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
* {@inheritDoc}
* @param array<string, mixed> $column
*
* @return string
*/
public function getMediumIntTypeDeclarationSql(array $field)
public function getMediumIntTypeDeclarationSql(array $column)
{
// SQLite autoincrement is implicit for INTEGER PKs, but not for MEDIUMINT fields.
if (! empty($field['autoincrement'])) {
return $this->getIntegerTypeDeclarationSQL($field);
// SQLite autoincrement is implicit for INTEGER PKs, but not for MEDIUMINT columns
if (! empty($column['autoincrement'])) {
return $this->getIntegerTypeDeclarationSQL($column);
}
return 'MEDIUMINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
return 'MEDIUMINT' . $this->_getCommonIntegerTypeDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTimeTypeDeclarationSQL(array $column)
{
return 'DATETIME';
}
@@ -270,7 +281,7 @@ class SqlitePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getDateTypeDeclarationSQL(array $fieldDeclaration)
public function getDateTypeDeclarationSQL(array $column)
{
return 'DATE';
}
@@ -278,7 +289,7 @@ class SqlitePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
public function getTimeTypeDeclarationSQL(array $column)
{
return 'TIME';
}
@@ -286,14 +297,14 @@ class SqlitePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
protected function _getCommonIntegerTypeDeclarationSQL(array $column)
{
// sqlite autoincrement is only possible for the primary key
if (! empty($columnDef['autoincrement'])) {
if (! empty($column['autoincrement'])) {
return ' PRIMARY KEY AUTOINCREMENT';
}
return ! empty($columnDef['unsigned']) ? ' UNSIGNED' : '';
return ! empty($column['unsigned']) ? ' UNSIGNED' : '';
}
/**
@@ -332,7 +343,14 @@ class SqlitePlatform extends AbstractPlatform
}
}
$query = ['CREATE TABLE ' . $name . ' (' . $queryFields . ')'];
$tableComment = '';
if (isset($options['comment'])) {
$comment = trim($options['comment'], " '");
$tableComment = $this->getInlineTableCommentSQL($comment);
}
$query = ['CREATE TABLE ' . $name . ' ' . $tableComment . '(' . $queryFields . ')'];
if (isset($options['alter']) && $options['alter'] === true) {
return $query;
@@ -356,10 +374,10 @@ class SqlitePlatform extends AbstractPlatform
/**
* Generate a PRIMARY KEY definition if no autoincrement value is used
*
* @param string[] $columns
* @param mixed[] $options
* @param mixed[][] $columns
* @param mixed[] $options
*/
private function getNonAutoincrementPrimaryKeyDefinition(array $columns, array $options) : string
private function getNonAutoincrementPrimaryKeyDefinition(array $columns, array $options): string
{
if (empty($options['primary'])) {
return '';
@@ -368,7 +386,7 @@ class SqlitePlatform extends AbstractPlatform
$keyColumns = array_unique(array_values($options['primary']));
foreach ($keyColumns as $keyColumn) {
if (isset($columns[$keyColumn]['autoincrement']) && ! empty($columns[$keyColumn]['autoincrement'])) {
if (! empty($columns[$keyColumn]['autoincrement'])) {
return '';
}
}
@@ -412,7 +430,7 @@ class SqlitePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getClobTypeDeclarationSQL(array $field)
public function getClobTypeDeclarationSQL(array $column)
{
return 'CLOB';
}
@@ -433,7 +451,7 @@ class SqlitePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getListTableColumnsSQL($table, $currentDatabase = null)
public function getListTableColumnsSQL($table, $database = null)
{
$table = str_replace('.', '__', $table);
@@ -443,7 +461,7 @@ class SqlitePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getListTableIndexesSQL($table, $currentDatabase = null)
public function getListTableIndexesSQL($table, $database = null)
{
$table = str_replace('.', '__', $table);
@@ -455,9 +473,13 @@ class SqlitePlatform extends AbstractPlatform
*/
public function getListTablesSQL()
{
return "SELECT name FROM sqlite_master WHERE type = 'table' AND name != 'sqlite_sequence' AND name != 'geometry_columns' AND name != 'spatial_ref_sys' "
. 'UNION ALL SELECT name FROM sqlite_temp_master '
. "WHERE type = 'table' ORDER BY name";
return 'SELECT name FROM sqlite_master'
. " WHERE type = 'table'"
. " AND name != 'sqlite_sequence'"
. " AND name != 'geometry_columns'"
. " AND name != 'spatial_ref_sys'"
. ' UNION ALL SELECT name FROM sqlite_temp_master'
. " WHERE type = 'table' ORDER BY name";
}
/**
@@ -491,8 +513,18 @@ class SqlitePlatform extends AbstractPlatform
{
$query = parent::getAdvancedForeignKeyOptionsSQL($foreignKey);
$query .= ($foreignKey->hasOption('deferrable') && $foreignKey->getOption('deferrable') !== false ? ' ' : ' NOT ') . 'DEFERRABLE';
$query .= ' INITIALLY ' . ($foreignKey->hasOption('deferred') && $foreignKey->getOption('deferred') !== false ? 'DEFERRED' : 'IMMEDIATE');
if (! $foreignKey->hasOption('deferrable') || $foreignKey->getOption('deferrable') === false) {
$query .= ' NOT';
}
$query .= ' DEFERRABLE';
$query .= ' INITIALLY';
if ($foreignKey->hasOption('deferred') && $foreignKey->getOption('deferred') !== false) {
$query .= ' DEFERRED';
} else {
$query .= ' IMMEDIATE';
}
return $query;
}
@@ -592,7 +624,7 @@ class SqlitePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getForUpdateSql()
public function getForUpdateSQL()
{
return '';
}
@@ -605,6 +637,11 @@ class SqlitePlatform extends AbstractPlatform
return '--' . str_replace("\n", "\n--", $comment) . "\n";
}
private function getInlineTableCommentSQL(string $comment): string
{
return $this->getInlineColumnCommentSQL($comment);
}
/**
* {@inheritDoc}
*/
@@ -660,7 +697,9 @@ class SqlitePlatform extends AbstractPlatform
protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
{
if (! $diff->fromTable instanceof Table) {
throw new DBALException('Sqlite platform requires for alter table the table diff with reference to original table schema');
throw new Exception(
'Sqlite platform requires for alter table the table diff with reference to original table schema'
);
}
$sql = [];
@@ -680,13 +719,22 @@ class SqlitePlatform extends AbstractPlatform
*/
protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff)
{
if (! $diff->fromTable instanceof Table) {
throw new DBALException('Sqlite platform requires for alter table the table diff with reference to original table schema');
$fromTable = $diff->fromTable;
if (! $fromTable instanceof Table) {
throw new Exception(
'Sqlite platform requires for alter table the table diff with reference to original table schema'
);
}
$sql = [];
$tableName = $diff->newName ? $diff->getNewName(): $diff->getName($this);
foreach ($this->getIndexesInAlteredTable($diff) as $index) {
$tableName = $diff->getNewName();
if ($tableName === false) {
$tableName = $diff->getName($this);
}
foreach ($this->getIndexesInAlteredTable($diff, $fromTable) as $index) {
if ($index->isPrimary()) {
continue;
}
@@ -712,7 +760,7 @@ class SqlitePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getBlobTypeDeclarationSQL(array $field)
public function getBlobTypeDeclarationSQL(array $column)
{
return 'BLOB';
}
@@ -754,7 +802,7 @@ class SqlitePlatform extends AbstractPlatform
*/
public function getCreatePrimaryKeySQL(Index $index, $table)
{
throw new DBALException('Sqlite platform does not support alter primary key.');
throw new Exception('Sqlite platform does not support alter primary key.');
}
/**
@@ -762,7 +810,7 @@ class SqlitePlatform extends AbstractPlatform
*/
public function getCreateForeignKeySQL(ForeignKeyConstraint $foreignKey, $table)
{
throw new DBALException('Sqlite platform does not support alter foreign key.');
throw new Exception('Sqlite platform does not support alter foreign key.');
}
/**
@@ -770,7 +818,7 @@ class SqlitePlatform extends AbstractPlatform
*/
public function getDropForeignKeySQL($foreignKey, $table)
{
throw new DBALException('Sqlite platform does not support alter foreign key.');
throw new Exception('Sqlite platform does not support alter foreign key.');
}
/**
@@ -778,11 +826,13 @@ class SqlitePlatform extends AbstractPlatform
*/
public function getCreateConstraintSQL(Constraint $constraint, $table)
{
throw new DBALException('Sqlite platform does not support alter constraint.');
throw new Exception('Sqlite platform does not support alter constraint.');
}
/**
* {@inheritDoc}
*
* @param int|null $createFlags
*/
public function getCreateTableSQL(Table $table, $createFlags = null)
{
@@ -792,7 +842,10 @@ class SqlitePlatform extends AbstractPlatform
}
/**
* {@inheritDoc}
* @param string $table
* @param string|null $database
*
* @return string
*/
public function getListTableForeignKeysSQL($table, $database = null)
{
@@ -813,7 +866,9 @@ class SqlitePlatform extends AbstractPlatform
$fromTable = $diff->fromTable;
if (! $fromTable instanceof Table) {
throw new DBALException('Sqlite platform requires for alter table the table diff with reference to original table schema');
throw new Exception(
'Sqlite platform requires for alter table the table diff with reference to original table schema'
);
}
$table = clone $fromTable;
@@ -896,21 +951,44 @@ class SqlitePlatform extends AbstractPlatform
if (! $this->onSchemaAlterTable($diff, $tableSql)) {
$dataTable = new Table('__temp__' . $table->getName());
$newTable = new Table($table->getQuotedName($this), $columns, $this->getPrimaryIndexInAlteredTable($diff), $this->getForeignKeysInAlteredTable($diff), 0, $table->getOptions());
$newTable = new Table(
$table->getQuotedName($this),
$columns,
$this->getPrimaryIndexInAlteredTable($diff, $fromTable),
$this->getForeignKeysInAlteredTable($diff, $fromTable),
0,
$table->getOptions()
);
$newTable->addOption('alter', true);
$sql = $this->getPreAlterTableIndexForeignKeySQL($diff);
//$sql = array_merge($sql, $this->getCreateTableSQL($dataTable, 0));
$sql[] = sprintf('CREATE TEMPORARY TABLE %s AS SELECT %s FROM %s', $dataTable->getQuotedName($this), implode(', ', $oldColumnNames), $table->getQuotedName($this));
$sql[] = sprintf(
'CREATE TEMPORARY TABLE %s AS SELECT %s FROM %s',
$dataTable->getQuotedName($this),
implode(', ', $oldColumnNames),
$table->getQuotedName($this)
);
$sql[] = $this->getDropTableSQL($fromTable);
$sql = array_merge($sql, $this->getCreateTableSQL($newTable));
$sql[] = sprintf('INSERT INTO %s (%s) SELECT %s FROM %s', $newTable->getQuotedName($this), implode(', ', $newColumnNames), implode(', ', $oldColumnNames), $dataTable->getQuotedName($this));
$sql[] = sprintf(
'INSERT INTO %s (%s) SELECT %s FROM %s',
$newTable->getQuotedName($this),
implode(', ', $newColumnNames),
implode(', ', $oldColumnNames),
$dataTable->getQuotedName($this)
);
$sql[] = $this->getDropTableSQL($dataTable);
if ($diff->newName && $diff->newName !== $diff->name) {
$renamedTable = $diff->getNewName();
$sql[] = 'ALTER TABLE ' . $newTable->getQuotedName($this) . ' RENAME TO ' . $renamedTable->getQuotedName($this);
$newName = $diff->getNewName();
if ($newName !== false) {
$sql[] = sprintf(
'ALTER TABLE %s RENAME TO %s',
$newTable->getQuotedName($this),
$newName->getQuotedName($this)
);
}
$sql = array_merge($sql, $this->getPostAlterTableIndexForeignKeySQL($diff));
@@ -926,7 +1004,8 @@ class SqlitePlatform extends AbstractPlatform
{
// Suppress changes on integer type autoincrement columns.
foreach ($diff->changedColumns as $oldColumnName => $columnDiff) {
if (! $columnDiff->fromColumn instanceof Column ||
if (
! $columnDiff->fromColumn instanceof Column ||
! $columnDiff->column instanceof Column ||
! $columnDiff->column->getAutoincrement() ||
! $columnDiff->column->getType() instanceof Types\IntegerType
@@ -949,10 +1028,17 @@ class SqlitePlatform extends AbstractPlatform
unset($diff->changedColumns[$oldColumnName]);
}
if (! empty($diff->renamedColumns) || ! empty($diff->addedForeignKeys) || ! empty($diff->addedIndexes)
|| ! empty($diff->changedColumns) || ! empty($diff->changedForeignKeys) || ! empty($diff->changedIndexes)
|| ! empty($diff->removedColumns) || ! empty($diff->removedForeignKeys) || ! empty($diff->removedIndexes)
|| ! empty($diff->renamedIndexes)
if (
! empty($diff->renamedColumns)
|| ! empty($diff->addedForeignKeys)
|| ! empty($diff->addedIndexes)
|| ! empty($diff->changedColumns)
|| ! empty($diff->changedForeignKeys)
|| ! empty($diff->changedIndexes)
|| ! empty($diff->removedColumns)
|| ! empty($diff->removedForeignKeys)
|| ! empty($diff->removedIndexes)
|| ! empty($diff->renamedIndexes)
) {
return false;
}
@@ -968,28 +1054,37 @@ class SqlitePlatform extends AbstractPlatform
continue;
}
$field = array_merge(['unique' => null, 'autoincrement' => null, 'default' => null], $column->toArray());
$type = $field['type'];
$definition = array_merge([
'unique' => null,
'autoincrement' => null,
'default' => null,
], $column->toArray());
$type = $definition['type'];
switch (true) {
case isset($field['columnDefinition']) || $field['autoincrement'] || $field['unique']:
case $type instanceof Types\DateTimeType && $field['default'] === $this->getCurrentTimestampSQL():
case $type instanceof Types\DateType && $field['default'] === $this->getCurrentDateSQL():
case $type instanceof Types\TimeType && $field['default'] === $this->getCurrentTimeSQL():
case isset($definition['columnDefinition']) || $definition['autoincrement'] || $definition['unique']:
case $type instanceof Types\DateTimeType && $definition['default'] === $this->getCurrentTimestampSQL():
case $type instanceof Types\DateType && $definition['default'] === $this->getCurrentDateSQL():
case $type instanceof Types\TimeType && $definition['default'] === $this->getCurrentTimeSQL():
return false;
}
$field['name'] = $column->getQuotedName($this);
if ($type instanceof Types\StringType && $field['length'] === null) {
$field['length'] = 255;
$definition['name'] = $column->getQuotedName($this);
if ($type instanceof Types\StringType && $definition['length'] === null) {
$definition['length'] = 255;
}
$sql[] = 'ALTER TABLE ' . $table->getQuotedName($this) . ' ADD COLUMN ' . $this->getColumnDeclarationSQL($field['name'], $field);
$sql[] = 'ALTER TABLE ' . $table->getQuotedName($this) . ' ADD COLUMN '
. $this->getColumnDeclarationSQL($definition['name'], $definition);
}
if (! $this->onSchemaAlterTable($diff, $tableSql)) {
if ($diff->newName !== false) {
$newTable = new Identifier($diff->newName);
$sql[] = 'ALTER TABLE ' . $table->getQuotedName($this) . ' RENAME TO ' . $newTable->getQuotedName($this);
$sql[] = 'ALTER TABLE ' . $table->getQuotedName($this) . ' RENAME TO '
. $newTable->getQuotedName($this);
}
}
@@ -999,11 +1094,11 @@ class SqlitePlatform extends AbstractPlatform
/**
* @return string[]
*/
private function getColumnNamesInAlteredTable(TableDiff $diff)
private function getColumnNamesInAlteredTable(TableDiff $diff, Table $fromTable)
{
$columns = [];
foreach ($diff->fromTable->getColumns() as $columnName => $column) {
foreach ($fromTable->getColumns() as $columnName => $column) {
$columns[strtolower($columnName)] = $column->getName();
}
@@ -1028,7 +1123,8 @@ class SqlitePlatform extends AbstractPlatform
$columns[strtolower($columnName)] = $columnName;
}
foreach ($diff->addedColumns as $columnName => $column) {
foreach ($diff->addedColumns as $column) {
$columnName = $column->getName();
$columns[strtolower($columnName)] = $columnName;
}
@@ -1038,10 +1134,10 @@ class SqlitePlatform extends AbstractPlatform
/**
* @return Index[]
*/
private function getIndexesInAlteredTable(TableDiff $diff)
private function getIndexesInAlteredTable(TableDiff $diff, Table $fromTable)
{
$indexes = $diff->fromTable->getIndexes();
$columnNames = $this->getColumnNamesInAlteredTable($diff);
$indexes = $fromTable->getIndexes();
$columnNames = $this->getColumnNamesInAlteredTable($diff, $fromTable);
foreach ($indexes as $key => $index) {
foreach ($diff->renamedIndexes as $oldIndexName => $renamedIndex) {
@@ -1059,19 +1155,27 @@ class SqlitePlatform extends AbstractPlatform
if (! isset($columnNames[$normalizedColumnName])) {
unset($indexes[$key]);
continue 2;
} else {
$indexColumns[] = $columnNames[$normalizedColumnName];
if ($columnName !== $columnNames[$normalizedColumnName]) {
$changed = true;
}
}
$indexColumns[] = $columnNames[$normalizedColumnName];
if ($columnName === $columnNames[$normalizedColumnName]) {
continue;
}
$changed = true;
}
if (! $changed) {
continue;
}
$indexes[$key] = new Index($index->getName(), $indexColumns, $index->isUnique(), $index->isPrimary(), $index->getFlags());
$indexes[$key] = new Index(
$index->getName(),
$indexColumns,
$index->isUnique(),
$index->isPrimary(),
$index->getFlags()
);
}
foreach ($diff->removedIndexes as $index) {
@@ -1098,10 +1202,10 @@ class SqlitePlatform extends AbstractPlatform
/**
* @return ForeignKeyConstraint[]
*/
private function getForeignKeysInAlteredTable(TableDiff $diff)
private function getForeignKeysInAlteredTable(TableDiff $diff, Table $fromTable)
{
$foreignKeys = $diff->fromTable->getForeignKeys();
$columnNames = $this->getColumnNamesInAlteredTable($diff);
$foreignKeys = $fromTable->getForeignKeys();
$columnNames = $this->getColumnNamesInAlteredTable($diff, $fromTable);
foreach ($foreignKeys as $key => $constraint) {
$changed = false;
@@ -1111,22 +1215,34 @@ class SqlitePlatform extends AbstractPlatform
if (! isset($columnNames[$normalizedColumnName])) {
unset($foreignKeys[$key]);
continue 2;
} else {
$localColumns[] = $columnNames[$normalizedColumnName];
if ($columnName !== $columnNames[$normalizedColumnName]) {
$changed = true;
}
}
$localColumns[] = $columnNames[$normalizedColumnName];
if ($columnName === $columnNames[$normalizedColumnName]) {
continue;
}
$changed = true;
}
if (! $changed) {
continue;
}
$foreignKeys[$key] = new ForeignKeyConstraint($localColumns, $constraint->getForeignTableName(), $constraint->getForeignColumns(), $constraint->getName(), $constraint->getOptions());
$foreignKeys[$key] = new ForeignKeyConstraint(
$localColumns,
$constraint->getForeignTableName(),
$constraint->getForeignColumns(),
$constraint->getName(),
$constraint->getOptions()
);
}
foreach ($diff->removedForeignKeys as $constraint) {
if (! $constraint instanceof ForeignKeyConstraint) {
$constraint = new Identifier($constraint);
}
$constraintName = strtolower($constraint->getName());
if (! strlen($constraintName) || ! isset($foreignKeys[$constraintName])) {
continue;
@@ -1150,11 +1266,11 @@ class SqlitePlatform extends AbstractPlatform
/**
* @return Index[]
*/
private function getPrimaryIndexInAlteredTable(TableDiff $diff)
private function getPrimaryIndexInAlteredTable(TableDiff $diff, Table $fromTable)
{
$primaryIndex = [];
foreach ($this->getIndexesInAlteredTable($diff) as $index) {
foreach ($this->getIndexesInAlteredTable($diff, $fromTable) as $index) {
if (! $index->isPrimary()) {
continue;
}

View File

@@ -14,6 +14,9 @@ final class TrimMode
public const BOTH = 3;
/**
* @codeCoverageIgnore
*/
private function __construct()
{
}