cvs: php4 /pear/DB odbc.php

From: Date: Tue, 21 Aug 2001 02:46:29 +0000
Subject: cvs: php4 /pear/DB odbc.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-606@lists.php.net to get a copy of this message
chagenbu Mon Aug 20 22:46:29 2001 EDT Modified files: /php4/pear/DB odbc.php Log: add emulated sequences for ODBC. These are _not_ atomic; if someone knows how to make them so with generic sql, please make it so. Index: php4/pear/DB/odbc.php diff -u php4/pear/DB/odbc.php:1.38 php4/pear/DB/odbc.php:1.39 --- php4/pear/DB/odbc.php:1.38 Mon Aug 20 22:43:54 2001 +++ php4/pear/DB/odbc.php Mon Aug 20 22:46:29 2001 @@ -17,7 +17,7 @@ // | | // +----------------------------------------------------------------------+ // -// $Id: odbc.php,v 1.38 2001/08/21 02:43:54 chagenbu Exp $ +// $Id: odbc.php,v 1.39 2001/08/21 02:46:29 chagenbu Exp $ // // Database independent query interface definition for PHP's ODBC // extension. @@ -266,6 +266,78 @@ return odbc_error() . ' ' . odbc_errormsg(); } return odbc_error($this->connection) . ' ' . odbc_errormsg($this->connection); + } + + // }}} + // {{{ nextId() + + /** + * Get the next value in a sequence. We emulate sequences + * for odbc. Will create the sequence if it does not exist. + * + * @access public + * + * @param $seq_name the name of the sequence + * + * @param $ondemand whether to create the sequence table on demand + * (default is true) + * + * @return a sequence integer, or a DB error + */ + function nextId($seq_name, $ondemand = true) + { + $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name); + $repeat = 0; + do { + $result = $this->query("update ${sqn}_seq set id = id + 1"); + if ($ondemand && DB::isError($result) && + $result->getCode() == DB_ERROR_NOT_FOUND) { + $repeat = 1; + $result = $this->createSequence($seq_name); + if (DB::isError($result)) { + return $result; + } + $result = $this->query("insert into ${sqn}_seq (id) values(0)"); + } else { + $repeat = 0; + } + } while ($repeat); + + if (DB::isError($result)) { + return $result; + } + + $result = $this->query("select id from ${sqn}_seq"); + if (DB::isError($result)) { + return $result; + } + + $row = $result->fetchRow(DB_FETCHMODE_ASSOC); + if (DB::isError($row || !$row)) { + return $row; + } + + return $row['id']; + } + + // }}} + // {{{ createSequence() + + function createSequence($seq_name) + { + $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name); + return $this->query("CREATE TABLE ${sqn}_seq ". + '(id bigint NOT NULL,'. + ' PRIMARY KEY(id))'); + } + + // }}} + // {{{ dropSequence() + + function dropSequence($seq_name) + { + $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name); + return $this->query("DROP TABLE ${sqn}_seq"); } // }}}

« previous php.pear.cvs (#606) next »