Ok, story real database server info, as well as caching it

Store it on installation too - allows us to check the db version used on installation and used currently to warn the user about incompatibilities

git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@8814 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
Meik Sievertsen 2008-09-04 12:01:47 +00:00
parent 4a225280a0
commit 2fcd96ca72
18 changed files with 199 additions and 74 deletions

View file

@ -620,7 +620,7 @@ class mysql_extractor extends base_extractor
if ($new_extract === null) if ($new_extract === null)
{ {
if ($db->sql_layer === 'mysqli' || version_compare($db->mysql_version, '3.23.20', '>=')) if ($db->sql_layer === 'mysqli' || version_compare($db->sql_server_info(true), '3.23.20', '>='))
{ {
$new_extract = true; $new_extract = true;
} }

View file

@ -265,7 +265,7 @@ class phpbb_db_tools
break; break;
case 'mysql4': case 'mysql4':
if (version_compare($this->db->mysql_version, '4.1.3', '>=')) if (version_compare($this->db->sql_server_info(true), '4.1.3', '>='))
{ {
$this->sql_layer = 'mysql_41'; $this->sql_layer = 'mysql_41';
} }

View file

@ -65,6 +65,11 @@ class dbal
var $any_char; var $any_char;
var $one_char; var $one_char;
/**
* Exact version of the DBAL, directly queried
*/
var $sql_server_version = false;
/** /**
* Constructor * Constructor
*/ */

View file

@ -37,26 +37,42 @@ class dbal_firebird extends dbal
$this->persistency = $persistency; $this->persistency = $persistency;
$this->user = $sqluser; $this->user = $sqluser;
$this->server = $sqlserver . (($port) ? ':' . $port : ''); $this->server = $sqlserver . (($port) ? ':' . $port : '');
$this->dbname = $database; $this->dbname = str_replace('\\', '/', $database);
$this->db_connect_id = ($this->persistency) ? @ibase_pconnect($this->server . ':' . $this->dbname, $this->user, $sqlpassword, false, false, 3) : @ibase_connect($this->server . ':' . $this->dbname, $this->user, $sqlpassword, false, false, 3); // There are three possibilities to connect to an interbase db
if (!$this->server)
{
$use_database = $this->dbname;
}
else if (strpos($this->server, '//') === 0)
{
$use_database = $this->server . $this->dbname;
}
else
{
$use_database = $this->server . ':' . $this->dbname;
}
$this->service_handle = (function_exists('ibase_service_attach')) ? @ibase_service_attach($this->server, $this->user, $sqlpassword) : false; $this->db_connect_id = ($this->persistency) ? @ibase_pconnect($use_database, $this->user, $sqlpassword, false, false, 3) : @ibase_connect($use_database, $this->user, $sqlpassword, false, false, 3);
$this->service_handle = (function_exists('ibase_service_attach') && $this->server) ? @ibase_service_attach($this->server, $this->user, $sqlpassword) : false;
return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error(''); return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
} }
/** /**
* Version information about used database * Version information about used database
* @param bool $raw if true, only return the fetched sql_server_version
* @return string sql server version
*/ */
function sql_server_info() function sql_server_info($raw = false)
{ {
if ($this->service_handle !== false && function_exists('ibase_server_info')) if ($this->service_handle !== false && function_exists('ibase_server_info'))
{ {
return @ibase_server_info($this->service_handle, IBASE_SVC_SERVER_VERSION); return @ibase_server_info($this->service_handle, IBASE_SVC_SERVER_VERSION);
} }
return 'Firebird/Interbase'; return ($raw) ? '2.0' : 'Firebird/Interbase';
} }
/** /**

View file

@ -62,24 +62,38 @@ class dbal_mssql extends dbal
/** /**
* Version information about used database * Version information about used database
* @param bool $raw if true, only return the fetched sql_server_version
* @return string sql server version
*/ */
function sql_server_info() function sql_server_info($raw = false)
{ {
$result_id = @mssql_query("SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')", $this->db_connect_id); global $cache;
$row = false; if (empty($cache) || ($this->sql_server_version = $cache->get('mssql_version')) === false)
if ($result_id)
{ {
$row = @mssql_fetch_assoc($result_id); $result_id = @mssql_query("SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')", $this->db_connect_id);
@mssql_free_result($result_id);
$row = false;
if ($result_id)
{
$row = @mssql_fetch_assoc($result_id);
@mssql_free_result($result_id);
}
$this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0;
if (!empty($cache))
{
$cache->put('mssql_version', $this->sql_server_version);
}
} }
if ($row) if ($raw)
{ {
return 'MSSQL<br />' . implode(' ', $row); return $this->sql_server_version;
} }
return 'MSSQL'; return ($this->sql_server_version) ? 'MSSQL<br />' . $this->sql_server_version : 'MSSQL';
} }
/** /**

View file

@ -73,24 +73,38 @@ class dbal_mssql_odbc extends dbal
/** /**
* Version information about used database * Version information about used database
* @param bool $raw if true, only return the fetched sql_server_version
* @return string sql server version
*/ */
function sql_server_info() function sql_server_info($raw = false)
{ {
$result_id = @odbc_exec($this->db_connect_id, "SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')"); global $cache;
$row = false; if (empty($cache) || ($this->sql_server_version = $cache->get('mssqlodbc_version')) === false)
if ($result_id)
{ {
$row = @odbc_fetch_array($result_id); $result_id = @odbc_exec($this->db_connect_id, "SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')");
@odbc_free_result($result_id);
$row = false;
if ($result_id)
{
$row = @odbc_fetch_array($result_id);
@odbc_free_result($result_id);
}
$this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0;
if (!empty($cache))
{
$cache->put('mssqlodbc_version', $this->sql_server_version);
}
} }
if ($row) if ($raw)
{ {
return 'MSSQL (ODBC)<br />' . implode(' ', $row); return $this->sql_server_version;
} }
return 'MSSQL (ODBC)'; return ($this->sql_server_version) ? 'MSSQL (ODBC)<br />' . $this->sql_server_version : 'MSSQL (ODBC)';
} }
/** /**

View file

@ -29,7 +29,6 @@ include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
*/ */
class dbal_mysql extends dbal class dbal_mysql extends dbal
{ {
var $mysql_version;
var $multi_insert = true; var $multi_insert = true;
/** /**
@ -52,13 +51,14 @@ class dbal_mysql extends dbal
if (@mysql_select_db($this->dbname, $this->db_connect_id)) if (@mysql_select_db($this->dbname, $this->db_connect_id))
{ {
// Determine what version we are using and if it natively supports UNICODE // Determine what version we are using and if it natively supports UNICODE
$this->mysql_version = mysql_get_server_info($this->db_connect_id); $this->sql_server_info();
if (version_compare($this->mysql_version, '4.1.3', '>=')) if (version_compare($this->sql_server_version, '4.1.3', '>='))
{ {
@mysql_query("SET NAMES 'utf8'", $this->db_connect_id); @mysql_query("SET NAMES 'utf8'", $this->db_connect_id);
// enforce strict mode on databases that support it // enforce strict mode on databases that support it
if (version_compare($this->mysql_version, '5.0.2', '>=')) if (version_compare($this->sql_server_version, '5.0.2', '>='))
{ {
$result = @mysql_query('SELECT @@session.sql_mode AS sql_mode', $this->db_connect_id); $result = @mysql_query('SELECT @@session.sql_mode AS sql_mode', $this->db_connect_id);
$row = @mysql_fetch_assoc($result); $row = @mysql_fetch_assoc($result);
@ -83,7 +83,7 @@ class dbal_mysql extends dbal
@mysql_query("SET SESSION sql_mode='{$mode}'", $this->db_connect_id); @mysql_query("SET SESSION sql_mode='{$mode}'", $this->db_connect_id);
} }
} }
else if (version_compare($this->mysql_version, '4.0.0', '<')) else if (version_compare($this->sql_server_version, '4.0.0', '<'))
{ {
$this->sql_layer = 'mysql'; $this->sql_layer = 'mysql';
} }
@ -97,10 +97,28 @@ class dbal_mysql extends dbal
/** /**
* Version information about used database * Version information about used database
* @param bool $raw if true, only return the fetched sql_server_version
* @return string sql server version
*/ */
function sql_server_info() function sql_server_info($raw = false)
{ {
return 'MySQL ' . $this->mysql_version; global $cache;
if (empty($cache) || ($this->sql_server_version = $cache->get('mysql_version')) === false)
{
$result = @mysql_query('SELECT VERSION() AS version', $this->db_connect_id);
$row = @mysql_fetch_assoc($result);
@mysql_free_result($result);
$this->sql_server_version = $row['version'];
if (!empty($cache))
{
$cache->put('mysql_version', $this->sql_server_version);
}
}
return ($raw) ? $this->sql_server_version : 'MySQL ' . $this->sql_server_version;
} }
/** /**
@ -367,13 +385,9 @@ class dbal_mysql extends dbal
if ($test_prof === null) if ($test_prof === null)
{ {
$test_prof = false; $test_prof = false;
if (strpos($this->mysql_version, 'community') !== false) if (version_compare($this->sql_server_info(true), '5.0.37', '>=') && version_compare($this->sql_server_info(true), '5.1', '<'))
{ {
$ver = substr($this->mysql_version, 0, strpos($this->mysql_version, '-')); $test_prof = true;
if (version_compare($ver, '5.0.37', '>=') && version_compare($ver, '5.1', '<'))
{
$test_prof = true;
}
} }
} }

View file

@ -45,12 +45,14 @@ class dbal_mysqli extends dbal
if ($this->db_connect_id && $this->dbname != '') if ($this->db_connect_id && $this->dbname != '')
{ {
@mysqli_query($this->db_connect_id, "SET NAMES 'utf8'"); @mysqli_query($this->db_connect_id, "SET NAMES 'utf8'");
// enforce strict mode on databases that support it // enforce strict mode on databases that support it
if (mysqli_get_server_version($this->db_connect_id) >= 50002) if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
{ {
$result = @mysqli_query($this->db_connect_id, 'SELECT @@session.sql_mode AS sql_mode'); $result = @mysqli_query($this->db_connect_id, 'SELECT @@session.sql_mode AS sql_mode');
$row = @mysqli_fetch_assoc($result); $row = @mysqli_fetch_assoc($result);
@mysqli_free_result($result); @mysqli_free_result($result);
$modes = array_map('trim', explode(',', $row['sql_mode'])); $modes = array_map('trim', explode(',', $row['sql_mode']));
// TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES // TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
@ -78,10 +80,28 @@ class dbal_mysqli extends dbal
/** /**
* Version information about used database * Version information about used database
* @param bool $raw if true, only return the fetched sql_server_version
* @return string sql server version
*/ */
function sql_server_info() function sql_server_info($raw = false)
{ {
return 'MySQL(i) ' . @mysqli_get_server_info($this->db_connect_id); global $cache;
if (empty($cache) || ($this->sql_server_version = $cache->get('mysqli_version')) === false)
{
$result = @mysqli_query($this->db_connect_id, 'SELECT VERSION() AS version');
$row = @mysqli_fetch_assoc($result);
@mysqli_free_result($result);
$this->sql_server_version = $row['version'];
if (!empty($cache))
{
$cache->put('mysqli_version', $this->sql_server_version);
}
}
return ($raw) ? $this->sql_server_version : 'MySQL(i) ' . $this->sql_server_version;
} }
/** /**

View file

@ -55,10 +55,31 @@ class dbal_oracle extends dbal
/** /**
* Version information about used database * Version information about used database
* @param bool $raw if true, only return the fetched sql_server_version
* @return string sql server version
*/ */
function sql_server_info() function sql_server_info($raw = false)
{ {
return @ociserverversion($this->db_connect_id); /*
global $cache;
if (empty($cache) || ($this->sql_server_version = $cache->get('oracle_version')) === false)
{
$result = @ociparse($this->db_connect_id, 'SELECT * FROM v$version WHERE banner LIKE \'Oracle%\'');
@ociexecute($result, OCI_DEFAULT);
@ocicommit($this->db_connect_id);
$row = array();
@ocifetchinto($result, $row, OCI_ASSOC + OCI_RETURN_NULLS);
@ocifreestatement($result);
$this->sql_server_version = trim($row['BANNER']);
$cache->put('oracle_version', $this->sql_server_version);
}
*/
$this->sql_server_version = @ociserverversion($this->db_connect_id);
return $this->sql_server_version;
} }
/** /**

View file

@ -26,7 +26,6 @@ include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
class dbal_postgres extends dbal class dbal_postgres extends dbal
{ {
var $last_query_text = ''; var $last_query_text = '';
var $pgsql_version;
/** /**
* Connect to server * Connect to server
@ -81,24 +80,7 @@ class dbal_postgres extends dbal
if ($this->db_connect_id) if ($this->db_connect_id)
{ {
// determine what version of PostgreSQL is running, we can be more efficient if they are running 8.2+ if (version_compare($this->sql_server_info(true), '8.2', '>='))
if (version_compare(PHP_VERSION, '5.0.0', '>='))
{
$this->pgsql_version = @pg_parameter_status($this->db_connect_id, 'server_version');
}
else
{
$query_id = @pg_query($this->db_connect_id, 'SELECT VERSION()');
$row = @pg_fetch_assoc($query_id, null);
@pg_free_result($query_id);
if (!empty($row['version']))
{
$this->pgsql_version = substr($row['version'], 10);
}
}
if (!empty($this->pgsql_version) && $this->pgsql_version[0] >= '8' && $this->pgsql_version[2] >= '2')
{ {
$this->multi_insert = true; $this->multi_insert = true;
} }
@ -115,10 +97,28 @@ class dbal_postgres extends dbal
/** /**
* Version information about used database * Version information about used database
* @param bool $raw if true, only return the fetched sql_server_version
* @return string sql server version
*/ */
function sql_server_info() function sql_server_info($raw = false)
{ {
return 'PostgreSQL ' . $this->pgsql_version; global $cache;
if (empty($cache) || ($this->sql_server_version = $cache->get('pgsql_version')) === false)
{
$query_id = @pg_query($this->db_connect_id, 'SELECT VERSION() AS version');
$row = @pg_fetch_assoc($query_id, null);
@pg_free_result($query_id);
$this->sql_server_version = (!empty($row['version'])) ? trim(substr($row['version'], 10)) : 0;
if (!empty($cache))
{
$cache->put('pgsql_version', $this->sql_server_version);
}
}
return ($raw) ? $this->sql_server_version : 'PostgreSQL ' . $this->sql_server_version;
} }
/** /**

View file

@ -41,18 +41,31 @@ class dbal_sqlite extends dbal
if ($this->db_connect_id) if ($this->db_connect_id)
{ {
@sqlite_query('PRAGMA short_column_names = 1', $this->db_connect_id); @sqlite_query('PRAGMA short_column_names = 1', $this->db_connect_id);
// @sqlite_query('PRAGMA encoding = "UTF-8"', $this->db_connect_id);
} }
return ($this->db_connect_id) ? true : array('message' => $error); return ($this->db_connect_id) ? true : array('message' => $error);
} }
/** /**
* Version information about used database * Version information about used database
* @param bool $raw if true, only return the fetched sql_server_version
* @return string sql server version
*/ */
function sql_server_info() function sql_server_info($raw = false)
{ {
return 'SQLite ' . @sqlite_libversion(); global $cache;
if (empty($cache) || ($this->sql_server_version = $cache->get('sqlite_version')) === false)
{
$result = @sqlite_query('SELECT sqlite_version() AS version', $this->db_connect_id);
$row = @sqlite_fetch_array($result, SQLITE_ASSOC);
$this->sql_server_version = (!empty($row['version'])) ? $row['version'] : 0;
$cache->put('sqlite_version', $this->sql_server_version);
}
return ($raw) ? $this->sql_server_version : 'SQLite ' . $this->sql_server_version;
} }
/** /**

View file

@ -2616,7 +2616,7 @@ function update_foes($group_id = false, $user_id = false)
{ {
case 'mysqli': case 'mysqli':
case 'mysql4': case 'mysql4':
$sql = 'DELETE ' . (($db->sql_layer === 'mysqli' || version_compare($db->mysql_version, '4.1', '>=')) ? 'z.*' : ZEBRA_TABLE) . ' $sql = 'DELETE ' . (($db->sql_layer === 'mysqli' || version_compare($db->sql_server_info(true), '4.1', '>=')) ? 'z.*' : ZEBRA_TABLE) . '
FROM ' . ZEBRA_TABLE . ' z, ' . USER_GROUP_TABLE . ' ug FROM ' . ZEBRA_TABLE . ' z, ' . USER_GROUP_TABLE . ' ug
WHERE z.zebra_id = ug.user_id WHERE z.zebra_id = ug.user_id
AND z.foe = 1 AND z.foe = 1

View file

@ -704,7 +704,7 @@ class fulltext_mysql extends search_backend
if (!isset($this->stats['post_subject'])) if (!isset($this->stats['post_subject']))
{ {
if ($db->sql_layer == 'mysqli' || version_compare($db->mysql_version, '4.1.3', '>=')) if ($db->sql_layer == 'mysqli' || version_compare($db->sql_server_info(true), '4.1.3', '>='))
{ {
//$alter[] = 'MODIFY post_subject varchar(100) COLLATE utf8_unicode_ci DEFAULT \'\' NOT NULL'; //$alter[] = 'MODIFY post_subject varchar(100) COLLATE utf8_unicode_ci DEFAULT \'\' NOT NULL';
} }
@ -717,7 +717,7 @@ class fulltext_mysql extends search_backend
if (!isset($this->stats['post_text'])) if (!isset($this->stats['post_text']))
{ {
if ($db->sql_layer == 'mysqli' || version_compare($db->mysql_version, '4.1.3', '>=')) if ($db->sql_layer == 'mysqli' || version_compare($db->sql_server_info(true), '4.1.3', '>='))
{ {
$alter[] = 'MODIFY post_text mediumtext COLLATE utf8_unicode_ci NOT NULL'; $alter[] = 'MODIFY post_text mediumtext COLLATE utf8_unicode_ci NOT NULL';
} }

View file

@ -1710,7 +1710,7 @@ function phpbb_check_username_collisions()
break; break;
case 'mysql4': case 'mysql4':
if (version_compare($db->mysql_version, '4.1.3', '>=')) if (version_compare($db->sql_server_info(true), '4.1.3', '>='))
{ {
$map_dbms = 'mysql_41'; $map_dbms = 'mysql_41';
} }

View file

@ -552,7 +552,7 @@ switch ($db->sql_layer)
break; break;
case 'mysql4': case 'mysql4':
if (version_compare($db->mysql_version, '4.1.3', '>=')) if (version_compare($db->sql_server_info(true), '4.1.3', '>='))
{ {
$map_dbms = 'mysql_41'; $map_dbms = 'mysql_41';
} }
@ -1837,6 +1837,9 @@ function change_database_data(&$no_updates, $version)
set_config('enable_queue_trigger', '0'); set_config('enable_queue_trigger', '0');
set_config('queue_trigger_posts', '3'); set_config('queue_trigger_posts', '3');
// Not prefilling yet
set_config('dbms_version', '');
// Resync post counts // Resync post counts
$sql = 'SELECT COUNT(p.post_id) AS num_posts, u.user_id $sql = 'SELECT COUNT(p.post_id) AS num_posts, u.user_id
FROM ' . USERS_TABLE . ' u FROM ' . USERS_TABLE . ' u

View file

@ -685,7 +685,7 @@ class install_convert extends module
// Thanks MySQL, for silently converting... // Thanks MySQL, for silently converting...
case 'mysql': case 'mysql':
case 'mysql4': case 'mysql4':
if (version_compare($src_db->mysql_version, '4.1.3', '>=')) if (version_compare($src_db->sql_server_info(true), '4.1.3', '>='))
{ {
$convert->mysql_convert = true; $convert->mysql_convert = true;
} }

View file

@ -1157,7 +1157,7 @@ class install_install extends module
// If mysql is chosen, we need to adjust the schema filename slightly to reflect the correct version. ;) // If mysql is chosen, we need to adjust the schema filename slightly to reflect the correct version. ;)
if ($data['dbms'] == 'mysql') if ($data['dbms'] == 'mysql')
{ {
if (version_compare($db->mysql_version, '4.1.3', '>=')) if (version_compare($db->sql_server_info(true), '4.1.3', '>='))
{ {
$available_dbms[$data['dbms']]['SCHEMA'] .= '_41'; $available_dbms[$data['dbms']]['SCHEMA'] .= '_41';
} }
@ -1363,6 +1363,10 @@ class install_install extends module
'UPDATE ' . $data['table_prefix'] . "forums 'UPDATE ' . $data['table_prefix'] . "forums
SET forum_last_post_time = $current_time", SET forum_last_post_time = $current_time",
'UPDATE ' . $data['table_prefix'] . "config
SET config_value = '" . $db->sql_escape($db->sql_server_info(true)) . "'
WHERE config_name = 'dbms_version'",
); );
if (@extension_loaded('gd') || can_load_dll('gd')) if (@extension_loaded('gd') || can_load_dll('gd'))

View file

@ -75,6 +75,7 @@ INSERT INTO phpbb_config (config_name, config_value) VALUES ('coppa_enable', '0'
INSERT INTO phpbb_config (config_name, config_value) VALUES ('coppa_fax', ''); INSERT INTO phpbb_config (config_name, config_value) VALUES ('coppa_fax', '');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('coppa_mail', ''); INSERT INTO phpbb_config (config_name, config_value) VALUES ('coppa_mail', '');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('database_gc', '604800'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('database_gc', '604800');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('dbms_version', '');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('default_dateformat', 'D M d, Y g:i a'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('default_dateformat', 'D M d, Y g:i a');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('default_style', '1'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('default_style', '1');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('display_last_edited', '1'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('display_last_edited', '1');