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
* 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,
);
/**

View File

@ -18,7 +18,7 @@
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
Doctrine::autoload('Doctrine_Connection_Module');
/**
* Doctrine_Sequence
* 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
return $this->currID($seqName);
}
throw $e;
}
return $result;
}

View File

@ -45,32 +45,31 @@ 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 {
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

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}