1
0
mirror of synced 2024-12-13 22:56:04 +03:00
This commit is contained in:
zYne 2007-01-08 00:13:41 +00:00
parent c8d0a378dd
commit 4928dfef8c
8 changed files with 61 additions and 52 deletions

View File

@ -61,17 +61,23 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
* *
* export Doctrine_Export driver, handles db structure modification abstraction (contains * export Doctrine_Export driver, handles db structure modification abstraction (contains
* methods such as alterTable, createConstraint etc.) * 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_Connection::__get()
* @see Doctrine_DataDict * @see Doctrine_DataDict
* @see Doctrine_Expression * @see Doctrine_Expression
* @see Doctrine_Export * @see Doctrine_Export
* @see Doctrine_Transaction * @see Doctrine_Transaction
* @see Doctrine_Sequence
*/ */
private $modules = array('transaction' => false, private $modules = array('transaction' => false,
'expression' => false, 'expression' => false,
'dataDict' => false, 'dataDict' => false,
'export' => false, 'export' => false,
'import' => false,
'sequence' => false,
'unitOfWork' => false, 'unitOfWork' => false,
); );
/** /**

View File

@ -282,7 +282,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
foreach (array_keys($definition['fields']) as $field) { foreach (array_keys($definition['fields']) as $field) {
$fields[] = $this->conn->quoteIdentifier($field); $fields[] = $this->conn->quoteIdentifier($field);
} }
$query .= ' ('. implode(', ', $fields) . ')'; $query .= ' (' . implode(', ', $fields) . ')';
return $query; return $query;
} }

View File

@ -18,7 +18,7 @@
* and is licensed under the LGPL. For more information, see * and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>. * <http://www.phpdoctrine.com>.
*/ */
Doctrine::autoload('Doctrine_Connection_Module');
/** /**
* Doctrine_Sequence * Doctrine_Sequence
* The base class for sequence handling drivers. * The base class for sequence handling drivers.

View File

@ -64,6 +64,7 @@ class Doctrine_Sequence_Firebird extends Doctrine_Sequence
// BUT generators are not always reset, so return the actual value // BUT generators are not always reset, so return the actual value
return $this->currID($seqName); return $this->currID($seqName);
} }
throw $e;
} }
return $result; return $result;
} }

View File

@ -45,31 +45,30 @@ class Doctrine_Sequence_Mssql extends Doctrine_Sequence
$sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true); $sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true);
$seqcolName = $this->conn->quoteIdentifier($this->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true); $seqcolName = $this->conn->quoteIdentifier($this->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
$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 ' . $sequenceName . ' ON '
"INSERT INTO $sequence_name ($seqcol_name) VALUES (0)"; . 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (0)';
} else { } else {
$query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (0)"; $query = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (0)';
} }
$result =& $this->_doQuery($query, true); try {
$this->popExpect();
if (PEAR::isError($result)) { $this->conn->exec($query);
if ($ondemand && !$this->_checkSequence($sequence_name)) {
$this->loadModule('Manager', null, true); } catch(Doctrine_Connection_Exception $e) {
if ($onDemand && !$this->_checkSequence($sequenceName)) {
// 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) {
'nextID: on demand sequence '.$seq_name.' could not be created'); 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;
} }
$value = $this->lastInsertID($sequenceName); $value = $this->lastInsertID($sequenceName);
@ -95,18 +94,18 @@ class Doctrine_Sequence_Mssql extends Doctrine_Sequence
*/ */
public function lastInsertID($table = null, $field = null) public function lastInsertID($table = null, $field = null)
{ {
$server_info = $this->getServerVersion(); $serverInfo = $this->getServerVersion();
if (is_array($server_info) if (is_array($serverInfo)
&& ! is_null($server_info['major']) && ! is_null($serverInfo['major'])
&& $server_info['major'] >= 8) { && $serverInfo['major'] >= 8) {
$query = "SELECT SCOPE_IDENTITY()"; $query = 'SELECT SCOPE_IDENTITY()';
} else { } else {
$query = "SELECT @@IDENTITY"; $query = 'SELECT @@IDENTITY';
} }
return $this->fetchOne($query, 'integer'); return $this->fetchOne($query);
} }
/** /**
* Returns the current id of a sequence * Returns the current id of a sequence

View File

@ -40,10 +40,10 @@ 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($seqName, $ondemand = true) public function nextId($seqName, $ondemand = true)
{ {
$sequenceName = $this->quoteIdentifier($this->getSequenceName($seq_name), true); $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true);
$seqcolName = $this->quoteIdentifier($this->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true); $seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
$query = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (NULL)'; $query = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (NULL)';
try { try {
@ -63,9 +63,11 @@ class Doctrine_Sequence_Mysql extends Doctrine_Sequence
// First ID of a newly created sequence is 1 // First ID of a newly created sequence is 1
return 1; return 1;
} }
return $result; throw $e;
} }
$value = $this->lastInsertID();
$value = $this->lastInsertId();
if (is_numeric($value)) { if (is_numeric($value)) {
$query = 'DELETE FROM ' . $sequenceName . ' WHERE ' . $seqcolName . ' < ' . $value; $query = 'DELETE FROM ' . $sequenceName . ' WHERE ' . $seqcolName . ' < ' . $value;
$this->conn->exec($query); $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 * @param string name of the field into which a new row was inserted
* @return integer|boolean * @return integer|boolean
*/ */
public function lastInsertID($table = null, $field = null) public function lastInsertId($table = null, $field = null)
{ {
return $this->conn->getDbh()->lastInsertId(); return $this->conn->getDbh()->lastInsertId();
} }
@ -97,12 +99,12 @@ class Doctrine_Sequence_Mysql extends Doctrine_Sequence
* *
* @return integer current id in the given 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); $seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
$query = 'SELECT MAX(' . $seqcolName . ') FROM ' . $sequenceName; $query = 'SELECT MAX(' . $seqcolName . ') FROM ' . $sequenceName;
return $this->queryOne($query, 'integer'); return (int) $this->conn->fetchOne($query);
} }
} }

