Skip to content

Commit c4ba519

Browse files
add getTables and getViews
1 parent 444141b commit c4ba519

14 files changed

+332
-52
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,47 @@ public function processColumnListing($results)
4949
return $results;
5050
}
5151

52+
/**
53+
* Process the results of a tables query.
54+
*
55+
* @param array $results
56+
* @return array
57+
*/
58+
public function processTables($results)
59+
{
60+
return array_map(function ($result) {
61+
$result = (object) $result;
62+
63+
return [
64+
'name' => $result->name,
65+
'schema' => $result->schema ?? null, // MySQL, PostgreSQL, and SQL Server
66+
'size' => isset($result->size) ? (int) $result->size : null,
67+
'comment' => $result->comment ?? null, // MySQL and PostgreSQL
68+
'collation' => $result->collation ?? null, // MySQL only
69+
'engine' => $result->engine ?? null, // MySQL only
70+
];
71+
}, $results);
72+
}
73+
74+
/**
75+
* Process the results of a views query.
76+
*
77+
* @param array $results
78+
* @return array
79+
*/
80+
public function processViews($results)
81+
{
82+
return array_map(function ($result) {
83+
$result = (object) $result;
84+
85+
return [
86+
'name' => $result->name,
87+
'schema' => $result->schema ?? null, // MySQL, PostgreSQL, and SQL Server
88+
'definition' => $result->definition,
89+
];
90+
}, $results);
91+
}
92+
5293
/**
5394
* Process the results of a columns query.
5495
*

src/Illuminate/Database/Schema/Builder.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,37 @@ 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 for 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 for 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+
);
165193
}
166194

167195
/**
@@ -390,6 +418,8 @@ public function dropAllTypes()
390418
/**
391419
* Get all of the table names for the database.
392420
*
421+
* @deprecated Will be removed in a future Laravel version.
422+
*
393423
* @return array
394424
*
395425
* @throws \LogicException

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,48 @@ 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`, table_schema as `schema`, (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`, table_schema as `schema`, 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+
78113
/**
79114
* Compile the query to determine the list of columns.
80115
*
@@ -535,6 +570,8 @@ public function compileDropAllViews($views)
535570
/**
536571
* Compile the SQL needed to retrieve all table names.
537572
*
573+
* @deprecated Will be removed in a future Laravel version.
574+
*
538575
* @return string
539576
*/
540577
public function compileGetAllTables()
@@ -545,6 +582,8 @@ public function compileGetAllTables()
545582
/**
546583
* Compile the SQL needed to retrieve all view names.
547584
*
585+
* @deprecated Will be removed in a future Laravel version.
586+
*
548587
* @return string
549588
*/
550589
public function compileGetAllViews()

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,29 @@ 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+
81104
/**
82105
* Compile the query to determine the list of columns.
83106
*
@@ -401,6 +424,8 @@ public function compileDropAllTypes($types)
401424
/**
402425
* Compile the SQL needed to retrieve all table names.
403426
*
427+
* @deprecated Will be removed in a future Laravel version.
428+
*
404429
* @param string|array $searchPath
405430
* @return string
406431
*/
@@ -412,6 +437,8 @@ public function compileGetAllTables($searchPath)
412437
/**
413438
* Compile the SQL needed to retrieve all view names.
414439
*
440+
* @deprecated Will be removed in a future Laravel version.
441+
*
415442
* @param string|array $searchPath
416443
* @return string
417444
*/

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,52 @@ class SQLiteGrammar extends Grammar
3030
/**
3131
* Compile the query to determine if a table exists.
3232
*
33+
* @deprecated Will be removed in a future Laravel version.
34+
*
3335
* @return string
3436
*/
3537
public function compileTableExists()
3638
{
3739
return "select * from sqlite_master where type = 'table' and name = ?";
3840
}
3941

