Skip to content

Commit 827893f

Browse files
jhavenzjhavens-rcntaylorotwell
authored
[9.x] Add getAllTables support for SQLite and SQLServer schema builders (#41896)
* add sqlite and sqlserver table column getters * formatting Co-authored-by: Jonathan Havens <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent fab6107 commit 827893f

File tree

6 files changed

+267
-1
lines changed

6 files changed

+267
-1
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,26 @@ public function compileDropAllViews()
246246
return "delete from sqlite_master where type in ('view')";
247247
}
248248

249+
/**
250+
* Compile the SQL needed to retrieve all table names.
251+
*
252+
* @return string
253+
*/
254+
public function compileGetAllTables()
255+
{
256+
return 'select type, name from sqlite_master where type = \'table\' and name not like \'sqlite_%\'';
257+
}
258+
259+
/**
260+
* Compile the SQL needed to retrieve all view names.
261+
*
262+
* @return string
263+
*/
264+
public function compileGetAllViews()
265+
{
266+
return 'select type, name from sqlite_master where type = \'view\'';
267+
}
268+
249269
/**
250270
* Compile the SQL needed to rebuild the database.
251271
*

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,27 @@ public function compileDropAllViews()
393393
EXEC sp_executesql @sql;";
394394
}
395395

396+
/**
397+
* Compile the SQL needed to retrieve all table names.
398+
*
399+
* @return string
400+
*/
401+
public function compileGetAllTables()
402+
{
403+
return "select name, type from sys.tables where type = 'U'";
404+
}
405+
406+
/**
407+
* Compile the SQL needed to retrieve all view names.
408+
*
409+
* @return string
410+
*/
411+
public function compileGetAllViews()
412+
{
413+
return "select name, type from sys.objects where type = 'V'";
414+
}
415+
416+
396417
/**
397418
* Create the column definition for a char type.
398419
*

src/Illuminate/Database/Schema/SQLiteBuilder.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,30 @@ public function dropAllViews()
6666
$this->connection->select($this->grammar->compileRebuild());
6767
}
6868

69+
/**
70+
* Get all of the table names for the database.
71+
*
72+
* @return array
73+
*/
74+
public function getAllTables()
75+
{
76+
return $this->connection->select(
77+
$this->grammar->compileGetAllTables()
78+
);
79+
}
80+
81+
/**
82+
* Get all of the view names for the database.
83+
*
84+
* @return array
85+
*/
86+
public function getAllViews()
87+
{
88+
return $this->connection->select(
89+
$this->grammar->compileGetAllViews()
90+
);
91+
}
92+
6993
/**
7094
* Empty the database file.
7195
*

src/Illuminate/Database/Schema/SqlServerBuilder.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function dropDatabaseIfExists($name)
3030
);
3131
}
3232

33-
/**
33+
/**
3434
* Drop all tables from the database.
3535
*
3636
* @return void
@@ -51,4 +51,28 @@ public function dropAllViews()
5151
{
5252
$this->connection->statement($this->grammar->compileDropAllViews());
5353
}
54+
55+
/**
56+
* Drop all tables from the database.
57+
*
58+
* @return array
59+
*/
60+
public function getAllTables()
61+
{
62+
return $this->connection->select(
63+
$this->grammar->compileGetAllTables()
64+
);
65+
}
66+
67+
/**
68+
* Get all of the view names for the database.
69+
*
70+
* @return array
71+
*/
72+
public function getAllViews()
73+
{
74+
return $this->connection->select(
75+
$this->grammar->compileGetAllViews()
76+
);
77+
}
5478
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Database\SqlServer;
4+
5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\DB;
7+
use Illuminate\Support\Facades\Schema;
8+
use stdClass;
9+
10+
class DatabaseSqlServerSchemaBuilderTest extends SqlServerTestCase
11+
{
12+
protected function getEnvironmentSetUp($app)
13+
{
14+
if (getenv('DB_CONNECTION') !== 'sqlsrv') {
15+
$this->markTestSkipped('Test requires a SQLServer connection.');
16+
}
17+
18+
$this->driver = 'sqlsrv';
19+
}
20+
21+
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
22+
{
23+
Schema::create('users', function (Blueprint $table) {
24+
$table->integer('id');
25+
$table->string('name');
26+
$table->string('age');
27+
$table->enum('color', ['red', 'blue']);
28+
});
29+
}
30+
31+
protected function destroyDatabaseMigrations()
32+
{
33+
Schema::drop('users');
34+
}
35+
36+
public function testGetAllTablesAndColumnListing()
37+
{
38+
$tables = Schema::getAllTables();
39+
40+
$this->assertCount(2, $tables);
41+
$tableProperties = array_values((array) $tables[0]);
42+
$this->assertEquals(['migrations', 'U '], $tableProperties);
43+
44+
$this->assertInstanceOf(stdClass::class, $tables[1]);
45+
46+
$tableProperties = array_values((array) $tables[1]);
47+
$this->assertEquals(['users', 'U '], $tableProperties);
48+
49+
$columns = Schema::getColumnListing('users');
50+
51+
foreach (['id', 'name', 'age', 'color'] as $column) {
52+
$this->assertContains($column, $columns);
53+
}
54+
55+
Schema::create('posts', function (Blueprint $table) {
56+
$table->integer('id');
57+
$table->string('title');
58+
});
59+
$tables = Schema::getAllTables();
60+
$this->assertCount(3, $tables);
61+
Schema::drop('posts');
62+
}
63+
64+
public function testGetAllViews()
65+
{
66+
DB::connection('sqlsrv')->statement(<<<SQL
67+
CREATE VIEW users_view
68+
AS
69+
SELECT name,age from users;
70+
SQL);
71+
72+
$tableView = Schema::getAllViews();
73+
74+
$this->assertCount(1, $tableView);
75+
$this->assertInstanceOf(stdClass::class, $obj = array_values($tableView)[0]);
76+
$this->assertEquals('users_view', $obj->name);
77+
$this->assertEquals('V ', $obj->type);
78+
79+
DB::connection('sqlsrv')->statement(<<<SQL
80+
DROP VIEW IF EXISTS users_view;
81+
SQL);
82+
83+
$this->assertEmpty(Schema::getAllViews());
84+
}
85+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Database\Sqlite;
4+
5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\DB;
7+
use Illuminate\Support\Facades\Schema;
8+
use Illuminate\Tests\Integration\Database\DatabaseTestCase;
9+
use stdClass;
10+
11+
class DatabaseSqliteSchemaBuilderTest extends DatabaseTestCase
12+
{
13+
protected function getEnvironmentSetUp($app)
14+
{
15+
if (getenv('DB_CONNECTION') !== 'testing') {
16+
$this->markTestSkipped('Test requires a Sqlite connection.');
17+
}
18+
19+
$app['config']->set('database.default', 'conn1');
20+
21+
$app['config']->set('database.connections.conn1', [
22+
'driver' => 'sqlite',
23+
'database' => ':memory:',
24+
'prefix' => '',
25+
]);
26+
}
27+
28+
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
29+
{
30+
Schema::create('users', function (Blueprint $table) {
31+
$table->integer('id');
32+
$table->string('name');
33+
$table->string('age');
34+
$table->enum('color', ['red', 'blue']);
35+
});
36+
}
37+
38+
protected function destroyDatabaseMigrations()
39+
{
40+
Schema::drop('users');
41+
}
42+
43+
public function testGetAllTablesAndColumnListing()
44+
{
45+
$tables = Schema::getAllTables();
46+
47+
$this->assertCount(2, $tables);
48+
$tableProperties = array_values((array) $tables[0]);
49+
$this->assertEquals(['table', 'migrations'], $tableProperties);
50+
51+
$this->assertInstanceOf(stdClass::class, $tables[1]);
52+
53+
$tableProperties = array_values((array) $tables[1]);
54+
$this->assertEquals(['table', 'users'], $tableProperties);
55+
56+
$columns = Schema::getColumnListing('users');
57+
58+
foreach (['id', 'name', 'age', 'color'] as $column) {
59+
$this->assertContains($column, $columns);
60+
}
61+
62+
Schema::create('posts', function (Blueprint $table) {
63+
$table->integer('id');
64+
$table->string('title');
65+
});
66+
$tables = Schema::getAllTables();
67+
$this->assertCount(3, $tables);
68+
Schema::drop('posts');
69+
}
70+
71+
public function testGetAllViews()
72+
{
73+
DB::connection('conn1')->statement(<<<SQL
74+
CREATE VIEW users_view
75+
AS
76+
SELECT name,age from users;
77+
SQL);
78+
79+
$tableView = Schema::getAllViews();
80+
81+
$this->assertCount(1, $tableView);
82+
$this->assertInstanceOf(stdClass::class, $obj = array_values($tableView)[0]);
83+
$this->assertEquals('users_view', $obj->name);
84+
$this->assertEquals('view', $obj->type);
85+
86+
DB::connection('conn1')->statement(<<<SQL
87+
DROP VIEW IF EXISTS users_view;
88+
SQL);
89+
90+
$this->assertEmpty(Schema::getAllViews());
91+
}
92+
}

0 commit comments

Comments
 (0)