From 4928dfef8c8ad0f0efebdd42e83d757b0d22572b Mon Sep 17 00:00:00 2001 From: zYne Date: Mon, 8 Jan 2007 00:13:41 +0000 Subject: [PATCH] --- lib/Doctrine/Connection.php | 6 ++++ lib/Doctrine/Export.php | 2 +- lib/Doctrine/Sequence.php | 2 +- lib/Doctrine/Sequence/Firebird.php | 1 + lib/Doctrine/Sequence/Mssql.php | 47 +++++++++++++++--------------- lib/Doctrine/Sequence/Mysql.php | 20 +++++++------ lib/Doctrine/Sequence/Oracle.php | 23 ++++++++------- lib/Doctrine/Sequence/Sqlite.php | 12 ++++---- 8 files changed, 61 insertions(+), 52 deletions(-) diff --git a/lib/Doctrine/Connection.php b/lib/Doctrine/Connection.php index fa6586d39..fb489f5b2 100644 --- a/lib/Doctrine/Connection.php +++ b/lib/Doctrine/Connection.php @@ -61,17 +61,23 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * * export Doctrine_Export driver, handles db structure modification abstraction (contains * methods such as alterTable, createConstraint etc.) + * import Doctrine_Import driver, handles db schema reading + * + * sequence Doctrine_Sequence driver, handles sequential id generation and retrieval * * @see Doctrine_Connection::__get() * @see Doctrine_DataDict * @see Doctrine_Expression * @see Doctrine_Export * @see Doctrine_Transaction + * @see Doctrine_Sequence */ private $modules = array('transaction' => false, 'expression' => false, 'dataDict' => false, 'export' => false, + 'import' => false, + 'sequence' => false, 'unitOfWork' => false, ); /** diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index 723a006db..f7f42cf96 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -282,7 +282,7 @@ class Doctrine_Export extends Doctrine_Connection_Module foreach (array_keys($definition['fields']) as $field) { $fields[] = $this->conn->quoteIdentifier($field); } - $query .= ' ('. implode(', ', $fields) . ')'; + $query .= ' (' . implode(', ', $fields) . ')'; return $query; } diff --git a/lib/Doctrine/Sequence.php b/lib/Doctrine/Sequence.php index 9624a5e98..76b9cdce1 100644 --- a/lib/Doctrine/Sequence.php +++ b/lib/Doctrine/Sequence.php @@ -18,7 +18,7 @@ * and is licensed under the LGPL. For more information, see * . */ - +Doctrine::autoload('Doctrine_Connection_Module'); /** * Doctrine_Sequence * The base class for sequence handling drivers. diff --git a/lib/Doctrine/Sequence/Firebird.php b/lib/Doctrine/Sequence/Firebird.php index bfa904f27..aa6ae8f45 100644 --- a/lib/Doctrine/Sequence/Firebird.php +++ b/lib/Doctrine/Sequence/Firebird.php @@ -64,6 +64,7 @@ class Doctrine_Sequence_Firebird extends Doctrine_Sequence // BUT generators are not always reset, so return the actual value return $this->currID($seqName); } + throw $e; } return $result; } diff --git a/lib/Doctrine/Sequence/Mssql.php b/lib/Doctrine/Sequence/Mssql.php index 44351c8fc..322bc50f7 100644 --- a/lib/Doctrine/Sequence/Mssql.php +++ b/lib/Doctrine/Sequence/Mssql.php @@ -45,31 +45,30 @@ class Doctrine_Sequence_Mssql extends Doctrine_Sequence $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 ". - "INSERT INTO $sequence_name ($seqcol_name) VALUES (0)"; + $query = 'SET IDENTITY_INSERT ' . $sequenceName . ' ON ' + . 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (0)'; } else { - $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (0)"; + $query = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (0)'; } - $result =& $this->_doQuery($query, true); - $this->popExpect(); - if (PEAR::isError($result)) { - if ($ondemand && !$this->_checkSequence($sequence_name)) { - $this->loadModule('Manager', null, true); + try { + + $this->conn->exec($query); + + } catch(Doctrine_Connection_Exception $e) { + if ($onDemand && !$this->_checkSequence($sequenceName)) { // 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, - 'nextID: on demand sequence '.$seq_name.' could not be created'); - } 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($sequenceName); @@ -95,18 +94,18 @@ class Doctrine_Sequence_Mssql extends Doctrine_Sequence */ public function lastInsertID($table = null, $field = null) { - $server_info = $this->getServerVersion(); - if (is_array($server_info) - && ! is_null($server_info['major']) - && $server_info['major'] >= 8) { + $serverInfo = $this->getServerVersion(); + if (is_array($serverInfo) + && ! is_null($serverInfo['major']) + && $serverInfo['major'] >= 8) { - $query = "SELECT SCOPE_IDENTITY()"; + $query = 'SELECT SCOPE_IDENTITY()'; } else { - $query = "SELECT @@IDENTITY"; + $query = 'SELECT @@IDENTITY'; } - return $this->fetchOne($query, 'integer'); + return $this->fetchOne($query); } /** * Returns the current id of a sequence diff --git a/lib/Doctrine/Sequence/Mysql.php b/lib/Doctrine/Sequence/Mysql.php index e43bcdbd2..f68674f80 100644 --- a/lib/Doctrine/Sequence/Mysql.php +++ b/lib/Doctrine/Sequence/Mysql.php @@ -40,10 +40,10 @@ class Doctrine_Sequence_Mysql extends Doctrine_Sequence * * @return integer next id in the given sequence */ - public function nextID($seqName, $ondemand = true) + public function nextId($seqName, $ondemand = true) { - $sequenceName = $this->quoteIdentifier($this->getSequenceName($seq_name), true); - $seqcolName = $this->quoteIdentifier($this->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true); + $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true); + $seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true); $query = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (NULL)'; try { @@ -63,9 +63,11 @@ class Doctrine_Sequence_Mysql extends Doctrine_Sequence // First ID of a newly created sequence is 1 return 1; } - return $result; + throw $e; } - $value = $this->lastInsertID(); + + $value = $this->lastInsertId(); + if (is_numeric($value)) { $query = 'DELETE FROM ' . $sequenceName . ' WHERE ' . $seqcolName . ' < ' . $value; $this->conn->exec($query); @@ -86,7 +88,7 @@ class Doctrine_Sequence_Mysql extends Doctrine_Sequence * @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) { return $this->conn->getDbh()->lastInsertId(); } @@ -97,12 +99,12 @@ class Doctrine_Sequence_Mysql extends Doctrine_Sequence * * @return integer current id in the given sequence */ - public function currID($seqName) + public function currId($seqName) { - $sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true); + $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true); $seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true); $query = 'SELECT MAX(' . $seqcolName . ') FROM ' . $sequenceName; - return $this->queryOne($query, 'integer'); + return (int) $this->conn->fetchOne($query); } } diff --git a/lib/Doctrine/Sequence/Oracle.php b/lib/Doctrine/Sequence/Oracle.php index 61aedd339..86791882d 100644 --- a/lib/Doctrine/Sequence/Oracle.php +++ b/lib/Doctrine/Sequence/Oracle.php @@ -42,12 +42,11 @@ class Doctrine_Sequence_Oracle extends Doctrine_Sequence */ public function nextID($seqName, $onDemand = true) { - $sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true); - - $query = "SELECT $sequence_name.nextval FROM DUAL"; + $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true); + $query = 'SELECT ' . $sequenceName . '.nextval FROM DUAL'; try { - $result = $this->queryOne($query, 'integer'); + $result = $this->conn->fetchOne($query); } catch(Doctrine_Connection_Exception $e) { if ($onDemand && $e->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) { @@ -58,6 +57,7 @@ class Doctrine_Sequence_Oracle extends Doctrine_Sequence } return $this->nextId($seqName, false); } + throw $e; } return $result; } @@ -70,9 +70,10 @@ class Doctrine_Sequence_Oracle extends Doctrine_Sequence */ public function lastInsertID($table = null, $field = null) { - $seq = $table.(empty($field) ? '' : '_'.$field); - $sequenceName = $this->quoteIdentifier($this->getSequenceName($seqName), true); - return $this->fetchOne("SELECT $sequence_name.currval", 'integer'); + $seqName = $table . (empty($field) ? '' : '_'.$field); + $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true); + + return $this->conn->fetchOne('SELECT ' . $sequenceName . '.currval'); } /** * Returns the current id of a sequence @@ -83,11 +84,11 @@ class Doctrine_Sequence_Oracle extends Doctrine_Sequence */ public function currID($seqName) { - $sequenceName = $this->quoteIdentifier($this->getSequenceName($seqName), true); + $sequenceName = $this->conn->quoteIdentifier($this->conn->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'); + $query .= ' WHERE sequence_name=' . $this->conn->quote($sequenceName, 'text'); + $query .= ' OR sequence_name=' . $this->conn->quote(strtoupper($sequenceName), 'text'); - return $this->fetchOne($query, 'integer'); + return $this->conn->fetchOne($query); } } diff --git a/lib/Doctrine/Sequence/Sqlite.php b/lib/Doctrine/Sequence/Sqlite.php index c7e78cb95..c9cb23313 100644 --- a/lib/Doctrine/Sequence/Sqlite.php +++ b/lib/Doctrine/Sequence/Sqlite.php @@ -42,14 +42,14 @@ class Doctrine_Sequence_Sqlite extends Doctrine_Sequence */ public function nextID($seqName, $onDemand = true) { - $sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true); - $seqcolName = $this->conn->quoteIdentifier($this->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true); + $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true); + $seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true); $query = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (NULL)'; try { - $this->conn->exec($query, true); + $this->conn->exec($query); } catch(Doctrine_Connection_Exception $e) { if ($onDemand && $result->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) { @@ -102,11 +102,11 @@ class Doctrine_Sequence_Sqlite extends Doctrine_Sequence */ public function currID($seqName) { - $sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true); - $seqcolName = $this->conn->quoteIdentifier($this->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true); + $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true); + $seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true); $query = 'SELECT MAX(' . $seqcolName . ') FROM ' . $sequenceName; - return $this->fetchOne($query, 'integer'); + return (int) $this->conn->fetchOne($query); } }