Commit 83bb8478 authored by Adrien Crivelli's avatar Adrien Crivelli

Refactor partial into options array

This coherent with what is done for Table. All platform specific things
are grouped into an options array. Eventually flags should be migrated
into options as well.
parent 52237874
......@@ -1759,9 +1759,11 @@ abstract class AbstractPlatform
*/
protected function getPartialIndexSQL(Index $index)
{
$where = $index->getWhere();
return $this->supportsPartialIndexes() && $where ? ' WHERE ' . $where : '';
if ($this->supportsPartialIndexes() && $index->hasOption('where')) {
return ' WHERE ' . $index->getOption('where');
} else {
return '';
}
}
/**
......
......@@ -819,7 +819,7 @@ abstract class AbstractSchemaManager
'unique' => $tableIndex['non_unique'] ? false : true,
'primary' => $tableIndex['primary'],
'flags' => isset($tableIndex['flags']) ? $tableIndex['flags'] : array(),
'where' => isset($tableIndex['where']) ? $tableIndex['where'] : null,
'options' => isset($tableIndex['where']) ? array('where' => $tableIndex['where']) : array(),
);
} else {
$result[$keyName]['columns'][] = $tableIndex['column_name'];
......@@ -842,7 +842,7 @@ abstract class AbstractSchemaManager
}
if ( ! $defaultPrevented) {
$index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary'], $data['flags'], $data['where']);
$index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary'], $data['flags'], $data['options']);
}
if ($index) {
......
......@@ -50,11 +50,13 @@ class Index extends AbstractAsset implements Constraint
protected $_flags = array();
/**
* Platform specific condition for partial indexes
* Platform specific options
*
* @var string|null
* $_flags should eventually be refactored into options
*
* @var array
*/
protected $where = null;
protected $options = array();
/**
* @param string $indexName
......@@ -62,16 +64,16 @@ class Index extends AbstractAsset implements Constraint
* @param boolean $isUnique
* @param boolean $isPrimary
* @param string[] $flags
* @param string|null $where
* @param array $options
*/
public function __construct($indexName, array $columns, $isUnique = false, $isPrimary = false, array $flags = array(), $where = null)
public function __construct($indexName, array $columns, $isUnique = false, $isPrimary = false, array $flags = array(), array $options = array())
{
$isUnique = $isUnique || $isPrimary;
$this->_setName($indexName);
$this->_isUnique = $isUnique;
$this->_isPrimary = $isPrimary;
$this->where = $where;
$this->options = $options;
foreach ($columns as $column) {
$this->_addColumn($column);
......@@ -208,9 +210,9 @@ class Index extends AbstractAsset implements Constraint
$sameColumns = $this->spansColumns($other->getColumns());
if ($sameColumns) {
if ($other->getWhere() != $this->getWhere()) {
if (!$this->samePartialIndex($other)) {
return false;
} elseif ( ! $this->isUnique() && !$this->isPrimary()) {
} elseif (!$this->isUnique() && !$this->isPrimary()) {
// this is a special case: If the current key is neither primary or unique, any uniqe or
// primary key will always have the same effect for the index and there cannot be any constraint
// overlaps. This means a primary or unique index can always fulfill the requirements of just an
......@@ -243,7 +245,7 @@ class Index extends AbstractAsset implements Constraint
return false;
}
if ($this->spansColumns($other->getColumns()) && ($this->isPrimary() || $this->isUnique()) && $this->getWhere() == $other->getWhere()) {
if ($this->spansColumns($other->getColumns()) && ($this->isPrimary() || $this->isUnique()) && $this->samePartialIndex($other)) {
return true;
}
......@@ -301,11 +303,47 @@ class Index extends AbstractAsset implements Constraint
}
/**
* Returns the where condition for partial indexes, if any
* @return string|null
* @param string $name
*
* @return boolean
*/
public function hasOption($name)
{
return isset($this->options[$name]);
}
/**
* @param string $name
*
* @return mixed
*/
public function getWhere()
public function getOption($name)
{
return $this->where;
return $this->options[$name];
}
/**
* @return array
*/
public function getOptions()
{
return $this->options;
}
/**
* Return whether the two indexes have the same partial index
* @param \Doctrine\DBAL\Schema\Index $other
* @return boolean
*/
private function samePartialIndex(Index $other)
{
if ($this->hasOption('where') && $other->hasOption('where') && $this->getOption('where') == $other->getOption('where')) {
return true;
} elseif (!$this->hasOption('where') && !$other->hasOption('where')) {
return true;
} else {
return false;
}
}
}
......@@ -146,11 +146,11 @@ class Table extends AbstractAsset
* @param array $columnNames
* @param string|null $indexName
* @param array $flags
* @param string|null $where
* @param array $options
*
* @return self
*/
public function addIndex(array $columnNames, $indexName = null, array $flags = array(), $where = null)
public function addIndex(array $columnNames, $indexName = null, array $flags = array(), array $options = array())
{
if ($indexName == null) {
$indexName = $this->_generateIdentifierName(
......@@ -158,7 +158,7 @@ class Table extends AbstractAsset
);
}
return $this->_addIndex($this->_createIndex($columnNames, $indexName, false, false, $flags, $where));
return $this->_addIndex($this->_createIndex($columnNames, $indexName, false, false, $flags, $options));
}
/**
......@@ -193,11 +193,11 @@ class Table extends AbstractAsset
/**
* @param array $columnNames
* @param string|null $indexName
* @param string|null $where
* @param array $options
*
* @return self
*/
public function addUniqueIndex(array $columnNames, $indexName = null, $where = null)
public function addUniqueIndex(array $columnNames, $indexName = null, $options = array())
{
if ($indexName === null) {
$indexName = $this->_generateIdentifierName(
......@@ -205,7 +205,7 @@ class Table extends AbstractAsset
);
}
return $this->_addIndex($this->_createIndex($columnNames, $indexName, true, false, array(), $where));
return $this->_addIndex($this->_createIndex($columnNames, $indexName, true, false, array(), $options));
}
/**
......@@ -279,13 +279,13 @@ class Table extends AbstractAsset
* @param boolean $isUnique
* @param boolean $isPrimary
* @param array $flags
* @param string|null $where
* @param array $options
*
* @return Index
*
* @throws SchemaException
*/
private function _createIndex(array $columnNames, $indexName, $isUnique, $isPrimary, array $flags = array(), $where = null)
private function _createIndex(array $columnNames, $indexName, $isUnique, $isPrimary, array $flags = array(), array $options = array())
{
if (preg_match('(([^a-zA-Z0-9_]+))', $indexName)) {
throw SchemaException::indexNameInvalid($indexName);
......@@ -301,7 +301,7 @@ class Table extends AbstractAsset
}
}
return new Index($indexName, $columnNames, $isUnique, $isPrimary, $flags, $where);
return new Index($indexName, $columnNames, $isUnique, $isPrimary, $flags, $options);
}
/**
......
......@@ -139,7 +139,7 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
public function testGeneratesPartialIndexesSqlOnlyWhenSupportingPartialIndexes()
{
$where = 'test IS NULL AND test2 IS NOT NULL';
$indexDef = new \Doctrine\DBAL\Schema\Index('name', array('test', 'test2'), false, false, array(), $where);
$indexDef = new \Doctrine\DBAL\Schema\Index('name', array('test', 'test2'), false, false, array(), array('where' => $where));
$expected = ' WHERE ' . $where;
......
......@@ -81,9 +81,9 @@ class IndexTest extends \PHPUnit_Framework_TestCase
public function testFullfilledWithPartial()
{
$without = new Index('without', array('col1', 'col2'), true, false, array(), null);
$partial = new Index('partial', array('col1', 'col2'), true, false, array(), 'col1 IS NULL');
$another = new Index('another', array('col1', 'col2'), true, false, array(), 'col1 IS NULL');
$without = new Index('without', array('col1', 'col2'), true, false, array(), array());
$partial = new Index('partial', array('col1', 'col2'), true, false, array(), array('where' => 'col1 IS NULL'));
$another = new Index('another', array('col1', 'col2'), true, false, array(), array('where' => 'col1 IS NULL'));
$this->assertFalse($partial->isFullfilledBy($without));
$this->assertFalse($without->isFullfilledBy($partial));
......@@ -96,9 +96,9 @@ class IndexTest extends \PHPUnit_Framework_TestCase
public function testOverrulesWithPartial()
{
$without = new Index('without', array('col1', 'col2'), true, false, array(), null);
$partial = new Index('partial', array('col1', 'col2'), true, false, array(), 'col1 IS NULL');
$another = new Index('another', array('col1', 'col2'), true, false, array(), 'col1 IS NULL');
$without = new Index('without', array('col1', 'col2'), true, false, array(), array());
$partial = new Index('partial', array('col1', 'col2'), true, false, array(), array('where' => 'col1 IS NULL'));
$another = new Index('another', array('col1', 'col2'), true, false, array(), array('where' => 'col1 IS NULL'));
$this->assertFalse($partial->overrules($without));
$this->assertFalse($without->overrules($partial));
......
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