2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage core
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Global database interface, complete with static methods.
|
|
|
|
* Use this class for interacting with the database.
|
|
|
|
*/
|
|
|
|
class DB {
|
|
|
|
/**
|
|
|
|
* The global database connection.
|
|
|
|
* @var Database
|
|
|
|
*/
|
|
|
|
protected static $globalConn;
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* The last SQL query run.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public static $lastQuery;
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set the global database connection.
|
|
|
|
* Pass an object that's a subclass of Database. This object will be used when {@link DB::query()}
|
|
|
|
* is called.
|
|
|
|
* @var Database $globalConn
|
|
|
|
*/
|
|
|
|
static function setConn($globalConn) {
|
|
|
|
DB::$globalConn = $globalConn;
|
2007-09-14 03:28:16 +02:00
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Get the global database connection.
|
|
|
|
* @return Database
|
|
|
|
*/
|
|
|
|
static function getConn() {
|
|
|
|
return DB::$globalConn;
|
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Connect to a database.
|
|
|
|
* Given the database configuration, this method will create the correct subclass of Database,
|
|
|
|
* and set it as the global connection.
|
|
|
|
* @param array $database A map of options. The 'type' is the name of the subclass of Database to use. For the rest of the options, see the specific class.
|
|
|
|
*/
|
|
|
|
static function connect($databaseConfig) {
|
2007-09-15 01:07:23 +02:00
|
|
|
if(!isset($databaseConfig['type']) || empty($databaseConfig['type'])) {
|
|
|
|
user_error("DB::connect: Not passed a valid database config", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
if (isset($databaseConfig['pdo']) && $databaseConfig['pdo']) { // TODO:pkrenn_remove
|
2007-09-14 03:28:16 +02:00
|
|
|
$conn = new PDODatabase($databaseConfig);
|
|
|
|
} else { // TODO:pkrenn_remove begin
|
|
|
|
$dbClass = $databaseConfig['type'];
|
|
|
|
$conn = new $dbClass($databaseConfig);
|
|
|
|
} // TODO:pkrenn_remove end
|
2007-07-19 12:40:28 +02:00
|
|
|
DB::setConn($conn);
|
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-09-14 03:28:16 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Execute the given SQL query.
|
|
|
|
* @param string $sql The SQL query to execute
|
|
|
|
* @param int $errorLevel The level of error reporting to enable for the query
|
|
|
|
* @return Query
|
|
|
|
*/
|
2007-09-16 18:44:42 +02:00
|
|
|
static function query($sql, $errorLevel = E_USER_ERROR) {
|
2007-07-19 12:40:28 +02:00
|
|
|
DB::$lastQuery = $sql;
|
2007-09-16 18:44:42 +02:00
|
|
|
return DB::$globalConn->query($sql, $errorLevel);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Execute a complex manipulation on the database.
|
|
|
|
* A manipulation is an array of insert / or update sequences. The keys of the array are table names,
|
|
|
|
* and the values are map containing 'command' and 'fields'. Command should be 'insert' or 'update',
|
|
|
|
* and fields should be a map of field names to field values, including quotes. The field value can
|
|
|
|
* also be a SQL function or similar.
|
|
|
|
* @param array $manipulation
|
|
|
|
*/
|
2007-09-16 18:44:42 +02:00
|
|
|
static function manipulate($manipulation) {
|
2007-07-19 12:40:28 +02:00
|
|
|
DB::$lastQuery = $manipulation;
|
2007-09-16 18:44:42 +02:00
|
|
|
return DB::$globalConn->manipulate($manipulation);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Get the autogenerated ID from the previous INSERT query.
|
|
|
|
* @return int
|
|
|
|
*/
|
2007-09-14 03:35:54 +02:00
|
|
|
static function getGeneratedID($table) {
|
|
|
|
return DB::$globalConn->getGeneratedID($table);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Get the ID for the next new record for the table.
|
|
|
|
* @var string $table The name od the table.
|
|
|
|
* @return int
|
|
|
|
*/
|
2007-09-16 18:44:42 +02:00
|
|
|
static function getNextID($table) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return DB::$globalConn->getNextID($table);
|
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Check if the connection to the database is active.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
static function isActive() {
|
|
|
|
return DB::$globalConn->isActive();
|
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +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.
|
2007-09-14 03:28:16 +02:00
|
|
|
* @param string $connect Connection string
|
|
|
|
* @param string $username Database username
|
|
|
|
* @param string $password Database Password
|
|
|
|
* @param string $database Database to which to create
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return boolean Returns true if successful
|
|
|
|
*/
|
2007-09-14 03:28:16 +02:00
|
|
|
static function createDatabase($connect, $username, $password, $database) {
|
|
|
|
return DB::$globalConn->createDatabase($connect, $username, $password, $database);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a new table.
|
|
|
|
* The table will have a single field - the integer key ID.
|
|
|
|
* @param string $table Name of table to create.
|
|
|
|
*/
|
|
|
|
static function createTable($table) {
|
|
|
|
return DB::$globalConn->createTable($table);
|
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a new field on a table.
|
|
|
|
* @param string $table Name of the table.
|
|
|
|
* @param string $field Name of the field to add.
|
|
|
|
* @param string $spec The field specification, eg 'INTEGER NOT NULL'
|
|
|
|
*/
|
|
|
|
static function createField($table, $field, $spec) {
|
|
|
|
return DB::$globalConn->createField($table, $field, $spec);
|
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Generate the following table in the database, modifying whatever already exists
|
|
|
|
* as necessary.
|
|
|
|
* @param string $table The name of the table
|
|
|
|
* @param string $fieldSchema A list of the fields to create, in the same form as DataObject::$db
|
|
|
|
* @param string $indexSchema A list of indexes to create. The keys of the array are the names of the index.
|
|
|
|
* The values of the array can be one of:
|
|
|
|
* - true: Create a single column index on the field named the same as the index.
|
|
|
|
* - array('fields' => array('A','B','C'), 'type' => 'index/unique/fulltext'): This gives you full
|
|
|
|
* control over the index.
|
|
|
|
*/
|
|
|
|
static function requireTable($table, $fieldSchema = null, $indexSchema = null) {
|
|
|
|
return DB::$globalConn->requireTable($table, $fieldSchema, $indexSchema);
|
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Generate the given field on the table, modifying whatever already exists as necessary.
|
|
|
|
* @param string $table The table name.
|
|
|
|
* @param string $field The field name.
|
|
|
|
* @param string $spec The field specification.
|
|
|
|
*/
|
|
|
|
static function requireField($table, $field, $spec) {
|
|
|
|
return DB::$globalConn->requireField($table, $field, $spec);
|
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Generate the given index in the database, modifying whatever already exists as necessary.
|
|
|
|
* @param string $table The table name.
|
|
|
|
* @param string $index The index name.
|
|
|
|
* @param string|boolean $spec The specification of the index. See requireTable() for more information.
|
|
|
|
*/
|
|
|
|
static function requireIndex($table, $index, $spec) {
|
|
|
|
return DB::$globalConn->requireIndex($table, $index, $spec);
|
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* If the given table exists, move it out of the way by renaming it to _obsolete_(tablename).
|
|
|
|
* @param string $table The table name.
|
|
|
|
*/
|
|
|
|
static function dontRequireTable($table) {
|
|
|
|
return DB::$globalConn->dontRequireTable($table);
|
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Checks a table's integrity and repairs it if necessary.
|
|
|
|
* @var string $tableName The name of the table.
|
|
|
|
* @return boolean Return true if the table has integrity after the method is complete.
|
|
|
|
*/
|
|
|
|
static function checkAndRepairTable($table) {
|
|
|
|
return DB::$globalConn->checkAndRepairTable($table);
|
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the number of rows affected by the previous operation.
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
static function affectedRows() {
|
|
|
|
return DB::$globalConn->affectedRows();
|
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns a list of all tables in the database.
|
|
|
|
* The table names will be in lower case.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
static function tableList() {
|
|
|
|
return DB::$globalConn->tableList();
|
|
|
|
}
|
2007-11-15 23:29:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of all the fields for the given table.
|
|
|
|
* Returns a map of field name => field spec.
|
|
|
|
* @param string $table The table name.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
static function fieldList($table) {
|
|
|
|
return DB::$globalConn->fieldList($table);
|
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Enable supression of database messages.
|
|
|
|
*/
|
|
|
|
static function quiet() {
|
|
|
|
return DB::$globalConn->quiet();
|
|
|
|
}
|
|
|
|
}
|
2007-11-15 23:29:10 +01:00
|
|
|
?>
|