Skip to content

Commit 0e8b0c7

Browse files
committed
feat: add support for MySQL, MariaDB, and SQL Server
1 parent 7426fca commit 0e8b0c7

11 files changed

+199
-12
lines changed

src/Grammars/MariaDbGrammar.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CalebDW\SqlEntities\Grammars;
6+
7+
use CalebDW\SqlEntities\View;
8+
use Override;
9+
10+
class MariaDbGrammar extends Grammar
11+
{
12+
#[Override]
13+
protected function compileViewCreate(View $entity): string
14+
{
15+
return <<<SQL
16+
CREATE VIEW {$entity->name()} AS
17+
{$entity}
18+
SQL;
19+
}
20+
21+
#[Override]
22+
protected function compileViewDrop(View $entity): string
23+
{
24+
return <<<SQL
25+
DROP VIEW IF EXISTS {$entity->name()}
26+
SQL;
27+
}
28+
}

src/Grammars/MySqlGrammar.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CalebDW\SqlEntities\Grammars;
6+
7+
use CalebDW\SqlEntities\View;
8+
use Override;
9+
10+
class MySqlGrammar extends Grammar
11+
{
12+
#[Override]
13+
protected function compileViewCreate(View $entity): string
14+
{
15+
return <<<SQL
16+
CREATE VIEW {$entity->name()} AS
17+
{$entity}
18+
SQL;
19+
}
20+
21+
#[Override]
22+
protected function compileViewDrop(View $entity): string
23+
{
24+
return <<<SQL
25+
DROP VIEW IF EXISTS {$entity->name()}
26+
SQL;
27+
}
28+
}

src/Grammars/PostgresGrammar.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class PostgresGrammar extends Grammar
1313
protected function compileViewCreate(View $entity): string
1414
{
1515
return <<<SQL
16-
CREATE OR REPLACE VIEW {$entity->name()} AS
16+
CREATE VIEW {$entity->name()} AS
1717
{$entity}
1818
SQL;
1919
}

src/Grammars/SqlServerGrammar.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CalebDW\SqlEntities\Grammars;
6+
7+
use CalebDW\SqlEntities\View;
8+
use Override;
9+
10+
class SqlServerGrammar extends Grammar
11+
{
12+
#[Override]
13+
protected function compileViewCreate(View $entity): string
14+
{
15+
return <<<SQL
16+
CREATE VIEW {$entity->name()} AS
17+
{$entity}
18+
SQL;
19+
}
20+
21+
#[Override]
22+
protected function compileViewDrop(View $entity): string
23+
{
24+
return <<<SQL
25+
DROP VIEW IF EXISTS {$entity->name()}
26+
SQL;
27+
}
28+
}

