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\CreateFunctionCompiler; |
14
|
|
|
use Umbrellio\Postgres\Compilers\CreateProcedureCompiler; |
15
|
|
|
use Umbrellio\Postgres\Compilers\CreateTriggerCompiler; |
16
|
|
|
use Umbrellio\Postgres\Compilers\DropFunctionCompiler; |
17
|
|
|
use Umbrellio\Postgres\Compilers\DropProcedureCompiler; |
18
|
|
|
use Umbrellio\Postgres\Compilers\DropTriggerCompiler; |
19
|
|
|
use Umbrellio\Postgres\Compilers\ExcludeCompiler; |
20
|
|
|
use Umbrellio\Postgres\Compilers\UniqueCompiler; |
21
|
|
|
use Umbrellio\Postgres\Schema\Builders\Constraints\Check\CheckBuilder; |
22
|
|
|
use Umbrellio\Postgres\Schema\Builders\Constraints\Exclude\ExcludeBuilder; |
23
|
|
|
use Umbrellio\Postgres\Schema\Builders\Indexes\Unique\UniqueBuilder; |
24
|
|
|
use Umbrellio\Postgres\Schema\Builders\Indexes\Unique\UniquePartialBuilder; |
25
|
|
|
use Umbrellio\Postgres\Schema\Builders\Routines\CreateFunctionBuilder; |
26
|
|
|
use Umbrellio\Postgres\Schema\Builders\Routines\CreateProcedureBuilder; |
27
|
|
|
use Umbrellio\Postgres\Schema\Builders\Routines\CreateTriggerBuilder; |
28
|
|
|
use Umbrellio\Postgres\Schema\Builders\Routines\DropFunctionBuilder; |
29
|
|
|
use Umbrellio\Postgres\Schema\Builders\Routines\DropProcedureBuilder; |
30
|
|
|
use Umbrellio\Postgres\Schema\Builders\Routines\DropTriggerBuilder; |
31
|
|
|
use Umbrellio\Postgres\Schema\Types\DateRangeType; |
32
|
|
|
use Umbrellio\Postgres\Schema\Types\NumericType; |
33
|
|
|
use Umbrellio\Postgres\Schema\Types\TsRangeType; |
34
|
|
|
use Umbrellio\Postgres\Schema\Types\TsTzRangeType; |
35
|
|
|
|
36
|
|
|
class PostgresGrammar extends BasePostgresGrammar |
37
|
|
|
{ |
38
|
28 |
|
public function compileCreate(Blueprint $blueprint, Fluent $command): string |
39
|
|
|
{ |
40
|
28 |
|
$like = $this->getCommandByName($blueprint, 'like'); |
41
|
28 |
|
$ifNotExists = $this->getCommandByName($blueprint, 'ifNotExists'); |
42
|
|
|
|
43
|
28 |
|
return CreateCompiler::compile( |
44
|
28 |
|
$this, |
45
|
28 |
|
$blueprint, |
46
|
28 |
|
$this->getColumns($blueprint), |
47
|
28 |
|
compact('like', 'ifNotExists') |
48
|
28 |
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @codeCoverageIgnore |
53
|
|
|
*/ |
54
|
|
|
public function compileAttachPartition(Blueprint $blueprint, Fluent $command): string |
55
|
|
|
{ |
56
|
|
|
return AttachPartitionCompiler::compile($this, $blueprint, $command); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @codeCoverageIgnore |
61
|
|
|
*/ |
62
|
|
|
public function compileDetachPartition(Blueprint $blueprint, Fluent $command): string |
63
|
|
|
{ |
64
|
|
|
return sprintf( |
65
|
|
|
'alter table %s detach partition %s', |
66
|
|
|
$this->wrapTable($blueprint), |
67
|
|
|
$command->get('partition') |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @codeCoverageIgnore |
73
|
|
|
*/ |
74
|
|
|
public function compileCreateView(Blueprint $blueprint, Fluent $command): string |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$materialize = $command->get('materialize') ? 'materialized' : ''; |
77
|
|
|
return implode(' ', array_filter([ |
78
|
|
|
'create', |
79
|
|
|
$materialize, |
80
|
|
|
'view', |
81
|
|
|
$this->wrapTable($command->get('view')), |
82
|
|
|
'as', |
83
|
|
|
$command->get('select'), |
84
|
|
|
])); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function compileCreateTrigger(Blueprint $blueprint, CreateTriggerBuilder $command): array |
88
|
|
|
{ |
89
|
|
|
return CreateTriggerCompiler::compile($this, $blueprint, $command); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function compileCreateFunction(Blueprint $blueprint, CreateFunctionBuilder $command): array |
93
|
|
|
{ |
94
|
|
|
return CreateFunctionCompiler::compile($this, $blueprint, $command); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function compileCreateProcedure(Blueprint $blueprint, CreateProcedureBuilder $command): array |
98
|
|
|
{ |
99
|
|
|
return CreateProcedureCompiler::compile($this, $blueprint, $command); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function compileDropTrigger(Blueprint $blueprint, DropTriggerBuilder $command): array |
103
|
|
|
{ |
104
|
|
|
return DropTriggerCompiler::compile($this, $blueprint, $command); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function compileDropFunction(Blueprint $blueprint, DropFunctionBuilder $command): string |
108
|
|
|
{ |
109
|
|
|
return DropFunctionCompiler::compile($this, $blueprint, $command); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function compileDropProcedure(Blueprint $blueprint, DropProcedureBuilder $command): string |
113
|
|
|
{ |
114
|
|
|
return DropProcedureCompiler::compile($this, $blueprint, $command); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @codeCoverageIgnore |
119
|
|
|
*/ |
120
|
|
|
public function compileDropView(Blueprint $blueprint, Fluent $command): string |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
return 'drop view ' . $this->wrapTable($command->get('view')); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @codeCoverageIgnore |
127
|
|
|
*/ |
128
|
|
|
public function compileViewExists(): string |
129
|
|
|
{ |
130
|
|
|
return 'select * from information_schema.views where table_schema = ? and table_name = ?'; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @codeCoverageIgnore |
135
|
|
|
*/ |
136
|
|
|
public function compileForeignKeysListing(string $tableName): string |
137
|
|
|
{ |
138
|
|
|
return sprintf(" |
139
|
|
|
SELECT |
140
|
|
|
kcu.column_name as source_column_name, |
141
|
|
|
ccu.table_name AS target_table_name, |
142
|
|
|
ccu.column_name AS target_column_name |
143
|
|
|
FROM |
144
|
|
|
information_schema.table_constraints AS tc |
145
|
|
|
JOIN information_schema.key_column_usage AS kcu |
146
|
|
|
ON tc.constraint_name = kcu.constraint_name |
147
|
|
|
AND tc.table_schema = kcu.table_schema |
148
|
|
|
JOIN information_schema.constraint_column_usage AS ccu |
149
|
|
|
ON ccu.constraint_name = tc.constraint_name |
150
|
|
|
AND ccu.table_schema = tc.table_schema |
151
|
|
|
WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='%s'; |
152
|
|
|
", $tableName); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @codeCoverageIgnore |
157
|
|
|
*/ |
158
|
|
|
public function compileViewDefinition(): string |
159
|
|
|
{ |
160
|
|
|
return 'select view_definition from information_schema.views where table_schema = ? and table_name = ?'; |
161
|
|
|
} |
162
|
|
|
|
163
|
13 |
|
public function compileUniquePartial(Blueprint $blueprint, UniqueBuilder $command): string |
164
|
|
|
{ |
165
|
13 |
|
$constraints = $command->get('constraints'); |
166
|
13 |
|
if ($constraints instanceof UniquePartialBuilder) { |
167
|
12 |
|
return UniqueCompiler::compile($this, $blueprint, $command, $constraints); |
168
|
|
|
} |
169
|
1 |
|
return $this->compileUnique($blueprint, $command); |
170
|
|
|
} |
171
|
|
|
|
172
|
6 |
|
public function compileExclude(Blueprint $blueprint, ExcludeBuilder $command): string |
173
|
|
|
{ |
174
|
6 |
|
return ExcludeCompiler::compile($this, $blueprint, $command); |
175
|
|
|
} |
176
|
|
|
|
177
|
3 |
|
public function compileCheck(Blueprint $blueprint, CheckBuilder $command): string |
178
|
|
|
{ |
179
|
3 |
|
return CheckCompiler::compile($this, $blueprint, $command); |
180
|
|
|
} |
181
|
|
|
|
182
|
3 |
|
protected function typeNumeric(Fluent $column): string |
183
|
|
|
{ |
184
|
3 |
|
$type = NumericType::TYPE_NAME; |
185
|
3 |
|
$precision = $column->get('precision'); |
186
|
3 |
|
$scale = $column->get('scale'); |
187
|
|
|
|
188
|
3 |
|
if ($precision && $scale) { |
189
|
1 |
|
return "{$type}({$precision}, {$scale})"; |
190
|
|
|
} |
191
|
|
|
|
192
|
2 |
|
if ($precision) { |
193
|
1 |
|
return "{$type}({$precision})"; |
194
|
|
|
} |
195
|
|
|
|
196
|
1 |
|
return $type; |
197
|
|
|
} |
198
|
|
|
|
199
|
1 |
|
protected function typeTsrange(Fluent $column): string |
|
|
|
|
200
|
|
|
{ |
201
|
1 |
|
return TsRangeType::TYPE_NAME; |
202
|
|
|
} |
203
|
|
|
|
204
|
1 |
|
protected function typeTstzrange(Fluent $column): string |
|
|
|
|
205
|
|
|
{ |
206
|
1 |
|
return TsTzRangeType::TYPE_NAME; |
207
|
|
|
} |
208
|
|
|
|
209
|
1 |
|
protected function typeDaterange(Fluent $column): string |
|
|
|
|
210
|
|
|
{ |
211
|
1 |
|
return DateRangeType::TYPE_NAME; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.