diff options
author | Dries Buytaert <dries@buytaert.net> | 2004-11-29 13:13:29 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2004-11-29 13:13:29 +0000 |
commit | 29337ad8bbcf9388cf41e88193764cc2cb8b87f2 (patch) | |
tree | 5d7f97669f40ee1345a6c824ff33a9e2575b45c5 /includes/database.pgsql.inc | |
parent | e35137a5ad5d98c74777fc49bbd586ce1d0627c9 (diff) | |
download | brdo-29337ad8bbcf9388cf41e88193764cc2cb8b87f2.tar.gz brdo-29337ad8bbcf9388cf41e88193764cc2cb8b87f2.tar.bz2 |
- Patch #13581 by Steven: Db_query() allows a variable amount of parameters so you can pass the query arguments in. There is however an alternative syntax: instead of passing the query arguments as function arguments, you can also pass a single array with the query arguments in it. For example the following two statements are equivalent:
db_query($query, $a, $b, $c);
db_query($query, array($a, $b, $c));
This usage is particularly interesting when the query is constructed dynamically, and the amount of arguments to pass varies. In that case we use the second method to avoid using call_user_func_array(). This behaviour is not documented explicitly, but it is used in several places.
However, db_query_range() and pager_query() do not support this syntax properly, which means there are several pieces of code which still revert to the ugly call_user_func_array() call.
This patch updates db_query_range() and pager_query() so they support the array-passing method. I also added documentation about this method to each of the db functions.
I also cleaned up the code for db_query (it was weird and hard to understand) and moved db_query() and db_queryd() from database.xxxxx.inc to database.inc: it was the same between both mysql and pgsql, as it doesn't do anything database specific. It just prefixes the tables and inserts the arguments. The actual db query is performed in _db_query(), which is still in database.xxxxx.inc.
Finally, I updated several places with the new syntax, and the code is a lot cleaner. For example:
- array_unshift($params, "SELECT u.* FROM {users} u WHERE $query u.status < 3");
- $params[] = 0;
- $params[] = 1;
- $result = call_user_func_array('db_query_range', $params);
+ $result = db_query_range("SELECT u.* FROM {users} u WHERE $query u.status < 3", $params, 0, 1);
and
- return call_user_func_array('db_query_range', array_merge(array($query), $args, array((int)$pager_from_array[$element], (int)$limit)));
+ return db_query_range($query, $args, (int)$pager_from_array[$element], (int)$limit);
I've tested it on mysql. I didn't alter the actual db behaviour, so pgsql should be okay too.
This patch is important because many people avoid the call_user_func_array() method and put data directly into the db query. This is very, very bad because the database prefix will be applied to it, and strip out braces. It's also generally bad form as you have to call check_query() yourself. With the new, documented syntax, there is no more excuse to put data directly in the query.
Diffstat (limited to 'includes/database.pgsql.inc')
-rw-r--r-- | includes/database.pgsql.inc | 75 |
1 files changed, 9 insertions, 66 deletions
diff --git a/includes/database.pgsql.inc b/includes/database.pgsql.inc index 2d5399018..a5c11cedd 100644 --- a/includes/database.pgsql.inc +++ b/includes/database.pgsql.inc @@ -31,65 +31,6 @@ function db_connect($url) { } /** - * Runs a basic query in the active database. - * - * User-supplied arguments to the query should be passed in as separate parameters - * so that they can be properly escaped to avoid SQL injection attacks. - * - * @param $query - * A string containing an SQL query. - * @param ... - * A variable number of arguments which are substituted into the query using - * printf() syntax. - * @return - * A database query result resource, or FALSE if the query was not executed - * correctly. - */ -function db_query($query) { - $args = func_get_args(); - - $query = db_prefix_tables($query); - if (count($args) > 1) { - if(is_array($args[1])){ - $args1 = array_map('db_escape_string', $args[1]); - $nargs = array_merge(array($query), $args1); - } - else { - $nargs = array_map('db_escape_string', $args); - $nargs[0] = $query; - } - return _db_query(call_user_func_array('sprintf', $nargs)); - } - else { - return _db_query($query); - } -} - -/** - * Debugging version of db_query(). - * - * Echoes the query to the browser. - */ -function db_queryd($query) { - $args = func_get_args(); - $query = db_prefix_tables($query); - if (count($args) > 1) { - if(is_array($args[1])){ - $args1 = array_map('db_escape_string', $args[1]); - $nargs = array_merge(array($query), $args1); - } - else { - $nargs = array_map('db_escape_string', $args); - $nargs[0] = $query; - } - return _db_query(call_user_func_array('sprintf', $nargs), 1); - } - else { - return _db_query($query, 1); - } -} - -/** * Helper function for db_query(). */ function _db_query($query, $debug = 0) { @@ -228,7 +169,8 @@ function db_affected_rows() { * A string containing an SQL query. * @param ... * A variable number of arguments which are substituted into the query using - * printf() syntax. + * printf() syntax. Instead of a variable number of query arguments, you may + * also pass a single array containing the query arguments. * @param $from * The first result row to return. * @param $count @@ -241,16 +183,17 @@ function db_query_range($query) { $args = func_get_args(); $count = array_pop($args); $from = array_pop($args); + + $query = db_prefix_tables($query); if (count(func_get_args()) > 3) { + // Check for array (alternative syntax). + if (is_array($args[1])) { + $args = array_merge(array($query), $args[1]); + } $args = array_map('db_escape_string', $args); - $query = db_prefix_tables($query); $args[0] = $query; $query = call_user_func_array('sprintf', $args); } - else { - $query = func_get_arg(0); - $query = db_prefix_tables($query); - } $query .= ' LIMIT '. $count .' OFFSET '. $from; return _db_query($query); } @@ -291,4 +234,4 @@ function db_escape_string($text) { * @} End of "ingroup database". */ -?> +?>
\ No newline at end of file |