]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/DbaDatabase.php
Rename functional for PearDB backend
[SourceForge/phpwiki.git] / lib / DbaDatabase.php
1 <?php rcs_id('$Id: DbaDatabase.php,v 1.5 2002-12-31 02:32:28 wainstead Exp $');
2
3 require_once('lib/ErrorManager.php');
4 // FIXME: autodetect supported handlers.
5
6 define('DBA_DATABASE_DEFAULT_TIMEOUT', 20);
7
8 class DbaDatabase
9 {
10     function DbaDatabase($filename, $mode = false, $handler = 'gdbm') {
11         $this->_file = $filename;
12         $this->_handler = $handler;
13         $this->_timeout = DBA_DATABASE_DEFAULT_TIMEOUT;
14         $this->_dbh = false;
15         if ($mode)
16             $this->open($mode);
17     }
18
19     function set_timeout($timeout) {
20         $this->_timeout = $timeout;
21     }
22     
23     function open($mode = 'w') {
24         if ($this->_dbh)
25             return;             // already open.
26         
27         $watchdog = $this->_timeout;
28
29         global $ErrorManager;
30         $this->_dba_open_error = false;
31         $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_dba_open_error_handler'));
32
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.";
36         }
37
38         while (($dbh = dba_open($this->_file, $mode, $this->_handler)) < 1) {
39             if (--$watchdog <= 0)
40                 break;
41             flush();
42             sleep(1);
43         }
44         $ErrorManager->popErrorHandler();
45
46         if (!$dbh) {
47             if ( ($error = $this->_dba_open_error) ) {
48                 $error->errno = E_USER_ERROR;
49                 $ErrorManager->handleError($error);
50             }
51             else {
52                 trigger_error("dba_open failed", E_USER_ERROR);
53             }
54         }
55         $this->_dbh = $dbh;
56         return !empty($dbh);
57     }
58
59     function close() {
60         if ($this->_dbh)
61             dba_close($this->_dbh);
62         $this->_dbh = false;
63     }
64
65     function exists($key) {
66         return dba_exists($key, $this->_dbh);
67     }
68     
69     function fetch($key) {
70         $val = dba_fetch($key, $this->_dbh);
71         if ($val === false)
72             return $this->_error("fetch($key)");
73         return $val;
74     }
75
76     function insert($key, $val) {
77         if (!dba_insert($key, $val, $this->_dbh))
78             return $this->_error("insert($key)");
79     }
80
81     function replace($key, $val) {
82         if (!dba_replace($key, $val, $this->_dbh))
83             return $this->_error("replace($key)");
84     }
85
86     
87     function firstkey() {
88         return dba_firstkey($this->_dbh);
89     }
90
91     function nextkey() {
92         return dba_nextkey($this->_dbh);
93     }
94
95     function delete($key) {
96         if (!dba_delete($key, $this->_dbh))
97             return $this->_error("delete($key)");
98     }
99
100     function get($key) {
101         return dba_fetch($key, $this->_dbh);
102     }
103
104     function set($key, $val) {
105         $dbh = &$this->_dbh;
106         if (dba_exists($key, $dbh)) {
107             if ($val !== false) {
108                 if (!dba_replace($key, $val, $dbh))
109                     return $this->_error("store[replace]($key)");
110             }
111             else {
112                 if (!dba_delete($key, $dbh))
113                     return $this->_error("store[delete]($key)");
114             }
115         }
116         else {
117             if (!dba_insert($key, $val, $this->_dbh))
118                 return $this->_error("store[insert]($key)");
119         }
120     }
121
122     function sync() {
123         if (!dba_sync($this->_dbh))
124             return $this->_error("sync()");
125     }
126
127     function optimize() {
128         if (!dba_optimize($this->_dbh))
129             return $this->_error("optimize()");
130     }
131     
132     function _error($mes) {
133         trigger_error("DbaDatabase: $mes", E_USER_WARNING);
134         return false;
135         
136         trigger_error("$this->_file: dba error: $mes", E_USER_ERROR);
137     }
138
139     function _dump() {
140         $dbh = &$this->_dbh;
141         for ($key = $this->firstkey($dbh); $key; $key = $this->nextkey($dbh))
142             printf("%10s: %s\n", $key, $this->fetch($key));
143     }
144
145     function _dba_open_error_handler ($error) {
146         $this->_dba_open_error = $error;
147         return true;
148     }
149 }
150
151
152 // (c-file-style: "gnu")
153 // Local Variables:
154 // mode: php
155 // tab-width: 8
156 // c-basic-offset: 4
157 // c-hanging-comment-ender-p: nil
158 // indent-tabs-mode: nil
159 // End:   
160 ?>