Commit e8efcd76 authored by Benjamin Eberlei's avatar Benjamin Eberlei

[DBAL-139] Add support for creating sequences with CACHE/NOCACHE options.

parent 6ce265d1
...@@ -188,7 +188,8 @@ class OraclePlatform extends AbstractPlatform ...@@ -188,7 +188,8 @@ class OraclePlatform extends AbstractPlatform
return 'CREATE SEQUENCE ' . $sequence->getQuotedName($this) . return 'CREATE SEQUENCE ' . $sequence->getQuotedName($this) .
' START WITH ' . $sequence->getInitialValue() . ' START WITH ' . $sequence->getInitialValue() .
' MINVALUE ' . $sequence->getInitialValue() . ' MINVALUE ' . $sequence->getInitialValue() .
' INCREMENT BY ' . $sequence->getAllocationSize(); ' INCREMENT BY ' . $sequence->getAllocationSize() .
$this->getSequenceCacheSQL($sequence);
} }
/** /**
...@@ -197,7 +198,26 @@ class OraclePlatform extends AbstractPlatform ...@@ -197,7 +198,26 @@ class OraclePlatform extends AbstractPlatform
public function getAlterSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence) public function getAlterSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
{ {
return 'ALTER SEQUENCE ' . $sequence->getQuotedName($this) . return 'ALTER SEQUENCE ' . $sequence->getQuotedName($this) .
' INCREMENT BY ' . $sequence->getAllocationSize(); ' INCREMENT BY ' . $sequence->getAllocationSize()
. $this->getSequenceCacheSQL($sequence);
}
/**
* Cache definition for sequences
*
* @return string
*/
private function getSequenceCacheSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
{
if ($sequence->getCache() === 0) {
return ' NOCACHE';
} else if ($sequence->getCache() === 1) {
return ' CACHE 20';
} else if ($sequence->getCache() > 1) {
return ' CACHE ' . $sequence->getCache();
}
return '';
} }
/** /**
......
...@@ -548,7 +548,8 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -548,7 +548,8 @@ class PostgreSqlPlatform extends AbstractPlatform
return 'CREATE SEQUENCE ' . $sequence->getQuotedName($this) . return 'CREATE SEQUENCE ' . $sequence->getQuotedName($this) .
' INCREMENT BY ' . $sequence->getAllocationSize() . ' INCREMENT BY ' . $sequence->getAllocationSize() .
' MINVALUE ' . $sequence->getInitialValue() . ' MINVALUE ' . $sequence->getInitialValue() .
' START ' . $sequence->getInitialValue(); ' START ' . $sequence->getInitialValue() .
$this->getSequenceCacheSQL($sequence);
} }
/** /**
...@@ -557,7 +558,22 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -557,7 +558,22 @@ class PostgreSqlPlatform extends AbstractPlatform
public function getAlterSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence) public function getAlterSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
{ {
return 'ALTER SEQUENCE ' . $sequence->getQuotedName($this) . return 'ALTER SEQUENCE ' . $sequence->getQuotedName($this) .
' INCREMENT BY ' . $sequence->getAllocationSize(); ' INCREMENT BY ' . $sequence->getAllocationSize() .
$this->getSequenceCacheSQL($sequence);
}
/**
* Cache definition for sequences
*
* @return string
*/
private function getSequenceCacheSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
{
if ($sequence->getCache() > 1) {
return ' CACHE ' . $sequence->getCache();
}
return '';
} }
/** /**
......
...@@ -33,23 +33,29 @@ class Sequence extends AbstractAsset ...@@ -33,23 +33,29 @@ class Sequence extends AbstractAsset
/** /**
* @var integer * @var integer
*/ */
protected $_allocationSize = 1; protected $allocationSize = 1;
/** /**
* @var integer * @var integer
*/ */
protected $_initialValue = 1; protected $initialValue = 1;
/**
* @var integer|null
*/
protected $cache = null;
/** /**
* @param string $name * @param string $name
* @param integer $allocationSize * @param integer $allocationSize
* @param integer $initialValue * @param integer $initialValue
*/ */
public function __construct($name, $allocationSize=1, $initialValue=1) public function __construct($name, $allocationSize = 1, $initialValue = 1, $cache = null)
{ {
$this->_setName($name); $this->_setName($name);
$this->_allocationSize = (is_numeric($allocationSize))?$allocationSize:1; $this->allocationSize = is_numeric($allocationSize) ? $allocationSize : 1;
$this->_initialValue = (is_numeric($initialValue))?$initialValue:1; $this->initialValue = is_numeric($initialValue) ? $initialValue : 1;
$this->cache = $cache;
} }
/** /**
...@@ -57,7 +63,7 @@ class Sequence extends AbstractAsset ...@@ -57,7 +63,7 @@ class Sequence extends AbstractAsset
*/ */
public function getAllocationSize() public function getAllocationSize()
{ {
return $this->_allocationSize; return $this->allocationSize;
} }
/** /**
...@@ -65,27 +71,51 @@ class Sequence extends AbstractAsset ...@@ -65,27 +71,51 @@ class Sequence extends AbstractAsset
*/ */
public function getInitialValue() public function getInitialValue()
{ {
return $this->_initialValue; return $this->initialValue;
}
/**
* @return integer|null
*/
public function getCache()
{
return $this->cache;
} }
/** /**
* @param integer $allocationSize * @param integer $allocationSize
* *
* @return void * @return \Doctrine\DBAL\Schema\Sequence
*/ */
public function setAllocationSize($allocationSize) public function setAllocationSize($allocationSize)
{ {
$this->_allocationSize = (is_numeric($allocationSize))?$allocationSize:1; $this->allocationSize = is_numeric($allocationSize) ? $allocationSize : 1;
return $this;
} }
/** /**
* @param integer $initialValue * @param integer $initialValue
* *
* @return void * @return \Doctrine\DBAL\Schema\Sequence
*/ */
public function setInitialValue($initialValue) public function setInitialValue($initialValue)
{ {
$this->_initialValue = (is_numeric($initialValue))?$initialValue:1; $this->initialValue = is_numeric($initialValue) ? $initialValue : 1;
return $this;
}
/**
* @param integer $cache
*
* @return \Doctrine\DBAL\Schema\Sequence
*/
public function setCache($cache)
{
$this->cache = $cache;
return $this;
} }
/** /**
......
...@@ -346,4 +346,23 @@ class OraclePlatformTest extends AbstractPlatformTestCase ...@@ -346,4 +346,23 @@ class OraclePlatformTest extends AbstractPlatformTestCase
{ {
$this->assertSame('mytable_mycolumn_SEQ', $this->_platform->getIdentitySequenceName('mytable', 'mycolumn')); $this->assertSame('mytable_mycolumn_SEQ', $this->_platform->getIdentitySequenceName('mytable', 'mycolumn'));
} }
/**
* @dataProvider dataCreateSequenceWithCache
* @group DBAL-139
*/
public function testCreateSequenceWithCache($cacheSize, $expectedSql)
{
$sequence = new \Doctrine\DBAL\Schema\Sequence('foo', 1, 1, $cacheSize);
$this->assertContains($expectedSql, $this->_platform->getCreateSequenceSQL($sequence));
}
public function dataCreateSequenceWithCache()
{
return array(
array(1, 'CACHE 20'),
array(0, 'NOCACHE'),
array(3, 'CACHE 3')
);
}
} }
...@@ -426,4 +426,21 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase ...@@ -426,4 +426,21 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase
{ {
$this->assertSame('mytable_mycolumn_seq', $this->_platform->getIdentitySequenceName('mytable', 'mycolumn')); $this->assertSame('mytable_mycolumn_seq', $this->_platform->getIdentitySequenceName('mytable', 'mycolumn'));
} }
/**
* @dataProvider dataCreateSequenceWithCache
* @group DBAL-139
*/
public function testCreateSequenceWithCache($cacheSize, $expectedSql)
{
$sequence = new \Doctrine\DBAL\Schema\Sequence('foo', 1, 1, $cacheSize);
$this->assertContains($expectedSql, $this->_platform->getCreateSequenceSQL($sequence));
}
public function dataCreateSequenceWithCache()
{
return array(
array(3, 'CACHE 3')
);
}
} }
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