Commit 33706eee authored by zYne's avatar zYne

Updated sequence drivers

parent 0a5a3bcd
......@@ -40,29 +40,29 @@ class Doctrine_Sequence_Firebird extends Doctrine_Sequence
*
* @return integer next id in the given sequence
*/
public function nextID($seqName, $ondemand = true)
public function nextID($seqName, $onDemand = true)
{
$sequence_name = $this->getSequenceName($seq_name);
$query = 'SELECT GEN_ID('.$sequence_name.', 1) as the_value FROM RDB$DATABASE';
$this->expectError('*');
$sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true);
$query = 'SELECT GEN_ID(' . $sequenceName . ', 1) as the_value FROM RDB$DATABASE';
try {
$result = $this->queryOne($query, 'integer');
$this->popExpect();
if (PEAR::isError($result)) {
if ($ondemand) {
$this->loadModule('Manager', null, true);
} catch(Doctrine_Connection_Exception $e) {
if ($onDemand && $e->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) {
// Since we are creating the sequence on demand
// we know the first id = 1 so initialize the
// sequence at 2
$result = $this->manager->createSequence($seq_name, 2);
if (PEAR::isError($result)) {
return $this->raiseError($result, null, null,
'on demand sequence could not be created', __FUNCTION__);
} else {
try {
$result = $this->conn->export->createSequence($seqName, 2);
} catch(Doctrine_Exception $e) {
throw new Doctrine_Sequence_Exception('on demand sequence ' . $seqName . ' could not be created');
}
// First ID of a newly created sequence is 1
// return 1;
// BUT generators are not always reset, so return the actual value
return $this->currID($seq_name);
}
return $this->currID($seqName);
}
}
return $result;
......@@ -87,16 +87,17 @@ class Doctrine_Sequence_Firebird extends Doctrine_Sequence
*/
public function currID($seqName)
{
$sequence_name = $this->getSequenceName($seq_name);
$query = 'SELECT GEN_ID('.$sequence_name.', 0) as the_value FROM RDB$DATABASE';
$sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true);
$query = 'SELECT GEN_ID(' . $sequence_name . ', 0) as the_value FROM RDB$DATABASE';
try {
$value = $this->queryOne($query);
if (PEAR::isError($value)) {
return $this->raiseError($result, null, null,
'Unable to select from ' . $seq_name, __FUNCTION__);
} catch(Doctrine_Connection_Exception $e) {
throw new Doctrine_Sequence_Exception('Unable to select from ' . $seqName);
}
if (!is_numeric($value)) {
return $this->raiseError(MDB2_ERROR, null, null,
'could not find value in sequence table', __FUNCTION__);
if ( ! is_numeric($value)) {
throw new Doctrine_Sequence_Exception('could not find value in sequence table');
}
return $value;
}
......
......@@ -42,8 +42,9 @@ class Doctrine_Sequence_Mssql extends Doctrine_Sequence
*/
public function nextID($seqName, $ondemand = true)
{
$sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
$seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true);
$sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true);
$seqcolName = $this->conn->quoteIdentifier($this->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
$this->expectError(MDB2_ERROR_NOSUCHTABLE);
if ($this->_checkSequence($sequence_name)) {
$query = "SET IDENTITY_INSERT $sequence_name ON ".
......@@ -70,13 +71,18 @@ class Doctrine_Sequence_Mssql extends Doctrine_Sequence
}
return $result;
}
$value = $this->lastInsertID($sequence_name);
$value = $this->lastInsertID($sequenceName);
if (is_numeric($value)) {
$query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value";
$result =& $this->_doQuery($query, true);
$query = 'DELETE FROM ' . $sequenceName . ' WHERE ' . $seqcolName . ' < ' . $value;
$this->conn->exec($query);
/**
TODO: is the following needed ?
if (PEAR::isError($result)) {
$this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name;
}
*/
}
return $value;
}
......@@ -91,14 +97,16 @@ class Doctrine_Sequence_Mssql extends Doctrine_Sequence
{
$server_info = $this->getServerVersion();
if (is_array($server_info)
&& !is_null($server_info['major'])
&& ! is_null($server_info['major'])
&& $server_info['major'] >= 8) {
$query = "SELECT SCOPE_IDENTITY()";
} else {
$query = "SELECT @@IDENTITY";
}
return $this->queryOne($query, 'integer');
return $this->fetchOne($query, 'integer');
}
/**
* Returns the current id of a sequence
......
......@@ -40,38 +40,41 @@ class Doctrine_Sequence_Mysql extends Doctrine_Sequence
*
* @return integer next id in the given sequence
*/
public function nextID($seq_name, $ondemand = true)
public function nextID($seqName, $ondemand = true)
{
$sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
$seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true);
$query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (NULL)";
$this->expectError(MDB2_ERROR_NOSUCHTABLE);
$result =& $this->_doQuery($query, true);
$this->popExpect();
if (PEAR::isError($result)) {
if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) {
$this->loadModule('Manager', null, true);
$sequenceName = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
$seqcolName = $this->quoteIdentifier($this->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
$query = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (NULL)';
try {
$this->conn->exec($query);
} catch(Doctrine_Connection_Exception $e) {
if ($onDemand && $e->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) {
// Since we are creating the sequence on demand
// we know the first id = 1 so initialize the
// sequence at 2
$result = $this->manager->createSequence($seq_name, 2);
if (PEAR::isError($result)) {
return $this->raiseError($result, null, null,
'on demand sequence '.$seq_name.' could not be created', __FUNCTION__);
} else {
try {
$result = $this->conn->export->createSequence($seqName, 2);
} catch(Doctrine_Exception $e) {
throw new Doctrine_Sequence_Exception('on demand sequence ' . $seqName . ' could not be created');
}
// First ID of a newly created sequence is 1
return 1;
}
}
return $result;
}
$value = $this->lastInsertID();
if (is_numeric($value)) {
$query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value";
$result =& $this->_doQuery($query, true);
$query = 'DELETE FROM ' . $sequenceName . ' WHERE ' . $seqcolName . ' < ' . $value;
$this->conn->exec($query);
/**
TODO: is the following needed ?
if (PEAR::isError($result)) {
$this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name;
}
*/
}
return $value;
}
......@@ -81,19 +84,11 @@ class Doctrine_Sequence_Mysql extends Doctrine_Sequence
*
* @param string name of the table into which a new row was inserted
* @param string name of the field into which a new row was inserted
* @return integer|boolean
*/
public function lastInsertID($table = null, $field = null)
{
$connection = $this->getConnection();
if (PEAR::isError($connection)) {
return $connection;
}
$value = @mysqli_insert_id($connection);
if (!$value) {
return $this->raiseError(null, null, null,
'Could not get last insert ID', __FUNCTION__);
}
return $value;
return $this->conn->getDbh()->lastInsertId();
}
/**
* Returns the current id of a sequence
......
......@@ -71,8 +71,8 @@ class Doctrine_Sequence_Oracle extends Doctrine_Sequence
public function lastInsertID($table = null, $field = null)
{
$seq = $table.(empty($field) ? '' : '_'.$field);
$sequence_name = $this->quoteIdentifier($this->getSequenceName($seq), true);
return $this->queryOne("SELECT $sequence_name.currval", 'integer');
$sequenceName = $this->quoteIdentifier($this->getSequenceName($seqName), true);
return $this->fetchOne("SELECT $sequence_name.currval", 'integer');
}
/**
* Returns the current id of a sequence
......@@ -83,10 +83,11 @@ class Doctrine_Sequence_Oracle extends Doctrine_Sequence
*/
public function currID($seqName)
{
$sequence_name = $this->getSequenceName($seq_name);
$sequenceName = $this->quoteIdentifier($this->getSequenceName($seqName), true);
$query = 'SELECT (last_number-1) FROM user_sequences';
$query.= ' WHERE sequence_name=' . $this->quote($sequence_name, 'text');
$query.= ' OR sequence_name=' . $this->quote(strtoupper($sequence_name), 'text');
return $this->queryOne($query, 'integer');
$query .= ' WHERE sequence_name=' . $this->conn->quote($sequence_name, 'text');
$query .= ' OR sequence_name=' . $this->conn->quote(strtoupper($sequence_name), 'text');
return $this->fetchOne($query, 'integer');
}
}
......@@ -60,7 +60,7 @@ class Doctrine_Sequence_Sqlite extends Doctrine_Sequence
try {
$result = $this->conn->export->createSequence($seqName, 2);
} catch(Doctrine_Exception $e) {
throw new Doctrine_Sequence_Sqlite_Exception('on demand sequence ' . $seqName . ' could not be created');
throw new Doctrine_Sequence_Exception('on demand sequence ' . $seqName . ' could not be created');
}
// First ID of a newly created sequence is 1
return 1;
......@@ -70,11 +70,14 @@ class Doctrine_Sequence_Sqlite extends Doctrine_Sequence
$value = $this->conn->getDbh()->lastInsertID();
if (is_numeric($value)) {
$query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value";
$result =& $this->_doQuery($query, true);
$query = 'DELETE FROM ' . $sequenceName . ' WHERE ' . $seqcolName . ' < ' . $value;
$this->conn->exec($query);
/**
TODO: is the following needed ?
if (PEAR::isError($result)) {
$this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name;
}
*/
}
return $value;
}
......@@ -84,15 +87,11 @@ class Doctrine_Sequence_Sqlite extends Doctrine_Sequence
*
* @param string name of the table into which a new row was inserted
* @param string name of the field into which a new row was inserted
* @return integer|boolean
*/
public function lastInsertID($table = null, $field = null)
{
$value = $this->conn->getDbh()->lastInsertID();
if (!$value) {
return $this->raiseError(null, null, null,
'Could not get last insert ID', __FUNCTION__);
}
return $value;
return $this->conn->getDbh()->lastInsertID();
}
/**
* Returns the current id of a sequence
......
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