1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
<?php rcs_id('$Id: DbaDatabase.php,v 1.21 2006/09/06 05:42:54 rurban Exp $');
require_once('lib/ErrorManager.php');
define('DBA_DATABASE_DEFAULT_TIMEOUT', 5);
class DbaDatabase
{
function DbaDatabase($filename, $mode = false, $handler = 'gdbm') {
$this->_file = $filename;
$this->_handler = $handler;
$this->_timeout = DBA_DATABASE_DEFAULT_TIMEOUT;
$this->_dbh = false;
if (function_exists("dba_handlers")) { // since 4.3.0
if (!in_array($handler, dba_handlers()))
$this->_error(
sprintf(
_("The DBA handler %s is unsupported!")."\n".
_("Supported handlers are: %s"),
$handler, join(",",dba_handlers())));
}
if ($mode)
$this->open($mode);
}
function set_timeout($timeout) {
$this->_timeout = $timeout;
}
function open($mode = 'w') {
if ($this->_dbh)
return; // already open.
$watchdog = $this->_timeout;
global $ErrorManager;
$this->_dba_open_error = false;
$ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_dba_open_error_handler'));
// oops, you don't have DBA support.
if (!function_exists("dba_open")) {
echo "You don't seem to have DBA support compiled into PHP.";
}
// lock supported since 4.3.0:
if (check_php_version(4,3,0) and (strlen($mode) == 1)) {
// PHP 4.3.x Windows lock bug workaround: http://bugs.php.net/bug.php?id=23975
if (isWindows()) {
$mode .= "-"; // suppress locking, or
} elseif ($this->_handler != 'gdbm') { // gdbm does it internally
$mode .= "d"; // else use internal locking
}
}
while (($dbh = dba_open($this->_file, $mode, $this->_handler)) < 1) {
if ($watchdog <= 0)
break;
flush();
// "c" failed, try "w" instead.
if (substr($mode,0,1) == "c" and file_exists($this->_file))
$mode = "w";
// conflict: wait some random time to unlock (see ethernet)
$secs = 0.5 + ((double)rand(1,32767)/32767);
sleep($secs);
$watchdog -= $secs;
if (strlen($mode) == 2) $mode = substr($mode,0,-1);
}
$ErrorManager->popErrorHandler();
if (!$dbh) {
if ( ($error = $this->_dba_open_error) ) {
$error->errno = E_USER_ERROR;
$error->errstr .= "\nfile: " . $this->_file
. "\nmode: " . $mode
. "\nhandler: " . $this->_handler;
$ErrorManager->handleError($error);
}
else {
trigger_error("dba_open failed", E_USER_ERROR);
}
}
$this->_dbh = $dbh;
return !empty($dbh);
}
function close() {
if ($this->_dbh)
dba_close($this->_dbh);
$this->_dbh = false;
}
function exists($key) {
return dba_exists($key, $this->_dbh);
}
function fetch($key) {
$val = dba_fetch($key, $this->_dbh);
if ($val === false)
return $this->_error("fetch($key)");
return $val;
}
function insert($key, $val) {
if (!dba_insert($key, $val, $this->_dbh))
return $this->_error("insert($key)");
}
function replace($key, $val) {
if (!dba_replace($key, $val, $this->_dbh))
return $this->_error("replace($key)");
}
function firstkey() {
return dba_firstkey($this->_dbh);
}
function nextkey() {
return dba_nextkey($this->_dbh);
}
function delete($key) {
if (!dba_delete($key, $this->_dbh))
return $this->_error("delete($key)");
}
function get($key) {
return dba_fetch($key, $this->_dbh);
}
function set($key, $val) {
$dbh = &$this->_dbh;
if (dba_exists($key, $dbh)) {
if ($val !== false) {
if (!dba_replace($key, $val, $dbh))
return $this->_error("store[replace]($key)");
}
else {
if (!dba_delete($key, $dbh))
return $this->_error("store[delete]($key)");
}
}
else {
if (!dba_insert($key, $val, $dbh))
return $this->_error("store[insert]($key)");
}
}
function sync() {
if (!dba_sync($this->_dbh))
return $this->_error("sync()");
}
function optimize() {
if (!dba_optimize($this->_dbh))
return $this->_error("optimize()");
return 1;
}
function _error($mes) {
//trigger_error("DbaDatabase: $mes", E_USER_WARNING);
//return false;
trigger_error("$this->_file: dba error: $mes", E_USER_ERROR);
}
function _dump() {
$dbh = &$this->_dbh;
for ($key = $this->firstkey($dbh); $key; $key = $this->nextkey($dbh))
printf("%10s: %s\n", $key, $this->fetch($key));
}
function _dba_open_error_handler($error) {
$this->_dba_open_error = $error;
return true;
}
}
// $Log: DbaDatabase.php,v $
// Revision 1.21 2006/09/06 05:42:54 rurban
// unify dbh arg
//
// Revision 1.20 2006/08/15 13:35:33 rurban
// just aesthetics
//
// Revision 1.19 2006/06/18 11:01:25 rurban
// add rcsid log
//
// (c-file-style: "gnu")
// Local Variables:
// mode: php
// tab-width: 8
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:
?>
|