Skip to content

Commit

Permalink
[10.x] Get tables and views info (#49020)
Browse files Browse the repository at this point in the history
* add getTables and getViews

* fix tests

* fix a typo

* add more integration tests

* add more integration tests

* remove redundant schema on mysql

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
hafezdivandari and taylorotwell authored Nov 27, 2023
1 parent 2fd25f6 commit 50807db
Show file tree
Hide file tree
Showing 16 changed files with 595 additions and 244 deletions.
49 changes: 45 additions & 4 deletions src/Illuminate/Database/Query/Processors/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,44 @@ public function processInsertGetId(Builder $query, $sql, $values, $sequence = nu
}

/**
* Process the results of a column listing query.
* Process the results of a tables query.
*
* @deprecated Will be removed in a future Laravel version.
* @param array $results
* @return array
*/
public function processTables($results)
{
return array_map(function ($result) {
$result = (object) $result;

return [
'name' => $result->name,
'schema' => $result->schema ?? null, // PostgreSQL and SQL Server
'size' => isset($result->size) ? (int) $result->size : null,
'comment' => $result->comment ?? null, // MySQL and PostgreSQL
'collation' => $result->collation ?? null, // MySQL only
'engine' => $result->engine ?? null, // MySQL only
];
}, $results);
}

/**
* Process the results of a views query.
*
* @param array $results
* @return array
*/
public function processColumnListing($results)
public function processViews($results)
{
return $results;
return array_map(function ($result) {
$result = (object) $result;

return [
'name' => $result->name,
'schema' => $result->schema ?? null, // PostgreSQL and SQL Server
'definition' => $result->definition,
];
}, $results);
}

/**
Expand All @@ -59,4 +87,17 @@ public function processColumns($results)
{
return $results;
}

/**
* Process the results of a column listing query.
*
* @deprecated Will be removed in a future Laravel version.
*
* @param array $results
* @return array
*/
public function processColumnListing($results)
{
return $results;
}
}
60 changes: 45 additions & 15 deletions src/Illuminate/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,51 @@ public function hasTable($table)
{
$table = $this->connection->getTablePrefix().$table;

return count($this->connection->selectFromWriteConnection(
$this->grammar->compileTableExists(), [$table]
)) > 0;
foreach ($this->getTables() as $value) {
if (strtolower($table) === strtolower($value['name'])) {
return true;
}
}

return false;
}

/**
* Get the tables that belong to the database.
*
* @return array
*/
public function getTables()
{
return $this->connection->getPostProcessor()->processTables(
$this->connection->selectFromWriteConnection($this->grammar->compileTables())
);
}

/**
* Get the views that belong to the database.
*
* @return array
*/
public function getViews()
{
return $this->connection->getPostProcessor()->processViews(
$this->connection->selectFromWriteConnection($this->grammar->compileViews())
);
}

/**
* Get all of the table names for the database.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return array
*
* @throws \LogicException
*/
public function getAllTables()
{
throw new LogicException('This database driver does not support getting all tables.');
}

/**
Expand Down Expand Up @@ -385,18 +427,6 @@ public function dropAllTypes()
throw new LogicException('This database driver does not support dropping all types.');
}

/**
* Get all of the table names for the database.
*
* @return array
*
* @throws \LogicException
*/
public function getAllTables()
{
throw new LogicException('This database driver does not support getting all tables.');
}

/**
* Rename a table on the schema.
*
Expand Down
79 changes: 59 additions & 20 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,72 @@ public function compileDropDatabaseIfExists($name)
/**
* Compile the query to determine the list of tables.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return string
*/
public function compileTableExists()
{
return "select * from information_schema.tables where table_schema = ? and table_name = ? and table_type = 'BASE TABLE'";
}

/**
* Compile the query to determine the tables.
*
* @param string $database
* @return string
*/
public function compileTables($database)
{
return sprintf(
'select table_name as `name`, (data_length + index_length) as `size`, '
.'table_comment as `comment`, engine as `engine`, table_collation as `collation` '
."from information_schema.tables where table_schema = %s and table_type = 'BASE TABLE' "
.'order by table_name',
$this->quoteString($database)
);
}

