Passed
Push — master ( aeb9c0...cd9c67 )
by Dallas
05:37
created

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