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