Merge remote-tracking branch 'origin/1.2'

# Conflicts:
#	code/PostgreSQLSchemaManager.php
#	composer.json
This commit is contained in:
Damian Mooyman 2016-01-21 09:30:15 +13:00
commit 24caacbf3b

View File

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