Skip to content

Commit fde8721

Browse files
committed
feat!: add characteristics to SqlEntity contract
1 parent a3fa004 commit fde8721

9 files changed

+49
-61
lines changed

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,6 @@ class Add extends Function_
251251
/** The language the function is written in. */
252252
protected string $language = 'SQL';
253253

254-
protected array $characteristics = [];
255-
256254
/** The function return type. */
257255
protected string $returns = 'integer';
258256

src/Concerns/DefaultSqlEntityBehaviour.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ trait DefaultSqlEntityBehaviour
1515
/** The connection name. */
1616
protected ?string $connection = null;
1717

18-
/** The entity name. */
19-
protected ?string $name = null;
18+
/**
19+
* Any additional characteristics for the entity.
20+
*
21+
* @var list<string>
22+
*/
23+
protected array $characteristics = [];
2024

2125
/**
2226
* Any dependencies that need to be handled before this entity.
@@ -25,6 +29,9 @@ trait DefaultSqlEntityBehaviour
2529
*/
2630
protected array $dependencies = [];
2731

32+
/** The entity name. */
33+
protected ?string $name = null;
34+
2835
#[Override]
2936
public function name(): string
3037
{
@@ -37,6 +44,12 @@ public function connectionName(): ?string
3744
return $this->connection;
3845
}
3946

47+
#[Override]
48+
public function characteristics(): array
49+
{
50+
return $this->characteristics;
51+
}
52+
4053
#[Override]
4154
public function dependencies(): array
4255
{

src/Contracts/SqlEntity.php

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ public function name(): string;
1919
/** The entity connection name. */
2020
public function connectionName(): ?string;
2121

22+
/**
23+
* Any additional characteristics for the entity.
24+
*
25+
* @return list<string>
26+
*/
27+
public function characteristics(): array;
28+
2229
/**
2330
* Any dependencies that need to be handled before this entity.
2431
*

src/Function_.php

-17
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ abstract class Function_ implements SqlEntity
2727
/** If the function is loadable. */
2828
protected bool $loadable = false;
2929

30-
/**
31-
* The function characteristics.
32-
*
33-
* @var list<string>
34-
*/
35-
protected array $characteristics = [];
36-
3730
/** The function return type. */
3831
protected string $returns;
3932

@@ -53,16 +46,6 @@ public function arguments(): array
5346
return $this->arguments;
5447
}
5548

