Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Get tables and views info #49020

Merged
merged 8 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, '
."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