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 2 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
41 changes: 41 additions & 0 deletions src/Illuminate/Database/Query/Processors/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,47 @@ public function processColumnListing($results)
return $results;
}

/**
* Process the results of a tables query.
*
* @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, // MySQL, 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 processViews($results)
{
return array_map(function ($result) {
$result = (object) $result;

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

/**
* Process the results of a columns query.
*
Expand Down
36 changes: 33 additions & 3 deletions src/Illuminate/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,37 @@ 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 for the database.
*
* @return array
*/
public function getTables()
{
return $this->connection->getPostProcessor()->processTables(
$this->connection->selectFromWriteConnection($this->grammar->compileTables())
);
}

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

/**
Expand Down Expand Up @@ -388,6 +416,8 @@ public function dropAllTypes()
/**
* Get all of the table names for the database.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return array
*
* @throws \LogicException
Expand Down
39 changes: 39 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,48 @@ 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`, table_schema as `schema`, (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`, table_schema as `schema`, view_definition as `definition` '
.'from information_schema.views where table_schema = %s '
.'order by table_name',
$this->quoteString($database)
);
}

/**
* Compile the query to determine the list of columns.
*
Expand Down Expand Up @@ -535,6 +570,8 @@ public function compileDropAllViews($views)
/**
* Compile the SQL needed to retrieve all table names.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return string
*/
public function compileGetAllTables()
Expand All @@ -545,6 +582,8 @@ public function compileGetAllTables()
/**
* Compile the SQL needed to retrieve all view names.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return string
*/
public function compileGetAllViews()
Expand Down
27 changes: 27 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,29 @@ 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 query to determine the list of columns.
*
Expand Down Expand Up @@ -401,6 +424,8 @@ public function compileDropAllTypes($types)
/**
* Compile the SQL needed to retrieve all table names.
*
* @deprecated Will be removed in a future Laravel version.
*
* @param string|array $searchPath
* @return string
*/
Expand All @@ -412,6 +437,8 @@ public function compileGetAllTables($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
*/
Expand Down
43 changes: 43 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,52 @@ class SQLiteGrammar extends Grammar
/**
* Compile the query to determine if a table exists.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return string
*/
public function compileTableExists()
{
return "select * from sqlite_master where type = 'table' and name = ?";
}

/**
* Compile the query to determine if the dbstat table is available.
*
* @return string
*/
public function compileDbstatExtists()
hafezdivandari marked this conversation as resolved.
Show resolved Hide resolved
{
return "select exists (select 1 from pragma_compile_options where compile_options = 'ENABLE_DBSTAT_VTAB') as enabled";
}

/**
* Compile the query to determine the tables.
*
* @param bool $withSize
* @return string
*/
public function compileTables($withSize = false)
{
return $withSize
? 'select m.tbl_name as name, sum(s.pgsize) as size from sqlite_master as m '
.'join dbstat as s on s.name = m.name '
."where m.type in ('table', 'index') and m.tbl_name not like 'sqlite_%' "
.'group by m.tbl_name '
.'order by m.tbl_name'
: "select name from sqlite_master where type = 'table' and name not like 'sqlite_%' order by name";
}

/**
* Compile the query to determine the views.
*
* @return string
*/
public function compileViews()
{
return "select name, sql as definition from sqlite_master where type = 'view' order by name";
}

/**
* Compile the query to determine the list of columns.
*
Expand Down Expand Up @@ -286,6 +325,8 @@ public function compileDropAllViews()
/**
* Compile the SQL needed to retrieve all table names.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return string
*/
public function compileGetAllTables()
Expand All @@ -296,6 +337,8 @@ public function compileGetAllTables()
/**
* Compile the SQL needed to retrieve all view names.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return string
*/
public function compileGetAllViews()
Expand Down
33 changes: 33 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,42 @@ public function compileDropDatabaseIfExists($name)
/**
* Compile the query to determine if a table exists.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return string
*/
public function compileTableExists()
{
return "select * from sys.sysobjects where id = object_id(?) and xtype in ('U', 'V')";
}

/**
* Compile the query to determine the tables.
*
* @return string
*/
public function compileTables()
{
return 'select t.name as name, SCHEMA_NAME(t.schema_id) as [schema], sum(u.total_pages) * 8 * 1024 as size '
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hafezdivandari Why are you multiplying by 8 specifically here?

Copy link
Contributor Author

@hafezdivandari hafezdivandari Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because each page is 8 KB according to docs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hafezdivandari But why not just multiply by 8192 directly instead then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because that's more readable and easier to understand imo.

.'from sys.tables as t '
.'join sys.partitions as p on p.object_id = t.object_id '
.'join sys.allocation_units as u on u.container_id = p.hobt_id '
.'group by t.name, t.schema_id '
.'order by t.name';
}

/**
* Compile the query to determine the views.
*
* @return string
*/
public function compileViews()
{
return 'select name, SCHEMA_NAME(v.schema_id) as [schema], definition from sys.views as v '
.'inner join sys.sql_modules as m on v.object_id = m.object_id '
.'order by name';
}

/**
* Compile the query to determine the list of columns.
*
Expand Down Expand Up @@ -507,6 +536,8 @@ public function compileDropAllViews()
/**
* Compile the SQL needed to retrieve all table names.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return string
*/
public function compileGetAllTables()
Expand All @@ -517,6 +548,8 @@ public function compileGetAllTables()
/**
* Compile the SQL needed to retrieve all view names.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return string
*/
public function compileGetAllViews()
Expand Down
Loading