mirror of
https://github.com/silverstripe/silverstripe-postgresql
synced 2024-10-22 17:05:45 +02:00
Converted to PSR-2
This commit is contained in:
parent
24caacbf3b
commit
e46a37090c
@ -12,7 +12,6 @@
|
||||
*/
|
||||
class PostgreSQLConnector extends DBConnector
|
||||
{
|
||||
|
||||
/**
|
||||
* Connection to the PG Database database
|
||||
*
|
||||
|
@ -8,7 +8,6 @@
|
||||
*/
|
||||
class PostgreSQLDatabase extends SS_Database
|
||||
{
|
||||
|
||||
/**
|
||||
* Database schema manager object
|
||||
*
|
||||
|
@ -9,7 +9,6 @@
|
||||
*/
|
||||
class PostgreSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelper
|
||||
{
|
||||
|
||||
/**
|
||||
* Create a connection of the appropriate type
|
||||
*
|
||||
|
@ -8,7 +8,6 @@
|
||||
*/
|
||||
class PostgreSQLQuery extends SS_Query
|
||||
{
|
||||
|
||||
/**
|
||||
* The internal Postgres handle that points to the result set.
|
||||
* @var resource
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
class PostgreSQLQueryBuilder extends DBQueryBuilder
|
||||
{
|
||||
|
||||
/**
|
||||
* Return the LIMIT clause ready for inserting into a query.
|
||||
*
|
||||
|
@ -8,7 +8,6 @@
|
||||
*/
|
||||
class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
{
|
||||
|
||||
/**
|
||||
* Identifier for this schema, used for configuring schema-specific table
|
||||
* creation options
|
||||
@ -58,7 +57,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
|
||||
public function createDatabase($name)
|
||||
{
|
||||
if(PostgreSQLDatabase::model_schema_as_database()) {
|
||||
if (PostgreSQLDatabase::model_schema_as_database()) {
|
||||
$schemaName = $this->database->databaseToSchemaName($name);
|
||||
return $this->createSchema($schemaName);
|
||||
}
|
||||
@ -79,7 +78,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
|
||||
public function databaseExists($name)
|
||||
{
|
||||
if(PostgreSQLDatabase::model_schema_as_database()) {
|
||||
if (PostgreSQLDatabase::model_schema_as_database()) {
|
||||
$schemaName = $this->database->databaseToSchemaName($name);
|
||||
return $this->schemaExists($schemaName);
|
||||
}
|
||||
@ -98,10 +97,10 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
|
||||
public function databaseList()
|
||||
{
|
||||
if(PostgreSQLDatabase::model_schema_as_database()) {
|
||||
if (PostgreSQLDatabase::model_schema_as_database()) {
|
||||
$schemas = $this->schemaList();
|
||||
$names = array();
|
||||
foreach($schemas as $schema) {
|
||||
foreach ($schemas as $schema) {
|
||||
$names[] = $this->database->schemaToDatabaseName($schema);
|
||||
}
|
||||
return array_unique($names);
|
||||
@ -121,7 +120,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
|
||||
public function dropDatabase($name)
|
||||
{
|
||||
if(PostgreSQLDatabase::model_schema_as_database()) {
|
||||
if (PostgreSQLDatabase::model_schema_as_database()) {
|
||||
$schemaName = $this->database->databaseToSchemaName($name);
|
||||
return $this->dropSchema($schemaName);
|
||||
}
|
||||
@ -186,7 +185,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
$fieldSchemas .= "\"$k\" $v,\n";
|
||||
}
|
||||
}
|
||||
if(!empty($options[self::ID])) {
|
||||
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');
|
||||
@ -197,7 +196,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
|
||||
//First of all, does this table already exist
|
||||
$doesExist = $this->hasTable($table);
|
||||
if($doesExist) {
|
||||
if ($doesExist) {
|
||||
// Table already exists, just return the name, in line with baseclass documentation.
|
||||
return $table;
|
||||
}
|
||||
@ -206,9 +205,9 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
//for GiST searches
|
||||
$fulltexts = '';
|
||||
$triggers = '';
|
||||
if($indexes) {
|
||||
foreach($indexes as $name => $this_index){
|
||||
if(is_array($this_index) && $this_index['type'] == 'fulltext') {
|
||||
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'];
|
||||
@ -223,7 +222,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
}
|
||||
|
||||
//Do we need to create a tablespace for this item?
|
||||
if($advancedOptions && isset($advancedOptions['tablespace'])){
|
||||
if ($advancedOptions && isset($advancedOptions['tablespace'])) {
|
||||
$this->createOrReplaceTablespace(
|
||||
$advancedOptions['tablespace']['name'],
|
||||
$advancedOptions['tablespace']['location']
|
||||
@ -239,17 +238,17 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
primary key (\"ID\")
|
||||
)$tableSpace; $indexSchemas $addOptions");
|
||||
|
||||
if($triggers!=''){
|
||||
if ($triggers!='') {
|
||||
$this->query($triggers);
|
||||
}
|
||||
|
||||
//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);
|
||||
}
|
||||
|
||||
//Lastly, clustering goes here:
|
||||
if($advancedOptions && isset($advancedOptions['cluster'])){
|
||||
if ($advancedOptions && isset($advancedOptions['cluster'])) {
|
||||
$this->query("CLUSTER \"$table\" USING \"{$advancedOptions['cluster']}\";");
|
||||
}
|
||||
|
||||
@ -312,7 +311,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
}
|
||||
|
||||
//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->query("ALTER TABLE \"$table\" SET TABLESPACE {$advancedOptions['tablespace']['name']};");
|
||||
}
|
||||
@ -330,7 +329,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
$indexSpec = $this->parseIndexSpec($indexName, $indexSpec);
|
||||
$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
|
||||
|
||||
//Go and get the tsearch details:
|
||||
@ -339,7 +338,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
//Drop this column if it already exists:
|
||||
|
||||
//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']}\";";
|
||||
}
|
||||
|
||||
@ -367,9 +366,9 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
//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') {
|
||||
if ($indexSpec['type']=='fulltext') {
|
||||
$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']};";
|
||||
$triggers.=$ts_details['triggers'];
|
||||
}
|
||||
@ -377,7 +376,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
|
||||
//Check that this index doesn't already exist:
|
||||
$indexes=$this->indexList($table);
|
||||
if(isset($indexes[$indexName])){
|
||||
if (isset($indexes[$indexName])) {
|
||||
$alterIndexList[] = "DROP INDEX IF EXISTS \"$indexNamePG\";";
|
||||
}
|
||||
|
||||
@ -388,18 +387,18 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
}
|
||||
}
|
||||
|
||||
if($alterList) {
|
||||
if ($alterList) {
|
||||
$alterations = implode(",\n", $alterList);
|
||||
$this->query("ALTER TABLE \"$table\" " . $alterations);
|
||||
}
|
||||
|
||||
//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'];
|
||||
$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]));
|
||||
Database::alteration_message(
|
||||
sprintf("Table %s options changed: %s", $table, $alteredOptions[$this->class]),
|
||||
@ -415,14 +414,14 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
$this->query($drop_triggers);
|
||||
}
|
||||
|
||||
if($triggers) {
|
||||
if ($triggers) {
|
||||
$this->query($triggers);
|
||||
|
||||
$triggerbits=explode(';', $triggers);
|
||||
foreach($triggerbits as $trigger){
|
||||
foreach ($triggerbits as $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
|
||||
$this->query("UPDATE \"{$table}\" SET \"{$trigger_fields[0]}\"=\"$trigger_fields[0]\";");
|
||||
}
|
||||
@ -434,7 +433,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
}
|
||||
|
||||
//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']);
|
||||
}
|
||||
|
||||
@ -461,7 +460,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
array($oid)
|
||||
)->first();
|
||||
|
||||
if($clustered) {
|
||||
if ($clustered) {
|
||||
$this->query("ALTER TABLE \"$table\" SET WITHOUT CLUSTER;");
|
||||
}
|
||||
}
|
||||
@ -492,23 +491,23 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
return '';
|
||||
}
|
||||
|
||||
if(isset($matches[1])) {
|
||||
if (isset($matches[1])) {
|
||||
$alterCol = "ALTER COLUMN \"$colName\" TYPE $matches[1]\n";
|
||||
|
||||
// SET null / not null
|
||||
if(!empty($matches[2])) {
|
||||
if (!empty($matches[2])) {
|
||||
$alterCol .= ",\nALTER COLUMN \"$colName\" SET $matches[2]";
|
||||
}
|
||||
|
||||
// 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\" 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])) {
|
||||
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], ')');
|
||||
@ -519,10 +518,10 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
//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")) {
|
||||
if ($this->hasTable("{$tableName}_Live")) {
|
||||
$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);";
|
||||
}
|
||||
|
||||
@ -530,12 +529,12 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
}
|
||||
|
||||
//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\"";
|
||||
}
|
||||
|
||||
//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]";
|
||||
}
|
||||
}
|
||||
@ -576,7 +575,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
public function renameField($tableName, $oldName, $newName)
|
||||
{
|
||||
$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\"");
|
||||
|
||||
//Remove this from the cached list:
|
||||
@ -601,12 +600,12 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
$output = array();
|
||||
if ($fields) {
|
||||
foreach ($fields as $field) {
|
||||
switch($field['data_type']){
|
||||
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){
|
||||
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[]))
|
||||
@ -614,19 +613,19 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
|
||||
//TODO: replace all this with a regular expression!
|
||||
$value=$constraint['pg_get_constraintdef'];
|
||||
$value=substr($value, strpos($value,'='));
|
||||
$value=substr($value, strpos($value, '='));
|
||||
$value=str_replace("''", "'", $value);
|
||||
|
||||
$in_value=false;
|
||||
$constraints=array();
|
||||
$current_value='';
|
||||
for($i=0; $i<strlen($value); $i++){
|
||||
for ($i=0; $i<strlen($value); $i++) {
|
||||
$char=substr($value, $i, 1);
|
||||
if ($in_value) {
|
||||
$current_value.=$char;
|
||||
}
|
||||
|
||||
if($char=="'"){
|
||||
if ($char=="'") {
|
||||
if (!$in_value) {
|
||||
$in_value=true;
|
||||
} else {
|
||||
@ -637,12 +636,12 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
}
|
||||
}
|
||||
|
||||
if(sizeof($constraints)>0){
|
||||
if (sizeof($constraints)>0) {
|
||||
//Get the 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));
|
||||
}
|
||||
} else{
|
||||
} else {
|
||||
$output[$field['column_name']]='varchar(' . $field['character_maximum_length'] . ')';
|
||||
}
|
||||
break;
|
||||
@ -751,7 +750,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
//This is techically a bug, since new tables will not be indexed.
|
||||
|
||||
// If requesting the definition rather than the DDL
|
||||
if($asDbValue) {
|
||||
if ($asDbValue) {
|
||||
$indexName=trim($indexName, '()');
|
||||
return $indexName;
|
||||
}
|
||||
@ -805,13 +804,13 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
public function alterIndex($tableName, $indexName, $indexSpec)
|
||||
{
|
||||
$indexSpec = trim($indexSpec);
|
||||
if($indexSpec[0] != '(') {
|
||||
list($indexType, $indexFields) = explode(' ',$indexSpec,2);
|
||||
if ($indexSpec[0] != '(') {
|
||||
list($indexType, $indexFields) = explode(' ', $indexSpec, 2);
|
||||
} else {
|
||||
$indexFields = $indexSpec;
|
||||
}
|
||||
|
||||
if(!$indexType) {
|
||||
if (!$indexType) {
|
||||
$indexType = "index";
|
||||
}
|
||||
|
||||
@ -833,17 +832,17 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
)->first();
|
||||
|
||||
// Option 1: output as a string
|
||||
if(strpos($trigger['tgargs'],'\000') !== false) {
|
||||
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);
|
||||
$bytes = str_split($trigger['tgargs'], 2);
|
||||
$argList = array();
|
||||
$nextArg = "";
|
||||
foreach($bytes as $byte) {
|
||||
if($byte == "00") {
|
||||
foreach ($bytes as $byte) {
|
||||
if ($byte == "00") {
|
||||
$argList[] = $nextArg;
|
||||
$nextArg = "";
|
||||
} else {
|
||||
@ -867,7 +866,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
);
|
||||
|
||||
$indexList = array();
|
||||
foreach($indexes as $index) {
|
||||
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'];
|
||||
@ -876,12 +875,12 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
$type = '';
|
||||
|
||||
//Check for uniques:
|
||||
if(substr($index['indexdef'], 0, 13)=='CREATE UNIQUE') {
|
||||
if (substr($index['indexdef'], 0, 13)=='CREATE UNIQUE') {
|
||||
$type = 'unique';
|
||||
}
|
||||
|
||||
//check for hashes, btrees etc:
|
||||
if(strpos(strtolower($index['indexdef']), 'using hash ')!==false) {
|
||||
if (strpos(strtolower($index['indexdef']), 'using hash ')!==false) {
|
||||
$type = 'hash';
|
||||
}
|
||||
|
||||
@ -889,7 +888,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
//if(strpos(strtolower($index['indexdef']), 'using btree ')!==false)
|
||||
// $prefix='using btree ';
|
||||
|
||||
if(strpos(strtolower($index['indexdef']), 'using rtree ')!==false) {
|
||||
if (strpos(strtolower($index['indexdef']), 'using rtree ')!==false) {
|
||||
$type = 'rtree';
|
||||
}
|
||||
|
||||
@ -921,7 +920,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
"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) {
|
||||
foreach ($result as $record) {
|
||||
$table = reset($record);
|
||||
$tables[strtolower($table)] = $table;
|
||||
}
|
||||
@ -937,7 +936,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
*/
|
||||
protected function constraintExists($constraint)
|
||||
{
|
||||
if(!isset(self::$cached_constraints[$constraint])){
|
||||
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;",
|
||||
@ -969,7 +968,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
$result = $this->preparedQuery($query, $tableName, $this->database->currentSchema());
|
||||
|
||||
$table = array();
|
||||
while($row = pg_fetch_assoc($result)) {
|
||||
while ($row = pg_fetch_assoc($result)) {
|
||||
$table[] = array(
|
||||
'Column' => $row['Column'],
|
||||
'DataType' => $row['DataType']
|
||||
@ -994,7 +993,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
WHERE trigger_name = ? AND trigger_schema = ?;",
|
||||
array($triggerName, $this->database->currentSchema())
|
||||
)->first();
|
||||
if($exists){
|
||||
if ($exists) {
|
||||
$this->query("DROP trigger IF EXISTS $triggerName ON \"$tableName\";");
|
||||
}
|
||||
}
|
||||
@ -1007,7 +1006,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
*/
|
||||
protected function triggerFieldsFromTrigger($trigger)
|
||||
{
|
||||
if($trigger){
|
||||
if ($trigger) {
|
||||
$tsvector='tsvector_update_trigger';
|
||||
$ts_pos=strpos($trigger, $tsvector);
|
||||
$details=trim(substr($trigger, $ts_pos+strlen($tsvector)), '();');
|
||||
@ -1040,15 +1039,15 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
//Annoyingly, we need to do a good ol' fashioned switch here:
|
||||
$default = $values['default'] ? '1' : '0';
|
||||
|
||||
if(!isset($values['arrayValue'])) {
|
||||
if (!isset($values['arrayValue'])) {
|
||||
$values['arrayValue']='';
|
||||
}
|
||||
|
||||
if($asDbValue) {
|
||||
if ($asDbValue) {
|
||||
return array('data_type'=>'smallint');
|
||||
}
|
||||
|
||||
if($values['arrayValue'] != '') {
|
||||
if ($values['arrayValue'] != '') {
|
||||
$default = '';
|
||||
} else {
|
||||
$default = ' default ' . (int)$values['default'];
|
||||
@ -1064,7 +1063,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
*/
|
||||
public function date($values)
|
||||
{
|
||||
if(!isset($values['arrayValue'])) {
|
||||
if (!isset($values['arrayValue'])) {
|
||||
$values['arrayValue']='';
|
||||
}
|
||||
|
||||
@ -1080,23 +1079,23 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
*/
|
||||
public function decimal($values, $asDbValue=false)
|
||||
{
|
||||
if(!isset($values['arrayValue'])) {
|
||||
if (!isset($values['arrayValue'])) {
|
||||
$values['arrayValue']='';
|
||||
}
|
||||
|
||||
// Avoid empty strings being put in the db
|
||||
if($values['precision'] == '') {
|
||||
if ($values['precision'] == '') {
|
||||
$precision = 1;
|
||||
} else {
|
||||
$precision = $values['precision'];
|
||||
}
|
||||
|
||||
$defaultValue = '';
|
||||
if(isset($values['default']) && is_numeric($values['default'])) {
|
||||
if (isset($values['default']) && is_numeric($values['default'])) {
|
||||
$defaultValue = ' default ' . floatval($values['default']);
|
||||
}
|
||||
|
||||
if($asDbValue) {
|
||||
if ($asDbValue) {
|
||||
return array('data_type' => 'numeric', 'precision' => $precision);
|
||||
} else {
|
||||
return "decimal($precision){$values['arrayValue']}$defaultValue";
|
||||
@ -1113,11 +1112,11 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
{
|
||||
//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'])) {
|
||||
if (!isset($values['arrayValue'])) {
|
||||
$values['arrayValue']='';
|
||||
}
|
||||
|
||||
if($values['arrayValue']!='') {
|
||||
if ($values['arrayValue']!='') {
|
||||
$default = '';
|
||||
} else {
|
||||
$default = " default '{$values['default']}'";
|
||||
@ -1135,11 +1134,11 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
*/
|
||||
public function float($values, $asDbValue = false)
|
||||
{
|
||||
if(!isset($values['arrayValue'])) {
|
||||
if (!isset($values['arrayValue'])) {
|
||||
$values['arrayValue']='';
|
||||
}
|
||||
|
||||
if($asDbValue) {
|
||||
if ($asDbValue) {
|
||||
return array('data_type' => 'double precision');
|
||||
} else {
|
||||
return "float{$values['arrayValue']}";
|
||||
@ -1167,15 +1166,15 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
*/
|
||||
public function int($values, $asDbValue = false)
|
||||
{
|
||||
if(!isset($values['arrayValue'])) {
|
||||
if (!isset($values['arrayValue'])) {
|
||||
$values['arrayValue']='';
|
||||
}
|
||||
|
||||
if($asDbValue) {
|
||||
if ($asDbValue) {
|
||||
return array('data_type'=>'integer', 'precision'=>'32');
|
||||
}
|
||||
|
||||
if($values['arrayValue']!='') {
|
||||
if ($values['arrayValue']!='') {
|
||||
$default='';
|
||||
} else {
|
||||
$default=' default ' . (int)$values['default'];
|
||||
@ -1193,15 +1192,15 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
*/
|
||||
public function bigint($values, $asDbValue = false)
|
||||
{
|
||||
if(!isset($values['arrayValue'])) {
|
||||
if (!isset($values['arrayValue'])) {
|
||||
$values['arrayValue']='';
|
||||
}
|
||||
|
||||
if($asDbValue) {
|
||||
if ($asDbValue) {
|
||||
return array('data_type'=>'bigint', 'precision'=>'64');
|
||||
}
|
||||
|
||||
if($values['arrayValue']!='') {
|
||||
if ($values['arrayValue']!='') {
|
||||
$default='';
|
||||
} else {
|
||||
$default=' default ' . (int)$values['default'];
|
||||
@ -1220,11 +1219,11 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
*/
|
||||
public function SS_Datetime($values, $asDbValue = false)
|
||||
{
|
||||
if(!isset($values['arrayValue'])) {
|
||||
if (!isset($values['arrayValue'])) {
|
||||
$values['arrayValue']='';
|
||||
}
|
||||
|
||||
if($asDbValue) {
|
||||
if ($asDbValue) {
|
||||
return array('data_type'=>'timestamp without time zone');
|
||||
} else {
|
||||
return "timestamp{$values['arrayValue']}";
|
||||
@ -1240,11 +1239,11 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
*/
|
||||
public function text($values, $asDbValue = false)
|
||||
{
|
||||
if(!isset($values['arrayValue'])) {
|
||||
if (!isset($values['arrayValue'])) {
|
||||
$values['arrayValue'] = '';
|
||||
}
|
||||
|
||||
if($asDbValue) {
|
||||
if ($asDbValue) {
|
||||
return array('data_type'=>'text');
|
||||
} else {
|
||||
return "text{$values['arrayValue']}";
|
||||
@ -1259,7 +1258,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
*/
|
||||
public function time($values)
|
||||
{
|
||||
if(!isset($values['arrayValue'])) {
|
||||
if (!isset($values['arrayValue'])) {
|
||||
$values['arrayValue'] = '';
|
||||
}
|
||||
|
||||
@ -1275,15 +1274,15 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
*/
|
||||
public function varchar($values, $asDbValue=false)
|
||||
{
|
||||
if(!isset($values['arrayValue'])) {
|
||||
if (!isset($values['arrayValue'])) {
|
||||
$values['arrayValue'] = '';
|
||||
}
|
||||
|
||||
if(!isset($values['precision'])) {
|
||||
if (!isset($values['precision'])) {
|
||||
$values['precision'] = 255;
|
||||
}
|
||||
|
||||
if($asDbValue) {
|
||||
if ($asDbValue) {
|
||||
return array('data_type'=>'varchar', 'precision'=>$values['precision']);
|
||||
} else {
|
||||
return "varchar({$values['precision']}){$values['arrayValue']}";
|
||||
@ -1300,12 +1299,12 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
*/
|
||||
public function year($values, $asDbValue = false)
|
||||
{
|
||||
if(!isset($values['arrayValue'])) {
|
||||
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) {
|
||||
if ($asDbValue) {
|
||||
return array('data_type'=>'decimal', 'precision'=>'4');
|
||||
} else {
|
||||
return "decimal(4,0){$values['arrayValue']}";
|
||||
@ -1376,7 +1375,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
{
|
||||
//return array('SiteTree','Page');
|
||||
$constraints = $this->constraintExists("{$tableName}_{$fieldName}_check");
|
||||
if($constraints) {
|
||||
if ($constraints) {
|
||||
return $this->enumValuesFromConstraint($constraints['pg_get_constraintdef']);
|
||||
} else {
|
||||
return array();
|
||||
@ -1395,7 +1394,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
$constraint = substr($constraint, 0, -11);
|
||||
$constraints = array();
|
||||
$segments = explode(',', $constraint);
|
||||
foreach($segments as $this_segment){
|
||||
foreach ($segments as $this_segment) {
|
||||
$bits = preg_split('/ *:: */', $this_segment);
|
||||
array_unshift($constraints, trim($bits[0], " '"));
|
||||
}
|
||||
@ -1437,7 +1436,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
// DB::query("DROP TABLESPACE $name;");
|
||||
|
||||
//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';");
|
||||
}
|
||||
}
|
||||
@ -1459,16 +1458,16 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
$first=true;
|
||||
|
||||
//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']);
|
||||
$tableSpace=' TABLESPACE ' . $extensions['tablespace']['name'];
|
||||
} else {
|
||||
$tableSpace='';
|
||||
}
|
||||
|
||||
foreach($partitions as $partition_name => $partition_value){
|
||||
foreach ($partitions as $partition_name => $partition_value) {
|
||||
//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;");
|
||||
} else {
|
||||
//Drop the constraint, we will recreate in in the next line
|
||||
@ -1476,7 +1475,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
"SELECT conname FROM pg_constraint WHERE conname = ?;",
|
||||
array("{$partition_name}_pkey")
|
||||
);
|
||||
if($existing_constraint){
|
||||
if ($existing_constraint) {
|
||||
$this->query("ALTER TABLE \"$partition_name\" DROP CONSTRAINT \"{$partition_name}_pkey\";");
|
||||
}
|
||||
$this->dropTrigger(strtolower('trigger_' . $tableName . '_insert'), $tableName);
|
||||
@ -1484,7 +1483,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
|
||||
$this->query("ALTER TABLE \"$partition_name\" ADD CONSTRAINT \"{$partition_name}_pkey\" PRIMARY KEY (\"ID\");");
|
||||
|
||||
if($first){
|
||||
if ($first) {
|
||||
$trigger.='IF';
|
||||
$first=false;
|
||||
} else {
|
||||
@ -1493,16 +1492,16 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
|
||||
$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.
|
||||
// Some of this code is duplicated, and could be tidied up
|
||||
foreach($indexes as $name => $this_index){
|
||||
if($this_index['type']=='fulltext'){
|
||||
foreach ($indexes as $name => $this_index) {
|
||||
if ($this_index['type']=='fulltext') {
|
||||
$fillfactor = $where = '';
|
||||
if(isset($this_index['fillfactor'])) {
|
||||
if (isset($this_index['fillfactor'])) {
|
||||
$fillfactor = 'WITH (FILLFACTOR = ' . $this_index['fillfactor'] . ')';
|
||||
}
|
||||
if(isset($this_index['where'])) {
|
||||
if (isset($this_index['where'])) {
|
||||
$where = 'WHERE ' . $this_index['where'];
|
||||
}
|
||||
$clusterMethod = PostgreSQLDatabase::default_fts_cluster_method();
|
||||
@ -1510,14 +1509,14 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
$ts_details = $this->fulltext($this_index, $partition_name, $name);
|
||||
$this->query($ts_details['triggers']);
|
||||
} else {
|
||||
if(is_array($this_index)) {
|
||||
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) {
|
||||
if ($createIndex !== false) {
|
||||
$this->query($createIndex);
|
||||
}
|
||||
}
|
||||
@ -1525,7 +1524,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
}
|
||||
|
||||
//Lastly, clustering goes here:
|
||||
if($extensions && isset($extensions['cluster'])){
|
||||
if ($extensions && isset($extensions['cluster'])) {
|
||||
$this->query("CLUSTER \"$partition_name\" USING \"{$extensions['cluster']}\";");
|
||||
}
|
||||
}
|
||||
@ -1549,7 +1548,7 @@ class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
array($language)
|
||||
)->first();
|
||||
|
||||
if(!$result) {
|
||||
if (!$result) {
|
||||
$this->query("CREATE LANGUAGE $language;");
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,6 @@
|
||||
*/
|
||||
class PostgreSQLConnectorTest extends SapphireTest
|
||||
{
|
||||
|
||||
public function testSubstitutesPlaceholders()
|
||||
{
|
||||
$connector = new PostgreSQLConnector();
|
||||
|
Loading…
Reference in New Issue
Block a user