1
0
mirror of synced 2025-01-18 22:41:43 +03:00

Updated sequence drivers

This commit is contained in:
zYne 2007-01-07 18:31:17 +00:00
parent 0a5a3bcdfe
commit 33706eee6d
5 changed files with 88 additions and 84 deletions

View File

@ -40,29 +40,29 @@ class Doctrine_Sequence_Firebird extends Doctrine_Sequence
* *
* @return integer next id in the given 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); $sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true);
$query = 'SELECT GEN_ID('.$sequence_name.', 1) as the_value FROM RDB$DATABASE';
$this->expectError('*'); $query = 'SELECT GEN_ID(' . $sequenceName . ', 1) as the_value FROM RDB$DATABASE';
$result = $this->queryOne($query, 'integer'); try {
$this->popExpect();
if (PEAR::isError($result)) { $result = $this->queryOne($query, 'integer');
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 // Since we are creating the sequence on demand
// we know the first id = 1 so initialize the // we know the first id = 1 so initialize the
// sequence at 2 // sequence at 2
$result = $this->manager->createSequence($seq_name, 2); try {
if (PEAR::isError($result)) { $result = $this->conn->export->createSequence($seqName, 2);
return $this->raiseError($result, null, null, } catch(Doctrine_Exception $e) {
'on demand sequence could not be created', __FUNCTION__); throw new Doctrine_Sequence_Exception('on demand sequence ' . $seqName . ' could not be created');
} else {
// 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);
} }
// 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($seqName);
} }
} }
return $result; return $result;
@ -87,16 +87,17 @@ class Doctrine_Sequence_Firebird extends Doctrine_Sequence
*/ */
public function currID($seqName) public function currID($seqName)
{ {
$sequence_name = $this->getSequenceName($seq_name); $sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true);
$query = 'SELECT GEN_ID('.$sequence_name.', 0) as the_value FROM RDB$DATABASE';
$value = $this->queryOne($query);
if (PEAR::isError($value)) { $query = 'SELECT GEN_ID(' . $sequence_name . ', 0) as the_value FROM RDB$DATABASE';
return $this->raiseError($result, null, null, try {
'Unable to select from ' . $seq_name, __FUNCTION__); $value = $this->queryOne($query);
} catch(Doctrine_Connection_Exception $e) {
throw new Doctrine_Sequence_Exception('Unable to select from ' . $seqName);
} }
if (!is_numeric($value)) { if ( ! is_numeric($value)) {
return $this->raiseError(MDB2_ERROR, null, null, throw new Doctrine_Sequence_Exception('could not find value in sequence table');
'could not find value in sequence table', __FUNCTION__);
} }
return $value; return $value;
} }

View File