42+
/**
43+
* Compile the query to determine if the dbstat table is available.
44+
*
45+
* @return string
46+
*/
47+
public function compileDbstatExtists()
48+
{
49+
return "select exists (select 1 from pragma_compile_options where compile_options = 'ENABLE_DBSTAT_VTAB') as enabled";
50+
}
51+
52+
/**
53+
* Compile the query to determine the tables.
54+
*
55+
* @param bool $withSize
56+
* @return string
57+
*/
58+
public function compileTables($withSize = false)
59+
{
60+
return $withSize
61+
? 'select m.tbl_name as name, sum(s.pgsize) as size from sqlite_master as m '
62+
.'join dbstat as s on s.name = m.name '
63+
."where m.type in ('table', 'index') and m.tbl_name not like 'sqlite_%' "
64+
.'group by m.tbl_name '
65+
.'order by m.tbl_name'
66+
: "select name from sqlite_master where type = 'table' and name not like 'sqlite_%' order by name";
67+
}
68+
69+
/**
70+
* Compile the query to determine the views.
71+
*
72+
* @return string
73+
*/
74+
public function compileViews()
75+
{
76+
return "select name, sql as definition from sqlite_master where type = 'view' order by name";
77+
}
78+
4079
/**
4180
* Compile the query to determine the list of columns.
4281
*
@@ -287,6 +326,8 @@ public function compileDropAllViews()
287326
/**
288327
* Compile the SQL needed to retrieve all table names.
289328
*
329+
* @deprecated Will be removed in a future Laravel version.
330+
*
290331
* @return string
291332
*/
292333
public function compileGetAllTables()
@@ -297,6 +338,8 @@ public function compileGetAllTables()
297338
/**
298339
* Compile the SQL needed to retrieve all view names.
299340
*
341+
* @deprecated Will be removed in a future Laravel version.
342+
*
300343
* @return string
301344
*/
302345
public function compileGetAllViews()

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,42 @@ public function compileDropDatabaseIfExists($name)
6969
/**
7070
* Compile the query to determine if a table exists.
7171
*
72+
* @deprecated Will be removed in a future Laravel version.
73+
*
7274
* @return string
7375
*/
7476
public function compileTableExists()
7577
{
7678
return "select * from sys.sysobjects where id = object_id(?) and xtype in ('U', 'V')";
7779
}
7880

81+
/**
82+
* Compile the query to determine the tables.
83+
*
84+
* @return string
85+
*/
86+
public function compileTables()
87+
{
88+
return 'select t.name as name, SCHEMA_NAME(t.schema_id) as [schema], sum(u.total_pages) * 8 * 1024 as size '
89+
.'from sys.tables as t '
90+
.'join sys.partitions as p on p.object_id = t.object_id '
91+
.'join sys.allocation_units as u on u.container_id = p.hobt_id '
92+
.'group by t.name '
93+
.'order by t.name';
94+
}
95+
96+
/**
97+
* Compile the query to determine the views.
98+
*
99+
* @return string
100+
*/
101+
public function compileViews()
102+
{
103+
return 'select name, SCHEMA_NAME(v.schema_id) as [schema], definition from sys.views as v '
104+
.'inner join sys.sql_modules as m on v.object_id = m.object_id '
105+
.'order by name';
106+
}
107+
79108
/**
80109
* Compile the query to determine the list of columns.
81110
*
@@ -507,6 +536,8 @@ public function compileDropAllViews()
507536
/**
508537
* Compile the SQL needed to retrieve all table names.
509538
*
539+
* @deprecated Will be removed in a future Laravel version.
540+
*
510541
* @return string
511542
*/
512543
public function compileGetAllTables()
@@ -517,6 +548,8 @@ public function compileGetAllTables()
517548
/**
518549
* Compile the SQL needed to retrieve all view names.
519550
*
551+
* @deprecated Will be removed in a future Laravel version.
552+
*
520553
* @return string
521554
*/
522555
public function compileGetAllViews()

0 commit comments

Comments
 (0)