Only in httpdocs/: files Only in httpdocs/: i147160.patch diff -urp /var/www/vhosts/ddev.fourkitchens.com/private/drupal-5.1/includes/bootstrap.inc httpdocs/includes/bootstrap.inc --- /var/www/vhosts/ddev.fourkitchens.com/private/drupal-5.1/includes/bootstrap.inc 2007-01-15 05:52:02.000000000 -0600 +++ httpdocs/includes/bootstrap.inc 2007-07-10 19:14:00.000000000 -0500 @@ -233,7 +233,7 @@ function drupal_unset_globals() { * Loads the configuration and sets the base URL correctly. */ function conf_init() { - global $db_url, $db_prefix, $base_url, $base_path, $base_root, $conf, $installed_profile; + global $db_url, $db_slave_url, $db_prefix, $base_url, $base_path, $base_root, $conf, $installed_profile; $conf = array(); include_once './'. conf_path() .'/settings.php'; diff -urp /var/www/vhosts/ddev.fourkitchens.com/private/drupal-5.1/includes/database.inc httpdocs/includes/database.inc --- /var/www/vhosts/ddev.fourkitchens.com/private/drupal-5.1/includes/database.inc 2007-01-03 04:59:02.000000000 -0600 +++ httpdocs/includes/database.inc 2007-07-10 19:25:37.000000000 -0500 @@ -99,16 +99,35 @@ function db_prefix_tables($sql) { * @return the name of the previously active database or FALSE if non was found. */ function db_set_active($name = 'default') { - global $db_url, $db_type, $active_db; - static $db_conns; + global $db_url, $db_slave_url, $db_type, $active_db, $active_slave_db; + static $db_conns, $db_slave_conns; if (!isset($db_conns[$name])) { - // Initiate a new connection, using the named DB URL specified. + // If the $name doesn't have a corresponding URL, use the default. + if (!isset($db_url[$name])) { + $name = 'default'; + } + + // Initiate a new connection, using the specifed DB URL. if (is_array($db_url)) { - $connect_url = array_key_exists($name, $db_url) ? $db_url[$name] : $db_url['default']; + $connect_url = $db_url[$name]; + if (is_array($db_slave_url[$name])) { + $slave_index = mt_rand(0, count($db_slave_url[$name])); + $slave_connect_url = $db_slave_url[$name][$slave_index]; + } + else { + $slave_connect_url = $db_slave_url[$name]; + } } else { $connect_url = $db_url; + if (is_array($db_slave_url)) { + $slave_index = mt_rand(0, count($db_slave_url) - 1); + $slave_connect_url = $db_slave_url[$slave_index]; + } + else { + $slave_connect_url = $db_slave_url; + } } $db_type = substr($connect_url, 0, strpos($connect_url, '://')); @@ -126,11 +145,20 @@ function db_set_active($name = 'default' } $db_conns[$name] = db_connect($connect_url); + if (!empty($slave_connect_url)) { + $db_slave_conns[$name] = db_connect($slave_connect_url); + } } $previous_db = $active_db; - // Set the active connection. + // Set the active connections. $active_db = $db_conns[$name]; + if (isset($db_slave_conns[$name])) { + $active_slave_db = $db_slave_conns[$name]; + } + else { + unset($active_slave_db); + } return array_search($previous_db, $db_conns); } @@ -201,6 +229,42 @@ function db_query($query) { } /** + * Runs a basic query in the active slave 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. Instead of a variable number of query arguments, + * you may also pass a single array containing the query arguments. + + * Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose + * in '') and %%. + * + * NOTE: using this syntax will cast NULL and FALSE values to decimal 0, + * and TRUE values to decimal 1. + * + * @return + * A database query result resource, or FALSE if the query was not + * executed correctly. + */ +function db_query_slave($query) { + $args = func_get_args(); + array_shift($args); + $query = db_prefix_tables($query); + if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax + $args = $args[0]; + } + _db_query_callback($args, TRUE); + $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query); + return _db_query($quer, FALSE, TRUE); +} + +/** * Helper function for db_rewrite_sql. * * Collects JOIN and WHERE statements via hook_sql. diff -urp /var/www/vhosts/ddev.fourkitchens.com/private/drupal-5.1/includes/database.mysqli.inc httpdocs/includes/database.mysqli.inc --- /var/www/vhosts/ddev.fourkitchens.com/private/drupal-5.1/includes/database.mysqli.inc 2006-12-27 16:50:09.000000000 -0600 +++ httpdocs/includes/database.mysqli.inc 2007-07-10 20:33:12.000000000 -0500 @@ -121,15 +121,22 @@ function db_connect($url) { /** * Helper function for db_query(). */ -function _db_query($query, $debug = 0) { - global $active_db, $queries; +function _db_query($query, $debug = 0, $slave = FALSE) { + global $active_db, $active_slave_db, $queries; if (variable_get('dev_query', 0)) { list($usec, $sec) = explode(' ', microtime()); $timer = (float)$usec + (float)$sec; } - $result = mysqli_query($active_db, $query); + $sent_to_slave = FALSE; + if (isset($active_slave_db) && $slave) { + $result = mysqli_query($active_slave_db, $query); + $sent_to_slave = TRUE; + } + else { + $result = mysqli_query($active_db, $query); + } if (variable_get('dev_query', 0)) { $bt = debug_backtrace(); @@ -137,7 +144,7 @@ function _db_query($query, $debug = 0) { list($usec, $sec) = explode(' ', microtime()); $stop = (float)$usec + (float)$sec; $diff = $stop - $timer; - $queries[] = array($query, $diff); + $queries[] = array($query, $diff, $sent_to_slave); } if ($debug) { @@ -301,6 +308,50 @@ function db_query_range($query) { } /** + * Runs a limited-range query in the active slave database. + * + * Use this as a substitute for db_query_slave() when a subset of the query is to be + * returned. + * 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. The query arguments can be enclosed in one + * array instead. + * Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose + * in '') and %%. + * + * NOTE: using this syntax will cast NULL and FALSE values to decimal 0, + * and TRUE values to decimal 1. + * + * @param $from + * The first result row to return. + * @param $count + * The maximum number of result rows to return. + * @return + * A database query result resource, or FALSE if the query was not executed + * correctly. + */ +function db_query_range_slave($query) { + $args = func_get_args(); + $count = array_pop($args); + $from = array_pop($args); + array_shift($args); + + $query = db_prefix_tables($query); + if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax + $args = $args[0]; + } + _db_query_callback($args, TRUE); + $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query); + $query .= ' LIMIT '. (int)$from .', '. (int)$count; + return _db_query($query, FALSE, TRUE); +} + +/** * Runs a SELECT query and stores its results in a temporary table. * * Use this as a substitute for db_query() when the results need to stored diff -urp /var/www/vhosts/ddev.fourkitchens.com/private/drupal-5.1/includes/database.mysql.inc httpdocs/includes/database.mysql.inc --- /var/www/vhosts/ddev.fourkitchens.com/private/drupal-5.1/includes/database.mysql.inc 2007-01-21 20:20:50.000000000 -0600 +++ httpdocs/includes/database.mysql.inc 2007-07-10 20:32:28.000000000 -0500 @@ -142,23 +142,30 @@ function db_connect($url) { /** * Helper function for db_query(). */ -function _db_query($query, $debug = 0) { - global $active_db, $queries; +function _db_query($query, $debug = 0, $slave = FALSE) { + global $active_db, $active_slave_db, $queries; if (variable_get('dev_query', 0)) { list($usec, $sec) = explode(' ', microtime()); $timer = (float)$usec + (float)$sec; } - $result = mysql_query($query, $active_db); - + $sent_to_slave = FALSE; + if (isset($active_slave_db) && $slave) { + $result = mysql_query($active_slave_db, $query); + $sent_to_slave = TRUE; + } + else { + $result = mysql_query($active_db, $query); + } + if (variable_get('dev_query', 0)) { $bt = debug_backtrace(); $query = $bt[2]['function'] . "\n" . $query; list($usec, $sec) = explode(' ', microtime()); $stop = (float)$usec + (float)$sec; $diff = $stop - $timer; - $queries[] = array($query, $diff); + $queries[] = array($query, $diff, $sent_to_slave); } if ($debug) { @@ -321,6 +328,50 @@ function db_query_range($query) { } /** + * Runs a limited-range query in the active slave database. + * + * Use this as a substitute for db_query_slave() when a subset of the query is to be + * returned. + * 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. The query arguments can be enclosed in one + * array instead. + * Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose + * in '') and %%. + * + * NOTE: using this syntax will cast NULL and FALSE values to decimal 0, + * and TRUE values to decimal 1. + * + * @param $from + * The first result row to return. + * @param $count + * The maximum number of result rows to return. + * @return + * A database query result resource, or FALSE if the query was not executed + * correctly. + */ +function db_query_range_slave($query) { + $args = func_get_args(); + $count = array_pop($args); + $from = array_pop($args); + array_shift($args); + + $query = db_prefix_tables($query); + if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax + $args = $args[0]; + } + _db_query_callback($args, TRUE); + $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query); + $query .= ' LIMIT '. (int)$from .', '. (int)$count; + return _db_query($query, FALSE, TRUE); +} + +/** * Runs a SELECT query and stores its results in a temporary table. * * Use this as a substitute for db_query() when the results need to stored diff -urp /var/www/vhosts/ddev.fourkitchens.com/private/drupal-5.1/includes/database.pgsql.inc httpdocs/includes/database.pgsql.inc --- /var/www/vhosts/ddev.fourkitchens.com/private/drupal-5.1/includes/database.pgsql.inc 2006-12-27 16:13:56.000000000 -0600 +++ httpdocs/includes/database.pgsql.inc 2007-07-10 20:32:52.000000000 -0500 @@ -114,15 +114,22 @@ function db_connect($url) { /** * Helper function for db_query(). */ -function _db_query($query, $debug = 0) { - global $active_db, $last_result, $queries; +function _db_query($query, $debug = 0, $slave = FALSE) { + global $active_db, $active_slave_db, $last_result, $queries; if (variable_get('dev_query', 0)) { list($usec, $sec) = explode(' ', microtime()); $timer = (float)$usec + (float)$sec; } - $last_result = pg_query($active_db, $query); + $sent_to_slave = FALSE; + if (isset($active_slave_db) && $slave) { + $last_result = pg_query($active_slave_db, $query); + $sent_to_slave = TRUE; + } + else { + $last_result = pg_query($active_db, $query); + } if (variable_get('dev_query', 0)) { $bt = debug_backtrace(); @@ -130,7 +137,7 @@ function _db_query($query, $debug = 0) { list($usec, $sec) = explode(' ', microtime()); $stop = (float)$usec + (float)$sec; $diff = $stop - $timer; - $queries[] = array($query, $diff); + $queries[] = array($query, $diff, $sent_to_slave); } if ($debug) { @@ -291,6 +298,51 @@ function db_query_range($query) { } /** + * Runs a limited-range query in the active slave database. + * + * Use this as a substitute for db_query_slave() when a subset of the query + * is to be returned. + * 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. Instead of a variable number of query arguments, + * you may also pass a single array containing the query arguments. + * Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose + * in '') and %%. + * + * NOTE: using this syntax will cast NULL and FALSE values to decimal 0, + * and TRUE values to decimal 1. + * + * @param $from + * The first result row to return. + * @param $count + * The maximum number of result rows to return. + * @return + * A database query result resource, or FALSE if the query was not executed + * correctly. + */ +function db_query_range_slave($query) { + $args = func_get_args(); + $count = array_pop($args); + $from = array_pop($args); + array_shift($args); + + $query = db_prefix_tables($query); + if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax + $args = $args[0]; + } + _db_query_callback($args, TRUE); + $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query); + $query .= ' LIMIT '. (int)$count .' OFFSET '. (int)$from; + return _db_query($query, FALSE, TRUE); +} + +/** * Runs a SELECT query and stores its results in a temporary table. * * Use this as a substitute for db_query() when the results need to stored