mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
pkrenn: Various database updates (merged from branches/gsoc)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@41704 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
713802622b
commit
f0b9103de4
@ -30,7 +30,7 @@ class DB {
|
|||||||
*/
|
*/
|
||||||
static function setConn($globalConn) {
|
static function setConn($globalConn) {
|
||||||
DB::$globalConn = $globalConn;
|
DB::$globalConn = $globalConn;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the global database connection.
|
* Get the global database connection.
|
||||||
@ -48,11 +48,24 @@ class DB {
|
|||||||
*/
|
*/
|
||||||
static function connect($databaseConfig) {
|
static function connect($databaseConfig) {
|
||||||
if(!$databaseConfig['type']) user_error("DB::connect: Not passed a valid database config", E_USER_ERROR);
|
if(!$databaseConfig['type']) user_error("DB::connect: Not passed a valid database config", E_USER_ERROR);
|
||||||
$dbClass = $databaseConfig['type'];
|
if ($databaseConfig['pdo']) { // TODO:pkrenn_remove
|
||||||
$conn = new $dbClass($databaseConfig);
|
$conn = new PDODatabase($databaseConfig);
|
||||||
|
} else { // TODO:pkrenn_remove begin
|
||||||
|
$dbClass = $databaseConfig['type'];
|
||||||
|
$conn = new $dbClass($databaseConfig);
|
||||||
|
} // TODO:pkrenn_remove end
|
||||||
DB::setConn($conn);
|
DB::setConn($conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the connection string from input.
|
||||||
|
* @param array $parameters The connection details.
|
||||||
|
* @return string $connect The connection string.
|
||||||
|
**/
|
||||||
|
public function getConnect($parameters) {
|
||||||
|
return DB::$globalConn->getConnect($parameters);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the given SQL query.
|
* Execute the given SQL query.
|
||||||
* @param string $sql The SQL query to execute
|
* @param string $sql The SQL query to execute
|
||||||
@ -106,10 +119,14 @@ class DB {
|
|||||||
* Create the database and connect to it. This can be called if the
|
* Create the database and connect to it. This can be called if the
|
||||||
* initial database connection is not successful because the database
|
* initial database connection is not successful because the database
|
||||||
* does not exist.
|
* does not exist.
|
||||||
|
* @param string $connect Connection string
|
||||||
|
* @param string $username Database username
|
||||||
|
* @param string $password Database Password
|
||||||
|
* @param string $database Database to which to create
|
||||||
* @return boolean Returns true if successful
|
* @return boolean Returns true if successful
|
||||||
*/
|
*/
|
||||||
static function createDatabase() {
|
static function createDatabase($connect, $username, $password, $database) {
|
||||||
return DB::$globalConn->createDatabase();
|
return DB::$globalConn->createDatabase($connect, $username, $password, $database);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,9 +49,20 @@ abstract class Database extends Object {
|
|||||||
* Create the database and connect to it. This can be called if the
|
* Create the database and connect to it. This can be called if the
|
||||||
* initial database connection is not successful because the database
|
* initial database connection is not successful because the database
|
||||||
* does not exist.
|
* does not exist.
|
||||||
|
* @param string $connect Connection string
|
||||||
|
* @param string $username Database username
|
||||||
|
* @param string $password Database Password
|
||||||
|
* @param string $database Database to which to create
|
||||||
* @return boolean Returns true if successful
|
* @return boolean Returns true if successful
|
||||||
*/
|
*/
|
||||||
abstract function createDatabase();
|
abstract function createDatabase($connect, $username, $password, $database);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the connection string from input
|
||||||
|
* @param array $parameters The connection details
|
||||||
|
* @return string $connect The connection string
|
||||||
|
**/
|
||||||
|
abstract function getConnect($parameters);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new table.
|
* Create a new table.
|
||||||
|
@ -135,7 +135,12 @@ class DatabaseAdmin extends Controller {
|
|||||||
if(!$quiet) {
|
if(!$quiet) {
|
||||||
echo '<p><b>Creating database</b></p>';
|
echo '<p><b>Creating database</b></p>';
|
||||||
}
|
}
|
||||||
DB::createDatabase();
|
$parameters = $_REQUEST['db'];
|
||||||
|
$connect = DB::getConnect($parameters);
|
||||||
|
$username = $parameters[username];
|
||||||
|
$password = $parameters[password];
|
||||||
|
$database = $parameters[database];
|
||||||
|
DB::createDatabase($connect, $username, $password, $database);
|
||||||
// ManifestBuilder::compileManifest();
|
// ManifestBuilder::compileManifest();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -432,4 +437,4 @@ class DatabaseAdmin extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
@ -46,6 +46,13 @@ class MySQLDatabase extends Database {
|
|||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Not implemented, needed for PDO
|
||||||
|
*/
|
||||||
|
public function getConnect($parameters) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if this database supports collations
|
* Returns true if this database supports collations
|
||||||
* @return boolean
|
* @return boolean
|
||||||
@ -109,10 +116,10 @@ class MySQLDatabase extends Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function isActive() {
|
public function isActive() {
|
||||||
return $this->active ? true : false;
|
return $this->active ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createDatabase() {
|
public function createDatabase($connect, $username, $password, $db) {
|
||||||
$this->query("CREATE DATABASE $this->database");
|
$this->query("CREATE DATABASE $this->database");
|
||||||
$this->query("USE $this->database");
|
$this->query("USE $this->database");
|
||||||
|
|
||||||
@ -361,7 +368,7 @@ class MySQLDatabase extends Database {
|
|||||||
$table = strtolower(reset($record));
|
$table = strtolower(reset($record));
|
||||||
$tables[$table] = $table;
|
$tables[$table] = $table;
|
||||||
}
|
}
|
||||||
return isset($tables) ? $tables : null;
|
return isset($tables) ? $tables : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,11 +1,30 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @package sapphire
|
||||||
|
* @subpackage core
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PDO (general database) connector class.
|
* PDO (general database) connector class.
|
||||||
*/
|
*/
|
||||||
class PDODatabase extends Database {
|
class PDODatabase extends Database {
|
||||||
|
/**
|
||||||
|
* Connection to the DBMS.
|
||||||
|
* @var resource
|
||||||
|
*/
|
||||||
private $dbConn;
|
private $dbConn;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* True if we are connected to a database.
|
||||||
|
* @var boolean
|
||||||
|
*/
|
||||||
private $active;
|
private $active;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the database.
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
private $database;
|
private $database;
|
||||||
|
|
||||||
|
|
||||||
@ -14,58 +33,229 @@ class PDODatabase extends Database {
|
|||||||
* @param parameters An map of parameters, which should include:
|
* @param parameters An map of parameters, which should include:
|
||||||
* <ul><li>database: The database to connect with</li>
|
* <ul><li>database: The database to connect with</li>
|
||||||
* <li>server: The server, eg, localhost</li>
|
* <li>server: The server, eg, localhost</li>
|
||||||
|
* <li>port: The port on which the server is listening (optional)</li>
|
||||||
|
* <li>instance: Instance of the server, MS SQL only (optional)</li>
|
||||||
* <li>username: The username to log on with</li>
|
* <li>username: The username to log on with</li>
|
||||||
* <li>password: The password to log on with</li>
|
* <li>password: The password to log on with</li>
|
||||||
* <li>database: The database to connect to</li></ul>
|
* <li>database: The database to connect to</li></ul>
|
||||||
*/
|
*/
|
||||||
public function __construct($parameters) {
|
public function __construct($parameters) {
|
||||||
switch ($parameters['database']) {
|
$connect = PDODatabase::getConnect($parameters);
|
||||||
case "MySQL":
|
$connectWithDB = $connect . ';dbname=' . $parameters['database'];
|
||||||
$connect = 'mysql:host=' . $parameters['server'] . ';dbname=' . $parameters['database'];
|
try { // Try connect to the database, if it does not exist, create it
|
||||||
break;
|
$this->dbConn = new PDO($connectWithDB, $parameters['username'], $parameters['password']);
|
||||||
case "PostgreSQL":
|
} catch (PDOException $e) {
|
||||||
$connect = 'pgsql:host=' . $parameters['server'] . ';port=5432;dbname=' . $parameters['database'];
|
if (!self::createDatabase($connect, $parameters['username'], $parameters['password'], $parameters['database'])) {
|
||||||
break;
|
$this->databaseError("Could not connect to the database, make sure the server is available and user credentials are correct");
|
||||||
case "MSSQL":
|
}
|
||||||
$connect = 'mssql:host=' . $parameters['server'] . ';dbname=' . $parameters['database'];
|
|
||||||
break;
|
|
||||||
default: $this->databaseError("Database not available");
|
|
||||||
}
|
}
|
||||||
$this->dbConn = new PDO($connect, $parameters['username'], $parameters['password']);
|
|
||||||
$this->database = $parameters['database'];
|
|
||||||
if(!$this->dbConn) $this->databaseError("Could connect to MySQL database");
|
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the connection string from input.
|
||||||
|
* @param array $parameters The connection details.
|
||||||
|
* @return string $connect The connection string.
|
||||||
|
**/
|
||||||
|
public function getConnect($parameters) {
|
||||||
|
switch ($parameters['type']) {
|
||||||
|
case "mysql":
|
||||||
|
$port = '3306';
|
||||||
|
$type = 'mysql';
|
||||||
|
$instance = '';
|
||||||
|
break;
|
||||||
|
case "postgresql":
|
||||||
|
$port = '5432';
|
||||||
|
$type = 'pgsql';
|
||||||
|
$instance = '';
|
||||||
|
break;
|
||||||
|
case "mssql":
|
||||||
|
$port = '1433';
|
||||||
|
if (isset($parameters['instance']) && $parameters['instance'] != '') {
|
||||||
|
$instance = '\\' . $parameters['instance'];
|
||||||
|
} else {
|
||||||
|
$instance = '';
|
||||||
|
}
|
||||||
|
$type = 'mssql';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$this->databaseError("This database server is not available");
|
||||||
|
}
|
||||||
|
if (isset($parameters['port']) && is_numeric($parameters['port'])) {
|
||||||
|
$port = $parameters['port'];
|
||||||
|
}
|
||||||
|
$connect = $type . ':host=' . $parameters['server'] . $instance . ';port=' . $port;
|
||||||
|
return $connect;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if this database supports collations
|
* Returns true if this database supports collations
|
||||||
*/
|
*/
|
||||||
public function supportsCollations() {
|
public function supportsCollations() {
|
||||||
|
$collations = false;
|
||||||
|
switch (PDO::ATTR_DRIVER_NAME) {
|
||||||
|
case "pgsql": // Generally supported in PostgreSQL (supported versions)
|
||||||
|
case "mssql": // Generally supported in MS SQL (supported versions)
|
||||||
|
$collations = true;
|
||||||
|
break;
|
||||||
|
case "mysql":
|
||||||
|
if ($this->getVersion() >= 4.1) { // Supported in MySQL since 4.1
|
||||||
|
$collations = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return $collations;
|
||||||
}
|
}
|
||||||
|
|
||||||
//private $mysqlVersion;
|
/**
|
||||||
|
* Get the database version.
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
public function getVersion() {
|
public function getVersion() {
|
||||||
|
switch ($type) {
|
||||||
|
case "mysql":
|
||||||
|
case "postgresql":
|
||||||
|
$query = "SELECT VERSION()";
|
||||||
|
break;
|
||||||
|
case "mssql":
|
||||||
|
$query = "SELECT @@VERSION";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$getData = $dbConn->prepare($query);
|
||||||
|
$getData->execute();
|
||||||
|
$dbVersion = $getData->fetchColumn();
|
||||||
|
$version = ereg_replace("([A-Za-z-])", "", $dbVersion);
|
||||||
|
return substr(trim($version), 0, 3); // Just get the major and minor version
|
||||||
}
|
}
|
||||||
|
|
||||||
public function query($sql, $errorLevel = E_USER_ERROR) {
|
public function query($sql, $errorLevel = E_USER_ERROR) {
|
||||||
|
if(isset($_REQUEST['previewwrite']) && in_array(strtolower(substr($sql,0,6)), array('insert','update'))) {
|
||||||
|
echo "<p>Will execute: $sql</p>";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//Debug::backtrace();
|
||||||
|
if(isset($_REQUEST['showqueries'])) {
|
||||||
|
Debug::message("\n" . $sql . "\n");
|
||||||
|
$starttime = microtime(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$query = $dbConn->prepare($sql);
|
||||||
|
$handle = $query->execute(); // Execute and save the return value (true or false)
|
||||||
|
$result = $query->fetchAll(); // Get the result itself
|
||||||
|
|
||||||
|
if(isset($_REQUEST['showqueries'])) {
|
||||||
|
$duration = microtime(true) - $starttime;
|
||||||
|
Debug::message("\n" . $duration . "\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!$handle && $errorLevel) {
|
||||||
|
$error = $query->errorInfo();
|
||||||
|
$this->databaseError("Couldn't run query: $sql | " . $error[2], $errorLevel);
|
||||||
|
}
|
||||||
|
return new PDOQuery($result);
|
||||||
}
|
}
|
||||||
public function getGeneratedID() {
|
|
||||||
}
|
|
||||||
public function getNextID($table) {
|
|
||||||
}
|
|
||||||
public function isActive() {
|
|
||||||
}
|
|
||||||
public function createDatabase() {
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* Create a new table with an integer primary key called ID.
|
* Get the ID for the next new record for the table.
|
||||||
|
* @var string $table The name od the table.
|
||||||
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function createTable($tableName) {
|
public function getNextID($table) {
|
||||||
|
$sql = "SELECT MAX(ID)+1 FROM :table";
|
||||||
|
$create->bindParam(":table", $table);
|
||||||
|
$query = $dbConn->prepare($sql);
|
||||||
|
$handle = $query->execute();
|
||||||
|
$result = $query->fetchColumn();
|
||||||
|
return $handle ? $result : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isActive() {
|
||||||
|
return $this->active ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the database and connect to it. This can be called if the
|
||||||
|
* initial database connection is not successful because the database
|
||||||
|
* does not exist.
|
||||||
|
* @param string $connect Connection string
|
||||||
|
* @param string $username Database username
|
||||||
|
* @param string $password Database Password
|
||||||
|
* @param string $database Database to which to create
|
||||||
|
* @return boolean Returns true if successful
|
||||||
|
*/
|
||||||
|
public function createDatabase($connect, $username, $password, $database) {
|
||||||
|
try {
|
||||||
|
$dbConn = new PDO($connect, $username, $password);
|
||||||
|
$create = $dbConn->prepare("CREATE DATABASE :database");
|
||||||
|
$create->bindParam(":database", $database);
|
||||||
|
$create->execute();
|
||||||
|
$this->active = true;
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
$this->databaseError($e->getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Create a new table with an integer primary key called ID.
|
* Create a new table with an integer primary key called ID.
|
||||||
|
* @var string $tableName The name of the table.
|
||||||
|
* @return void.
|
||||||
|
*/
|
||||||
|
public function createTable($tableName, $fields = null, $indexes = null) {
|
||||||
|
$fieldSchemas = $indexSchemas = "";
|
||||||
|
if ($fields) {
|
||||||
|
foreach($fields as $k => $v) $fieldSchemas .= "`$k` $v,\n";
|
||||||
|
}
|
||||||
|
if ($indexes) {
|
||||||
|
foreach($indexes as $k => $v) $fieldSchemas .= $this->getIndexSqlDefinition($k, $v) . ",\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ($parameters['type']) {
|
||||||
|
case "mysql": $create = $dbConn->prepare("CREATE TABLE :tableName (ID INT(11) NOT NULL AUTO_INCREMENT, $fieldSchemas $indexSchemas PRIMARY KEY (ID)) TYPE=MyISAM");
|
||||||
|
break;
|
||||||
|
case "postgresql": $create = $dbConn->prepare("CREATE TABLE :tableName (ID SERIAL, $fieldSchemas $indexSchemas PRIMARY KEY (ID))");
|
||||||
|
break;
|
||||||
|
case "mssql": $create = $dbConn->prepare("CREATE TABLE :tableName (ID INT(11) IDENTITY(1,1), $fieldSchemas $indexSchemas PRIMARY KEY (ID))");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$this->databaseError("This database server is not available");
|
||||||
|
}
|
||||||
|
$create->bindParam(":tableName", $tableName);
|
||||||
|
$create->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function alterTable($table, $newFields, $newIndexes, $alteredFields, $alteredIndexes) {
|
||||||
|
$fieldSchemas = $indexSchemas = "";
|
||||||
|
|
||||||
|
if ($newFields) {
|
||||||
|
foreach($newFields as $k => $v) $alterList[] .= "ADD `$k` $v";
|
||||||
|
}
|
||||||
|
if ($newIndexes) {
|
||||||
|
foreach($newIndexes as $k => $v) $alterList[] .= "ADD " . $this->getIndexSqlDefinition($k, $v) . ",\n";
|
||||||
|
}
|
||||||
|
if ($alteredFields) {
|
||||||
|
foreach($alteredFields as $k => $v) $alterList[] .= "CHANGE `$k` `$k` $v";
|
||||||
|
}
|
||||||
|
if ($alteredIndexes) foreach($alteredIndexes as $k => $v) {
|
||||||
|
$alterList[] .= "DROP INDEX `$k`";
|
||||||
|
$alterList[] .= "ADD ". $this->getIndexSqlDefinition($k, $v);
|
||||||
|
}
|
||||||
|
|
||||||
|
$alterations = implode(",\n", $alterList);
|
||||||
|
$this->query("ALTER TABLE `$tableName` " . $alterations);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Rename an existing table, the TO is necessary for PostgreSQL and MS SQL.
|
||||||
|
* @var string $oldTableName The name of the existing table.
|
||||||
|
* @var string $newTableName How the table should be named from now on.
|
||||||
|
* @return void.
|
||||||
*/
|
*/
|
||||||
public function renameTable($oldTableName, $newTableName) {
|
public function renameTable($oldTableName, $newTableName) {
|
||||||
|
$query = "ALTER TABLE :oldTableName RENAME TO :newTableName";
|
||||||
|
$create->bindParam(":oldTableName", $oldTableName);
|
||||||
|
$create->bindParam(":newTableName", $newTableName);
|
||||||
|
$create = $dbConn->prepare($query);
|
||||||
|
$create->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -80,7 +270,13 @@ class PDODatabase extends Database {
|
|||||||
* Add the given field to the given table.
|
* Add the given field to the given table.
|
||||||
*/
|
*/
|
||||||
public function createField($tableName, $fieldName, $fieldSpec) {
|
public function createField($tableName, $fieldName, $fieldSpec) {
|
||||||
|
$create = $dbConn->prepare("ALTER TABLE :tableName ADD :fieldName :fieldSpec");
|
||||||
|
$create->bindParam(":tableName", $tableName);
|
||||||
|
$create->bindParam(":fieldName", $fieldName);
|
||||||
|
$create->bindParam(":fieldSpec", $fieldSpec);
|
||||||
|
$create->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Change the database type of the given field
|
* Change the database type of the given field
|
||||||
*/
|
*/
|
||||||
@ -117,32 +313,33 @@ class PDODatabase extends Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A result-set from a MySQL database.
|
* A result-set from a database query (array).
|
||||||
*/
|
*/
|
||||||
class MySQLQuery extends Query {
|
class PDOQuery extends Query {
|
||||||
private $database;
|
private $result;
|
||||||
private $handle;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hook the result-set given into a Query class, suitable for use by sapphire.
|
* Hook the result-set given into a Query class, suitable for use by sapphire.
|
||||||
* @param database The database object that created this query.
|
* @param array $result The array of all returned values.
|
||||||
* @param handle the internal mysql handle that is points to the resultset.
|
|
||||||
*/
|
*/
|
||||||
public function __construct(MySQLDatabase $database, $handle) {
|
public function __construct(PDODatabase $result) {
|
||||||
$this->database = $database;
|
$this->result = $result;
|
||||||
$this->handle = $handle;
|
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __destroy() {
|
public function __destroy() {
|
||||||
|
$this->result = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function seek($row) {
|
public function seek($row) {
|
||||||
|
return in_array($row, $this->result);
|
||||||
}
|
}
|
||||||
public function numRecords() {
|
public function numRecords() {
|
||||||
|
return count($this->result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function nextRecord() {
|
public function nextRecord() {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user