1 <?php rcs_id('$Id: DbaDatabase.php,v 1.5 2002-12-31 02:32:28 wainstead Exp $');
3 require_once('lib/ErrorManager.php');
4 // FIXME: autodetect supported handlers.
6 define('DBA_DATABASE_DEFAULT_TIMEOUT', 20);
10 function DbaDatabase($filename, $mode = false, $handler = 'gdbm') {
11 $this->_file = $filename;
12 $this->_handler = $handler;
13 $this->_timeout = DBA_DATABASE_DEFAULT_TIMEOUT;
19 function set_timeout($timeout) {
20 $this->_timeout = $timeout;
23 function open($mode = 'w') {
25 return; // already open.
27 $watchdog = $this->_timeout;
30 $this->_dba_open_error = false;
31 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_dba_open_error_handler'));
33 // oops, you don't have DBM support.
34 if (!function_exists("dba_open")) {
35 echo "You don't seem to have DBM file support compiled into PHP.";
38 while (($dbh = dba_open($this->_file, $mode, $this->_handler)) < 1) {
44 $ErrorManager->popErrorHandler();
47 if ( ($error = $this->_dba_open_error) ) {
48 $error->errno = E_USER_ERROR;
49 $ErrorManager->handleError($error);
52 trigger_error("dba_open failed", E_USER_ERROR);
61 dba_close($this->_dbh);
65 function exists($key) {
66 return dba_exists($key, $this->_dbh);
69 function fetch($key) {
70 $val = dba_fetch($key, $this->_dbh);
72 return $this->_error("fetch($key)");
76 function insert($key, $val) {
77 if (!dba_insert($key, $val, $this->_dbh))
78 return $this->_error("insert($key)");
81 function replace($key, $val) {
82 if (!dba_replace($key, $val, $this->_dbh))
83 return $this->_error("replace($key)");
88 return dba_firstkey($this->_dbh);
92 return dba_nextkey($this->_dbh);
95 function delete($key) {
96 if (!dba_delete($key, $this->_dbh))
97 return $this->_error("delete($key)");
101 return dba_fetch($key, $this->_dbh);
104 function set($key, $val) {
106 if (dba_exists($key, $dbh)) {
107 if ($val !== false) {
108 if (!dba_replace($key, $val, $dbh))
109 return $this->_error("store[replace]($key)");
112 if (!dba_delete($key, $dbh))
113 return $this->_error("store[delete]($key)");
117 if (!dba_insert($key, $val, $this->_dbh))
118 return $this->_error("store[insert]($key)");
123 if (!dba_sync($this->_dbh))
124 return $this->_error("sync()");
127 function optimize() {
128 if (!dba_optimize($this->_dbh))
129 return $this->_error("optimize()");
132 function _error($mes) {
133 trigger_error("DbaDatabase: $mes", E_USER_WARNING);
136 trigger_error("$this->_file: dba error: $mes", E_USER_ERROR);
141 for ($key = $this->firstkey($dbh); $key; $key = $this->nextkey($dbh))
142 printf("%10s: %s\n", $key, $this->fetch($key));
145 function _dba_open_error_handler ($error) {
146 $this->_dba_open_error = $error;
152 // (c-file-style: "gnu")
157 // c-hanging-comment-ender-p: nil
158 // indent-tabs-mode: nil