View File

@ -42,12 +42,11 @@ class Doctrine_Sequence_Oracle extends Doctrine_Sequence
*/ */
public function nextID($seqName, $onDemand = true) public function nextID($seqName, $onDemand = true)
{ {
$sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true); $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true);
$query = 'SELECT ' . $sequenceName . '.nextval FROM DUAL';
$query = "SELECT $sequence_name.nextval FROM DUAL";
try { try {
$result = $this->queryOne($query, 'integer'); $result = $this->conn->fetchOne($query);
} catch(Doctrine_Connection_Exception $e) { } catch(Doctrine_Connection_Exception $e) {
if ($onDemand && $e->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) { if ($onDemand && $e->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) {
@ -58,6 +57,7 @@ class Doctrine_Sequence_Oracle extends Doctrine_Sequence
} }
return $this->nextId($seqName, false); return $this->nextId($seqName, false);
} }
throw $e;
} }
return $result; return $result;
} }
@ -70,9 +70,10 @@ 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); $seqName = $table . (empty($field) ? '' : '_'.$field);
$sequenceName = $this->quoteIdentifier($this->getSequenceName($seqName), true); $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true);
return $this->fetchOne("SELECT $sequence_name.currval", 'integer');
return $this->conn->fetchOne('SELECT ' . $sequenceName . '.currval');
} }
/** /**
* Returns the current id of a sequence * Returns the current id of a sequence
@ -83,11 +84,11 @@ class Doctrine_Sequence_Oracle extends Doctrine_Sequence
*/ */
public function currID($seqName) 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 = 'SELECT (last_number-1) FROM user_sequences';
$query .= ' WHERE sequence_name=' . $this->conn->quote($sequence_name, 'text'); $query .= ' WHERE sequence_name=' . $this->conn->quote($sequenceName, 'text');
$query .= ' OR sequence_name=' . $this->conn->quote(strtoupper($sequence_name), 'text'); $query .= ' OR sequence_name=' . $this->conn->quote(strtoupper($sequenceName), 'text');
return $this->fetchOne($query, 'integer'); return $this->conn->fetchOne($query);
} }
} }

View File

@ -42,14 +42,14 @@ class Doctrine_Sequence_Sqlite extends Doctrine_Sequence
*/ */
public function nextID($seqName, $onDemand = true) public function nextID($seqName, $onDemand = true)
{ {
$sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true); $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true);
$seqcolName = $this->conn->quoteIdentifier($this->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true); $seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
$query = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (NULL)'; $query = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (NULL)';
try { try {
$this->conn->exec($query, true); $this->conn->exec($query);
} catch(Doctrine_Connection_Exception $e) { } catch(Doctrine_Connection_Exception $e) {
if ($onDemand && $result->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) { if ($onDemand && $result->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) {
@ -102,11 +102,11 @@ class Doctrine_Sequence_Sqlite extends Doctrine_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->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true); $seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
$query = 'SELECT MAX(' . $seqcolName . ') FROM ' . $sequenceName; $query = 'SELECT MAX(' . $seqcolName . ') FROM ' . $sequenceName;
return $this->fetchOne($query, 'integer'); return (int) $this->conn->fetchOne($query);
} }
} }