1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Umbrellio\Postgres\Schema\Grammars; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Schema\Blueprint; |
8
|
|
|
use Illuminate\Database\Schema\Grammars\PostgresGrammar as BasePostgresGrammar; |
9
|
|
|
use Illuminate\Support\Fluent; |
10
|
|
|
use Umbrellio\Postgres\Compilers\AttachPartitionCompiler; |
11
|
|
|
use Umbrellio\Postgres\Compilers\CheckCompiler; |
12
|
|
|
use Umbrellio\Postgres\Compilers\CreateCompiler; |
13
|
|
|
use Umbrellio\Postgres\Compilers\ExcludeCompiler; |
14
|
|
|
use Umbrellio\Postgres\Compilers\UniqueCompiler; |
15
|
|
|
use Umbrellio\Postgres\Schema\Builders\Constraints\Check\CheckBuilder; |
16
|
|
|
use Umbrellio\Postgres\Schema\Builders\Constraints\Exclude\ExcludeBuilder; |
17
|
|
|
use Umbrellio\Postgres\Schema\Builders\Indexes\Unique\UniqueBuilder; |
18
|
|
|
use Umbrellio\Postgres\Schema\Builders\Indexes\Unique\UniquePartialBuilder; |
19
|
|
|
use Umbrellio\Postgres\Schema\Types\NumericType; |
20
|
|
|
use Umbrellio\Postgres\Schema\Types\TsRangeType; |
21
|
|
|
|
22
|
|
|
class PostgresGrammar extends BasePostgresGrammar |
23
|
|
|
{ |
24
|
34 |
|
public function compileCreate(Blueprint $blueprint, Fluent $command): string |
25
|
|
|
{ |
26
|
34 |
|
$like = $this->getCommandByName($blueprint, 'like'); |
27
|
34 |
|
$ifNotExists = $this->getCommandByName($blueprint, 'ifNotExists'); |
28
|
|
|
|
29
|
34 |
|
return CreateCompiler::compile( |
30
|
34 |
|
$this, |
31
|
|
|
$blueprint, |
32
|
34 |
|
$this->getColumns($blueprint), |
33
|
34 |
|
compact('like', 'ifNotExists') |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
public function compileAttachPartition(Blueprint $blueprint, Fluent $command): string |
38
|
|
|
{ |
39
|
4 |
|
return AttachPartitionCompiler::compile($this, $blueprint, $command); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function compileDetachPartition(Blueprint $blueprint, Fluent $command): string |
43
|
|
|
{ |
44
|
1 |
|
return sprintf('alter table %s detach partition %s', |
45
|
1 |
|
$this->wrapTable($blueprint), |
46
|
1 |
|
$command->get('partition') |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
public function compileCreateView(/** @scrutinizer ignore-unused */ Blueprint $blueprint, Fluent $command): string |
51
|
|
|
{ |
52
|
2 |
|
$materialize = $command->get('materialize') ? 'materialized' : ''; |
53
|
2 |
|
return implode(' ', array_filter([ |
54
|
2 |
|
'create', |
55
|
2 |
|
$materialize, |
56
|
2 |
|
'view', |
57
|
2 |
|
$this->wrapTable($command->get('view')), |
58
|
2 |
|
'as', |
59
|
2 |
|
$command->get('select'), |
60
|
|
|
])); |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
public function compileDropView(/** @scrutinizer ignore-unused */ Blueprint $blueprint, Fluent $command): string |
64
|
|
|
{ |
65
|
2 |
|
return 'drop view ' . $this->wrapTable($command->get('view')); |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
public function compileViewExists(): string |
69
|
|
|
{ |
70
|
2 |
|
return 'select * from information_schema.views where table_schema = ? and table_name = ?'; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
public function compileForeignKeysListing(string $tableName): string |
74
|
|
|
{ |
75
|
1 |
|
return sprintf(" |
76
|
|
|
SELECT |
77
|
|
|
kcu.column_name as source_column_name, |
78
|
|
|
ccu.table_name AS target_table_name, |
79
|
|
|
ccu.column_name AS target_column_name |
80
|
|
|
FROM |
81
|
|
|
information_schema.table_constraints AS tc |
82
|
|
|
JOIN information_schema.key_column_usage AS kcu |
83
|
|
|
ON tc.constraint_name = kcu.constraint_name |
84
|
|
|
AND tc.table_schema = kcu.table_schema |
85
|
|
|
JOIN information_schema.constraint_column_usage AS ccu |
86
|
|
|
ON ccu.constraint_name = tc.constraint_name |
87
|
|
|
AND ccu.table_schema = tc.table_schema |
88
|
|
|
WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='%s'; |
89
|
|
|
", $tableName); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
public function compileViewDefinition(): string |
93
|
|
|
{ |
94
|
2 |
|
return 'select view_definition from information_schema.views where table_schema = ? and table_name = ?'; |
95
|
|
|
} |
96
|
|
|
|
97
|
13 |
|
public function compileUniquePartial(Blueprint $blueprint, UniqueBuilder $command): string |
98
|
|
|
{ |
99
|
13 |
|
$constraints = $command->get('constraints'); |
100
|
13 |
|
if ($constraints instanceof UniquePartialBuilder) { |
101
|
12 |
|
return UniqueCompiler::compile($this, $blueprint, $command, $constraints); |
102
|
|
|
} |
103
|
1 |
|
return $this->compileUnique($blueprint, $command); |
104
|
|
|
} |
105
|
|
|
|
106
|
6 |
|
public function compileExclude(Blueprint $blueprint, ExcludeBuilder $command): string |
107
|
|
|
{ |
108
|
6 |
|
return ExcludeCompiler::compile($this, $blueprint, $command); |
109
|
|
|
} |
110
|
|
|
|
111
|
3 |
|
public function compileCheck(Blueprint $blueprint, CheckBuilder $command): string |
112
|
|
|
{ |
113
|
3 |
|
return CheckCompiler::compile($this, $blueprint, $command); |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
protected function typeNumeric(Fluent $column): string |
117
|
|
|
{ |
118
|
3 |
|
$type = NumericType::TYPE_NAME; |
119
|
3 |
|
$precision = $column->get('precision'); |
120
|
3 |
|
$scale = $column->get('scale'); |
121
|
|
|
|
122
|
3 |
|
if ($precision && $scale) { |
123
|
1 |
|
return "${type}({$precision}, {$scale})"; |
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
if ($precision) { |
127
|
1 |
|
return "${type}({$precision})"; |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
return $type; |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
protected function typeTsrange(/** @scrutinizer ignore-unused */ Fluent $column): string |
134
|
|
|
{ |
135
|
1 |
|
return TsRangeType::TYPE_NAME; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|