Commit 803a83c5 authored by Steve Müller's avatar Steve Müller

normalize asset names consistently

parent 8cc41b3f
...@@ -183,7 +183,7 @@ class Table extends AbstractAsset ...@@ -183,7 +183,7 @@ class Table extends AbstractAsset
*/ */
public function dropIndex($indexName) public function dropIndex($indexName)
{ {
$indexName = strtolower($indexName); $indexName = $this->normalizeIdentifier($indexName);
if ( ! $this->hasIndex($indexName)) { if ( ! $this->hasIndex($indexName)) {
throw SchemaException::indexDoesNotExist($indexName, $this->_name); throw SchemaException::indexDoesNotExist($indexName, $this->_name);
} }
...@@ -222,8 +222,8 @@ class Table extends AbstractAsset ...@@ -222,8 +222,8 @@ class Table extends AbstractAsset
*/ */
public function renameIndex($oldIndexName, $newIndexName = null) public function renameIndex($oldIndexName, $newIndexName = null)
{ {
$oldIndexName = strtolower($oldIndexName); $oldIndexName = $this->normalizeIdentifier($oldIndexName);
$normalizedNewIndexName = strtolower($newIndexName); $normalizedNewIndexName = $this->normalizeIdentifier($newIndexName);
if ($oldIndexName === $normalizedNewIndexName) { if ($oldIndexName === $normalizedNewIndexName) {
return $this; return $this;
...@@ -287,7 +287,7 @@ class Table extends AbstractAsset ...@@ -287,7 +287,7 @@ class Table extends AbstractAsset
*/ */
private function _createIndex(array $columnNames, $indexName, $isUnique, $isPrimary, array $flags = array(), array $options = array()) private function _createIndex(array $columnNames, $indexName, $isUnique, $isPrimary, array $flags = array(), array $options = array())
{ {
if (preg_match('(([^a-zA-Z0-9_]+))', $indexName)) { if (preg_match('(([^a-zA-Z0-9_]+))', $this->normalizeIdentifier($indexName))) {
throw SchemaException::indexNameInvalid($indexName); throw SchemaException::indexNameInvalid($indexName);
} }
...@@ -362,7 +362,7 @@ class Table extends AbstractAsset ...@@ -362,7 +362,7 @@ class Table extends AbstractAsset
*/ */
public function dropColumn($columnName) public function dropColumn($columnName)
{ {
$columnName = strtolower($columnName); $columnName = $this->normalizeIdentifier($columnName);
unset($this->_columns[$columnName]); unset($this->_columns[$columnName]);
return $this; return $this;
...@@ -469,7 +469,7 @@ class Table extends AbstractAsset ...@@ -469,7 +469,7 @@ class Table extends AbstractAsset
protected function _addColumn(Column $column) protected function _addColumn(Column $column)
{ {
$columnName = $column->getName(); $columnName = $column->getName();
$columnName = strtolower($columnName); $columnName = $this->normalizeIdentifier($columnName);
if (isset($this->_columns[$columnName])) { if (isset($this->_columns[$columnName])) {
throw SchemaException::columnAlreadyExists($this->getName(), $columnName); throw SchemaException::columnAlreadyExists($this->getName(), $columnName);
...@@ -497,7 +497,7 @@ class Table extends AbstractAsset ...@@ -497,7 +497,7 @@ class Table extends AbstractAsset
} }
$indexName = $indexCandidate->getName(); $indexName = $indexCandidate->getName();
$indexName = strtolower($indexName); $indexName = $this->normalizeIdentifier($indexName);
if (isset($this->_indexes[$indexName]) || ($this->_primaryKeyName != false && $indexCandidate->isPrimary())) { if (isset($this->_indexes[$indexName]) || ($this->_primaryKeyName != false && $indexCandidate->isPrimary())) {
throw SchemaException::indexAlreadyExists($indexName, $this->_name); throw SchemaException::indexAlreadyExists($indexName, $this->_name);
...@@ -535,7 +535,7 @@ class Table extends AbstractAsset ...@@ -535,7 +535,7 @@ class Table extends AbstractAsset
array_merge((array)$this->getName(), $constraint->getLocalColumns()), "fk", $this->_getMaxIdentifierLength() array_merge((array)$this->getName(), $constraint->getLocalColumns()), "fk", $this->_getMaxIdentifierLength()
); );
} }
$name = strtolower($name); $name = $this->normalizeIdentifier($name);
$this->_fkConstraints[$name] = $constraint; $this->_fkConstraints[$name] = $constraint;
// add an explicit index on the foreign key columns. If there is already an index that fulfils this requirements drop the request. // add an explicit index on the foreign key columns. If there is already an index that fulfils this requirements drop the request.
...@@ -553,7 +553,7 @@ class Table extends AbstractAsset ...@@ -553,7 +553,7 @@ class Table extends AbstractAsset
*/ */
public function hasForeignKey($constraintName) public function hasForeignKey($constraintName)
{ {
$constraintName = strtolower($constraintName); $constraintName = $this->normalizeIdentifier($constraintName);
return isset($this->_fkConstraints[$constraintName]); return isset($this->_fkConstraints[$constraintName]);
} }
...@@ -569,7 +569,7 @@ class Table extends AbstractAsset ...@@ -569,7 +569,7 @@ class Table extends AbstractAsset
*/ */
public function getForeignKey($constraintName) public function getForeignKey($constraintName)
{ {
$constraintName = strtolower($constraintName); $constraintName = $this->normalizeIdentifier($constraintName);
if (!$this->hasForeignKey($constraintName)) { if (!$this->hasForeignKey($constraintName)) {
throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name); throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name);
} }
...@@ -588,7 +588,7 @@ class Table extends AbstractAsset ...@@ -588,7 +588,7 @@ class Table extends AbstractAsset
*/ */
public function removeForeignKey($constraintName) public function removeForeignKey($constraintName)
{ {
$constraintName = strtolower($constraintName); $constraintName = $this->normalizeIdentifier($constraintName);
if (!$this->hasForeignKey($constraintName)) { if (!$this->hasForeignKey($constraintName)) {
throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name); throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name);
} }
...@@ -631,7 +631,7 @@ class Table extends AbstractAsset ...@@ -631,7 +631,7 @@ class Table extends AbstractAsset
*/ */
public function hasColumn($columnName) public function hasColumn($columnName)
{ {
$columnName = $this->trimQuotes(strtolower($columnName)); $columnName = $this->normalizeIdentifier($columnName);
return isset($this->_columns[$columnName]); return isset($this->_columns[$columnName]);
} }
...@@ -647,7 +647,7 @@ class Table extends AbstractAsset ...@@ -647,7 +647,7 @@ class Table extends AbstractAsset
*/ */
public function getColumn($columnName) public function getColumn($columnName)
{ {
$columnName = strtolower($this->trimQuotes($columnName)); $columnName = $this->normalizeIdentifier($columnName);
if ( ! $this->hasColumn($columnName)) { if ( ! $this->hasColumn($columnName)) {
throw SchemaException::columnDoesNotExist($columnName, $this->_name); throw SchemaException::columnDoesNotExist($columnName, $this->_name);
} }
...@@ -704,7 +704,7 @@ class Table extends AbstractAsset ...@@ -704,7 +704,7 @@ class Table extends AbstractAsset
*/ */
public function hasIndex($indexName) public function hasIndex($indexName)
{ {
$indexName = strtolower($indexName); $indexName = $this->normalizeIdentifier($indexName);
return (isset($this->_indexes[$indexName])); return (isset($this->_indexes[$indexName]));
} }
...@@ -720,7 +720,7 @@ class Table extends AbstractAsset ...@@ -720,7 +720,7 @@ class Table extends AbstractAsset
*/ */
public function getIndex($indexName) public function getIndex($indexName)
{ {
$indexName = strtolower($indexName); $indexName = $this->normalizeIdentifier($indexName);
if ( ! $this->hasIndex($indexName)) { if ( ! $this->hasIndex($indexName)) {
throw SchemaException::indexDoesNotExist($indexName, $this->_name); throw SchemaException::indexDoesNotExist($indexName, $this->_name);
} }
...@@ -814,4 +814,18 @@ class Table extends AbstractAsset ...@@ -814,4 +814,18 @@ class Table extends AbstractAsset
$this->_fkConstraints[$k]->setLocalTable($this); $this->_fkConstraints[$k]->setLocalTable($this);
} }
} }
/**
* Normalizes a given identifier.
*
* Trims quotes and lowercases the given identifier.
*
* @param string $identifier The identifier to normalize.
*
* @return string The normalized identifier.
*/
private function normalizeIdentifier($identifier)
{
return $this->trimQuotes(strtolower($identifier));
}
} }
...@@ -623,4 +623,76 @@ class TableTest extends \Doctrine\Tests\DbalTestCase ...@@ -623,4 +623,76 @@ class TableTest extends \Doctrine\Tests\DbalTestCase
$table->renameIndex('idx_id', 'idx_foo'); $table->renameIndex('idx_id', 'idx_foo');
} }
/**
* @dataProvider getNormalizesAssetNames
* @group DBAL-831
*/
public function testNormalizesColumnNames($assetName)
{
$table = new Table('test');
$table->addColumn($assetName, 'integer');
$table->addIndex(array($assetName), $assetName);
$table->addForeignKeyConstraint('test', array($assetName), array($assetName), array(), $assetName);
$this->assertTrue($table->hasColumn($assetName));
$this->assertTrue($table->hasColumn('foo'));
$this->assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn($assetName));
$this->assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn('foo'));
$this->assertTrue($table->hasIndex($assetName));
$this->assertTrue($table->hasIndex('foo'));
$this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex($assetName));
$this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex('foo'));
$this->assertTrue($table->hasForeignKey($assetName));
$this->assertTrue($table->hasForeignKey('foo'));
$this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $table->getForeignKey($assetName));
$this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $table->getForeignKey('foo'));
$table->renameIndex($assetName, $assetName);
$this->assertTrue($table->hasIndex($assetName));
$this->assertTrue($table->hasIndex('foo'));
$table->renameIndex($assetName, 'foo');
$this->assertTrue($table->hasIndex($assetName));
$this->assertTrue($table->hasIndex('foo'));
$table->renameIndex('foo', $assetName);
$this->assertTrue($table->hasIndex($assetName));
$this->assertTrue($table->hasIndex('foo'));
$table->renameIndex($assetName, 'bar');
$this->assertFalse($table->hasIndex($assetName));
$this->assertFalse($table->hasIndex('foo'));
$this->assertTrue($table->hasIndex('bar'));
$table->renameIndex('bar', $assetName);
$table->dropColumn($assetName);
$table->dropIndex($assetName);
$table->removeForeignKey($assetName);
$this->assertFalse($table->hasColumn($assetName));
$this->assertFalse($table->hasColumn('foo'));
$this->assertFalse($table->hasIndex($assetName));
$this->assertFalse($table->hasIndex('foo'));
$this->assertFalse($table->hasForeignKey($assetName));
$this->assertFalse($table->hasForeignKey('foo'));
}
public function getNormalizesAssetNames()
{
return array(
array('foo'),
array('FOO'),
array('`foo`'),
array('`FOO`'),
array('"foo"'),
array('"FOO"'),
array('"foo"'),
array('"FOO"'),
);
}
} }
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