This commit is contained in:
parent
27e36c2e23
commit
1d14143597
@ -32,5 +32,57 @@ Doctrine::autoload('Doctrine_Sequence');
|
|||||||
*/
|
*/
|
||||||
class Doctrine_Sequence_Pgsql extends Doctrine_Sequence
|
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
|
||||||
|
*
|
||||||
|
* @return integer next id in the given sequence
|
||||||
|
*/
|
||||||
|
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__);
|
||||||
|
}
|
||||||
|
return $this->nextId($seq_name, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns the autoincrement ID if supported or $id or fetches the current
|
||||||
|
* ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
|
||||||
|
*
|
||||||
|
* @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)
|
||||||
|
{
|
||||||
|
$seq = $table.(empty($field) ? '' : '_'.$field);
|
||||||
|
$sequence_name = $this->getSequenceName($seq);
|
||||||
|
return $this->queryOne("SELECT currval('$sequence_name')", 'integer');
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns the current id of a sequence
|
||||||
|
*
|
||||||
|
* @param string $seqName name of the sequence
|
||||||
|
*
|
||||||
|
* @return integer current id in the given sequence
|
||||||
|
*/
|
||||||
|
public function currID($seqName)
|
||||||
|
{
|
||||||
|
$sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
|
||||||
|
return $this->queryOne("SELECT last_value FROM $sequence_name", 'integer');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,5 +32,81 @@ Doctrine::autoload('Doctrine_Sequence');
|
|||||||
*/
|
*/
|
||||||
class Doctrine_Sequence_Sqlite extends Doctrine_Sequence
|
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
|
||||||
|
*
|
||||||
|
* @return integer next id in the given sequence
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
$value = $this->lastInsertID();
|
||||||
|
if (is_numeric($value)) {
|
||||||
|
$query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value";
|
||||||
|
$result =& $this->_doQuery($query, true);
|
||||||
|
if (PEAR::isError($result)) {
|
||||||
|
$this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns the autoincrement ID if supported or $id or fetches the current
|
||||||
|
* ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
|
||||||
|
*
|
||||||
|
* @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)
|
||||||
|
{
|
||||||
|
$connection = $this->getConnection();
|
||||||
|
if (PEAR::isError($connection)) {
|
||||||
|
return $connection;
|
||||||
|
}
|
||||||
|
$value = @sqlite_last_insert_rowid($connection);
|
||||||
|
if (!$value) {
|
||||||
|
return $this->raiseError(null, null, null,
|
||||||
|
'Could not get last insert ID', __FUNCTION__);
|
||||||
|
}
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns the current id of a sequence
|
||||||
|
*
|
||||||
|
* @param string $seqName name of the sequence
|
||||||
|
*
|
||||||
|
* @return integer current id in the given 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');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user