src/SqlEntityManager.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
use CalebDW\SqlEntities\Contracts\SqlEntity;
88
use CalebDW\SqlEntities\Grammars\Grammar;
9+
use CalebDW\SqlEntities\Grammars\MariaDbGrammar;
10+
use CalebDW\SqlEntities\Grammars\MySqlGrammar;
911
use CalebDW\SqlEntities\Grammars\PostgresGrammar;
1012
use CalebDW\SqlEntities\Grammars\SQLiteGrammar;
13+
use CalebDW\SqlEntities\Grammars\SqlServerGrammar;
1114
use Illuminate\Database\Connection;
1215
use Illuminate\Database\DatabaseManager;
1316
use Illuminate\Support\Collection;
@@ -136,9 +139,12 @@ protected function grammar(Connection $connection): Grammar
136139
protected function createGrammar(string $driver, Connection $connection): Grammar
137140
{
138141
return match ($driver) {
139-
'sqlite' => new SQLiteGrammar($connection),
140-
'pgsql' => new PostgresGrammar($connection),
141-
default => throw new InvalidArgumentException(
142+
'mariadb' => new MariaDbGrammar($connection),
143+
'mysql' => new MySqlGrammar($connection),
144+
'pgsql' => new PostgresGrammar($connection),
145+
'sqlite' => new SQLiteGrammar($connection),
146+
'sqlsrv' => new SqlServerGrammar($connection),
147+
default => throw new InvalidArgumentException(
142148
"Unsupported driver [{$driver}].",
143149
),
144150
};

tests/Feature/SqlEntityManagerTest.php

+14-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
use Workbench\Database\Entities\views\FooConnectionUserView;
1111
use Workbench\Database\Entities\views\UserView;
1212

13+
dataset('drivers', [
14+
'mariadb' => 'mariadb',
15+
'mysql' => 'mysql',
16+
'pgsql' => 'pgsql',
17+
'sqlite' => 'sqlite',
18+
'sqlsrv' => 'sqlsrv',
19+
]);
20+
1321
beforeEach(function () {
1422
test()->connection = test()->mock(Connection::class);
1523

@@ -49,15 +57,15 @@
4957
});
5058

5159
describe('create', function () {
52-
it('creates an entity', function (string|SqlEntity $entity) {
60+
it('creates an entity', function (string $driver, string|SqlEntity $entity) {
5361
test()->connection
54-
->shouldReceive('getDriverName')->once()->andReturn('sqlite')
62+
->shouldReceive('getDriverName')->once()->andReturn($driver)
5563
->shouldReceive('statement')
5664
->once()
5765
->withArgs(fn ($sql) => str_contains($sql, 'CREATE VIEW'));
5866

5967
test()->manager->create($entity);
60-
})->with([
68+
})->with('drivers')->with([
6169
'name' => 'user_view',
6270
'class' => UserView::class,
6371
'entity' => new UserView(),
@@ -76,15 +84,15 @@
7684
});
7785

7886
describe('drop', function () {
79-
it('drops an entity', function (string|SqlEntity $entity) {
87+
it('drops an entity', function (string $driver, string|SqlEntity $entity) {
8088
test()->connection
81-
->shouldReceive('getDriverName')->once()->andReturn('pgsql')
89+
->shouldReceive('getDriverName')->once()->andReturn($driver)
8290
->shouldReceive('statement')
8391
->once()
8492
->withArgs(fn ($sql) => str_contains($sql, 'DROP VIEW'));
8593

8694
test()->manager->drop($entity);
87-
})->with([
95+
})->with('drivers')->with([
8896
'name' => 'user_view',
8997
'class' => UserView::class,
9098
'entity' => new UserView(),
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use CalebDW\SqlEntities\Grammars\MariaDbGrammar;
6+
use Illuminate\Database\Connection;
7+
use Workbench\Database\Entities\views\UserView;
8+
9+
beforeEach(function () {
10+
$connection = Mockery::mock(Connection::class);
11+
12+
test()->grammar = new MariaDbGrammar($connection);
13+
});
14+
15+
it('compiles view drop', function () {
16+
$sql = test()->grammar->compileCreate(new UserView());
17+
18+
expect($sql)->toBe(<<<'SQL'
19+
CREATE VIEW user_view AS
20+
SELECT id, name FROM users
21+
SQL);
22+
});
23+
24+
it('compiles view create', function () {
25+
$sql = test()->grammar->compileDrop(new UserView());
26+
27+
expect($sql)->toBe(<<<'SQL'
28+
DROP VIEW IF EXISTS user_view
29+
SQL);
30+
});
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use CalebDW\SqlEntities\Grammars\MySqlGrammar;
6+
use Illuminate\Database\Connection;
7+
use Workbench\Database\Entities\views\UserView;
8+
9+
beforeEach(function () {
10+
$connection = Mockery::mock(Connection::class);
11+
12+
test()->grammar = new MySqlGrammar($connection);
13+
});
14+
15+
it('compiles view drop', function () {
16+
$sql = test()->grammar->compileCreate(new UserView());
17+
18+
expect($sql)->toBe(<<<'SQL'
19+
CREATE VIEW user_view AS
20+
SELECT id, name FROM users
21+
SQL);
22+
});
23+
24+
it('compiles view create', function () {
25+
$sql = test()->grammar->compileDrop(new UserView());
26+
27+
expect($sql)->toBe(<<<'SQL'
28+
DROP VIEW IF EXISTS user_view
29+
SQL);
30+
});

tests/Unit/Grammars/PostgresGrammarTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
$sql = test()->grammar->compileCreate(new UserView());
1717

1818
expect($sql)->toBe(<<<'SQL'
19-
CREATE OR REPLACE VIEW user_view AS
19+
CREATE VIEW user_view AS
2020
SELECT id, name FROM users
2121
SQL);
2222
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use CalebDW\SqlEntities\Grammars\SqlServerGrammar;
6+
use Illuminate\Database\Connection;
7+
use Workbench\Database\Entities\views\UserView;
8+
9+
beforeEach(function () {
10+
$connection = Mockery::mock(Connection::class);
11+
12+
test()->grammar = new SqlServerGrammar($connection);
13+
});
14+
15+
it('compiles view drop', function () {
16+
$sql = test()->grammar->compileCreate(new UserView());
17+
18+
expect($sql)->toBe(<<<'SQL'
19+
CREATE VIEW user_view AS
20+
SELECT id, name FROM users
21+
SQL);
22+
});
23+
24+
it('compiles view create', function () {
25+
$sql = test()->grammar->compileDrop(new UserView());
26+
27+
expect($sql)->toBe(<<<'SQL'
28+
DROP VIEW IF EXISTS user_view
29+
SQL);
30+
});

tests/Unit/Listeners/SyncEntitiesSubscriberTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use CalebDW\SqlEntities\Listeners\SyncSqlEntities;
77
use Illuminate\Database\Events\MigrationsEnded;
88
use Illuminate\Database\Events\MigrationsStarted;
9-
use Mockery;
109

1110
beforeEach(function () {
1211
test()->manager = Mockery::mock(SqlEntityManager::class);

0 commit comments

Comments
 (0)