2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\ORM;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use SilverStripe\Control\Cookie;
|
|
|
|
use SilverStripe\Dev\Deprecation;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\ORM\Connect\DBConnector;
|
2016-07-01 04:26:18 +02:00
|
|
|
use SilverStripe\ORM\Connect\DBSchemaManager;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\ORM\Connect\SS_Query;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\Queries\SQLExpression;
|
|
|
|
use SilverStripe\ORM\Connect\SS_Database;
|
2016-08-19 00:51:35 +02:00
|
|
|
use InvalidArgumentException;
|
|
|
|
use LogicException;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Global database interface, complete with static methods.
|
|
|
|
* Use this class for interacting with the database.
|
|
|
|
*/
|
|
|
|
class DB {
|
2013-06-21 00:32:08 +02:00
|
|
|
|
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;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-10-26 23:03:29 +01:00
|
|
|
|
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.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param SS_Database $connection The connecton object to set as the connection.
|
|
|
|
* @param string $name The name to give to this connection. If you omit this argument, the connection
|
2009-09-23 01:48:54 +02:00
|
|
|
* will be the default one used by the ORM. However, you can store other named connections to
|
2013-06-21 00:32:08 +02:00
|
|
|
* be accessed through DB::get_conn($name). This is useful when you have an application that
|
2009-09-23 01:48:54 +02:00
|
|
|
* needs to connect to more than one database.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public static function set_conn(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.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
|
|
|
* @param string $name An optional name given to a connection in the DB::setConn() call. If omitted,
|
2009-09-23 01:48:54 +02:00
|
|
|
* 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
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public static function get_conn($name = 'default') {
|
2009-09-23 05:35:05 +02:00
|
|
|
if(isset(self::$connections[$name])) {
|
|
|
|
return self::$connections[$name];
|
|
|
|
}
|
2016-06-17 06:28:58 +02:00
|
|
|
return null;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
/**
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated since version 4.0 Use DB::get_conn instead
|
2016-08-19 00:51:35 +02:00
|
|
|
* @todo PSR-2 standardisation will probably un-deprecate this
|
2013-06-21 00:32:08 +02:00
|
|
|
*/
|
|
|
|
public static function getConn($name = 'default') {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use DB::get_conn instead');
|
2013-06-21 00:32:08 +02:00
|
|
|
return self::get_conn($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the schema manager for the current database
|
|
|
|
*
|
|
|
|
* @param string $name An optional name given to a connection in the DB::setConn() call. If omitted,
|
|
|
|
* the default connection is returned.
|
|
|
|
* @return DBSchemaManager
|
|
|
|
*/
|
|
|
|
public static function get_schema($name = 'default') {
|
|
|
|
$connection = self::get_conn($name);
|
2016-06-03 10:51:02 +02:00
|
|
|
if($connection) {
|
|
|
|
return $connection->getSchemaManager();
|
|
|
|
}
|
|
|
|
return null;
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a sql query with the specified connection
|
|
|
|
*
|
|
|
|
* @param SQLExpression $expression The expression object to build from
|
|
|
|
* @param array $parameters Out parameter for the resulting query parameters
|
|
|
|
* @param string $name An optional name given to a connection in the DB::setConn() call. If omitted,
|
|
|
|
* the default connection is returned.
|
|
|
|
* @return string The resulting SQL as a string
|
|
|
|
*/
|
|
|
|
public static function build_sql(SQLExpression $expression, &$parameters, $name = 'default') {
|
|
|
|
$connection = self::get_conn($name);
|
|
|
|
if($connection) {
|
|
|
|
return $connection->getQueryBuilder()->buildSQL($expression, $parameters);
|
|
|
|
} else {
|
|
|
|
$parameters = array();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-27 10:19:46 +02:00
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Retrieves the connector object for the current database
|
|
|
|
*
|
|
|
|
* @param string $name An optional name given to a connection in the DB::setConn() call. If omitted,
|
|
|
|
* the default connection is returned.
|
|
|
|
* @return DBConnector
|
|
|
|
*/
|
|
|
|
public static function get_connector($name = 'default') {
|
|
|
|
$connection = self::get_conn($name);
|
2016-06-03 10:51:02 +02:00
|
|
|
if($connection) {
|
|
|
|
return $connection->getConnector();
|
|
|
|
}
|
|
|
|
return null;
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set an alternative database in a browser cookie,
|
2012-12-06 16:25:45 +01:00
|
|
|
* with the cookie lifetime set to the browser session.
|
|
|
|
* This is useful for integration testing on temporary databases.
|
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* There is a strict naming convention for temporary databases to avoid abuse:
|
2012-12-06 16:25:45 +01:00
|
|
|
* <prefix> (default: 'ss_') + tmpdb + <7 digits>
|
|
|
|
* As an additional security measure, temporary databases will
|
|
|
|
* be ignored in "live" mode.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2012-12-06 16:25:45 +01:00
|
|
|
* 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.
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param string $name
|
2008-08-27 10:19:46 +02:00
|
|
|
*/
|
2012-12-06 16:25:45 +01:00
|
|
|
public static function set_alternative_database_name($name = null) {
|
2015-07-09 00:46:13 +02:00
|
|
|
// Skip if CLI
|
|
|
|
if(Director::is_cli()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-12-06 16:25:45 +01:00
|
|
|
if($name) {
|
|
|
|
if(!self::valid_alternative_database_name($name)) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
'Invalid alternative database name: "%s"',
|
|
|
|
$name
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$key = Config::inst()->get('SilverStripe\\Security\\Security', 'token');
|
2012-12-06 16:25:45 +01:00
|
|
|
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 {
|
2014-05-04 15:34:58 +02:00
|
|
|
Cookie::force_expiry("alternativeDatabaseName", null, null, false, true);
|
|
|
|
Cookie::force_expiry("alternativeDatabaseNameIv", null, null, false, true);
|
2012-12-06 16:25:45 +01:00
|
|
|
}
|
2008-08-27 10:19:46 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +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) {
|
2016-06-23 01:37:22 +02:00
|
|
|
$key = Config::inst()->get('SilverStripe\\Security\\Security', 'token');
|
2012-12-06 16:25:45 +01:00
|
|
|
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.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2012-12-06 16:25:45 +01:00
|
|
|
* @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
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* Given the database configuration, this method will create the correct
|
2014-03-16 04:52:24 +01:00
|
|
|
* subclass of {@link SS_Database}.
|
|
|
|
*
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param array $databaseConfig 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.
|
|
|
|
* @param string $label identifier for the connection
|
2014-03-16 04:52:24 +01:00
|
|
|
* @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') {
|
2013-06-21 00:32:08 +02:00
|
|
|
|
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'];
|
|
|
|
|
2014-08-13 07:06:38 +02:00
|
|
|
// Using Injector->create allows us to use registered configurations
|
2013-06-21 00:32:08 +02:00
|
|
|
// which may or may not map to explicit objects
|
2014-08-13 07:06:38 +02:00
|
|
|
$conn = Injector::inst()->create($dbClass);
|
2013-06-21 00:32:08 +02:00
|
|
|
$conn->connect($databaseConfig);
|
|
|
|
|
|
|
|
self::set_conn($conn, $label);
|
2014-03-16 04:52:24 +01:00
|
|
|
|
|
|
|
return $conn;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +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-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;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
return self::get_conn()->query($sql, $errorLevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for generating a list of parameter placeholders for the
|
|
|
|
* given argument(s)
|
|
|
|
*
|
|
|
|
* @param array|integer $input An array of items needing placeholders, or a
|
|
|
|
* number to specify the number of placeholders
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param string $join The string to join each placeholder together with
|
2013-06-21 00:32:08 +02:00
|
|
|
* @return string|null Either a list of placeholders, or null
|
|
|
|
*/
|
|
|
|
public static function placeholders($input, $join = ', ') {
|
|
|
|
if(is_array($input)) {
|
|
|
|
$number = count($input);
|
|
|
|
} elseif(is_numeric($input)) {
|
|
|
|
$number = intval($input);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if($number === 0) return null;
|
|
|
|
return implode($join, array_fill(0, $number, '?'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the given SQL parameterised query with the specified arguments
|
|
|
|
*
|
|
|
|
* @param string $sql The SQL query to execute. The ? character will denote parameters.
|
|
|
|
* @param array $parameters An ordered list of arguments.
|
|
|
|
* @param int $errorLevel The level of error reporting to enable for the query
|
|
|
|
* @return SS_Query
|
|
|
|
*/
|
|
|
|
public static function prepared_query($sql, $parameters, $errorLevel = E_USER_ERROR) {
|
|
|
|
self::$lastQuery = $sql;
|
|
|
|
|
|
|
|
return self::get_conn()->preparedQuery($sql, $parameters, $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.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
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
|
|
|
|
* ),
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2010-10-15 01:57:40 +02:00
|
|
|
* // 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>
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
|
|
|
* 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()},
|
2010-10-15 01:57:40 +02:00
|
|
|
* which needs to do a single write on a number of different tables.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
|
|
|
* @todo Update this to support paramaterised queries
|
|
|
|
*
|
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;
|
2016-06-03 10:51:02 +02:00
|
|
|
self::get_conn()->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.
|
2016-06-23 01:37:22 +02:00
|
|
|
*
|
|
|
|
* @param string $table
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return int
|
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public static function get_generated_id($table) {
|
|
|
|
return self::get_conn()->getGeneratedID($table);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Check if the connection to the database is active.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public static function is_active() {
|
|
|
|
return ($conn = self::get_conn()) && $conn->isActive();
|
|
|
|
}
|
|
|
|
|
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.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
|
|
|
* @param string $database Name of database to create
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return boolean Returns true if successful
|
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public static function create_database($database) {
|
|
|
|
return self::get_conn()->selectDatabase($database, true);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a new table.
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param string $table The name of the table
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param array$fields A map of field names to field types
|
|
|
|
* @param array $indexes A map of indexes
|
|
|
|
* @param array $options An map of additional options. The available keys are as follows:
|
|
|
|
* - 'MSSQLDatabase'/'MySQLDatabase'/'PostgreSQLDatabase' - database-specific options such as "engine"
|
|
|
|
* for MySQL.
|
2009-05-21 07:08:11 +02:00
|
|
|
* - 'temporary' - If true, then a temporary table will be created
|
2016-06-03 10:51:02 +02:00
|
|
|
* @param array $advancedOptions Advanced creation options
|
2013-06-21 00:32:08 +02:00
|
|
|
* @return string The table name generated. This may be different from the table name, for example with
|
|
|
|
* temporary tables.
|
|
|
|
*/
|
|
|
|
public static function create_table($table, $fields = null, $indexes = null, $options = null,
|
|
|
|
$advancedOptions = null
|
|
|
|
) {
|
|
|
|
return self::get_schema()->createTable($table, $fields, $indexes, $options, $advancedOptions);
|
|
|
|
}
|
|
|
|
|
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'
|
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public static function create_field($table, $field, $spec) {
|
|
|
|
return self::get_schema()->createField($table, $field, $spec);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Generate the following table in the database, modifying whatever already exists
|
|
|
|
* as necessary.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @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.
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param boolean $hasAutoIncPK A flag indicating that the primary key on this table is an autoincrement type
|
2009-05-19 05:55:14 +02:00
|
|
|
* @param string $options SQL statement to append to the CREATE TABLE call.
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param array $extensions List of extensions
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public static function require_table($table, $fieldSchema = null, $indexSchema = null, $hasAutoIncPK = true,
|
|
|
|
$options = null, $extensions = null
|
|
|
|
) {
|
2016-06-03 10:51:02 +02:00
|
|
|
self::get_schema()->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.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $table The table name.
|
|
|
|
* @param string $field The field name.
|
|
|
|
* @param string $spec The field specification.
|
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public static function require_field($table, $field, $spec) {
|
2016-06-03 10:51:02 +02:00
|
|
|
self::get_schema()->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.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @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.
|
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public static function require_index($table, $index, $spec) {
|
|
|
|
self::get_schema()->requireIndex($table, $index, $spec);
|
|
|
|
}
|
|
|
|
|
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).
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $table The table name.
|
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public static function dont_require_table($table) {
|
|
|
|
self::get_schema()->dontRequireTable($table);
|
|
|
|
}
|
|
|
|
|
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()}.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2009-07-09 07:55:56 +02:00
|
|
|
* @param string $table The table name.
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $fieldName The field name not to require
|
|
|
|
*/
|
|
|
|
public static function dont_require_field($table, $fieldName) {
|
|
|
|
self::get_schema()->dontRequireField($table, $fieldName);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Checks a table's integrity and repairs it if necessary.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param string $table The name of the table.
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return boolean Return true if the table has integrity after the method is complete.
|
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public static function check_and_repair_table($table) {
|
|
|
|
return self::get_schema()->checkAndRepairTable($table);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the number of rows affected by the previous operation.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
|
|
|
* @return integer The number of affected rows
|
|
|
|
*/
|
|
|
|
public static function affected_rows() {
|
|
|
|
return self::get_conn()->affectedRows();
|
|
|
|
}
|
|
|
|
|
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.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
|
|
|
* @return array The list of tables
|
|
|
|
*/
|
|
|
|
public static function table_list() {
|
|
|
|
return self::get_schema()->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.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2007-11-15 23:29:10 +01:00
|
|
|
* @param string $table The table name.
|
2013-06-21 00:32:08 +02:00
|
|
|
* @return array The list of fields
|
|
|
|
*/
|
|
|
|
public static function field_list($table) {
|
|
|
|
return self::get_schema()->fieldList($table);
|
|
|
|
}
|
|
|
|
|
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() {
|
2013-06-21 00:32:08 +02:00
|
|
|
self::get_schema()->quiet();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-10-26 23:03:29 +01:00
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Show a message about database alteration
|
|
|
|
*
|
|
|
|
* @param string $message to display
|
|
|
|
* @param string $type one of [created|changed|repaired|obsolete|deleted|error]
|
2009-10-26 23:03:29 +01:00
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public static function alteration_message($message, $type = "") {
|
|
|
|
self::get_schema()->alterationMessage($message, $type);
|
2009-10-26 23:03:29 +01:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|