BUGFIX Installer should cache the database connection so queries testing database creation can be made properly (from r98744)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/phpinstaller/trunk@112095 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
sminnee 2010-10-13 02:42:14 +00:00
parent 5663e264e1
commit 42144c288d

View File

@ -194,7 +194,7 @@ if((isset($_REQUEST['go']) || $installFromCli) && !$req->hasErrors() && !$dbReq-
*/ */
class InstallRequirements { class InstallRequirements {
var $errors, $warnings, $tests; var $errors, $warnings, $tests, $dbConn;
/** /**
* Just check that the database configuration is okay * Just check that the database configuration is okay
@ -611,11 +611,15 @@ class InstallRequirements {
} }
} }
/**
* The established database connection is cached to $dbConn on the instance
* of this class so various queries can be run via {@link requireDatabaseOrCreatePermissions())
*/
function requireDatabaseConnection($databaseConfig, $testDetails) { function requireDatabaseConnection($databaseConfig, $testDetails) {
$this->testing($testDetails); $this->testing($testDetails);
if($databaseConfig['type'] == 'MySQLDatabase') { if($databaseConfig['type'] == 'MySQLDatabase') {
$conn = @mysql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']); $this->dbConn = $conn = @mysql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']);
if($conn || mysql_errno() < 2000) { if($conn || mysql_errno() < 2000) {
return true; return true;
} else { } else {
@ -624,13 +628,13 @@ class InstallRequirements {
} }
} elseif($databaseConfig['type'] == 'MSSQLDatabase') { } elseif($databaseConfig['type'] == 'MSSQLDatabase') {
if(function_exists('mssql_connect')) { if(function_exists('mssql_connect')) {
$conn = @mssql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password'], true); $this->dbConn = $conn = @mssql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password'], true);
} else { } else {
$connectionInfo = array( $connectionInfo = array(
'UID' => $databaseConfig['username'], 'UID' => $databaseConfig['username'],
'PWD' => $databaseConfig['password'], 'PWD' => $databaseConfig['password'],
); );
$conn = @sqlsrv_connect($databaseConfig['server'], $connectionInfo); $this->dbConn = $conn = @sqlsrv_connect($databaseConfig['server'], $connectionInfo);
} }
if($conn) { if($conn) {
@ -645,7 +649,7 @@ class InstallRequirements {
$userPart = $username ? " user=$username" : ''; $userPart = $username ? " user=$username" : '';
$passwordPart = $password ? " password=$password" : ''; $passwordPart = $password ? " password=$password" : '';
$connstring = "host=$server port=5432 {$userPart}{$passwordPart}"; $connstring = "host=$server port=5432 {$userPart}{$passwordPart}";
$conn = @pg_connect($connstring); $this->dbConn = $conn = @pg_connect($connstring);
if($conn) { if($conn) {
return true; return true;
@ -728,11 +732,11 @@ class InstallRequirements {
if($databaseConfig['type'] == 'MySQLDatabase') { if($databaseConfig['type'] == 'MySQLDatabase') {
$conn = @mysql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']); $conn = @mysql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']);
if(@mysql_select_db($databaseConfig['database'])) { if(@mysql_select_db($databaseConfig['database'], $this->dbConn)) {
$okay = "Database '$databaseConfig[database]' exists"; $okay = "Database '$databaseConfig[database]' exists";
} else { } else {
if(@mysql_query("CREATE DATABASE testing123")) { if(@mysql_query("CREATE DATABASE testing123", $this->dbConn)) {
mysql_query("DROP DATABASE testing123"); mysql_query("DROP DATABASE testing123", $this->dbConn);
$okay = "Able to create a new database"; $okay = "Able to create a new database";
} }
} }
@ -740,11 +744,11 @@ class InstallRequirements {
if(@mysql_select_db($databaseConfig['database'])) { if(@mysql_select_db($databaseConfig['database'])) {
$okay = "Database '$databaseConfig[database]' exists"; $okay = "Database '$databaseConfig[database]' exists";
} else { } else {
if(function_exists('mssql_connect') && @mssql_query("CREATE DATABASE testing123")) { if(function_exists('mssql_connect') && @mssql_query("CREATE DATABASE testing123", $this->dbConn)) {
mssql_query("DROP DATABASE testing123"); mssql_query($this->dbConn, "DROP DATABASE testing123");
$okay = "Able to create a new database"; $okay = "Able to create a new database";
} elseif(function_exists('sqlsrv_connect') && @sqlsrv_query("CREATE DATABASE testing123")) { } elseif(function_exists('sqlsrv_connect') && @sqlsrv_query($this->dbConn, "CREATE DATABASE testing123")) {
sqlsrv_query("DROP DATABASE testing123"); sqlsrv_query($this->dbConn, "DROP DATABASE testing123");
$okay = "Able to create a new database"; $okay = "Able to create a new database";
} }
} }
@ -752,14 +756,14 @@ class InstallRequirements {
$database = $databaseConfig['database']; $database = $databaseConfig['database'];
$dbExists = false; $dbExists = false;
$result = pg_query("SELECT datname FROM pg_database WHERE datname = '$database'"); $result = pg_query($this->dbConn, "SELECT datname FROM pg_database WHERE datname = '$database'");
if(pg_fetch_array($result)) $dbExists = true; if(pg_fetch_array($result)) $dbExists = true;
if($dbExists) { if($dbExists) {
$okay = "Database '$database' exists"; $okay = "Database '$database' exists";
} else { } else {
if(@pg_query("CREATE DATABASE testing123")) { if(@pg_query($this->dbConn, "CREATE DATABASE testing123")) {
pg_query("DROP DATABASE testing123"); pg_query($this->dbConn, "DROP DATABASE testing123");
$okay = "Able to create a new database"; $okay = "Able to create a new database";
} }
} }