]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/DbaDatabase.php
Cleanup of special PageList column types
[SourceForge/phpwiki.git] / lib / DbaDatabase.php
1 <?php rcs_id('$Id: DbaDatabase.php,v 1.8 2004-04-06 20:00:10 rurban 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         // PHP 4.3.x Windows lock bug workaround: http://bugs.php.net/bug.php?id=23975
39         if (isWindows() and (strlen($mode) == 1)) {
40             $mode .= "-"; // add locking
41         }
42         while (($dbh = dba_open($this->_file, $mode, $this->_handler)) < 1) {
43             if (--$watchdog <= 0)
44                 break;
45             flush();
46             sleep(1);
47         }
48         $ErrorManager->popErrorHandler();
49
50         if (!$dbh) {
51             if ( ($error = $this->_dba_open_error) ) {
52                 $error->errno = E_USER_ERROR;
53                 $ErrorManager->handleError($error);
54             }
55             else {
56                 trigger_error("dba_open failed", E_USER_ERROR);
57             }
58         }
59         $this->_dbh = $dbh;
60         return !empty($dbh);
61     }
62
63     function close() {
64         if ($this->_dbh)
65             dba_close($this->_dbh);
66         $this->_dbh = false;
67     }
68
69     function exists($key) {
70         return dba_exists($key, $this->_dbh);
71     }
72     
73     function fetch($key) {
74         $val = dba_fetch($key, $this->_dbh);
75         if ($val === false)
76             return $this->_error("fetch($key)");
77         return $val;
78     }
79
80     function insert($key, $val) {
81         if (!dba_insert($key, $val, $this->_dbh))
82             return $this->_error("insert($key)");
83     }
84
85     function replace($key, $val) {
86         if (!dba_replace($key, $val, $this->_dbh))
87             return $this->_error("replace($key)");
88     }
89
90     
91     function firstkey() {
92         return dba_firstkey($this->_dbh);
93     }
94
95     function nextkey() {
96         return dba_nextkey($this->_dbh);
97     }
98
99     function delete($key) {
100         if (!dba_delete($key, $this->_dbh))
101             return $this->_error("delete($key)");
102     }
103
104     function get($key) {
105         return dba_fetch($key, $this->_dbh);
106     }
107
108     function set($key, $val) {
109         $dbh = &$this->_dbh;
110         if (dba_exists($key, $dbh)) {
111             if ($val !== false) {
112                 if (!dba_replace($key, $val, $dbh))
113                     return $this->_error("store[replace]($key)");
114             }
115             else {
116                 if (!dba_delete($key, $dbh))
117                     return $this->_error("store[delete]($key)");
118             }
119         }
120         else {
121             if (!dba_insert($key, $val, $this->_dbh))
122                 return $this->_error("store[insert]($key)");
123         }
124     }
125
126     function sync() {
127         if (!dba_sync($this->_dbh))
128             return $this->_error("sync()");
129     }
130
131     function optimize() {
132         if (!dba_optimize($this->_dbh))
133             return $this->_error("optimize()");
134         return 1;
135     }
136     
137     function _error($mes) {
138         trigger_error("DbaDatabase: $mes", E_USER_WARNING);
139         return false;
140         
141         trigger_error("$this->_file: dba error: $mes", E_USER_ERROR);
142     }
143
144     function _dump() {
145         $dbh = &$this->_dbh;
146         for ($key = $this->firstkey($dbh); $key; $key = $this->nextkey($dbh))
147             printf("%10s: %s\n", $key, $this->fetch($key));
148     }
149
150     function _dba_open_error_handler ($error) {
151         $this->_dba_open_error = $error;
152         return true;
153     }
154 }
155
156
157 // (c-file-style: "gnu")
158 // Local Variables:
159 // mode: php
160 // tab-width: 8
161 // c-basic-offset: 4
162 // c-hanging-comment-ender-p: nil
163 // indent-tabs-mode: nil
164 // End:   
165 ?>