@ -42,8 +42,9 @@ class Doctrine_Sequence_Mssql extends Doctrine_Sequence
*/ */
public function nextID($seqName, $ondemand = true) public function nextID($seqName, $ondemand = true)
{ {
$sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true); $sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true);
$seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true); $seqcolName = $this->conn->quoteIdentifier($this->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
$this->expectError(MDB2_ERROR_NOSUCHTABLE); $this->expectError(MDB2_ERROR_NOSUCHTABLE);
if ($this->_checkSequence($sequence_name)) { if ($this->_checkSequence($sequence_name)) {
$query = "SET IDENTITY_INSERT $sequence_name ON ". $query = "SET IDENTITY_INSERT $sequence_name ON ".
@ -70,13 +71,18 @@ class Doctrine_Sequence_Mssql extends Doctrine_Sequence
} }
return $result; return $result;
} }
$value = $this->lastInsertID($sequence_name);
$value = $this->lastInsertID($sequenceName);
if (is_numeric($value)) { if (is_numeric($value)) {
$query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value"; $query = 'DELETE FROM ' . $sequenceName . ' WHERE ' . $seqcolName . ' < ' . $value;
$result =& $this->_doQuery($query, true); $this->conn->exec($query);
/**
TODO: is the following needed ?
if (PEAR::isError($result)) { if (PEAR::isError($result)) {
$this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name; $this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name;
} }
*/
} }
return $value; return $value;
} }
@ -91,14 +97,16 @@ class Doctrine_Sequence_Mssql extends Doctrine_Sequence
{ {
$server_info = $this->getServerVersion(); $server_info = $this->getServerVersion();
if (is_array($server_info) if (is_array($server_info)
&& !is_null($server_info['major']) && ! is_null($server_info['major'])
&& $server_info['major'] >= 8) { && $server_info['major'] >= 8) {
$query = "SELECT SCOPE_IDENTITY()";
$query = "SELECT SCOPE_IDENTITY()";
} else { } else {
$query = "SELECT @@IDENTITY"; $query = "SELECT @@IDENTITY";
} }
return $this->queryOne($query, 'integer'); return $this->fetchOne($query, 'integer');
} }
/** /**
* Returns the current id of a sequence * Returns the current id of a sequence

View File

@ -40,38 +40,41 @@ class Doctrine_Sequence_Mysql extends Doctrine_Sequence
* *
* @return integer next id in the given 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); $sequenceName = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
$seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true); $seqcolName = $this->quoteIdentifier($this->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
$query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (NULL)"; $query = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (NULL)';
$this->expectError(MDB2_ERROR_NOSUCHTABLE);
$result =& $this->_doQuery($query, true); try {
$this->popExpect();
if (PEAR::isError($result)) { $this->conn->exec($query);
if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) {
$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 // Since we are creating the sequence on demand
// we know the first id = 1 so initialize the // we know the first id = 1 so initialize the
// sequence at 2 // sequence at 2
$result = $this->manager->createSequence($seq_name, 2); try {
if (PEAR::isError($result)) { $result = $this->conn->export->createSequence($seqName, 2);
return $this->raiseError($result, null, null, } catch(Doctrine_Exception $e) {
'on demand sequence '.$seq_name.' could not be created', __FUNCTION__); throw new Doctrine_Sequence_Exception('on demand sequence ' . $seqName . ' could not be created');
} else {
// First ID of a newly created sequence is 1
return 1;
} }
// First ID of a newly created sequence is 1
return 1;
} }
return $result; return $result;
} }
$value = $this->lastInsertID(); $value = $this->lastInsertID();
if (is_numeric($value)) { if (is_numeric($value)) {
$query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value"; $query = 'DELETE FROM ' . $sequenceName . ' WHERE ' . $seqcolName . ' < ' . $value;
$result =& $this->_doQuery($query, true); $this->conn->exec($query);
/**
TODO: is the following needed ?
if (PEAR::isError($result)) { if (PEAR::isError($result)) {
$this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name; $this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name;
} }
*/
} }
return $value; return $value;
} }
@ -79,21 +82,13 @@ class Doctrine_Sequence_Mysql extends Doctrine_Sequence
* Returns the autoincrement ID if supported or $id or fetches the current * Returns the autoincrement ID if supported or $id or fetches the current
* ID in a sequence called: $table.(empty($field) ? '' : '_'.$field) * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
* *
* @param string name of the table into which a new row was inserted * @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 * @param string name of the field into which a new row was inserted
* @return integer|boolean
*/ */
public function lastInsertID($table = null, $field = null) public function lastInsertID($table = null, $field = null)
{ {
$connection = $this->getConnection(); return $this->conn->getDbh()->lastInsertId();
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;
} }
/** /**
* Returns the current id of a sequence * Returns the current id of a sequence

View File

@ -71,8 +71,8 @@ class Doctrine_Sequence_Oracle extends Doctrine_Sequence
public function lastInsertID($table = null, $field = null) public function lastInsertID($table = null, $field = null)
{ {
$seq = $table.(empty($field) ? '' : '_'.$field); $seq = $table.(empty($field) ? '' : '_'.$field);
$sequence_name = $this->quoteIdentifier($this->getSequenceName($seq), true); $sequenceName = $this->quoteIdentifier($this->getSequenceName($seqName), true);
return $this->queryOne("SELECT $sequence_name.currval", 'integer'); return $this->fetchOne("SELECT $sequence_name.currval", 'integer');
} }
/** /**
* Returns the current id of a sequence * Returns the current id of a sequence
@ -83,10 +83,11 @@ class Doctrine_Sequence_Oracle extends Doctrine_Sequence
*/ */
public function currID($seqName) 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 = 'SELECT (last_number-1) FROM user_sequences';
$query.= ' WHERE sequence_name=' . $this->quote($sequence_name, 'text'); $query .= ' WHERE sequence_name=' . $this->conn->quote($sequence_name, 'text');
$query.= ' OR sequence_name=' . $this->quote(strtoupper($sequence_name), 'text'); $query .= ' OR sequence_name=' . $this->conn->quote(strtoupper($sequence_name), 'text');
return $this->queryOne($query, 'integer');
return $this->fetchOne($query, 'integer');
} }
} }

View File

@ -60,7 +60,7 @@ class Doctrine_Sequence_Sqlite extends Doctrine_Sequence
try { try {
$result = $this->conn->export->createSequence($seqName, 2); $result = $this->conn->export->createSequence($seqName, 2);
} catch(Doctrine_Exception $e) { } 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 // First ID of a newly created sequence is 1
return 1; return 1;
@ -70,11 +70,14 @@ class Doctrine_Sequence_Sqlite extends Doctrine_Sequence
$value = $this->conn->getDbh()->lastInsertID(); $value = $this->conn->getDbh()->lastInsertID();
if (is_numeric($value)) { if (is_numeric($value)) {
$query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value"; $query = 'DELETE FROM ' . $sequenceName . ' WHERE ' . $seqcolName . ' < ' . $value;
$result =& $this->_doQuery($query, true); $this->conn->exec($query);
/**
TODO: is the following needed ?
if (PEAR::isError($result)) { if (PEAR::isError($result)) {
$this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name; $this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name;
} }
*/
} }
return $value; 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 table into which a new row was inserted
* @param string name of the field 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) public function lastInsertID($table = null, $field = null)
{ {
$value = $this->conn->getDbh()->lastInsertID(); return $this->conn->getDbh()->lastInsertID();
if (!$value) {
return $this->raiseError(null, null, null,
'Could not get last insert ID', __FUNCTION__);
}
return $value;
} }
/** /**
* Returns the current id of a sequence * Returns the current id of a sequence