Skip to content

Commit 50807db

Browse files
[10.x] Get tables and views info (#49020)
* 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]>
1 parent 2fd25f6 commit 50807db

16 files changed

+595
-244
lines changed

src/Illuminate/Database/Query/Processors/Processor.php

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,44 @@ public function processInsertGetId(Builder $query, $sql, $values, $sequence = nu
3737
}
3838

3939
/**
40-
* Process the results of a column listing query.
40+
* Process the results of a tables query.
4141
*
42-
* @deprecated Will be removed in a future Laravel version.
42+
* @param array $results
43+
* @return array
44+
*/
45+
public function processTables($results)
46+
{
47+
return array_map(function ($result) {
48+
$result = (object) $result;
49+
50+
return [
51+
'name' => $result->name,
52+
'schema' => $result->schema ?? null, // PostgreSQL and SQL Server
53+
'size' => isset($result->size) ? (int) $result->size : null,
54+
'comment' => $result->comment ?? null, // MySQL and PostgreSQL
55+
'collation' => $result->collation ?? null, // MySQL only
56+
'engine' => $result->engine ?? null, // MySQL only
57+
];
58+
}, $results);
59+
}
60+
61+
/**
62+
* Process the results of a views query.
4363
*
4464
* @param array $results
4565
* @return array
4666
*/
47-
public function processColumnListing($results)
67+
public function processViews($results)
4868
{
49-
return $results;
69+
return array_map(function ($result) {
70+
$result = (object) $result;
71+
72+
return [
73+
'name' => $result->name,
74+
'schema' => $result->schema ?? null, // PostgreSQL and SQL Server
75+
'definition' => $result->definition,
76+
];
77+
}, $results);
5078
}
5179

5280
/**
@@ -59,4 +87,17 @@ public function processColumns($results)
5987
{
6088
return $results;
6189
}
90+
91+
/**
92+
* Process the results of a column listing query.
93+
*
94+
* @deprecated Will be removed in a future Laravel version.
95+
*
96+
* @param array $results
97+
* @return array
98+
*/
99+
public function processColumnListing($results)
100+
{
101+
return $results;
102+
}
62103
}

src/Illuminate/Database/Schema/Builder.php

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,51 @@ public function hasTable($table)
159159
{
160160
$table = $this->connection->getTablePrefix().$table;
161161

162-
return count($this->connection->selectFromWriteConnection(
163-
$this->grammar->compileTableExists(), [$table]
164-
)) > 0;
162+
foreach ($this->getTables() as $value) {
163+
if (strtolower($table) === strtolower($value['name'])) {
164+
return true;
165+
}
166+
}
167+
168+
return false;
169+
}
170+
171+
/**
172+
* Get the tables that belong to the database.
173+
*
174+
* @return array
175+
*/
176+
public function getTables()
177+
{
178+
return $this->connection->getPostProcessor()->processTables(
179+
$this->connection->selectFromWriteConnection($this->grammar->compileTables())
180+
);
181+
}
182+
183+
/**
184+
* Get the views that belong to the database.
185+
*
186+
* @return array
187+
*/
188+
public function getViews()
189+
{
190+
return $this->connection->getPostProcessor()->processViews(
191+
$this->connection->selectFromWriteConnection($this->grammar->compileViews())
192+
);
193+
}
194+
195+
/**
196+
* Get all of the table names for the database.
197+
*
198+
* @deprecated Will be removed in a future Laravel version.
199+
*
200+
* @return array
201+
*
202+
* @throws \LogicException
203+
*/
204+
public function getAllTables()
205+
{
206+
throw new LogicException('This database driver does not support getting all tables.');
165207
}
166208

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

388-
/**
389-
* Get all of the table names for the database.
390-
*
391-
* @return array
392-
*
393-
* @throws \LogicException
394-
*/
395-
public function getAllTables()
396-
{
397-
throw new LogicException('This database driver does not support getting all tables.');
398-
}
399-
400430
/**
401431
* Rename a table on the schema.
402432
*

src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,72 @@ public function compileDropDatabaseIfExists($name)
6868
/**
6969
* Compile the query to determine the list of tables.
7070
*
71+
* @deprecated Will be removed in a future Laravel version.
72+
*
7173
* @return string
7274
*/
7375
public function compileTableExists()
7476
{
7577
return "select * from information_schema.tables where table_schema = ? and table_name = ? and table_type = 'BASE TABLE'";
7678
}
7779

