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
public function getListSequencesSQL($database)
{
return "SELECT
relname
c.relname, n.nspname AS schemaname
FROM
pg_class
WHERE relkind = 'S' AND relnamespace IN
(SELECT oid FROM pg_namespace
WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
pg_class c, pg_namespace n
WHERE relkind = 'S' AND n.oid = c.relnamespace AND
(n.nspname NOT LIKE 'pg_%' AND n.nspname != 'information_schema')";
}
public function getListTablesSQL()
{
return "SELECT
c.relname AS table_name
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_'";
return "SELECT tablename AS table_name, schemaname AS schema_name
FROM pg_tables WHERE schemaname NOT LIKE 'pg_%' AND schemaname != 'information_schema'";
}
public function getListViewsSQL($database)
......@@ -192,9 +179,9 @@ class PostgreSqlPlatform extends AbstractPlatform
WHERE r.conrelid =
(
SELECT c.oid
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname = '" . $table . "' AND pg_catalog.pg_table_is_visible(c.oid)
FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n
WHERE " .$this->getTableWhereClause($table) ."
AND n.oid = c.relnamespace
)
AND r.contype = 'f'";
}
......@@ -237,11 +224,23 @@ class PostgreSqlPlatform extends AbstractPlatform
FROM pg_class, pg_index
WHERE oid IN (
SELECT indexrelid
FROM pg_index, pg_class
WHERE pg_class.relname='$table' AND pg_class.oid=pg_index.indrelid
FROM pg_index si, pg_class sc, pg_namespace sn
WHERE " . $this->getTableWhereClause($table, 'sc', 'sn')." AND sc.oid=si.indrelid AND sc.relnamespace = sn.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)
{
return "SELECT
......@@ -264,11 +263,12 @@ class PostgreSqlPlatform extends AbstractPlatform
WHERE c.oid = pg_attrdef.adrelid
AND pg_attrdef.adnum=a.attnum
) AS default
FROM pg_attribute a, pg_class c, pg_type t
WHERE c.relname = '$table'
FROM pg_attribute a, pg_class c, pg_type t, pg_namespace n
WHERE ".$this->getTableWhereClause($table, 'c', 'n') ."
AND a.attnum > 0
AND a.attrelid = c.oid
AND a.atttypid = t.oid
AND n.oid = c.relnamespace
ORDER BY a.attnum";
}
......
......@@ -109,7 +109,11 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
protected function _getPortableTableDefinition($table)
{
return $table['table_name'];
if ($table['schema_name'] == 'public') {
return $table['table_name'];
} else {
return $table['schema_name'] . "." . $table['table_name'];
}
}
/**
......@@ -155,8 +159,14 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
protected function _getPortableSequenceDefinition($sequence)
{
$data = $this->_conn->fetchAll('SELECT min_value, increment_by FROM ' . $sequence['relname']);
return new Sequence($sequence['relname'], $data[0]['increment_by'], $data[0]['min_value']);
if ($sequence['schemaname'] != 'public') {
$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)
......
......@@ -98,6 +98,40 @@ class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$tableFinal = $this->_sm->listTableDetails('autoinc_table_drop');
$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
......
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