2008-11-23 02:20:39 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* PostgreSQL connector class.
|
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage model
|
|
|
|
*/
|
|
|
|
class PostgreSQLDatabase extends SS_Database {
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2008-11-23 02:20:39 +01:00
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* Database schema manager object
|
|
|
|
*
|
|
|
|
* @var PostgreSQLSchemaManager
|
2008-11-23 02:20:39 +01:00
|
|
|
*/
|
2013-04-03 06:27:11 +02:00
|
|
|
protected $schemaManager;
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2008-11-23 02:20:39 +01:00
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* The currently selected database schema name.
|
|
|
|
*
|
|
|
|
* @var string
|
2008-11-23 02:20:39 +01:00
|
|
|
*/
|
2013-04-03 06:27:11 +02:00
|
|
|
protected $schema;
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
protected $supportsTransactions = true;
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
const MASTER_DATABASE = 'postgres';
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
const MASTER_SCHEMA = 'public';
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2008-11-23 02:20:39 +01:00
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* Full text cluster method. (e.g. GIN or GiST)
|
|
|
|
*
|
2008-11-23 02:20:39 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2013-04-03 06:27:11 +02:00
|
|
|
public static function default_fts_cluster_method() {
|
|
|
|
return Config::inst()->get('PostgreSQLDatabase', 'default_fts_cluster_method');
|
2008-11-23 02:20:39 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2008-11-23 02:20:39 +01:00
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* Full text search method.
|
|
|
|
*
|
2008-11-23 02:20:39 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2013-04-03 06:27:11 +02:00
|
|
|
public static function default_fts_search_method() {
|
|
|
|
return Config::inst()->get('PostgreSQLDatabase', 'default_fts_search_method');
|
2008-11-23 02:20:39 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2010-02-04 05:29:37 +01:00
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* Determines whether to check a database exists on the host by
|
|
|
|
* querying the 'postgres' database and running createDatabase.
|
2011-01-11 22:17:17 +01:00
|
|
|
*
|
2013-04-03 06:27:11 +02:00
|
|
|
* Some locked down systems prevent access to the 'postgres' table in
|
|
|
|
* which case you need to set this to false.
|
|
|
|
*
|
|
|
|
* If allow_query_master_postgres is false, and model_schema_as_database is also false,
|
|
|
|
* then attempts to create or check databases beyond the initial connection will
|
|
|
|
* result in a runtime error.
|
2010-02-04 05:29:37 +01:00
|
|
|
*/
|
2013-04-03 06:27:11 +02:00
|
|
|
public static function allow_query_master_postgres() {
|
|
|
|
return Config::inst()->get('PostgreSQLDatabase', 'allow_query_master_postgres');
|
2010-02-04 05:29:37 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2008-11-23 02:20:39 +01:00
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* For instances where multiple databases are used beyond the initial connection
|
|
|
|
* you may set this option to true to force database switches to switch schemas
|
|
|
|
* instead of using databases. This may be useful if the database user does not
|
|
|
|
* have cross-database permissions, and in cases where multiple databases are used
|
|
|
|
* (such as in running test cases).
|
|
|
|
*
|
|
|
|
* If this is true then the database will only be set during the initial connection,
|
|
|
|
* and attempts to change to this database will use the 'public' schema instead
|
2008-11-23 02:20:39 +01:00
|
|
|
*/
|
2013-04-03 06:27:11 +02:00
|
|
|
public static function model_schema_as_database() {
|
|
|
|
return Config::inst()->get('PostgreSQLDatabase', 'model_schema_as_database');
|
2008-11-23 02:20:39 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2008-11-23 02:20:39 +01:00
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* Override the language that tsearch uses. By default it is 'english, but
|
|
|
|
* could be any of the supported languages that can be found in the
|
|
|
|
* pg_catalog.pg_ts_config table.
|
2011-01-11 22:17:17 +01:00
|
|
|
*
|
2013-04-03 06:27:11 +02:00
|
|
|
* @var string
|
2008-11-23 02:20:39 +01:00
|
|
|
*/
|
2013-04-03 06:27:11 +02:00
|
|
|
public static function search_language() {
|
|
|
|
return Config::inst()->get('PostgreSQLDatabase', 'search_language');
|
2008-11-23 02:20:39 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2008-11-23 02:20:39 +01:00
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* The database name specified at initial connection
|
|
|
|
*
|
|
|
|
* @var string
|
2008-11-23 02:20:39 +01:00
|
|
|
*/
|
2013-04-03 06:27:11 +02:00
|
|
|
protected $databaseOriginal = '';
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2008-11-23 02:20:39 +01:00
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* The schema name specified at initial construction. When model_schema_as_database
|
|
|
|
* is set to true selecting the $databaseOriginal database will instead reset
|
|
|
|
* the schema to this
|
2011-01-11 22:17:17 +01:00
|
|
|
*
|
2013-04-03 06:27:11 +02:00
|
|
|
* @var string
|
2008-11-23 02:20:39 +01:00
|
|
|
*/
|
2013-04-03 06:27:11 +02:00
|
|
|
protected $schemaOriginal = '';
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2008-11-23 02:20:39 +01:00
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* Connection parameters specified at inital connection
|
2014-07-14 23:32:26 +02:00
|
|
|
*
|
|
|
|
* @var array
|
2008-11-23 02:20:39 +01:00
|
|
|
*/
|
2013-04-03 06:27:11 +02:00
|
|
|
protected $parameters = array();
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2014-07-14 23:32:26 +02:00
|
|
|
public function connect($parameters) {
|
2013-04-03 06:27:11 +02:00
|
|
|
// Check database name
|
|
|
|
if(empty($parameters['database'])) {
|
|
|
|
// Check if we can use the master database
|
|
|
|
if(!self::allow_query_master_postgres()) {
|
|
|
|
throw new ErrorException('PostegreSQLDatabase::connect called without a database name specified');
|
|
|
|
}
|
|
|
|
// Fallback to master database connection if permission allows
|
|
|
|
$parameters['database'] = self::MASTER_DATABASE;
|
|
|
|
}
|
|
|
|
$this->databaseOriginal = $parameters['database'];
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
// check schema name
|
|
|
|
if(empty($parameters['schema'])) {
|
|
|
|
$parameters['schema'] = self::MASTER_SCHEMA;
|
|
|
|
}
|
|
|
|
$this->schemaOriginal = $parameters['schema'];
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
// Ensure that driver is available (required by PDO)
|
|
|
|
if(empty($parameters['driver'])) {
|
|
|
|
$parameters['driver'] = $this->getDatabaseServer();
|
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
// Ensure port number is set (required by postgres)
|
|
|
|
if(empty($parameters['port'])) {
|
|
|
|
$parameters['port'] = 5432;
|
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
$this->parameters = $parameters;
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
// If allowed, check that the database exists. Otherwise naively assume
|
|
|
|
// that the original database exists
|
|
|
|
if(self::allow_query_master_postgres()) {
|
|
|
|
// Use master connection to setup initial schema
|
|
|
|
$this->connectMaster();
|
|
|
|
if(!$this->schemaManager->postgresDatabaseExists($this->databaseOriginal)) {
|
|
|
|
$this->schemaManager->createPostgresDatabase($this->databaseOriginal);
|
|
|
|
}
|
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
// Connect to the actual database we're requesting
|
|
|
|
$this->connectDefault();
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
// Set up the schema if required
|
|
|
|
$this->setSchema($this->schemaOriginal, true);
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
// Set the timezone if required.
|
|
|
|
if (isset($parameters['timezone'])) {
|
|
|
|
$this->selectTimezone($parameters['timezone']);
|
|
|
|
}
|
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
protected function connectMaster() {
|
|
|
|
$parameters = $this->parameters;
|
|
|
|
$parameters['database'] = self::MASTER_DATABASE;
|
|
|
|
$this->connector->connect($parameters, true);
|
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
protected function connectDefault() {
|
|
|
|
$parameters = $this->parameters;
|
|
|
|
$parameters['database'] = $this->databaseOriginal;
|
|
|
|
$this->connector->connect($parameters, true);
|
2009-11-24 23:46:55 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2009-02-13 03:46:54 +01:00
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* Sets the system timezone for the database connection
|
|
|
|
*
|
|
|
|
* @param string $timezone
|
2009-02-13 03:46:54 +01:00
|
|
|
*/
|
2013-04-03 06:27:11 +02:00
|
|
|
public function selectTimezone($timezone) {
|
|
|
|
if (empty($timezone)) return;
|
|
|
|
$this->query("SET SESSION TIME ZONE '$timezone';");
|
2009-02-13 03:46:54 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
public function supportsCollations() {
|
|
|
|
return true;
|
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
public function supportsTimezoneOverride() {
|
|
|
|
return true;
|
2008-11-24 00:29:09 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
public function getDatabaseServer() {
|
|
|
|
return "postgresql";
|
2009-03-12 03:48:48 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2008-11-24 00:29:09 +01:00
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* Returns the name of the current schema in use
|
|
|
|
*
|
|
|
|
* @return string Name of current schema
|
2008-11-24 00:29:09 +01:00
|
|
|
*/
|
2013-04-03 06:27:11 +02:00
|
|
|
public function currentSchema() {
|
|
|
|
return $this->schema;
|
2008-11-24 00:29:09 +01:00
|
|
|
}
|
|
|
|
|
2009-11-26 22:19:18 +01:00
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* Utility method to manually set the schema to an alternative
|
|
|
|
* Check existance & sets search path to the supplied schema name
|
|
|
|
*
|
|
|
|
* @param string $name Name of the schema
|
|
|
|
* @param boolean $create Flag indicating whether the schema should be created
|
|
|
|
* if it doesn't exist. If $create is false and the schema doesn't exist
|
|
|
|
* then an error will be raised
|
|
|
|
* @param int|boolean $errorLevel The level of error reporting to enable for
|
|
|
|
* the query, or false if no error should be raised
|
|
|
|
* @return boolean Flag indicating success
|
|
|
|
*/
|
|
|
|
public function setSchema($schema, $create = false, $errorLevel = E_USER_ERROR) {
|
|
|
|
if(!$this->schemaManager->schemaExists($schema)) {
|
|
|
|
// Check DB creation permisson
|
|
|
|
if (!$create) {
|
|
|
|
if ($errorLevel !== false) {
|
|
|
|
user_error("Schema $schema does not exist", $errorLevel);
|
|
|
|
}
|
|
|
|
$this->schema = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->schemaManager->createSchema($schema);
|
2009-11-26 22:19:18 +01:00
|
|
|
}
|
2013-04-03 06:27:11 +02:00
|
|
|
$this->setSchemaSearchPath($schema);
|
|
|
|
$this->schema = $schema;
|
|
|
|
return true;
|
2009-11-26 22:19:18 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2009-03-12 03:48:48 +01:00
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* Override the schema search path. Search using the arguments supplied.
|
|
|
|
* NOTE: The search path is normally set through setSchema() and only
|
|
|
|
* one schema is selected. The facility to add more than one schema to
|
|
|
|
* the search path is provided as an advanced PostgreSQL feature for raw
|
|
|
|
* SQL queries. Sapphire cannot search for datamodel tables in alternate
|
|
|
|
* schemas, so be wary of using alternate schemas within the ORM environment.
|
|
|
|
*
|
|
|
|
* @param string $arg1 First schema to use
|
|
|
|
* @param string $arg2 Second schema to use
|
|
|
|
* @param string $argN Nth schema to use
|
2009-04-06 01:20:09 +02:00
|
|
|
*/
|
2013-04-03 06:27:11 +02:00
|
|
|
public function setSchemaSearchPath() {
|
|
|
|
if(func_num_args() == 0) {
|
|
|
|
user_error('At least one Schema must be supplied to set a search path.', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
$schemas = array_values(func_get_args());
|
|
|
|
$this->query("SET search_path TO \"" . implode("\",\"", $schemas) . "\"");
|
2009-04-06 01:20:09 +02:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2009-09-16 05:51:38 +02:00
|
|
|
/**
|
|
|
|
* The core search engine configuration.
|
2009-10-05 04:03:00 +02:00
|
|
|
* @todo Properly extract the search functions out of the core.
|
2011-01-11 22:17:17 +01:00
|
|
|
*
|
2009-09-16 05:51:38 +02:00
|
|
|
* @param string $keywords Keywords as a space separated string
|
|
|
|
* @return object DataObjectSet of result pages
|
|
|
|
*/
|
2009-09-30 05:50:27 +02:00
|
|
|
public function searchEngine($classesToSearch, $keywords, $start, $pageLength, $sortBy = "ts_rank DESC", $extraFilter = "", $booleanSearch = false, $alternativeFileFilter = "", $invertedMatch = false) {
|
2010-08-03 06:18:51 +02:00
|
|
|
//Fix the keywords to be ts_query compatitble:
|
|
|
|
//Spaces must have pipes
|
|
|
|
//@TODO: properly handle boolean operators here.
|
2013-04-03 06:27:11 +02:00
|
|
|
$keywords= trim($keywords);
|
|
|
|
$keywords= str_replace(' ', ' | ', $keywords);
|
|
|
|
$keywords= str_replace('"', "'", $keywords);
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
$keywords = $this->quoteString(trim($keywords));
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2009-09-30 05:50:27 +02:00
|
|
|
//We can get a list of all the tsvector columns though this query:
|
|
|
|
//We know what tables to search in based on the $classesToSearch variable:
|
2013-04-03 06:27:11 +02:00
|
|
|
$classesPlaceholders = DB::placeholders($classesToSearch);
|
|
|
|
$result = $this->preparedQuery("
|
|
|
|
SELECT table_name, column_name, data_type
|
|
|
|
FROM information_schema.columns
|
|
|
|
WHERE data_type='tsvector' AND table_name in ($classesPlaceholders);",
|
|
|
|
$classesToSearch
|
|
|
|
);
|
2010-03-31 02:27:28 +02:00
|
|
|
if (!$result->numRecords()) throw new Exception('there are no full text columns to search');
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
$tables = array();
|
|
|
|
$tableParameters = array();
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2009-10-05 22:26:41 +02:00
|
|
|
// Make column selection lists
|
|
|
|
$select = array(
|
2012-05-08 05:33:18 +02:00
|
|
|
'SiteTree' => array(
|
2013-04-03 06:27:11 +02:00
|
|
|
'"ClassName"',
|
|
|
|
'"SiteTree"."ID"',
|
|
|
|
'"ParentID"',
|
|
|
|
'"Title"',
|
|
|
|
'"URLSegment"',
|
|
|
|
'"Content"',
|
|
|
|
'"LastEdited"',
|
|
|
|
'"Created"',
|
|
|
|
'NULL AS "Name"',
|
|
|
|
'"CanViewType"'
|
2012-05-08 05:33:18 +02:00
|
|
|
),
|
|
|
|
'File' => array(
|
2013-04-03 06:27:11 +02:00
|
|
|
'"ClassName"',
|
|
|
|
'"File"."ID"',
|
|
|
|
'0 AS "ParentID"',
|
|
|
|
'"Title"',
|
|
|
|
'NULL AS "URLSegment"',
|
2015-10-16 00:47:58 +02:00
|
|
|
'NULL AS "Content"',
|
2013-04-03 06:27:11 +02:00
|
|
|
'"LastEdited"',
|
|
|
|
'"Created"',
|
|
|
|
'"Name"',
|
|
|
|
'NULL AS "CanViewType"'
|
2012-05-08 05:33:18 +02:00
|
|
|
)
|
2009-10-05 22:26:41 +02:00
|
|
|
);
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2009-09-16 05:51:38 +02:00
|
|
|
foreach($result as $row){
|
2013-04-03 06:27:11 +02:00
|
|
|
$conditions = array();
|
|
|
|
if($row['table_name'] === 'SiteTree' || $row['table_name'] === 'File') {
|
|
|
|
$conditions[] = array('"ShowInSearch"' => 1);
|
2011-09-15 16:01:53 +02:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
$method = self::default_fts_search_method();
|
|
|
|
$conditions[] = "\"{$row['table_name']}\".\"{$row['column_name']}\" $method q ";
|
2014-07-14 23:32:26 +02:00
|
|
|
$query = DataObject::get($row['table_name'], $conditions)->dataQuery()->query();
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
// Could parameterise this, but convention is only to to so for where conditions
|
|
|
|
$query->addFrom(array(
|
|
|
|
'tsearch' => ", to_tsquery('" . self::search_language() . "', $keywords) AS q"
|
|
|
|
));
|
2012-05-08 05:33:18 +02:00
|
|
|
$query->setSelect(array());
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2012-05-08 05:33:18 +02:00
|
|
|
foreach($select[$row['table_name']] as $clause) {
|
|
|
|
if(preg_match('/^(.*) +AS +"?([^"]*)"?/i', $clause, $matches)) {
|
|
|
|
$query->selectField($matches[1], $matches[2]);
|
|
|
|
} else {
|
|
|
|
$query->selectField($clause);
|
|
|
|
}
|
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2012-05-08 05:33:18 +02:00
|
|
|
$query->selectField("ts_rank(\"{$row['table_name']}\".\"{$row['column_name']}\", q)", 'Relevance');
|
|
|
|
$query->setOrderBy(array());
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2009-09-30 05:50:27 +02:00
|
|
|
//Add this query to the collection
|
2013-04-03 06:27:11 +02:00
|
|
|
$tables[] = $query->sql($parameters);
|
|
|
|
$tableParameters = array_merge($tableParameters, $parameters);
|
2009-09-16 05:51:38 +02:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
$limit = $pageLength;
|
|
|
|
$offset = $start;
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
if($keywords) $orderBy = " ORDER BY $sortBy";
|
2009-09-30 05:50:27 +02:00
|
|
|
else $orderBy='';
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2009-09-30 05:50:27 +02:00
|
|
|
$fullQuery = "SELECT * FROM (" . implode(" UNION ", $tables) . ") AS q1 $orderBy LIMIT $limit OFFSET $offset";
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2009-09-16 05:51:38 +02:00
|
|
|
// Get records
|
2013-04-03 06:27:11 +02:00
|
|
|
$records = $this->preparedQuery($fullQuery, $tableParameters);
|
2009-09-30 05:50:27 +02:00
|
|
|
$totalCount=0;
|
2009-09-16 05:51:38 +02:00
|
|
|
foreach($records as $record){
|
2009-10-05 22:26:41 +02:00
|
|
|
$objects[] = new $record['ClassName']($record);
|
2009-09-30 05:50:27 +02:00
|
|
|
$totalCount++;
|
2009-09-16 05:51:38 +02:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2012-05-08 05:33:18 +02:00
|
|
|
if(isset($objects)) $results = new ArrayList($objects);
|
|
|
|
else $results = new ArrayList();
|
|
|
|
$list = new PaginatedList($results);
|
2012-09-17 06:51:20 +02:00
|
|
|
$list->setLimitItems(false);
|
2012-05-08 05:33:18 +02:00
|
|
|
$list->setPageStart($start);
|
|
|
|
$list->setPageLength($pageLength);
|
|
|
|
$list->setTotalItems($totalCount);
|
|
|
|
return $list;
|
2009-09-16 05:51:38 +02:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
public function supportsTransactions() {
|
2009-10-01 23:01:23 +02:00
|
|
|
return $this->supportsTransactions;
|
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2009-10-08 03:19:15 +02:00
|
|
|
/*
|
|
|
|
* This is a quick lookup to discover if the database supports particular extensions
|
|
|
|
*/
|
|
|
|
public function supportsExtensions($extensions=Array('partitions', 'tablespaces', 'clustering')){
|
2013-04-03 06:27:11 +02:00
|
|
|
if(isset($extensions['partitions'])) return true;
|
|
|
|
elseif(isset($extensions['tablespaces'])) return true;
|
|
|
|
elseif(isset($extensions['clustering'])) return true;
|
|
|
|
else return false;
|
2011-03-11 04:43:27 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
public function transactionStart($transaction_mode = false, $session_characteristics = false){
|
|
|
|
$this->query('BEGIN;');
|
2009-10-01 23:01:23 +02:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
if($transaction_mode) {
|
2014-07-14 23:32:26 +02:00
|
|
|
$this->query("SET TRANSACTION {$transaction_mode};");
|
2013-04-03 06:27:11 +02:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
if($session_characteristics) {
|
2014-07-14 23:32:26 +02:00
|
|
|
$this->query("SET SESSION CHARACTERISTICS AS TRANSACTION {$session_characteristics};");
|
2013-04-03 06:27:11 +02:00
|
|
|
}
|
2009-10-01 23:01:23 +02:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2009-10-01 23:01:23 +02:00
|
|
|
public function transactionSavepoint($savepoint){
|
2014-07-14 23:32:26 +02:00
|
|
|
$this->query("SAVEPOINT {$savepoint};");
|
2009-10-01 23:01:23 +02:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
public function transactionRollback($savepoint = false){
|
|
|
|
if($savepoint) {
|
2014-07-14 23:32:26 +02:00
|
|
|
$this->query("ROLLBACK TO {$savepoint};");
|
2013-04-03 06:27:11 +02:00
|
|
|
} else {
|
|
|
|
$this->query('ROLLBACK;');
|
2009-10-08 03:19:15 +02:00
|
|
|
}
|
2009-11-24 23:46:55 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
public function transactionEnd($chain = false){
|
|
|
|
$this->query('COMMIT;');
|
2009-10-08 03:19:15 +02:00
|
|
|
}
|
2010-02-04 05:29:37 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
public function comparisonClause($field, $value, $exact = false, $negate = false, $caseSensitive = null, $parameterised = false) {
|
2012-12-11 01:47:47 +01:00
|
|
|
if($exact && $caseSensitive === null) {
|
|
|
|
$comp = ($negate) ? '!=' : '=';
|
|
|
|
} else {
|
|
|
|
$comp = ($caseSensitive === true) ? 'LIKE' : 'ILIKE';
|
|
|
|
if($negate) $comp = 'NOT ' . $comp;
|
2014-04-08 03:24:05 +02:00
|
|
|
$field.='::text';
|
2012-12-11 01:47:47 +01:00
|
|
|
}
|
2013-04-03 06:27:11 +02:00
|
|
|
|
|
|
|
if($parameterised) {
|
|
|
|
return sprintf("%s %s ?", $field, $comp);
|
|
|
|
} else {
|
|
|
|
return sprintf("%s %s '%s'", $field, $comp, $value);
|
|
|
|
}
|
2012-12-11 01:47:47 +01:00
|
|
|
}
|
|
|
|
|
2010-02-04 05:29:37 +01:00
|
|
|
/**
|
|
|
|
* Function to return an SQL datetime expression that can be used with Postgres
|
|
|
|
* used for querying a datetime in a certain format
|
|
|
|
* @param string $date to be formated, can be either 'now', literal datetime like '1973-10-14 10:30:00' or field name, e.g. '"SiteTree"."Created"'
|
|
|
|
* @param string $format to be used, supported specifiers:
|
|
|
|
* %Y = Year (four digits)
|
|
|
|
* %m = Month (01..12)
|
|
|
|
* %d = Day (01..31)
|
|
|
|
* %H = Hour (00..23)
|
|
|
|
* %i = Minutes (00..59)
|
|
|
|
* %s = Seconds (00..59)
|
|
|
|
* %U = unix timestamp, can only be used on it's own
|
|
|
|
* @return string SQL datetime expression to query for a formatted datetime
|
|
|
|
*/
|
2014-07-14 23:32:26 +02:00
|
|
|
public function formattedDatetimeClause($date, $format) {
|
2010-02-04 05:29:37 +01:00
|
|
|
preg_match_all('/%(.)/', $format, $matches);
|
2014-07-14 23:32:26 +02:00
|
|
|
foreach($matches[1] as $match) {
|
2013-04-03 06:27:11 +02:00
|
|
|
if(array_search($match, array('Y','m','d','H','i','s','U')) === false) {
|
|
|
|
user_error('formattedDatetimeClause(): unsupported format character %' . $match, E_USER_WARNING);
|
|
|
|
}
|
|
|
|
}
|
2010-02-04 05:29:37 +01:00
|
|
|
|
|
|
|
$translate = array(
|
|
|
|
'/%Y/' => 'YYYY',
|
|
|
|
'/%m/' => 'MM',
|
|
|
|
'/%d/' => 'DD',
|
|
|
|
'/%H/' => 'HH24',
|
|
|
|
'/%i/' => 'MI',
|
|
|
|
'/%s/' => 'SS',
|
|
|
|
);
|
|
|
|
$format = preg_replace(array_keys($translate), array_values($translate), $format);
|
|
|
|
|
|
|
|
if(preg_match('/^now$/i', $date)) {
|
|
|
|
$date = "NOW()";
|
|
|
|
} else if(preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date)) {
|
|
|
|
$date = "TIMESTAMP '$date'";
|
|
|
|
}
|
|
|
|
|
2010-02-04 22:30:10 +01:00
|
|
|
if($format == '%U') return "FLOOR(EXTRACT(epoch FROM $date))";
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2010-02-04 05:29:37 +01:00
|
|
|
return "to_char($date, TEXT '$format')";
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2010-02-04 05:29:37 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2010-02-04 05:29:37 +01:00
|
|
|
/**
|
|
|
|
* Function to return an SQL datetime expression that can be used with Postgres
|
|
|
|
* used for querying a datetime addition
|
|
|
|
* @param string $date, can be either 'now', literal datetime like '1973-10-14 10:30:00' or field name, e.g. '"SiteTree"."Created"'
|
|
|
|
* @param string $interval to be added, use the format [sign][integer] [qualifier], e.g. -1 Day, +15 minutes, +1 YEAR
|
|
|
|
* supported qualifiers:
|
|
|
|
* - years
|
|
|
|
* - months
|
|
|
|
* - days
|
|
|
|
* - hours
|
|
|
|
* - minutes
|
|
|
|
* - seconds
|
|
|
|
* This includes the singular forms as well
|
|
|
|
* @return string SQL datetime expression to query for a datetime (YYYY-MM-DD hh:mm:ss) which is the result of the addition
|
|
|
|
*/
|
2014-07-14 23:32:26 +02:00
|
|
|
public function datetimeIntervalClause($date, $interval) {
|
2010-02-04 05:29:37 +01:00
|
|
|
if(preg_match('/^now$/i', $date)) {
|
|
|
|
$date = "NOW()";
|
|
|
|
} else if(preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date)) {
|
|
|
|
$date = "TIMESTAMP '$date'";
|
|
|
|
}
|
|
|
|
|
2010-04-30 06:26:53 +02:00
|
|
|
// ... when being too precise becomes a pain. we need to cut of the fractions.
|
2010-02-04 22:30:10 +01:00
|
|
|
// TIMESTAMP(0) doesn't work because it rounds instead flooring
|
|
|
|
return "CAST(SUBSTRING(CAST($date + INTERVAL '$interval' AS VARCHAR) FROM 1 FOR 19) AS TIMESTAMP)";
|
2010-02-04 05:29:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to return an SQL datetime expression that can be used with Postgres
|
|
|
|
* used for querying a datetime substraction
|
|
|
|
* @param string $date1, can be either 'now', literal datetime like '1973-10-14 10:30:00' or field name, e.g. '"SiteTree"."Created"'
|
|
|
|
* @param string $date2 to be substracted of $date1, can be either 'now', literal datetime like '1973-10-14 10:30:00' or field name, e.g. '"SiteTree"."Created"'
|
|
|
|
* @return string SQL datetime expression to query for the interval between $date1 and $date2 in seconds which is the result of the substraction
|
|
|
|
*/
|
2014-07-14 23:32:26 +02:00
|
|
|
public function datetimeDifferenceClause($date1, $date2) {
|
2010-02-04 05:29:37 +01:00
|
|
|
if(preg_match('/^now$/i', $date1)) {
|
|
|
|
$date1 = "NOW()";
|
|
|
|
} else if(preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date1)) {
|
|
|
|
$date1 = "TIMESTAMP '$date1'";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(preg_match('/^now$/i', $date2)) {
|
|
|
|
$date2 = "NOW()";
|
|
|
|
} else if(preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date2)) {
|
|
|
|
$date2 = "TIMESTAMP '$date2'";
|
|
|
|
}
|
|
|
|
|
2010-02-04 22:30:10 +01:00
|
|
|
return "(FLOOR(EXTRACT(epoch FROM $date1)) - FLOOR(EXTRACT(epoch from $date2)))";
|
2010-02-04 05:29:37 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
function now(){
|
|
|
|
return 'NOW()';
|
2010-03-18 04:16:30 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
function random(){
|
|
|
|
return 'RANDOM()';
|
2010-11-25 04:45:32 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2010-11-25 04:45:32 +01:00
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* Determines the name of the current database to be reported externally
|
|
|
|
* by substituting the schema name for the database name.
|
|
|
|
* Should only be used when model_schema_as_database is true
|
|
|
|
*
|
|
|
|
* @param string $schema Name of the schema
|
|
|
|
* @return string Name of the database to report
|
2010-11-25 04:45:32 +01:00
|
|
|
*/
|
2013-04-03 06:27:11 +02:00
|
|
|
public function schemaToDatabaseName($schema) {
|
|
|
|
switch($schema) {
|
|
|
|
case $this->schemaOriginal: return $this->databaseOriginal;
|
|
|
|
default: return $schema;
|
|
|
|
}
|
2010-11-25 04:45:32 +01:00
|
|
|
}
|
2008-11-23 02:20:39 +01:00
|
|
|
|
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* Translates a requested database name to a schema name to substitute internally.
|
|
|
|
* Should only be used when model_schema_as_database is true
|
|
|
|
*
|
|
|
|
* @param string $database Name of the database
|
|
|
|
* @return string Name of the schema to use for this database internally
|
2008-11-23 02:20:39 +01:00
|
|
|
*/
|
2013-04-03 06:27:11 +02:00
|
|
|
public function databaseToSchemaName($database) {
|
|
|
|
switch($database) {
|
|
|
|
case $this->databaseOriginal: return $this->schemaOriginal;
|
|
|
|
default: return $database;
|
|
|
|
}
|
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
public function dropSelectedDatabase() {
|
|
|
|
if(self::model_schema_as_database()) {
|
|
|
|
// Check current schema is valid
|
|
|
|
$oldSchema = $this->schema;
|
|
|
|
if(empty($oldSchema)) return true; // Nothing selected to drop
|
2008-11-23 02:20:39 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
// Select another schema
|
|
|
|
if($oldSchema !== $this->schemaOriginal) {
|
|
|
|
$this->setSchema($this->schemaOriginal);
|
|
|
|
} elseif($oldSchema !== self::MASTER_SCHEMA) {
|
|
|
|
$this->setSchema(self::MASTER_SCHEMA);
|
|
|
|
} else {
|
|
|
|
$this->schema = null;
|
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
// Remove this schema
|
|
|
|
$this->schemaManager->dropSchema($oldSchema);
|
|
|
|
} else {
|
|
|
|
parent::dropSelectedDatabase();
|
|
|
|
}
|
2008-11-23 02:20:39 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
public function getSelectedDatabase() {
|
|
|
|
if(self::model_schema_as_database()) {
|
|
|
|
return $this->schemaToDatabaseName($this->schema);
|
|
|
|
}
|
|
|
|
return parent::getSelectedDatabase();
|
2008-11-23 02:20:39 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
public function selectDatabase($name, $create = false, $errorLevel = E_USER_ERROR) {
|
|
|
|
// Substitute schema here as appropriate
|
|
|
|
if(self::model_schema_as_database()) {
|
|
|
|
// Selecting the database itself should be treated as selecting the public schema
|
|
|
|
$schemaName = $this->databaseToSchemaName($name);
|
|
|
|
return $this->setSchema($schemaName, $create, $errorLevel);
|
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
// Database selection requires that a new connection is established.
|
|
|
|
// This is not ideal postgres practise
|
|
|
|
if (!$this->schemaManager->databaseExists($name)) {
|
|
|
|
// Check DB creation permisson
|
|
|
|
if (!$create) {
|
|
|
|
if ($errorLevel !== false) {
|
|
|
|
user_error("Attempted to connect to non-existing database \"$name\"", $errorLevel);
|
|
|
|
}
|
|
|
|
// Unselect database
|
|
|
|
$this->connector->unloadDatabase();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->schemaManager->createDatabase($name);
|
2008-11-23 02:20:39 +01:00
|
|
|
}
|
2013-04-03 06:27:11 +02:00
|
|
|
|
|
|
|
// New connection made here, treating the new database name as the new original
|
|
|
|
$this->databaseOriginal = $name;
|
|
|
|
$this->connectDefault();
|
2008-11-23 02:20:39 +01:00
|
|
|
}
|
2011-01-11 22:17:17 +01:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
/**
|
|
|
|
* Delete all entries from the table instead of truncating it.
|
|
|
|
*
|
|
|
|
* This gives a massive speed improvement compared to using TRUNCATE, with
|
|
|
|
* the caveat that primary keys are not reset etc.
|
|
|
|
*
|
|
|
|
* @see DatabaseAdmin::clearAllData()
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
*/
|
|
|
|
public function clearTable($table) {
|
|
|
|
$this->query('DELETE FROM "'.$table.'";');
|
|
|
|
}
|
2008-11-23 02:20:39 +01:00
|
|
|
}
|