80+
/**
81+
* Compile the query to determine the tables.
82+
*
83+
* @param string $database
84+
* @return string
85+
*/
86+
public function compileTables($database)
87+
{
88+
return sprintf(
89+
'select table_name as `name`, (data_length + index_length) as `size`, '
90+
.'table_comment as `comment`, engine as `engine`, table_collation as `collation` '
91+
."from information_schema.tables where table_schema = %s and table_type = 'BASE TABLE' "
92+
.'order by table_name',
93+
$this->quoteString($database)
94+
);
95+
}
96+
97+
/**
98+
* Compile the query to determine the views.
99+
*
100+
* @param string $database
101+
* @return string
102+
*/
103+
public function compileViews($database)
104+
{
105+
return sprintf(
106+
'select table_name as `name`, view_definition as `definition` '
107+
.'from information_schema.views where table_schema = %s '
108+
.'order by table_name',
109+
$this->quoteString($database)
110+
);
111+
}
112+
113+
/**
114+
* Compile the SQL needed to retrieve all table names.
115+
*
116+
* @deprecated Will be removed in a future Laravel version.
117+
*
118+
* @return string
119+
*/
120+
public function compileGetAllTables()
121+
{
122+
return 'SHOW FULL TABLES WHERE table_type = \'BASE TABLE\'';
123+
}
124+
125+
/**
126+
* Compile the SQL needed to retrieve all view names.
127+
*
128+
* @deprecated Will be removed in a future Laravel version.
129+
*
130+
* @return string
131+
*/
132+
public function compileGetAllViews()
133+
{
134+
return 'SHOW FULL TABLES WHERE table_type = \'VIEW\'';
135+
}
136+
78137
/**
79138
* Compile the query to determine the list of columns.
80139
*
@@ -532,26 +591,6 @@ public function compileDropAllViews($views)
532591
return 'drop view '.implode(',', $this->wrapArray($views));
533592
}
534593

535-
/**
536-
* Compile the SQL needed to retrieve all table names.
537-
*
538-
* @return string
539-
*/
540-
public function compileGetAllTables()
541-
{
542-
return 'SHOW FULL TABLES WHERE table_type = \'BASE TABLE\'';
543-
}
544-
545-
/**
546-
* Compile the SQL needed to retrieve all view names.
547-
*
548-
* @return string
549-
*/
550-
public function compileGetAllViews()
551-
{
552-
return 'SHOW FULL TABLES WHERE table_type = \'VIEW\'';
553-
}
554-
555594
/**
556595
* Compile the command to enable foreign key constraints.
557596
*

src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,55 @@ public function compileTableExists()
7878
return "select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'";
7979
}
8080

81+
/**
82+
* Compile the query to determine the tables.
83+
*
84+
* @return string
85+
*/
86+
public function compileTables()
87+
{
88+
return 'select c.relname as name, n.nspname as schema, pg_total_relation_size(c.oid) as size, '
89+
."obj_description(c.oid, 'pg_class') as comment from pg_class c, pg_namespace n "
90+
."where c.relkind = 'r' and n.oid = c.relnamespace "
91+
.'order by c.relname';
92+
}
93+
94+
/**
95+
* Compile the query to determine the views.
96+
*
97+
* @return string
98+
*/
99+
public function compileViews()
100+
{
101+
return 'select viewname as name, schemaname as schema, definition from pg_views order by viewname';
102+
}
103+
104+
/**
105+
* Compile the SQL needed to retrieve all table names.
106+
*
107+
* @deprecated Will be removed in a future Laravel version.
108+
*
109+
* @param string|array $searchPath
110+
* @return string
111+
*/
112+
public function compileGetAllTables($searchPath)
113+
{
114+
return "select tablename, concat('\"', schemaname, '\".\"', tablename, '\"') as qualifiedname from pg_catalog.pg_tables where schemaname in ('".implode("','", (array) $searchPath)."')";
115+
}
116+
117+
/**
118+
* Compile the SQL needed to retrieve all view names.
119+
*
120+
* @deprecated Will be removed in a future Laravel version.
121+
*
122+
* @param string|array $searchPath
123+
* @return string
124+
*/
125+
public function compileGetAllViews($searchPath)
126+
{
127+
return "select viewname, concat('\"', schemaname, '\".\"', viewname, '\"') as qualifiedname from pg_catalog.pg_views where schemaname in ('".implode("','", (array) $searchPath)."')";
128+
}
129+
81130
/**
82131
* Compile the query to determine the list of columns.
83132
*
@@ -398,28 +447,6 @@ public function compileDropAllTypes($types)
398447
return 'drop type '.implode(',', $this->escapeNames($types)).' cascade';
399448
}
400449

401-
/**
402-
* Compile the SQL needed to retrieve all table names.
403-
*
404-
* @param string|array $searchPath
405-
* @return string
406-
*/
407-
public function compileGetAllTables($searchPath)
408-
{
409-
return "select tablename, concat('\"', schemaname, '\".\"', tablename, '\"') as qualifiedname from pg_catalog.pg_tables where schemaname in ('".implode("','", (array) $searchPath)."')";
410-
}
411-
412-
/**
413-
* Compile the SQL needed to retrieve all view names.
414-
*
415-
* @param string|array $searchPath
416-
* @return string
417-
*/
418-
public function compileGetAllViews($searchPath)
419-
{
420-
return "select viewname, concat('\"', schemaname, '\".\"', viewname, '\"') as qualifiedname from pg_catalog.pg_views where schemaname in ('".implode("','", (array) $searchPath)."')";
421-
}
422-
423450
/**
424451
* Compile the SQL needed to retrieve all type names.
425452
*

0 commit comments

Comments
 (0)