You are here

function DB_pgsql::nextId in Flickr API 5

Returns the next free id in a sequence

DB_pgsql::createSequence(), DB_pgsql::dropSequence()

Parameters

string $seq_name name of the sequence:

boolean $ondemand when true, the seqence is automatically: created if it does not exist

Return value

int the next id number in the sequence. A DB_Error object on failure.

Overrides DB_common::nextId

See also

DB_common::nextID(), DB_common::getSequenceName(),

File

phpFlickr/PEAR/DB/pgsql.php, line 671

Class

DB_pgsql
The methods PEAR DB uses to interact with PHP's pgsql extension for interacting with PostgreSQL databases

Code

function nextId($seq_name, $ondemand = true) {
  $seqname = $this
    ->getSequenceName($seq_name);
  $repeat = false;
  do {
    $this
      ->pushErrorHandling(PEAR_ERROR_RETURN);
    $result =& $this
      ->query("SELECT NEXTVAL('{$seqname}')");
    $this
      ->popErrorHandling();
    if ($ondemand && DB::isError($result) && $result
      ->getCode() == DB_ERROR_NOSUCHTABLE) {
      $repeat = true;
      $this
        ->pushErrorHandling(PEAR_ERROR_RETURN);
      $result = $this
        ->createSequence($seq_name);
      $this
        ->popErrorHandling();
      if (DB::isError($result)) {
        return $this
          ->raiseError($result);
      }
    }
    else {
      $repeat = false;
    }
  } while ($repeat);
  if (DB::isError($result)) {
    return $this
      ->raiseError($result);
  }
  $arr = $result
    ->fetchRow(DB_FETCHMODE_ORDERED);
  $result
    ->free();
  return $arr[0];
}