Auth_PrefManager2
[ class tree: Auth_PrefManager2 ] [ index: Auth_PrefManager2 ] [ all elements ]

Source for file DB.php

Documentation is available at DB.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
  3. // +----------------------------------------------------------------------+
  4. // | Copyright (c) 2004 Jon Wood                                          |
  5. // +----------------------------------------------------------------------+
  6. // | This source file is subject to version 2.02 of the PHP license,      |
  7. // | that is bundled with this package in the file LICENSE, and is        |
  8. // | available at through the world-wide-web at                           |
  9. // | http://www.php.net/license/2_02.txt.                                 |
  10. // | If you did not receive a copy of the PHP license and are unable to   |
  11. // | obtain it through the world-wide-web, please send a note to          |
  12. // | license@php.net so we can mail you a copy immediately.               |
  13. // +----------------------------------------------------------------------+
  14. // | Authors: Jon Wood <jon@jellybob.co.uk>                               |
  15. // +----------------------------------------------------------------------+
  16. //
  17. // $Id: DB.php,v 1.3 2004/07/07 16:05:30 jellybob Exp $
  18.  
  19. /**
  20.  * Require the core class.
  21.  */
  22. require_once('Auth/PrefManager2/Container.php');
  23.  
  24. /**
  25.  * Require PEAR DB for data management.
  26.  */
  27. require_once('DB.php');
  28.  
  29. /**
  30.  * The PEAR DB container for Auth_PrefManager2
  31.  *
  32.  * @author Jon Wood <jon@jellybob.co.uk>
  33.  * @package Auth_PrefManager2
  34.  * @category Authentication
  35.  */
  36. {
  37.     /**
  38.      * The DB object being used for data access.
  39.      * 
  40.      * @access private
  41.      * @var DB 
  42.      */
  43.     var $_db = null;
  44.        
  45.     function Auth_PrefManager2_Container_DB($options = array())
  46.     {
  47.         $this->Auth_PrefManager2_Container($options);
  48.         return $this->_connect();
  49.     }
  50.     
  51.     /**
  52.      * Sets a value with the container.
  53.      *
  54.      * @param string $owner The owner to set the preference for.
  55.      * @param string $preference The name of the preference to set.
  56.      * @param string $application The application to set for.
  57.      * @return bool Success/failure
  58.      * @access protected
  59.      * @abstract
  60.      */
  61.     function _set($owner$preference$value$application)
  62.     {
  63.         if ($this->_exists($owner$preference$application)) {
  64.             // If the preference already exists update its value.
  65.             $query sprintf('UPDATE %s SET %s=%s WHERE %s=%s AND %s=%s AND %s=%s',
  66.                              $this->_options['table'],
  67.                              $this->_options['value_column'],
  68.                              $this->_db->quoteSmart($value),
  69.                              $this->_options['owner_column'],
  70.                              $this->_db->quoteSmart($owner),
  71.                              $this->_options['preference_column'],
  72.                              $this->_db->quoteSmart($preference),
  73.                              $this->_options['application_column'],
  74.                              $this->_db->quoteSmart($application));
  75.         else {
  76.             // Otherwise insert a new row.
  77.             $query sprintf('INSERT INTO %s (%s, %s, %s, %s) VALUES(%s, %s, %s, %s)',
  78.                              $this->_options['table'],
  79.                              $this->_options['value_column'],
  80.                              $this->_options['owner_column'],
  81.                              $this->_options['preference_column'],
  82.                              $this->_options['application_column'],
  83.                              $this->_db->quoteSmart($value),
  84.                              $this->_db->quoteSmart($owner),
  85.                              $this->_db->quoteSmart($preference),
  86.                              $this->_db->quoteSmart($application));
  87.         }
  88.         
  89.         $result $this->_runQuery($query);
  90.         
  91.         if (!is_null($result)) {
  92.             return true;
  93.         }
  94.         
  95.         return false;
  96.     }
  97.     
  98.     /**
  99.      * Gets a value from the container.
  100.      *
  101.      * @param string $owner The owner to set the preference for.
  102.      * @param string $preference The name of the preference to set.
  103.      * @param mixed $value The value to set the preference to.
  104.      * @param string $application The application to set for.
  105.      * @return mixed|nullThe value, or null if none is set.
  106.      * @access protected
  107.      * @abstract
  108.      */
  109.     function _get($owner$preference$application)
  110.     {
  111.         if ($this->_exists($owner$preference$application)) {
  112.             // If the preference already exists update its value.
  113.             $query sprintf('SELECT %s FROM %s WHERE %s=%s AND %s=%s AND %s=%s',
  114.                              $this->_options['value_column'],
  115.                              $this->_options['table'],
  116.                              $this->_options['owner_column'],
  117.                              $this->_db->quoteSmart($owner),
  118.                              $this->_options['preference_column'],
  119.                              $this->_db->quoteSmart($preference),
  120.                              $this->_options['application_column'],
  121.                              $this->_db->quoteSmart($application));
  122.         else {
  123.             return null;
  124.         }
  125.         
  126.         $result $this->_runQuery($query);
  127.         
  128.         if (!is_null($result)) {
  129.             $row $result->fetchRow();
  130.             return $row[0];
  131.         }
  132.         
  133.         return null;
  134.     }
  135.     
  136.     /**
  137.      * Deletes a value from the container.
  138.      *
  139.      * @param string $owner The owner to delete the preference for.
  140.      * @param string $preference The name of the preference to delete.
  141.      * @param string $application The application to delete from.
  142.      * @return bool Success/failure
  143.      * @access protected
  144.      * @abstract
  145.      */
  146.     function _delete($owner$preference$application)
  147.     {
  148.         if ($this->_exists($owner$preference$application)) {
  149.             // If the preference already exists update its value.
  150.             $query sprintf('DELETE FROM %s WHERE %s=%s AND %s=%s AND %s=%s',
  151.                              $this->_options['table'],
  152.                              $this->_options['owner_column'],
  153.                              $this->_db->quoteSmart($owner),
  154.                              $this->_options['preference_column'],
  155.                              $this->_db->quoteSmart($preference),
  156.                              $this->_options['application_column'],
  157.                              $this->_db->quoteSmart($application));
  158.         else {
  159.             // Should this be returning true, since the value is no longer
  160.             // there, or false, because no delete has been done?
  161.             return true;
  162.         }
  163.         
  164.         $result $this->_runQuery($query);
  165.         
  166.         if (!is_null($result)) {
  167.             return true;
  168.         }
  169.         
  170.         return false;
  171.     }
  172.     
  173.     /**
  174.      * Checks if the specified preference exists in the data container.
  175.      *
  176.      * Returns null if an error occurs.
  177.      *
  178.      * @param string $owner The owner to delete the preference for.
  179.      * @param string $preference The name of the preference to delete.
  180.      * @param string $application The application to delete from.
  181.      * @return bool Does the pref exist?
  182.      * @access protected
  183.      * @abstract
  184.      */
  185.     function _exists($owner$preference$application)
  186.     {
  187.         $query sprintf('SELECT COUNT(%s) FROM %s WHERE %s=%s AND %s=%s AND %s=%s',
  188.                          $this->_options['owner_column'],
  189.                          $this->_options['table'],
  190.                          $this->_options['owner_column'],
  191.                          $this->_db->quoteSmart($owner),
  192.                          $this->_options['preference_column'],
  193.                          $this->_db->quoteSmart($preference),
  194.                          $this->_options['application_column'],
  195.                          $this->_db->quoteSmart($application));
  196.                          echo $query "<br />";
  197.         
  198.         $result $this->_runQuery($query);
  199.         if (!is_null($result)) {
  200.             $count $result->fetchRow();
  201.             return (bool)$count[0];
  202.         }
  203.         
  204.         return null;
  205.     }
  206.     
  207.     /**
  208.      * Connects to the DSN provided in the options array.
  209.      *
  210.      * @return bool Success/failure
  211.      * @throws AUTH_PREFMANAGER2_DB_CONNECTION_FAILED
  212.      * @access private
  213.      */
  214.     function _connect()
  215.     {
  216.         $db =DB::connect($this->_options['dsn']);
  217.         
  218.         if (PEAR::isError($db)) {
  219.             $this->_throwError(AUTH_PREFMANAGER2_DB_CONNECT_FAILED'error'array('dsn' => $this->_options['dsn'])$db);
  220.             return false;
  221.         }
  222.         
  223.         $this->_db $db;
  224.         return true;
  225.     }
  226.     
  227.     /**
  228.      * Runs a query on the database.
  229.      *
  230.      * Returns null on error.
  231.      *
  232.      * @param string $query The query to run.
  233.      * @return DB_Result|nullThe result object for the query.
  234.      * @throws AUTH_PREFMANAGER2_DB_QUERY_FAILED
  235.      * @access private
  236.      * @todo Improve the connection handling here.
  237.      */
  238.     function &_runQuery($query)
  239.     {
  240.         if (is_null($this->_db)) {
  241.             $this->_connect();
  242.         }
  243.         
  244.         if (!is_null($this->_db)) {
  245.             $result $this->_db->query($query);
  246.             if (DB::isError($result)) {
  247.                 $this->_throwError(AUTH_PREFMANAGER2_DB_QUERY_FAILED'error'array('query' => $query)$result);
  248.                 return null;
  249.             }
  250.             
  251.             return $result;
  252.         }
  253.         
  254.         return null;
  255.     }
  256.     
  257.     /**
  258.      * Reads the options array, and sets default values for anything
  259.      * which isn't set.
  260.      *
  261.      * @param array $options An array of options.
  262.      * @return void 
  263.      * @access protected
  264.      */
  265.     function _parseOptions($options)
  266.     {
  267.         if (!isset($options['table'])) {
  268.             $options['table''preferences';
  269.         }
  270.         
  271.         if (!isset($options['owner_column'])) {
  272.             $options['owner_column''owner';
  273.         }
  274.         
  275.         if (!isset($options['application_column'])) {
  276.             $options['application_column''application';
  277.         }
  278.         
  279.         if (!isset($options['preference_column'])) {
  280.             $options['preference_column''name';
  281.         }
  282.         
  283.         if (!isset($options['value_column'])) {
  284.             $options['value_column''value';
  285.         }
  286.         
  287.         if (!isset($options['dsn'])) {
  288.             $this->_throwError(AUTH_PREFMANAGER2_DB_NO_DSN);
  289.         }
  290.         
  291.         parent::_parseOptions($options);
  292.     }
  293. }
  294. ?>

Documentation generated on Mon, 11 Mar 2019 13:54:06 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.