Merge pull request #52 from open-sausages/pulls/namespace

Update for namespaced changes
This commit is contained in:
Sam Minnée 2016-07-04 15:46:24 +12:00 committed by GitHub
commit eadb7ac352
15 changed files with 262 additions and 338 deletions

View File

@ -18,4 +18,4 @@ before_script:
- cd ~/builds/ss - cd ~/builds/ss
script: script:
- phpunit framework/tests - vendor/bin/phpunit framework/tests

7
.upgrade.yml Normal file
View File

@ -0,0 +1,7 @@
mappings:
PostgreSQLConnector: SilverStripe\PostgreSQL\PostgreSQLConnector
PostgreSQLDatabase: SilverStripe\PostgreSQL\PostgreSQLDatabase
PostgreSQLDatabaseConfigurationHelper: SilverStripe\PostgreSQL\PostgreSQLDatabaseConfigurationHelper
PostgreSQLQuery: SilverStripe\PostgreSQL\PostgreSQLQuery
PostgreSQLQueryBuilder: SilverStripe\PostgreSQL\PostgreSQLQueryBuilder
PostgreSQLSchemaManager: SilverStripe\PostgreSQL\PostgreSQLSchemaManager

View File

@ -1 +0,0 @@
<?php

View File

@ -3,14 +3,21 @@ name: postgresqlconnectors
--- ---
Injector: Injector:
PostgrePDODatabase: PostgrePDODatabase:
class: 'PostgreSQLDatabase' class: 'SilverStripe\PostgreSQL\PostgreSQLDatabase'
properties: properties:
connector: %$PDOConnector connector: %$PDOConnector
schemaManager: %$PostgreSQLSchemaManager schemaManager: %$PostgreSQLSchemaManager
queryBuilder: %$PostgreSQLQueryBuilder queryBuilder: %$PostgreSQLQueryBuilder
PostgreSQLDatabase: PostgreSQLDatabase:
class: 'PostgreSQLDatabase' class: 'SilverStripe\PostgreSQL\PostgreSQLDatabase'
properties: properties:
connector: %$PostgreSQLConnector connector: %$PostgreSQLConnector
schemaManager: %$PostgreSQLSchemaManager schemaManager: %$PostgreSQLSchemaManager
queryBuilder: %$PostgreSQLQueryBuilder queryBuilder: %$PostgreSQLQueryBuilder
PostgreSQLConnector:
class: 'SilverStripe\PostgreSQL\PostgreSQLConnector'
type: prototype
PostgreSQLSchemaManager:
class: 'SilverStripe\PostgreSQL\PostgreSQLSchemaManager'
PostgreSQLQueryBuilder:
class: 'SilverStripe\PostgreSQL\PostgreSQLQueryBuilder'

View File

@ -1,32 +0,0 @@
PostgreSQLDatabase:
# Determines whether to check a database exists on the host by
# querying the 'postgres' database and running createDatabase.
#
# 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.
allow_query_master_postgres: true
# 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
#
# If this is false then errors may be generated during some cross database operations.
model_schema_as_database: true
# 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.
search_language: 'english'
# These two values describe how T-search will work.
# You can use either GiST or GIN, and '@@' (gist) or '@@@' (gin)
# Combinations of these two will also work, so you'll need to pick
# one which works best for you
default_fts_cluster_method: 'GIN'
default_fts_search_method: '@@@'

View File

@ -2,6 +2,7 @@
// PDO Postgre database // PDO Postgre database
DatabaseAdapterRegistry::register(array( DatabaseAdapterRegistry::register(array(
/** @skipUpgrade */
'class' => 'PostgrePDODatabase', 'class' => 'PostgrePDODatabase',
'title' => 'PostgreSQL 8.3+ (using PDO)', 'title' => 'PostgreSQL 8.3+ (using PDO)',
'helperPath' => dirname(__FILE__).'/code/PostgreSQLDatabaseConfigurationHelper.php', 'helperPath' => dirname(__FILE__).'/code/PostgreSQLDatabaseConfigurationHelper.php',
@ -15,6 +16,7 @@ DatabaseAdapterRegistry::register(array(
// PDO Postgre database // PDO Postgre database
DatabaseAdapterRegistry::register(array( DatabaseAdapterRegistry::register(array(
/** @skipUpgrade */
'class' => 'PostgreSQLDatabase', 'class' => 'PostgreSQLDatabase',
'title' => 'PostgreSQL 8.3+ (using pg_connect)', 'title' => 'PostgreSQL 8.3+ (using pg_connect)',
'helperPath' => dirname(__FILE__).'/code/PostgreSQLDatabaseConfigurationHelper.php', 'helperPath' => dirname(__FILE__).'/code/PostgreSQLDatabaseConfigurationHelper.php',

View File

@ -1,5 +1,10 @@
<?php <?php
namespace SilverStripe\PostgreSQL;
use SilverStripe\ORM\Connect\DBConnector;
use ErrorException;
/** /**
* PostgreSQL connector class using the PostgreSQL specific api * PostgreSQL connector class using the PostgreSQL specific api
* *
@ -216,14 +221,16 @@ class PostgreSQLConnector extends DBConnector
} }
// Execute query // Execute query
// Unfortunately error-suppression is required in order to handle sql errors elegantly.
// Please use PDO if you can help it
if (!empty($parameters)) { if (!empty($parameters)) {
$result = pg_query_params($this->dbConn, $sql, $parameters); $result = @pg_query_params($this->dbConn, $sql, $parameters);
} else { } else {
$result = pg_query($this->dbConn, $sql); $result = @pg_query($this->dbConn, $sql);
} }
// Handle error // Handle error
if ($result === false) { if (!$result) {
$this->databaseError($this->getLastError(), $errorLevel, $sql, $parameters); $this->databaseError($this->getLastError(), $errorLevel, $sql, $parameters);
return null; return null;
} }
@ -253,16 +260,6 @@ class PostgreSQLConnector extends DBConnector
return pg_escape_string($this->dbConn, $value); return pg_escape_string($this->dbConn, $value);
} }
public function escapeIdentifier($value, $separator = '.')
{
if (empty($separator) && function_exists('pg_escape_identifier')) {
return pg_escape_identifier($this->dbConn, $value);
}
// Let parent function handle recursive calls
return parent::escapeIdentifier($value, $separator);
}
public function selectDatabase($name) public function selectDatabase($name)
{ {
if ($name !== $this->databaseName) { if ($name !== $this->databaseName) {

View File

@ -1,5 +1,17 @@
<?php <?php
namespace SilverStripe\PostgreSQL;
use SilverStripe\Framework\Core\Configurable;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\Connect\SS_Database;
use Config;
use ErrorException;
use Exception;
use PaginatedList;
/** /**
* PostgreSQL connector class. * PostgreSQL connector class.
* *
@ -8,6 +20,8 @@
*/ */
class PostgreSQLDatabase extends SS_Database class PostgreSQLDatabase extends SS_Database
{ {
use Configurable;
/** /**
* Database schema manager object * Database schema manager object
* *
@ -22,8 +36,66 @@ class PostgreSQLDatabase extends SS_Database
*/ */
protected $schema; protected $schema;
/**
* Toggle if transactions are supported. Defaults to true.
*
* @var bool
*/
protected $supportsTransactions = true; protected $supportsTransactions = true;
/**
* Determines whether to check a database exists on the host by
* querying the 'postgres' database and running createDatabase.
*
* 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.
*
* @config
* @var bool
*/
private static $allow_query_master_postgres = true;
/**
* 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
*
* If this is false then errors may be generated during some cross database operations.
*/
private static $model_schema_as_database = true;
/**
* 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.
*/
private static $search_language = 'english';
/*
* Describe how T-search will work.
* You can use either GiST or GIN, and '@@' (gist) or '@@@' (gin)
* Combinations of these two will also work, so you'll need to pick
* one which works best for you
*/
private static $default_fts_cluster_method = 'GIN';
/*
* Describe how T-search will work.
* You can use either GiST or GIN, and '@@' (gist) or '@@@' (gin)
* Combinations of these two will also work, so you'll need to pick
* one which works best for you
*/
private static $default_fts_search_method = '@@@';
const MASTER_DATABASE = 'postgres'; const MASTER_DATABASE = 'postgres';
const MASTER_SCHEMA = 'public'; const MASTER_SCHEMA = 'public';
@ -35,7 +107,7 @@ class PostgreSQLDatabase extends SS_Database
*/ */
public static function default_fts_cluster_method() public static function default_fts_cluster_method()
{ {
return Config::inst()->get('PostgreSQLDatabase', 'default_fts_cluster_method'); return static::config()->default_fts_cluster_method;
} }
/** /**
@ -45,7 +117,7 @@ class PostgreSQLDatabase extends SS_Database
*/ */
public static function default_fts_search_method() public static function default_fts_search_method()
{ {
return Config::inst()->get('PostgreSQLDatabase', 'default_fts_search_method'); return static::config()->default_fts_search_method;
} }
/** /**
@ -58,10 +130,12 @@ class PostgreSQLDatabase extends SS_Database
* If allow_query_master_postgres is false, and model_schema_as_database is also 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 * then attempts to create or check databases beyond the initial connection will
* result in a runtime error. * result in a runtime error.
*
* @return bool
*/ */
public static function allow_query_master_postgres() public static function allow_query_master_postgres()
{ {
return Config::inst()->get('PostgreSQLDatabase', 'allow_query_master_postgres'); return static::config()->allow_query_master_postgres;
} }
/** /**
@ -73,10 +147,12 @@ class PostgreSQLDatabase extends SS_Database
* *
* If this is true then the database will only be set during the initial connection, * 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 * and attempts to change to this database will use the 'public' schema instead
*
* @return bool
*/ */
public static function model_schema_as_database() public static function model_schema_as_database()
{ {
return Config::inst()->get('PostgreSQLDatabase', 'model_schema_as_database'); return static::config()->model_schema_as_database;
} }
/** /**
@ -84,11 +160,11 @@ class PostgreSQLDatabase extends SS_Database
* could be any of the supported languages that can be found in the * could be any of the supported languages that can be found in the
* pg_catalog.pg_ts_config table. * pg_catalog.pg_ts_config table.
* *
* @var string * @return string
*/ */
public static function search_language() public static function search_language()
{ {
return Config::inst()->get('PostgreSQLDatabase', 'search_language'); return static::config()->search_language;
} }
/** /**
@ -223,7 +299,7 @@ class PostgreSQLDatabase extends SS_Database
* Utility method to manually set the schema to an alternative * Utility method to manually set the schema to an alternative
* Check existance & sets search path to the supplied schema name * Check existance & sets search path to the supplied schema name
* *
* @param string $name Name of the schema * @param string $schema Name of the schema
* @param boolean $create Flag indicating whether the schema should be created * @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 * if it doesn't exist. If $create is false and the schema doesn't exist
* then an error will be raised * then an error will be raised
@ -257,13 +333,11 @@ class PostgreSQLDatabase extends SS_Database
* SQL queries. Sapphire cannot search for datamodel tables in alternate * SQL queries. Sapphire cannot search for datamodel tables in alternate
* schemas, so be wary of using alternate schemas within the ORM environment. * schemas, so be wary of using alternate schemas within the ORM environment.
* *
* @param string $arg1 First schema to use * @param string ...$arg Schema name to use. Add additional schema names as extra arguments.
* @param string $arg2 Second schema to use
* @param string $argN Nth schema to use
*/ */
public function setSchemaSearchPath() public function setSchemaSearchPath($arg = null)
{ {
if (func_num_args() == 0) { if (!$arg) {
user_error('At least one Schema must be supplied to set a search path.', E_USER_ERROR); user_error('At least one Schema must be supplied to set a search path.', E_USER_ERROR);
} }
$schemas = array_values(func_get_args()); $schemas = array_values(func_get_args());
@ -274,8 +348,17 @@ class PostgreSQLDatabase extends SS_Database
* The core search engine configuration. * The core search engine configuration.
* @todo Properly extract the search functions out of the core. * @todo Properly extract the search functions out of the core.
* *
* @param array $classesToSearch
* @param string $keywords Keywords as a space separated string * @param string $keywords Keywords as a space separated string
* @return object DataObjectSet of result pages * @param int $start
* @param int $pageLength
* @param string $sortBy
* @param string $extraFilter
* @param bool $booleanSearch
* @param string $alternativeFileFilter
* @param bool $invertedMatch
* @return PaginatedList List of result pages
* @throws Exception
*/ */
public function searchEngine($classesToSearch, $keywords, $start, $pageLength, $sortBy = "ts_rank DESC", $extraFilter = "", $booleanSearch = false, $alternativeFileFilter = "", $invertedMatch = false) public function searchEngine($classesToSearch, $keywords, $start, $pageLength, $sortBy = "ts_rank DESC", $extraFilter = "", $booleanSearch = false, $alternativeFileFilter = "", $invertedMatch = false)
{ {
@ -613,7 +696,7 @@ class PostgreSQLDatabase extends SS_Database
// Check current schema is valid // Check current schema is valid
$oldSchema = $this->schema; $oldSchema = $this->schema;
if (empty($oldSchema)) { if (empty($oldSchema)) {
return true; return;
} // Nothing selected to drop } // Nothing selected to drop
// Select another schema // Select another schema
@ -667,6 +750,7 @@ class PostgreSQLDatabase extends SS_Database
// New connection made here, treating the new database name as the new original // New connection made here, treating the new database name as the new original
$this->databaseOriginal = $name; $this->databaseOriginal = $name;
$this->connectDefault(); $this->connectDefault();
return true;
} }
/** /**

View File

@ -1,4 +1,12 @@
<?php <?php
namespace SilverStripe\PostgreSQL;
use DatabaseConfigurationHelper;
use PDO;
use Exception;
use DatabaseAdapterRegistry;
/** /**
* This is a helper class for the SS installer. * This is a helper class for the SS installer.
* *
@ -120,24 +128,6 @@ class PostgreSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelp
); );
} }
/**
* Helper function to quote a string value
*
* @param mixed $conn Connection object/resource
* @param string $value Value to quote
* @return string Quoted strieng
*/
protected function quote($conn, $value)
{
if ($conn instanceof PDO) {
return $conn->quote($value);
} elseif (is_resource($conn)) {
return "'".pg_escape_string($conn, $value)."'";
} else {
user_error('Invalid database connection', E_USER_ERROR);
}
}
/** /**
* Helper function to execute a query * Helper function to execute a query
* *

View File

@ -1,5 +1,9 @@
<?php <?php
namespace SilverStripe\PostgreSQL;
use SilverStripe\ORM\Connect\SS_Query;
/** /**
* A result-set from a PostgreSQL database. * A result-set from a PostgreSQL database.
* *
@ -16,8 +20,7 @@ class PostgreSQLQuery extends SS_Query
/** /**
* Hook the result-set given into a Query class, suitable for use by sapphire. * Hook the result-set given into a Query class, suitable for use by sapphire.
* @param database The database object that created this query. * @param resource $handle the internal Postgres handle that is points to the resultset.
* @param handle the internal Postgres handle that is points to the resultset.
*/ */
public function __construct($handle) public function __construct($handle)
{ {

View File

@ -1,5 +1,11 @@
<?php <?php
namespace SilverStripe\PostgreSQL;
use SilverStripe\ORM\Queries\SQLSelect;
use SilverStripe\ORM\Connect\DBQueryBuilder;
use InvalidArgumentException;
class PostgreSQLQueryBuilder extends DBQueryBuilder class PostgreSQLQueryBuilder extends DBQueryBuilder
{ {
/** /**

View File

@ -1,5 +1,11 @@
<?php <?php
namespace SilverStripe\PostgreSQL;
use SilverStripe\ORM\Connect\DBSchemaManager;
use SilverStripe\ORM\DB;
use Deprecation;
/** /**
* PostgreSQL schema manager * PostgreSQL schema manager
* *
@ -122,7 +128,8 @@ class PostgreSQLSchemaManager extends DBSchemaManager
{ {
if (PostgreSQLDatabase::model_schema_as_database()) { if (PostgreSQLDatabase::model_schema_as_database()) {
$schemaName = $this->database->databaseToSchemaName($name); $schemaName = $this->database->databaseToSchemaName($name);
return $this->dropSchema($schemaName); $this->dropSchema($schemaName);
return;
} }
$this->dropPostgresDatabase($name); $this->dropPostgresDatabase($name);
} }
@ -400,7 +407,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
if ($alteredOptions && isset($this->class) && isset($alteredOptions[$this->class])) { if ($alteredOptions && isset($this->class) && isset($alteredOptions[$this->class])) {
$this->query(sprintf("ALTER TABLE \"%s\" %s", $table, $alteredOptions[$this->class])); $this->query(sprintf("ALTER TABLE \"%s\" %s", $table, $alteredOptions[$this->class]));
Database::alteration_message( DB::alteration_message(
sprintf("Table %s options changed: %s", $table, $alteredOptions[$this->class]), sprintf("Table %s options changed: %s", $table, $alteredOptions[$this->class]),
"changed" "changed"
); );
@ -710,37 +717,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
} }
} }
/* protected function getIndexSqlDefinition($tableName, $indexName, $indexSpec)
* @todo - factor out? Is DBSchemaManager::convertIndexSpec sufficient?
public function convertIndexSpec($indexSpec, $asDbValue=false, $table=''){
if(!$asDbValue){
if(is_array($indexSpec)){
//Here we create a db-specific version of whatever index we need to create.
switch($indexSpec['type']){
case 'fulltext':
$indexSpec='fulltext (' . $indexSpec['value'] . ')';
break;
case 'unique':
$indexSpec='unique (' . $indexSpec['value'] . ')';
break;
case 'hash':
$indexSpec='using hash (' . $indexSpec['value'] . ')';
break;
case 'index':
//The default index is 'btree', which we'll use by default (below):
default:
$indexSpec='using btree (' . $indexSpec['value'] . ')';
break;
}
}
} else {
$indexSpec = $this->buildPostgresIndexName($table, $indexSpec);
}
return $indexSpec;
}*/
protected function getIndexSqlDefinition($tableName, $indexName, $indexSpec, $asDbValue=false)
{ {
//TODO: create table partition support //TODO: create table partition support
@ -750,12 +727,6 @@ class PostgreSQLSchemaManager extends DBSchemaManager
//Therefore, we now check for the existance of indexes before we create them. //Therefore, we now check for the existance of indexes before we create them.
//This is techically a bug, since new tables will not be indexed. //This is techically a bug, since new tables will not be indexed.
// If requesting the definition rather than the DDL
if ($asDbValue) {
$indexName=trim($indexName, '()');
return $indexName;
}
// Determine index name // Determine index name
$tableCol = $this->buildPostgresIndexName($tableName, $indexName); $tableCol = $this->buildPostgresIndexName($tableName, $indexName);
@ -808,6 +779,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
if ($indexSpec[0] != '(') { if ($indexSpec[0] != '(') {
list($indexType, $indexFields) = explode(' ', $indexSpec, 2); list($indexType, $indexFields) = explode(' ', $indexSpec, 2);
} else { } else {
$indexType = null;
$indexFields = $indexSpec; $indexFields = $indexSpec;
} }
@ -1045,28 +1017,12 @@ class PostgreSQLSchemaManager extends DBSchemaManager
* Return a boolean type-formatted string * Return a boolean type-formatted string
* *
* @param array $values Contains a tokenised list of info about this data type * @param array $values Contains a tokenised list of info about this data type
* @param boolean $asDbValue
* @return string * @return string
*/ */
public function boolean($values, $asDbValue=false) public function boolean($values)
{ {
//Annoyingly, we need to do a good ol' fashioned switch here:
$default = $values['default'] ? '1' : '0'; $default = $values['default'] ? '1' : '0';
return "smallint default {$default}";
if (!isset($values['arrayValue'])) {
$values['arrayValue']='';
}
if ($asDbValue) {
return array('data_type'=>'smallint');
}
if ($values['arrayValue'] != '') {
$default = '';
} else {
$default = ' default ' . (int)$values['default'];
}
return "smallint{$values['arrayValue']}" . $default;
} }
/** /**
@ -1077,26 +1033,17 @@ class PostgreSQLSchemaManager extends DBSchemaManager
*/ */
public function date($values) public function date($values)
{ {
if (!isset($values['arrayValue'])) { return "date";
$values['arrayValue']='';
}
return "date{$values['arrayValue']}";
} }
/** /**
* Return a decimal type-formatted string * Return a decimal type-formatted string
* *
* @param array $values Contains a tokenised list of info about this data type * @param array $values Contains a tokenised list of info about this data type
* @param boolean $asDbValue
* @return string * @return string
*/ */
public function decimal($values, $asDbValue=false) public function decimal($values)
{ {
if (!isset($values['arrayValue'])) {
$values['arrayValue']='';
}
// Avoid empty strings being put in the db // Avoid empty strings being put in the db
if ($values['precision'] == '') { if ($values['precision'] == '') {
$precision = 1; $precision = 1;
@ -1109,11 +1056,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
$defaultValue = ' default ' . floatval($values['default']); $defaultValue = ' default ' . floatval($values['default']);
} }
if ($asDbValue) { return "decimal($precision)$defaultValue";
return array('data_type' => 'numeric', 'precision' => $precision);
} else {
return "decimal($precision){$values['arrayValue']}$defaultValue";
}
} }
/** /**
@ -1124,103 +1067,52 @@ class PostgreSQLSchemaManager extends DBSchemaManager
*/ */
public function enum($values) public function enum($values)
{ {
//Enums are a bit different. We'll be creating a varchar(255) with a constraint of all the usual enum options.
//NOTE: In this one instance, we are including the table name in the values array
if (!isset($values['arrayValue'])) {
$values['arrayValue']='';
}
if ($values['arrayValue']!='') {
$default = '';
} else {
$default = " default '{$values['default']}'"; $default = " default '{$values['default']}'";
} return "varchar(255)" . $default . " check (\"" . $values['name'] . "\" in ('" . implode('\', \'', $values['enums']) . "'))";
return "varchar(255){$values['arrayValue']}" . $default . " check (\"" . $values['name'] . "\" in ('" . implode('\', \'', $values['enums']) . "'))";
} }
/** /**
* Return a float type-formatted string * Return a float type-formatted string
* *
* @param array $values Contains a tokenised list of info about this data type * @param array $values Contains a tokenised list of info about this data type
* @param boolean $asDbValue
* @return string * @return string
*/ */
public function float($values, $asDbValue = false) public function float($values)
{ {
if (!isset($values['arrayValue'])) { return "float";
$values['arrayValue']='';
}
if ($asDbValue) {
return array('data_type' => 'double precision');
} else {
return "float{$values['arrayValue']}";
}
} }
/** /**
* Return a float type-formatted string cause double is not supported * Return a float type-formatted string cause double is not supported
* *
* @param array $values Contains a tokenised list of info about this data type * @param array $values Contains a tokenised list of info about this data type
* @param boolean $asDbValue
* @return string * @return string
*/ */
public function double($values, $asDbValue=false) public function double($values)
{ {
return $this->float($values, $asDbValue); return $this->float($values);
} }
/** /**
* Return a int type-formatted string * Return a int type-formatted string
* *
* @param array $values Contains a tokenised list of info about this data type * @param array $values Contains a tokenised list of info about this data type
* @param boolean $asDbValue
* @return string * @return string
*/ */
public function int($values, $asDbValue = false) public function int($values)
{ {
if (!isset($values['arrayValue'])) { return "integer default " . (int)$values['default'];
$values['arrayValue']='';
}
if ($asDbValue) {
return array('data_type'=>'integer', 'precision'=>'32');
}
if ($values['arrayValue']!='') {
$default='';
} else {
$default=' default ' . (int)$values['default'];
}
return "integer{$values['arrayValue']}" . $default;
} }
/** /**
* Return a bigint type-formatted string * Return a bigint type-formatted string
* *
* @param array $values Contains a tokenised list of info about this data type * @param array $values Contains a tokenised list of info about this data type
* @param boolean $asDbValue
* @return string * @return string
*/ */
public function bigint($values, $asDbValue = false) public function bigint($values)
{ {
if (!isset($values['arrayValue'])) { return "bigint default" . (int)$values['default'];
$values['arrayValue']='';
}
if ($asDbValue) {
return array('data_type'=>'bigint', 'precision'=>'64');
}
if ($values['arrayValue']!='') {
$default='';
} else {
$default=' default ' . (int)$values['default'];
}
return "bigint{$values['arrayValue']}" . $default;
} }
/** /**
@ -1228,40 +1120,22 @@ class PostgreSQLSchemaManager extends DBSchemaManager
* For PostgreSQL, we simply return the word 'timestamp', no other parameters are necessary * For PostgreSQL, we simply return the word 'timestamp', no other parameters are necessary
* *
* @param array $values Contains a tokenised list of info about this data type * @param array $values Contains a tokenised list of info about this data type
* @param boolean $asDbValue
* @return string * @return string
*/ */
public function SS_Datetime($values, $asDbValue = false) public function datetime($values)
{ {
if (!isset($values['arrayValue'])) { return "timestamp";
$values['arrayValue']='';
}
if ($asDbValue) {
return array('data_type'=>'timestamp without time zone');
} else {
return "timestamp{$values['arrayValue']}";
}
} }
/** /**
* Return a text type-formatted string * Return a text type-formatted string
* *
* @param array $values Contains a tokenised list of info about this data type * @param array $values Contains a tokenised list of info about this data type
* @param boolean $asDbValue
* @return string * @return string
*/ */
public function text($values, $asDbValue = false) public function text($values)
{ {
if (!isset($values['arrayValue'])) { return "text";
$values['arrayValue'] = '';
}
if ($asDbValue) {
return array('data_type'=>'text');
} else {
return "text{$values['arrayValue']}";
}
} }
/** /**
@ -1272,35 +1146,22 @@ class PostgreSQLSchemaManager extends DBSchemaManager
*/ */
public function time($values) public function time($values)
{ {
if (!isset($values['arrayValue'])) { return "time";
$values['arrayValue'] = '';
}
return "time{$values['arrayValue']}";
} }
/** /**
* Return a varchar type-formatted string * Return a varchar type-formatted string
* *
* @param array $values Contains a tokenised list of info about this data type * @param array $values Contains a tokenised list of info about this data type
* @param boolean $asDbValue
* @return string * @return string
*/ */
public function varchar($values, $asDbValue=false) public function varchar($values)
{ {
if (!isset($values['arrayValue'])) {
$values['arrayValue'] = '';
}
if (!isset($values['precision'])) { if (!isset($values['precision'])) {
$values['precision'] = 255; $values['precision'] = 255;
} }
if ($asDbValue) { return "varchar({$values['precision']})";
return array('data_type'=>'varchar', 'precision'=>$values['precision']);
} else {
return "varchar({$values['precision']}){$values['arrayValue']}";
}
} }
/* /*
@ -1308,21 +1169,11 @@ class PostgreSQLSchemaManager extends DBSchemaManager
* For Postgres, we'll use a 4 digit numeric * For Postgres, we'll use a 4 digit numeric
* *
* @param array $values Contains a tokenised list of info about this data type * @param array $values Contains a tokenised list of info about this data type
* @param boolean $asDbValue
* @return string * @return string
*/ */
public function year($values, $asDbValue = false) public function year($values)
{ {
if (!isset($values['arrayValue'])) { return "decimal(4,0)";
$values['arrayValue'] = '';
}
//TODO: the DbValue result does not include the numeric_scale option (ie, the ,0 value in 4,0)
if ($asDbValue) {
return array('data_type'=>'decimal', 'precision'=>'4');
} else {
return "decimal(4,0){$values['arrayValue']}";
}
} }
/** /**
@ -1334,7 +1185,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
* @param array $this_index Index specification for the fulltext index * @param array $this_index Index specification for the fulltext index
* @param string $tableName * @param string $tableName
* @param string $name * @param string $name
* @param array $spec * @return array
*/ */
protected function fulltext($this_index, $tableName, $name) protected function fulltext($this_index, $tableName, $name)
{ {
@ -1462,7 +1313,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
* @param array $indexes * @param array $indexes
* @param array $extensions * @param array $extensions
*/ */
public function createOrReplacePartition($tableName, $partitions, $indexes, $extensions) public function createOrReplacePartition($tableName, $partitions, $indexes = [], $extensions = [])
{ {
//We need the plpgsql language to be installed for this to work: //We need the plpgsql language to be installed for this to work:

View File

@ -12,6 +12,9 @@
"require": { "require": {
"silverstripe/framework": "~4.0" "silverstripe/framework": "~4.0"
}, },
"require-dev": {
"phpunit/PHPUnit": "~4.8"
},
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "2.0.x-dev" "dev-master": "2.0.x-dev"

View File

@ -1,5 +1,7 @@
<?php <?php
use SilverStripe\PostgreSQL\PostgreSQLConnector;
/** /**
* Description of PostgreSQLConnectorTest * Description of PostgreSQLConnectorTest
* *

View File

@ -1,4 +1,9 @@
<?php <?php
use SilverStripe\ORM\DB;
use SilverStripe\ORM\DataObject;
use SilverStripe\PostgreSQL\PostgreSQLDatabase;
/** /**
* @package postgresql * @package postgresql
* @subpackage tests * @subpackage tests