Commit 1605e5c3 authored by Benjamin Eberlei's avatar Benjamin Eberlei

DDC-1202 fix identifier quoting on MSSQL Platform

parent 0985b38b
...@@ -22,7 +22,8 @@ namespace Doctrine\DBAL\Platforms; ...@@ -22,7 +22,8 @@ namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\Index, Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Index,
Doctrine\DBAL\Schema\Table;
/** /**
* The MsSqlPlatform provides the behavior, features and SQL dialect of the * The MsSqlPlatform provides the behavior, features and SQL dialect of the
...@@ -99,7 +100,7 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -99,7 +100,7 @@ class MsSqlPlatform extends AbstractPlatform
/** /**
* @override * @override
*/ */
public function supportsCreateDropDatabase() public function supportsCreateDropDatabase()
{ {
return false; return false;
} }
...@@ -151,14 +152,14 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -151,14 +152,14 @@ class MsSqlPlatform extends AbstractPlatform
*/ */
protected function _getCreateTableSQL($tableName, array $columns, array $options = array()) protected function _getCreateTableSQL($tableName, array $columns, array $options = array())
{ {
// @todo does other code breaks because of this? // @todo does other code breaks because of this?
// foce primary keys to be not null // foce primary keys to be not null
foreach ($columns as &$column) { foreach ($columns as &$column) {
if (isset($column['primary']) && $column['primary']) { if (isset($column['primary']) && $column['primary']) {
$column['notnull'] = true; $column['notnull'] = true;
} }
} }
$columnListSql = $this->getColumnDeclarationListSQL($columns); $columnListSql = $this->getColumnDeclarationListSQL($columns);
if (isset($options['uniqueConstraints']) && !empty($options['uniqueConstraints'])) { if (isset($options['uniqueConstraints']) && !empty($options['uniqueConstraints'])) {
...@@ -195,53 +196,53 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -195,53 +196,53 @@ class MsSqlPlatform extends AbstractPlatform
return $sql; return $sql;
} }
/** /**
* @override * @override
*/ */
public function getUniqueConstraintDeclarationSQL($name, Index $index) public function getUniqueConstraintDeclarationSQL($name, Index $index)
{ {
$constraint = parent::getUniqueConstraintDeclarationSQL($name, $index); $constraint = parent::getUniqueConstraintDeclarationSQL($name, $index);
$constraint = $this->_appendUniqueConstraintDefinition($constraint, $index); $constraint = $this->_appendUniqueConstraintDefinition($constraint, $index);
return $constraint; return $constraint;
} }
/** /**
* @override * @override
*/ */
public function getCreateIndexSQL(Index $index, $table) public function getCreateIndexSQL(Index $index, $table)
{ {
$constraint = parent::getCreateIndexSQL($index, $table); $constraint = parent::getCreateIndexSQL($index, $table);
if ($index->isUnique()) { if ($index->isUnique()) {
$constraint = $this->_appendUniqueConstraintDefinition($constraint, $index); $constraint = $this->_appendUniqueConstraintDefinition($constraint, $index);
} }
return $constraint; return $constraint;
} }
/** /**
* Extend unique key constraint with required filters * Extend unique key constraint with required filters
* *
* @param string $sql * @param string $sql
* @param Index $index * @param Index $index
* @return string * @return string
*/ */
private function _appendUniqueConstraintDefinition($sql, Index $index) private function _appendUniqueConstraintDefinition($sql, Index $index)
{ {
$fields = array(); $fields = array();
foreach ($index->getColumns() as $field => $definition) { foreach ($index->getColumns() as $field => $definition) {
if (!is_array($definition)) { if (!is_array($definition)) {
$field = $definition; $field = $definition;
} }
$fields[] = $field . ' IS NOT NULL'; $fields[] = $field . ' IS NOT NULL';
} }
return $sql . ' WHERE ' . implode(' AND ', $fields); return $sql . ' WHERE ' . implode(' AND ', $fields);
} }
/** /**
* @override * @override
...@@ -418,36 +419,36 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -418,36 +419,36 @@ class MsSqlPlatform extends AbstractPlatform
{ {
$trimFn = ''; $trimFn = '';
if (!$char) { if (!$char) {
if ($pos == self::TRIM_LEADING) { if ($pos == self::TRIM_LEADING) {
$trimFn = 'LTRIM'; $trimFn = 'LTRIM';
} else if ($pos == self::TRIM_TRAILING) { } else if ($pos == self::TRIM_TRAILING) {
$trimFn = 'RTRIM'; $trimFn = 'RTRIM';
} else { } else {
return 'LTRIM(RTRIM(' . $str . '))'; return 'LTRIM(RTRIM(' . $str . '))';
} }
return $trimFn . '(' . $str . ')'; return $trimFn . '(' . $str . ')';
} else { } else {
/** Original query used to get those expressions /** Original query used to get those expressions
declare @c varchar(100) = 'xxxBarxxx', @trim_char char(1) = 'x'; declare @c varchar(100) = 'xxxBarxxx', @trim_char char(1) = 'x';
declare @pat varchar(10) = '%[^' + @trim_char + ']%'; declare @pat varchar(10) = '%[^' + @trim_char + ']%';
select @c as string select @c as string
, @trim_char as trim_char , @trim_char as trim_char
, stuff(@c, 1, patindex(@pat, @c) - 1, null) as trim_leading , 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(@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; , 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 + ']%'"; $pattern = "'%[^' + $char + ']%'";
if ($pos == self::TRIM_LEADING) { if ($pos == self::TRIM_LEADING) {
return 'stuff(' . $str . ', 1, patindex(' . $pattern .', ' . $str . ') - 1, null)'; return 'stuff(' . $str . ', 1, patindex(' . $pattern . ', ' . $str . ') - 1, null)';
} else if ($pos == self::TRIM_TRAILING) { } else if ($pos == self::TRIM_TRAILING) {
return 'reverse(stuff(reverse(' . $str . '), 1, patindex(' . $pattern .', reverse(' . $str . ')) - 1, null))'; return 'reverse(stuff(reverse(' . $str . '), 1, patindex(' . $pattern . ', reverse(' . $str . ')) - 1, null))';
} else { } else {
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))';
} }
} }
} }
/** /**
...@@ -687,34 +688,34 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -687,34 +688,34 @@ class MsSqlPlatform extends AbstractPlatform
protected function initializeDoctrineTypeMappings() protected function initializeDoctrineTypeMappings()
{ {
$this->doctrineTypeMapping = array( $this->doctrineTypeMapping = array(
'bigint' => 'bigint', 'bigint' => 'bigint',
'numeric' => 'decimal', 'numeric' => 'decimal',
'bit' => 'boolean', 'bit' => 'boolean',
'smallint' => 'smallint', 'smallint' => 'smallint',
'decimal' => 'decimal', 'decimal' => 'decimal',
'smallmoney' => 'integer', 'smallmoney' => 'integer',
'int' => 'integer', 'int' => 'integer',
'tinyint' => 'smallint', 'tinyint' => 'smallint',
'money' => 'integer', 'money' => 'integer',
'float' => 'float', 'float' => 'float',
'real' => 'float', 'real' => 'float',
'double' => 'float', 'double' => 'float',
'double precision' => 'float', 'double precision' => 'float',
'date' => 'date', 'date' => 'date',
'datetimeoffset' => 'datetimetz', 'datetimeoffset' => 'datetimetz',
'datetime2' => 'datetime', 'datetime2' => 'datetime',
'smalldatetime' => 'datetime', 'smalldatetime' => 'datetime',
'datetime' => 'datetime', 'datetime' => 'datetime',
'time' => 'time', 'time' => 'time',
'char' => 'string', 'char' => 'string',
'varchar' => 'string', 'varchar' => 'string',
'text' => 'text', 'text' => 'text',
'nchar' => 'string', 'nchar' => 'string',
'nvarchar' => 'string', 'nvarchar' => 'string',
'ntext' => 'text', 'ntext' => 'text',
'binary' => 'text', 'binary' => 'text',
'varbinary' => 'text', 'varbinary' => 'text',
'image' => 'text', 'image' => 'text',
); );
} }
...@@ -750,21 +751,20 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -750,21 +751,20 @@ class MsSqlPlatform extends AbstractPlatform
{ {
return 'ROLLBACK TRANSACTION ' . $savepoint; return 'ROLLBACK TRANSACTION ' . $savepoint;
} }
/** /**
* @override * @override
*/ */
public function appendLockHint($fromClause, $lockMode) public function appendLockHint($fromClause, $lockMode)
{ {
// @todo coorect // @todo coorect
if ($lockMode == \Doctrine\DBAL\LockMode::PESSIMISTIC_READ) { if ($lockMode == \Doctrine\DBAL\LockMode::PESSIMISTIC_READ) {
return $fromClause . ' WITH (tablockx)'; return $fromClause . ' WITH (tablockx)';
} else if ($lockMode == \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE) { } else if ($lockMode == \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE) {
return $fromClause . ' WITH (tablockx)'; return $fromClause . ' WITH (tablockx)';
} else {
return $fromClause;
} }
else {
return $fromClause;
}
} }
/** /**
...@@ -774,9 +774,25 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -774,9 +774,25 @@ class MsSqlPlatform extends AbstractPlatform
{ {
return ' '; return ' ';
} }
protected function getReservedKeywordsClass() protected function getReservedKeywordsClass()
{ {
return 'Doctrine\DBAL\Platforms\Keywords\MsSQLKeywords'; return 'Doctrine\DBAL\Platforms\Keywords\MsSQLKeywords';
} }
/**
* Quotes a string so that it can be safely used as a table or column name,
* even if it is a reserved word of the platform.
*
* NOTE: Just because you CAN use quoted identifiers doesn't mean
* you SHOULD use them. In general, they end up causing way more
* problems than they solve.
*
* @param string $str identifier name to be quoted
* @return string quoted identifier string
*/
public function quoteIdentifier($str)
{
return "[" . $str . "]";
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment