Failed Conditions
Pull Request — master (#28)
by Dallas
08:29
created

PostgresGrammar   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
eloc 41
c 1
b 0
f 0
dl 0
loc 122
rs 10

18 Methods

Rating   Name   Duplication   Size   Complexity  
A compileCreateProcedure() 0 3 1
A compileDetachPartition() 0 5 1
A compileCreateTrigger() 0 3 1
A compileAttachPartition() 0 3 1
A compileViewExists() 0 3 1
A compileUniquePartial() 0 7 2
A compileExclude() 0 3 1
A compileCreate() 0 10 1
A typeTsrange() 0 3 1
A compileCreateView() 0 7 2
A compileViewDefinition() 0 3 1
A compileCheck() 0 3 1
A compileDropView() 0 3 1
A compileDropTrigger() 0 3 1
A compileDropProcedure() 0 3 1
A compileDropFunction() 0 3 1
A compileCreateFunction() 0 3 1
A typeNumeric() 0 15 4
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
0 ignored issues
show
Unused Code introduced by
The parameter $blueprint is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

60
    public function compileCreateView(/** @scrutinizer ignore-unused */ Blueprint $blueprint, Fluent $command): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
0 ignored issues
show
Unused Code introduced by
The parameter $blueprint is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

100
    public function compileDropView(/** @scrutinizer ignore-unused */ Blueprint $blueprint, Fluent $command): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

151
    protected function typeTsrange(/** @scrutinizer ignore-unused */ Fluent $column): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
152
    {
153
        return 'tsrange';
154
    }
155
}
156