Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.67% |
33 / 36 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
Grammar | |
91.67% |
33 / 36 |
|
40.00% |
2 / 5 |
18.19 | |
0.00% |
0 / 1 |
compileCase | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
supportsSequences | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
compileCreateSequence | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
5.03 | |||
compileNextVal | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
5.03 | |||
compileDropSequence | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
5.05 |
1 | <?php |
2 | |
3 | namespace Baril\Orderly\Mixins; |
4 | |
5 | use Illuminate\Database\Query\Grammars\MySqlGrammar; |
6 | use Illuminate\Database\Query\Grammars\PostgresGrammar; |
7 | use Illuminate\Database\Query\Grammars\SQLiteGrammar; |
8 | use Illuminate\Database\Query\Grammars\SqlServerGrammar; |
9 | use LogicException; |
10 | |
11 | /** |
12 | * @mixin \Illuminate\Database\Query\Grammars\Grammar |
13 | */ |
14 | class Grammar |
15 | { |
16 | public function compileCase() |
17 | { |
18 | return function ($expression, array $cases, $else = null): string { |
19 | /** @var \Illuminate\Database\Query\Grammars\Grammar $this */ |
20 | $cases = implode(' ', array_map(function ($when, $then) { |
21 | return "when {$this->quoteString($when)} then {$this->quoteString($then)}"; |
22 | }, array_keys($cases), array_values($cases))); |
23 | $else = is_null($else) ? '' : "else {$this->quoteString($else)}"; |
24 | return "case {$this->wrap($expression)} $cases $else END"; |
25 | }; |
26 | } |
27 | |
28 | public function supportsSequences() |
29 | { |
30 | return function (): bool { |
31 | return !($this instanceof SQLiteGrammar); |
32 | }; |
33 | } |
34 | |
35 | public function compileCreateSequence() |
36 | { |
37 | return function (string $name, int $start = 1, int $increment = 1): string { |
38 | switch (true) { |
39 | case $this instanceof MySqlGrammar: |
40 | $start -= $increment; |
41 | return "set @$name := $start"; |
42 | case $this instanceof PostgresGrammar: |
43 | case $this instanceof SqlServerGrammar: |
44 | return "create sequence {$this->wrap($name)} start with $start increment by $increment"; |
45 | default: |
46 | throw new LogicException('This grammar doesn\'t support sequences!'); |
47 | } |
48 | }; |
49 | } |
50 | |
51 | public function compileNextVal() |
52 | { |
53 | return function (string $name, int $increment = 1): string { |
54 | switch (true) { |
55 | case $this instanceof MySqlGrammar: |
56 | return "@$name := @$name + $increment"; |
57 | case $this instanceof PostgresGrammar: |
58 | return "nextval({$this->quoteString($name)})"; |
59 | case $this instanceof SqlServerGrammar: |
60 | return "next value for {$this->wrap($name)}"; |
61 | default: |
62 | throw new LogicException('This grammar doesn\'t support sequences!'); |
63 | } |
64 | }; |
65 | } |
66 | |
67 | public function compileDropSequence() |
68 | { |
69 | return function (string $name): string { |
70 | switch (true) { |
71 | case $this instanceof MySqlGrammar: |
72 | return 'select 1'; |
73 | case $this instanceof PostgresGrammar: |
74 | case $this instanceof SqlServerGrammar: |
75 | return "drop sequence {$this->wrap($name)}"; |
76 | default: |
77 | throw new LogicException('This grammar doesn\'t support sequences!'); |
78 | } |
79 | }; |
80 | } |
81 | } |