From 170b674a0953ce95e06f7a8442eb0f1097e7ee72 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Sun, 16 Mar 2003 07:02:20 +0000 Subject: - All LIMIT queries must go through the pager or through db_query_range(). The syntax for db_query_range() was enhanced so it matches db_query(). So you may pass extra arguments of the SQL statement which are checked via check_query() and then substituted into the SQL statement. After these optional arguments, you always pass $from and $count parameters which define your range. Most often, the $from is 0 and the count is the max number of records you want returned. Patch by Moshe. - The pager_query() function for PEAR was enhanced so that it adds proper GROUP BY statement counting the number of records to be paged. Patch by James Arthur. - MSSQL database scheme by Moshe. --- includes/database.pear.inc | 62 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 11 deletions(-) (limited to 'includes/database.pear.inc') diff --git a/includes/database.pear.inc b/includes/database.pear.inc index 4a5faef9b..0b56efcdf 100644 --- a/includes/database.pear.inc +++ b/includes/database.pear.inc @@ -23,6 +23,7 @@ function db_connect($url) { * @return sql result resource */ function db_query($query) { + $args = func_get_args(); if (count($args) > 1) { $args = array_map("check_query", $args); @@ -52,11 +53,19 @@ function _db_query($query, $debug = 0) { global $db_handle, $queries; if (variable_get("dev_query", 0)) { - $queries[] = $query; + list($usec, $sec) = explode(" ", microtime()); + $timer = (float)$usec + (float)$sec; } $result = $db_handle->query($query); + if (variable_get("dev_query", 0)) { + list($usec, $sec) = explode(" ", microtime()); + $stop = (float)$usec + (float)$sec; + $diff = $stop - $timer; + $queries[] = array($query, $diff); + } + if ($debug) { print "

query: $query

"; } @@ -103,7 +112,13 @@ function db_error() { function db_next_id($name) { global $db_handle; - return $db_handle->nextID($name); + $result = $db_handle->nextID($name); + if (DB::isError($result)) { + watchdog("error", "database: ". $result->getMessage() ."\nquery: ". htmlspecialchars($query)); + } + else { + return $result; + } } function db_affected_rows() { @@ -113,21 +128,46 @@ function db_affected_rows() { } /** - * Generates a limited query - * - * @param string $query query - * @param integer $from the row to start to fetching - * @param integer $count the numbers of rows to fetch + * Runs a LIMIT query in the database. * + * @param $query sql query followed by 'from' and 'count' parameters, followed by a variable number of arguments which are substituted into query by sprintf. 'from' is the row to start to fetching. 'count' the numbers of rows to fetch. * @return mixed a DB_Result object or a DB_Error * * @access public */ +function db_query_range($query) { + global $db_handle, $queries; -function db_query_range($query, $from, $count) { - global $db_handle; + if (variable_get("dev_query", 0)) { + list($usec, $sec) = explode(" ", microtime()); + $timer = (float)$usec + (float)$sec; + } - return $db_handle->limitQuery($query, $from, $count); + $args = func_get_args(); + $count = array_pop($args); + $from = array_pop($args); + if (count(func_get_args()) > 3) { + $args = array_map("check_query", $args); + $args[0] = $query; + $result = $db_handle->limitQuery(call_user_func_array("sprintf", $args), $from, $count); + } + else { + $result = $db_handle->limitQuery(func_get_arg(0), $from, $count); + } + + if (variable_get("dev_query", 0)) { + list($usec, $sec) = explode(" ", microtime()); + $stop = (float)$usec + (float)$sec; + $diff = $stop - $timer; + $queries[] = array($query. " [LIMIT $from, $count]", $diff); + } + + if (DB::isError($result)) { + watchdog("error", "database: ". $result->getMessage() ."\nquery: ". htmlspecialchars($query)); + } + else { + return $result; + } } -?> +?> \ No newline at end of file -- cgit v1.2.3