1
0
mirror of synced 2025-01-05 16:53:21 +03:00

sequence porting continues

This commit is contained in:
zYne 2007-01-06 10:47:57 +00:00
parent 1d14143597
commit 24f6b06256
3 changed files with 69 additions and 65 deletions

View File

@ -36,25 +36,27 @@ class Doctrine_Sequence_Oracle extends Doctrine_Sequence
* Returns the next free id of a sequence * Returns the next free id of a sequence
* *
* @param string $seqName name of the 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 * @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"; $query = "SELECT $sequence_name.nextval FROM DUAL";
$this->expectError(MDB2_ERROR_NOSUCHTABLE);
$result = $this->queryOne($query, 'integer'); try {
$this->popExpect(); $result = $this->queryOne($query, 'integer');
if (PEAR::isError($result)) { } catch(Doctrine_Connection_Exception $e) {
if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) { if ($onDemand && $e->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) {
$this->loadModule('Manager', null, true);
$result = $this->manager->createSequence($seq_name); try {
if (PEAR::isError($result)) { $result = $this->conn->export->createSequence($seqName);
return $result; } 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; return $result;
@ -83,8 +85,8 @@ class Doctrine_Sequence_Oracle extends Doctrine_Sequence
{ {
$sequence_name = $this->getSequenceName($seq_name); $sequence_name = $this->getSequenceName($seq_name);
$query = 'SELECT (last_number-1) FROM user_sequences'; $query = 'SELECT (last_number-1) FROM user_sequences';
$query.= ' WHERE sequence_name='.$this->quote($sequence_name, 'text'); $query.= ' WHERE sequence_name=' . $this->quote($sequence_name, 'text');
$query.= ' OR sequence_name='.$this->quote(strtoupper($sequence_name), 'text'); $query.= ' OR sequence_name=' . $this->quote(strtoupper($sequence_name), 'text');
return $this->queryOne($query, 'integer'); return $this->queryOne($query, 'integer');
} }
} }

View File

@ -36,26 +36,26 @@ class Doctrine_Sequence_Pgsql extends Doctrine_Sequence
* Returns the next free id of a sequence * Returns the next free id of a sequence
* *
* @param string $seqName name of the 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 * @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 NEXTVAL('$sequence_name')";
$this->expectError(MDB2_ERROR_NOSUCHTABLE); $query = "SELECT NEXTVAL('" . $sequenceName . "')";
$result = $this->queryOne($query, 'integer'); try {
$this->popExpect(); $result = $this->fetchOne($query, 'integer');
if (PEAR::isError($result)) { } catch(Doctrine_Connection_Exception $e) {
if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) { if ($onDemand && $e->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) {
$this->loadModule('Manager', null, true);
$result = $this->manager->createSequence($seq_name); try {
if (PEAR::isError($result)) { $result = $this->conn->export->createSequence($seqName);
return $this->raiseError($result, null, null, } catch(Doctrine_Exception $e) {
'on demand sequence could not be created', __FUNCTION__); 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; 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 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 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); $seq = $table.(empty($field) ? '' : '_'.$field);
$sequence_name = $this->getSequenceName($seq); $sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true);
return $this->queryOne("SELECT currval('$sequence_name')", 'integer');
return $this->fetchOne("SELECT currval('" . $sequenceName . "')", 'integer');
} }
/** /**
* Returns the current id of a sequence * 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 * @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); $sequenceName = $this->quoteIdentifier($this->getSequenceName($seqName), true);
return $this->queryOne("SELECT last_value FROM $sequence_name", 'integer'); return $this->fetchOne('SELECT last_value FROM ' . $sequenceName, 'integer');
} }
} }

View File

@ -36,36 +36,39 @@ class Doctrine_Sequence_Sqlite extends Doctrine_Sequence
* Returns the next free id of a sequence * Returns the next free id of a sequence
* *
* @param string $seqName name of the 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 * @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);
$seqcol_name = $this->options['seqcol_name']; $seqcolName = $this->conn->quoteIdentifier($this->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
$query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (NULL)";
$this->expectError(MDB2_ERROR_NOSUCHTABLE); $query = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (NULL)';
$result =& $this->_doQuery($query, true);
$this->popExpect(); try {
if (PEAR::isError($result)) {
if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) { $this->conn->exec($query, true);
$this->loadModule('Manager', null, true);
} catch(Doctrine_Connection_Exception $e) {
if ($onDemand && $result->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) {
// 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);
if (PEAR::isError($result)) { try {
return $this->raiseError($result, null, null, $result = $this->conn->export->createSequence($seqName, 2);
'on demand sequence '.$seq_name.' could not be created', __FUNCTION__); } catch(Doctrine_Exception $e) {
} else { 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;
} }
// 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)) { if (is_numeric($value)) {
$query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value"; $query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value";
$result =& $this->_doQuery($query, true); $result =& $this->_doQuery($query, true);
@ -84,11 +87,7 @@ class Doctrine_Sequence_Sqlite extends Doctrine_Sequence
*/ */
public function lastInsertID($table = null, $field = null) public function lastInsertID($table = null, $field = null)
{ {
$connection = $this->getConnection(); $value = $this->conn->getDbh()->lastInsertID();
if (PEAR::isError($connection)) {
return $connection;
}
$value = @sqlite_last_insert_rowid($connection);
if (!$value) { if (!$value) {
return $this->raiseError(null, null, null, return $this->raiseError(null, null, null,
'Could not get last insert ID', __FUNCTION__); 'Could not get last insert ID', __FUNCTION__);
@ -104,9 +103,11 @@ class Doctrine_Sequence_Sqlite extends Doctrine_Sequence
*/ */
public function currID($seqName) public function currID($seqName)
{ {
$sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true); $sequenceName = $this->conn->quoteIdentifier($this->getSequenceName($seqName), true);
$seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true); $seqcolName = $this->conn->quoteIdentifier($this->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
$query = "SELECT MAX($seqcol_name) FROM $sequence_name";
return $this->queryOne($query, 'integer'); $query = 'SELECT MAX(' . $seqcolName . ') FROM ' . $sequenceName;
return $this->fetchOne($query, 'integer');
} }
} }