home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / System / SharedMemory / Sqlite.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  4.7 KB  |  190 lines

  1. <?php
  2. /**
  3. *
  4. * The SQLite driver for SharedMemory
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.0 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category   System
  15. * @package    System_Sharedmemory
  16. * @author     Evgeny Stepanischev <bolk@lixil.ru>
  17. * @copyright  2005 Evgeny Stepanischev
  18. * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  19. * @version    CVS: $Id:$
  20. * @link       http://pear.php.net/package/System_SharedMemory
  21. */
  22.  
  23. /**
  24. *
  25. * The methods PEAR SharedMemory uses to interact with PHP's SQLite extension
  26. * for interacting with SQLite shared memory
  27. *
  28. * These methods overload the ones declared System_SharedMemory_Common
  29. *
  30. * @category   System
  31. * @package    System_Sharedmemory
  32. * @package    System_Sharedmemory
  33. * @author     Evgeny Stepanischev <bolk@lixil.ru>
  34. * @copyright  2005 Evgeny Stepanischev
  35. * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  36. * @version    CVS: $Id:$
  37. * @link       http://pear.php.net/package/System_SharedMemory
  38. */
  39.  
  40. require_once 'System/SharedMemory/Common.php';
  41.  
  42. // {{{ 
  43.  
  44. class System_SharedMemory_Sqlite extends System_SharedMemory_Common
  45. {
  46.     // {{{ properties
  47.     /**
  48.     * SQLite object handler
  49.     *
  50.     * @var object
  51.     *
  52.     * @access private
  53.     */
  54.     var $_h;
  55.  
  56.     /**
  57.     * true if plugin was connected to backend
  58.     *
  59.     * @var bool
  60.     *
  61.     * @access private
  62.     */
  63.     var $_connected;
  64.  
  65.     /**
  66.     * hash of SQLite table options
  67.     *
  68.     * @var array
  69.     *
  70.     * @access private
  71.     */
  72.     var $_options;
  73.     // }}}
  74.     // {{{ constructor
  75.  
  76.     /**
  77.      * Constructor. Init all variables.
  78.      * SQLite table must be created:
  79.      * CREATE sharedmemory(var text PRIMARY KEY, value TEXT)
  80.      * It's very important!
  81.      *
  82.      * @param array $options
  83.      *
  84.      * @access public
  85.      */
  86.     function System_SharedMemory_Sqlite($options)
  87.     {
  88.         $this->_options = $this->_default($options, array
  89.         (
  90.             'db' => ':memory:',
  91.             'table'  => 'sharedmemory',
  92.             'var' => 'var',
  93.             'value' => 'value',
  94.             'persistent' => false,
  95.         ));
  96.  
  97.         $func = $this->_options['persistent'] ? 'sqlite_popen' : 'sqlite_open';
  98.  
  99.         $this->_h = $func($this->_options['db']);
  100.         $this->_connected = is_resource($this->_h);
  101.     }
  102.     // }}}
  103.     // {{{ isConnected()
  104.  
  105.     /**
  106.      * returns true if plugin was 
  107.      * successfully connected to backend
  108.      *
  109.      * @return bool true if connected
  110.      * @access public
  111.      */
  112.     function isConnected()
  113.     {
  114.         return $this->_connected;
  115.     }
  116.     // }}}
  117.     // {{{ get()
  118.  
  119.     /**
  120.      * returns value of variable in shared mem
  121.      *
  122.      * @param string $name name of variable
  123.      *
  124.      * @return mixed value of the variable
  125.      * @access public
  126.      */
  127.     function get($name)
  128.     {
  129.         $name   = sqlite_escape_string($name);
  130.         $sql = 'SELECT '.$this->_options['value'].
  131.                'FROM '.$this->_options['table'].
  132.                'WHERE '.$this->_options['var'].'=\''.$name.'\''.
  133.                'LIMIT 1';
  134.  
  135.         $result = sqlite_query($this->_h, $sql);
  136.         if (sqlite_num_rows($result)) {
  137.             return unserialize(sqlite_fetch_single($result));
  138.         }
  139.  
  140.         return null;
  141.     }
  142.     // }}}
  143.     // {{{ set()
  144.  
  145.     /**
  146.      * set value of variable in shared mem
  147.      *
  148.      * @param string $name  name of the variable
  149.      * @param string $value value of the variable
  150.      *
  151.      * @return bool true on success
  152.      * @access public
  153.      */
  154.     function set($name, $value)
  155.     {
  156.         $name  = sqlite_escape_string($name);
  157.         $value = sqlite_escape_string(serialize($value));
  158.  
  159.         $sql  = 'REPLACE INTO '.$this->_options['table'].
  160.                 ' ('.$this->_options['var'].', '.$this->_options['value'].
  161.                 'VALUES (\''.$name.'\', \''.$value.'\')';
  162.  
  163.         sqlite_query($this->_h, $sql);
  164.         return true;
  165.     }
  166.     // }}}
  167.     // {{{ rm()
  168.  
  169.     /**
  170.      * remove variable from memory
  171.      *
  172.      * @param string $name  name of the variable
  173.      *
  174.      * @return bool true on success
  175.      * @access public
  176.      */
  177.     function rm($name)
  178.     {
  179.         $name  = sqlite_escape_string($name);
  180.  
  181.         $sql  = 'DELETE FROM '.$this->_options['table'].
  182.                ' WHERE '.$this->_options['var'].'=\''.$name.'\'';
  183.  
  184.         sqlite_query($this->_h, $sql);
  185.         return true;
  186.     }
  187.     // }}}
  188. }
  189. // }}}
  190. ?>