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

Source for file DBA.php

Documentation is available at DBA.php

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

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