cvs: php4 /pear DB.php /pear/DB common.php mysql.php

From: Date: Mon, 04 Dec 2000 17:13:31 +0000
Subject: cvs: php4 /pear DB.php /pear/DB common.php mysql.php
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-2937@lists.php.net to get a copy of this message
sbergmann Mon Dec 4 09:13:31 2000 EDT Modified files: /php4/pear DB.php /php4/pear/DB common.php mysql.php Log: Added nextid() method to DB_common. Added lock() and unlock() methods to DB_mysql. Index: php4/pear/DB.php diff -u php4/pear/DB.php:1.36 php4/pear/DB.php:1.37 --- php4/pear/DB.php:1.36 Sun Dec 3 23:05:16 2000 +++ php4/pear/DB.php Mon Dec 4 09:13:29 2000 @@ -17,7 +17,7 @@ // | | // +----------------------------------------------------------------------+ // -// $Id: DB.php,v 1.36 2000/12/04 07:05:16 sbergmann Exp $ +// $Id: DB.php,v 1.37 2000/12/04 17:13:29 sbergmann Exp $ // // Database independent query interface. // Index: php4/pear/DB/common.php diff -u php4/pear/DB/common.php:1.25 php4/pear/DB/common.php:1.26 --- php4/pear/DB/common.php:1.25 Sun Dec 3 22:56:29 2000 +++ php4/pear/DB/common.php Mon Dec 4 09:13:30 2000 @@ -40,6 +40,7 @@ var $error_callback; var $last_query = ''; var $fetchmode = DB_FETCHMODE_DEFAULT; + var $seq_table = "db_sequence"; // }}} // {{{ toString() @@ -399,7 +400,7 @@ { if( is_array( $data ) ) { - $pdata = &$data[$i]; + $pdata = &$data[ $i ]; } else @@ -805,6 +806,65 @@ } return $results; + } + + // }}} + // {{{ nextid() + + function nextid( $seq_name ) + { + // lock db_sequence table + if( $this->lock( $this->seq_table ) ) + { + // check, if this sequence already exists + $result = $this->query( "select nextid from " . $this->seq_table . " where seq_name = '$seq_name'" ); + + // catch db error + if( DB::isError( $result ) ) + { + return false; + } + + // sequence already exists + if( is_array( $row = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) ) + { + // get next id for this sequence + $id = $row[ "nextid" ]; + + // update next id for this sequence in db_sequence table + $result = $this->query( "update " . $this->seq_table . " set nextid = nextid + 1 where seq_name = '$seq_name'" ); + + // catch db error + if( DB::isError( $result ) ) + { + return false; + } + } + + // sequence doesn't exist, create it + else + { + // init sequence's id to 1 + $id = 1; + + // create entry in db_sequence table + $result = $this->query( "insert into " . $this->seq_table . " values( '$seq_name', '$id' )" ); + + // catch db error + if( DB::isError( $result ) ) + { + return false; + } + } + } + + else + { + return false; + } + + // return next id for requested sequence + return $id; } // }}} Index: php4/pear/DB/mysql.php diff -u php4/pear/DB/mysql.php:1.29 php4/pear/DB/mysql.php:1.30 --- php4/pear/DB/mysql.php:1.29 Sun Dec 3 22:56:29 2000 +++ php4/pear/DB/mysql.php Mon Dec 4 09:13:30 2000 @@ -543,6 +543,73 @@ } // }}} + // {{{ lock() + + function lock( $table, $mode = "write" ) + { + // build query string to lock tables + $query = "lock tables "; + + // parameter $table is an array - parse it + if( is_array( $table ) ) + { + // walk through array + while( list( $key, $value ) = each( $table ) ) + { + if( $key == "read" && $key != 0) + { + $query .= "$value read, "; + } + + else + { + $query .= "$value $mode, "; + } + } + + $query = substr( $query, 0, -2 ); + } + + // parameter $table is just one table name + else + { + $query .= "$table $mode"; + } + + // perform query to lock table(s) + $result = mysql_query( $query, $this->connection ); + + // check, if query succeeded + if( !$result ) + { + // query failed + return false; + } + + // query succeeded + return true; + } + + // }}} + // {{{ lock() + + function unlock() + { + // perform query to unlock tables + $result = mysql_query( "unlock tables", $this->connection ); + + // check, if query succeeded + if( !$result ) + { + // query failed + return false; + } + + // query succeeded + return true; + } + + // }}} // TODO/wishlist: // simpleFetch

« previous php.cvs (#2937) next »