Passed
Push — master ( ad651e...c5dde6 )
by Dallas
05:37
created

PostgresGrammar::typeTstzrange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 34
    public function compileCreate(Blueprint $blueprint, Fluent $command): string
27
    {
28 34
        $like = $this->getCommandByName($blueprint, 'like');
29 34
        $ifNotExists = $this->getCommandByName($blueprint, 'ifNotExists');
30
31 34
        return CreateCompiler::compile(
32
            $this,
33
            $blueprint,
34 34
            $this->getColumns($blueprint),
35 34
            compact('like', 'ifNotExists')
36
        );
37
    }
38
39 4
    public function compileAttachPartition(Blueprint $blueprint, Fluent $command): string
40
    {
41 4
        return AttachPartitionCompiler::compile($this, $blueprint, $command);
42
    }
43
44 1
    public function compileDetachPartition(Blueprint $blueprint, Fluent $command): string
45
    {
46 1
        return sprintf(
47
            'alter table %s detach partition %s',
48 1
            $this->wrapTable($blueprint),
49 1
            $command->get('partition')
50
        );
51
    }
52
53 2
    public function compileCreateView(/** @scrutinizer ignore-unused */ Blueprint $blueprint, Fluent $command): string
54
    {
55 2
        $materialize = $command->get('materialize') ? 'materialized' : '';
56 2
        return implode(' ', array_filter([
57
            'create',
58
            $materialize,
59
            'view',
60 2
            $this->wrapTable($command->get('view')),
61
            'as',
62 2
            $command->get('select'),
63
        ]));
64
    }
65
66 2
    public function compileDropView(/** @scrutinizer ignore-unused */ Blueprint $blueprint, Fluent $command): string
67
    {
68 2
        return 'drop view ' . $this->wrapTable($command->get('view'));
69
    }
70
71 2
    public function compileViewExists(): string
72
    {
73 2
        return 'select * from information_schema.views where table_schema = ? and table_name = ?';
74
    }
75
76 1
    public function compileForeignKeysListing(string $tableName): string
77
    {
78 1
        return sprintf("
79
            SELECT
80
                kcu.column_name as source_column_name,
81
                ccu.table_name AS target_table_name,
82
                ccu.column_name AS target_column_name
83
            FROM
84
                information_schema.table_constraints AS tc
85
                    JOIN information_schema.key_column_usage AS kcu
86
                         ON tc.constraint_name = kcu.constraint_name
87
                             AND tc.table_schema = kcu.table_schema
88
                    JOIN information_schema.constraint_column_usage AS ccu
89
                         ON ccu.constraint_name = tc.constraint_name
90
                             AND ccu.table_schema = tc.table_schema
91
            WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='%s';
92
        ", $tableName);
93
    }
94
95 2
    public function compileViewDefinition(): string
96
    {
97 2
        return 'select view_definition from information_schema.views where table_schema = ? and table_name = ?';
98
    }
99
100 13
    public function compileUniquePartial(Blueprint $blueprint, UniqueBuilder $command): string
101
    {
102 13
        $constraints = $command->get('constraints');
103 13
        if ($constraints instanceof UniquePartialBuilder) {
104 12
            return UniqueCompiler::compile($this, $blueprint, $command, $constraints);
105
        }
106 1
        return $this->compileUnique($blueprint, $command);
107
    }
108
109 6
    public function compileExclude(Blueprint $blueprint, ExcludeBuilder $command): string
110
    {
111 6
        return ExcludeCompiler::compile($this, $blueprint, $command);
112
    }
113
114 3
    public function compileCheck(Blueprint $blueprint, CheckBuilder $command): string
115
    {
116 3
        return CheckCompiler::compile($this, $blueprint, $command);
117
    }
118
119 3
    protected function typeNumeric(Fluent $column): string
120
    {
121 3
        $type = NumericType::TYPE_NAME;
122 3
        $precision = $column->get('precision');
123 3
        $scale = $column->get('scale');
124
125 3
        if ($precision && $scale) {
126 1
            return "${type}({$precision}, {$scale})";
127
        }
128
129 2
        if ($precision) {
130 1
            return "${type}({$precision})";
131
        }
132
133 1
        return $type;
134
    }
135
136 1
    protected function typeTsrange(/** @scrutinizer ignore-unused */ Fluent $column): string
137
    {
138 1
        return TsRangeType::TYPE_NAME;
139
    }
140
141 1
    protected function typeTstzrange(/** @scrutinizer ignore-unused */ Fluent $column): string
142
    {
143 1
        return TsTzRangeType::TYPE_NAME;
144
    }
145
146 1
    protected function typeDaterange(/** @scrutinizer ignore-unused */ Fluent $column): string
147
    {
148 1
        return DateRangeType::TYPE_NAME;
149
    }
150
}
151