Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow other schema than "public" on PostgreSQL databases #217

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/Pgsql/PgsqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,27 @@ public function getConnectionCollation()
return $array[0]['lc_collate'];
}

/**
* Internal function to get the name of the default schema for the current PostgreSQL connexion.
* 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 +204,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 +226,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
16 changes: 13 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