Commit 03af26ab authored by Benjamin Eberlei's avatar Benjamin Eberlei

DBAL-64 - Fix optional quoting of identifiers in DBAL\Schema assets.

parent 3d7bb1e6
......@@ -21,6 +21,8 @@
namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* The abstract asset allows to reset the name of all assets without publishing this to the public userland.
*
......@@ -40,6 +42,8 @@ abstract class AbstractAsset
*/
protected $_name;
protected $_quoted = false;
/**
* Set name of this asset
*
......@@ -47,6 +51,10 @@ abstract class AbstractAsset
*/
protected function _setName($name)
{
if (strlen($name) && $name[0] == '`') {
$this->_quoted = true;
$name = trim($name, '`');
}
$this->_name = $name;
}
......@@ -60,6 +68,18 @@ abstract class AbstractAsset
return $this->_name;
}
/**
* Get the quoted representation of this asset but only if it was defined with one. Otherwise
* return the plain unquoted value as inserted.
*
* @param AbstractPlatform $platform
* @return string
*/
public function getQuotedName(AbstractPlatform $platform)
{
return ($this->_quoted) ? $platform->quoteIdentifier($this->_name) : $this->_name;
}
/**
* Generate an identifier from a list of column names obeying a certain string length.
*
......
......@@ -293,7 +293,7 @@ abstract class AbstractSchemaManager
public function dropIndex($index, $table)
{
if($index instanceof Index) {
$index = $index->getName();
$index = $index->getQuotedName($this->_platform);
}
$this->_execSql($this->_platform->getDropIndexSQL($index, $table));
......@@ -418,7 +418,7 @@ abstract class AbstractSchemaManager
*/
public function createView(View $view)
{
$this->_execSql($this->_platform->getCreateViewSQL($view->getName(), $view->getSql()));
$this->_execSql($this->_platform->getCreateViewSQL($view->getQuotedName($this->_platform), $view->getSql()));
}
/* dropAndCreate*() Methods */
......@@ -445,7 +445,7 @@ abstract class AbstractSchemaManager
*/
public function dropAndCreateIndex(Index $index, $table)
{
$this->tryMethod('dropIndex', $index->getName(), $table);
$this->tryMethod('dropIndex', $index->getQuotedName($this->_platform), $table);
$this->createIndex($index, $table);
}
......@@ -480,7 +480,7 @@ abstract class AbstractSchemaManager
*/
public function dropAndCreateTable(Table $table)
{
$this->tryMethod('dropTable', $table->getName());
$this->tryMethod('dropTable', $table->getQuotedName($this->_platform));
$this->createTable($table);
}
......@@ -502,7 +502,7 @@ abstract class AbstractSchemaManager
*/
public function dropAndCreateView(View $view)
{
$this->tryMethod('dropView', $view->getName());
$this->tryMethod('dropView', $view->getQuotedName($this->_platform));
$this->createView($view);
}
......@@ -622,7 +622,7 @@ abstract class AbstractSchemaManager
$list = array();
foreach ($tableColumns as $key => $column) {
if ($column = $this->_getPortableTableColumnDefinition($column)) {
$name = strtolower($column->getName());
$name = strtolower($column->getQuotedName($this->_platform));
$list[$name] = $column;
}
}
......@@ -711,7 +711,7 @@ abstract class AbstractSchemaManager
$list = array();
foreach ($views as $key => $value) {
if ($view = $this->_getPortableViewDefinition($value)) {
$viewName = strtolower($view->getName());
$viewName = strtolower($view->getQuotedName($this->_platform));
$list[$viewName] = $view;
}
}
......
......@@ -95,7 +95,9 @@ class CreateSchemaSqlCollector implements Visitor
// Append the foreign key constraints SQL
if ($this->_platform->supportsForeignKeyConstraints()) {
$this->_createFkConstraintQueries = array_merge($this->_createFkConstraintQueries,
(array) $this->_platform->getCreateForeignKeySQL($fkConstraint, $localTable->getName())
(array) $this->_platform->getCreateForeignKeySQL(
$fkConstraint, $localTable->getQuotedName($this->_platform)
)
);
}
}
......
......@@ -83,7 +83,7 @@ class DropSchemaSqlCollector implements Visitor
*/
public function acceptTable(Table $table)
{
$this->_tables[] = $this->_platform->getDropTableSQL($table->getName());
$this->_tables[] = $this->_platform->getDropTableSQL($table->getQuotedName($this->_platform));
}
/**
......@@ -104,7 +104,7 @@ class DropSchemaSqlCollector implements Visitor
throw SchemaException::namedForeignKeyRequired($localTable, $fkConstraint);
}
$this->_constraints[] = $this->_platform->getDropForeignKeySQL($fkConstraint->getName(), $localTable->getName());
$this->_constraints[] = $this->_platform->getDropForeignKeySQL($fkConstraint->getQuotedName($this->_platform), $localTable->getQuotedName($this->_platform));
}
/**
......@@ -121,7 +121,7 @@ class DropSchemaSqlCollector implements Visitor
*/
public function acceptSequence(Sequence $sequence)
{
$this->_sequences[] = $this->_platform->getDropSequenceSQL($sequence->getName());
$this->_sequences[] = $this->_platform->getDropSequenceSQL($sequence->getQuotedName($this->_platform));
}
/**
......
......@@ -71,4 +71,20 @@ class ColumnTest extends \PHPUnit_Framework_TestCase
$string = Type::getType('string');
return new Column("foo", $string, $options);
}
/**
* @group DBAL-64
*/
public function testQuotedColumnName()
{
$string = Type::getType('string');
$column = new Column("`bar`", $string, array());
$mysqlPlatform = new \Doctrine\DBAL\Platforms\MySqlPlatform();
$sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform();
$this->assertEquals('bar', $column->getName());
$this->assertEquals('`bar`', $column->getQuotedName($mysqlPlatform));
$this->assertEquals('"bar"', $column->getQuotedName($sqlitePlatform));
}
}
\ No newline at end of file
......@@ -400,4 +400,19 @@ class TableTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($table->hasIndex('bar_baz_idx'));
$this->assertTrue($table->hasIndex('bar_baz_uniq'));
}
/**
* @group DBAL-64
*/
public function testQuotedTableName()
{
$table = new Table("`bar`");
$mysqlPlatform = new \Doctrine\DBAL\Platforms\MySqlPlatform();
$sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform();
$this->assertEquals('bar', $table->getName());
$this->assertEquals('`bar`', $table->getQuotedName($mysqlPlatform));
$this->assertEquals('"bar"', $table->getQuotedName($sqlitePlatform));
}
}
\ No newline at end of file
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