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

From: Date: Sun, 16 Jul 2000 07:59:40 +0000
Subject: cvs: php4 /pear/DB common.php mysql.php storage.php
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-780@lists.php.net to get a copy of this message
ssb Sun Jul 16 00:59:40 2000 EDT Modified files: /php4/pear/DB common.php mysql.php storage.php Log: * DB_common: - added getCol() and toString() - added prepare/execute support to getRow() - support for multiple simultaneous emulated prepare/execute * DB_storage: - added setup(), insert(), toString() and dump() Index: php4/pear/DB/common.php diff -u php4/pear/DB/common.php:1.10 php4/pear/DB/common.php:1.11 --- php4/pear/DB/common.php:1.10 Sun Feb 27 06:46:27 2000 +++ php4/pear/DB/common.php Sun Jul 16 00:59:40 2000 @@ -42,8 +42,26 @@ var $features; // assoc of capabilities for this DB implementation var $errorcode_map; // assoc mapping native error codes to DB ones var $type; // DB type (mysql, oci8, odbc etc.) + var $prepare_tokens; + var $prepare_types; + var $prepare_maxstmt; // }}} + // {{{ toString() + + function toString() { + $info = get_class(&$this); + $info .= + ": (phptype=" . $this->phptype . + ", dbsyntax=" . $this->dbsyntax . + ")"; + if ($this->connection) { + $info .= " [connected]"; + } + return $info; + } + + // }}} // {{{ constructor function DB_common() { @@ -159,7 +177,7 @@ */ function execute_emulate_query($stmt, $data = false) { $p = &$this->prepare_tokens; - $stmt = 0; // XXX HORRIBLE HACK + $stmt = (int)$this->prepare_maxstmt++; if (!isset($this->prepare_tokens[$stmt]) || !is_array($this->prepare_tokens[$stmt]) || !sizeof($this->prepare_tokens[$stmt])) { @@ -252,6 +270,9 @@ } $ret = &$row[0]; $this->freeResult($res); + if (isset($sth)) { + $this->freeResult($sth); + } return $ret; } @@ -267,7 +288,15 @@ * 0, or a DB error code. */ function &getRow($query, $getmode = DB_GETMODE_DEFAULT, $params = array()) { - $res = $this->simpleQuery($query); + if (sizeof($params) > 0) { + $sth = $this->prepare($query); + if (DB::isError($sth)) { + return $sth; + } + $res = $this->execute($sth, &$params); + } else { + $res = $this->simpleQuery($query); + } if (DB::isError($res)) { return $res; } @@ -280,6 +309,45 @@ } // }}} + + /** + * Fetch a single column from a result set and return it as an + * indexed array. + * + * @param $query the SQL query + * + * @param $col which column to return (integer [column number, + * starting at 0] or string [column name]) + * + * @return array an indexed array with the data from the first + * row at index 0, or a DB error code. + */ + function &getCol($query, $col = 0, $params = array()) { + if (sizeof($params) > 0) { + $sth = $this->prepare($query); + if (DB::isError($sth)) { + return $sth; + } + $res = $this->execute($sth, &$params); + } else { + $res = $this->simpleQuery($query); + } + if (DB::isError($res)) { + return $res; + } + $getmode = is_int($col) ? DB_GETMODE_ORDERED : DB_GETMODE_ASSOC; + $ret = array(); + while ($row = $this->fetchRow($res, $getmode)) { + if (DB::isError($row)) { + $ret = $row; + break; + } + $ret[] = $row[$col]; + } + $this->freeResult($res); + return $ret; + } + // {{{ getAssoc() /** @@ -337,19 +405,18 @@ } $results = array(); if ($cols > 2 || $force_array) { + return DB_ERROR; // return array values // XXX this part can be optimized - while (!DB::isError($row = $this->fetchRow($res))) { + while ($row = $this->fetchRow($res) && !DB::isError($row)) { reset($row); // we copy the row of data into a new array // to get indices running from 0 again - for ($i = 1; $i < $cols; $i++) { - $results[$row[0]][$i-1] = $row[$i]; - } + $results[$row[0]] = array_slice($row, 1); } } else { // return scalar values - while (!DB::isError($row = $this->fetchRow($res))) { + while (($row = $this->fetchRow($res)) && !DB::isError($row)) { $results[$row[0]] = $row[1]; } } Index: php4/pear/DB/mysql.php diff -u php4/pear/DB/mysql.php:1.16 php4/pear/DB/mysql.php:1.17 --- php4/pear/DB/mysql.php:1.16 Wed Jun 28 19:35:08 2000 +++ php4/pear/DB/mysql.php Sun Jul 16 00:59:40 2000 @@ -28,7 +28,7 @@ // be registered here. // -include_once 'DB/common.php'; +require_once 'DB/common.php'; class DB_mysql extends DB_common { // {{{ properties Index: php4/pear/DB/storage.php diff -u php4/pear/DB/storage.php:1.4 php4/pear/DB/storage.php:1.5 --- php4/pear/DB/storage.php:1.4 Thu Mar 9 13:57:14 2000 +++ php4/pear/DB/storage.php Sun Jul 16 00:59:40 2000 @@ -21,7 +21,7 @@ // can be manipulated and that updates the database accordingly. // -use "DB"; +require_once "DB.php"; function DB_storage_destructor() { global $DB_storage_object_list; @@ -31,6 +31,7 @@ while (list($ind, $obj) = each($DB_storage_object_list)) { $obj->destroy(); } + $DB_storage_object_list = null; } } @@ -60,10 +61,11 @@ */ function DB_storage($table, $keycolumn, &$dbh) { global $DB_storage_object_list; - if (!is_array($DB_storage_object_list)) { + if (is_array($DB_storage_object_list)) { + $DB_storage_object_list[] = &$this; + } else { $DB_storage_object_list = array(&$this); } - $DB_storage_object_list[] = &$this; $this->_table = $table; $this->_keycolumn = $keycolumn; $this->_dbh = $dbh; @@ -71,10 +73,98 @@ } /** + * Method used to initialize a DB_storage object from the + * configured table. + * @param $keyval the key of the row to fetch + * @return int DB_OK on success, DB error if not + */ + function setup($keyval) { + if (is_int($keyval)) { + $qval = "$keyval"; + } else { + $qval = "'" . $this->_dbh->quoteString($keyval) . "'"; + } + $sth = $this->_dbh->query("SELECT * FROM " . + $this->_table . " WHERE " . + $this->_keycolumn . " = $qval"); + if (DB::isError($sth)) { + return $sth; + } + while ($row = $sth->fetchRow(DB_GETMODE_ASSOC)) { + reset($row); + while (list($key, $value) = each($row)) { + $this->_properties[$key] = true; + $this->$key = &$value; + unset($value); + } + } + } + + /** + * Create a new (empty) row in the configured table for this + * object. + */ + function insert($newid = false) { + if (is_int($newid)) { + $qid = "$newid"; + } else { + $qid = "'" . $this->_dbh->quoteString($newid) . "'"; + } + $sth = $this->_dbh->query("INSERT INTO " . + $this->_table . " (" . + $this->_keycolumn . + ") VALUES($qid)"); + if (DB::isError($sth)) { + return $sth; + } + $this->setup($newid); + } + + /** + * Output a simple description of this DB_storage object. + * @return string object description + */ + function toString() { + $info = get_class(&$this); + $info .= " (table="; + $info .= $this->_table; + $info .= ", keycolumn="; + $info .= $this->_keycolumn; + $info .= ", dbh="; + if (is_object($this->_dbh)) { + $info .= $this->_dbh->toString(); + } else { + $info .= "null"; + } + $info .= ")"; + if (sizeof($this->_properties)) { + $keyname = $this->_keycolumn; + $key = $this->$keyname; + $info .= " [loaded, key=$key]"; + } + if (sizeof($this->_changes)) { + $info .= " [modified]"; + } + return $info; + } + + /** + * Dump the contents of this object to "standard output". + */ + function dump() { + reset($this->_properties); + while (list($prop, $foo) = each($this->_properties)) { + print "$prop = "; + print htmlentities($this->$prop); + print "<BR>\n"; + } + } + + /** * Static method used to create new DB storage objects. * @param $data assoc. array where the keys are the names * of properties/columns - * @return object a new instance of DB_storage (or inheriting class) + * @return object a new instance of DB_storage or a subclass of it */ function &create($table, &$data) { $classname = get_class(&$this);

« previous php.cvs (#780) next »