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