mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
pkrenn: Code cleaned up and affectedRows() are now supported (merged from branches/gsoc)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@41706 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
d7a4e657dc
commit
c144bf1aef
@ -28,6 +28,13 @@ class PDODatabase extends Database {
|
|||||||
private $database;
|
private $database;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Last PDO statement, needed for affectedRows()
|
||||||
|
* @var PDO object
|
||||||
|
*/
|
||||||
|
private $stmt;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connect to a database (MySQL, PostgreSQL, or MS SQL).
|
* Connect to a database (MySQL, PostgreSQL, or MS SQL).
|
||||||
* @param parameters An map of parameters, which should include:
|
* @param parameters An map of parameters, which should include:
|
||||||
@ -121,9 +128,9 @@ class PDODatabase extends Database {
|
|||||||
$query = "SELECT @@VERSION";
|
$query = "SELECT @@VERSION";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$getData = $dbConn->prepare($query);
|
$stmt = $dbConn->prepare($query);
|
||||||
$getData->execute();
|
$stmt->execute();
|
||||||
$dbVersion = $getData->fetchColumn();
|
$dbVersion = $stmt->fetchColumn();
|
||||||
$version = ereg_replace("([A-Za-z-])", "", $dbVersion);
|
$version = ereg_replace("([A-Za-z-])", "", $dbVersion);
|
||||||
return substr(trim($version), 0, 3); // Just get the major and minor version
|
return substr(trim($version), 0, 3); // Just get the major and minor version
|
||||||
}
|
}
|
||||||
@ -144,9 +151,9 @@ class PDODatabase extends Database {
|
|||||||
$starttime = microtime(true);
|
$starttime = microtime(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
$query = $dbConn->prepare($sql);
|
$stmt = $dbConn->prepare($sql);
|
||||||
$handle = $query->execute(); // Execute and save the return value (true or false)
|
$handle = $stmt->execute(); // Execute and save the return value (true or false)
|
||||||
$result = $query->fetchAll(); // Get the result itself
|
$result = $stmt->fetchAll(); // Get the result itself
|
||||||
|
|
||||||
if(isset($_REQUEST['showqueries'])) {
|
if(isset($_REQUEST['showqueries'])) {
|
||||||
$duration = microtime(true) - $starttime;
|
$duration = microtime(true) - $starttime;
|
||||||
@ -154,7 +161,7 @@ class PDODatabase extends Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(!$handle && $errorLevel) {
|
if(!$handle && $errorLevel) {
|
||||||
$error = $query->errorInfo();
|
$error = $stmt->errorInfo();
|
||||||
$this->databaseError("Couldn't run query: $sql | " . $error[2], $errorLevel);
|
$this->databaseError("Couldn't run query: $sql | " . $error[2], $errorLevel);
|
||||||
}
|
}
|
||||||
return new PDOQuery($result);
|
return new PDOQuery($result);
|
||||||
@ -162,15 +169,25 @@ class PDODatabase extends Database {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the ID for the next new record for the table.
|
* Get the ID for the next new record for the table.
|
||||||
|
* Get the autogenerated ID from the previous INSERT query.
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getGeneratedID() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OBSOLETE: Get the ID for the next new record for the table.
|
||||||
* @var string $table The name od the table.
|
* @var string $table The name od the table.
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getNextID($table) {
|
public function getNextID($table) {
|
||||||
$sql = "SELECT MAX(ID)+1 FROM :table";
|
user_error('getNextID is OBSOLETE (and will no longer work properly)', E_USER_WARNING);
|
||||||
$create->bindParam(":table", $table);
|
$stmt = $dbConn->prepare("SELECT MAX(ID)+1 FROM :table");
|
||||||
$query = $dbConn->prepare($sql);
|
$stmt->bindParam(":table", $table);
|
||||||
$handle = $query->execute();
|
$handle = $stmt->execute();
|
||||||
$result = $query->fetchColumn();
|
$result = $stmt->fetchColumn();
|
||||||
return $handle ? $result : 1;
|
return $handle ? $result : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,9 +212,9 @@ class PDODatabase extends Database {
|
|||||||
public function createDatabase($connect, $username, $password, $database) {
|
public function createDatabase($connect, $username, $password, $database) {
|
||||||
try {
|
try {
|
||||||
$dbConn = new PDO($connect, $username, $password);
|
$dbConn = new PDO($connect, $username, $password);
|
||||||
$create = $dbConn->prepare("CREATE DATABASE :database");
|
$stmt = $dbConn->prepare("CREATE DATABASE :database");
|
||||||
$create->bindParam(":database", $database);
|
$stmt->bindParam(":database", $database);
|
||||||
$create->execute();
|
$stmt->execute();
|
||||||
$this->active = true;
|
$this->active = true;
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
$this->databaseError($e->getMessage());
|
$this->databaseError($e->getMessage());
|
||||||
@ -218,19 +235,19 @@ class PDODatabase extends Database {
|
|||||||
|
|
||||||
switch ($parameters['type']) {
|
switch ($parameters['type']) {
|
||||||
case "mysql":
|
case "mysql":
|
||||||
$create = $dbConn->prepare("CREATE TABLE :tableName (ID INT(11) NOT NULL AUTO_INCREMENT, $fieldSchemas PRIMARY KEY (ID)) TYPE=MyISAM");
|
$stmt = $dbConn->prepare("CREATE TABLE :tableName (ID INT(11) NOT NULL AUTO_INCREMENT, $fieldSchemas PRIMARY KEY (ID)) TYPE=MyISAM");
|
||||||
break;
|
break;
|
||||||
case "postgresql":
|
case "postgresql":
|
||||||
$create = $dbConn->prepare("CREATE TABLE :tableName (ID SERIAL, $fieldSchemas PRIMARY KEY (ID))");
|
$stmt = $dbConn->prepare("CREATE TABLE :tableName (ID SERIAL, $fieldSchemas PRIMARY KEY (ID))");
|
||||||
break;
|
break;
|
||||||
case "mssql":
|
case "mssql":
|
||||||
$create = $dbConn->prepare("CREATE TABLE :tableName (ID INT(11) IDENTITY(1,1), $fieldSchemas PRIMARY KEY (ID))");
|
$stmt = $dbConn->prepare("CREATE TABLE :tableName (ID INT(11) IDENTITY(1,1), $fieldSchemas PRIMARY KEY (ID))");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$this->databaseError("This database server is not available");
|
$this->databaseError("This database server is not available");
|
||||||
}
|
}
|
||||||
$create->bindParam(":tableName", $tableName);
|
$stmt->bindParam(":tableName", $tableName);
|
||||||
$create->execute();
|
$stmt->execute();
|
||||||
|
|
||||||
if ($indexes) {
|
if ($indexes) {
|
||||||
alterTable($tableName, null, $indexes, null, null);
|
alterTable($tableName, null, $indexes, null, null);
|
||||||
@ -249,36 +266,36 @@ class PDODatabase extends Database {
|
|||||||
public function alterTable($table, $newFields, $newIndexes, $alteredFields, $alteredIndexes) {
|
public function alterTable($table, $newFields, $newIndexes, $alteredFields, $alteredIndexes) {
|
||||||
|
|
||||||
if ($newFields) {
|
if ($newFields) {
|
||||||
$add = $dbConn->prepare("ALTER TABLE :table ADD :field :type");
|
$stmt = $dbConn->prepare("ALTER TABLE :table ADD :field :type");
|
||||||
$add->bindParam(':table', $table);
|
$stmt->bindParam(':table', $table);
|
||||||
$add->bindParam(':field', $field);
|
$stmt->bindParam(':field', $field);
|
||||||
$add->bindParam(':type', $type);
|
$stmt->bindParam(':type', $type);
|
||||||
foreach ($newFields as $k => $v) {
|
foreach ($newFields as $k => $v) {
|
||||||
$field = $k;
|
$field = $k;
|
||||||
$type = $v;
|
$type = $v;
|
||||||
$add->execute();
|
$stmt->execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($newIndexes) {
|
if ($newIndexes) {
|
||||||
$add = $dbConn->prepare("CREATE INDEX :name ON :table :column");
|
$stmt = $dbConn->prepare("CREATE INDEX :name ON :table :column");
|
||||||
$add->bindParam(':table', $table);
|
$stmt->bindParam(':table', $table);
|
||||||
$add->bindParam(':name', $name);
|
$stmt->bindParam(':name', $name);
|
||||||
$add->bindParam(':column', $column);
|
$stmt->bindParam(':column', $column);
|
||||||
foreach ($newIndexes as $k => $v) {
|
foreach ($newIndexes as $k => $v) {
|
||||||
$name = $k;
|
$name = $k;
|
||||||
$column = $v;
|
$column = $v;
|
||||||
$add->execute();
|
$stmt->execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($alteredFields) {
|
if ($alteredFields) {
|
||||||
switch ($parameters['type']) {
|
switch ($parameters['type']) {
|
||||||
case "mysql":
|
case "mysql":
|
||||||
$alter = $dbConn->prepare("ALTER TABLE :table CHANGE :field :field :type");
|
$stmt = $dbConn->prepare("ALTER TABLE :table CHANGE :field :field :type");
|
||||||
break;
|
break;
|
||||||
case "postgresql":
|
case "postgresql":
|
||||||
$alter = $dbConn->prepare("
|
$stmt = $dbConn->prepare("
|
||||||
BEGIN;
|
BEGIN;
|
||||||
ALTER TABLE :table RENAME :field TO oldfield;
|
ALTER TABLE :table RENAME :field TO oldfield;
|
||||||
ALTER TABLE :table ADD COLUMN :field :type;
|
ALTER TABLE :table ADD COLUMN :field :type;
|
||||||
@ -288,50 +305,49 @@ class PDODatabase extends Database {
|
|||||||
");
|
");
|
||||||
break;
|
break;
|
||||||
case "mssql":
|
case "mssql":
|
||||||
$this->dbh->query("ALTER TABLE :table ALTER COLUMN :field :type");
|
$stmt = $dbConn->prepare("ALTER TABLE :table ALTER COLUMN :field :type");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$this->databaseError("This database server is not available");
|
$this->databaseError("This database server is not available");
|
||||||
}
|
}
|
||||||
$alter->bindParam(':table', $table);
|
$stmt->bindParam(':table', $table);
|
||||||
$alter->bindParam(':field', $field);
|
$stmt->bindParam(':field', $field);
|
||||||
$alter->bindParam(':type', $type);
|
$stmt->bindParam(':type', $type);
|
||||||
foreach ($alteredFields as $k => $v) {
|
foreach ($alteredFields as $k => $v) {
|
||||||
$field = $k;
|
$field = $k;
|
||||||
$type = $v;
|
$type = $v;
|
||||||
$alter->execute();
|
$stmt->execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($alteredIndexes) {
|
if ($alteredIndexes) {
|
||||||
$drop = $dbConn->prepare("DROP INDEX :drop");
|
$drop = $dbConn->prepare("DROP INDEX :drop");
|
||||||
$alter->bindParam(':drop', $drop);
|
$drop->bindParam(':drop', $drop);
|
||||||
$alter = $dbConn->prepare("CREATE INDEX :name ON :table :column");
|
$stmt = $dbConn->prepare("CREATE INDEX :name ON :table :column");
|
||||||
$alter->bindParam(':table', $table);
|
$stmt->bindParam(':table', $table);
|
||||||
$alter->bindParam(':name', $name);
|
$stmt->bindParam(':name', $name);
|
||||||
$alter->bindParam(':column', $column);
|
$stmt->bindParam(':column', $column);
|
||||||
foreach ($newIndexes as $k => $v) {
|
foreach ($newIndexes as $k => $v) {
|
||||||
$drop = $k;
|
$drop = $k;
|
||||||
$drop->execute();
|
$drop->execute();
|
||||||
$name = $k;
|
$name = $k;
|
||||||
$column = $v;
|
$column = $v;
|
||||||
$add->execute();
|
$stmt->execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rename an existing table, the TO is necessary for PostgreSQL and MS SQL.
|
* Rename an existing table, the TO is necessary for PostgreSQL and MS SQL.
|
||||||
* @var string $oldTableName The name of the existing table.
|
* @var string $oldTableName The name of the existing table.
|
||||||
* @var string $newTableName How the table should be named from now on.
|
* @var string $newTableName How the table should be named from now on.
|
||||||
* @return void.
|
* @return void.
|
||||||
*/
|
*/
|
||||||
public function renameTable($oldTableName, $newTableName) {
|
public function renameTable($oldTableName, $newTableName) {
|
||||||
$query = "ALTER TABLE :oldTableName RENAME TO :newTableName";
|
$stmt = $dbConn->prepare("ALTER TABLE :oldTableName RENAME TO :newTableName");
|
||||||
$create->bindParam(":oldTableName", $oldTableName);
|
$stmt->bindParam(":oldTableName", $oldTableName);
|
||||||
$create->bindParam(":newTableName", $newTableName);
|
$stmt->bindParam(":newTableName", $newTableName);
|
||||||
$create = $dbConn->prepare($query);
|
$stmt->execute();
|
||||||
$create->execute();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -377,11 +393,11 @@ class PDODatabase extends Database {
|
|||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function createField($tableName, $fieldName, $fieldSpec) {
|
public function createField($tableName, $fieldName, $fieldSpec) {
|
||||||
$create = $dbConn->prepare("ALTER TABLE :tableName ADD :fieldName :fieldSpec");
|
$stmt = $dbConn->prepare("ALTER TABLE :tableName ADD :fieldName :fieldSpec");
|
||||||
$create->bindParam(":tableName", $tableName);
|
$stmt->bindParam(":tableName", $tableName);
|
||||||
$create->bindParam(":fieldName", $fieldName);
|
$stmt->bindParam(":fieldName", $fieldName);
|
||||||
$create->bindParam(":fieldSpec", $fieldSpec);
|
$stmt->bindParam(":fieldSpec", $fieldSpec);
|
||||||
$create->execute();
|
$stmt->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -394,10 +410,10 @@ class PDODatabase extends Database {
|
|||||||
public function alterField($table, $field, $type) {
|
public function alterField($table, $field, $type) {
|
||||||
switch ($parameters['type']) {
|
switch ($parameters['type']) {
|
||||||
case "mysql":
|
case "mysql":
|
||||||
$alter = $dbConn->prepare("ALTER TABLE :table CHANGE :field :field :type");
|
$stmt = $dbConn->prepare("ALTER TABLE :table CHANGE :field :field :type");
|
||||||
break;
|
break;
|
||||||
case "postgresql":
|
case "postgresql":
|
||||||
$alter = $dbConn->prepare("
|
$stmt = $dbConn->prepare("
|
||||||
BEGIN;
|
BEGIN;
|
||||||
ALTER TABLE :table RENAME :field TO oldfield;
|
ALTER TABLE :table RENAME :field TO oldfield;
|
||||||
ALTER TABLE :table ADD COLUMN :field :type;
|
ALTER TABLE :table ADD COLUMN :field :type;
|
||||||
@ -407,15 +423,15 @@ class PDODatabase extends Database {
|
|||||||
");
|
");
|
||||||
break;
|
break;
|
||||||
case "mssql":
|
case "mssql":
|
||||||
$this->dbh->query("ALTER TABLE :table ALTER COLUMN :field :type");
|
$stmt = $dbConn->prepare("ALTER TABLE :table ALTER COLUMN :field :type");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$this->databaseError("This database server is not available");
|
$this->databaseError("This database server is not available");
|
||||||
}
|
}
|
||||||
$alter->bindParam(':table', $table);
|
$stmt->bindParam(':table', $table);
|
||||||
$alter->bindParam(':field', $field);
|
$stmt->bindParam(':field', $field);
|
||||||
$alter->bindParam(':type', $type);
|
$stmt->bindParam(':type', $type);
|
||||||
$alter->execute();
|
$stmt->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -426,11 +442,11 @@ class PDODatabase extends Database {
|
|||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function createIndex($tableName, $indexName, $indexSpec) {
|
public function createIndex($tableName, $indexName, $indexSpec) {
|
||||||
$add = $dbConn->prepare("CREATE INDEX :name ON :table :column");
|
$stmt = $dbConn->prepare("CREATE INDEX :name ON :table :column");
|
||||||
$add->bindParam(':table', $tableName);
|
$stmt->bindParam(':table', $tableName);
|
||||||
$add->bindParam(':name', $indexName);
|
$stmt->bindParam(':name', $indexName);
|
||||||
$add->bindParam(':column', $indexSpec);
|
$stmt->bindParam(':column', $indexSpec);
|
||||||
$add->execute();
|
$stmt->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -442,13 +458,13 @@ class PDODatabase extends Database {
|
|||||||
*/
|
*/
|
||||||
public function alterIndex($tableName, $indexName, $indexSpec) {
|
public function alterIndex($tableName, $indexName, $indexSpec) {
|
||||||
$drop = $dbConn->prepare("DROP INDEX :drop");
|
$drop = $dbConn->prepare("DROP INDEX :drop");
|
||||||
$alter->bindParam(':drop', $indexName);
|
$drop->bindParam(':drop', $indexName);
|
||||||
$alter = $dbConn->prepare("CREATE INDEX :name ON :table :column");
|
$stmt = $dbConn->prepare("CREATE INDEX :name ON :table :column");
|
||||||
$alter->bindParam(':table', $tableName);
|
$stmt->bindParam(':table', $tableName);
|
||||||
$alter->bindParam(':name', $indexName);
|
$stmt->bindParam(':name', $indexName);
|
||||||
$alter->bindParam(':column', $indexSpec);
|
$stmt->bindParam(':column', $indexSpec);
|
||||||
$drop->execute();
|
$drop->execute();
|
||||||
$add->execute();
|
$stmt->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -485,14 +501,15 @@ class PDODatabase extends Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the number of rows affected by the previous operation.
|
* Return the number of rows affected (DELETE, INSERT, or UPDATE) by the previous operation.
|
||||||
*/
|
*/
|
||||||
public function affectedRows() {
|
public function affectedRows() {
|
||||||
|
return $stmt->rowCount();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A result-set from a database query (array).
|
* A result-set from a database query (array).
|
||||||
*/
|
*/
|
||||||
class PDOQuery extends Query {
|
class PDOQuery extends Query {
|
||||||
private $result;
|
private $result;
|
||||||
|
Loading…
Reference in New Issue
Block a user