Commit 17c1476e authored by Benjamin Eberlei's avatar Benjamin Eberlei

DBAL-75 - Fix handling of schema in PostgreSQL Platform and SchemaManager.

parent 16e2d112
...@@ -154,30 +154,17 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -154,30 +154,17 @@ class PostgreSqlPlatform extends AbstractPlatform
public function getListSequencesSQL($database) public function getListSequencesSQL($database)
{ {
return "SELECT return "SELECT
relname c.relname, n.nspname AS schemaname
FROM FROM
pg_class pg_class c, pg_namespace n
WHERE relkind = 'S' AND relnamespace IN WHERE relkind = 'S' AND n.oid = c.relnamespace AND
(SELECT oid FROM pg_namespace (n.nspname NOT LIKE 'pg_%' AND n.nspname != 'information_schema')";
WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
} }
public function getListTablesSQL() public function getListTablesSQL()
{ {
return "SELECT return "SELECT tablename AS table_name, schemaname AS schema_name
c.relname AS table_name FROM pg_tables WHERE schemaname NOT LIKE 'pg_%' AND schemaname != 'information_schema'";
FROM pg_class c, pg_user u
WHERE c.relowner = u.usesysid
AND c.relkind = 'r'
AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname)
AND c.relname !~ '^(pg_|sql_)'
UNION
SELECT c.relname AS table_name
FROM pg_class c
WHERE c.relkind = 'r'
AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname)
AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner)
AND c.relname !~ '^pg_'";
} }
public function getListViewsSQL($database) public function getListViewsSQL($database)
...@@ -192,9 +179,9 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -192,9 +179,9 @@ class PostgreSqlPlatform extends AbstractPlatform
WHERE r.conrelid = WHERE r.conrelid =
( (
SELECT c.oid SELECT c.oid
FROM pg_catalog.pg_class c FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE " .$this->getTableWhereClause($table) ."
WHERE c.relname = '" . $table . "' AND pg_catalog.pg_table_is_visible(c.oid) AND n.oid = c.relnamespace
) )
AND r.contype = 'f'"; AND r.contype = 'f'";
} }
...@@ -237,11 +224,23 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -237,11 +224,23 @@ class PostgreSqlPlatform extends AbstractPlatform
FROM pg_class, pg_index FROM pg_class, pg_index
WHERE oid IN ( WHERE oid IN (
SELECT indexrelid SELECT indexrelid
FROM pg_index, pg_class FROM pg_index si, pg_class sc, pg_namespace sn
WHERE pg_class.relname='$table' AND pg_class.oid=pg_index.indrelid WHERE " . $this->getTableWhereClause($table, 'sc', 'sn')." AND sc.oid=si.indrelid AND sc.relnamespace = sn.oid
) AND pg_index.indexrelid = oid"; ) AND pg_index.indexrelid = oid";
} }
private function getTableWhereClause($table, $classAlias = 'c', $namespaceAlias = 'n')
{
$whereClause = "";
if (strpos($table, ".") !== false) {
list($schema, $table) = explode(".", $table);
$whereClause = "$classAlias.relname = '" . $table . "' AND $namespaceAlias.nspname = '" . $schema . "'";
} else {
$whereClause = "$classAlias.relname = '" . $table . "'";
}
return $whereClause;
}
public function getListTableColumnsSQL($table) public function getListTableColumnsSQL($table)
{ {
return "SELECT return "SELECT
...@@ -264,11 +263,12 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -264,11 +263,12 @@ class PostgreSqlPlatform extends AbstractPlatform
WHERE c.oid = pg_attrdef.adrelid WHERE c.oid = pg_attrdef.adrelid
AND pg_attrdef.adnum=a.attnum AND pg_attrdef.adnum=a.attnum
) AS default ) AS default
FROM pg_attribute a, pg_class c, pg_type t FROM pg_attribute a, pg_class c, pg_type t, pg_namespace n
WHERE c.relname = '$table' WHERE ".$this->getTableWhereClause($table, 'c', 'n') ."
AND a.attnum > 0 AND a.attnum > 0
AND a.attrelid = c.oid AND a.attrelid = c.oid
AND a.atttypid = t.oid AND a.atttypid = t.oid
AND n.oid = c.relnamespace
ORDER BY a.attnum"; ORDER BY a.attnum";
} }
......
...@@ -109,7 +109,11 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager ...@@ -109,7 +109,11 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
protected function _getPortableTableDefinition($table) protected function _getPortableTableDefinition($table)
{ {
if ($table['schema_name'] == 'public') {
return $table['table_name']; return $table['table_name'];
} else {
return $table['schema_name'] . "." . $table['table_name'];
}
} }
/** /**
...@@ -155,8 +159,14 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager ...@@ -155,8 +159,14 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
protected function _getPortableSequenceDefinition($sequence) protected function _getPortableSequenceDefinition($sequence)
{ {
$data = $this->_conn->fetchAll('SELECT min_value, increment_by FROM ' . $sequence['relname']); if ($sequence['schemaname'] != 'public') {
return new Sequence($sequence['relname'], $data[0]['increment_by'], $data[0]['min_value']); $sequenceName = $sequence['schemaname'] . "." . $sequence['relname'];
} else {
$sequenceName = $sequence['relname'];
}
$data = $this->_conn->fetchAll('SELECT min_value, increment_by FROM ' . $sequenceName);
return new Sequence($sequenceName, $data[0]['increment_by'], $data[0]['min_value']);
} }
protected function _getPortableTableColumnDefinition($tableColumn) protected function _getPortableTableColumnDefinition($tableColumn)
......
...@@ -98,6 +98,40 @@ class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -98,6 +98,40 @@ class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$tableFinal = $this->_sm->listTableDetails('autoinc_table_drop'); $tableFinal = $this->_sm->listTableDetails('autoinc_table_drop');
$this->assertFalse($tableFinal->getColumn('id')->getAutoincrement()); $this->assertFalse($tableFinal->getColumn('id')->getAutoincrement());
} }
/**
* @group DBAL-75
*/
public function testTableWithSchema()
{
$this->_conn->exec('CREATE SCHEMA nested');
$nestedRelatedTable = new \Doctrine\DBAL\Schema\Table('nested.schemarelated');
$column = $nestedRelatedTable->addColumn('id', 'integer');
$column->setAutoincrement(true);
$nestedRelatedTable->setPrimaryKey(array('id'));
$nestedSchemaTable = new \Doctrine\DBAL\Schema\Table('nested.schematable');
$column = $nestedSchemaTable->addColumn('id', 'integer');
$column->setAutoincrement(true);
$nestedSchemaTable->setPrimaryKey(array('id'));
$nestedSchemaTable->addUnnamedForeignKeyConstraint($nestedRelatedTable, array('id'), array('id'));
$this->_sm->createTable($nestedRelatedTable);
$this->_sm->createTable($nestedSchemaTable);
$tables = $this->_sm->listTableNames();
$this->assertContains('nested.schematable', $tables, "The table should be detected with its non-public schema.");
$nestedSchemaTable = $this->_sm->listTableDetails('nested.schematable');
$this->assertTrue($nestedSchemaTable->hasColumn('id'));
$this->assertEquals(array('id'), $nestedSchemaTable->getPrimaryKey()->getColumns());
$relatedFks = $nestedSchemaTable->getForeignKeys();
$this->assertEquals(1, count($relatedFks));
$relatedFk = array_pop($relatedFks);
$this->assertEquals("nested.schemarelated", $relatedFk->getForeignTableName());
}
} }
class MoneyType extends Type class MoneyType extends Type
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment