home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / DBA.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  8.9 KB  |  237 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | Copyright (c) 2002 Brent Cook                                        |
  5. // +----------------------------------------------------------------------+
  6. // | This library is free software; you can redistribute it and/or        |
  7. // | modify it under the terms of the GNU Lesser General Public           |
  8. // | License as published by the Free Software Foundation; either         |
  9. // | version 2.1 of the License, or (at your option) any later version.   |
  10. // |                                                                      |
  11. // | This library is distributed in the hope that it will be useful,      |
  12. // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  13. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
  14. // | Lesser General Public License for more details.                      |
  15. // |                                                                      |
  16. // | You should have received a copy of the GNU Lesser General Public     |
  17. // | License along with this library; if not, write to the Free Software  |
  18. // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA|
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: DBA.php,v 1.30 2002/12/18 02:27:28 busterb Exp $
  22. //
  23.  
  24. require_once('PEAR.php');
  25.  
  26. /**
  27.  * If you add an error code here, make sure you also add a textual
  28.  * version of it in DBA::errorMessage().
  29.  */
  30.                                                 // userinfo contains:
  31. define('DBA_ERROR',                    -1);
  32. define('DBA_ERROR_UNSUP_DRIVER',       -2);     // drivername
  33. define('DBA_ERROR_UNSUP_PERSISTENCE',  -3);
  34. define('DBA_ERROR_NO_DRIVER',          -4);     // drivername
  35. define('DBA_ERROR_NO_DBNAME',          -5);
  36. define('DBA_ERROR_NOSUCHDB',           -6);     // dbname
  37. define('DBA_ERROR_INVALID_MODE',       -7);     // filemode
  38. define('DBA_ERROR_CANNOT_CREATE',      -8);     // dbname
  39. define('DBA_ERROR_CANNOT_OPEN',        -9);
  40. define('DBA_ERROR_CANNOT_DROP',       -10);
  41. define('DBA_ERROR_NOT_READABLE',      -11);
  42. define('DBA_ERROR_NOT_WRITEABLE',     -12);
  43. define('DBA_ERROR_NOT_OPEN',          -13);
  44. define('DBA_ERROR_NOT_FOUND',         -14);
  45. define('DBA_ERROR_ALREADY_EXISTS',    -15);     // key
  46.  
  47. /**
  48.  * DBA is a set of classes for handling and extending Berkeley DB style
  49.  * databases. It works around some of the quirks in the built-in dba
  50.  * functions in PHP (e.g. gdbm does not support dba_replace), has a file-based
  51.  * dbm engine for installations where dba support is not included in PHP.
  52.  *
  53.  * @author  Brent Cook <busterb@mail.utexas.edu>
  54.  * @version 1.0
  55.  * @access  public
  56.  * @package DBA
  57.  */
  58. class DBA extends PEAR
  59. {
  60.     /**
  61.      * Default constructor
  62.      */
  63.     function DBA()
  64.     {
  65.         // call the base constructor
  66.         $this->PEAR();
  67.     }
  68.  
  69.     /**
  70.      * Creates a new DBA object
  71.      *
  72.      * @static
  73.      * @param   string $driver type of storage object to return
  74.      * @return  object DBA storage object, returned by reference
  75.      */
  76.     function &create($driver = 'file')
  77.     {
  78.         if (!function_exists('dba_open') || ($driver=='file')) {
  79.             require_once 'DBA/Driver/File.php';
  80.             return new DBA_Driver_File();
  81.         } elseif (in_array($driver, DBA::getDriverList())) {
  82.             require_once 'DBA/Driver/Builtin.php';
  83.             return new DBA_Driver_Builtin($driver);
  84.         } else {
  85.             return DBA::raiseError(DBA_ERROR_UNSUP_DRIVER, NULL, NULL,
  86.                 'driver: '.$driver);
  87.         }
  88.     }
  89.  
  90.     /**
  91.      * Returns whether a result code from a DBA method is an error
  92.      *
  93.      * @param   int       $value  result code
  94.      * @return  boolean   whether $value is a DBA_Error
  95.      * @access public
  96.      */
  97.     function isError($value)
  98.     {
  99.         return (is_object($value) &&
  100.             (get_class($value) == 'dba_error' || is_subclass_of($value, 'dba_error')));
  101.     }
  102.     
  103.     /**
  104.      * Return a textual error message for a DBA error code
  105.      *
  106.      * @param   int     $value error code
  107.      * @return  string  error message, or false if the error code was
  108.      *                  not recognized
  109.      * @access public
  110.      */
  111.     function errorMessage($value)
  112.     {
  113.         static $errorMessages;
  114.         if (!isset($errorMessages)) {
  115.             $errorMessages = array(
  116.                 DBA_ERROR                   => 'unknown error',
  117.                 DBA_ERROR_UNSUP_DRIVER      => 'unsupported database driver',
  118.                 DBA_ERROR_UNSUP_PERSISTENCE => 'persistent connections not supported',
  119.                 DBA_ERROR_NO_DRIVER         => 'no driver loaded',
  120.                 DBA_ERROR_NO_DBNAME         => 'no databasename given',
  121.                 DBA_ERROR_NOSUCHDB          => 'database not found',
  122.                 DBA_ERROR_INVALID_MODE      => 'invalid file mode',
  123.                 DBA_ERROR_CANNOT_CREATE     => 'can not create database',
  124.                 DBA_ERROR_CANNOT_OPEN       => 'can not open database',
  125.                 DBA_ERROR_CANNOT_DROP       => 'can not drop database',
  126.                 DBA_ERROR_NOT_READABLE      => 'database not readable',
  127.                 DBA_ERROR_NOT_WRITEABLE     => 'database not writeable',
  128.                 DBA_ERROR_NOT_OPEN          => 'database not open',
  129.                 DBA_ERROR_NOT_FOUND         => 'key not found',
  130.                 DBA_ERROR_ALREADY_EXISTS    => 'key already exists',
  131.             );
  132.         }
  133.  
  134.         if (DBA::isError($value)) {
  135.             $value = $value->getCode();
  136.         }
  137.  
  138.         return isset($errorMessages[$value]) ?
  139.             $errorMessages[$value] : $errorMessages[DBA_ERROR];
  140.     }
  141.  
  142.     /**
  143.      * This method is used to communicate an error and invoke error
  144.      * callbacks etc.  Basically a wrapper for PEAR::raiseError
  145.      * without the message string.
  146.      *
  147.      * @param mixed $code integer error code, or a PEAR error object (all
  148.      *      other parameters are ignored if this parameter is an object
  149.      * @param int $mode error mode, see PEAR_Error docs
  150.      * @param mixed $options If error mode is PEAR_ERROR_TRIGGER, this is the
  151.      *      error level (E_USER_NOTICE etc).  If error mode is
  152.      *      PEAR_ERROR_CALLBACK, this is the callback function, either as a
  153.      *      function name, or as an array of an object and method name. For
  154.      *      other error modes this parameter is ignored.
  155.      * @param string $userinfo Extra debug information.
  156.      * @return object a PEAR error object
  157.      * @access public
  158.      * @see PEAR_Error
  159.      */
  160.     function &raiseError($code = DBA_ERROR, $mode = NULL, $options = NULL,
  161.         $userinfo = NULL)
  162.     {
  163.         // The error is yet a DBA error object
  164.         if (is_object($code)) {
  165.             return PEAR::raiseError($code, NULL, NULL, NULL, NULL, NULL, TRUE);
  166.         }
  167.  
  168.         return PEAR::raiseError(NULL, $code, $mode, $options, $userinfo,
  169.             'DBA_Error', TRUE);
  170.     }
  171.     
  172.     /**
  173.      * Returns whether a database exists
  174.      *
  175.      * @param  string $name name of the database to find
  176.      * @param  string @driver driver to test for
  177.      * @return boolean true if the database exists
  178.      */
  179.     function db_exists($name, $driver = 'file')
  180.     {
  181.         if (!function_exists('dba_open') || ($driver=='file')) {
  182.             // the file driver stores data in two files
  183.             return (file_exists($name.'.dat') && file_exists($name.'.idx'));
  184.         } elseif (in_array($driver, array('db2', 'db3', 'db4', 'gdbm'))) {
  185.             // these drivers store data in one file
  186.             return file_exists($name);
  187.         } else {
  188.             // do not know how other drivers store data
  189.             return DBA::raiseError(DBA_ERROR_NO_DRIVER, NULL, NULL, 'driver: '.$driver);
  190.         }
  191.     }
  192.  
  193.     /**
  194.      * Returns an array of the currently supported drivers
  195.      *
  196.      * @return array
  197.      */
  198.     function getDriverList()
  199.     {
  200.         return array_merge(dba_handlers(), array('file'));
  201.     }
  202. }
  203.  
  204. /**
  205.  * DBA_Error implements a class for reporting portable database error
  206.  * messages. Based on the PEAR::MDB implementation.
  207.  *
  208.  * @package DBA
  209.  * @author  Olaf Conradi <conradi@cs.utwente.nl>
  210.  */
  211. class DBA_Error extends PEAR_Error
  212. {
  213.     
  214.     /**
  215.      * DBA_Error constructor.
  216.      *
  217.      * @param mixed   $code      DBA error code, or string with error message.
  218.      * @param integer $mode      what "error mode" to operate in
  219.      * @param integer $level     what error level to use for
  220.      *                           $mode & PEAR_ERROR_TRIGGER
  221.      * @param smixed  $debuginfo additional debug info, such as the last query
  222.      */
  223.     function DBA_Error($code = DBA_ERROR, $mode = PEAR_ERROR_RETURN,
  224.               $level = E_USER_NOTICE, $debuginfo = NULL)
  225.     {
  226.         if (is_int($code)) {
  227.             $this->PEAR_Error('DBA Error: '.DBA::errorMessage($code), $code,
  228.                 $mode, $level, $debuginfo);
  229.         } else {
  230.             $this->PEAR_Error('DBA Error: '.$code, DBA_ERROR, $mode, $level,
  231.                 $debuginfo);
  232.         }
  233.     }
  234. }
  235.  
  236. ?>
  237.