home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Vyzkuste / phptriad / phptriad2-2-1.exe / php / pear / Cache / DB.php < prev    next >
Encoding:
PHP Script  |  2001-11-13  |  11.6 KB  |  418 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 4.0                                                      |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de>               |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: DB.php,v 1.7.2.3 2001/11/13 01:26:40 ssb Exp $
  19.  
  20. require_once 'Cache.php';
  21. require_once 'DB.php';
  22.  
  23. /**
  24. * Cache_DB
  25. *
  26. * @author       Sebastian Bergmann <sb@sebastian-bergmann.de>
  27. * @module       Cache_DB
  28. * @modulegroup  Cache_DB
  29. * @package      Cache
  30. * @version      $Revision: 1.7.2.3 $
  31. * @access       public
  32. */
  33. class Cache_DB extends Cache {
  34.     /**
  35.     * PEAR DB Object
  36.     *
  37.     * @var  object
  38.     */
  39.     var $db;
  40.  
  41.     /**
  42.     * Lifetime of a cached result set (in seconds)
  43.     *
  44.     * @var  integer
  45.     */
  46.     var $expires = 3600;
  47.  
  48.     /**
  49.     * PEAR DB DSN
  50.     *
  51.     * @var  string
  52.     */
  53.     var $dsn = '';
  54.  
  55.     /**
  56.     * PEAR DB Options
  57.     *
  58.     * @var  mixed
  59.     */
  60.     var $options = false;
  61.  
  62.     /**
  63.     * Fetchmode
  64.     *
  65.     * @var  integer
  66.     */
  67.     var $fetchmode = DB_FETCHMODE_ASSOC;
  68.  
  69.     /**
  70.     * Fetchmode Object Class
  71.     *
  72.     * @var  string
  73.     */
  74.     var $fetchmode_object_class = 'DB_row';
  75.  
  76.     /**
  77.     * Constructor
  78.     *
  79.     * @param    string  Name of container class
  80.     * @param    array   Array with container class options
  81.     * @param    integer Lifetime of a cached result set (in seconds)
  82.     */
  83.     function Cache_DB($container  = 'file',
  84.                       $container_options = array('cache_dir'       => '.',
  85.                                                  'filename_prefix' => 'query_'
  86.                                                 ),
  87.                       $expires = 3600
  88.                      )
  89.     {
  90.         $this->Cache($container, $container_options);
  91.         $this->expires = $expires;      
  92.     }
  93.  
  94.     /**
  95.     * PEAR-Deconstructor
  96.     * Call deconstructor of parent,
  97.     * close database connection if open
  98.     */
  99.     function _Cache_DB() {
  100.         $this->_Cache();
  101.  
  102.         if (is_object($this->db)) {
  103.             $this->db->disconnect();
  104.         }
  105.     }
  106.  
  107.     /**
  108.     * Connect to a database.
  109.     *
  110.     * @param  string  PEAR DB DSN for the database connection
  111.     * @param  mixed   options
  112.     * @throws object  DB_Error
  113.     */
  114.     function connect($dsn, $options = false) {
  115.         if (!isset($this->db)) {
  116.             $this->db = DB::connect($dsn, $options);
  117.         
  118.             if (DB::isError($this->db)) {
  119.                 return $this->db;
  120.             }
  121.         }
  122.     }
  123.  
  124.     /**
  125.     * Register a database connection for connect on demand.
  126.     *
  127.     * @param  string  PEAR DB DSN for the database connection
  128.     * @param  mixed   options
  129.     */
  130.     function setConnection($dsn, $options = false) {
  131.       $this->dsn     = $dsn;
  132.       $this->options = $options;
  133.     }
  134.  
  135.     /**
  136.     * Sets which fetch mode should be used by default on queries
  137.     * on this connection.
  138.     *
  139.     * @param integer  DB_FETCHMODE_ASSOC, DB_FETCHMODE_OBJECT or
  140.     *                 DB_FETCHMODE_ORDERED
  141.     *
  142.     * @param string   The class of the object to be returned by
  143.     *                 the fetch methods when the DB_FETCHMODE_OBJECT
  144.     *                 mode is selected.
  145.     */
  146.     function setFetchMode($fetchmode, $object_class = null) {
  147.         switch ($fetchmode) {
  148.             case DB_FETCHMODE_OBJECT: {
  149.                 if ($object_class) {
  150.                     $this->fetchmode_object_class = $object_class;
  151.                 }
  152.             }
  153.  
  154.             case DB_FETCHMODE_ORDERED:
  155.             case DB_FETCHMODE_ASSOC: {
  156.                 $this->fetchmode = $fetchmode;
  157.             }
  158.             break;
  159.         }
  160.     }
  161.  
  162.     /**
  163.     * Perform an SQL query.
  164.     *
  165.     * @param  string  SQL Query String
  166.     * @return object  Cache_DB_Result
  167.     * @throws object  Cache_Error
  168.     */
  169.     function &query($query) {
  170.         if (stristr($query, 'SELECT')) {
  171.             $cache_id = md5($query);
  172.  
  173.             $result = $this->get($cache_id, 'db_cache');
  174.  
  175.             if ($result == NULL) {
  176.                 if (!isset($this->db)) {
  177.                     if (!empty($this->dsn)) {
  178.                         $this->connect($this->dsn, $this->options);
  179.                     } else {
  180.                         return new Cache_Error('No database connection. Either open a connection
  181.                                                using connect() or register a connection with
  182.                                                setConnection($dsn, $options)',
  183.                                                __FILE__,
  184.                                                __LINE__
  185.                                               );
  186.                     }
  187.                 }
  188.  
  189.                 $_result = $this->db->query($query);
  190.  
  191.                 if (!DB::isError($_result)) {
  192.                     $rows = array();
  193.  
  194.                     while ($row = $_result->fetchRow(DB_FETCHMODE_ASSOC)) {
  195.                         $rows[] = $row;
  196.                     }
  197.  
  198.                     $result = new Cache_DB_Result($rows, $this->fetchmode, $this->fetchmode_object_class);
  199.  
  200.                     $this->save($cache_id, $result, $this->expires, 'db_cache');
  201.                 } else {
  202.                     return $_result;
  203.                 }
  204.             }
  205.         } else {
  206.             if (!isset($this->db)) {
  207.                 if (!empty($this->dsn)) {
  208.                     $this->connect($this->dsn, $this->options);
  209.                     $result = $this->db->query($query);
  210.                 } else {
  211.                     return new Cache_Error('No database connection. Either open a connection
  212.                                            using connect() or register a connection with
  213.                                            setConnection($dsn, $options)',
  214.                                            __FILE__,
  215.                                            __LINE__
  216.                                           );
  217.                 }
  218.             }
  219.         }
  220.  
  221.         return $result;
  222.     }
  223. }
  224.  
  225. /**
  226. * Cache_DB_Result
  227. *
  228. * @author       Sebastian Bergmann <sb@sebastian-bergmann.de>
  229. * @module       Cache_DB
  230. * @modulegroup  Cache_DB
  231. * @package      Cache
  232. * @version      $Revision: 1.7.2.3 $
  233. * @access       public
  234. */
  235. class Cache_DB_Result {
  236.     /**
  237.     * Names of the result set's columns
  238.     *
  239.     * @var  array
  240.     */
  241.     var $column_names = array();
  242.  
  243.     /**
  244.     * Cursor
  245.     *
  246.     * @var  integer
  247.     */
  248.     var $cursor = 0;
  249.  
  250.     /**
  251.     * Fetchmode
  252.     *
  253.     * @var  integer
  254.     */
  255.     var $fetchmode = DB_FETCHMODE_ASSOC;
  256.  
  257.     /**
  258.     * Fetchmode Object Class
  259.     *
  260.     * @var  string
  261.     */
  262.     var $fetchmode_object_class = 'DB_row';
  263.  
  264.     /**
  265.     * Number of columns in the result set
  266.     *
  267.     * @var  integer
  268.     */
  269.     var $num_columns = 0;
  270.  
  271.     /**
  272.     * Number of rows in the result set
  273.     *
  274.     * @var  integer
  275.     */
  276.     var $num_rows = 0;
  277.  
  278.     /**
  279.     * Rows of the result set
  280.     *
  281.     * @var  array
  282.     */
  283.     var $rows = array();
  284.  
  285.     /**
  286.     * Constructor
  287.     *
  288.     * @param  array   rows
  289.     * @param  integer fetchmode
  290.     * @param  string  fetchmode_object_class
  291.     */
  292.     function Cache_DB_Result(&$rows, $fetchmode, $fetchmode_object_class) {
  293.       $this->rows                   = $rows;
  294.       $this->fetchmode              = $fetchmode;
  295.       $this->fetchmode_object_class = $fetchmode_object_class;
  296.  
  297.       $this->column_names = array_keys($this->rows[0]);
  298.       $this->cursor       = 0;
  299.       $this->num_columns  = sizeof($this->column_names);
  300.       $this->num_rows     = sizeof($this->rows);
  301.     }
  302.  
  303.     /**
  304.      * Fetch and return a row of data.
  305.      * @param   integer format of fetched row
  306.      * @param   mixed   row to fetch
  307.      * @return  mixed   a row of data, NULL on no more rows
  308.      * @throws  object  DB_Error
  309.      */
  310.     function fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null) {
  311.         if ($fetchmode === DB_FETCHMODE_DEFAULT) {
  312.             $fetchmode = $this->fetchmode;
  313.         }
  314.  
  315.         if ($fetchmode === DB_FETCHMODE_OBJECT) {
  316.             $fetchmode = DB_FETCHMODE_ASSOC;
  317.             $return_object = true;
  318.         }
  319.   
  320.         if ($rownum === null) {
  321.             $rownum = $this->cursor++;
  322.         }
  323.  
  324.         if ($rownum < sizeof($this->rows)) {
  325.             $row = $this->rows[$rownum];
  326.         } else {
  327.             return false;
  328.         }
  329.  
  330.         switch ($fetchmode) {
  331.             case DB_FETCHMODE_ASSOC: {
  332.                 if (isset($return_object)) {
  333.                     $class  =  $this->fetchmode_object_class;
  334.                     $object =& new $class($row);
  335.  
  336.                     return $object;
  337.                 } else {
  338.                     return $row;
  339.                 }
  340.             }
  341.             break;
  342.  
  343.             case DB_FETCHMODE_ORDERED: {
  344.                 $_row = array();
  345.  
  346.                 foreach ($this->column_names as $column_name) {
  347.                     $_row[] = $row[$column_name];
  348.                 }
  349.  
  350.                 return $_row;
  351.             }
  352.             break;
  353.  
  354.             default: {
  355.                return false;
  356.             }
  357.         }
  358.     }
  359.  
  360.     /**
  361.      * Fetch a row of data into an existing variable.
  362.      *
  363.      * @param   array   reference to data containing the row
  364.      * @param   integer format of fetched row
  365.      * @param   integer row number to fetch
  366.      * @return  mixed   DB_OK on success, NULL on no more rows
  367.      */
  368.     function fetchInto(&$row, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null) {
  369.         if ($row = $this->fetchRow($fetchmode, $rownum)) {
  370.             return DB_OK;
  371.         } else {
  372.             return NULL;
  373.         }
  374.     }
  375.  
  376.     /**
  377.      * Get the the number of columns in a result set.
  378.      *
  379.      * @return integer  number of columns
  380.      */
  381.     function numCols() {
  382.         return $this->num_columns;
  383.     }
  384.  
  385.     /**
  386.      * Get the number of rows in a result set.
  387.      *
  388.      * @return integer  number of rows
  389.      */
  390.     function numRows() {
  391.         return $this->num_rows;
  392.     }
  393.  
  394.     /**
  395.      * Frees the resources allocated for this result set.
  396.      */
  397.     function free() {
  398.         $this->column_names = array();
  399.         $this->rows         = array();
  400.         $this->num_columns  = 0;
  401.         $this->num_rows     = 0;
  402.     }
  403.  
  404.     /**
  405.      * tableInfo() is not implemented in the PEAR Cache DB module.
  406.      * @param   mixed   $mode
  407.      * @throws  object  Cache_Error
  408.      */
  409.     function tableInfo($mode = null) {
  410.         return new Cache_Error('tableInfo() is not implemented in the
  411.                                PEAR Cache DB module.',
  412.                                __FILE__,
  413.                                __LINE__
  414.                               );
  415.     }
  416. }
  417. ?>
  418.