2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Global database interface, complete with static methods.
|
|
|
|
* Use this class for interacting with the database.
|
2010-10-15 01:57:40 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage model
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class DB {
|
2009-10-26 23:03:29 +01:00
|
|
|
/**
|
|
|
|
* This constant was added in SilverStripe 2.4 to indicate that SQL-queries
|
|
|
|
* should now use ANSI-compatible syntax. The most notable affect of this
|
|
|
|
* change is that table and field names should be escaped with double quotes
|
|
|
|
* and not backticks
|
|
|
|
*/
|
|
|
|
const USE_ANSI_SQL = true;
|
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* The global database connection.
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @var SS_Database
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-09-23 01:48:54 +02:00
|
|
|
private static $connections = array();
|
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
|
|
|
|
2009-06-08 01:07:25 +02:00
|
|
|
/**
|
|
|
|
* Internal flag to keep track of when db connection was attempted.
|
|
|
|
*/
|
|
|
|
private static $connection_attempted = false;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set the global database connection.
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* Pass an object that's a subclass of SS_Database. This object will be used when {@link DB::query()}
|
2007-07-19 12:40:28 +02:00
|
|
|
* is called.
|
2009-09-23 01:48:54 +02:00
|
|
|
* @param $connection The connecton object to set as the connection.
|
|
|
|
* @param $name The name to give to this connection. If you omit this argument, the connection
|
|
|
|
* will be the default one used by the ORM. However, you can store other named connections to
|
|
|
|
* be accessed through DB::getConn($name). This is useful when you have an application that
|
|
|
|
* needs to connect to more than one database.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function setConn(SS_Database $connection, $name = 'default') {
|
2009-09-23 01:48:54 +02:00
|
|
|
self::$connections[$name] = $connection;
|
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.
|
2009-09-23 01:48:54 +02:00
|
|
|
* @param $name An optional name given to a connection in the DB::setConn() call. If omitted,
|
|
|
|
* the default connection is returned.
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @return SS_Database
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function getConn($name = 'default') {
|
2009-09-23 05:35:05 +02:00
|
|
|
if(isset(self::$connections[$name])) {
|
|
|
|
return self::$connections[$name];
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-27 10:19:46 +02:00
|
|
|
|
|
|
|
/**
|
2012-12-06 16:25:45 +01:00
|
|
|
* Set an alternative database in a browser cookie,
|
|
|
|
* with the cookie lifetime set to the browser session.
|
|
|
|
* This is useful for integration testing on temporary databases.
|
|
|
|
*
|
|
|
|
* There is a strict naming convention for temporary databases to avoid abuse:
|
|
|
|
* <prefix> (default: 'ss_') + tmpdb + <7 digits>
|
|
|
|
* As an additional security measure, temporary databases will
|
|
|
|
* be ignored in "live" mode.
|
|
|
|
*
|
|
|
|
* Note that the database will be set on the next request.
|
2008-08-27 10:19:46 +02:00
|
|
|
* Set it to null to revert to the main database.
|
|
|
|
*/
|
2012-12-06 16:25:45 +01:00
|
|
|
public static function set_alternative_database_name($name = null) {
|
|
|
|
if($name) {
|
|
|
|
if(!self::valid_alternative_database_name($name)) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
'Invalid alternative database name: "%s"',
|
|
|
|
$name
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = Config::inst()->get('Security', 'token');
|
|
|
|
if(!$key) {
|
|
|
|
throw new LogicException('"Security.token" not found, run "sake dev/generatesecuretoken"');
|
|
|
|
}
|
|
|
|
if(!function_exists('mcrypt_encrypt')) {
|
|
|
|
throw new LogicException('DB::set_alternative_database_name() requires the mcrypt PHP extension');
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = md5($key); // Ensure key is correct length for chosen cypher
|
|
|
|
$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB);
|
2013-01-24 07:56:02 +01:00
|
|
|
$iv = mcrypt_create_iv($ivSize);
|
2012-12-06 16:25:45 +01:00
|
|
|
$encrypted = mcrypt_encrypt(
|
|
|
|
MCRYPT_RIJNDAEL_256, $key, $name, MCRYPT_MODE_CFB, $iv
|
|
|
|
);
|
|
|
|
|
|
|
|
// Set to browser session lifetime, and restricted to HTTP access only
|
|
|
|
Cookie::set("alternativeDatabaseName", base64_encode($encrypted), 0, null, null, false, true);
|
|
|
|
Cookie::set("alternativeDatabaseNameIv", base64_encode($iv), 0, null, null, false, true);
|
|
|
|
} else {
|
|
|
|
Cookie::set("alternativeDatabaseName", null, 0, null, null, false, true);
|
|
|
|
Cookie::set("alternativeDatabaseNameIv", null, 0, null, null, false, true);
|
|
|
|
}
|
2008-08-27 10:19:46 +02:00
|
|
|
}
|
2009-01-08 00:00:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of the database in use
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_alternative_database_name() {
|
2012-12-06 16:25:45 +01:00
|
|
|
$name = Cookie::get("alternativeDatabaseName");
|
|
|
|
$iv = Cookie::get("alternativeDatabaseNameIv");
|
|
|
|
|
|
|
|
if($name) {
|
|
|
|
$key = Config::inst()->get('Security', 'token');
|
|
|
|
if(!$key) {
|
|
|
|
throw new LogicException('"Security.token" not found, run "sake dev/generatesecuretoken"');
|
|
|
|
}
|
|
|
|
if(!function_exists('mcrypt_encrypt')) {
|
|
|
|
throw new LogicException('DB::set_alternative_database_name() requires the mcrypt PHP extension');
|
|
|
|
}
|
|
|
|
$key = md5($key); // Ensure key is correct length for chosen cypher
|
|
|
|
$decrypted = mcrypt_decrypt(
|
|
|
|
MCRYPT_RIJNDAEL_256, $key, base64_decode($name), MCRYPT_MODE_CFB, base64_decode($iv)
|
|
|
|
);
|
|
|
|
return (self::valid_alternative_database_name($decrypted)) ? $decrypted : false;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the name is valid, as a security
|
|
|
|
* measure against setting arbitrary databases.
|
|
|
|
*
|
|
|
|
* @param String $name
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public static function valid_alternative_database_name($name) {
|
|
|
|
if(Director::isLive()) return false;
|
|
|
|
|
|
|
|
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
|
|
|
|
$pattern = strtolower(sprintf('/^%stmpdb\d{7}$/', $prefix));
|
|
|
|
return (bool)preg_match($pattern, $name);
|
2009-01-08 00:00:54 +01:00
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Connect to a database.
|
2014-03-16 04:52:24 +01:00
|
|
|
*
|
|
|
|
* Given the database configuration, this method will create the correct
|
|
|
|
* subclass of {@link SS_Database}.
|
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param array $database A map of options. The 'type' is the name of the subclass of SS_Database to use. For the
|
|
|
|
* rest of the options, see the specific class.
|
2014-03-16 04:52:24 +01:00
|
|
|
* @param string $name identifier for the connection
|
|
|
|
*
|
|
|
|
* @return SS_Database
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2014-03-16 04:52:24 +01:00
|
|
|
public static function connect($databaseConfig, $label = 'default') {
|
|
|
|
|
2012-12-19 16:05:37 +01:00
|
|
|
// This is used by the "testsession" module to test up a test session using an alternative name
|
2012-12-06 16:25:45 +01:00
|
|
|
if($name = self::get_alternative_database_name()) {
|
2012-10-15 20:40:38 +02:00
|
|
|
$databaseConfig['database'] = $name;
|
2010-10-19 06:52:59 +02:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
2010-10-19 06:52:59 +02:00
|
|
|
|
2009-06-08 01:07:25 +02:00
|
|
|
self::$connection_attempted = true;
|
2009-06-27 10:30:17 +02:00
|
|
|
|
|
|
|
$dbClass = $databaseConfig['type'];
|
2013-12-05 02:21:17 +01:00
|
|
|
$conn = Injector::inst()->create($dbClass, $databaseConfig);
|
2009-06-27 10:30:17 +02:00
|
|
|
|
2014-03-16 04:52:24 +01:00
|
|
|
self::setConn($conn, $label);
|
|
|
|
|
|
|
|
return $conn;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-06-08 01:07:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if a database connection has been attempted.
|
|
|
|
* In particular, it lets the caller know if we're still so early in the execution pipeline that
|
|
|
|
* we haven't even tried to connect to the database yet.
|
|
|
|
*/
|
|
|
|
public static function connection_attempted() {
|
|
|
|
return self::$connection_attempted;
|
|
|
|
}
|
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.
|
|
|
|
**/
|
2010-05-25 06:21:07 +02:00
|
|
|
public static function getConnect($parameters) {
|
2009-09-23 01:48:54 +02:00
|
|
|
return self::getConn()->getConnect($parameters);
|
2007-09-14 03:28:16 +02:00
|
|
|
}
|
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
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @return SS_Query
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function query($sql, $errorLevel = E_USER_ERROR) {
|
2009-09-23 01:48:54 +02:00
|
|
|
self::$lastQuery = $sql;
|
2008-08-12 04:51:33 +02:00
|
|
|
|
2009-09-23 01:48:54 +02:00
|
|
|
return self::getConn()->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.
|
2010-10-15 01:57:40 +02:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* <code>
|
|
|
|
* array(
|
|
|
|
* // Command: insert
|
|
|
|
* "table name" => array(
|
|
|
|
* "command" => "insert",
|
|
|
|
* "fields" => array(
|
|
|
|
* "ClassName" => "'MyClass'", // if you're setting a literal, you need to escape and provide quotes
|
|
|
|
* "Created" => "now()", // alternatively, you can call DB functions
|
|
|
|
* "ID" => 234,
|
|
|
|
* ),
|
|
|
|
* "id" => 234 // an alternative to providing ID in the fields list
|
|
|
|
* ),
|
|
|
|
*
|
|
|
|
* // Command: update
|
|
|
|
* "other table" => array(
|
|
|
|
* "command" => "update",
|
|
|
|
* "fields" => array(
|
|
|
|
* "ClassName" => "'MyClass'",
|
|
|
|
* "LastEdited" => "now()",
|
|
|
|
* ),
|
|
|
|
* "where" => "ID = 234",
|
|
|
|
* "id" => 234 // an alternative to providing a where clause
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* You'll note that only one command on a given table can be called.
|
|
|
|
* That's a limitation of the system that's due to it being written for {@link DataObject::write()},
|
|
|
|
* which needs to do a single write on a number of different tables.
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param array $manipulation
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function manipulate($manipulation) {
|
2009-09-23 01:48:54 +02:00
|
|
|
self::$lastQuery = $manipulation;
|
|
|
|
return self::getConn()->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
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function getGeneratedID($table) {
|
2009-09-23 01:48:54 +02:00
|
|
|
return self::getConn()->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
|
|
|
/**
|
|
|
|
* Check if the connection to the database is active.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function isActive() {
|
2009-09-23 01:48:54 +02:00
|
|
|
if($conn = self::getConn()) return $conn->isActive();
|
2008-11-18 02:48:37 +01:00
|
|
|
else return false;
|
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 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
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @param string $username SS_Database username
|
|
|
|
* @param string $password SS_Database Password
|
|
|
|
* @param string $database SS_Database to which to create
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return boolean Returns true if successful
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function createDatabase($connect, $username, $password, $database) {
|
2009-09-23 01:48:54 +02:00
|
|
|
return self::getConn()->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.
|
2009-05-21 07:08:11 +02:00
|
|
|
* @param $tableName The name of the table
|
|
|
|
* @param $fields A map of field names to field types
|
|
|
|
* @param $indexes A map of indexes
|
|
|
|
* @param $options An map of additional options. The available keys are as follows:
|
|
|
|
* - 'MSSQLDatabase'/'MySQLDatabase'/'PostgreSQLDatabase' - database-specific options such as "engine" for MySQL.
|
|
|
|
* - 'temporary' - If true, then a temporary table will be created
|
|
|
|
* @return The table name generated. This may be different from the table name, for example with temporary tables.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function createTable($table, $fields = null, $indexes = null, $options = null) {
|
2009-09-23 01:48:54 +02:00
|
|
|
return self::getConn()->createTable($table, $fields, $indexes, $options);
|
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 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'
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function createField($table, $field, $spec) {
|
2009-09-23 01:48:54 +02:00
|
|
|
return self::getConn()->createField($table, $field, $spec);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
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.
|
2009-03-11 22:48:59 +01:00
|
|
|
* @param boolean $hasAutoIncPK A flag indicating that the primary key on this table is an autoincrement type
|
2007-07-19 12:40:28 +02:00
|
|
|
* 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.
|
2009-05-19 05:55:14 +02:00
|
|
|
* @param string $options SQL statement to append to the CREATE TABLE call.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-26 23:34:00 +02:00
|
|
|
public static function requireTable($table, $fieldSchema = null, $indexSchema = null, $hasAutoIncPK=true,
|
|
|
|
$options = null, $extensions=null) {
|
|
|
|
|
2009-10-08 03:21:41 +02:00
|
|
|
return self::getConn()->requireTable($table, $fieldSchema, $indexSchema, $hasAutoIncPK, $options, $extensions);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
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.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function requireField($table, $field, $spec) {
|
2009-09-23 01:48:54 +02:00
|
|
|
return self::getConn()->requireField($table, $field, $spec);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
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.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function requireIndex($table, $index, $spec) {
|
2009-09-23 01:48:54 +02:00
|
|
|
return self::getConn()->requireIndex($table, $index, $spec);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
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.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function dontRequireTable($table) {
|
2009-09-23 01:48:54 +02:00
|
|
|
return self::getConn()->dontRequireTable($table);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-07-09 07:55:56 +02:00
|
|
|
|
|
|
|
/**
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* See {@link SS_Database->dontRequireField()}.
|
2009-07-09 07:55:56 +02:00
|
|
|
*
|
|
|
|
* @param string $table The table name.
|
|
|
|
* @param string $fieldName
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function dontRequireField($table, $fieldName) {
|
2009-09-23 01:48:54 +02:00
|
|
|
return self::getConn()->dontRequireField($table, $fieldName);
|
2009-07-09 07:55:56 +02:00
|
|
|
}
|
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.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function checkAndRepairTable($table) {
|
2009-09-23 01:48:54 +02:00
|
|
|
return self::getConn()->checkAndRepairTable($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
|
|
|
/**
|
|
|
|
* Return the number of rows affected by the previous operation.
|
|
|
|
* @return int
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function affectedRows() {
|
2009-09-23 01:48:54 +02:00
|
|
|
return self::getConn()->affectedRows();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
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
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function tableList() {
|
2009-09-23 01:48:54 +02:00
|
|
|
return self::getConn()->tableList();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
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
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function fieldList($table) {
|
2009-09-23 01:48:54 +02:00
|
|
|
return self::getConn()->fieldList($table);
|
2007-11-15 23:29:10 +01:00
|
|
|
}
|
2007-09-16 18:44:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Enable supression of database messages.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function quiet() {
|
2009-09-23 01:48:54 +02:00
|
|
|
return self::getConn()->quiet();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-10-26 23:03:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a message about database alteration.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function alteration_message($message,$type="") {
|
2009-10-26 23:03:29 +01:00
|
|
|
return self::getConn()->alterationMessage($message, $type);
|
|
|
|
}
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|