function garbageCollection($maxlifetime) { $this->flushPreload(); $query = sprintf('DELETE FROM %s WHERE (expires <= %d AND expires > 0) OR changed <= (NOW() - %d)', $this->cache_table, time(), $maxlifetime); $res = dbx_query($this->db, $query); if (dbx_error($this->db)) { return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__); } $query = sprintf('select sum(length(cachedata)) as CacheSize from %s', $this->cache_table); $res = dbx_query($this->db, $query); //if cache is to big. if ($res->data[0][CacheSize] > $this->highwater) { //find the lowwater mark. $query = sprintf('select length(cachedata) as size, changed from %s order by changed DESC', $this->cache_table); $res = dbx_query($this->db, $query); $keep_size = 0; $i = 0; while ($keep_size < $this->lowwater && $i < $res->rows) { $keep_size += $res->data[$i][size]; $i++; } //delete all entries, which were changed before the "lowwwater mark" $query = sprintf('delete from %s where changed <= %s', $this->cache_table, $res->data[$i][changed]); $res = dbx_query($this->db, $query); } }
/** * Read string data from database */ function read($table, $fields, $where = '') { if (count($fields) > 0) { // Query table for data $query = 'SELECT `' . dbx_escape_string($this->handle, implode('`,`', $fields)) . '` FROM `' . dbx_escape_string($this->handle, $table) . '`;'; if (isset($where) && !empty($where) && $where != '') { // Fetch only some $query = 'SELECT `' . dbx_escape_string($this->handle, implode('`,`', $fields)) . '` FROM `' . dbx_escape_string($this->handle, $table) . '` WHERE ' . dbx_escape_string($this->handle, $where) . ';'; } echo "[QUERY]: {$query}\n"; $result = dbx_query($this->handle, $query); if (!is_object($result)) { die("Error performing READ query.<br />\nError returned: " . dbx_error($this->handle) . "\n"); } } else { die("Please select at least one field to read!\n"); } return $result; }
function jz_db_error($link) { return dbx_error($link); }
function da_sql_error($link, $config) { return dbx_error($link); }
/** * This function is a very basic query executor. It shouldn't usually be used by you, as there are abstracted versions available. * * @param string The complete SQL query * @param array A DB connection * @param ?integer The maximum number of rows to affect (NULL: no limit) * @param ?integer The start row to affect (NULL: no specification) * @param boolean Whether to output an error on failure * @param boolean Whether to get the autoincrement ID created for an insert query * @return ?mixed The results (NULL: no results), or the insert ID */ function db_query($query, $db_parts, $max = NULL, $start = NULL, $fail_ok = false, $get_insert_id = false) { list($db, ) = $db_parts; if (isset($query[500000])) { $test_result = $this->db_query('SHOW VARIABLES LIKE \'max_allowed_packet\'', $db_parts, NULL, NULL, true); if (!is_array($test_result)) { return NULL; } if (intval($test_result[0]['Value']) < intval(strlen($query) * 1.2)) { if ($get_insert_id) { fatal_exit(do_lang_tempcode('QUERY_FAILED_TOO_BIG', escape_html($query))); } return NULL; } } if ($max !== NULL && $start !== NULL) { $query .= ' LIMIT ' . strval($start) . ',' . strval($max); } elseif ($max !== NULL) { $query .= ' LIMIT ' . strval($max); } elseif ($start !== NULL) { $query .= ' LIMIT ' . strval($start) . ',30000000'; } $results = @dbx_query($db, $query, DBX_RESULT_INFO); if ($results === 0 && (!$fail_ok || strpos(dbx_error($db), 'is marked as crashed and should be repaired') !== false)) { $err = dbx_error($db); if (function_exists('ocp_mark_as_escaped')) { ocp_mark_as_escaped($err); } if (!running_script('upgrader') && get_page_name() != 'admin_import') { if (!function_exists('do_lang') || is_null(do_lang('QUERY_FAILED', NULL, NULL, NULL, NULL, false))) { fatal_exit(htmlentities('Query failed: ' . $query . ' : ' . $err)); } fatal_exit(do_lang_tempcode('QUERY_FAILED', escape_html($query), $err)); } else { echo htmlentities('Database query failed: ' . $query . ' [') . $err . htmlentities(']' . '<br />' . chr(10)); return NULL; } } if (is_object($results) && (strtoupper(substr($query, 0, 7)) == 'SELECT ' || strtoupper(substr($query, 0, 8)) == '(SELECT ' || strtoupper(substr($query, 0, 8)) == 'EXPLAIN ' || strtoupper(substr($query, 0, 9)) == 'DESCRIBE ' || strtoupper(substr($query, 0, 5)) == 'SHOW ')) { return $this->db_get_query_rows($results); } if ($get_insert_id) { if (strtoupper(substr($query, 0, 7)) == 'UPDATE ') { if (function_exists('mysql_affected_rows')) { return mysql_affected_rows($db->handle); } else { return -1; } } //return mysql_insert_id($db->handle); if (strtoupper(substr($query, 0, 12)) == 'INSERT INTO ') { $table = substr($query, 12, strpos($query, ' ', 12) - 12); $rows = $this->db_query('SELECT MAX(id) AS x FROM ' . $table, $db_parts, 1, 0, false, false); return $rows[0]['x']; } } return NULL; }