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;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Last PDO statement, needed for affectedRows()
|
||||
* @var PDO object
|
||||
*/
|
||||
private $stmt;
|
||||
|
||||
/**
|
||||
* Connect to a database (MySQL, PostgreSQL, or MS SQL).
|
||||
* @param parameters An map of parameters, which should include:
|
||||
@ -121,9 +128,9 @@ class PDODatabase extends Database {
|
||||
$query = "SELECT @@VERSION";
|
||||
break;
|
||||
}
|
||||
$getData = $dbConn->prepare($query);
|
||||
$getData->execute();
|
||||
$dbVersion = $getData->fetchColumn();
|
||||
$stmt = $dbConn->prepare($query);
|
||||
$stmt->execute();
|
||||
$dbVersion = $stmt->fetchColumn();
|
||||
$version = ereg_replace("([A-Za-z-])", "", $dbVersion);
|
||||
return substr(trim($version), 0, 3); // Just get the major and minor version
|
||||
}
|
||||
@ -144,17 +151,17 @@ class PDODatabase extends Database {
|
||||
$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
|
||||
|
||||
$stmt = $dbConn->prepare($sql);
|
||||
$handle = $stmt->execute(); // Execute and save the return value (true or false)
|
||||
$result = $stmt->fetchAll(); // Get the result itself
|
||||
|
||||
if(isset($_REQUEST['showqueries'])) {
|
||||
$duration = microtime(true) - $starttime;
|
||||
Debug::message("\n" . $duration . "\n");
|
||||
}
|
||||
|
||||
if(!$handle && $errorLevel) {
|
||||
$error = $query->errorInfo();
|
||||
$error = $stmt->errorInfo();
|
||||
$this->databaseError("Couldn't run query: $sql | " . $error[2], $errorLevel);
|
||||
}
|
||||
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 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.
|
||||
* @return int
|
||||
*/
|
||||
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();
|
||||
user_error('getNextID is OBSOLETE (and will no longer work properly)', E_USER_WARNING);
|
||||
$stmt = $dbConn->prepare("SELECT MAX(ID)+1 FROM :table");
|
||||
$stmt->bindParam(":table", $table);
|
||||
$handle = $stmt->execute();
|
||||
$result = $stmt->fetchColumn();
|
||||
return $handle ? $result : 1;
|
||||
}
|
||||
|
||||
@ -195,9 +212,9 @@ class PDODatabase extends Database {
|
||||
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();
|
||||
$stmt = $dbConn->prepare("CREATE DATABASE :database");
|
||||
$stmt->bindParam(":database", $database);
|
||||
$stmt->execute();
|
||||
$this->active = true;
|
||||
} catch (PDOException $e) {
|
||||
$this->databaseError($e->getMessage());
|
||||
@ -218,20 +235,20 @@ class PDODatabase extends Database {
|
||||
|
||||
switch ($parameters['type']) {
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
default:
|
||||
$this->databaseError("This database server is not available");
|
||||
}
|
||||
$create->bindParam(":tableName", $tableName);
|
||||
$create->execute();
|
||||
|
||||
$stmt->bindParam(":tableName", $tableName);
|
||||
$stmt->execute();
|
||||
|
||||
if ($indexes) {
|
||||
alterTable($tableName, null, $indexes, null, null);
|
||||
}
|
||||
@ -249,36 +266,36 @@ class PDODatabase extends Database {
|
||||
public function alterTable($table, $newFields, $newIndexes, $alteredFields, $alteredIndexes) {
|
||||
|
||||
if ($newFields) {
|
||||
$add = $dbConn->prepare("ALTER TABLE :table ADD :field :type");
|
||||
$add->bindParam(':table', $table);
|
||||
$add->bindParam(':field', $field);
|
||||
$add->bindParam(':type', $type);
|
||||
$stmt = $dbConn->prepare("ALTER TABLE :table ADD :field :type");
|
||||
$stmt->bindParam(':table', $table);
|
||||
$stmt->bindParam(':field', $field);
|
||||
$stmt->bindParam(':type', $type);
|
||||
foreach ($newFields as $k => $v) {
|
||||
$field = $k;
|
||||
$type = $v;
|
||||
$add->execute();
|
||||
$stmt->execute();
|
||||
}
|
||||
}
|
||||
|
||||
if ($newIndexes) {
|
||||
$add = $dbConn->prepare("CREATE INDEX :name ON :table :column");
|
||||
$add->bindParam(':table', $table);
|
||||
$add->bindParam(':name', $name);
|
||||
$add->bindParam(':column', $column);
|
||||
$stmt = $dbConn->prepare("CREATE INDEX :name ON :table :column");
|
||||
$stmt->bindParam(':table', $table);
|
||||
$stmt->bindParam(':name', $name);
|
||||
$stmt->bindParam(':column', $column);
|
||||
foreach ($newIndexes as $k => $v) {
|
||||
$name = $k;
|
||||
$column = $v;
|
||||
$add->execute();
|
||||
$stmt->execute();
|
||||
}
|
||||
}
|
||||
|
||||
if ($alteredFields) {
|
||||
switch ($parameters['type']) {
|
||||
case "mysql":
|
||||
$alter = $dbConn->prepare("ALTER TABLE :table CHANGE :field :field :type");
|
||||
$stmt = $dbConn->prepare("ALTER TABLE :table CHANGE :field :field :type");
|
||||
break;
|
||||
case "postgresql":
|
||||
$alter = $dbConn->prepare("
|
||||
$stmt = $dbConn->prepare("
|
||||
BEGIN;
|
||||
ALTER TABLE :table RENAME :field TO oldfield;
|
||||
ALTER TABLE :table ADD COLUMN :field :type;
|
||||
@ -288,50 +305,49 @@ class PDODatabase extends Database {
|
||||
");
|
||||
break;
|
||||
case "mssql":
|
||||
$this->dbh->query("ALTER TABLE :table ALTER COLUMN :field :type");
|
||||
$stmt = $dbConn->prepare("ALTER TABLE :table ALTER COLUMN :field :type");
|
||||
break;
|
||||
default:
|
||||
$this->databaseError("This database server is not available");
|
||||
}
|
||||
$alter->bindParam(':table', $table);
|
||||
$alter->bindParam(':field', $field);
|
||||
$alter->bindParam(':type', $type);
|
||||
$stmt->bindParam(':table', $table);
|
||||
$stmt->bindParam(':field', $field);
|
||||
$stmt->bindParam(':type', $type);
|
||||
foreach ($alteredFields as $k => $v) {
|
||||
$field = $k;
|
||||
$type = $v;
|
||||
$alter->execute();
|
||||
$stmt->execute();
|
||||
}
|
||||
}
|
||||
|
||||
if ($alteredIndexes) {
|
||||
$drop = $dbConn->prepare("DROP INDEX :drop");
|
||||
$alter->bindParam(':drop', $drop);
|
||||
$alter = $dbConn->prepare("CREATE INDEX :name ON :table :column");
|
||||
$alter->bindParam(':table', $table);
|
||||
$alter->bindParam(':name', $name);
|
||||
$alter->bindParam(':column', $column);
|
||||
$drop->bindParam(':drop', $drop);
|
||||
$stmt = $dbConn->prepare("CREATE INDEX :name ON :table :column");
|
||||
$stmt->bindParam(':table', $table);
|
||||
$stmt->bindParam(':name', $name);
|
||||
$stmt->bindParam(':column', $column);
|
||||
foreach ($newIndexes as $k => $v) {
|
||||
$drop = $k;
|
||||
$drop->execute();
|
||||
$name = $k;
|
||||
$column = $v;
|
||||
$add->execute();
|
||||
$stmt->execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @return void.
|
||||
*/
|
||||
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();
|
||||
$stmt = $dbConn->prepare("ALTER TABLE :oldTableName RENAME TO :newTableName");
|
||||
$stmt->bindParam(":oldTableName", $oldTableName);
|
||||
$stmt->bindParam(":newTableName", $newTableName);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -377,11 +393,11 @@ class PDODatabase extends Database {
|
||||
* @return void
|
||||
*/
|
||||
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();
|
||||
$stmt = $dbConn->prepare("ALTER TABLE :tableName ADD :fieldName :fieldSpec");
|
||||
$stmt->bindParam(":tableName", $tableName);
|
||||
$stmt->bindParam(":fieldName", $fieldName);
|
||||
$stmt->bindParam(":fieldSpec", $fieldSpec);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -394,10 +410,10 @@ class PDODatabase extends Database {
|
||||
public function alterField($table, $field, $type) {
|
||||
switch ($parameters['type']) {
|
||||
case "mysql":
|
||||
$alter = $dbConn->prepare("ALTER TABLE :table CHANGE :field :field :type");
|
||||
$stmt = $dbConn->prepare("ALTER TABLE :table CHANGE :field :field :type");
|
||||
break;
|
||||
case "postgresql":
|
||||
$alter = $dbConn->prepare("
|
||||
$stmt = $dbConn->prepare("
|
||||
BEGIN;
|
||||
ALTER TABLE :table RENAME :field TO oldfield;
|
||||
ALTER TABLE :table ADD COLUMN :field :type;
|
||||
@ -407,15 +423,15 @@ class PDODatabase extends Database {
|
||||
");
|
||||
break;
|
||||
case "mssql":
|
||||
$this->dbh->query("ALTER TABLE :table ALTER COLUMN :field :type");
|
||||
$stmt = $dbConn->prepare("ALTER TABLE :table ALTER COLUMN :field :type");
|
||||
break;
|
||||
default:
|
||||
$this->databaseError("This database server is not available");
|
||||
}
|
||||
$alter->bindParam(':table', $table);
|
||||
$alter->bindParam(':field', $field);
|
||||
$alter->bindParam(':type', $type);
|
||||
$alter->execute();
|
||||
$stmt->bindParam(':table', $table);
|
||||
$stmt->bindParam(':field', $field);
|
||||
$stmt->bindParam(':type', $type);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -426,11 +442,11 @@ class PDODatabase extends Database {
|
||||
* @return void
|
||||
*/
|
||||
public function createIndex($tableName, $indexName, $indexSpec) {
|
||||
$add = $dbConn->prepare("CREATE INDEX :name ON :table :column");
|
||||
$add->bindParam(':table', $tableName);
|
||||
$add->bindParam(':name', $indexName);
|
||||
$add->bindParam(':column', $indexSpec);
|
||||
$add->execute();
|
||||
$stmt = $dbConn->prepare("CREATE INDEX :name ON :table :column");
|
||||
$stmt->bindParam(':table', $tableName);
|
||||
$stmt->bindParam(':name', $indexName);
|
||||
$stmt->bindParam(':column', $indexSpec);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -442,13 +458,13 @@ class PDODatabase extends Database {
|
||||
*/
|
||||
public function alterIndex($tableName, $indexName, $indexSpec) {
|
||||
$drop = $dbConn->prepare("DROP INDEX :drop");
|
||||
$alter->bindParam(':drop', $indexName);
|
||||
$alter = $dbConn->prepare("CREATE INDEX :name ON :table :column");
|
||||
$alter->bindParam(':table', $tableName);
|
||||
$alter->bindParam(':name', $indexName);
|
||||
$alter->bindParam(':column', $indexSpec);
|
||||
$drop->bindParam(':drop', $indexName);
|
||||
$stmt = $dbConn->prepare("CREATE INDEX :name ON :table :column");
|
||||
$stmt->bindParam(':table', $tableName);
|
||||
$stmt->bindParam(':name', $indexName);
|
||||
$stmt->bindParam(':column', $indexSpec);
|
||||
$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() {
|
||||
return $stmt->rowCount();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A result-set from a database query (array).
|
||||
* A result-set from a database query (array).
|
||||
*/
|
||||
class PDOQuery extends Query {
|
||||
private $result;
|
||||
|
Loading…
Reference in New Issue
Block a user