Skip to content

Commit

Permalink
feat: Implement compileTables missing method (#868)
Browse files Browse the repository at this point in the history
* Add method to compile table queries in OracleGrammar

A new method 'compileTables' has been added to OracleGrammar.php. This addition allows the query to be compiled to determine the tables for a database. Accompanying tests for this feature were also added in Oci8SchemaGrammarTest.php.

* Fix space

* Fix $owner comment missing space

* Update OracleGrammar to enhance table data

The OracleGrammar.php and associated test file have been updated. These modifications provide additional information, such as the schema, size, comments, and collation when compiling table data. Now, the user will have a more detailed view of their database tables.

* Update src/Oci8/Schema/Grammars/OracleGrammar.php

Co-authored-by: Arjay Angeles <aqangeles@gmail.com>

* Update OracleGrammar.php

* Normalize case and adjust size calculation in SQL query

Lowercased table names, schema, and collation parameters to ensure consistency. Also simplified the size calculation by removing the rounding and conversion to megabytes.

---------

Co-authored-by: Luca Zangheri <luca.zangheri@wuerth.it>
Co-authored-by: Arjay Angeles <aqangeles@gmail.com>
  • Loading branch information
3 people authored Jul 31, 2024
1 parent 39be2bd commit 0fe8b7e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Oci8/Schema/Grammars/OracleGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -1021,4 +1021,28 @@ public function compileForeignKeyConstraints(string $owner, string $action): str
end loop;
end;';
}

/**
* Compile the query to determine the tables.
*
* @param string $owner
* @return string
*/
public function compileTables(string $owner): string
{
return 'select lower(all_tab_comments.table_name) as "name",
lower(all_tables.owner) as "schema",
sum(user_segments.bytes) as "size",
all_tab_comments.comments as "comments",
(select lower(value) from nls_database_parameters where parameter = \'NLS_SORT\') as "collation"
from all_tables
join all_tab_comments on all_tab_comments.table_name = all_tables.table_name
left join user_segments on user_segments.segment_name = all_tables.table_name
where all_tables.owner = \''.strtoupper($owner).'\'
and all_tab_comments.owner = \''.strtoupper($owner).'\'
and all_tab_comments.table_type in (\'TABLE\')
group by all_tab_comments.table_name, all_tables.owner, all_tables.num_rows,
all_tables.avg_row_len, all_tables.blocks, all_tab_comments.comments
order by all_tab_comments.table_name';
}
}
14 changes: 14 additions & 0 deletions src/Oci8/Schema/OracleBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,18 @@ public function enableForeignKeyConstraints()
$this->grammar->compileEnableForeignKeyConstraints($this->connection->getConfig('username'))
);
}

/**
* Get the tables that belong to the database.
*
* @return array
*/
public function getTables()
{
return $this->connection->getPostProcessor()->processTables(
$this->connection->selectFromWriteConnection(
$this->grammar->compileTables($this->connection->getConfig('username'))
)
);
}
}
22 changes: 22 additions & 0 deletions tests/Database/Oci8SchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1134,4 +1134,26 @@ public function testCompileDisableForeignKeyConstraints()

$this->assertEquals($expected, $statement);
}

public function testCompileTables()
{
$statement = $this->getGrammar()->compileTables('username');

$expected = 'select lower(all_tab_comments.table_name) as "name",
lower(all_tables.owner) as "schema",
sum(user_segments.bytes) as "size",
all_tab_comments.comments as "comments",
(select lower(value) from nls_database_parameters where parameter = \'NLS_SORT\') as "collation"
from all_tables
join all_tab_comments on all_tab_comments.table_name = all_tables.table_name
left join user_segments on user_segments.segment_name = all_tables.table_name
where all_tables.owner = \'USERNAME\'
and all_tab_comments.owner = \'USERNAME\'
and all_tab_comments.table_type in (\'TABLE\')
group by all_tab_comments.table_name, all_tables.owner, all_tables.num_rows,
all_tables.avg_row_len, all_tables.blocks, all_tab_comments.comments
order by all_tab_comments.table_name';

$this->assertEquals($expected, $statement);
}
}

0 comments on commit 0fe8b7e

Please sign in to comment.