API CHANGE Installer now uses a database configuration helper class which isolates the logic of checking the database away from the installer, this interface can be used by other databases like MSSQL and PostgreSQL. The installer now looks for a specific file inside each database module, provided it's configured in install.php MySQL is provided by default, as it lives in sapphire (from r98786)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@102798 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-04-14 03:32:30 +00:00
parent 6bd64ad703
commit 618328ace7
2 changed files with 135 additions and 0 deletions

View File

@ -0,0 +1,42 @@
<?php
/**
* Interface for database helper classes.
* @package sapphire
*/
interface DatabaseConfigurationHelper {
/**
* Ensure that the database function for connectivity is available.
* If it is, we assume the PHP module for this database has been setup correctly.
*
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
* @return boolean
*/
public function requireDatabaseFunctions($databaseConfig);
/**
* Ensure that the database server exists.
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
* @return array Result - e.g. array('okay' => true, 'error' => 'details of error')
*/
public function requireDatabaseServer($databaseConfig);
/**
* Ensure a database connection is possible using credentials provided.
* The established connection resource is returned with the results as well.
*
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
* @return array Result - e.g. array('okay' => true, 'connection' => mysql link, 'error' => 'details of error')
*/
public function requireDatabaseConnection($databaseConfig);
/**
* Ensure that the database connection is able to use an existing database,
* or be able to create one if it doesn't exist.
*
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
* @return array Result - e.g. array('okay' => true, 'existsAlready' => 'true')
*/
public function requireDatabaseOrCreatePermissions($databaseConfig);
}

View File

@ -0,0 +1,93 @@
<?php
/**
* This is a helper class for the SS installer.
*
* It does all the specific checking for MySQLDatabase
* to ensure that the configuration is setup correctly.
*
* @package mssql
*/
class MySQLDatabaseConfigurationHelper implements DatabaseConfigurationHelper {
/**
* Ensure that the database function for connectivity is available.
* If it is, we assume the PHP module for this database has been setup correctly.
*
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
* @return boolean
*/
public function requireDatabaseFunctions($databaseConfig) {
return (function_exists('mysql_connect')) ? true : false;
}
/**
* Ensure that the database server exists.
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
* @return array Result - e.g. array('okay' => true, 'error' => 'details of error')
*/
public function requireDatabaseServer($databaseConfig) {
$okay = false;
$conn = @mysql_connect($databaseConfig['server'], null, null);
if($conn || mysql_errno() < 2000) {
$okay = true;
} else {
$okay = false;
$error = mysql_error();
}
return array(
'okay' => $okay,
'error' => $error
);
}
/**
* Ensure a database connection is possible using credentials provided.
* The established connection resource is returned with the results as well.
*
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
* @return array Result - e.g. array('okay' => true, 'connection' => mysql link, 'error' => 'details of error')
*/
public function requireDatabaseConnection($databaseConfig) {
$okay = false;
$conn = @mysql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']);
if($conn) {
$okay = true;
} else {
$okay = false;
$error = mysql_error();
}
return array(
'okay' => $okay,
'connection' => $conn,
'error' => $error
);
}
/**
* Ensure that the database connection is able to use an existing database,
* or be able to create one if it doesn't exist.
*
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
* @return array Result - e.g. array('okay' => true, 'existsAlready' => 'true')
*/
public function requireDatabaseOrCreatePermissions($databaseConfig) {
$okay = false;
$existsAlready = false;
$conn = @mysql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']);
if(@mysql_select_db($databaseConfig['database'], $conn)) {
$okay = true;
$existsAlready = true;
} else {
if(@mysql_query("CREATE DATABASE testing123", $conn)) {
mysql_query("DROP DATABASE testing123", $conn);
$okay = true;
$existsAlready = false;
}
}
return array(
'okay' => $okay,
'existsAlready' => $existsAlready
);
}
}