Commit fba268be authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge pull request #462 from deeky666/respect-sqlserver-schemaname

Fix full qualified table name schema introspection
parents 84ac8d98 803b3265
......@@ -752,6 +752,13 @@ class SQLAnywherePlatform extends AbstractPlatform
*/
public function getListTableColumnsSQL($table, $database = null)
{
$user = 'USER_NAME()';
if (strpos($table, '.') !== false) {
list($user, $table) = explode('.', $table);
$user = "'" . $user . "'";
}
return "SELECT col.column_name,
COALESCE(def.user_type_name, def.domain_name) AS 'type',
def.declared_width AS 'length',
......@@ -766,6 +773,7 @@ class SQLAnywherePlatform extends AbstractPlatform
ON col.table_id = def.base_table_id AND col.column_id = def.base_column_id
LEFT JOIN SYS.SYSREMARK AS rem
ON col.object_id = rem.object_id
WHERE def.base_owner_name = $user
ORDER BY def.base_column_id ASC";
}
......@@ -776,10 +784,18 @@ class SQLAnywherePlatform extends AbstractPlatform
*/
public function getListTableConstraintsSQL($table)
{
$user = '';
if (strpos($table, '.') !== false) {
list($user, $table) = explode('.', $table);
$user = "'" . $user . "'";
}
return "SELECT con.*
FROM SYS.SYSCONSTRAINT AS con
JOIN SYS.SYSTABLE AS tab ON con.table_object_id = tab.object_id
WHERE tab.table_name = '$table'";
JOIN SYS.SYSTAB AS tab ON con.table_object_id = tab.object_id
WHERE tab.table_name = '$table'
AND tab.creator = USER_ID($user)";
}
/**
......@@ -787,6 +803,13 @@ class SQLAnywherePlatform extends AbstractPlatform
*/
public function getListTableForeignKeysSQL($table)
{
$user = '';
if (strpos($table, '.') !== false) {
list($user, $table) = explode('.', $table);
$user = "'" . $user . "'";
}
return "SELECT fcol.column_name AS local_column,
ptbl.table_name AS foreign_table,
pcol.column_name AS foreign_column,
......@@ -854,6 +877,7 @@ class SQLAnywherePlatform extends AbstractPlatform
AND fk.foreign_index_id = dt.foreign_key_id
AND dt.event = 'D'
WHERE ftbl.table_name = '$table'
AND ftbl.creator = USER_ID($user)
ORDER BY fk.foreign_index_id ASC, idxcol.sequence ASC";
}
......@@ -862,6 +886,13 @@ class SQLAnywherePlatform extends AbstractPlatform
*/
public function getListTableIndexesSQL($table, $currentDatabase = null)
{
$user = '';
if (strpos($table, '.') !== false) {
list($user, $table) = explode('.', $table);
$user = "'" . $user . "'";
}
return "SELECT idx.index_name AS key_name,
IF idx.index_category = 1
THEN 1
......@@ -895,6 +926,7 @@ class SQLAnywherePlatform extends AbstractPlatform
JOIN SYS.SYSTAB AS tbl
ON idx.table_id = tbl.table_id
WHERE tbl.table_name = '$table'
AND tbl.creator = USER_ID($user)
ORDER BY idx.index_id ASC, idxcol.sequence ASC";
}
......
......@@ -793,6 +793,8 @@ class SQLServerPlatform extends AbstractPlatform
ON col.user_type_id = type.user_type_id
JOIN sys.objects AS obj
ON col.object_id = obj.object_id
JOIN sys.schemas AS scm
ON obj.schema_id = scm.schema_id
LEFT JOIN sys.default_constraints def
ON col.default_object_id = def.object_id
AND col.object_id = def.parent_object_id
......@@ -801,7 +803,7 @@ class SQLServerPlatform extends AbstractPlatform
AND col.column_id = prop.minor_id
AND prop.name = 'MS_Description'
WHERE obj.type = 'U'
AND obj.name = '$table'";
AND " . $this->getTableWhereClause($table, 'scm.name', 'obj.name');
}
/**
......@@ -822,7 +824,8 @@ class SQLServerPlatform extends AbstractPlatform
INNER JOIN sys.foreign_key_columns AS fc
INNER JOIN sys.objects AS o ON o.OBJECT_ID = fc.referenced_object_id
ON f.OBJECT_ID = fc.constraint_object_id
WHERE OBJECT_NAME (f.parent_object_id) = '" . $table . "'";
WHERE " .
$this->getTableWhereClause($table, 'SCHEMA_NAME (f.schema_id)', 'OBJECT_NAME (f.parent_object_id)');
}
/**
......@@ -840,10 +843,11 @@ class SQLServerPlatform extends AbstractPlatform
ELSE NULL
END AS flags
FROM sys.tables AS tbl
JOIN sys.schemas AS scm ON tbl.schema_id = scm.schema_id
JOIN sys.indexes AS idx ON tbl.object_id = idx.object_id
JOIN sys.index_columns AS idxcol ON idx.object_id = idxcol.object_id AND idx.index_id = idxcol.index_id
JOIN sys.columns AS col ON idxcol.object_id = col.object_id AND idxcol.column_id = col.column_id
WHERE tbl.name = '$table'
WHERE " . $this->getTableWhereClause($table, 'scm.name', 'tbl.name') . "
ORDER BY idx.index_id ASC, idxcol.index_column_id ASC";
}
......@@ -863,6 +867,27 @@ class SQLServerPlatform extends AbstractPlatform
return "SELECT name FROM sysobjects WHERE type = 'V' ORDER BY name";
}
/**
* Returns the where clause to filter schema and table name in a query.
*
* @param string $table The full qualified name of the table.
* @param string $tableColumn The name of the column to compare the schema to in the where clause.
* @param string $schemaColumn The name of the column to compare the table to in the where clause.
*
* @return string
*/
private function getTableWhereClause($table, $schemaColumn, $tableColumn)
{
if (strpos($table, ".") !== false) {
list($schema, $table) = explode(".", $table);
$schema = "'" . $schema . "'";
} else {
$schema = "SCHEMA_NAME()";
}
return "({$tableColumn} = '{$table}' AND {$schemaColumn} = {$schema})";
}
/**
* {@inheritDoc}
*/
......
......@@ -743,4 +743,44 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$this->assertInstanceOf('Doctrine\DBAL\Types\BinaryType', $table->getColumn('column_binary')->getType());
$this->assertTrue($table->getColumn('column_binary')->getFixed());
}
public function testListTableDetailsWithFullQualifiedTableName()
{
if ( ! $this->_sm->getDatabasePlatform()->supportsSchemas()) {
$this->markTestSkipped('Test only works on platforms that support schemas.');
}
$defaultSchemaName = $this->_sm->getDatabasePlatform()->getDefaultSchemaName();
$primaryTableName = 'primary_table';
$foreignTableName = 'foreign_table';
$table = new Table($foreignTableName);
$table->addColumn('id', 'integer', array('autoincrement' => true));
$table->setPrimaryKey(array('id'));
$this->_sm->dropAndCreateTable($table);
$table = new Table($primaryTableName);
$table->addColumn('id', 'integer', array('autoincrement' => true));
$table->addColumn('foo', 'integer');
$table->addColumn('bar', 'string');
$table->addForeignKeyConstraint($foreignTableName, array('foo'), array('id'));
$table->addIndex(array('bar'));
$table->setPrimaryKey(array('id'));
$this->_sm->dropAndCreateTable($table);
$this->assertEquals(
$this->_sm->listTableColumns($primaryTableName),
$this->_sm->listTableColumns($defaultSchemaName . '.' . $primaryTableName)
);
$this->assertEquals(
$this->_sm->listTableIndexes($primaryTableName),
$this->_sm->listTableIndexes($defaultSchemaName . '.' . $primaryTableName)
);
$this->assertEquals(
$this->_sm->listTableForeignKeys($primaryTableName),
$this->_sm->listTableForeignKeys($defaultSchemaName . '.' . $primaryTableName)
);
}
}
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