--- db.inc 2011-04-11 14:50:27.740479566 -0400 +++ ../../../../fud/FUDforum/sql/mysql/db.inc 2011-04-13 15:29:42.742857745 -0400 @@ -8,8 +8,7 @@ * under the terms of the GNU General Public License as published by the * Free Software Foundation; version 2 of the License. **/ -// define('fud_query_stats', 1); - +# define('fud_query_stats', 1); if (!defined('fud_sql_lnk')) { $connect_func = $GLOBALS['FUD_OPT_1'] & 256 ? 'mysql_pconnect' : 'mysql_connect'; @@ -25,6 +24,22 @@ define('__dbtype__', 'mysql'); } +# Connect to slave if specified +if($GLOBALS['FUD_OPT_4'] & 4) { + if (!defined('fud_sql_slave_lnk')) { + $connect_func = $GLOBALS['FUD_OPT_1'] & 256 ? 'mysql_pconnect' : 'mysql_connect'; + + $conn = @$connect_func($GLOBALS['DBHOST_SLAVE_HOST'], $GLOBALS['DBHOST_USER'], $GLOBALS['DBHOST_PASSWORD']) or fud_sql_error_handler('Initiating slave '. $connect_func, mysql_error(), mysql_errno(), 'Unknown'); + define('fud_sql_slave_lnk', $conn); + @mysql_select_db($GLOBALS['DBHOST_DBNAME'], fud_sql_slave_lnk) or fud_sql_error_handler('Opening slave database '. $GLOBALS['DBHOST_DBNAME'], mysql_error(fud_sql_slave_lnk), mysql_errno(fud_sql_slave_lnk), db_version()); + if (function_exists('mysql_set_charset')) { // Requires PHP 5.2.3 and MySQL 5.0.7 or later. + mysql_set_charset('utf8'); + } else { + mysql_query('SET NAMES \'utf8\' COLLATE \'utf8_unicode_ci\'', $conn); + } + } +} + function db_version() { if (!defined('__FUD_SQL_VERSION__')) { @@ -72,7 +87,14 @@ if (!defined('fud_query_stats')) { function q($query) { - $r = mysql_query($query, fud_sql_lnk) or fud_sql_error_handler($query, mysql_error(fud_sql_lnk), mysql_errno(fud_sql_lnk), db_version()); + # Assume master DB, switch to slave if query is a SELECT + # Force master if SELECT /* USE MASTER */ + # If databases are locked, assume a transaction and use MASTER exclusively. + $db = fud_sql_lnk; + if(strtoupper(substr(trim($query), 0, 6)) == 'SELECT' && strtoupper(substr(trim($query), 0, 23)) != "SELECT /* USE MASTER */" && !db_locked()) { + $db = fud_sql_slave_lnk; + } + $r = mysql_query($query, $db) or fud_sql_error_handler($query, mysql_error($db), mysql_errno($db), db_version()); return $r; } function uq($query) @@ -83,6 +105,15 @@ } else { function q($query) { + # Assume master DB, switch to slave if query is a SELECT + # Force master if SELECT /* USE MASTER */ + # If databases are locked, assume a transaction and use MASTER exclusively. + $db = fud_sql_lnk; + $db_used = "MASTER"; + if(strtoupper(substr(trim($query), 0, 6)) == 'SELECT' && strtoupper(substr(trim($query), 0, 23)) != "SELECT /* USE MASTER */" && !db_locked()) { + $db = fud_sql_slave_lnk; + $db_used = "SLAVE"; + } if (!isset($GLOBALS['__DB_INC_INTERNALS__']['query_count'])) { $GLOBALS['__DB_INC_INTERNALS__']['query_count'] = 1; } else { @@ -94,7 +125,7 @@ } $s = microtime(true); - $result = mysql_query($query, fud_sql_lnk) or fud_sql_error_handler($query, mysql_error(fud_sql_lnk), mysql_errno(fud_sql_lnk), db_version()); + $result = mysql_query($query, $db) or fud_sql_error_handler($query, mysql_error($db), mysql_errno($db), db_version()); $e = microtime(true); $GLOBALS['__DB_INC_INTERNALS__']['last_time'] = ($e - $s); @@ -104,7 +135,7 @@ echo '
'. preg_replace('!\s+!', ' ', $query) .''; echo '
query count: '. $GLOBALS['__DB_INC_INTERNALS__']['query_count'] .' time taken: '. $GLOBALS['__DB_INC_INTERNALS__']['last_time'] .''; echo '
Affected rows: '. db_affected() .''; - echo '
total sql time: '. $GLOBALS['__DB_INC_INTERNALS__']['total_sql_time'] .''; + echo '
total sql time: '. $GLOBALS['__DB_INC_INTERNALS__']['total_sql_time'] .' ON ' . $db_used . ''; return $result; } --- admglobal.php 2011-04-11 14:50:27.640241335 -0400 +++ ../../fud/adm/admglobal.php 2011-04-13 11:29:47.244042805 -0400 @@ -228,6 +228,8 @@ print_bit_field('Use Persistent Connections', 'DBHOST_PERSIST'); print_bit_field('Use Temporary Tables', 'USE_TEMP_TABLES'); print_bit_field('Use Database for message storage', 'DB_MESSAGE_STORAGE'); + print_bit_field('Use Database Slaves', 'DBHOST_USE_SLAVE'); + print_reg_field('Database Slave Server', 'DBHOST_SLAVE_HOST'); ?>