diff --git a/lib/Doctrine/Sequence/Firebird.php b/lib/Doctrine/Sequence/Firebird.php index 7d5ca6e42..bfa904f27 100644 --- a/lib/Doctrine/Sequence/Firebird.php +++ b/lib/Doctrine/Sequence/Firebird.php @@ -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('*'); - $result = $this->queryOne($query, 'integer'); - $this->popExpect(); - if (PEAR::isError($result)) { - if ($ondemand) { - $this->loadModule('Manager', null, true); + $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'); + + } 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 { - // 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); + 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($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'; - $value = $this->queryOne($query); - if (PEAR::isError($value)) { - return $this->raiseError($result, null, null, - 'Unable to select from ' . $seq_name, __FUNCTION__); + $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); + } 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; } diff --git a/lib/Doctrine/Sequence/Mssql.php b/lib/Doctrine/Sequence/Mssql.php index 400f34048..44351c8fc 100644 --- a/lib/Doctrine/Sequence/Mssql.php +++ b/lib/Doctrine/Sequence/Mssql.php @@ -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']) - && $server_info['major'] >= 8) { - $query = "SELECT SCOPE_IDENTITY()"; + && ! is_null($server_info['major']) + && $server_info['major'] >= 8) { + + $query = "SELECT SCOPE_IDENTITY()"; + } else { - $query = "SELECT @@IDENTITY"; + $query = "SELECT @@IDENTITY"; } - return $this->queryOne($query, 'integer'); + return $this->fetchOne($query, 'integer'); } /** * Returns the current id of a sequence diff --git a/lib/Doctrine/Sequence/Mysql.php b/lib/Doctrine/Sequence/Mysql.php index a2b3f1abe..e43bcdbd2 100644 --- a/lib/Doctrine/Sequence/Mysql.php +++ b/lib/Doctrine/Sequence/Mysql.php @@ -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 { - // First ID of a newly created sequence is 1 - return 1; + 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; } @@ -79,21 +82,13 @@ class Doctrine_Sequence_Mysql extends Doctrine_Sequence * Returns the autoincrement ID if supported or $id or fetches the current * 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 field 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 + * @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 diff --git a/lib/Doctrine/Sequence/Oracle.php b/lib/Doctrine/Sequence/Oracle.php index bc29245ff..61aedd339 100644 --- a/lib/Doctrine/Sequence/Oracle.php +++ b/lib/Doctrine/Sequence/Oracle.php @@ -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); - $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'); + $sequenceName = $this->quoteIdentifier($this->getSequenceName($seqName), true); + $query = 'SELECT (last_number-1) FROM user_sequences'; + $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'); } } diff --git a/lib/Doctrine/Sequence/Sqlite.php b/lib/Doctrine/Sequence/Sqlite.php index 5a1592d1f..c7e78cb95 100644 --- a/lib/Doctrine/Sequence/Sqlite.php +++ b/lib/Doctrine/Sequence/Sqlite.php @@ -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