Source for file Db.php

Documentation is available at Db.php

  1. <?php
  2. /*
  3.  *  $Id: Db.php 2258 2007-08-17 10:49:51Z jepso $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information, see
  19.  * <http://www.phpdoctrine.com>.
  20.  */
  21.  
  22. /**
  23.  * Doctrine_Cache_Db
  24.  *
  25.  * @package     Doctrine
  26.  * @subpackage  Doctrine_Cache
  27.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  28.  * @category    Object Relational Mapping
  29.  * @link        www.phpdoctrine.com
  30.  * @since       1.0
  31.  * @version     $Revision: 2258 $
  32.  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
  33.  */
  34. class Doctrine_Cache_Db extends Doctrine_Cache_Driver implements Countable
  35. {
  36.     /**
  37.      * constructor
  38.      *
  39.      * @param array $_options      an array of options
  40.      */
  41.     public function __construct($options
  42.     {
  43.         if isset($options['connection']|| 
  44.              ($options['connection'instanceof Doctrine_Connection)) {
  45.  
  46.             throw new Doctrine_Cache_Exception('Connection option not set.');
  47.         }
  48.         
  49.         if isset($options['tableName']||
  50.              is_string($options['tableName'])) {
  51.              
  52.              throw new Doctrine_Cache_Exception('Table name option not set.');
  53.         }
  54.         
  55.  
  56.         $this->_options = $options;
  57.     }
  58.     /**
  59.      * getConnection
  60.      * returns the connection object associated with this cache driver
  61.      *
  62.      * @return Doctrine_Connection      connection object
  63.      */
  64.     public function getConnection(
  65.     {
  66.         return $this->_options['connection'];
  67.     }
  68.     /**
  69.      * Test if a cache is available for the given id and (if yes) return it (false else)
  70.      *
  71.      * Note : return value is always "string" (unserialization is done by the core not by the backend)
  72.      *
  73.      * @param string $id cache id
  74.      * @param boolean $testCacheValidity        if set to false, the cache validity won't be tested
  75.      * @return string cached datas (or false)
  76.      */
  77.     public function fetch($id$testCacheValidity true)
  78.     {
  79.         $sql 'SELECT data, expire FROM ' $this->_options['tableName']
  80.              . ' WHERE id = ?';
  81.  
  82.         if ($testCacheValidity{
  83.             $sql .= ' AND (expire=0 OR expire > ' time(')';
  84.         }
  85.  
  86.         $result $this->getConnection()->fetchAssoc($sqlarray($id));
  87.         
  88.         if isset($result[0])) {
  89.             return false;
  90.         }
  91.         
  92.         return unserialize($result[0]['data']);
  93.     }
  94.     /**
  95.      * Test if a cache is available or not (for the given id)
  96.      *
  97.      * @param string $id cache id
  98.      * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
  99.      */
  100.     public function contains($id
  101.     {
  102.         $sql 'SELECT expire FROM ' $this->_options['tableName']
  103.              . ' WHERE id = ? AND (expire=0 OR expire > ' time(')';
  104.  
  105.         return $this->getConnection()->fetchOne($sqlarray($id));
  106.     }
  107.     /**
  108.      * Save some string datas into a cache record
  109.      *
  110.      * Note : $data is always saved as a string
  111.      *
  112.      * @param string $data      data to cache
  113.      * @param string $id        cache id
  114.      * @param int $lifeTime     if != false, set a specific lifetime for this cache record (null => infinite lifeTime)
  115.      * @return boolean true if no problem
  116.      */
  117.     public function save($data$id$lifeTime false)
  118.     {
  119.         $sql 'INSERT INTO ' $this->_options['tableName']
  120.              . ' (id, data, expire) VALUES (?, ?, ?)';
  121.         
  122.         if ($lifeTime{
  123.             $expire time($lifeTime;
  124.         else {
  125.             $expire 0;
  126.         }
  127.         
  128.         $params array($idserialize($data)$expire);
  129.  
  130.         return (bool) $this->getConnection()->exec($sql$params);
  131.     }
  132.     /**
  133.      * Remove a cache record
  134.      * 
  135.      * @param string $id cache id
  136.      * @return boolean true if no problem
  137.      */
  138.     public function delete($id
  139.     {
  140.         $sql 'DELETE FROM ' $this->_options['tableName'' WHERE id = ?';
  141.  
  142.         return (bool) $this->getConnection()->exec($sqlarray($id));
  143.     }
  144.     
  145.     /**
  146.      * Removes all cache records
  147.      *
  148.      * $return bool true on success, false on failure
  149.      */
  150.     public function deleteAll()
  151.     {
  152.         $sql 'DELETE FROM ' $this->_options['tableName'];
  153.         
  154.         return (bool) $this->getConnection()->exec($sql);
  155.     }
  156.     
  157.     /**
  158.      * count
  159.      * returns the number of cached elements
  160.      *
  161.      * @return integer 
  162.      */
  163.     public function count()
  164.     {
  165.         $sql 'SELECT COUNT(*) FROM ' $this->_options['tableName'];
  166.         
  167.         return (int) $this->getConnection()->fetchOne($sql);
  168.     }
  169.     
  170.     /**
  171.      * Creates the cache table.
  172.      */
  173.     public function createTable()
  174.     {
  175.         $name $this->_options['tableName'];
  176.         
  177.         $fields array(
  178.             'id' => array(
  179.                 'type'   => 'string',
  180.                 'length' => 255
  181.             ),
  182.             'data' => array(
  183.                 'type'    => 'blob'
  184.             ),
  185.             'expire' => array(
  186.                 'type'    => 'timestamp'
  187.             )
  188.         );
  189.         
  190.         $options array(
  191.             'primary' => array('id')
  192.         );
  193.         
  194.         $this->getConnection()->export->createTable($name$fields$options);
  195.     }
  196. }