/**
* Compile the query to determine the views.
*
* @param string $database
* @return string
*/
public function compileViews($database)
{
return sprintf(
'select table_name as `name`, view_definition as `definition` '
.'from information_schema.views where table_schema = %s '
.'order by table_name',
$this->quoteString($database)
);
}

/**
* Compile the SQL needed to retrieve all table names.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return string
*/
public function compileGetAllTables()
{
return 'SHOW FULL TABLES WHERE table_type = \'BASE TABLE\'';
}

/**
* Compile the SQL needed to retrieve all view names.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return string
*/
public function compileGetAllViews()
{
return 'SHOW FULL TABLES WHERE table_type = \'VIEW\'';
}

/**
* Compile the query to determine the list of columns.
*
Expand Down Expand Up @@ -532,26 +591,6 @@ public function compileDropAllViews($views)
return 'drop view '.implode(',', $this->wrapArray($views));
}

/**
* Compile the SQL needed to retrieve all table names.
*
* @return string
*/
public function compileGetAllTables()
{
return 'SHOW FULL TABLES WHERE table_type = \'BASE TABLE\'';
}

/**
* Compile the SQL needed to retrieve all view names.
*
* @return string
*/
public function compileGetAllViews()
{
return 'SHOW FULL TABLES WHERE table_type = \'VIEW\'';
}

/**
* Compile the command to enable foreign key constraints.
*
Expand Down
71 changes: 49 additions & 22 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,55 @@ public function compileTableExists()
return "select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'";
}

/**
* Compile the query to determine the tables.
*
* @return string
*/
public function compileTables()
{
return 'select c.relname as name, n.nspname as schema, pg_total_relation_size(c.oid) as size, '

This comment has been minimized.

Copy link
@jszoja

jszoja Aug 6, 2024

Because of introducing pg_total_relation_size() the postgres db driver no longer works for cockroachdb.

I'm not saying it is wrong, but just that starting from laravel/framework 10.34.0 projects relying on postgres driver for cockroachdb connections need to switch the driver to cockroachdb if there is any reliable one.

."obj_description(c.oid, 'pg_class') as comment from pg_class c, pg_namespace n "
."where c.relkind = 'r' and n.oid = c.relnamespace "
.'order by c.relname';
}

/**
* Compile the query to determine the views.
*
* @return string
*/
public function compileViews()
{
return 'select viewname as name, schemaname as schema, definition from pg_views order by viewname';
}

/**
* Compile the SQL needed to retrieve all table names.
*
* @deprecated Will be removed in a future Laravel version.
*
* @param string|array $searchPath
* @return string
*/
public function compileGetAllTables($searchPath)
{
return "select tablename, concat('\"', schemaname, '\".\"', tablename, '\"') as qualifiedname from pg_catalog.pg_tables where schemaname in ('".implode("','", (array) $searchPath)."')";
}

/**
* Compile the SQL needed to retrieve all view names.
*
* @deprecated Will be removed in a future Laravel version.
*
* @param string|array $searchPath
* @return string
*/
public function compileGetAllViews($searchPath)
{
return "select viewname, concat('\"', schemaname, '\".\"', viewname, '\"') as qualifiedname from pg_catalog.pg_views where schemaname in ('".implode("','", (array) $searchPath)."')";
}

/**
* Compile the query to determine the list of columns.
*
Expand Down Expand Up @@ -398,28 +447,6 @@ public function compileDropAllTypes($types)
return 'drop type '.implode(',', $this->escapeNames($types)).' cascade';
}

/**
* Compile the SQL needed to retrieve all table names.
*
* @param string|array $searchPath
* @return string
*/
public function compileGetAllTables($searchPath)
{
return "select tablename, concat('\"', schemaname, '\".\"', tablename, '\"') as qualifiedname from pg_catalog.pg_tables where schemaname in ('".implode("','", (array) $searchPath)."')";
}

/**
* Compile the SQL needed to retrieve all view names.
*
* @param string|array $searchPath
* @return string
*/
public function compileGetAllViews($searchPath)
{
return "select viewname, concat('\"', schemaname, '\".\"', viewname, '\"') as qualifiedname from pg_catalog.pg_views where schemaname in ('".implode("','", (array) $searchPath)."')";
}

/**
* Compile the SQL needed to retrieve all type names.
*
Expand Down
Loading

0 comments on commit 50807db

Please sign in to comment.