cvs: pear /DBA DBA.php package.xml /DBA/DBA/Driver Builtin.php File.php
| From: | Brent Cook | Date: | Fri, 01 Nov 2002 03:44:14 +0000 |
| Subject: | cvs: pear /DBA DBA.php package.xml /DBA/DBA/Driver Builtin.php File.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-7255@lists.php.net to get a copy of this message | ||
busterb Thu Oct 31 22:44:14 2002 EDT
Modified files:
/pear/DBA DBA.php package.xml
/pear/DBA/DBA/Driver Builtin.php File.php
Log:
Now emit error codes as well as error messages
Index: pear/DBA/DBA.php diff -u pear/DBA/DBA.php:1.26 pear/DBA/DBA.php:1.27 --- pear/DBA/DBA.php:1.26 Sun Sep 29 23:25:10 2002 +++ pear/DBA/DBA.php Thu Oct 31 22:44:13 2002 @@ -18,12 +18,33 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: DBA.php,v 1.26 2002/09/30 03:25:10 busterb Exp $ +// $Id: DBA.php,v 1.27 2002/11/01 03:44:13 busterb Exp $ // require_once('PEAR.php'); /** + * If you add an error code here, make sure you also add a textual + * version of it in DBA::errorMessage(). + */ + // userinfo contains: +define('DBA_ERROR', -1); +define('DBA_ERROR_UNSUP_DRIVER', -2); // drivername +define('DBA_ERROR_UNSUP_PERSISTENCE', -3); +define('DBA_ERROR_NO_DRIVER', -4); // drivername +define('DBA_ERROR_NO_DBNAME', -5); +define('DBA_ERROR_NOSUCHDB', -6); // dbname +define('DBA_ERROR_INVALID_MODE', -7); // filemode +define('DBA_ERROR_CANNOT_CREATE', -8); // dbname +define('DBA_ERROR_CANNOT_OPEN', -9); +define('DBA_ERROR_CANNOT_DROP', -10); +define('DBA_ERROR_NOT_READABLE', -11); +define('DBA_ERROR_NOT_WRITEABLE', -12); +define('DBA_ERROR_NOT_OPEN', -13); +define('DBA_ERROR_NOT_FOUND', -14); +define('DBA_ERROR_ALREADY_EXISTS', -15); // key + +/** * DBA is a set of classes for handling and extending Berkeley DB style * databases. It works around some of the quirks in the built-in dba * functions in PHP (e.g. gdbm does not support dba_replace), has a file-based @@ -61,14 +82,93 @@ require_once 'DBA/Driver/Builtin.php'; return new DBA_Driver_Builtin($driver); } else { - return PEAR::raiseError('Unsupported DBA driver, '.$driver); + return DBA::raiseError(DBA_ERROR_UNSUP_DRIVER, NULL, NULL, + 'driver: '.$driver); } } - function raiseError($message) { - return PEAR::raiseError('DBA: '.$message); + /** + * Returns whether a result code from a DBA method is an error + * + * @param int $value result code + * @return boolean whether $value is a DBA_Error + * @access public + */ + function isError($value) + { + return (is_object($value) && + (get_class($value) == 'dba_error' || is_subclass_of($value, 'dba_error'))); } + + /** + * Return a textual error message for a DBA error code + * + * @param int $value error code + * @return string error message, or false if the error code was + * not recognized + * @access public + */ + function errorMessage($value) + { + static $errorMessages; + if (!isset($errorMessages)) { + $errorMessages = array( + DBA_ERROR => 'unknown error', + DBA_ERROR_UNSUP_DRIVER => 'unsupported database driver', + DBA_ERROR_UNSUP_PERSISTENCE => 'persistent connections not supported', + DBA_ERROR_NO_DRIVER => 'no driver loaded', + DBA_ERROR_NO_DBNAME => 'no databasename given', + DBA_ERROR_NOSUCHDB => 'database not found', + DBA_ERROR_INVALID_MODE => 'invalid file mode', + DBA_ERROR_CANNOT_CREATE => 'can not create database', + DBA_ERROR_CANNOT_OPEN => 'can not open database', + DBA_ERROR_CANNOT_DROP => 'can not drop database', + DBA_ERROR_NOT_READABLE => 'database not readable', + DBA_ERROR_NOT_WRITEABLE => 'database not writeable', + DBA_ERROR_NOT_OPEN => 'database not open', + DBA_ERROR_NOT_FOUND => 'key not found', + DBA_ERROR_ALREADY_EXISTS => 'key already exists', + ); + } + + if (DBA::isError($value)) { + $value = $value->getCode(); + } + + return isset($errorMessages[$value]) ? + $errorMessages[$value] : $errorMessages[DBA_ERROR]; + } + + /** + * This method is used to communicate an error and invoke error + * callbacks etc. Basically a wrapper for PEAR::raiseError + * without the message string. + * + * @param mixed $code integer error code, or a PEAR error object (all + * other parameters are ignored if this parameter is an object + * @param int $mode error mode, see PEAR_Error docs + * @param mixed $options If error mode is PEAR_ERROR_TRIGGER, this is the + * error level (E_USER_NOTICE etc). If error mode is + * PEAR_ERROR_CALLBACK, this is the callback function, either as a + * function name, or as an array of an object and method name. For + * other error modes this parameter is ignored. + * @param string $userinfo Extra debug information. + * @return object a PEAR error object + * @access public + * @see PEAR_Error + */ + function &raiseError($code = DBA_ERROR, $mode = NULL, $options = NULL, + $userinfo = NULL) + { + // The error is yet a DBA error object + if (is_object($code)) { + return PEAR::raiseError($code, NULL, NULL, NULL, NULL, NULL, TRUE); + } + return PEAR::raiseError(NULL, $code, $mode, $options, $userinfo, + 'DBA_Error', TRUE); + } + /** * Returns whether a database exists * @@ -83,7 +183,7 @@ } elseif (($driver == 'db3') || ($driver == 'gdbm')){ return file_exists($name); } else { - return PEAR::raiseError('Unknown DBA driver, '.$driver); + return DBA::raiseError(DBA_ERROR_NO_DRIVER, NULL, NULL, 'driver: '.$driver); } } @@ -97,4 +197,40 @@ return array('gdbm', 'db3', 'file', 'db2'); } } + +/** + * DBA_Error implements a class for reporting portable database error + * messages. Based on the PEAR::DBM implementation. + * + * @package DBA + * @author Olaf Conradi <conradi@cs.utwente.nl> + */ +class DBA_Error extends PEAR_Error +{ + + // {{{ constructor + + /** + * DBA_Error constructor. + * + * @param mixed $code DBA error code, or string with error message. + * @param integer $mode what "error mode" to operate in + * @param integer $level what error level to use for + * $mode & PEAR_ERROR_TRIGGER + * @param smixed $debuginfo additional debug info, such as the last query + */ + function DBA_Error($code = DBA_ERROR, $mode = PEAR_ERROR_RETURN, + $level = E_USER_NOTICE, $debuginfo = NULL) + { + if (is_int($code)) { + $this->PEAR_Error('DBA Error: '.DBA::errorMessage($code), $code, + $mode, $level, $debuginfo); + } else { + $this->PEAR_Error('DBA Error: '.$code, DBA_ERROR, $mode, $level, + $debuginfo); + } + } + // }}} +} + ?> Index: pear/DBA/package.xml diff -u pear/DBA/package.xml:1.24 pear/DBA/package.xml:1.25 --- pear/DBA/package.xml:1.24 Sun Sep 29 23:25:10 2002 +++ pear/DBA/package.xml Thu Oct 31 22:44:13 2002 @@ -16,22 +16,23 @@ <email>busterb@mail.utexas.edu</email> <role>lead</role> </maintainer> + <maintainer> + <user>undefined</user> + <name>Olaf Conradi</name> + <email>conradi@cs.utwente.nl</email> + <role>contributor</role> + </maintainer> </maintainers> <release> <license>LGPL</license> - <version>0.9.4</version> - <date>2002-09-29</date> + <version>0.9.5</version> + <date>2002-10-31</date> <state>beta</state> <notes> -Several nice changes for this release: - * The DBA::exists() function has been renamed to DBA::db_exists(). This is to not be confused with the driver method exists(). - - * Added db2 support. Apparently, it just didn't work on my OS due to outdated db2 libraries (Slackware-current). Be sure to compile PHP with db2 2.7.7. - - * Added db_drop() and drop() methods to the drivers. db_drop() is static and takes a database name as an argument; drop() deletes the currently open or last open database. - -Many of the functions that were included with the earlier developer releases, such as the table manager and SQL parser, have been split into separate packages to be released soon. +* Errors now return error codes in addition to messages. Thanks to Olaf Conradi +for the work on this. +* Minor updates to code comments </notes> <filelist> <dir name="/" baseinstalldir="/"> Index: pear/DBA/DBA/Driver/Builtin.php diff -u pear/DBA/DBA/Driver/Builtin.php:1.8 pear/DBA/DBA/Driver/Builtin.php:1.9 --- pear/DBA/DBA/Driver/Builtin.php:1.8 Sun Sep 29 23:22:23 2002 +++ pear/DBA/DBA/Driver/Builtin.php Thu Oct 31 22:44:14 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: Builtin.php,v 1.8 2002/09/30 03:22:23 busterb Exp $ +// $Id: Builtin.php,v 1.9 2002/11/01 03:44:14 busterb Exp $ require_once 'DBA.php'; @@ -85,7 +85,7 @@ } // }}} - // {{{ open($dbName='', $mode='r', $driver=NULL) + // {{{ open($dbName='', $mode='r', $persistent=false) /** * Opens a database. * @@ -104,7 +104,7 @@ function open($dbName='', $mode='r', $persistent = false) { if (is_null($this->_driver)) { - return $this->raiseError('No dba driver specified'); + return $this->raiseError(DBA_ERROR_NO_DRIVER); } if ($this->_driver == 'gdbm') { @@ -114,7 +114,7 @@ } if ($dbName == '') { - return $this->raiseError('No database name specified'); + return $this->raiseError(DBA_ERROR_NO_DBNAME); } else { $this->_dbName = $dbName; } @@ -132,8 +132,8 @@ $this->_readable = true; break; default: - return $this->raiseError("Invalid file mode: $mode", - E_USER_ERROR); + return $this->raiseError(DBA_ERROR_INVALID_MODE, NULL, NULL, + 'filemode: '.$mode); } // open the index file @@ -145,8 +145,8 @@ if ($this->_dba === false) { $this->_writable = false; $this->_readable = false; - return $this->raiseError("Could not open database: $dbName" - ." with mode $mode"); + return $this->raiseError(DBA_ERROR_CANNOT_OPEN, NULL, NULL, + 'dbname: '.$dbName.' filemode: '.$mode); } } // }}} @@ -166,7 +166,7 @@ dba_sync($this->_dba); // db2 is known to require syncs dba_close($this->_dba); } else { - return $this->raiseError('No database was open'); + return $this->raiseError(DBA_ERROR_NOT_OPEN); } } // }}} @@ -194,7 +194,7 @@ return $this->open($this->_dbName, 'w'); } } else { - return $this->raiseError('No database was open'); + return $this->raiseError(DBA_ERROR_NOT_OPEN); } } // }}} @@ -261,12 +261,10 @@ { if ($this->isWritable()) { if (!dba_delete($key, $this->_dba)) { - return $this->raiseError('Cannot delete key: '.$key. - ', it does not exist'); + return $this->raiseError(DBA_ERROR_NOT_FOUND, NULL, NULL, 'key: '.$key); } } else { - return $this->raiseError('Cannot delete key '. - $key. ', DB not writable'); + return $this->raiseError(DBA_ERROR_NOT_WRITEABLE); } } // }}} @@ -285,12 +283,10 @@ if (dba_exists($key, $this->_dba)) { return dba_fetch($key, $this->_dba); } else { - return $this->raiseError('Cannot fetch key '.$key. - ', it does not exist'); + return $this->raiseError(DBA_ERROR_NOT_FOUND, NULL, NULL, 'key: '.$key); } } else { - return $this->raiseError('Cannot fetch '.$key.' on '. - $this->_dbName. ', DB not readable'); + return $this->raiseError(DBA_ERROR_NOT_READABLE); } } // }}} @@ -364,12 +360,11 @@ if ((!$this->_hasReplace && dba_exists($key, $this->_dba)) || (!dba_insert($key, $value, $this->_dba))) { - return $this->raiseError('Cannot insert on key: '. - $key. ', it already exists'); + return $this->raiseError(DBA_ERROR_ALREADY_EXISTS, NULL, NULL, + 'key: '.$key); } } else { - return $this->raiseError('Cannot replace on '. - $this->_dbName. ', DB not writable'); + return $this->raiseError(DBA_ERROR_NOT_WRITEABLE); } } // }}} @@ -381,7 +376,7 @@ * * @access public * @param $key string the key to insert - * @param $val string the value to store + * @param $value string the value to store * @return object PEAR_Error on failure */ function replace($key, $value) @@ -399,8 +394,7 @@ } } else { - return $this->raiseError('Cannot replace on '. - $this->_dbName. ', DB not writable'); + return $this->raiseError(DBA_ERROR_NOT_WRITEABLE); } } // }}} @@ -419,7 +413,8 @@ { $db = dba_open($dbName, 'n', $driver); if (!(($db !== false) && dba_close($db))) { - return $this->raiseError('Could not create database: '.$dbName); + return $this->raiseError(DBA_ERROR_CANNOT_CREATE, NULL, NULL, + 'dbname: '.$dbname); } } // }}} @@ -450,10 +445,12 @@ { if (DBA_Driver_Builtin::db_exists($dbName)) { if (!unlink($dbName)) { - return $this->raiseError('Could not unlink '.$dbName); + return $this->raiseError(DBA_ERROR_CANNOT_DROP, NULL, NULL, + 'dbname: '.$dbName); } } else { - return $this->raiseError($dbName.' does not exist'); + return $this->raiseError(DBA_ERROR_NOSUCHDB, NULL, NULL, + 'dbname: '.$dbName); } } // }}} Index: pear/DBA/DBA/Driver/File.php diff -u pear/DBA/DBA/Driver/File.php:1.7 pear/DBA/DBA/Driver/File.php:1.8 --- pear/DBA/DBA/Driver/File.php:1.7 Sun Sep 29 23:25:10 2002 +++ pear/DBA/DBA/Driver/File.php Thu Oct 31 22:44:14 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: File.php,v 1.7 2002/09/30 03:25:10 busterb Exp $ +// $Id: File.php,v 1.8 2002/11/01 03:44:14 busterb Exp $ require_once 'DBA.php'; @@ -149,11 +149,11 @@ function open($dbName='', $mode='r', $persistent = false) { if ($persistent) { - return $this->raiseError('Cannot support persistent connections'); + return $this->raiseError(DBA_ERROR_UNSUP_PERSISTENCE); } if ($dbName == '') { - return $this->raiseError('No database name specified'); + return $this->raiseError(DBA_ERROR_NO_DBNAME); } else { $this->_dbName = $dbName; $dat_name = $dbName.'.dat'; @@ -187,7 +187,8 @@ $this->_readable = true; break; default: - return $this->raiseError("Invalid file mode: $mode"); + return $this->raiseError(DBA_ERROR_INVALID_MODE, NULL, NULL, + 'filemode: '.$mode); } // open the index file @@ -195,8 +196,8 @@ if ($this->_idxFP === false) { $this->_writable = false; $this->_readable = false; - return $this->raiseError('Could not open index file: '.$idx_name. - ' with mode '. $file_mode); + return $this->raiseError(DBA_ERROR_CANNOT_OPEN, NULL, NULL, + 'could not open idx file '.$idx_name); } // open the data file @@ -205,7 +206,8 @@ fclose ($this->_idxFP); $this->_writable = false; $this->_readable = false; - return $this->raiseError('Could not open data file: '.$dat_name); + return $this->raiseError(DBA_ERROR_CANNOT_OPEN, NULL, NULL, + 'could not open data file '.$dat_name); } // get a shared lock if read-only, otherwise get an exclusive lock @@ -244,6 +246,8 @@ $this->_writable = false; fclose($this->_idxFP); fclose($this->_datFP); + } else { + return $this->raiseError(DBA_ERROR_NOT_OPEN); } } // }}} @@ -274,7 +278,7 @@ } } } else { - return $this->raiseError('No database was open'); + return $this->raiseError(DBA_ERROR_NOT_OPEN); } } // }}} @@ -354,12 +358,10 @@ if (isset($this->_usedBlocks[$key])) { $this->_freeUsedBlock($key); } else { - return $this->raiseError('Cannot delete key:'.$key. - ', it does not exist'); + return $this->raiseError(DBA_ERROR_NOT_FOUND, NULL, NULL, 'key: '.$key); } } else { - return $this->raiseError('Cannot delete key:'.$key. - ', DB not writable'); + return $this->raiseError(DBA_ERROR_NOT_WRITEABLE); } } // }}} @@ -376,15 +378,13 @@ { if ($this->isReadable()) { if (!isset($this->_usedBlocks[$key])) { - return $this->raiseError('Cannot fetch key '.$key. - ', it does not exist'); + return $this->raiseError(DBA_ERROR_NOT_FOUND, NULL, NULL, 'key: '.$key); } else { fseek($this->_datFP, $this->_usedBlocks[$key][DBA_SIMPLE_LOC]); return fread($this->_datFP,$this->_usedBlocks[$key][DBA_SIMPLE_VSIZE]); } } else { - return $this->raiseError('Cannot fetch '.$key.' on '. - $this->_dbName. ', DB not readable'); + return $this->raiseError(DBA_ERROR_NOT_READABLE); } } // }}} @@ -455,8 +455,8 @@ function insert($key, $value) { if ($this->exists($key)) { - return $this->raiseError('Cannot insert on key: '. - $key. ', it already exists'); + return $this->raiseError(DBA_ERROR_ALREADY_EXISTS, NULL, NULL, + 'key: '.$key); } else { return $this->replace($key, $value); } @@ -510,8 +510,7 @@ } } } else { - return $this->raiseError('Cannot replace on '. - $this->_dbName. ', DB not writable'); + return $this->raiseError(DBA_ERROR_NOT_WRITEABLE); } } // }}} @@ -651,10 +650,12 @@ { if (DBA_Driver_File::db_exists($dbName)) { if (!unlink($dbName.'.dat') || !unlink($dbName.'.idx')) { - return $this->raiseError('Could not unlink '.$dbName); + return $this->raiseError(DBA_ERROR_CANNOT_DROP, NULL, NULL, + 'dbname: '.$dbName); } } else { - return $this->raiseError($dbName.' does not exist'); + return $this->raiseError(DBA_ERROR_NOSUCHDB, NULL, NULL, + 'dbname: '.$dbName); } } // }}}
Index: pear/DBA/DBA.php diff -u pear/DBA/DBA.php:1.26 pear/DBA/DBA.php:1.27 --- pear/DBA/DBA.php:1.26 Sun Sep 29 23:25:10 2002 +++ pear/DBA/DBA.php Thu Oct 31 22:44:13 2002 @@ -18,12 +18,33 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: DBA.php,v 1.26 2002/09/30 03:25:10 busterb Exp $ +// $Id: DBA.php,v 1.27 2002/11/01 03:44:13 busterb Exp $ // require_once('PEAR.php'); /** + * If you add an error code here, make sure you also add a textual + * version of it in DBA::errorMessage(). + */ + // userinfo contains: +define('DBA_ERROR', -1); +define('DBA_ERROR_UNSUP_DRIVER', -2); // drivername +define('DBA_ERROR_UNSUP_PERSISTENCE', -3); +define('DBA_ERROR_NO_DRIVER', -4); // drivername +define('DBA_ERROR_NO_DBNAME', -5); +define('DBA_ERROR_NOSUCHDB', -6); // dbname +define('DBA_ERROR_INVALID_MODE', -7); // filemode +define('DBA_ERROR_CANNOT_CREATE', -8); // dbname +define('DBA_ERROR_CANNOT_OPEN', -9); +define('DBA_ERROR_CANNOT_DROP', -10); +define('DBA_ERROR_NOT_READABLE', -11); +define('DBA_ERROR_NOT_WRITEABLE', -12); +define('DBA_ERROR_NOT_OPEN', -13); +define('DBA_ERROR_NOT_FOUND', -14); +define('DBA_ERROR_ALREADY_EXISTS', -15); // key + +/** * DBA is a set of classes for handling and extending Berkeley DB style * databases. It works around some of the quirks in the built-in dba * functions in PHP (e.g. gdbm does not support dba_replace), has a file-based @@ -61,14 +82,93 @@ require_once 'DBA/Driver/Builtin.php'; return new DBA_Driver_Builtin($driver); } else { - return PEAR::raiseError('Unsupported DBA driver, '.$driver); + return DBA::raiseError(DBA_ERROR_UNSUP_DRIVER, NULL, NULL, + 'driver: '.$driver); } } - function raiseError($message) { - return PEAR::raiseError('DBA: '.$message); + /** + * Returns whether a result code from a DBA method is an error + * + * @param int $value result code + * @return boolean whether $value is a DBA_Error + * @access public + */ + function isError($value) + { + return (is_object($value) && + (get_class($value) == 'dba_error' || is_subclass_of($value, 'dba_error'))); } + + /** + * Return a textual error message for a DBA error code + * + * @param int $value error code + * @return string error message, or false if the error code was + * not recognized + * @access public + */ + function errorMessage($value) + { + static $errorMessages; + if (!isset($errorMessages)) { + $errorMessages = array( + DBA_ERROR => 'unknown error', + DBA_ERROR_UNSUP_DRIVER => 'unsupported database driver', + DBA_ERROR_UNSUP_PERSISTENCE => 'persistent connections not supported', + DBA_ERROR_NO_DRIVER => 'no driver loaded', + DBA_ERROR_NO_DBNAME => 'no databasename given', + DBA_ERROR_NOSUCHDB => 'database not found', + DBA_ERROR_INVALID_MODE => 'invalid file mode', + DBA_ERROR_CANNOT_CREATE => 'can not create database', + DBA_ERROR_CANNOT_OPEN => 'can not open database', + DBA_ERROR_CANNOT_DROP => 'can not drop database', + DBA_ERROR_NOT_READABLE => 'database not readable', + DBA_ERROR_NOT_WRITEABLE => 'database not writeable', + DBA_ERROR_NOT_OPEN => 'database not open', + DBA_ERROR_NOT_FOUND => 'key not found', + DBA_ERROR_ALREADY_EXISTS => 'key already exists', + ); + } + + if (DBA::isError($value)) { + $value = $value->getCode(); + } + + return isset($errorMessages[$value]) ? + $errorMessages[$value] : $errorMessages[DBA_ERROR]; + } + + /** + * This method is used to communicate an error and invoke error + * callbacks etc. Basically a wrapper for PEAR::raiseError + * without the message string. + * + * @param mixed $code integer error code, or a PEAR error object (all + * other parameters are ignored if this parameter is an object + * @param int $mode error mode, see PEAR_Error docs + * @param mixed $options If error mode is PEAR_ERROR_TRIGGER, this is the + * error level (E_USER_NOTICE etc). If error mode is + * PEAR_ERROR_CALLBACK, this is the callback function, either as a + * function name, or as an array of an object and method name. For + * other error modes this parameter is ignored. + * @param string $userinfo Extra debug information. + * @return object a PEAR error object + * @access public + * @see PEAR_Error + */ + function &raiseError($code = DBA_ERROR, $mode = NULL, $options = NULL, + $userinfo = NULL) + { + // The error is yet a DBA error object + if (is_object($code)) { + return PEAR::raiseError($code, NULL, NULL, NULL, NULL, NULL, TRUE); + } + return PEAR::raiseError(NULL, $code, $mode, $options, $userinfo, + 'DBA_Error', TRUE); + } + /** * Returns whether a database exists * @@ -83,7 +183,7 @@ } elseif (($driver == 'db3') || ($driver == 'gdbm')){ return file_exists($name); } else { - return PEAR::raiseError('Unknown DBA driver, '.$driver); + return DBA::raiseError(DBA_ERROR_NO_DRIVER, NULL, NULL, 'driver: '.$driver); } } @@ -97,4 +197,40 @@ return array('gdbm', 'db3', 'file', 'db2'); } } + +/** + * DBA_Error implements a class for reporting portable database error + * messages. Based on the PEAR::DBM implementation. + * + * @package DBA + * @author Olaf Conradi <conradi@cs.utwente.nl> + */ +class DBA_Error extends PEAR_Error +{ + + // {{{ constructor + + /** + * DBA_Error constructor. + * + * @param mixed $code DBA error code, or string with error message. + * @param integer $mode what "error mode" to operate in + * @param integer $level what error level to use for + * $mode & PEAR_ERROR_TRIGGER + * @param smixed $debuginfo additional debug info, such as the last query + */ + function DBA_Error($code = DBA_ERROR, $mode = PEAR_ERROR_RETURN, + $level = E_USER_NOTICE, $debuginfo = NULL) + { + if (is_int($code)) { + $this->PEAR_Error('DBA Error: '.DBA::errorMessage($code), $code, + $mode, $level, $debuginfo); + } else { + $this->PEAR_Error('DBA Error: '.$code, DBA_ERROR, $mode, $level, + $debuginfo); + } + } + // }}} +} + ?> Index: pear/DBA/package.xml diff -u pear/DBA/package.xml:1.24 pear/DBA/package.xml:1.25 --- pear/DBA/package.xml:1.24 Sun Sep 29 23:25:10 2002 +++ pear/DBA/package.xml Thu Oct 31 22:44:13 2002 @@ -16,22 +16,23 @@ <email>busterb@mail.utexas.edu</email> <role>lead</role> </maintainer> + <maintainer> + <user>undefined</user> + <name>Olaf Conradi</name> + <email>conradi@cs.utwente.nl</email> + <role>contributor</role> + </maintainer> </maintainers> <release> <license>LGPL</license> - <version>0.9.4</version> - <date>2002-09-29</date> + <version>0.9.5</version> + <date>2002-10-31</date> <state>beta</state> <notes> -Several nice changes for this release: - * The DBA::exists() function has been renamed to DBA::db_exists(). This is to not be confused with the driver method exists(). - - * Added db2 support. Apparently, it just didn't work on my OS due to outdated db2 libraries (Slackware-current). Be sure to compile PHP with db2 2.7.7. - - * Added db_drop() and drop() methods to the drivers. db_drop() is static and takes a database name as an argument; drop() deletes the currently open or last open database. - -Many of the functions that were included with the earlier developer releases, such as the table manager and SQL parser, have been split into separate packages to be released soon. +* Errors now return error codes in addition to messages. Thanks to Olaf Conradi +for the work on this. +* Minor updates to code comments </notes> <filelist> <dir name="/" baseinstalldir="/"> Index: pear/DBA/DBA/Driver/Builtin.php diff -u pear/DBA/DBA/Driver/Builtin.php:1.8 pear/DBA/DBA/Driver/Builtin.php:1.9 --- pear/DBA/DBA/Driver/Builtin.php:1.8 Sun Sep 29 23:22:23 2002 +++ pear/DBA/DBA/Driver/Builtin.php Thu Oct 31 22:44:14 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: Builtin.php,v 1.8 2002/09/30 03:22:23 busterb Exp $ +// $Id: Builtin.php,v 1.9 2002/11/01 03:44:14 busterb Exp $ require_once 'DBA.php'; @@ -85,7 +85,7 @@ } // }}} - // {{{ open($dbName='', $mode='r', $driver=NULL) + // {{{ open($dbName='', $mode='r', $persistent=false) /** * Opens a database. * @@ -104,7 +104,7 @@ function open($dbName='', $mode='r', $persistent = false) { if (is_null($this->_driver)) { - return $this->raiseError('No dba driver specified'); + return $this->raiseError(DBA_ERROR_NO_DRIVER); } if ($this->_driver == 'gdbm') { @@ -114,7 +114,7 @@ } if ($dbName == '') { - return $this->raiseError('No database name specified'); + return $this->raiseError(DBA_ERROR_NO_DBNAME); } else { $this->_dbName = $dbName; } @@ -132,8 +132,8 @@ $this->_readable = true; break; default: - return $this->raiseError("Invalid file mode: $mode", - E_USER_ERROR); + return $this->raiseError(DBA_ERROR_INVALID_MODE, NULL, NULL, + 'filemode: '.$mode); } // open the index file @@ -145,8 +145,8 @@ if ($this->_dba === false) { $this->_writable = false; $this->_readable = false; - return $this->raiseError("Could not open database: $dbName" - ." with mode $mode"); + return $this->raiseError(DBA_ERROR_CANNOT_OPEN, NULL, NULL, + 'dbname: '.$dbName.' filemode: '.$mode); } } // }}} @@ -166,7 +166,7 @@ dba_sync($this->_dba); // db2 is known to require syncs dba_close($this->_dba); } else { - return $this->raiseError('No database was open'); + return $this->raiseError(DBA_ERROR_NOT_OPEN); } } // }}} @@ -194,7 +194,7 @@ return $this->open($this->_dbName, 'w'); } } else { - return $this->raiseError('No database was open'); + return $this->raiseError(DBA_ERROR_NOT_OPEN); } } // }}} @@ -261,12 +261,10 @@ { if ($this->isWritable()) { if (!dba_delete($key, $this->_dba)) { - return $this->raiseError('Cannot delete key: '.$key. - ', it does not exist'); + return $this->raiseError(DBA_ERROR_NOT_FOUND, NULL, NULL, 'key: '.$key); } } else { - return $this->raiseError('Cannot delete key '. - $key. ', DB not writable'); + return $this->raiseError(DBA_ERROR_NOT_WRITEABLE); } } // }}} @@ -285,12 +283,10 @@ if (dba_exists($key, $this->_dba)) { return dba_fetch($key, $this->_dba); } else { - return $this->raiseError('Cannot fetch key '.$key. - ', it does not exist'); + return $this->raiseError(DBA_ERROR_NOT_FOUND, NULL, NULL, 'key: '.$key); } } else { - return $this->raiseError('Cannot fetch '.$key.' on '. - $this->_dbName. ', DB not readable'); + return $this->raiseError(DBA_ERROR_NOT_READABLE); } } // }}} @@ -364,12 +360,11 @@ if ((!$this->_hasReplace && dba_exists($key, $this->_dba)) || (!dba_insert($key, $value, $this->_dba))) { - return $this->raiseError('Cannot insert on key: '. - $key. ', it already exists'); + return $this->raiseError(DBA_ERROR_ALREADY_EXISTS, NULL, NULL, + 'key: '.$key); } } else { - return $this->raiseError('Cannot replace on '. - $this->_dbName. ', DB not writable'); + return $this->raiseError(DBA_ERROR_NOT_WRITEABLE); } } // }}} @@ -381,7 +376,7 @@ * * @access public * @param $key string the key to insert - * @param $val string the value to store + * @param $value string the value to store * @return object PEAR_Error on failure */ function replace($key, $value) @@ -399,8 +394,7 @@ } } else { - return $this->raiseError('Cannot replace on '. - $this->_dbName. ', DB not writable'); + return $this->raiseError(DBA_ERROR_NOT_WRITEABLE); } } // }}} @@ -419,7 +413,8 @@ { $db = dba_open($dbName, 'n', $driver); if (!(($db !== false) && dba_close($db))) { - return $this->raiseError('Could not create database: '.$dbName); + return $this->raiseError(DBA_ERROR_CANNOT_CREATE, NULL, NULL, + 'dbname: '.$dbname); } } // }}} @@ -450,10 +445,12 @@ { if (DBA_Driver_Builtin::db_exists($dbName)) { if (!unlink($dbName)) { - return $this->raiseError('Could not unlink '.$dbName); + return $this->raiseError(DBA_ERROR_CANNOT_DROP, NULL, NULL, + 'dbname: '.$dbName); } } else { - return $this->raiseError($dbName.' does not exist'); + return $this->raiseError(DBA_ERROR_NOSUCHDB, NULL, NULL, + 'dbname: '.$dbName); } } // }}} Index: pear/DBA/DBA/Driver/File.php diff -u pear/DBA/DBA/Driver/File.php:1.7 pear/DBA/DBA/Driver/File.php:1.8 --- pear/DBA/DBA/Driver/File.php:1.7 Sun Sep 29 23:25:10 2002 +++ pear/DBA/DBA/Driver/File.php Thu Oct 31 22:44:14 2002 @@ -18,7 +18,7 @@ // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // -// $Id: File.php,v 1.7 2002/09/30 03:25:10 busterb Exp $ +// $Id: File.php,v 1.8 2002/11/01 03:44:14 busterb Exp $ require_once 'DBA.php'; @@ -149,11 +149,11 @@ function open($dbName='', $mode='r', $persistent = false) { if ($persistent) { - return $this->raiseError('Cannot support persistent connections'); + return $this->raiseError(DBA_ERROR_UNSUP_PERSISTENCE); } if ($dbName == '') { - return $this->raiseError('No database name specified'); + return $this->raiseError(DBA_ERROR_NO_DBNAME); } else { $this->_dbName = $dbName; $dat_name = $dbName.'.dat'; @@ -187,7 +187,8 @@ $this->_readable = true; break; default: - return $this->raiseError("Invalid file mode: $mode"); + return $this->raiseError(DBA_ERROR_INVALID_MODE, NULL, NULL, + 'filemode: '.$mode); } // open the index file @@ -195,8 +196,8 @@ if ($this->_idxFP === false) { $this->_writable = false; $this->_readable = false; - return $this->raiseError('Could not open index file: '.$idx_name. - ' with mode '. $file_mode); + return $this->raiseError(DBA_ERROR_CANNOT_OPEN, NULL, NULL, + 'could not open idx file '.$idx_name); } // open the data file @@ -205,7 +206,8 @@ fclose ($this->_idxFP); $this->_writable = false; $this->_readable = false; - return $this->raiseError('Could not open data file: '.$dat_name); + return $this->raiseError(DBA_ERROR_CANNOT_OPEN, NULL, NULL, + 'could not open data file '.$dat_name); } // get a shared lock if read-only, otherwise get an exclusive lock @@ -244,6 +246,8 @@ $this->_writable = false; fclose($this->_idxFP); fclose($this->_datFP); + } else { + return $this->raiseError(DBA_ERROR_NOT_OPEN); } } // }}} @@ -274,7 +278,7 @@ } } } else { - return $this->raiseError('No database was open'); + return $this->raiseError(DBA_ERROR_NOT_OPEN); } } // }}} @@ -354,12 +358,10 @@ if (isset($this->_usedBlocks[$key])) { $this->_freeUsedBlock($key); } else { - return $this->raiseError('Cannot delete key:'.$key. - ', it does not exist'); + return $this->raiseError(DBA_ERROR_NOT_FOUND, NULL, NULL, 'key: '.$key); } } else { - return $this->raiseError('Cannot delete key:'.$key. - ', DB not writable'); + return $this->raiseError(DBA_ERROR_NOT_WRITEABLE); } } // }}} @@ -376,15 +378,13 @@ { if ($this->isReadable()) { if (!isset($this->_usedBlocks[$key])) { - return $this->raiseError('Cannot fetch key '.$key. - ', it does not exist'); + return $this->raiseError(DBA_ERROR_NOT_FOUND, NULL, NULL, 'key: '.$key); } else { fseek($this->_datFP, $this->_usedBlocks[$key][DBA_SIMPLE_LOC]); return fread($this->_datFP,$this->_usedBlocks[$key][DBA_SIMPLE_VSIZE]); } } else { - return $this->raiseError('Cannot fetch '.$key.' on '. - $this->_dbName. ', DB not readable'); + return $this->raiseError(DBA_ERROR_NOT_READABLE); } } // }}} @@ -455,8 +455,8 @@ function insert($key, $value) { if ($this->exists($key)) { - return $this->raiseError('Cannot insert on key: '. - $key. ', it already exists'); + return $this->raiseError(DBA_ERROR_ALREADY_EXISTS, NULL, NULL, + 'key: '.$key); } else { return $this->replace($key, $value); } @@ -510,8 +510,7 @@ } } } else { - return $this->raiseError('Cannot replace on '. - $this->_dbName. ', DB not writable'); + return $this->raiseError(DBA_ERROR_NOT_WRITEABLE); } } // }}} @@ -651,10 +650,12 @@ { if (DBA_Driver_File::db_exists($dbName)) { if (!unlink($dbName.'.dat') || !unlink($dbName.'.idx')) { - return $this->raiseError('Could not unlink '.$dbName); + return $this->raiseError(DBA_ERROR_CANNOT_DROP, NULL, NULL, + 'dbname: '.$dbName); } } else { - return $this->raiseError($dbName.' does not exist'); + return $this->raiseError(DBA_ERROR_NOSUCHDB, NULL, NULL, + 'dbname: '.$dbName); } } // }}}