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