1 <?php rcs_id('$Id: DbaDatabase.php,v 1.3 2001-12-28 09:52:57 carstenklapp 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'));
32 while (($dbh = dba_open($this->_file, $mode, $this->_handler)) < 1) {
38 $ErrorManager->popErrorHandler();
41 if ( ($error = $this->_dba_open_error) ) {
42 $error->errno = E_USER_ERROR;
43 $ErrorManager->handleError($error);
46 trigger_error(sprintf(_("%s failed"),'dba_open'), E_USER_ERROR);
55 dba_close($this->_dbh);
59 function exists($key) {
60 return dba_exists($key, $this->_dbh);
63 function fetch($key) {
64 $val = dba_fetch($key, $this->_dbh);
66 return $this->_error("fetch($key)");
70 function insert($key, $val) {
71 if (!dba_insert($key, $val, $this->_dbh))
72 return $this->_error("insert($key)");
75 function replace($key, $val) {
76 if (!dba_replace($key, $val, $this->_dbh))
77 return $this->_error("replace($key)");
82 return dba_firstkey($this->_dbh);
86 return dba_nextkey($this->_dbh);
89 function delete($key) {
90 if (!dba_delete($key, $this->_dbh))
91 return $this->_error("delete($key)");
95 return dba_fetch($key, $this->_dbh);
98 function set($key, $val) {
100 if (dba_exists($key, $dbh)) {
101 if ($val !== false) {
102 if (!dba_replace($key, $val, $dbh))
103 return $this->_error("store[replace]($key)");
106 if (!dba_delete($key, $dbh))
107 return $this->_error("store[delete]($key)");
111 if (!dba_insert($key, $val, $this->_dbh))
112 return $this->_error("store[insert]($key)");
117 if (!dba_sync($this->_dbh))
118 return $this->_error("sync()");
121 function optimize() {
122 if (!dba_optimize($this->_dbh))
123 return $this->_error("optimize()");
126 function _error($mes) {
127 trigger_error("DbaDatabase: $mes", E_USER_WARNING);
130 trigger_error("$this->_file: dba error: $mes", E_USER_ERROR);
135 for ($key = $this->firstkey($dbh); $key; $key = $this->nextkey($dbh))
136 printf("%10s: %s\n", $key, $this->fetch($key));
139 function _dba_open_error_handler ($error) {
140 $this->_dba_open_error = $error;
146 // (c-file-style: "gnu")
151 // c-hanging-comment-ender-p: nil
152 // indent-tabs-mode: nil