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
|
|
|
|
32
|
|
|
class PostgresGrammar extends BasePostgresGrammar |
33
|
|
|
{ |
34
|
|
|
public function compileCreate(Blueprint $blueprint, Fluent $command): string |
35
|
|
|
{ |
36
|
|
|
$like = $this->getCommandByName($blueprint, 'like'); |
37
|
|
|
$ifNotExists = $this->getCommandByName($blueprint, 'ifNotExists'); |
38
|
|
|
|
39
|
|
|
return CreateCompiler::compile( |
40
|
|
|
$this, |
41
|
|
|
$blueprint, |
42
|
|
|
$this->getColumns($blueprint), |
43
|
|
|
compact('like', 'ifNotExists') |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function compileAttachPartition(Blueprint $blueprint, Fluent $command): string |
48
|
|
|
{ |
49
|
|
|
return AttachPartitionCompiler::compile($this, $blueprint, $command); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function compileDetachPartition(Blueprint $blueprint, Fluent $command): string |
53
|
|
|
{ |
54
|
|
|
return sprintf('alter table %s detach partition %s', |
55
|
|
|
$this->wrapTable($blueprint), |
56
|
|
|
$command->get('partition') |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function compileCreateView(Blueprint $blueprint, Fluent $command): string |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$materialize = $command->get('materialize') ? 'materialized' : ''; |
63
|
|
|
return implode(' ', array_filter([ |
64
|
|
|
'create', $materialize, 'view', |
65
|
|
|
$this->wrapTable($command->get('view')), |
66
|
|
|
'as', $command->get('select'), |
67
|
|
|
])); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function compileCreateTrigger(Blueprint $blueprint, CreateTriggerBuilder $command): array |
71
|
|
|
{ |
72
|
|
|
return CreateTriggerCompiler::compile($this, $blueprint, $command); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function compileCreateFunction(Blueprint $blueprint, CreateFunctionBuilder $command): array |
76
|
|
|
{ |
77
|
|
|
return CreateFunctionCompiler::compile($this, $blueprint, $command); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function compileCreateProcedure(Blueprint $blueprint, CreateProcedureBuilder $command): array |
81
|
|
|
{ |
82
|
|
|
return CreateProcedureCompiler::compile($this, $blueprint, $command); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function compileDropTrigger(Blueprint $blueprint, DropTriggerBuilder $command): array |
86
|
|
|
{ |
87
|
|
|
return DropTriggerCompiler::compile($this, $blueprint, $command); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function compileDropFunction(Blueprint $blueprint, DropFunctionBuilder $command): string |
91
|
|
|
{ |
92
|
|
|
return DropFunctionCompiler::compile($this, $blueprint, $command); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function compileDropProcedure(Blueprint $blueprint, DropProcedureBuilder $command): string |
96
|
|
|
{ |
97
|
|
|
return DropProcedureCompiler::compile($this, $blueprint, $command); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function compileDropView(Blueprint $blueprint, Fluent $command): string |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
return 'drop view ' . $this->wrapTable($command->get('view')); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function compileViewExists(): string |
106
|
|
|
{ |
107
|
|
|
return 'select * from information_schema.views where table_schema = ? and table_name = ?'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function compileViewDefinition(): string |
111
|
|
|
{ |
112
|
|
|
return 'select view_definition from information_schema.views where table_schema = ? and table_name = ?'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function compileUniquePartial(Blueprint $blueprint, UniqueBuilder $command): string |
116
|
|
|
{ |
117
|
|
|
$constraints = $command->get('constraints'); |
118
|
|
|
if ($constraints instanceof UniquePartialBuilder) { |
119
|
|
|
return UniqueCompiler::compile($this, $blueprint, $command, $constraints); |
120
|
|
|
} |
121
|
|
|
return $this->compileUnique($blueprint, $command); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function compileExclude(Blueprint $blueprint, ExcludeBuilder $command): string |
125
|
|
|
{ |
126
|
|
|
return ExcludeCompiler::compile($this, $blueprint, $command); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function compileCheck(Blueprint $blueprint, CheckBuilder $command): string |
130
|
|
|
{ |
131
|
|
|
return CheckCompiler::compile($this, $blueprint, $command); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function typeNumeric(Fluent $column): string |
135
|
|
|
{ |
136
|
|
|
$type = 'numeric'; |
137
|
|
|
$precision = $column->get('precision'); |
138
|
|
|
$scale = $column->get('scale'); |
139
|
|
|
|
140
|
|
|
if ($precision && $scale) { |
141
|
|
|
return "${type}({$precision}, {$scale})"; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if ($precision) { |
145
|
|
|
return "${type}({$precision})"; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $type; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected function typeTsrange(Fluent $column): string |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
return 'tsrange'; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.