56-
/**
57-
* The function characteristics.
58-
*
59-
* @return list<string>
60-
*/
61-
public function characteristics(): array
62-
{
63-
return $this->characteristics;
64-
}
65-
6649
/** The language the function is written in. */
6750
public function language(): string
6851
{

src/Grammars/PostgresGrammar.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ protected function compileTriggerDrop(Trigger $entity): string
7171
#[Override]
7272
protected function compileViewCreate(View $entity): string
7373
{
74-
$checkOption = $this->compileCheckOption($entity->checkOption());
75-
$columns = $this->compileList($entity->columns());
76-
$recursive = $entity->isRecursive() ? ' RECURSIVE' : '';
74+
$checkOption = $this->compileCheckOption($entity->checkOption());
75+
$columns = $this->compileList($entity->columns());
76+
$recursive = $entity->isRecursive() ? ' RECURSIVE' : '';
77+
$characteristics = implode("\n", $entity->characteristics());
7778

7879
return <<<SQL
79-
CREATE OR REPLACE {$recursive} VIEW {$entity->name()} {$columns} AS
80-
{$entity->toString()}
80+
CREATE OR REPLACE {$recursive} VIEW {$entity->name()} {$columns}
81+
{$characteristics}
82+
AS {$entity->toString()}
8183
{$checkOption}
8284
SQL;
8385
}

src/Grammars/SqlServerGrammar.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ protected function compileTriggerCreate(Trigger $entity): string
4848
#[Override]
4949
protected function compileViewCreate(View $entity): string
5050
{
51-
$checkOption = $this->compileCheckOption($entity->checkOption());
52-
$columns = $this->compileList($entity->columns());
51+
$checkOption = $this->compileCheckOption($entity->checkOption());
52+
$columns = $this->compileList($entity->columns());
53+
$characteristics = implode("\n", $entity->characteristics());
5354

5455
return <<<SQL
55-
CREATE OR ALTER VIEW {$entity->name()} {$columns} AS
56-
{$entity->toString()}
56+
CREATE OR ALTER VIEW {$entity->name()} {$columns}
57+
{$characteristics}
58+
AS {$entity->toString()}
5759
{$checkOption}
5860
SQL;
5961
}

src/Trigger.php

-17
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ abstract class Trigger implements SqlEntity
1111
{
1212
use DefaultSqlEntityBehaviour;
1313

14-
/**
15-
* The trigger characteristics.
16-
*
17-
* @var list<string>
18-
*/
19-
protected array $characteristics = [];
20-
2114
/** If the trigger is a constraint trigger. */
2215
protected bool $constraint = false;
2316

@@ -34,16 +27,6 @@ abstract class Trigger implements SqlEntity
3427
/** The trigger timing. */
3528
protected string $timing;
3629

37-
/**
38-
* The function characteristics.
39-
*
40-
* @return list<string>
41-
*/
42-
public function characteristics(): array
43-
{
44-
return $this->characteristics;
45-
}
46-
4730
/** If the trigger is a constraint trigger. */
4831
public function constraint(): bool
4932
{

tests/Unit/Grammars/PostgresGrammarTest.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@
171171
$sql = test()->grammar->compileCreate(test()->entity);
172172

173173
expect($sql)->toBe(<<<'SQL'
174-
CREATE OR REPLACE VIEW user_view AS
175-
SELECT id, name FROM users
174+
CREATE OR REPLACE VIEW user_view
175+
AS SELECT id, name FROM users
176176
SQL);
177177
});
178178

@@ -182,8 +182,8 @@
182182
$sql = test()->grammar->compileCreate(test()->entity);
183183

184184
expect($sql)->toBe(<<<'SQL'
185-
CREATE OR REPLACE RECURSIVE VIEW user_view AS
186-
SELECT id, name FROM users
185+
CREATE OR REPLACE RECURSIVE VIEW user_view
186+
AS SELECT id, name FROM users
187187
SQL);
188188
});
189189

@@ -193,8 +193,8 @@
193193
$sql = test()->grammar->compileCreate(test()->entity);
194194

195195
expect($sql)->toBe(<<<SQL
196-
CREATE OR REPLACE VIEW user_view{$expected} AS
197-
SELECT id, name FROM users
196+
CREATE OR REPLACE VIEW user_view{$expected}
197+
AS SELECT id, name FROM users
198198
SQL);
199199
})->with([
200200
'one column' => [['id'], ' (id)'],
@@ -207,8 +207,8 @@
207207
$sql = test()->grammar->compileCreate(test()->entity);
208208

209209
expect($sql)->toBe(<<<SQL
210-
CREATE OR REPLACE VIEW user_view AS
211-
SELECT id, name FROM users
210+
CREATE OR REPLACE VIEW user_view
211+
AS SELECT id, name FROM users
212212
{$expected}
213213
SQL);
214214
})->with([

tests/Unit/Grammars/SqlServerGrammarTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@
124124
$sql = test()->grammar->compileCreate(test()->entity);
125125

126126
expect($sql)->toBe(<<<'SQL'
127-
CREATE OR ALTER VIEW user_view AS
128-
SELECT id, name FROM users
127+
CREATE OR ALTER VIEW user_view
128+
AS SELECT id, name FROM users
129129
SQL);
130130
});
131131

@@ -135,8 +135,8 @@
135135
$sql = test()->grammar->compileCreate(test()->entity);
136136

137137
expect($sql)->toBe(<<<SQL
138-
CREATE OR ALTER VIEW user_view{$expected} AS
139-
SELECT id, name FROM users
138+
CREATE OR ALTER VIEW user_view{$expected}
139+
AS SELECT id, name FROM users
140140
SQL);
141141
})->with([
142142
'one column' => [['id'], ' (id)'],
@@ -149,8 +149,8 @@
149149
$sql = test()->grammar->compileCreate(test()->entity);
150150

151151
expect($sql)->toBe(<<<SQL
152-
CREATE OR ALTER VIEW user_view AS
153-
SELECT id, name FROM users
152+
CREATE OR ALTER VIEW user_view
153+
AS SELECT id, name FROM users
154154
{$expected}
155155
SQL);
156156
})->with([

0 commit comments

Comments
 (0)