Will execute: $sql
"; 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); } /** * 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(); 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. * @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) { $query = "ALTER TABLE :oldTableName RENAME TO :newTableName"; $create->bindParam(":oldTableName", $oldTableName); $create->bindParam(":newTableName", $newTableName); $create = $dbConn->prepare($query); $create->execute(); } /** * Checks a table's integrity and repairs it if necessary */ public function checkAndRepairTable($tableName) { } protected function runTableCheckCommand($sql) { } /** * Add the given field to the given table. */ 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 */ public function alterField($tableName, $fieldName, $fieldSpec) { } /** * Get a list of all the fields for the given table. * Returns a map of field name => field spec */ public function fieldList($table) { } public function createIndex($tableName, $indexName, $indexSpec) { } public function alterIndex($tableName, $indexName, $indexSpec) { } public function indexList($table) { } /** * Returns a list of all the tables in the column. * Table names will all be in lowercase */ public function tableList() { } /** * Return the number of rows affected by the previous operation. */ public function affectedRows() { } } /** * A result-set from a database query (array). */ class PDOQuery extends Query { private $result; /** * Hook the result-set given into a Query class, suitable for use by sapphire. * @param array $result The array of all returned values. */ public function __construct(PDODatabase $result) { $this->result = $result; parent::__construct(); } public function __destroy() { $this->result = null; } public function seek($row) { return in_array($row, $this->result); } public function numRecords() { return count($this->result); } public function nextRecord() { } } ?>