I would like a tiny bit help re-writing db_query to use bind variables

Events happening in the community are now at Drupal community events on www.drupal.org.
crosenblum's picture

Basically I can remember from my oracle days, of the power of bind variables.

As drupal has a lot of queries, that are repeating, with just a change in variables.

For example cache_get function which has a query like:

SELECT SQL_CACHE data, created, headers, expire, serialized FROM drupal_cache_menu WHERE cid = 'links:navigation:page-cid:node:1'

Well i have already added the SQL_CACHE statement, but in addition, i want to replace db_query, so for certain repeated queries, we can use bind variales.

http://us3.php.net/manual/en/mysqli.prepare.php has the documentation, on how to use bind variables...

so what you'll be doing in future.

is db_query_prepared("SELECT SQL_CACHE data, created, headers, expire, serialized FROM drupal_cache_menu WHERE cid = ?",$cid);

then if there are queries that are constantly repeated on a page, then we can fool the db into thinking they're the same query, but return different results.

However, I am stuck trying to figure out the logic of db_query, because I am not an oo person.

This is what i have so far. But i need help to make sure i am on right path...

/**
* bind variables version of db_query
* two parameters
* $query - is the query itself
* $qvar - is the single variable that will be using bind variables via
/
function db_query_prepared($query,$qvar){
/
Gets an array of the function's argument list. /
$args = func_get_args();
/
Shift an element off the beginning of array */
array_shift($args);

/* test $mysqli->prepare /
$mysqli = new mysqli("localhost","root","");
$stmt = $mysqli->prepare($query);
/
bind parameters for markers /
mysqli_stmt_bind_param($stmt, 'sssd', $qvar);
echo $query;
echo $stmt;
exit;
/
Append a database prefix to all tables in a query. /
$query = db_prefix_tables($query);
/
Makes sure just 1 argument - but not sure /
if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
$args = $args[0];
}
/
Helper function for db_query(). /
_db_query_callback($args, TRUE);
/
not sure exactly what this does /
$query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
/
Helper function for db_query(). */
return _db_query($query);
}

Thanks

High performance

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: