2013-04-03 06:27:11 +02:00
< ? php
/**
* PostgreSQL schema manager
*
* @ package sapphire
* @ subpackage model
*/
2015-12-17 19:18:01 +01:00
class PostgreSQLSchemaManager extends DBSchemaManager
{
2013-04-03 06:27:11 +02:00
/**
* Identifier for this schema , used for configuring schema - specific table
* creation options
*/
const ID = 'PostgreSQL' ;
/**
* Instance of the database controller this schema belongs to
*
* @ var PostgreSQLDatabase
*/
protected $database = null ;
/**
* This holds a copy of all the constraint results that are returned
* via the function constraintExists () . This is a bit faster than
* repeatedly querying this column , and should allow the database
* to use it ' s built - in caching features for better queries .
*
* @ var array
*/
protected static $cached_constraints = array ();
/**
*
* This holds a copy of all the queries that run through the function fieldList ()
* This is one of the most - often called functions , and repeats itself a great deal in the unit tests .
*
* @ var array
*/
protected static $cached_fieldlists = array ();
2015-12-17 19:18:01 +01:00
protected function indexKey ( $table , $index , $spec )
{
2013-04-03 06:27:11 +02:00
return $this -> buildPostgresIndexName ( $table , $index );
}
/**
* Creates a postgres database , ignoring model_schema_as_database
*
* @ param string $name
*/
2015-12-17 19:18:01 +01:00
public function createPostgresDatabase ( $name )
{
2013-04-03 06:27:11 +02:00
$this -> query ( " CREATE DATABASE \" $name\ " ; " );
}
2015-12-17 19:18:01 +01:00
public function createDatabase ( $name )
{
2013-04-03 06:27:11 +02:00
if ( PostgreSQLDatabase :: model_schema_as_database ()) {
$schemaName = $this -> database -> databaseToSchemaName ( $name );
return $this -> createSchema ( $schemaName );
}
return $this -> createPostgresDatabase ( $name );
}
/**
* Determines if a postgres database exists , ignoring model_schema_as_database
*
* @ param string $name
* @ return boolean
*/
2015-12-17 19:18:01 +01:00
public function postgresDatabaseExists ( $name )
{
2013-04-03 06:27:11 +02:00
$result = $this -> preparedQuery ( " SELECT datname FROM pg_database WHERE datname = ?; " , array ( $name ));
return $result -> first () ? true : false ;
}
2015-12-17 19:18:01 +01:00
public function databaseExists ( $name )
{
2013-04-03 06:27:11 +02:00
if ( PostgreSQLDatabase :: model_schema_as_database ()) {
$schemaName = $this -> database -> databaseToSchemaName ( $name );
return $this -> schemaExists ( $schemaName );
}
return $this -> postgresDatabaseExists ( $name );
}
/**
* Determines the list of all postgres databases , ignoring model_schema_as_database
*
* @ return array
*/
2015-12-17 19:18:01 +01:00
public function postgresDatabaseList ()
{
2013-04-03 06:27:11 +02:00
return $this -> query ( " SELECT datname FROM pg_database WHERE datistemplate=false; " ) -> column ();
}
2015-12-17 19:18:01 +01:00
public function databaseList ()
{
2013-04-03 06:27:11 +02:00
if ( PostgreSQLDatabase :: model_schema_as_database ()) {
$schemas = $this -> schemaList ();
$names = array ();
foreach ( $schemas as $schema ) {
$names [] = $this -> database -> schemaToDatabaseName ( $schema );
}
return array_unique ( $names );
}
return $this -> postgresDatabaseList ();
}
/**
* Drops a postgres database , ignoring model_schema_as_database
*
* @ param string $name
*/
2015-12-17 19:18:01 +01:00
public function dropPostgresDatabase ( $name )
{
2013-04-03 06:27:11 +02:00
$nameSQL = $this -> database -> escapeIdentifier ( $name );
$this -> query ( " DROP DATABASE $nameSQL ; " );
}
2015-12-17 19:18:01 +01:00
public function dropDatabase ( $name )
{
2013-04-03 06:27:11 +02:00
if ( PostgreSQLDatabase :: model_schema_as_database ()) {
$schemaName = $this -> database -> databaseToSchemaName ( $name );
return $this -> dropSchema ( $schemaName );
}
$this -> dropPostgresDatabase ( $name );
}
/**
* Returns true if the schema exists in the current database
*
* @ param string $name
* @ return boolean
*/
2015-12-17 19:18:01 +01:00
public function schemaExists ( $name )
{
2013-04-03 06:27:11 +02:00
return $this -> preparedQuery (
" SELECT nspname FROM pg_catalog.pg_namespace WHERE nspname = ?; " ,
array ( $name )
) -> first () ? true : false ;
}
/**
* Creates a schema in the current database
*
* @ param string $name
*/
2015-12-17 19:18:01 +01:00
public function createSchema ( $name )
{
2013-04-03 06:27:11 +02:00
$nameSQL = $this -> database -> escapeIdentifier ( $name );
$this -> query ( " CREATE SCHEMA $nameSQL ; " );
}
/**
* Drops a schema from the database . Use carefully !
*
* @ param string $name
*/
2015-12-17 19:18:01 +01:00
public function dropSchema ( $name )
{
2013-04-03 06:27:11 +02:00
$nameSQL = $this -> database -> escapeIdentifier ( $name );
$this -> query ( " DROP SCHEMA $nameSQL CASCADE; " );
}
/**
* Returns the list of all available schemas on the current database
*
* @ return array
*/
2015-12-17 19:18:01 +01:00
public function schemaList ()
{
2013-04-03 06:27:11 +02:00
return $this -> query ( "
SELECT nspname
FROM pg_catalog . pg_namespace
WHERE nspname <> 'information_schema' AND nspname !~ E '^pg_' "
) -> column ();
}
2015-12-17 19:18:01 +01:00
public function createTable ( $table , $fields = null , $indexes = null , $options = null , $advancedOptions = null )
{
2013-04-03 06:27:11 +02:00
$fieldSchemas = $indexSchemas = " " ;
2015-12-17 19:18:01 +01:00
if ( $fields ) {
foreach ( $fields as $k => $v ) {
2013-04-03 06:27:11 +02:00
$fieldSchemas .= " \" $k\ " $v , \n " ;
}
2016-01-20 21:30:15 +01:00
}
2013-04-03 06:27:11 +02:00
if ( ! empty ( $options [ self :: ID ])) {
$addOptions = $options [ self :: ID ];
} elseif ( ! empty ( $options [ get_class ( $this )])) {
Deprecation :: notice ( '3.2' , 'Use PostgreSQLSchemaManager::ID for referencing postgres-specific table creation options' );
$addOptions = $options [ get_class ( $this )];
} else {
$addOptions = null ;
}
//First of all, does this table already exist
$doesExist = $this -> hasTable ( $table );
if ( $doesExist ) {
// Table already exists, just return the name, in line with baseclass documentation.
return $table ;
}
//If we have a fulltext search request, then we need to create a special column
//for GiST searches
$fulltexts = '' ;
$triggers = '' ;
if ( $indexes ) {
foreach ( $indexes as $name => $this_index ){
if ( is_array ( $this_index ) && $this_index [ 'type' ] == 'fulltext' ) {
$ts_details = $this -> fulltext ( $this_index , $table , $name );
$fulltexts .= $ts_details [ 'fulltexts' ] . ', ' ;
$triggers .= $ts_details [ 'triggers' ];
}
}
}
2015-12-17 19:18:01 +01:00
if ( $indexes ) {
foreach ( $indexes as $k => $v ) {
2013-04-03 06:27:11 +02:00
$indexSchemas .= $this -> getIndexSqlDefinition ( $table , $k , $v ) . " \n " ;
}
2015-12-17 19:18:01 +01:00
}
2013-04-03 06:27:11 +02:00
//Do we need to create a tablespace for this item?
if ( $advancedOptions && isset ( $advancedOptions [ 'tablespace' ])){
$this -> createOrReplaceTablespace (
$advancedOptions [ 'tablespace' ][ 'name' ],
$advancedOptions [ 'tablespace' ][ 'location' ]
);
$tableSpace = ' TABLESPACE ' . $advancedOptions [ 'tablespace' ][ 'name' ];
2015-12-17 19:18:01 +01:00
} else {
2013-04-03 06:27:11 +02:00
$tableSpace = '' ;
2015-12-17 19:18:01 +01:00
}
2013-04-03 06:27:11 +02:00
$this -> query ( " CREATE TABLE \" $table\ " (
$fieldSchemas
$fulltexts
primary key ( \ " ID \" )
) $tableSpace ; $indexSchemas $addOptions " );
if ( $triggers != '' ){
$this -> query ( $triggers );
}
//If we have a partitioning requirement, we do that here:
if ( $advancedOptions && isset ( $advancedOptions [ 'partitions' ])){
$this -> createOrReplacePartition ( $table , $advancedOptions [ 'partitions' ], $indexes , $advancedOptions );
}
//Lastly, clustering goes here:
if ( $advancedOptions && isset ( $advancedOptions [ 'cluster' ])){
$this -> query ( " CLUSTER \" $table\ " USING \ " { $advancedOptions [ 'cluster' ] } \" ; " );
}
return $table ;
}
/**
* Builds the internal Postgres index name given the silverstripe table and index name
*
* @ param string $tableName
* @ param string $indexName
* @ param string $prefix The optional prefix for the index . Defaults to " ix " for indexes .
* @ return string The postgres name of the index
*/
2015-12-17 19:18:01 +01:00
protected function buildPostgresIndexName ( $tableName , $indexName , $prefix = 'ix' )
{
2013-04-03 06:27:11 +02:00
// Assume all indexes also contain the table name
// MD5 the table/index name combo to keep it to a fixed length.
// Exclude the prefix so that the trigger name can be easily generated from the index name
$indexNamePG = " { $prefix } _ " . md5 ( " { $tableName } _ { $indexName } " );
// Limit to 63 characters
if ( strlen ( $indexNamePG ) > 63 ) {
return substr ( $indexNamePG , 0 , 63 );
} else {
return $indexNamePG ;
}
}
/**
* Builds the internal Postgres trigger name given the silverstripe table and trigger name
*
* @ param string $tableName
* @ param string $triggerName
* @ return string The postgres name of the trigger
*/
2015-12-17 19:18:01 +01:00
public function buildPostgresTriggerName ( $tableName , $triggerName )
{
2013-04-03 06:27:11 +02:00
// Kind of cheating, but behaves the same way as indexes
return $this -> buildPostgresIndexName ( $tableName , $triggerName , 'ts' );
}
2015-12-17 19:18:01 +01:00
public function alterTable ( $table , $newFields = null , $newIndexes = null , $alteredFields = null , $alteredIndexes = null , $alteredOptions = null , $advancedOptions = null )
{
2013-04-03 06:27:11 +02:00
$alterList = array ();
2015-12-17 19:18:01 +01:00
if ( $newFields ) {
foreach ( $newFields as $fieldName => $fieldSpec ) {
2013-04-03 06:27:11 +02:00
$alterList [] = " ADD \" $fieldName\ " $fieldSpec " ;
}
2015-12-17 19:18:01 +01:00
}
if ( $alteredFields ) {
foreach ( $alteredFields as $indexName => $indexSpec ) {
2013-04-03 06:27:11 +02:00
$val = $this -> alterTableAlterColumn ( $table , $indexName , $indexSpec );
2015-12-17 19:18:01 +01:00
if ( ! empty ( $val )) {
$alterList [] = $val ;
2013-04-03 06:27:11 +02:00
}
2015-12-17 19:18:01 +01:00
}
}
2013-04-03 06:27:11 +02:00
//Do we need to do anything with the tablespaces?
if ( $alteredOptions && isset ( $advancedOptions [ 'tablespace' ])){
$this -> createOrReplaceTablespace ( $advancedOptions [ 'tablespace' ][ 'name' ], $advancedOptions [ 'tablespace' ][ 'location' ]);
$this -> query ( " ALTER TABLE \" $table\ " SET TABLESPACE { $advancedOptions [ 'tablespace' ][ 'name' ]}; " );
}
//DB ABSTRACTION: we need to change the constraints to be a separate 'add' command,
//see http://www.postgresql.org/docs/8.1/static/sql-altertable.html
$alterIndexList = array ();
//Pick up the altered indexes here:
$fieldList = $this -> fieldList ( $table );
$fulltexts = false ;
$drop_triggers = false ;
$triggers = false ;
2015-12-17 19:18:01 +01:00
if ( $alteredIndexes ) {
foreach ( $alteredIndexes as $indexName => $indexSpec ) {
2013-04-03 06:27:11 +02:00
$indexSpec = $this -> parseIndexSpec ( $indexName , $indexSpec );
$indexNamePG = $this -> buildPostgresIndexName ( $table , $indexName );
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
if ( $indexSpec [ 'type' ] == 'fulltext' ) {
//For full text indexes, we need to drop the trigger, drop the index, AND drop the column
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
//Go and get the tsearch details:
$ts_details = $this -> fulltext ( $indexSpec , $table , $indexName );
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
//Drop this column if it already exists:
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
//No IF EXISTS option is available for Postgres <9.0
if ( array_key_exists ( $ts_details [ 'ts_name' ], $fieldList )){
$fulltexts .= " ALTER TABLE \" { $table } \" DROP COLUMN \" { $ts_details [ 'ts_name' ] } \" ; " ;
}
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
// We'll execute these later:
$triggerNamePG = $this -> buildPostgresTriggerName ( $table , $indexName );
$drop_triggers .= " DROP TRIGGER IF EXISTS \" $triggerNamePG\ " ON \ " $table\ " ; " ;
$fulltexts .= " ALTER TABLE \" { $table } \" ADD COLUMN { $ts_details [ 'fulltexts' ] } ; " ;
$triggers .= $ts_details [ 'triggers' ];
}
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
// Create index action (including fulltext)
$alterIndexList [] = " DROP INDEX IF EXISTS \" $indexNamePG\ " ; " ;
$createIndex = $this -> getIndexSqlDefinition ( $table , $indexName , $indexSpec );
2015-12-17 19:18:01 +01:00
if ( $createIndex !== false ) {
$alterIndexList [] = $createIndex ;
}
2013-04-03 06:27:11 +02:00
}
2015-12-17 19:18:01 +01:00
}
2013-04-03 06:27:11 +02:00
//Add the new indexes:
2015-12-17 19:18:01 +01:00
if ( $newIndexes ) {
foreach ( $newIndexes as $indexName => $indexSpec ) {
2013-04-03 06:27:11 +02:00
$indexSpec = $this -> parseIndexSpec ( $indexName , $indexSpec );
$indexNamePG = $this -> buildPostgresIndexName ( $table , $indexName );
//If we have a fulltext search request, then we need to create a special column
//for GiST searches
//Pick up the new indexes here:
if ( $indexSpec [ 'type' ] == 'fulltext' ) {
$ts_details = $this -> fulltext ( $indexSpec , $table , $indexName );
if ( ! isset ( $fieldList [ $ts_details [ 'ts_name' ]])){
$fulltexts .= " ALTER TABLE \" { $table } \" ADD COLUMN { $ts_details [ 'fulltexts' ] } ; " ;
$triggers .= $ts_details [ 'triggers' ];
}
}
//Check that this index doesn't already exist:
$indexes = $this -> indexList ( $table );
if ( isset ( $indexes [ $indexName ])){
$alterIndexList [] = " DROP INDEX IF EXISTS \" $indexNamePG\ " ; " ;
}
$createIndex = $this -> getIndexSqlDefinition ( $table , $indexName , $indexSpec );
2015-12-17 19:18:01 +01:00
if ( $createIndex !== false ) {
2013-04-03 06:27:11 +02:00
$alterIndexList [] = $createIndex ;
}
2015-12-17 19:18:01 +01:00
}
}
2013-04-03 06:27:11 +02:00
if ( $alterList ) {
$alterations = implode ( " , \n " , $alterList );
$this -> query ( " ALTER TABLE \" $table\ " " . $alterations );
}
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
//Do we need to create a tablespace for this item?
if ( $advancedOptions && isset ( $advancedOptions [ 'extensions' ][ 'tablespace' ])){
$extensions = $advancedOptions [ 'extensions' ];
$this -> createOrReplaceTablespace ( $extensions [ 'tablespace' ][ 'name' ], $extensions [ 'tablespace' ][ 'location' ]);
}
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
if ( $alteredOptions && isset ( $this -> class ) && isset ( $alteredOptions [ $this -> class ])) {
$this -> query ( sprintf ( " ALTER TABLE \" %s \" %s " , $table , $alteredOptions [ $this -> class ]));
Database :: alteration_message (
sprintf ( " Table %s options changed: %s " , $table , $alteredOptions [ $this -> class ]),
" changed "
);
}
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
//Create any fulltext columns and triggers here:
2015-12-17 19:18:01 +01:00
if ( $fulltexts ) {
$this -> query ( $fulltexts );
}
if ( $drop_triggers ) {
$this -> query ( $drop_triggers );
}
2013-04-03 06:27:11 +02:00
if ( $triggers ) {
$this -> query ( $triggers );
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
$triggerbits = explode ( ';' , $triggers );
foreach ( $triggerbits as $trigger ){
$trigger_fields = $this -> triggerFieldsFromTrigger ( $trigger );
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
if ( $trigger_fields ){
//We need to run a simple query to force the database to update the triggered columns
$this -> query ( " UPDATE \" { $table } \" SET \" { $trigger_fields [ 0 ] } \" = \" $trigger_fields[0] \" ; " );
}
}
}
2015-12-17 19:18:01 +01:00
foreach ( $alterIndexList as $alteration ) {
$this -> query ( $alteration );
}
2013-04-03 06:27:11 +02:00
//If we have a partitioning requirement, we do that here:
if ( $advancedOptions && isset ( $advancedOptions [ 'partitions' ])){
$this -> createOrReplacePartition ( $table , $advancedOptions [ 'partitions' ]);
}
//Lastly, clustering goes here:
if ( $advancedOptions && isset ( $advancedOptions [ 'cluster' ])) {
$clusterIndex = $this -> buildPostgresIndexName ( $table , $advancedOptions [ 'cluster' ]);
$this -> query ( " CLUSTER \" $table\ " USING \ " $clusterIndex\ " ; " );
} else {
//Check that clustering is not on this table, and if it is, remove it:
//This is really annoying. We need the oid of this table:
$stats = $this -> preparedQuery (
" SELECT relid FROM pg_stat_user_tables WHERE relname = ?; " ,
array ( $table )
) -> first ();
$oid = $stats [ 'relid' ];
//Now we can run a long query to get the clustered status:
//If anyone knows a better way to get the clustered status, then feel free to replace this!
$clustered = $this -> preparedQuery ( "
SELECT c2 . relname , i . indisclustered
FROM pg_catalog . pg_class c , pg_catalog . pg_class c2 , pg_catalog . pg_index i
WHERE c . oid = ? AND c . oid = i . indrelid AND i . indexrelid = c2 . oid AND indisclustered = 't' ; " ,
array ( $oid )
) -> first ();
if ( $clustered ) {
$this -> query ( " ALTER TABLE \" $table\ " SET WITHOUT CLUSTER ; " );
}
}
}
/*
* Creates an ALTER expression for a column in PostgreSQL
*
* @ param $tableName Name of the table to be altered
* @ param $colName Name of the column to be altered
* @ param $colSpec String which contains conditions for a column
* @ return string
*/
2015-12-17 19:18:01 +01:00
private function alterTableAlterColumn ( $tableName , $colName , $colSpec )
{
2013-04-03 06:27:11 +02:00
// First, we split the column specifications into parts
// TODO: this returns an empty array for the following string: int(11) not null auto_increment
// on second thoughts, why is an auto_increment field being passed through?
2015-12-17 19:18:01 +01:00
2015-11-02 06:37:20 +01:00
$pattern = '/^([\w(\,)]+)\s?((?:not\s)?null)?\s?(default\s[\w\.\']+)?\s?(check\s[\w()\'",\s]+)?$/i' ;
2013-04-03 06:27:11 +02:00
preg_match ( $pattern , $colSpec , $matches );
2015-12-17 19:18:01 +01:00
if ( sizeof ( $matches ) == 0 ) {
return '' ;
}
if ( $matches [ 1 ] == 'serial8' ) {
return '' ;
}
2013-04-03 06:27:11 +02:00
if ( isset ( $matches [ 1 ])) {
$alterCol = " ALTER COLUMN \" $colName\ " TYPE $matches [ 1 ] \n " ;
// SET null / not null
if ( ! empty ( $matches [ 2 ])) {
$alterCol .= " , \n ALTER COLUMN \" $colName\ " SET $matches [ 2 ] " ;
}
// SET default (we drop it first, for reasons of precaution)
if ( ! empty ( $matches [ 3 ])) {
$alterCol .= " , \n ALTER COLUMN \" $colName\ " DROP DEFAULT " ;
$alterCol .= " , \n ALTER COLUMN \" $colName\ " SET $matches [ 3 ] " ;
}
// SET check constraint (The constraint HAS to be dropped)
$existing_constraint = $this -> query ( " SELECT conname FROM pg_constraint WHERE conname=' { $tableName } _ { $colName } _check'; " ) -> value ();
if ( isset ( $matches [ 4 ])) {
//Take this new constraint and see what's outstanding from the target table:
$constraint_bits = explode ( '(' , $matches [ 4 ]);
$constraint_values = trim ( $constraint_bits [ 2 ], ')' );
$constraint_values_bits = explode ( ',' , $constraint_values );
$default = trim ( $constraint_values_bits [ 0 ], " ' " );
//Now go and convert anything that's not in this list to 'Page'
//We have to run this as a query, not as part of the alteration queries due to the way they are constructed.
$updateConstraint = '' ;
$updateConstraint .= " UPDATE \" { $tableName } \" SET \" $colName\ " = '$default' WHERE \ " $colName\ " NOT IN ( $constraint_values ); " ;
if ( $this -> hasTable ( " { $tableName } _Live " )) {
$updateConstraint .= " UPDATE \" { $tableName } _Live \" SET \" $colName\ " = '$default' WHERE \ " $colName\ " NOT IN ( $constraint_values ); " ;
}
if ( $this -> hasTable ( " { $tableName } _versions " )) {
$updateConstraint .= " UPDATE \" { $tableName } _versions \" SET \" $colName\ " = '$default' WHERE \ " $colName\ " NOT IN ( $constraint_values ); " ;
}
$this -> query ( $updateConstraint );
}
//First, delete any existing constraint on this column, even if it's no longer an enum
if ( $existing_constraint ) {
$alterCol .= " , \n DROP CONSTRAINT \" { $tableName } _ { $colName } _check \" " ;
}
//Now create the constraint (if we've asked for one)
if ( ! empty ( $matches [ 4 ])) {
$alterCol .= " , \n ADD CONSTRAINT \" { $tableName } _ { $colName } _check \" $matches[4] " ;
}
}
return isset ( $alterCol ) ? $alterCol : '' ;
}
2015-12-17 19:18:01 +01:00
public function renameTable ( $oldTableName , $newTableName )
{
2013-04-03 06:27:11 +02:00
$this -> query ( " ALTER TABLE \" $oldTableName\ " RENAME TO \ " $newTableName\ " " );
unset ( self :: $cached_fieldlists [ $oldTableName ]);
}
2015-12-17 19:18:01 +01:00
public function checkAndRepairTable ( $tableName )
{
2013-04-03 06:27:11 +02:00
$this -> query ( " VACUUM FULL ANALYZE \" $tableName\ " " );
$this -> query ( " REINDEX TABLE \" $tableName\ " " );
return true ;
}
2015-12-17 19:18:01 +01:00
public function createField ( $table , $field , $spec )
{
2013-04-03 06:27:11 +02:00
$this -> query ( " ALTER TABLE \" $table\ " ADD \ " $field\ " $spec " );
}
/**
* Change the database type of the given field .
*
* @ param string $tableName The name of the tbale the field is in .
* @ param string $fieldName The name of the field to change .
* @ param string $fieldSpec The new field specification
*/
2015-12-17 19:18:01 +01:00
public function alterField ( $tableName , $fieldName , $fieldSpec )
{
2013-04-03 06:27:11 +02:00
$this -> query ( " ALTER TABLE \" $tableName\ " CHANGE \ " $fieldName\ " \ " $fieldName\ " $fieldSpec " );
}
2015-12-17 19:18:01 +01:00
public function renameField ( $tableName , $oldName , $newName )
{
2013-04-03 06:27:11 +02:00
$fieldList = $this -> fieldList ( $tableName );
if ( array_key_exists ( $oldName , $fieldList )) {
$this -> query ( " ALTER TABLE \" $tableName\ " RENAME COLUMN \ " $oldName\ " TO \ " $newName\ " " );
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
//Remove this from the cached list:
unset ( self :: $cached_fieldlists [ $tableName ]);
}
}
2015-12-17 19:18:01 +01:00
public function fieldList ( $table )
{
2013-04-03 06:27:11 +02:00
//Query from http://www.alberton.info/postgresql_meta_info.html
//This gets us more information than we need, but I've included it all for the moment....
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
//if(!isset(self::$cached_fieldlists[$table])){
$fields = $this -> preparedQuery ( "
2014-07-14 23:32:26 +02:00
SELECT ordinal_position , column_name , data_type , column_default ,
2013-04-03 06:27:11 +02:00
is_nullable , character_maximum_length , numeric_precision , numeric_scale
2014-07-14 23:32:26 +02:00
FROM information_schema . columns WHERE table_name = ? and table_schema = ?
2013-04-03 06:27:11 +02:00
ORDER BY ordinal_position ; " ,
2014-07-14 23:32:26 +02:00
array ( $table , $this -> database -> currentSchema ())
2013-04-03 06:27:11 +02:00
);
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
$output = array ();
2015-12-17 19:18:01 +01:00
if ( $fields ) {
foreach ( $fields as $field ) {
2013-04-03 06:27:11 +02:00
switch ( $field [ 'data_type' ]){
case 'character varying' :
//Check to see if there's a constraint attached to this column:
//$constraint=$this->query("SELECT conname,pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE r.contype = 'c' AND conname='" . $table . '_' . $field['column_name'] . "_check' ORDER BY 1;")->first();
$constraint = $this -> constraintExists ( $table . '_' . $field [ 'column_name' ] . '_check' );
if ( $constraint ){
//Now we need to break this constraint text into bits so we can see what we have:
//Examples:
//CHECK ("CanEditType"::text = ANY (ARRAY['LoggedInUsers'::character varying, 'OnlyTheseUsers'::character varying, 'Inherit'::character varying]::text[]))
//CHECK ("ClassName"::text = 'PageComment'::text)
//TODO: replace all this with a regular expression!
$value = $constraint [ 'pg_get_constraintdef' ];
$value = substr ( $value , strpos ( $value , '=' ));
$value = str_replace ( " '' " , " ' " , $value );
$in_value = false ;
2015-12-17 19:18:01 +01:00
$constraints = array ();
2013-04-03 06:27:11 +02:00
$current_value = '' ;
for ( $i = 0 ; $i < strlen ( $value ); $i ++ ){
$char = substr ( $value , $i , 1 );
2015-12-17 19:18:01 +01:00
if ( $in_value ) {
2013-04-03 06:27:11 +02:00
$current_value .= $char ;
2015-12-17 19:18:01 +01:00
}
2013-04-03 06:27:11 +02:00
if ( $char == " ' " ){
2015-12-17 19:18:01 +01:00
if ( ! $in_value ) {
2013-04-03 06:27:11 +02:00
$in_value = true ;
2015-12-17 19:18:01 +01:00
} else {
2013-04-03 06:27:11 +02:00
$in_value = false ;
$constraints [] = substr ( $current_value , 0 , - 1 );
$current_value = '' ;
}
}
}
if ( sizeof ( $constraints ) > 0 ){
//Get the default:
$default = trim ( substr ( $field [ 'column_default' ], 0 , strpos ( $field [ 'column_default' ], '::' )), " ' " );
2015-12-17 19:18:01 +01:00
$output [ $field [ 'column_name' ]] = $this -> enum ( array ( 'default' => $default , 'name' => $field [ 'column_name' ], 'enums' => $constraints ));
2013-04-03 06:27:11 +02:00
}
} else {
$output [ $field [ 'column_name' ]] = 'varchar(' . $field [ 'character_maximum_length' ] . ')' ;
}
break ;
case 'numeric' :
2015-11-02 06:37:20 +01:00
$output [ $field [ 'column_name' ]] = 'decimal(' . $field [ 'numeric_precision' ] . ',' . $field [ 'numeric_scale' ] . ') default ' . floatval ( $field [ 'column_default' ]);
2013-04-03 06:27:11 +02:00
break ;
case 'integer' :
$output [ $field [ 'column_name' ]] = 'integer default ' . ( int ) $field [ 'column_default' ];
break ;
case 'timestamp without time zone' :
$output [ $field [ 'column_name' ]] = 'timestamp' ;
break ;
case 'smallint' :
$output [ $field [ 'column_name' ]] = 'smallint default ' . ( int ) $field [ 'column_default' ];
break ;
case 'time without time zone' :
$output [ $field [ 'column_name' ]] = 'time' ;
break ;
case 'double precision' :
$output [ $field [ 'column_name' ]] = 'float' ;
break ;
default :
$output [ $field [ 'column_name' ]] = $field ;
}
2015-12-17 19:18:01 +01:00
}
2013-04-03 06:27:11 +02:00
}
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
// self::$cached_fieldlists[$table]=$output;
//}
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
//return self::$cached_fieldlists[$table];
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
return $output ;
}
2015-12-17 19:18:01 +01:00
public function clearCachedFieldlist ( $tableName = false )
{
if ( $tableName ) {
unset ( self :: $cached_fieldlists [ $tableName ]);
} else {
self :: $cached_fieldlists = array ();
}
2013-04-03 06:27:11 +02:00
return true ;
}
/**
* Create an index on a table .
*
* @ param string $tableName The name of the table .
* @ param string $indexName The name of the index .
* @ param string $indexSpec The specification of the index , see Database :: requireIndex () for more details .
*/
2015-12-17 19:18:01 +01:00
public function createIndex ( $tableName , $indexName , $indexSpec )
{
2013-04-03 06:27:11 +02:00
$createIndex = $this -> getIndexSqlDefinition ( $tableName , $indexName , $indexSpec );
2015-12-17 19:18:01 +01:00
if ( $createIndex !== false ) {
$this -> query ( $createIndex );
}
2013-04-03 06:27:11 +02:00
}
/*
* @ 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 ;
} */
2015-12-17 19:18:01 +01:00
protected function getIndexSqlDefinition ( $tableName , $indexName , $indexSpec , $asDbValue = false )
{
2013-04-03 06:27:11 +02:00
//TODO: create table partition support
//TODO: create clustering options
//NOTE: it is possible for *_renamed tables to have indexes whose names are not updates
//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.
// If requesting the definition rather than the DDL
if ( $asDbValue ) {
$indexName = trim ( $indexName , '()' );
return $indexName ;
}
// Determine index name
$tableCol = $this -> buildPostgresIndexName ( $tableName , $indexName );
// Consolidate/Cleanup spec into array format
$indexSpec = $this -> parseIndexSpec ( $indexName , $indexSpec );
//Misc options first:
$fillfactor = $where = '' ;
if ( isset ( $indexSpec [ 'fillfactor' ])) {
$fillfactor = 'WITH (FILLFACTOR = ' . $indexSpec [ 'fillfactor' ] . ')' ;
}
if ( isset ( $indexSpec [ 'where' ])) {
$where = 'WHERE ' . $indexSpec [ 'where' ];
}
//create a type-specific index
// NOTE: hash should be removed. This is only here to demonstrate how other indexes can be made
// NOTE: Quote the index name to preserve case sensitivity
switch ( $indexSpec [ 'type' ]) {
case 'fulltext' :
// @see fulltext() for the definition of the trigger that ts_$IndexName uses for fulltext searching
$clusterMethod = PostgreSQLDatabase :: default_fts_cluster_method ();
$spec = " create index \" $tableCol\ " ON \ " $tableName\ " USING $clusterMethod ( \ " ts_ " . $indexName . " \" ) $fillfactor $where " ;
break ;
case 'unique' :
$spec = " create unique index \" $tableCol\ " ON \ " $tableName\ " ( " . $indexSpec['value'] . " ) $fillfactor $where " ;
break ;
case 'btree' :
$spec = " create index \" $tableCol\ " ON \ " $tableName\ " USING btree ( " . $indexSpec['value'] . " ) $fillfactor $where " ;
break ;
case 'hash' :
//NOTE: this is not a recommended index type
$spec = " create index \" $tableCol\ " ON \ " $tableName\ " USING hash ( " . $indexSpec['value'] . " ) $fillfactor $where " ;
break ;
case 'index' :
//'index' is the same as default, just a normal index with the default type decided by the database.
default :
$spec = " create index \" $tableCol\ " ON \ " $tableName\ " ( " . $indexSpec['value'] . " ) $fillfactor $where " ;
}
return trim ( $spec ) . ';' ;
}
2015-12-17 19:18:01 +01:00
public function alterIndex ( $tableName , $indexName , $indexSpec )
{
2013-04-03 06:27:11 +02:00
$indexSpec = trim ( $indexSpec );
if ( $indexSpec [ 0 ] != '(' ) {
list ( $indexType , $indexFields ) = explode ( ' ' , $indexSpec , 2 );
} else {
$indexFields = $indexSpec ;
}
if ( ! $indexType ) {
$indexType = " index " ;
}
$this -> query ( " DROP INDEX \" $indexName\ " " );
$this -> query ( " ALTER TABLE \" $tableName\ " ADD $indexType \ " $indexName\ " $indexFields " );
}
/**
* Given a trigger name attempt to determine the columns upon which it acts
2014-07-14 23:32:26 +02:00
*
2013-04-03 06:27:11 +02:00
* @ param string $triggerName Postgres trigger name
* @ return array List of columns
*/
2015-12-17 19:18:01 +01:00
protected function extractTriggerColumns ( $triggerName )
{
2013-04-03 06:27:11 +02:00
$trigger = $this -> preparedQuery (
" SELECT tgargs FROM pg_catalog.pg_trigger WHERE tgname = ? " ,
array ( $triggerName )
) -> first ();
// Option 1: output as a string
if ( strpos ( $trigger [ 'tgargs' ], '\000' ) !== false ) {
$argList = explode ( '\000' , $trigger [ 'tgargs' ]);
array_pop ( $argList );
// Option 2: hex-encoded (not sure why this happens, depends on PGSQL config)
} else {
$bytes = str_split ( $trigger [ 'tgargs' ], 2 );
$argList = array ();
$nextArg = " " ;
foreach ( $bytes as $byte ) {
if ( $byte == " 00 " ) {
$argList [] = $nextArg ;
$nextArg = " " ;
} else {
$nextArg .= chr ( hexdec ( $byte ));
}
}
}
// Drop first two arguments (trigger name and config name) and implode into nice list
return array_slice ( $argList , 2 );
}
2015-12-17 19:18:01 +01:00
public function indexList ( $table )
{
2013-04-03 06:27:11 +02:00
//Retrieve a list of indexes for the specified table
$indexes = $this -> preparedQuery ( "
SELECT tablename , indexname , indexdef
FROM pg_catalog . pg_indexes
WHERE tablename = ? AND schemaname = ? ; " ,
array ( $table , $this -> database -> currentSchema ())
);
$indexList = array ();
foreach ( $indexes as $index ) {
// Key for the indexList array. Differs from other DB implementations, which is why
// requireIndex() needed to be overridden
$indexName = $index [ 'indexname' ];
//We don't actually need the entire created command, just a few bits:
$type = '' ;
//Check for uniques:
if ( substr ( $index [ 'indexdef' ], 0 , 13 ) == 'CREATE UNIQUE' ) {
$type = 'unique' ;
}
//check for hashes, btrees etc:
if ( strpos ( strtolower ( $index [ 'indexdef' ]), 'using hash ' ) !== false ) {
$type = 'hash' ;
}
//TODO: Fix me: btree is the default index type:
//if(strpos(strtolower($index['indexdef']), 'using btree ')!==false)
// $prefix='using btree ';
if ( strpos ( strtolower ( $index [ 'indexdef' ]), 'using rtree ' ) !== false ) {
$type = 'rtree' ;
}
// For fulltext indexes we need to extract the columns from another source
if ( stristr ( $index [ 'indexdef' ], 'using gin' )) {
$type = 'fulltext' ;
// Extract trigger information from postgres
$triggerName = preg_replace ( '/^ix_/' , 'ts_' , $index [ 'indexname' ]);
$columns = $this -> extractTriggerColumns ( $triggerName );
$columnString = $this -> implodeColumnList ( $columns );
} else {
$columnString = $this -> quoteColumnSpecString ( $index [ 'indexdef' ]);
}
$indexList [ $indexName ] = $this -> parseIndexSpec ( $index , array (
'name' => $indexName , // Not the correct name in the PHP, as this will be a mangled postgres-unique code
'value' => $columnString ,
'type' => $type
));
}
return $indexList ;
}
2015-12-17 19:18:01 +01:00
public function tableList ()
{
2013-04-03 06:27:11 +02:00
$tables = array ();
$result = $this -> preparedQuery (
" SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = ? AND tablename NOT ILIKE 'pg \\ \ _%' AND tablename NOT ILIKE 'sql \\ \ _%' " ,
array ( $this -> database -> currentSchema ())
);
foreach ( $result as $record ) {
$table = reset ( $record );
$tables [ strtolower ( $table )] = $table ;
}
return $tables ;
}
/**
* Find out what the constraint information is , given a constraint name .
* We also cache this result , so the next time we don ' t need to do a
* query all over again .
*
* @ param string $constraint
*/
2015-12-17 19:18:01 +01:00
protected function constraintExists ( $constraint )
{
2013-04-03 06:27:11 +02:00
if ( ! isset ( self :: $cached_constraints [ $constraint ])){
$exists = $this -> preparedQuery ( "
SELECT conname , pg_catalog . pg_get_constraintdef ( r . oid , true )
FROM pg_catalog . pg_constraint r WHERE r . contype = 'c' AND conname = ? ORDER BY 1 ; " ,
array ( $constraint )
) -> first ();
self :: $cached_constraints [ $constraint ] = $exists ;
}
return self :: $cached_constraints [ $constraint ];
}
/**
* A function to return the field names and datatypes for the particular table
*
* @ param string $tableName
* @ return array List of columns an an associative array with the keys Column and DataType
*/
2015-12-17 19:18:01 +01:00
public function tableDetails ( $tableName )
{
2013-04-03 06:27:11 +02:00
$query = " SELECT a.attname as \" Column \" , pg_catalog.format_type(a.atttypid, a.atttypmod) as \" Datatype \"
FROM pg_catalog . pg_attribute a
WHERE a . attnum > 0 AND NOT a . attisdropped AND a . attrelid = (
SELECT c . oid
FROM pg_catalog . pg_class c
LEFT JOIN pg_catalog . pg_namespace n ON n . oid = c . relnamespace
WHERE c . relname = ? AND pg_catalog . pg_table_is_visible ( c . oid ) AND n . nspname = ?
); " ;
$result = $this -> preparedQuery ( $query , $tableName , $this -> database -> currentSchema ());
$table = array ();
while ( $row = pg_fetch_assoc ( $result )) {
$table [] = array (
'Column' => $row [ 'Column' ],
'DataType' => $row [ 'DataType' ]
);
}
return $table ;
}
/**
* Pass a legit trigger name and it will be dropped
* This assumes that the trigger has been named in a unique fashion
2014-07-14 23:32:26 +02:00
*
2013-04-03 06:27:11 +02:00
* @ param string $triggerName Name of the trigger
* @ param string $tableName Name of the table
*/
2015-12-17 19:18:01 +01:00
protected function dropTrigger ( $triggerName , $tableName )
{
2013-04-03 06:27:11 +02:00
$exists = $this -> preparedQuery ( "
SELECT trigger_name
FROM information_schema . triggers
WHERE trigger_name = ? AND trigger_schema = ? ; " ,
array ( $triggerName , $this -> database -> currentSchema ())
) -> first ();
if ( $exists ){
$this -> query ( " DROP trigger IF EXISTS $triggerName ON \" $tableName\ " ; " );
}
}
/**
* This will return the fields that the trigger is monitoring
2014-07-14 23:32:26 +02:00
*
2013-04-03 06:27:11 +02:00
* @ param string $trigger Name of the trigger
* @ return array
*/
2015-12-17 19:18:01 +01:00
protected function triggerFieldsFromTrigger ( $trigger )
{
2013-04-03 06:27:11 +02:00
if ( $trigger ){
$tsvector = 'tsvector_update_trigger' ;
$ts_pos = strpos ( $trigger , $tsvector );
$details = trim ( substr ( $trigger , $ts_pos + strlen ( $tsvector )), '();' );
//Now split this into bits:
$bits = explode ( ',' , $details );
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
$fields = $bits [ 2 ];
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
$field_bits = explode ( ',' , str_replace ( '"' , '' , $fields ));
$result = array ();
2015-12-17 19:18:01 +01:00
foreach ( $field_bits as $field_bit ) {
2013-04-03 06:27:11 +02:00
$result [] = trim ( $field_bit );
2015-12-17 19:18:01 +01:00
}
2013-04-03 06:27:11 +02:00
return $result ;
} else {
return false ;
}
}
/**
* Return a boolean type - formatted string
*
* @ param array $values Contains a tokenised list of info about this data type
* @ param boolean $asDbValue
* @ return string
*/
2015-12-17 19:18:01 +01:00
public function boolean ( $values , $asDbValue = false )
{
2013-04-03 06:27:11 +02:00
//Annoyingly, we need to do a good ol' fashioned switch here:
$default = $values [ 'default' ] ? '1' : '0' ;
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 ;
}
/**
* Return a date type - formatted string
*
* @ param array $values Contains a tokenised list of info about this data type
* @ return string
*/
2015-12-17 19:18:01 +01:00
public function date ( $values )
{
2013-04-03 06:27:11 +02:00
if ( ! isset ( $values [ 'arrayValue' ])) {
$values [ 'arrayValue' ] = '' ;
}
return " date { $values [ 'arrayValue' ] } " ;
}
/**
* Return a decimal type - formatted string
*
* @ param array $values Contains a tokenised list of info about this data type
* @ param boolean $asDbValue
* @ return string
*/
2015-12-17 19:18:01 +01:00
public function decimal ( $values , $asDbValue = false )
{
2013-04-03 06:27:11 +02:00
if ( ! isset ( $values [ 'arrayValue' ])) {
$values [ 'arrayValue' ] = '' ;
}
// Avoid empty strings being put in the db
if ( $values [ 'precision' ] == '' ) {
$precision = 1 ;
} else {
$precision = $values [ 'precision' ];
}
$defaultValue = '' ;
if ( isset ( $values [ 'default' ]) && is_numeric ( $values [ 'default' ])) {
2015-11-02 06:37:20 +01:00
$defaultValue = ' default ' . floatval ( $values [ 'default' ]);
2013-04-03 06:27:11 +02:00
}
if ( $asDbValue ) {
return array ( 'data_type' => 'numeric' , 'precision' => $precision );
} else {
return " decimal( $precision ) { $values [ 'arrayValue' ] } $defaultValue " ;
}
}
/**
* Return a enum type - formatted string
*
* @ param array $values Contains a tokenised list of info about this data type
* @ return string
*/
2015-12-17 19:18:01 +01:00
public function enum ( $values )
{
2013-04-03 06:27:11 +02:00
//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' ] } ' " ;
}
return " varchar(255) { $values [ 'arrayValue' ] } " . $default . " check ( \" " . $values [ 'name' ] . " \" in (' " . implode ( '\', \'' , $values [ 'enums' ]) . " ')) " ;
}
/**
* Return a float type - formatted string
*
* @ param array $values Contains a tokenised list of info about this data type
* @ param boolean $asDbValue
* @ return string
*/
2015-12-17 19:18:01 +01:00
public function float ( $values , $asDbValue = false )
{
2013-04-03 06:27:11 +02:00
if ( ! isset ( $values [ 'arrayValue' ])) {
$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
*
* @ param array $values Contains a tokenised list of info about this data type
* @ param boolean $asDbValue
* @ return string
*/
2015-12-17 19:18:01 +01:00
public function double ( $values , $asDbValue = false )
{
2013-04-03 06:27:11 +02:00
return $this -> float ( $values , $asDbValue );
}
/**
* Return a int type - formatted string
*
* @ param array $values Contains a tokenised list of info about this data type
* @ param boolean $asDbValue
* @ return string
*/
2015-12-17 19:18:01 +01:00
public function int ( $values , $asDbValue = false )
{
2013-04-03 06:27:11 +02:00
if ( ! isset ( $values [ 'arrayValue' ])) {
$values [ 'arrayValue' ] = '' ;
}
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
if ( $asDbValue ) {
2015-12-17 19:18:01 +01:00
return array ( 'data_type' => 'integer' , 'precision' => '32' );
2013-04-03 06:27:11 +02:00
}
if ( $values [ 'arrayValue' ] != '' ) {
$default = '' ;
} else {
$default = ' default ' . ( int ) $values [ 'default' ];
}
return " integer { $values [ 'arrayValue' ] } " . $default ;
}
2014-07-15 09:44:01 +02:00
/**
* Return a bigint type - formatted string
*
* @ param array $values Contains a tokenised list of info about this data type
* @ param boolean $asDbValue
* @ return string
*/
2015-12-17 19:18:01 +01:00
public function bigint ( $values , $asDbValue = false )
{
2014-07-15 09:44:01 +02:00
if ( ! isset ( $values [ 'arrayValue' ])) {
$values [ 'arrayValue' ] = '' ;
}
2015-12-17 19:18:01 +01:00
2014-07-15 09:44:01 +02:00
if ( $asDbValue ) {
2015-12-17 19:18:01 +01:00
return array ( 'data_type' => 'bigint' , 'precision' => '64' );
2014-07-15 09:44:01 +02:00
}
if ( $values [ 'arrayValue' ] != '' ) {
$default = '' ;
} else {
$default = ' default ' . ( int ) $values [ 'default' ];
}
return " bigint { $values [ 'arrayValue' ] } " . $default ;
}
2013-04-03 06:27:11 +02:00
/**
* Return a datetime type - formatted string
* 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 boolean $asDbValue
* @ return string
*/
2015-12-17 19:18:01 +01:00
public function SS_Datetime ( $values , $asDbValue = false )
{
2013-04-03 06:27:11 +02:00
if ( ! isset ( $values [ 'arrayValue' ])) {
$values [ 'arrayValue' ] = '' ;
}
if ( $asDbValue ) {
return array ( 'data_type' => 'timestamp without time zone' );
} else {
return " timestamp { $values [ 'arrayValue' ] } " ;
}
}
/**
* Return a text type - formatted string
*
* @ param array $values Contains a tokenised list of info about this data type
* @ param boolean $asDbValue
* @ return string
*/
2015-12-17 19:18:01 +01:00
public function text ( $values , $asDbValue = false )
{
2013-04-03 06:27:11 +02:00
if ( ! isset ( $values [ 'arrayValue' ])) {
$values [ 'arrayValue' ] = '' ;
}
if ( $asDbValue ) {
return array ( 'data_type' => 'text' );
} else {
return " text { $values [ 'arrayValue' ] } " ;
}
}
/**
* Return a time type - formatted string
*
* @ param array $values Contains a tokenised list of info about this data type
* @ return string
*/
2015-12-17 19:18:01 +01:00
public function time ( $values )
{
2013-04-03 06:27:11 +02:00
if ( ! isset ( $values [ 'arrayValue' ])) {
$values [ 'arrayValue' ] = '' ;
}
return " time { $values [ 'arrayValue' ] } " ;
}
/**
* Return a varchar type - formatted string
*
* @ param array $values Contains a tokenised list of info about this data type
* @ param boolean $asDbValue
* @ return string
*/
2015-12-17 19:18:01 +01:00
public function varchar ( $values , $asDbValue = false )
{
2013-04-03 06:27:11 +02:00
if ( ! isset ( $values [ 'arrayValue' ])) {
$values [ 'arrayValue' ] = '' ;
}
if ( ! isset ( $values [ 'precision' ])) {
$values [ 'precision' ] = 255 ;
}
if ( $asDbValue ) {
return array ( 'data_type' => 'varchar' , 'precision' => $values [ 'precision' ]);
} else {
return " varchar( { $values [ 'precision' ] } ) { $values [ 'arrayValue' ] } " ;
}
}
/*
* Return a 4 digit numeric type . MySQL has a proprietary 'Year' type .
* For Postgres , we ' ll use a 4 digit numeric
*
* @ param array $values Contains a tokenised list of info about this data type
* @ param boolean $asDbValue
* @ return string
*/
2015-12-17 19:18:01 +01:00
public function year ( $values , $asDbValue = false )
{
2013-04-03 06:27:11 +02:00
if ( ! isset ( $values [ 'arrayValue' ])) {
$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' ] } " ;
}
}
/**
* Create a fulltext search datatype for PostgreSQL
* This will also return a trigger to be applied to this table
*
* @ todo : create custom functions to allow weighted searches
*
* @ param array $this_index Index specification for the fulltext index
* @ param string $tableName
* @ param string $name
* @ param array $spec
*/
2015-12-17 19:18:01 +01:00
protected function fulltext ( $this_index , $tableName , $name )
{
2013-04-03 06:27:11 +02:00
//For full text search, we need to create a column for the index
$columns = $this -> quoteColumnSpecString ( $this_index [ 'value' ]);
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
$fulltexts = " \" ts_ $name\ " tsvector " ;
$triggerName = $this -> buildPostgresTriggerName ( $tableName , $name );
$language = PostgreSQLDatabase :: search_language ();
2015-12-17 19:18:01 +01:00
2013-04-03 06:27:11 +02:00
$this -> dropTrigger ( $triggerName , $tableName );
$triggers = " CREATE TRIGGER \" $triggerName\ " BEFORE INSERT OR UPDATE
ON \ " $tableName\ " FOR EACH ROW EXECUTE PROCEDURE
tsvector_update_trigger ( \ " ts_ $name\ " , 'pg_catalog.$language' , $columns ); " ;
return array (
'name' => $name ,
'ts_name' => " ts_ { $name } " ,
'fulltexts' => $fulltexts ,
'triggers' => $triggers
);
}
2015-12-17 19:18:01 +01:00
public function IdColumn ( $asDbValue = false , $hasAutoIncPK = true )
{
if ( $asDbValue ) {
return 'bigint' ;
} else {
return 'serial8 not null' ;
}
2013-04-03 06:27:11 +02:00
}
2015-12-17 19:18:01 +01:00
public function hasTable ( $tableName )
{
2013-04-03 06:27:11 +02:00
$result = $this -> preparedQuery (
" SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = ? AND tablename = ?; " ,
array ( $this -> database -> currentSchema (), $tableName )
);
return ( $result -> numRecords () > 0 );
}
/**
* Returns the values of the given enum field
2014-07-14 23:32:26 +02:00
*
2013-04-03 06:27:11 +02:00
* @ todo Make a proper implementation
2014-07-14 23:32:26 +02:00
*
2013-04-03 06:27:11 +02:00
* @ param string $tableName Name of table to check
* @ param string $fieldName name of enum field to check
* @ return array List of enum values
*/
2015-12-17 19:18:01 +01:00
public function enumValuesForField ( $tableName , $fieldName )
{
2013-04-03 06:27:11 +02:00
//return array('SiteTree','Page');
$constraints = $this -> constraintExists ( " { $tableName } _ { $fieldName } _check " );
if ( $constraints ) {
return $this -> enumValuesFromConstraint ( $constraints [ 'pg_get_constraintdef' ]);
} else {
return array ();
}
}
2014-07-14 23:32:26 +02:00
/**
* Get the actual enum fields from the constraint value :
*
* @ param string $constraint
* @ return array
*/
2015-12-17 19:18:01 +01:00
protected function enumValuesFromConstraint ( $constraint )
{
2014-07-14 23:32:26 +02:00
$constraint = substr ( $constraint , strpos ( $constraint , 'ANY (ARRAY[' ) + 11 );
$constraint = substr ( $constraint , 0 , - 11 );
$constraints = array ();
$segments = explode ( ',' , $constraint );
foreach ( $segments as $this_segment ){
$bits = preg_split ( '/ *:: */' , $this_segment );
array_unshift ( $constraints , trim ( $bits [ 0 ], " ' " ));
}
return $constraints ;
}
2015-12-17 19:18:01 +01:00
public function dbDataType ( $type )
{
2013-04-03 06:27:11 +02:00
$values = array (
'unsigned integer' => 'INT'
);
2015-12-17 19:18:01 +01:00
if ( isset ( $values [ $type ])) {
return $values [ $type ];
} else {
return '' ;
}
2013-04-03 06:27:11 +02:00
}
/*
* Given a tablespace and and location , either create a new one
* or update the existing one
*
* @ param string $name
* @ param string $location
*/
2015-12-17 19:18:01 +01:00
public function createOrReplaceTablespace ( $name , $location )
{
2013-04-03 06:27:11 +02:00
$existing = $this -> preparedQuery (
" SELECT spcname, spclocation FROM pg_tablespace WHERE spcname = ?; " ,
array ( $name )
) -> first ();
//NOTE: this location must be empty for this to work
//We can't seem to change the location of the tablespace through any ALTER commands :(
//If a tablespace with this name exists, but the location has changed, then drop the current one
//if($existing && $location!=$existing['spclocation'])
// DB::query("DROP TABLESPACE $name;");
//If this is a new tablespace, or we have dropped the current one:
if ( ! $existing || ( $existing && $location != $existing [ 'spclocation' ])) {
$this -> query ( " CREATE TABLESPACE $name LOCATION ' $location '; " );
}
}
/**
*
* @ param string $tableName
* @ param array $partitions
* @ param array $indexes
* @ param array $extensions
*/
2015-12-17 19:18:01 +01:00
public function createOrReplacePartition ( $tableName , $partitions , $indexes , $extensions )
{
2013-04-03 06:27:11 +02:00
//We need the plpgsql language to be installed for this to work:
$this -> createLanguage ( 'plpgsql' );
$trigger = 'CREATE OR REPLACE FUNCTION ' . $tableName . '_insert_trigger() RETURNS TRIGGER AS $$ BEGIN ' ;
$first = true ;
//Do we need to create a tablespace for this item?
if ( $extensions && isset ( $extensions [ 'tablespace' ])){
$this -> createOrReplaceTablespace ( $extensions [ 'tablespace' ][ 'name' ], $extensions [ 'tablespace' ][ 'location' ]);
$tableSpace = ' TABLESPACE ' . $extensions [ 'tablespace' ][ 'name' ];
} else {
$tableSpace = '' ;
}
foreach ( $partitions as $partition_name => $partition_value ){
//Check that this child table does not already exist:
if ( ! $this -> hasTable ( $partition_name )){
$this -> query ( " CREATE TABLE \" $partition_name\ " ( CHECK ( " . str_replace('NEW.', '', $partition_value ) . " )) INHERITS ( \ " $tableName\ " ) $tableSpace ; " );
} else {
//Drop the constraint, we will recreate in in the next line
$existing_constraint = $this -> preparedQuery (
" SELECT conname FROM pg_constraint WHERE conname = ?; " ,
array ( " { $partition_name } _pkey " )
);
if ( $existing_constraint ){
$this -> query ( " ALTER TABLE \" $partition_name\ " DROP CONSTRAINT \ " { $partition_name } _pkey \" ; " );
}
$this -> dropTrigger ( strtolower ( 'trigger_' . $tableName . '_insert' ), $tableName );
}
$this -> query ( " ALTER TABLE \" $partition_name\ " ADD CONSTRAINT \ " { $partition_name } _pkey \" PRIMARY KEY ( \" ID \" ); " );
if ( $first ){
$trigger .= 'IF' ;
$first = false ;
} else {
$trigger .= 'ELSIF' ;
}
$trigger .= " ( $partition_value ) THEN INSERT INTO \" $partition_name\ " VALUES ( NEW .* ); " ;
if ( $indexes ){
// We need to propogate the indexes through to the child pages.
// Some of this code is duplicated, and could be tidied up
foreach ( $indexes as $name => $this_index ){
if ( $this_index [ 'type' ] == 'fulltext' ){
$fillfactor = $where = '' ;
if ( isset ( $this_index [ 'fillfactor' ])) {
$fillfactor = 'WITH (FILLFACTOR = ' . $this_index [ 'fillfactor' ] . ')' ;
}
if ( isset ( $this_index [ 'where' ])) {
$where = 'WHERE ' . $this_index [ 'where' ];
}
$clusterMethod = PostgreSQLDatabase :: default_fts_cluster_method ();
$this -> query ( " CREATE INDEX \" " . $this -> buildPostgresIndexName ( $partition_name , $this_index [ 'name' ]) . " \" ON \" " . $partition_name . " \" USING $clusterMethod ( \" ts_ " . $name . " \" ) $fillfactor $where " );
$ts_details = $this -> fulltext ( $this_index , $partition_name , $name );
$this -> query ( $ts_details [ 'triggers' ]);
} else {
if ( is_array ( $this_index )) {
$index_name = $this_index [ 'name' ];
} else {
$index_name = trim ( $this_index , '()' );
}
$createIndex = $this -> getIndexSqlDefinition ( $partition_name , $index_name , $this_index );
if ( $createIndex !== false ) {
$this -> query ( $createIndex );
}
}
}
}
//Lastly, clustering goes here:
if ( $extensions && isset ( $extensions [ 'cluster' ])){
$this -> query ( " CLUSTER \" $partition_name\ " USING \ " { $extensions [ 'cluster' ] } \" ; " );
}
}
$trigger .= 'ELSE RAISE EXCEPTION \'Value id out of range. Fix the ' . $tableName . '_insert_trigger() function!\'; END IF; RETURN NULL; END; $$ LANGUAGE plpgsql;' ;
$trigger .= 'CREATE TRIGGER trigger_' . $tableName . '_insert BEFORE INSERT ON "' . $tableName . '" FOR EACH ROW EXECUTE PROCEDURE ' . $tableName . '_insert_trigger();' ;
$this -> query ( $trigger );
}
/*
* This will create a language if it doesn ' t already exist .
* This is used by the createOrReplacePartition function , which needs plpgsql
*
* @ param string $language Language name
*/
2015-12-17 19:18:01 +01:00
public function createLanguage ( $language )
{
2013-04-03 06:27:11 +02:00
$result = $this -> preparedQuery (
" SELECT lanname FROM pg_language WHERE lanname = ?; " ,
array ( $language )
) -> first ();
if ( ! $result ) {
$this -> query ( " CREATE LANGUAGE $language ; " );
}
}
/**
* Return a set type - formatted string
* This is used for Multi - enum support , which isn ' t actually supported by Postgres .
* Throws a user error to show our lack of support , and return an " int " , specifically for sapphire
* tests that test multi - enums . This results in a test failure , but not crashing the test run .
*
* @ param array $values Contains a tokenised list of info about this data type
* @ return string
*/
2015-12-17 19:18:01 +01:00
public function set ( $values )
{
2013-04-03 06:27:11 +02:00
user_error ( " PostGreSQL does not support multi-enum " , E_USER_ERROR );
return " int " ;
}
}