Commit 802d1366 authored by Benjamin Eberlei's avatar Benjamin Eberlei

[DBAL-285] Fix index comparing

parent 80201a10
......@@ -87,6 +87,14 @@ class Index extends AbstractAsset implements Constraint
return $this->_columns;
}
/**
* @return array
*/
public function getUnquotedColumns()
{
return array_map(array($this, 'trimQuotes'), $this->getColumns());
}
/**
* Is the index neither unique nor primary key?
*
......@@ -118,11 +126,11 @@ class Index extends AbstractAsset implements Constraint
* @param int $pos
* @return bool
*/
public function hasColumnAtPosition($columnName, $pos=0)
public function hasColumnAtPosition($columnName, $pos = 0)
{
$columnName = strtolower($columnName);
$indexColumns = \array_map('strtolower', $this->getColumns());
return \array_search($columnName, $indexColumns) === $pos;
$columnName = $this->trimQuotes(strtolower($columnName));
$indexColumns = array_map('strtolower', $this->getUnquotedColumns());
return array_search($columnName, $indexColumns) === $pos;
}
/**
......@@ -135,7 +143,7 @@ class Index extends AbstractAsset implements Constraint
{
$sameColumns = true;
for ($i = 0; $i < count($this->_columns); $i++) {
if (!isset($columnNames[$i]) || strtolower($this->_columns[$i]) != strtolower($columnNames[$i])) {
if (!isset($columnNames[$i]) || $this->trimQuotes(strtolower($this->_columns[$i])) != $this->trimQuotes(strtolower($columnNames[$i]))) {
$sameColumns = false;
}
}
......
......@@ -97,4 +97,19 @@ class IndexTest extends \PHPUnit_Framework_TestCase
$idx1->removeFlag('clustered');
$this->assertFalse($idx1->hasFlag('clustered'));
}
/**
* @group DBAL-285
*/
public function testIndexQuotes()
{
$index = new Index("foo", array("`bar`", "`baz`"));
$this->assertTrue($index->spansColumns(array("bar", "baz")));
$this->assertTrue($index->hasColumnAtPosition("bar", 0));
$this->assertTrue($index->hasColumnAtPosition("baz", 1));
$this->assertFalse($index->hasColumnAtPosition("bar", 1));
$this->assertFalse($index->hasColumnAtPosition("baz", 0));
}
}
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