home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Magazine / HomeAutomation / Apache / lib / php / DB / storage.php < prev   
Encoding:
PHP Script  |  2000-07-11  |  5.9 KB  |  184 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group                   |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@fast.no>                                   |
  17. // |                                                                      |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // DB_storage: a class that lets you return SQL data as objects that
  21. // can be manipulated and that updates the database accordingly.
  22. //
  23.  
  24. use "DB";
  25.  
  26. function DB_storage_destructor() {
  27.     global $DB_storage_object_list;
  28.  
  29.     if (is_array($DB_storage_object_list)) {
  30.     reset($DB_storage_object_list);
  31.     while (list($ind, $obj) = each($DB_storage_object_list)) {
  32.         $obj->destroy();
  33.     }
  34.     }
  35. }
  36.  
  37. class DB_storage {
  38.     /** the name of the table (or view, if the backend database supports
  39.         updates in views) we hold data from */
  40.     var $_table = null;
  41.     /** which column in the table contains primary keys */
  42.     var $_keycolumn = null;
  43.     /** DB connection handle used for all transactions */
  44.     var $_dbh = null;
  45.     /** an assoc with the names of database fields stored as properties
  46.     in this object */
  47.     var $_properties = array();
  48.     /** an assoc with the names of the properties in this object that
  49.     have been changed since they were fetched from the database */
  50.     var $_changes = array();
  51.     /** flag that decides if data in this object can be changed.
  52.     objects that don't have their table's key column in their
  53.     property lists will be flagged as read-only. */
  54.     var $_readonly = false;
  55.  
  56.     /**
  57.      * Constructor, adds itself to the DB_storage class's list of
  58.      * objects that should have their "destroy" method called when
  59.      * PHP shuts down (poor man's destructors).
  60.      */
  61.     function DB_storage($table, $keycolumn, &$dbh) {
  62.     global $DB_storage_object_list;
  63.     if (!is_array($DB_storage_object_list)) {
  64.         $DB_storage_object_list = array(&$this);
  65.     }
  66.     $DB_storage_object_list[] = &$this;
  67.     $this->_table = $table;
  68.     $this->_keycolumn = $keycolumn;
  69.     $this->_dbh = $dbh;
  70.     $this->_readonly = false;
  71.     }
  72.  
  73.     /**
  74.      * Static method used to create new DB storage objects.
  75.      * @param $data assoc. array where the keys are the names
  76.      *              of properties/columns
  77.      * @return object a new instance of DB_storage (or inheriting class)
  78.      */
  79.     function &create($table, &$data) {
  80.     $classname = get_class(&$this);
  81.     $obj = new $classname($table);
  82.     reset($data);
  83.     while (list($name, $value) = each($data)) {
  84.         $obj->_properties[$name] = true;
  85.         $obj->$name = &$value;
  86.     }
  87.     return $obj;
  88.     }
  89.  
  90.     /**
  91.      * Loads data into this object from the given query.  If this
  92.      * object already contains table data, changes will be saved and
  93.      * the object re-initialized first.
  94.      *
  95.      * @param $query SQL query
  96.      *
  97.      * @param $params parameter list in case you want to use
  98.      * prepare/execute mode
  99.      *
  100.      * @return int DB_OK on success, DB_WARNING_READ_ONLY if the
  101.      * returned object is read-only (because the object's specified
  102.      * key column was not found among the columns returned by $query),
  103.      * or another DB error code in case of errors.
  104.      */
  105.     function loadFromQuery($query, $params = false) {
  106.     if (sizeof($this->_properties)) {
  107.         if (sizeof($this->_changes)) {
  108.         $this->store();
  109.         $this->_changes = array();
  110.         }
  111.         $this->_properties = array();
  112.     }
  113.     $rowdata = $this->_dbh->getRow($query, DB_GETMODE_ASSOC, $params);
  114.     if (DB::isError($rowdata)) {
  115.         return $rowdata;
  116.     }
  117.     reset($rowdata);
  118.     $found_keycolumn = false;
  119.     while (list($key, $value) = each($rowdata)) {
  120.         if ($key == $this->_keycolumn) {
  121.         $found_keycolumn = true;
  122.         }
  123.         $this->_properties[$key] = true;
  124.         $this->$key = &$value;
  125.         unset($value); // have to unset, or all properties will
  126.                    // refer to the same value
  127.     }
  128.     if (!$found_keycolumn) {
  129.         $this->_readonly = true;
  130.         return DB_WARNING_READ_ONLY;
  131.     }
  132.     return DB_OK;
  133.     }
  134.  
  135.     function set($property, &$newvalue) {
  136.     // only change if $property is known and object is not
  137.     // read-only
  138.     if (!$this->_readonly && isset($this->_properties[$property])) {
  139.         $this->$property = $newvalue;
  140.         $this->_changes[$property]++;
  141.         return true;
  142.     }
  143.     return false;
  144.     }
  145.  
  146.     function &get($property) {
  147.     // only return if $property is known
  148.     if (isset($this->_properties[$property])) {
  149.         return $this->$property;
  150.     }
  151.     return null;
  152.     }
  153.  
  154.     function destroy($discard = false) {
  155.     if (!$discard && sizeof($this->_changes)) {
  156.         $this->store();
  157.     }
  158.     $this->_properties = array();
  159.     $this->_changes = array();
  160.     $this->_table = null;
  161.     }
  162.  
  163.     function store() {
  164.     while (list($name, $changed) = each($this->_changes)) {
  165.         $params[] = &$this->$name;
  166.         $vars[] = $name . ' = ?';
  167.     }
  168.     if ($vars) {
  169.         $query = 'UPDATE ' . $this->_table . ' SET ' .
  170.         implode(', ', $vars) . ' WHERE id = ?';
  171.         $params[] = $this->id;
  172.         $stmt = $this->_dbh->prepare($query);
  173.         $res = $this->_dbh->execute($stmt, &$params);
  174.         if (DB::isError($res)) {
  175.         return $res;
  176.         }
  177.         $this->_changes = array();
  178.     }
  179.     return DB_OK;
  180.     }
  181. }
  182.  
  183. ?>
  184.