sequences implementation for DB/pgsql
| From: | Alexey Borzov | Date: | Wed, 13 Dec 2000 13:12:55 +0000 |
| Subject: | sequences implementation for DB/pgsql | ||
| Groups: | php.pear | ||
| Request: | Send a blank email to php-pear+get-677@lists.php.net to get a copy of this message | ||
Greetings!
I've decided to give PEAR::DB a test and immediately noticed
that DB_pgsql lacks sequence support.
So I copied an implementation from DB_mysql and redone it for native
Postgres sequences.
The functions are tested and work as intended:
1) if a sequence exists, nextId is returned,
2) if it doesn't exist - it gets created,
3) if it cannot be created - an error is returned (I tested with the
sequence names that begin with 'pg_' prefix, which is reserved for
system catalogs).
The functions are formatted in accordance with the "latest
standard", PHPDoc comments are added, blah-blah-blah
So you can just commit them into CVS. ;]
--
Yours, Alexey V. Borzov, Webmaster of RDW
// {{{ nextId() /** * Get the next value in a sequence. * * We are using native PostgreSQL sequences. If a sequence * does not exist, it will be created. * * @access public * @param string $seq_name the name of the sequence * @param bool $ondemand whether to create the sequence on demand * @return a sequence integer, or a DB error */ function nextId($seq_name, $ondemand = true) { $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name); do { $result = $this->query("SELECT nextval('${sqn}_seq')"); if ($ondemand && DB::isError($result)) { // no error codes for Pg, so we do it on every error $result = $this->createSequence($seq_name); $repeat = !DB::isError($result); } else { $repeat = false; } } while ($repeat); if (DB::isError($result)) { return $result; } $arr = $result->fetchRow(DB_FETCHMODE_ORDERED); $result->free(); return $arr[0]; } // }}} // {{{ createSequence() /** * Create the sequence * * @param string $seq_name the name of the sequence * @return DB error * @access public */ function createSequence($seq_name) { $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name); return $this->query("CREATE SEQUENCE ${sqn}_seq"); } // }}}
// {{{ nextId() /** * Get the next value in a sequence. * * We are using native PostgreSQL sequences. If a sequence * does not exist, it will be created. * * @access public * @param string $seq_name the name of the sequence * @param bool $ondemand whether to create the sequence on demand * @return a sequence integer, or a DB error */ function nextId($seq_name, $ondemand = true) { $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name); do { $result = $this->query("SELECT nextval('${sqn}_seq')"); if ($ondemand && DB::isError($result)) { // no error codes for Pg, so we do it on every error $result = $this->createSequence($seq_name); $repeat = !DB::isError($result); } else { $repeat = false; } } while ($repeat); if (DB::isError($result)) { return $result; } $arr = $result->fetchRow(DB_FETCHMODE_ORDERED); $result->free(); return $arr[0]; } // }}} // {{{ createSequence() /** * Create the sequence * * @param string $seq_name the name of the sequence * @return DB error * @access public */ function createSequence($seq_name) { $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name); return $this->query("CREATE SEQUENCE ${sqn}_seq"); } // }}}