function UpdateActiveTable($pkeys = false, $forceUpdate = false) { global $ADODB_ASSOC_CASE, $_ADODB_ACTIVE_DBS, $ADODB_CACHE_DIR, $ADODB_ACTIVE_CACHESECS; global $ADODB_ACTIVE_DEFVALS, $ADODB_FETCH_MODE; $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; $table = $this->_table; $tables = $activedb->tables; $tableat = $this->_tableat; if (!$forceUpdate && !empty($tables[$tableat])) { $tobj = $tables[$tableat]; foreach ($tobj->flds as $name => $fld) { if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value)) { $this->{$name} = $fld->default_value; } else { $this->{$name} = null; } } return; } $db = $activedb->db; $fname = $ADODB_CACHE_DIR . '/adodb_' . $db->databaseType . '_active_' . $table . '.cache'; if (!$forceUpdate && $ADODB_ACTIVE_CACHESECS && $ADODB_CACHE_DIR && file_exists($fname)) { $fp = fopen($fname, 'r'); @flock($fp, LOCK_SH); $acttab = unserialize(fread($fp, 100000)); fclose($fp); if ($acttab->_created + $ADODB_ACTIVE_CACHESECS - abs(rand()) % 16 > time()) { // abs(rand()) randomizes deletion, reducing contention to delete/refresh file // ideally, you should cache at least 32 secs $activedb->tables[$table] = $acttab; //if ($db->debug) ADOConnection::outp("Reading cached active record file: $fname"); return; } else { if ($db->debug) { ADOConnection::outp("Refreshing cached active record file: {$fname}"); } } } $activetab = new ADODB_Active_Table(); $activetab->name = $table; $save = $ADODB_FETCH_MODE; $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; if ($db->fetchMode !== false) { $savem = $db->SetFetchMode(false); } $cols = $db->MetaColumns($table); if (isset($savem)) { $db->SetFetchMode($savem); } $ADODB_FETCH_MODE = $save; if (!$cols) { $this->Error("Invalid table name: {$table}", 'UpdateActiveTable'); return false; } $fld = reset($cols); if (!$pkeys) { if (isset($fld->primary_key)) { $pkeys = array(); foreach ($cols as $name => $fld) { if (!empty($fld->primary_key)) { $pkeys[] = $name; } } } else { $pkeys = $this->GetPrimaryKeys($db, $table); } } if (empty($pkeys)) { $this->Error("No primary key found for table {$table}", 'UpdateActiveTable'); return false; } $attr = array(); $keys = array(); switch ($ADODB_ASSOC_CASE) { case 0: foreach ($cols as $name => $fldobj) { $name = strtolower($name); if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) { $this->{$name} = $fldobj->default_value; } else { $this->{$name} = null; } $attr[$name] = $fldobj; } foreach ($pkeys as $k => $name) { $keys[strtolower($name)] = strtolower($name); } break; case 1: foreach ($cols as $name => $fldobj) { $name = strtoupper($name); if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) { $this->{$name} = $fldobj->default_value; } else { $this->{$name} = null; } $attr[$name] = $fldobj; } foreach ($pkeys as $k => $name) { $keys[strtoupper($name)] = strtoupper($name); } break; default: foreach ($cols as $name => $fldobj) { $name = $fldobj->name; if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) { $this->{$name} = $fldobj->default_value; } else { $this->{$name} = null; } $attr[$name] = $fldobj; } foreach ($pkeys as $k => $name) { $keys[$name] = $cols[$name]->name; } break; } $activetab->keys = $keys; $activetab->flds = $attr; $activetab->updateColsCount(); if ($ADODB_ACTIVE_CACHESECS && $ADODB_CACHE_DIR) { $activetab->_created = time(); $s = serialize($activetab); if (!function_exists('adodb_write_file')) { include ADODB_DIR . '/adodb-csvlib.inc.php'; } adodb_write_file($fname, $s); } if (isset($activedb->tables[$table])) { $oldtab = $activedb->tables[$table]; if ($oldtab) { $activetab->_belongsTo = $oldtab->_belongsTo; } if ($oldtab) { $activetab->_hasMany = $oldtab->_hasMany; } } $activedb->tables[$table] = $activetab; }
function writecache($filename, $contents, $debug, $secs2cache) { return adodb_write_file($filename, $contents, $debug); }
/** * Execute SQL, caching recordsets. * * @param [secs2cache] seconds to cache data, set to 0 to force query. * This is an optional parameter. * @param sql SQL statement to execute * @param [inputarr] holds the input data to bind to * @return RecordSet or false */ function &CacheExecute($secs2cache,$sql=false,$inputarr=false) { if (!is_numeric($secs2cache)) { $inputarr = $sql; $sql = $secs2cache; $secs2cache = $this->cacheSecs; } global $ADODB_INCLUDED_CSV; if (empty($ADODB_INCLUDED_CSV)) include_once(ADODB_DIR.'/adodb-csvlib.inc.php'); if (is_array($sql)) $sql = $sql[0]; $md5file = $this->_gencachename($sql.serialize($inputarr),true); $err = ''; if ($secs2cache > 0){ $rs = &csv2rs($md5file,$err,$secs2cache); $this->numCacheHits += 1; } else { $err='Timeout 1'; $rs = false; $this->numCacheMisses += 1; } if (!$rs) { // no cached rs found if ($this->debug) { if (get_magic_quotes_runtime()) { ADOConnection::outp("Please disable magic_quotes_runtime - it corrupts cache files :("); } if ($this->debug !== -1) ADOConnection::outp( " $md5file cache failure: $err (see sql below)"); } $rs = &$this->Execute($sql,$inputarr); if ($rs) { $eof = $rs->EOF; $rs = &$this->_rs2rs($rs); // read entire recordset into memory immediately $txt = _rs2serialize($rs,false,$sql); // serialize if (!adodb_write_file($md5file,$txt,$this->debug)) { if ($fn = $this->raiseErrorFn) { $fn($this->databaseType,'CacheExecute',-32000,"Cache write error",$md5file,$sql,$this); } if ($this->debug) ADOConnection::outp( " Cache write error"); } if ($rs->EOF && !$eof) { $rs->MoveFirst(); //$rs = &csv2rs($md5file,$err); $rs->connection = &$this; // Pablo suggestion } } else @unlink($md5file); } else { $this->_errorMsg = ''; $this->_errorCode = 0; if ($this->fnCacheExecute) { $fn = $this->fnCacheExecute; $fn($this, $secs2cache, $sql, $inputarr); } // ok, set cached object found $rs->connection = &$this; // Pablo suggestion if ($this->debug){ global $HTTP_SERVER_VARS; $inBrowser = isset($HTTP_SERVER_VARS['HTTP_USER_AGENT']); $ttl = $rs->timeCreated + $secs2cache - time(); $s = is_array($sql) ? $sql[0] : $sql; if ($inBrowser) $s = '<i>'.htmlspecialchars($s).'</i>'; ADOConnection::outp( " $md5file reloaded, ttl=$ttl [ $s ]"); } } return $rs; }
/** * Execute SQL, caching recordsets. * * @param [secs2cache] seconds to cache data, set to 0 to force query. * This is an optional parameter. * @param sql SQL statement to execute * @param [inputarr] holds the input data to bind to * @return RecordSet or false */ function CacheExecute($secs2cache, $sql = false, $inputarr = false) { if (!is_numeric($secs2cache)) { $inputarr = $sql; $sql = $secs2cache; $secs2cache = $this->cacheSecs; } if (is_array($sql)) { $sqlparam = $sql; $sql = $sql[0]; } else { $sqlparam = $sql; } if ($this->memCache) { global $ADODB_INCLUDED_MEMCACHE; if (empty($ADODB_INCLUDED_MEMCACHE)) { include ADODB_DIR . '/adodb-memcache.lib.inc.php'; } $md5file = $this->_gencachename($sql . serialize($inputarr), false, true); } else { global $ADODB_INCLUDED_CSV; if (empty($ADODB_INCLUDED_CSV)) { include ADODB_DIR . '/adodb-csvlib.inc.php'; } $md5file = $this->_gencachename($sql . serialize($inputarr), true); } $err = ''; if ($secs2cache > 0) { if ($this->memCache) { $rs = getmemCache($md5file, $err, $secs2cache, $this->memCacheHost, $this->memCachePort); } else { $rs = csv2rs($md5file, $err, $secs2cache, $this->arrayClass); } $this->numCacheHits += 1; } else { $err = 'Timeout 1'; $rs = false; $this->numCacheMisses += 1; } if (!$rs) { // no cached rs found if ($this->debug) { if (get_magic_quotes_runtime() && !$this->memCache) { ADOConnection::outp("Please disable magic_quotes_runtime - it corrupts cache files :("); } if ($this->debug !== -1) { ADOConnection::outp(" {$md5file} cache failure: {$err} (see sql below)"); } } $rs = $this->Execute($sqlparam, $inputarr); if ($rs && $this->memCache) { $rs = $this->_rs2rs($rs); // read entire recordset into memory immediately if (!putmemCache($md5file, $rs, $this->memCacheHost, $this->memCachePort, $this->memCacheCompress, $this->debug)) { if ($fn = $this->raiseErrorFn) { $fn($this->databaseType, 'CacheExecute', -32000, "Cache write error", $md5file, $sql, $this); } if ($this->debug) { ADOConnection::outp(" Cache write error"); } } } else { if ($rs) { $eof = $rs->EOF; $rs = $this->_rs2rs($rs); // read entire recordset into memory immediately $txt = _rs2serialize($rs, false, $sql); // serialize if (!adodb_write_file($md5file, $txt, $this->debug)) { if ($fn = $this->raiseErrorFn) { $fn($this->databaseType, 'CacheExecute', -32000, "Cache write error", $md5file, $sql, $this); } if ($this->debug) { ADOConnection::outp(" Cache write error"); } } if ($rs->EOF && !$eof) { $rs->MoveFirst(); //$rs = csv2rs($md5file,$err); $rs->connection = $this; // Pablo suggestion } } else { if (!$this->memCache) { @unlink($md5file); } } } } else { $this->_errorMsg = ''; $this->_errorCode = 0; if ($this->fnCacheExecute) { $fn = $this->fnCacheExecute; $fn($this, $secs2cache, $sql, $inputarr); } // ok, set cached object found $rs->connection = $this; // Pablo suggestion if ($this->debug) { $inBrowser = isset($_SERVER['HTTP_USER_AGENT']); $ttl = $rs->timeCreated + $secs2cache - time(); $s = is_array($sql) ? $sql[0] : $sql; if ($inBrowser) { $s = '<i>' . htmlspecialchars($s) . '</i>'; } ADOConnection::outp(" {$md5file} reloaded, ttl={$ttl} [ {$s} ]"); } } return $rs; }
/** * Execute SQL, caching recordsets. * * @param secs2cache seconds to cache data, set to 0 to force query * @param sql SQL statement to execute * @param [inputarr] holds the input data to bind to * @param [arg3] reserved for john lim for future use * @return RecordSet or false */ function &CacheExecute($secs2cache, $sql, $inputarr = false, $arg3 = false) { $md5file = $this->_gencachename($sql); $err = ''; if ($secs2cache > 0) { $rs =& csv2rs($md5file, $err, $secs2cache); } else { $err = 'Timeout 1'; $rs = false; } if (!$rs) { if ($this->debug) { print " {$md5file} cache failure: {$err}<br>"; } $rs =& $this->Execute($sql, $inputarr, $arg3); if ($rs) { $eof = $rs->EOF; $rs =& $this->_rs2rs($rs); $txt =& rs2csv($rs, false, $sql); if (!adodb_write_file($md5file, $txt, $this->debug) && $this->debug) { print ' Cache write error<br>'; } if ($rs->EOF && !$eof) { $rs =& csv2rs($md5file, $err); } } else { @unlink($md5file); } } else { if ($this->debug) { $ttl = $rs->timeCreated + $secs2cache - time(); print " {$md5file} success ttl={$ttl}<br>"; } } return $rs; }
/** * Execute SQL, caching recordsets. * * @param secs2cache seconds to cache data, set to 0 to force query * @param sql SQL statement to execute * @param [inputarr] holds the input data to bind to * @param [arg3] reserved for john lim for future use * @return RecordSet or false */ function &CacheExecute($secs2cache, $sql, $inputarr = false, $arg3 = false) { include_once ADODB_DIR . '/adodb-csvlib.inc.php'; // cannot cache if $inputarr set if ($inputarr) { return $this->Execute($sql, $inputarr, $arg3); } $md5file = $this->_gencachename($sql); $err = ''; if ($secs2cache > 0) { $rs =& csv2rs($md5file, $err, $secs2cache); } else { $err = 'Timeout 1'; $rs = false; } if (!$rs) { // no cached rs found if ($this->debug) { print " {$md5file} cache failure: {$err}<br>"; } $rs =& $this->Execute($sql, $inputarr, $arg3); if ($rs) { $eof = $rs->EOF; $rs =& $this->_rs2rs($rs); $txt =& rs2csv($rs, false, $sql); if (!adodb_write_file($md5file, $txt, $this->debug) && $this->debug) { print ' Cache write error<br>'; } if ($rs->EOF && !$eof) { $rs =& csv2rs($md5file, $err); $rs->connection =& $this; // Pablo suggestion } } else { @unlink($md5file); } } else { // ok, set cached object found $rs->connection =& $this; // Pablo suggestion if ($this->debug) { $ttl = $rs->timeCreated + $secs2cache - time(); print " {$md5file} success ttl={$ttl}<br>"; } } return $rs; }
/** * Execute SQL, caching recordsets. * * @param [secs2cache] seconds to cache data, set to 0 to force query. * This is an optional parameter. * @param sql SQL statement to execute * @param [inputarr] holds the input data to bind to * @param [arg3] reserved for john lim for future use * @return RecordSet or false */ function &CacheExecute($secs2cache, $sql = false, $inputarr = false, $arg3 = false) { if (!is_numeric($secs2cache)) { $arg3 = $inputarr; $inputarr = $sql; $sql = $secs2cache; $secs2cache = $this->cacheSecs; } include_once ADODB_DIR . '/adodb-csvlib.inc.php'; $md5file = $this->_gencachename($sql . serialize($inputarr), true); $err = ''; if ($secs2cache > 0) { $rs =& csv2rs($md5file, $err, $secs2cache); $this->numCacheHits += 1; } else { $err = 'Timeout 1'; $rs = false; $this->numCacheMisses += 1; } if (!$rs) { /* no cached rs found */ if ($this->debug) { if (get_magic_quotes_runtime()) { ADOConnection::outp("Please disable magic_quotes_runtime - it corrupts cache files :("); } ADOConnection::outp(" {$md5file} cache failure: {$err} (see sql below)"); } $rs =& $this->Execute($sql, $inputarr, $arg3); if ($rs) { $eof = $rs->EOF; $rs =& $this->_rs2rs($rs); /* read entire recordset into memory immediately */ $txt = _rs2serialize($rs, false, $sql); /* serialize */ if (!adodb_write_file($md5file, $txt, $this->debug)) { if ($fn = $this->raiseErrorFn) { $fn($this->databaseType, 'CacheExecute', -32000, "Cache write error", $md5file, $sql, $this); } if ($this->debug) { ADOConnection::outp(" Cache write error"); } } if ($rs->EOF && !$eof) { $rs->MoveFirst(); /* $rs = &csv2rs($md5file,$err); */ $rs->connection =& $this; /* Pablo suggestion */ } } else { @unlink($md5file); } } else { if ($this->fnCacheExecute) { $fn = $this->fnCacheExecute; $fn($this, $secs2cache, $sql, $inputarr); } /* ok, set cached object found */ $rs->connection =& $this; /* Pablo suggestion */ if ($this->debug) { global $HTTP_SERVER_VARS; $inBrowser = isset($HTTP_SERVER_VARS['HTTP_USER_AGENT']); $ttl = $rs->timeCreated + $secs2cache - time(); $s = is_array($sql) ? $sql[0] : $sql; if ($inBrowser) { $s = '<i>' . htmlspecialchars($s) . '</i>'; } ADOConnection::outp(" {$md5file} reloaded, ttl={$ttl} [ {$s} ]"); } } return $rs; }