2007-09-14 03:23:36 +02:00
|
|
|
<?php
|
|
|
|
|
2007-09-14 03:28:16 +02:00
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage core
|
|
|
|
*/
|
|
|
|
|
2007-09-14 03:23:36 +02:00
|
|
|
/**
|
|
|
|
* PDO (general database) connector class.
|
|
|
|
*/
|
|
|
|
class PDODatabase extends Database {
|
2007-09-14 03:28:16 +02:00
|
|
|
/**
|
|
|
|
* Connection to the DBMS.
|
|
|
|
* @var resource
|
|
|
|
*/
|
2007-09-14 03:23:36 +02:00
|
|
|
private $dbConn;
|
2007-09-14 03:28:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* True if we are connected to a database.
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2007-09-14 03:23:36 +02:00
|
|
|
private $active;
|
2007-09-14 03:28:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the database.
|
|
|
|
* @var string
|
|
|
|
*/
|
2007-09-14 03:23:36 +02:00
|
|
|
private $database;
|
|
|
|
|
|
|
|
|
2007-09-14 03:32:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Last PDO statement, needed for affectedRows()
|
|
|
|
* @var PDO object
|
|
|
|
*/
|
|
|
|
private $stmt;
|
|
|
|
|
2007-09-14 03:45:06 +02:00
|
|
|
/**
|
|
|
|
* Parameters used for creating a connection
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $param;
|
|
|
|
|
2007-09-14 03:23:36 +02:00
|
|
|
/**
|
|
|
|
* Connect to a database (MySQL, PostgreSQL, or MS SQL).
|
|
|
|
* @param parameters An map of parameters, which should include:
|
|
|
|
* <ul><li>database: The database to connect with</li>
|
|
|
|
* <li>server: The server, eg, localhost</li>
|
2007-09-14 03:28:16 +02:00
|
|
|
* <li>port: The port on which the server is listening (optional)</li>
|
|
|
|
* <li>instance: Instance of the server, MS SQL only (optional)</li>
|
2007-09-14 03:23:36 +02:00
|
|
|
* <li>username: The username to log on with</li>
|
|
|
|
* <li>password: The password to log on with</li>
|
|
|
|
* <li>database: The database to connect to</li></ul>
|
|
|
|
*/
|
|
|
|
public function __construct($parameters) {
|
2007-09-14 03:45:06 +02:00
|
|
|
$this->param = $parameters;
|
2007-09-14 03:44:34 +02:00
|
|
|
$connect = self::getConnect($parameters);
|
2007-09-14 03:28:16 +02:00
|
|
|
$connectWithDB = $connect . ';dbname=' . $parameters['database'];
|
|
|
|
try { // Try connect to the database, if it does not exist, create it
|
|
|
|
$this->dbConn = new PDO($connectWithDB, $parameters['username'], $parameters['password']);
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
if (!self::createDatabase($connect, $parameters['username'], $parameters['password'], $parameters['database'])) {
|
|
|
|
$this->databaseError("Could not connect to the database, make sure the server is available and user credentials are correct");
|
2007-09-14 03:44:34 +02:00
|
|
|
} else {
|
|
|
|
$this->dbConn = new PDO($connectWithDB, $parameters['username'], $parameters['password']); // After creating the database, connect to it
|
2007-09-14 03:28:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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 = '';
|
2007-09-14 03:23:36 +02:00
|
|
|
break;
|
2007-09-14 03:28:16 +02:00
|
|
|
case "postgresql":
|
|
|
|
$port = '5432';
|
|
|
|
$type = 'pgsql';
|
|
|
|
$instance = '';
|
2007-09-14 03:23:36 +02:00
|
|
|
break;
|
2007-09-14 03:28:16 +02:00
|
|
|
case "mssql":
|
|
|
|
$port = '1433';
|
|
|
|
if (isset($parameters['instance']) && $parameters['instance'] != '') {
|
|
|
|
$instance = '\\' . $parameters['instance'];
|
|
|
|
} else {
|
|
|
|
$instance = '';
|
|
|
|
}
|
|
|
|
$type = 'mssql';
|
2007-09-14 03:23:36 +02:00
|
|
|
break;
|
2007-09-14 03:28:16 +02:00
|
|
|
default:
|
|
|
|
$this->databaseError("This database server is not available");
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
2007-09-14 03:28:16 +02:00
|
|
|
if (isset($parameters['port']) && is_numeric($parameters['port'])) {
|
|
|
|
$port = $parameters['port'];
|
|
|
|
}
|
|
|
|
$connect = $type . ':host=' . $parameters['server'] . $instance . ';port=' . $port;
|
|
|
|
return $connect;
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this database supports collations
|
|
|
|
*/
|
|
|
|
public function supportsCollations() {
|
2007-09-14 03:28:16 +02:00
|
|
|
$collations = false;
|
2007-09-14 03:44:34 +02:00
|
|
|
switch (self::getDatabaseServer()) {
|
|
|
|
case "pgsql": // Generally supported in PostgreSQL (supported versions), but handled differently than in MySQL, so do not set
|
|
|
|
case "mssql": // Generally supported in MS SQL (supported versions), but handled differently than in MySQL, so do not set
|
|
|
|
$collations = false;
|
2007-09-14 03:28:16 +02:00
|
|
|
break;
|
|
|
|
case "mysql":
|
2007-09-14 03:44:34 +02:00
|
|
|
if (self::getVersion() >= 4.1) { // Supported in MySQL since 4.1
|
2007-09-14 03:28:16 +02:00
|
|
|
$collations = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $collations;
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 03:28:16 +02:00
|
|
|
/**
|
|
|
|
* Get the database version.
|
|
|
|
* @return float
|
|
|
|
*/
|
2007-09-14 03:23:36 +02:00
|
|
|
public function getVersion() {
|
2007-09-14 03:44:34 +02:00
|
|
|
switch (self::getDatabaseServer()) {
|
2007-09-14 03:28:16 +02:00
|
|
|
case "mysql":
|
2007-09-14 03:39:37 +02:00
|
|
|
case "pgsql":
|
2007-09-14 03:28:16 +02:00
|
|
|
$query = "SELECT VERSION()";
|
|
|
|
break;
|
|
|
|
case "mssql":
|
|
|
|
$query = "SELECT @@VERSION";
|
|
|
|
break;
|
|
|
|
}
|
2007-09-14 03:44:34 +02:00
|
|
|
$stmt = $this->dbConn->prepare($query);
|
2007-09-14 03:32:37 +02:00
|
|
|
$stmt->execute();
|
|
|
|
$dbVersion = $stmt->fetchColumn();
|
2007-09-14 03:28:16 +02:00
|
|
|
$version = ereg_replace("([A-Za-z-])", "", $dbVersion);
|
|
|
|
return substr(trim($version), 0, 3); // Just get the major and minor version
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 03:44:34 +02:00
|
|
|
/**
|
|
|
|
* Get the database server, namely mysql, pgsql, or mssql.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDatabaseServer() {
|
|
|
|
return $this->dbConn->getAttribute(PDO::ATTR_DRIVER_NAME);
|
|
|
|
}
|
|
|
|
|
2007-09-14 03:29:09 +02:00
|
|
|
/**
|
|
|
|
* Query the database.
|
|
|
|
* @var string $sql The query to be issued to the database.
|
|
|
|
* @return result Return the result of the quers (if any).
|
|
|
|
*/
|
2007-09-14 03:23:36 +02:00
|
|
|
public function query($sql, $errorLevel = E_USER_ERROR) {
|
2007-09-14 03:28:16 +02:00
|
|
|
if(isset($_REQUEST['previewwrite']) && in_array(strtolower(substr($sql,0,6)), array('insert','update'))) {
|
|
|
|
echo "<p>Will execute: $sql</p>";
|
|
|
|
return;
|
|
|
|
}
|
2007-09-14 03:29:09 +02:00
|
|
|
//Debug::backtrace();
|
2007-09-14 03:28:16 +02:00
|
|
|
if(isset($_REQUEST['showqueries'])) {
|
|
|
|
Debug::message("\n" . $sql . "\n");
|
|
|
|
$starttime = microtime(true);
|
|
|
|
}
|
|
|
|
|
2007-09-14 03:32:37 +02:00
|
|
|
$stmt = $dbConn->prepare($sql);
|
2007-09-14 03:44:34 +02:00
|
|
|
|
|
|
|
$stmt = $this->dbConn->prepare($sql);
|
2007-09-14 03:32:37 +02:00
|
|
|
$handle = $stmt->execute(); // Execute and save the return value (true or false)
|
|
|
|
|
2007-09-14 03:28:16 +02:00
|
|
|
if(isset($_REQUEST['showqueries'])) {
|
|
|
|
$duration = microtime(true) - $starttime;
|
|
|
|
Debug::message("\n" . $duration . "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$handle && $errorLevel) {
|
2007-09-14 03:32:37 +02:00
|
|
|
$error = $stmt->errorInfo();
|
2007-09-14 03:28:16 +02:00
|
|
|
$this->databaseError("Couldn't run query: $sql | " . $error[2], $errorLevel);
|
|
|
|
}
|
2007-09-14 03:44:34 +02:00
|
|
|
return new PDOQuery($this, $stmt);
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
2007-09-14 03:28:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ID for the next new record for the table.
|
2007-09-14 03:32:37 +02:00
|
|
|
* Get the autogenerated ID from the previous INSERT query.
|
2007-09-14 03:35:54 +02:00
|
|
|
* Simulate mysql_insert_id by fetching the highest ID as there is no other reliable method across databases.
|
2007-09-14 03:32:37 +02:00
|
|
|
* @return int
|
|
|
|
*/
|
2007-09-14 03:35:54 +02:00
|
|
|
public function getGeneratedID($table) {
|
2007-09-14 03:44:34 +02:00
|
|
|
$stmt = $this->dbConn->prepare("SELECT MAX(ID) FROM $table");
|
2007-09-14 03:35:54 +02:00
|
|
|
$handle = $stmt->execute();
|
|
|
|
$result = $stmt->fetchColumn();
|
|
|
|
return $handle ? $result : 0;
|
2007-09-14 03:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OBSOLETE: Get the ID for the next new record for the table.
|
2007-09-14 03:28:16 +02:00
|
|
|
* @var string $table The name od the table.
|
|
|
|
* @return int
|
|
|
|
*/
|
2007-09-14 03:23:36 +02:00
|
|
|
public function getNextID($table) {
|
2007-09-14 03:32:37 +02:00
|
|
|
user_error('getNextID is OBSOLETE (and will no longer work properly)', E_USER_WARNING);
|
2007-09-14 03:44:34 +02:00
|
|
|
$stmt = $this->dbConn->prepare("SELECT MAX(ID)+1 FROM $table");
|
2007-09-14 03:32:37 +02:00
|
|
|
$handle = $stmt->execute();
|
|
|
|
$result = $stmt->fetchColumn();
|
2007-09-14 03:28:16 +02:00
|
|
|
return $handle ? $result : 1;
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
2007-09-14 03:28:16 +02:00
|
|
|
|
2007-09-14 03:29:09 +02:00
|
|
|
/**
|
|
|
|
* Determine if the the table is active.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2007-09-14 03:23:36 +02:00
|
|
|
public function isActive() {
|
2007-09-14 03:28:16 +02:00
|
|
|
return $this->active ? true : false;
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
2007-09-14 03:28:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2007-09-14 03:44:34 +02:00
|
|
|
$dbh = new PDO($connect, $username, $password);
|
|
|
|
$stmt = $dbh->prepare("CREATE DATABASE $database");
|
2007-09-14 03:32:37 +02:00
|
|
|
$stmt->execute();
|
2007-09-14 03:28:16 +02:00
|
|
|
$this->active = true;
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
$this->databaseError($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
2007-09-14 03:45:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the named database exists.
|
|
|
|
*/
|
|
|
|
public function databaseExists($name) {
|
|
|
|
$SQL_name = Convert::raw2sql($name);
|
|
|
|
$connect = self::getConnect($this->param);
|
|
|
|
$connectWithDB = $connect . ';dbname=' . $SQL_name;
|
|
|
|
try { // Try connect to the database
|
|
|
|
$testConn = new PDO($connectWithDB, $this->param['username'], $this->param['password']);
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Switches to the given database.
|
|
|
|
* Simply switching database in PDO is not possible, you need to create a new PDO object
|
|
|
|
*/
|
|
|
|
public function selectDatabase($dbname) {
|
|
|
|
$this->dbConn = null; // Remove the old connection
|
|
|
|
$connect = self::getConnect($param);
|
|
|
|
$connectWithDB = $connect . ';dbname=' . $dbname;
|
|
|
|
try { // Try connect to the database, if it does not exist, create it
|
|
|
|
$this->dbConn = new PDO($connectWithDB, $param['username'], $param['password']);
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
if (!self::createDatabase($connect, $param['username'], $param['password'], $dbname)) {
|
|
|
|
$this->databaseError("Could not connect to the database, make sure the server is available and user credentials are correct");
|
|
|
|
} else {
|
|
|
|
$this->dbConn = new PDO($connectWithDB, $param['username'], $param['password']); // After creating the database, connect to it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-14 03:23:36 +02:00
|
|
|
/**
|
|
|
|
* Create a new table with an integer primary key called ID.
|
2007-09-14 03:28:16 +02:00
|
|
|
* @var string $tableName The name of the table.
|
|
|
|
* @return void.
|
2007-09-14 03:23:36 +02:00
|
|
|
*/
|
2007-09-14 03:28:16 +02:00
|
|
|
public function createTable($tableName, $fields = null, $indexes = null) {
|
|
|
|
$fieldSchemas = $indexSchemas = "";
|
|
|
|
if ($fields) {
|
|
|
|
foreach($fields as $k => $v) $fieldSchemas .= "`$k` $v,\n";
|
|
|
|
}
|
|
|
|
|
2007-09-14 03:44:34 +02:00
|
|
|
switch (self::getDatabaseServer()) {
|
2007-09-14 03:29:09 +02:00
|
|
|
case "mysql":
|
2007-09-14 03:44:34 +02:00
|
|
|
$stmt = $this->dbConn->prepare("CREATE TABLE $tableName (ID INT(11) NOT NULL AUTO_INCREMENT, $fieldSchemas PRIMARY KEY (ID)) TYPE=MyISAM");
|
2007-09-14 03:28:16 +02:00
|
|
|
break;
|
2007-09-14 03:39:37 +02:00
|
|
|
case "pgsql":
|
2007-09-14 03:44:34 +02:00
|
|
|
$stmt = $this->dbConn->prepare("CREATE TABLE $tableName (ID SERIAL, $fieldSchemas PRIMARY KEY (ID))");
|
2007-09-14 03:28:16 +02:00
|
|
|
break;
|
2007-09-14 03:29:09 +02:00
|
|
|
case "mssql":
|
2007-09-14 03:44:34 +02:00
|
|
|
$stmt = $this->dbConn->prepare("CREATE TABLE $tableName (ID INT(11) IDENTITY(1,1), $fieldSchemas PRIMARY KEY (ID))");
|
2007-09-14 03:28:16 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->databaseError("This database server is not available");
|
|
|
|
}
|
2007-09-14 03:32:37 +02:00
|
|
|
$stmt->execute();
|
|
|
|
|
2007-09-14 03:29:09 +02:00
|
|
|
if ($indexes) {
|
2007-09-14 03:44:34 +02:00
|
|
|
self::alterTable($tableName, null, $indexes, null, null);
|
2007-09-14 03:29:09 +02:00
|
|
|
}
|
2007-09-14 03:28:16 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 03:29:09 +02:00
|
|
|
/**
|
|
|
|
* Alter fields and indexes in existing table.
|
|
|
|
* @var string $tableName The name of the table.
|
|
|
|
* @var string $newFields Fields to add.
|
|
|
|
* @var string $newIndexes Indexes to add.
|
|
|
|
* @var string $alteredFields Fields to change.
|
|
|
|
* @var string $alteredIndexes Indexes to change.
|
|
|
|
* @return void.
|
|
|
|
*/
|
2007-09-14 03:44:34 +02:00
|
|
|
public function alterTable($table, $newFields = null, $newIndexes = null, $alteredFields = null, $alteredIndexes = null) {
|
|
|
|
|
2007-09-14 03:28:16 +02:00
|
|
|
if ($newFields) {
|
2007-09-14 03:39:37 +02:00
|
|
|
foreach ($newFields as $field => $type) {
|
2007-09-14 03:44:34 +02:00
|
|
|
$stmt = $this->dbConn->prepare("ALTER TABLE $table ADD $field $type");
|
2007-09-14 03:32:37 +02:00
|
|
|
$stmt->execute();
|
2007-09-14 03:29:09 +02:00
|
|
|
}
|
2007-09-14 03:28:16 +02:00
|
|
|
}
|
2007-09-14 03:29:09 +02:00
|
|
|
|
2007-09-14 03:28:16 +02:00
|
|
|
if ($newIndexes) {
|
2007-09-14 03:39:37 +02:00
|
|
|
foreach ($newIndexes as $name => $column) {
|
2007-09-14 03:44:34 +02:00
|
|
|
$stmt = $this->dbConn->prepare("CREATE INDEX $name ON $table $column");
|
2007-09-14 03:32:37 +02:00
|
|
|
$stmt->execute();
|
2007-09-14 03:29:09 +02:00
|
|
|
}
|
2007-09-14 03:28:16 +02:00
|
|
|
}
|
2007-09-14 03:29:09 +02:00
|
|
|
|
2007-09-14 03:28:16 +02:00
|
|
|
if ($alteredFields) {
|
2007-09-14 03:39:37 +02:00
|
|
|
foreach ($alteredFields as $field => $type) {
|
2007-09-14 03:44:34 +02:00
|
|
|
self::alterField($table, $field, $type);
|
2007-09-14 03:29:09 +02:00
|
|
|
}
|
2007-09-14 03:28:16 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 03:29:09 +02:00
|
|
|
if ($alteredIndexes) {
|
2007-09-14 03:39:37 +02:00
|
|
|
foreach ($newIndexes as $name => $column) {
|
2007-09-14 03:44:34 +02:00
|
|
|
$this->dbConn->query("DROP INDEX $name");
|
|
|
|
$stmt = $this->dbConn->prepare("CREATE INDEX $name ON $table $column");
|
2007-09-14 03:32:37 +02:00
|
|
|
$stmt->execute();
|
2007-09-14 03:29:09 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
2007-09-14 03:29:09 +02:00
|
|
|
|
2007-09-14 03:23:36 +02:00
|
|
|
/**
|
2007-09-14 03:28:16 +02:00
|
|
|
* Rename an existing table, the TO is necessary for PostgreSQL and MS SQL.
|
2007-09-14 03:32:37 +02:00
|
|
|
* @var string $oldTableName The name of the existing table.
|
2007-09-14 03:28:16 +02:00
|
|
|
* @var string $newTableName How the table should be named from now on.
|
|
|
|
* @return void.
|
2007-09-14 03:23:36 +02:00
|
|
|
*/
|
|
|
|
public function renameTable($oldTableName, $newTableName) {
|
2007-09-14 03:44:34 +02:00
|
|
|
$stmt = $this->dbConn->prepare("ALTER TABLE $oldTableName RENAME TO $newTableName");
|
2007-09-14 03:32:37 +02:00
|
|
|
$stmt->execute();
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-09-14 03:29:09 +02:00
|
|
|
* Checks a table's integrity and repairs it if necessary - only available in MySQL, not supported in PostgreSQL and MS SQL.
|
|
|
|
* @var string $tableName The name of the table.
|
|
|
|
* @return boolean Return true if the table has integrity after the method is complete.
|
2007-09-14 03:23:36 +02:00
|
|
|
*/
|
|
|
|
public function checkAndRepairTable($tableName) {
|
2007-09-14 03:29:09 +02:00
|
|
|
if ($parameters['type'] == "mysql") {
|
|
|
|
if (!$this->runTableCheckCommand("CHECK TABLE `$tableName`")) {
|
|
|
|
if(!Database::$supressOutput) {
|
|
|
|
echo "<li style=\"color: orange\">Table $tableName: repaired</li>";
|
|
|
|
}
|
|
|
|
return $this->runTableCheckCommand("REPAIR TABLE `$tableName` USE_FRM");
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->databaseError("Checking and repairing of tables is only supported in MySQL, for other databases please do manual checks");
|
|
|
|
return false;
|
|
|
|
}
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
2007-09-14 03:29:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function used by checkAndRepairTable.
|
|
|
|
* @param string $sql Query to run.
|
|
|
|
* @return boolean Returns if the query returns a successful result.
|
|
|
|
*/
|
2007-09-14 03:23:36 +02:00
|
|
|
protected function runTableCheckCommand($sql) {
|
2007-09-14 03:44:34 +02:00
|
|
|
foreach($this->dbConn->query($sql) as $testRecord) {
|
2007-09-14 03:29:09 +02:00
|
|
|
if(strtolower($testRecord['Msg_text']) != 'ok') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the given field to the given table.
|
2007-09-14 03:29:09 +02:00
|
|
|
* @param string $tableName The name of the table on which to create the field.
|
|
|
|
* @param string $fieldName The field to create.
|
|
|
|
* @param string $fieldSpec The datatype of the field.
|
|
|
|
* @return void
|
2007-09-14 03:23:36 +02:00
|
|
|
*/
|
|
|
|
public function createField($tableName, $fieldName, $fieldSpec) {
|
2007-09-14 03:44:34 +02:00
|
|
|
$stmt = $this->dbConn->prepare("ALTER TABLE $tableName ADD $fieldName $fieldSpec");
|
2007-09-14 03:32:37 +02:00
|
|
|
$stmt->execute();
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
2007-09-14 03:28:16 +02:00
|
|
|
|
2007-09-14 03:23:36 +02:00
|
|
|
/**
|
2007-09-14 03:29:09 +02:00
|
|
|
* Change the database type of the given field.
|
|
|
|
* @param string $table The table where to change the field.
|
|
|
|
* @param string $field The field to change.
|
|
|
|
* @param string $type The new type of the field
|
|
|
|
* @return void
|
2007-09-14 03:23:36 +02:00
|
|
|
*/
|
2007-09-14 03:29:09 +02:00
|
|
|
public function alterField($table, $field, $type) {
|
2007-09-14 03:44:34 +02:00
|
|
|
switch (self::getDatabaseServer()) {
|
2007-09-14 03:29:09 +02:00
|
|
|
case "mysql":
|
2007-09-14 03:44:34 +02:00
|
|
|
$stmt = $this->dbConn->prepare("ALTER TABLE $table CHANGE $field $field $type");
|
2007-09-14 03:29:09 +02:00
|
|
|
break;
|
2007-09-14 03:39:37 +02:00
|
|
|
case "pgsql":
|
2007-09-14 03:44:34 +02:00
|
|
|
$stmt = $this->dbConn->prepare("
|
2007-09-14 03:29:09 +02:00
|
|
|
BEGIN;
|
2007-09-14 03:39:37 +02:00
|
|
|
ALTER TABLE $table RENAME $field TO oldfield;
|
|
|
|
ALTER TABLE $table ADD COLUMN $field $type;
|
|
|
|
UPDATE $table SET $field = CAST(oldfield AS $type);
|
|
|
|
ALTER TABLE $table DROP COLUMN oldfield;
|
2007-09-14 03:29:09 +02:00
|
|
|
COMMIT;
|
|
|
|
");
|
|
|
|
break;
|
|
|
|
case "mssql":
|
2007-09-14 03:44:34 +02:00
|
|
|
$stmt = $this->dbConn->prepare("ALTER TABLE $table ALTER COLUMN $field $type");
|
2007-09-14 03:29:09 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->databaseError("This database server is not available");
|
|
|
|
}
|
2007-09-14 03:32:37 +02:00
|
|
|
$stmt->execute();
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-09-14 03:29:09 +02:00
|
|
|
* Create an index on a table.
|
|
|
|
* @param string $tableName The name of the table.
|
|
|
|
* @param string $indexName The name of the index.
|
|
|
|
* @param string $indexSpec The specification of the index, see Database::requireIndex() for more details.
|
|
|
|
* @return void
|
2007-09-14 03:23:36 +02:00
|
|
|
*/
|
|
|
|
public function createIndex($tableName, $indexName, $indexSpec) {
|
2007-09-14 03:44:34 +02:00
|
|
|
$stmt = $this->dbConn->prepare("CREATE INDEX $indexName ON $tableName $indexSpec");
|
2007-09-14 03:32:37 +02:00
|
|
|
$stmt->execute();
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
2007-09-14 03:29:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Alter an index on a table.
|
|
|
|
* @param string $tableName The name of the table.
|
|
|
|
* @param string $indexName The name of the index.
|
|
|
|
* @param string $indexSpec The specification of the index, see Database::requireIndex() for more details.
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-09-14 03:23:36 +02:00
|
|
|
public function alterIndex($tableName, $indexName, $indexSpec) {
|
2007-09-14 03:44:34 +02:00
|
|
|
$this->dbConn->query("DROP INDEX $indexName");
|
|
|
|
$stmt = $this->dbConn->prepare("CREATE INDEX $indexName ON $tableName $indexSpec");
|
2007-09-14 03:32:37 +02:00
|
|
|
$stmt->execute();
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
2007-09-14 03:29:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of all the fields for the given table.
|
2007-09-14 03:39:37 +02:00
|
|
|
* The results are not totally equal for all databases (for example collations are handled very differently, PostgreSQL disregards zerofill,...)
|
|
|
|
* but as close as possible and necessary.
|
2007-09-14 03:29:09 +02:00
|
|
|
* @param string $able Table of which to show the fields.
|
|
|
|
* Returns a map of field name => field spec.
|
|
|
|
*/
|
|
|
|
public function fieldList($table) {
|
2007-09-14 03:44:34 +02:00
|
|
|
switch (self::getDatabaseServer()) {
|
2007-09-14 03:39:37 +02:00
|
|
|
case "mysql":
|
2007-09-14 03:44:34 +02:00
|
|
|
foreach ($this->dbConn->query("SHOW FULL FIELDS IN $table") as $field) {
|
2007-09-14 03:39:37 +02:00
|
|
|
$fieldSpec = $field['Type'];
|
|
|
|
if(!$field['Null'] || $field['Null'] == 'NO') {
|
|
|
|
$fieldSpec .= ' not null';
|
|
|
|
}
|
|
|
|
if($field['Collation'] && $field['Collation'] != 'NULL') {
|
2007-09-14 03:44:34 +02:00
|
|
|
$values = $this->dbConn->prepare("SHOW COLLATION LIKE '$field[Collation]'");
|
2007-09-14 03:39:37 +02:00
|
|
|
$values->execute();
|
|
|
|
$collInfo = $values->fetchColumn();
|
|
|
|
$fieldSpec .= " character set $collInfo[Charset] collate $field[Collation]";
|
|
|
|
}
|
|
|
|
if($field['Default'] || $field['Default'] === "0") {
|
|
|
|
$fieldSpec .= " default '" . addslashes($field['Default']) . "'";
|
|
|
|
}
|
|
|
|
if($field['Extra']) $fieldSpec .= " $field[Extra]";
|
|
|
|
$fieldList[$field['Field']] = $fieldSpec;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "pgsql":
|
2007-09-14 03:44:34 +02:00
|
|
|
foreach ($this->dbConn->query("
|
2007-09-14 03:39:37 +02:00
|
|
|
SELECT
|
2007-09-14 03:41:13 +02:00
|
|
|
column_name AS cname,
|
|
|
|
column_default AS cdefault,
|
|
|
|
is_nullable AS nullable,
|
|
|
|
data_type AS dtype,
|
|
|
|
character_maximum_length AS maxlength
|
2007-09-14 03:39:37 +02:00
|
|
|
FROM
|
2007-09-14 03:41:13 +02:00
|
|
|
information_schema.columns
|
2007-09-14 03:39:37 +02:00
|
|
|
WHERE
|
2007-09-14 03:41:13 +02:00
|
|
|
table_name = $table
|
2007-09-14 03:39:37 +02:00
|
|
|
") as $field) {
|
2007-09-14 03:41:13 +02:00
|
|
|
if ($field['maxlength']) {
|
|
|
|
$fieldSpec = $field['dtype'] . "(" . $field['maxlength'] . ")";
|
|
|
|
} else {
|
|
|
|
$fieldSpec = $field['dtype'];
|
|
|
|
}
|
|
|
|
if ($field['nullable'] == 'NO') {
|
2007-09-14 03:39:37 +02:00
|
|
|
$fieldSpec .= ' not null';
|
|
|
|
}
|
2007-09-14 03:41:13 +02:00
|
|
|
if($field['cdefault'] || $field['cdefault'] === "0") {
|
|
|
|
$fieldSpec .= " default '" . addslashes($field['cdefault']) . "'";
|
|
|
|
}
|
|
|
|
$fieldList[$field['cname']] = $fieldSpec;
|
2007-09-14 03:39:37 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "mssql":
|
2007-09-14 03:44:34 +02:00
|
|
|
foreach ($this->dbConn->query("
|
2007-09-14 03:39:37 +02:00
|
|
|
SELECT
|
2007-09-14 03:41:13 +02:00
|
|
|
COLUMN_NAME AS 'cname',
|
|
|
|
COLUMN_DEFAULT AS 'cdefault',
|
|
|
|
IS_NULLABLE AS 'nullable',
|
|
|
|
DATA_TYPE AS 'dtype',
|
|
|
|
COLLATION_NAME AS 'collname',
|
|
|
|
CHARACTER_SET_NAME AS 'cset',
|
|
|
|
CHARACTER_MAXIMUM_LENGTH AS 'maxlength'
|
2007-09-14 03:39:37 +02:00
|
|
|
FROM
|
|
|
|
information_schema.columns
|
|
|
|
WHERE
|
2007-09-14 03:41:13 +02:00
|
|
|
TABLE_NAME = '$table'
|
2007-09-14 03:39:37 +02:00
|
|
|
") as $field) {
|
2007-09-14 03:41:13 +02:00
|
|
|
if ($field['maxlength']) {
|
|
|
|
$fieldSpec = $field['dtype'] . "(" . $field['maxlength'] . ")";
|
|
|
|
} else {
|
|
|
|
$fieldSpec = $field['dtype'];
|
|
|
|
}
|
|
|
|
if ($field['nullable'] == 'NO') {
|
2007-09-14 03:39:37 +02:00
|
|
|
$fieldSpec .= ' not null';
|
|
|
|
}
|
|
|
|
|
2007-09-14 03:41:13 +02:00
|
|
|
if($field['collname'] && $field['collname'] != 'NULL') {
|
|
|
|
$fieldSpec .= " character set $field[cset] collate $field[collname]";
|
2007-09-14 03:39:37 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 03:41:13 +02:00
|
|
|
if($field['cdefault'] || $field['cdefault'] === "0") {
|
|
|
|
$fieldSpec .= " default '" . addslashes($field['cdefault']) . "'";
|
2007-09-14 03:39:37 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 03:41:13 +02:00
|
|
|
$fieldList[$field['cname']] = $fieldSpec;
|
2007-09-14 03:39:37 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->databaseError("This database server is not available");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fieldList;
|
2007-09-14 03:29:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of all the indexes for the given table.
|
|
|
|
* @param string $able Table of which to show the indexes.
|
|
|
|
* Returns a map of indexes.
|
|
|
|
*/
|
2007-09-14 03:23:36 +02:00
|
|
|
public function indexList($table) {
|
2007-09-14 03:44:34 +02:00
|
|
|
switch (self::getDatabaseServer()) {
|
2007-09-14 03:41:13 +02:00
|
|
|
case "mysql":
|
2007-09-14 03:44:34 +02:00
|
|
|
foreach($this->dbConn->query("SHOW INDEXES IN '$table'") as $index) {
|
2007-09-14 03:41:13 +02:00
|
|
|
$groupedIndexes[$index['Key_name']]['fields'][$index['Seq_in_index']] = $index['Column_name'];
|
|
|
|
if($index['Index_type'] == 'FULLTEXT') {
|
|
|
|
$groupedIndexes[$index['Key_name']]['type'] = 'fulltext ';
|
|
|
|
} else if(!$index['Non_unique']) {
|
|
|
|
$groupedIndexes[$index['Key_name']]['type'] = 'unique ';
|
|
|
|
} else {
|
|
|
|
$groupedIndexes[$index['Key_name']]['type'] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach($groupedIndexes as $index => $details) {
|
|
|
|
ksort($details['fields']);
|
|
|
|
$indexList[$index] = $details['type'] . '(' . implode(',',$details['fields']) . ')';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "pgsql":
|
2007-09-14 03:44:34 +02:00
|
|
|
foreach($this->dbConn->query("SELECT indexname, indexdef FROM pg_indexes WHERE tablename = '$table'") as $index) {
|
2007-09-14 03:41:13 +02:00
|
|
|
$indexList[$index['indexname']] = $index['indexdef'];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "mssql":
|
2007-09-14 03:44:34 +02:00
|
|
|
foreach($this->dbConn->query("
|
2007-09-14 03:41:13 +02:00
|
|
|
SELECT
|
|
|
|
i.name AS 'iname',
|
|
|
|
i.type_desc AS 'itype',
|
|
|
|
s.name AS 'sname'
|
|
|
|
FROM
|
|
|
|
sys.indexes i,
|
|
|
|
sys.objects o,
|
|
|
|
sys.index_columns c,
|
|
|
|
sys.columns s
|
|
|
|
WHERE
|
|
|
|
o.name = '$table'
|
|
|
|
AND o.object_id = i.object_id
|
|
|
|
AND o.object_id = c.object_id
|
|
|
|
AND o.object_id = s.object_id
|
|
|
|
AND s.column_id = c.column_id
|
|
|
|
") as $index) {
|
|
|
|
$indexList[$index['iname']] = $index['itype'] . " (" . $index['sname'] . ")";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->databaseError("This database server is not available");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $indexList;
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
2007-09-14 03:29:09 +02:00
|
|
|
|
2007-09-14 03:23:36 +02:00
|
|
|
/**
|
2007-09-14 03:37:48 +02:00
|
|
|
* Returns a list of all the tables in the database.
|
2007-09-14 03:29:09 +02:00
|
|
|
* Table names will all be in lowercase.
|
|
|
|
* Returns a map of a table.
|
2007-09-14 03:23:36 +02:00
|
|
|
*/
|
|
|
|
public function tableList() {
|
2007-09-14 03:44:34 +02:00
|
|
|
switch (self::getDatabaseServer()) {
|
2007-09-14 03:37:48 +02:00
|
|
|
case "mysql":
|
|
|
|
$sql = "SHOW TABLES";
|
|
|
|
break;
|
2007-09-14 03:39:37 +02:00
|
|
|
case "pgsql":
|
2007-09-14 03:37:48 +02:00
|
|
|
$sql = "SELECT tablename FROM pg_tables WHERE tablename NOT ILIKE 'pg_%' AND tablename NOT ILIKE 'sql_%'";
|
|
|
|
break;
|
|
|
|
case "mssql":
|
|
|
|
$sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME NOT LIKE 'sysdiagrams%'";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->databaseError("This database server is not available");
|
|
|
|
}
|
2007-09-14 03:44:34 +02:00
|
|
|
if (is_array($this->dbConn->query($sql))) {
|
|
|
|
foreach($this->dbConn->query($sql) as $record) {
|
|
|
|
$table = strtolower(reset($record));
|
|
|
|
$tables[$table] = $table;
|
|
|
|
}
|
2007-09-14 03:37:48 +02:00
|
|
|
}
|
|
|
|
return isset($tables) ? $tables : null;
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-09-14 03:32:37 +02:00
|
|
|
* Return the number of rows affected (DELETE, INSERT, or UPDATE) by the previous operation.
|
2007-09-14 03:23:36 +02:00
|
|
|
*/
|
|
|
|
public function affectedRows() {
|
2007-09-14 03:32:37 +02:00
|
|
|
return $stmt->rowCount();
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-09-14 03:32:37 +02:00
|
|
|
* A result-set from a database query (array).
|
2007-09-14 03:23:36 +02:00
|
|
|
*/
|
2007-09-14 03:28:16 +02:00
|
|
|
class PDOQuery extends Query {
|
2007-09-14 03:35:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The object that holds the result set.
|
|
|
|
* @var $stmt
|
|
|
|
*/
|
|
|
|
private $stmt;
|
2007-09-14 03:23:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook the result-set given into a Query class, suitable for use by sapphire.
|
2007-09-14 03:35:54 +02:00
|
|
|
* @param PDO object $stmt The object of all returned values.
|
2007-09-14 03:23:36 +02:00
|
|
|
*/
|
2007-09-14 03:44:34 +02:00
|
|
|
public function __construct(PDODatabase $database, $stmt) {
|
|
|
|
$this->database = $database;
|
2007-09-14 03:35:54 +02:00
|
|
|
$this->stmt = $stmt;
|
2007-09-14 03:23:36 +02:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2007-09-14 03:35:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Free the result-set given into a Query class.
|
|
|
|
*/
|
2007-09-14 03:23:36 +02:00
|
|
|
public function __destroy() {
|
2007-09-14 03:35:54 +02:00
|
|
|
$this->stmt = null;
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 03:35:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if a given element is part of the result set.
|
|
|
|
* @param string string $row The element to search for.
|
|
|
|
*/
|
2007-09-14 03:23:36 +02:00
|
|
|
public function seek($row) {
|
2007-09-14 03:35:54 +02:00
|
|
|
return in_array($row, $this->stmt->fetchAll());
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
2007-09-14 03:35:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of results.
|
|
|
|
*/
|
2007-09-14 03:23:36 +02:00
|
|
|
public function numRecords() {
|
2007-09-14 03:35:54 +02:00
|
|
|
$value = $this->stmt->fetchAll();
|
|
|
|
return count($value);
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 03:35:54 +02:00
|
|
|
|
|
|
|
/**
|
2007-09-14 03:41:13 +02:00
|
|
|
*
|
2007-09-14 03:35:54 +02:00
|
|
|
*/
|
2007-09-14 03:23:36 +02:00
|
|
|
public function nextRecord() {
|
2007-09-14 03:35:54 +02:00
|
|
|
$record = $this->stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (count($record)) {
|
|
|
|
return $record;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2007-09-14 03:23:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|