Source for file storage.php
Documentation is available at storage.php
/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Stig Bakken <stig@php.net> |
// | Maintainer: Daniel Convissor <danielc@php.net> |
// +----------------------------------------------------------------------+
// $Id: storage.php,v 1.17 2004/02/04 01:42:05 danielc Exp $
* Provides an object interface to a table row.
* It lets you add, delete and change rows using objects rather than SQL
* @version $Id: storage.php,v 1.17 2004/02/04 01:42:05 danielc Exp $
* @author Stig Bakken <stig@php.net>
/** the name of the table (or view, if the backend database supports
updates in views) we hold data from */
/** which column(s) in the table contains primary keys, can be a
string for single-column primary keys, or an array of strings
for multiple-column primary keys */
/** DB connection handle used for all transactions */
/** an assoc with the names of database fields stored as properties
var $_properties = array ();
/** an assoc with the names of the properties in this object that
have been changed since they were fetched from the database */
/** flag that decides if data in this object can be changed.
objects that don't have their table's key column in their
property lists will be flagged as read-only. */
/** function or method that implements a validator for fields that
are set, this validator function returns true if the field is
* @param $table string the name of the database table
* @param $keycolumn mixed string with name of key column, or array of
* strings if the table has a primary key of more than one column
* @param $dbh object database connection object
* @param $validator mixed function or method used to validate
* each new value, called with three parameters: the name of the
* field/column that is changing, a reference to the new value and
* a reference to this object
function DB_storage($table, $keycolumn, &$dbh, $validator = null )
$this->_keycolumn = $keycolumn;
$this->_readonly = false;
$this->_validator = $validator;
* Utility method to build a "WHERE" clause to locate ourselves in
* XXX future improvement: use rowids?
function _makeWhere ($keyval = null )
for ($i = 0; $i < sizeof($this->_keycolumn); $i++ ) {
$keyval[] = $this->{$this->_keycolumn[$i]};
for ($i = 0; $i < sizeof($this->_keycolumn); $i++ ) {
$whereclause .= $this->_keycolumn[$i];
// there's not much point in having a NULL key,
// but we support it anyway
$whereclause .= ' IS NULL';
$whereclause .= ' = ' . $this->_dbh->quote ($keyval[$i]);
$keyval = @$this->{$this->_keycolumn };
$whereclause = $this->_keycolumn;
// there's not much point in having a NULL key,
// but we support it anyway
$whereclause .= ' IS NULL';
$whereclause .= ' = ' . $this->_dbh ->quote ($keyval);
* Method used to initialize a DB_storage object from the
* @param $keyval mixed the key[s] of the row to fetch (string or array)
* @return int DB_OK on success, a DB error if not
$whereclause = $this->_makeWhere ($keyval);
$query = 'SELECT * FROM ' . $this->_table . ' WHERE ' . $whereclause;
$sth = $this->_dbh ->query ($query);
foreach ($row as $key => $value) {
$this->_properties [$key] = true;
* Create a new (empty) row in the configured table for this
$primarykey = $this->_keycolumn;
$primarykey = array ($this->_keycolumn );
for ($i = 0; $i < sizeof($primarykey); $i++ ) {
$pkvals[] = $this->_dbh ->quote ($newpk[$i]);
$sth = $this->_dbh ->query (" INSERT INTO $this->_table (" .
implode(",", $primarykey) . ") VALUES(" .
* Output a simple description of this DB_storage object.
* @return string object description
$info .= "(" . implode(",", $this->_keycolumn ) . ")";
$info .= $this->_keycolumn;
if (sizeof($this->_properties )) {
$info .= " [loaded, key=";
$keyname = $this->_keycolumn;
for ($i = 0; $i < sizeof($keyname); $i++ ) {
$info .= $this->$keyname[$i];
$info .= $this->$keyname;
if (sizeof($this->_changes )) {
* Dump the contents of this object to "standard output".
foreach ($this->_properties as $prop => $foo) {
* Static method used to create new DB storage objects.
* @param $data assoc. array where the keys are the names
* @return object a new instance of DB_storage or a subclass of it
function &create($table, &$data)
$obj = & new $classname($table);
foreach ($data as $name => $value) {
$obj->_properties [$name] = true;
* Loads data into this object from the given query. If this
* object already contains table data, changes will be saved and
* the object re-initialized first.
* @param $query SQL query
* @param $params parameter list in case you want to use
* @return int DB_OK on success, DB_WARNING_READ_ONLY if the
* returned object is read-only (because the object's specified
* key column was not found among the columns returned by $query),
* or another DB error code in case of errors.
// XXX commented out for now
function loadFromQuery($query, $params = null)
if (sizeof($this->_properties)) {
if (sizeof($this->_changes)) {
$this->_changes = array();
$this->_properties = array();
$rowdata = $this->_dbh->getRow($query, DB_FETCHMODE_ASSOC, $params);
if (DB::isError($rowdata)) {
$found_keycolumn = false;
while (list($key, $value) = each($rowdata)) {
if ($key == $this->_keycolumn) {
$this->_properties[$key] = true;
unset($value); // have to unset, or all properties will
// refer to the same value
return DB_WARNING_READ_ONLY;
* Modify an attriute value.
function set($property, $newvalue)
// only change if $property is known and object is not
return $this->raiseError (null , DB_WARNING_READ_ONLY , null ,
if (@isset ($this->_properties [$property])) {
if (empty ($this->_validator )) {
$this->$property = $newvalue;
if (empty ($this->_changes [$property])) {
$this->_changes [$property] = 0;
$this->_changes [$property]++;
null , " invalid field: $property" ,
null , " unknown field: $property" ,
* Fetch an attribute value.
* @param string attribute name
* @return attribute contents, or null if the attribute name is
// only return if $property is known
if (isset ($this->_properties [$property])) {
* Destructor, calls DB_storage::store() if there are changes
if (sizeof($this->_changes )) {
$this->_properties = array ();
$this->_changes = array ();
* Stores changes to this object in the database.
* @return DB_OK or a DB error
foreach ($this->_changes as $name => $foo) {
$params[] = &$this->$name;
$vars[] = $name . ' = ?';
$query = 'UPDATE ' . $this->_table . ' SET ' .
$stmt = $this->_dbh ->prepare ($query);
$res = $this->_dbh ->execute ($stmt, $params);
$this->_changes = array ();
* Remove the row represented by this object from the database.
* @return mixed DB_OK or a DB error
return $this->raiseError (null , DB_WARNING_READ_ONLY , null ,
$query = 'DELETE FROM ' . $this->_table . ' WHERE '.
$res = $this->_dbh ->query ($query);
foreach ($this->_properties as $prop => $foo) {
$this->_properties = array ();
$this->_changes = array ();
Documentation generated on Mon, 11 Mar 2019 10:14:55 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|