Skip to content

Commit 297ccb4

Browse files
committed
chore: generalize compiling a list
1 parent d6e16a7 commit 297ccb4

10 files changed

+29
-25
lines changed

src/Grammars/Grammar.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use CalebDW\SqlEntities\Contracts\SqlEntity;
88
use CalebDW\SqlEntities\View;
99
use Illuminate\Database\Connection;
10+
use Illuminate\Support\Str;
1011
use InvalidArgumentException;
1112

1213
abstract class Grammar
@@ -18,24 +19,28 @@ public function __construct(
1819

1920
public function compileCreate(SqlEntity $entity): string
2021
{
21-
return match (true) {
22+
$statement = match (true) {
2223
$entity instanceof View => $this->compileViewCreate($entity),
2324

2425
default => throw new InvalidArgumentException(
2526
sprintf('Unsupported entity [%s].', $entity::class),
2627
),
2728
};
29+
30+
return $this->clean($statement);
2831
}
2932

3033
public function compileDrop(SqlEntity $entity): string
3134
{
32-
return match (true) {
35+
$statement = match (true) {
3336
$entity instanceof View => $this->compileViewDrop($entity),
3437

3538
default => throw new InvalidArgumentException(
3639
sprintf('Unsupported entity [%s].', $entity::class),
3740
),
3841
};
42+
43+
return $this->clean($statement);
3944
}
4045

4146
abstract protected function compileViewCreate(View $entity): string;
@@ -47,14 +52,14 @@ protected function compileViewDrop(View $entity): string
4752
SQL;
4853
}
4954

50-
/** @param list<string>|null $columns */
51-
protected function compileColumnsList(?array $columns): string
55+
/** @param list<string>|null $values */
56+
protected function compileList(?array $values): string
5257
{
53-
if ($columns === null) {
58+
if ($values === null) {
5459
return '';
5560
}
5661

57-
return ' (' . implode(', ', $columns) . ')';
62+
return '(' . implode(', ', $values) . ')';
5863
}
5964

6065
protected function compileCheckOption(string|true|null $option): string
@@ -71,4 +76,12 @@ protected function compileCheckOption(string|true|null $option): string
7176

7277
return "WITH {$option} CHECK OPTION";
7378
}
79+
80+
protected function clean(string $value): string
81+
{
82+
return Str::of($value)
83+
->replaceMatches('/ +/', ' ')
84+
->trim()
85+
->value();
86+
}
7487
}

src/Grammars/MariaDbGrammar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ class MariaDbGrammar extends Grammar
1212
#[Override]
1313
protected function compileViewCreate(View $entity): string
1414
{
15-
$columns = $this->compileColumnsList($entity->columns());
15+
$columns = $this->compileList($entity->columns());
1616
$checkOption = $this->compileCheckOption($entity->checkOption());
1717

1818
return <<<SQL
19-
CREATE OR REPLACE VIEW {$entity->name()}{$columns} AS
19+
CREATE OR REPLACE VIEW {$entity->name()} {$columns} AS
2020
{$entity->toString()}
2121
{$checkOption}
2222
SQL;

src/Grammars/MySqlGrammar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ class MySqlGrammar extends Grammar
1212
#[Override]
1313
protected function compileViewCreate(View $entity): string
1414
{
15-
$columns = $this->compileColumnsList($entity->columns());
15+
$columns = $this->compileList($entity->columns());
1616
$checkOption = $this->compileCheckOption($entity->checkOption());
1717

1818
return <<<SQL
19-
CREATE OR REPLACE VIEW {$entity->name()}{$columns} AS
19+
CREATE OR REPLACE VIEW {$entity->name()} {$columns} AS
2020
{$entity->toString()}
2121
{$checkOption}
2222
SQL;

src/Grammars/PostgresGrammar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ class PostgresGrammar extends Grammar
1313
protected function compileViewCreate(View $entity): string
1414
{
1515
$checkOption = $this->compileCheckOption($entity->checkOption());
16-
$columns = $this->compileColumnsList($entity->columns());
16+
$columns = $this->compileList($entity->columns());
1717
$recursive = $entity->isRecursive() ? ' RECURSIVE' : '';
1818

1919
return <<<SQL
20-
CREATE OR REPLACE{$recursive} VIEW {$entity->name()}{$columns} AS
20+
CREATE OR REPLACE {$recursive} VIEW {$entity->name()} {$columns} AS
2121
{$entity->toString()}
2222
{$checkOption}
2323
SQL;

src/Grammars/SQLiteGrammar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ class SQLiteGrammar extends Grammar
1212
#[Override]
1313
protected function compileViewCreate(View $entity): string
1414
{
15-
$columns = $this->compileColumnsList($entity->columns());
15+
$columns = $this->compileList($entity->columns());
1616

1717
return <<<SQL
18-
CREATE VIEW IF NOT EXISTS {$entity->name()}{$columns} AS
18+
CREATE VIEW IF NOT EXISTS {$entity->name()} {$columns} AS
1919
{$entity->toString()}
2020
SQL;
2121
}

src/Grammars/SqlServerGrammar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ class SqlServerGrammar extends Grammar
1313
protected function compileViewCreate(View $entity): string
1414
{
1515
$checkOption = $this->compileCheckOption($entity->checkOption());
16-
$columns = $this->compileColumnsList($entity->columns());
16+
$columns = $this->compileList($entity->columns());
1717

1818
return <<<SQL
19-
CREATE OR ALTER VIEW {$entity->name()}{$columns} AS
19+
CREATE OR ALTER VIEW {$entity->name()} {$columns} AS
2020
{$entity->toString()}
2121
{$checkOption}
2222
SQL;

tests/Unit/Grammars/MariaDbGrammarTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
expect($sql)->toBe(<<<'SQL'
2121
CREATE OR REPLACE VIEW user_view AS
2222
SELECT id, name FROM users
23-
2423
SQL);
2524
});
2625

@@ -32,7 +31,6 @@
3231
expect($sql)->toBe(<<<SQL
3332
CREATE OR REPLACE VIEW user_view{$expected} AS
3433
SELECT id, name FROM users
35-
3634
SQL);
3735
})->with([
3836
'one column' => [['id'], ' (id)'],

tests/Unit/Grammars/MySqlGrammarTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
expect($sql)->toBe(<<<'SQL'
2121
CREATE OR REPLACE VIEW user_view AS
2222
SELECT id, name FROM users
23-
2423
SQL);
2524
});
2625

@@ -32,7 +31,6 @@
3231
expect($sql)->toBe(<<<SQL
3332
CREATE OR REPLACE VIEW user_view{$expected} AS
3433
SELECT id, name FROM users
35-
3634
SQL);
3735
})->with([
3836
'one column' => [['id'], ' (id)'],

tests/Unit/Grammars/PostgresGrammarTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
expect($sql)->toBe(<<<'SQL'
2121
CREATE OR REPLACE VIEW user_view AS
2222
SELECT id, name FROM users
23-
2423
SQL);
2524
});
2625

@@ -32,7 +31,6 @@
3231
expect($sql)->toBe(<<<'SQL'
3332
CREATE OR REPLACE RECURSIVE VIEW user_view AS
3433
SELECT id, name FROM users
35-
3634
SQL);
3735
});
3836

@@ -44,7 +42,6 @@
4442
expect($sql)->toBe(<<<SQL
4543
CREATE OR REPLACE VIEW user_view{$expected} AS
4644
SELECT id, name FROM users
47-
4845
SQL);
4946
})->with([
5047
'one column' => [['id'], ' (id)'],

tests/Unit/Grammars/SqlServerGrammarTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
expect($sql)->toBe(<<<'SQL'
2121
CREATE OR ALTER VIEW user_view AS
2222
SELECT id, name FROM users
23-
2423
SQL);
2524
});
2625

@@ -32,7 +31,6 @@
3231
expect($sql)->toBe(<<<SQL
3332
CREATE OR ALTER VIEW user_view{$expected} AS
3433
SELECT id, name FROM users
35-
3634
SQL);
3735
})->with([
3836
'one column' => [['id'], ' (id)'],

0 commit comments

Comments
 (0)