Skip to content

Commit

Permalink
Merge pull request #5580 from morozov/issues/5579
Browse files Browse the repository at this point in the history
Filter MySQL and Oracle columns by table type
  • Loading branch information
morozov authored Aug 14, 2022
2 parents cb12264 + 76c47c4 commit 36b9ffc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
29 changes: 16 additions & 13 deletions src/Schema/MySQLSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,27 +400,30 @@ protected function selectTableColumns(string $databaseName, ?string $tableName =
$sql = 'SELECT';

if ($tableName === null) {
$sql .= ' TABLE_NAME,';
$sql .= ' c.TABLE_NAME,';
}

$sql .= <<<'SQL'
COLUMN_NAME AS field,
COLUMN_TYPE AS type,
IS_NULLABLE AS `null`,
COLUMN_KEY AS `key`,
COLUMN_DEFAULT AS `default`,
EXTRA,
COLUMN_COMMENT AS comment,
CHARACTER_SET_NAME AS characterset,
COLLATION_NAME AS collation
FROM information_schema.COLUMNS
c.COLUMN_NAME AS field,
c.COLUMN_TYPE AS type,
c.IS_NULLABLE AS `null`,
c.COLUMN_KEY AS `key`,
c.COLUMN_DEFAULT AS `default`,
c.EXTRA,
c.COLUMN_COMMENT AS comment,
c.CHARACTER_SET_NAME AS characterset,
c.COLLATION_NAME AS collation
FROM information_schema.COLUMNS c
INNER JOIN information_schema.TABLES t
ON t.TABLE_SCHEMA = c.TABLE_SCHEMA
AND t.TABLE_NAME = c.TABLE_NAME
SQL;

$conditions = ['TABLE_SCHEMA = ?'];
$conditions = ['c.TABLE_SCHEMA = ?', "t.TABLE_TYPE = 'BASE TABLE'"];
$params = [$databaseName];

if ($tableName !== null) {
$conditions[] = 'TABLE_NAME = ?';
$conditions[] = 't.TABLE_NAME = ?';
$params[] = $tableName;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Schema/OracleSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ protected function selectTableColumns(string $databaseName, ?string $tableName =
C.NULLABLE,
D.COMMENTS
FROM ALL_TAB_COLUMNS C
INNER JOIN ALL_TABLES T
ON T.OWNER = C.OWNER
AND T.TABLE_NAME = C.TABLE_NAME
LEFT JOIN ALL_COL_COMMENTS D
ON D.OWNER = C.OWNER
AND D.TABLE_NAME = C.TABLE_NAME
Expand Down
29 changes: 17 additions & 12 deletions tests/Functional/Schema/SchemaManagerFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,26 @@ public function testListTables(): void
$this->createTestTable('list_tables_test');
$tables = $this->schemaManager->listTables();

$foundTable = false;
foreach ($tables as $table) {
self::assertInstanceOf(Table::class, $table);
if (strtolower($table->getName()) !== 'list_tables_test') {
continue;
}
$table = $this->findTableByName($tables, 'list_tables_test');
self::assertNotNull($table);

$foundTable = true;
self::assertTrue($table->hasColumn('id'));
self::assertTrue($table->hasColumn('test'));
self::assertTrue($table->hasColumn('foreign_key_test'));
}

self::assertTrue($table->hasColumn('id'));
self::assertTrue($table->hasColumn('test'));
self::assertTrue($table->hasColumn('foreign_key_test'));
}
public function testListTablesDoesNotIncludeViews(): void
{
$this->createTestTable('test_table_for_view');

$sql = 'SELECT * FROM test_table_for_view';

self::assertTrue($foundTable, "The 'list_tables_test' table has to be found.");
$view = new View('test_view', $sql);
$this->schemaManager->createView($view);

$tables = $this->schemaManager->listTables();
$view = $this->findTableByName($tables, 'test_view');
self::assertNull($view);
}

/**
Expand Down

0 comments on commit 36b9ffc

Please sign in to comment.