From 24f6b06256069469fb63bc3acd8aebc24f2c29b2 Mon Sep 17 00:00:00 2001 From: zYne Date: Sat, 6 Jan 2007 10:47:57 +0000 Subject: [PATCH] sequence porting continues --- lib/Doctrine/Sequence/Oracle.php | 32 +++++++++-------- lib/Doctrine/Sequence/Pgsql.php | 43 +++++++++++------------ lib/Doctrine/Sequence/Sqlite.php | 59 ++++++++++++++++---------------- 3 files changed, 69 insertions(+), 65 deletions(-) diff --git a/lib/Doctrine/Sequence/Oracle.php b/lib/Doctrine/Sequence/Oracle.php index f93416f14..bc29245ff 100644 --- a/lib/Doctrine/Sequence/Oracle.php +++ b/lib/Doctrine/Sequence/Oracle.php @@ -36,25 +36,27 @@ class Doctrine_Sequence_Oracle extends Doctrine_Sequence * Returns the next free id of a sequence * * @param string $seqName name of the sequence - * @param bool when true missing sequences are automatic created + * @param bool onDemand when true missing sequences are automatic created * * @return integer next id in the given 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); + $query = "SELECT $sequence_name.nextval FROM DUAL"; - $this->expectError(MDB2_ERROR_NOSUCHTABLE); - $result = $this->queryOne($query, 'integer'); - $this->popExpect(); - if (PEAR::isError($result)) { - if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) { - $this->loadModule('Manager', null, true); - $result = $this->manager->createSequence($seq_name); - if (PEAR::isError($result)) { - return $result; + + try { + $result = $this->queryOne($query, 'integer'); + } catch(Doctrine_Connection_Exception $e) { + if ($onDemand && $e->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) { + + try { + $result = $this->conn->export->createSequence($seqName); + } catch(Doctrine_Exception $e) { + throw new Doctrine_Sequence_Oracle_Exception('on demand sequence ' . $seqName . ' could not be created'); } - return $this->nextId($seq_name, false); + return $this->nextId($seqName, false); } } return $result; @@ -83,8 +85,8 @@ class Doctrine_Sequence_Oracle extends Doctrine_Sequence { $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'); + $query.= ' WHERE sequence_name=' . $this->quote($sequence_name, 'text'); + $query.= ' OR sequence_name=' . $this->quote(strtoupper($sequence_name), 'text'); return $this->queryOne($query, 'integer'); } } diff --git a/lib/Doctrine/Sequence/Pgsql.php b/lib/Doctrine/Sequence/Pgsql.php index b249c7d37..67bb7096b 100644 --- a/lib/Doctrine/Sequence/Pgsql.php +++ b/lib/Doctrine/Sequence/Pgsql.php @@ -36,26 +36,26 @@ class Doctrine_Sequence_Pgsql extends Doctrine_Sequence * Returns the next free id of a sequence * * @param string $seqName name of the sequence - * @param bool when true missing sequences are automatic created + * @param bool onDemand when true missing sequences are automatic created * * @return integer next id in the given sequence */ - public function nextID($seqName, $ondemand = true) + public function nextId($seqName, $onDemand = true) { - $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true); - $query = "SELECT NEXTVAL('$sequence_name')"; - $this->expectError(MDB2_ERROR_NOSUCHTABLE); - $result = $this->queryOne($query, 'integer'); - $this->popExpect(); - if (PEAR::isError($result)) { - if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) { - $this->loadModule('Manager', null, true); - $result = $this->manager->createSequence($seq_name); - if (PEAR::isError($result)) { - return $this->raiseError($result, null, null, - 'on demand sequence could not be created', __FUNCTION__); + $sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true); + + $query = "SELECT NEXTVAL('" . $sequenceName . "')"; + try { + $result = $this->fetchOne($query, 'integer'); + } catch(Doctrine_Connection_Exception $e) { + if ($onDemand && $e->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) { + + try { + $result = $this->conn->export->createSequence($seqName); + } catch(Doctrine_Exception $e) { + throw new Doctrine_Sequence_Sqlite_Exception('on demand sequence ' . $seqName . ' could not be created'); } - return $this->nextId($seq_name, false); + return $this->nextId($seqName, false); } } return $result; @@ -67,11 +67,12 @@ class Doctrine_Sequence_Pgsql 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 */ - public function lastInsertID($table = null, $field = null) + public function lastInsertId($table = null, $field = null) { $seq = $table.(empty($field) ? '' : '_'.$field); - $sequence_name = $this->getSequenceName($seq); - return $this->queryOne("SELECT currval('$sequence_name')", 'integer'); + $sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true); + + return $this->fetchOne("SELECT currval('" . $sequenceName . "')", 'integer'); } /** * Returns the current id of a sequence @@ -80,9 +81,9 @@ class Doctrine_Sequence_Pgsql extends Doctrine_Sequence * * @return integer current id in the given sequence */ - public function currID($seqName) + public function currId($seqName) { - $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true); - return $this->queryOne("SELECT last_value FROM $sequence_name", 'integer'); + $sequenceName = $this->quoteIdentifier($this->getSequenceName($seqName), true); + return $this->fetchOne('SELECT last_value FROM ' . $sequenceName, 'integer'); } } diff --git a/lib/Doctrine/Sequence/Sqlite.php b/lib/Doctrine/Sequence/Sqlite.php index 6bad3d387..5a1592d1f 100644 --- a/lib/Doctrine/Sequence/Sqlite.php +++ b/lib/Doctrine/Sequence/Sqlite.php @@ -36,36 +36,39 @@ class Doctrine_Sequence_Sqlite extends Doctrine_Sequence * Returns the next free id of a sequence * * @param string $seqName name of the sequence - * @param bool when true missing sequences are automatic created + * @param bool $onDemand when true missing sequences are automatic created * * @return integer next id in the given sequence */ - public function nextID($seqName, $ondemand = true) + public function nextID($seqName, $onDemand = true) { - $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true); - $seqcol_name = $this->options['seqcol_name']; - $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->conn->quoteIdentifier($this->getSequenceName($seqName), true); + $seqcolName = $this->conn->quoteIdentifier($this->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true); + + $query = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (NULL)'; + + try { + + $this->conn->exec($query, true); + + } catch(Doctrine_Connection_Exception $e) { + if ($onDemand && $result->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_Sqlite_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(); + + $value = $this->conn->getDbh()->lastInsertID(); + if (is_numeric($value)) { $query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value"; $result =& $this->_doQuery($query, true); @@ -84,11 +87,7 @@ class Doctrine_Sequence_Sqlite extends Doctrine_Sequence */ public function lastInsertID($table = null, $field = null) { - $connection = $this->getConnection(); - if (PEAR::isError($connection)) { - return $connection; - } - $value = @sqlite_last_insert_rowid($connection); + $value = $this->conn->getDbh()->lastInsertID(); if (!$value) { return $this->raiseError(null, null, null, 'Could not get last insert ID', __FUNCTION__); @@ -104,9 +103,11 @@ class Doctrine_Sequence_Sqlite extends Doctrine_Sequence */ public function currID($seqName) { - $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true); - $seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true); - $query = "SELECT MAX($seqcol_name) FROM $sequence_name"; - return $this->queryOne($query, 'integer'); + $sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true); + $seqcolName = $this->conn->quoteIdentifier($this->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true); + + $query = 'SELECT MAX(' . $seqcolName . ') FROM ' . $sequenceName; + + return $this->fetchOne($query, 'integer'); } }