Skip to content

Commit

Permalink
Merge pull request #217 from richard67/master-add-postgresql-default-…
Browse files Browse the repository at this point in the history
…scheme

Allow other schema than "public" on PostgreSQL databases
  • Loading branch information
nibra authored Dec 14, 2020
2 parents da50bc1 + 2043179 commit 90cab6c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
22 changes: 20 additions & 2 deletions src/Pgsql/PgsqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,26 @@ public function getConnectionCollation()
return $array[0]['lc_collate'];
}

/**
* Internal function to get the name of the default schema for the current PostgreSQL connection.
* That is the schema where tables are created by Joomla.
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
private function getDefaultSchema()
{
// Supported since PostgreSQL 7.3
$this->setQuery('SELECT (current_schemas(false))[1]');

return $this->loadResult();
}

/**
* Shows the table CREATE statement that creates the given tables.
*
* This is unsuported by PostgreSQL.
* This is unsupported by PostgreSQL.
*
* @param mixed $tables A table name or a list of table names.
*
Expand Down Expand Up @@ -187,6 +203,8 @@ public function getTableColumns($table, $typeOnly = true)

$tableSub = $this->replacePrefix($table);

$defaultSchema = $this->getDefaultSchema();

$this->setQuery('
SELECT a.attname AS "column_name",
pg_catalog.format_type(a.atttypid, a.atttypmod) as "type",
Expand All @@ -207,7 +225,7 @@ public function getTableColumns($table, $typeOnly = true)
WHERE a.attrelid =
(SELECT oid FROM pg_catalog.pg_class WHERE relname=' . $this->quote($tableSub) . '
AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE
nspname = \'public\')
nspname = ' . $this->quote($defaultSchema) . ')
)
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum'
Expand Down
32 changes: 29 additions & 3 deletions src/Postgresql/PostgresqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public function getTableCreate($tables)
/**
* Retrieves field information about a given table.
*
* @param string $table The name of the database table.
* @param string $table The name of the database table. For PostgreSQL may start with a schema.
* @param boolean $typeOnly True to only return field types.
*
* @return array An array of fields for the database table.
Expand All @@ -428,8 +428,18 @@ public function getTableColumns($table, $typeOnly = true)
$this->connect();

$result = array();

$tableSub = $this->replacePrefix($table);
$fn = explode('.', $tableSub);

if (count($fn) === 2)
{
$schema = $fn[0];
$tableSub = $fn[1];
}
else
{
$schema = $this->getDefaultSchema();
}

$this->setQuery('
SELECT a.attname AS "column_name",
Expand All @@ -451,7 +461,7 @@ public function getTableColumns($table, $typeOnly = true)
WHERE a.attrelid =
(SELECT oid FROM pg_catalog.pg_class WHERE relname=' . $this->quote($tableSub) . '
AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE
nspname = \'public\')
nspname = ' . $this->quote($schema) . ')
)
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum'
Expand Down Expand Up @@ -1636,4 +1646,20 @@ public function decodeBinary($data)

return $data;
}

/**
* Internal function to get the name of the default schema for the current PostgreSQL connection.
* That is the schema where tables are created by Joomla.
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
private function getDefaultSchema()
{
// Supported since PostgreSQL 7.3
$this->setQuery('SELECT (current_schemas(false))[1]');

return $this->loadResult();
}
}

0 comments on commit 90cab6c

Please sign in to comment.