Passed
Pull Request — master (#266)
by Wilmer
10:46
created

DDLQueryBuilder::dropCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Query;
6
7
use Yiisoft\Db\Exception\NotSupportedException;
8
9
abstract class DDLQueryBuilder
10
{
11
    public function __construct(private QueryBuilderInterface $queryBuilder)
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\QueryBuilderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
    {
13
    }
14
15
    public function addCheck(string $name, string $table, string $expression): string
16
    {
17
        return 'ALTER TABLE '
18
            . $this->queryBuilder->quoter()->quoteTableName($table)
19
            . ' ADD CONSTRAINT '
20
            . $this->queryBuilder->quoter()->quoteColumnName($name)
21
            . ' CHECK (' . $this->queryBuilder->quoter()->quoteSql($expression) . ')';
22
    }
23
24
    public function addColumn(string $table, string $column, string $type): string
25
    {
26
        return 'ALTER TABLE '
27
            . $this->queryBuilder->quoter()->quoteTableName($table)
28
            . ' ADD '
29
            . $this->queryBuilder->quoter()->quoteColumnName($column)
30
            . ' '
31
            . $this->queryBuilder->getColumnType($type);
32
    }
33
34
    public function addCommentOnColumn(string $table, string $column, string $comment): string
35
    {
36
        return 'COMMENT ON COLUMN '
37
            . $this->queryBuilder->quoter()->quoteTableName($table)
38
            . '.'
39
            . $this->queryBuilder->quoter()->quoteColumnName($column)
40
            . ' IS '
41
            . $this->queryBuilder->quoter()->quoteValue($comment);
42
    }
43
44
45
    public function addCommentOnTable(string $table, string $comment): string
46
    {
47
        return 'COMMENT ON TABLE '
48
            . $this->queryBuilder->quoter()->quoteTableName($table)
49
            . ' IS '
50
            . $this->queryBuilder->quoter()->quoteValue($comment);
51
    }
52
53
    public function addDefaultValue(string $name, string $table, string $column, mixed $value): string
0 ignored issues
show
Unused Code introduced by
The parameter $name 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

53
    public function addDefaultValue(/** @scrutinizer ignore-unused */ string $name, string $table, string $column, mixed $value): 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...
Unused Code introduced by
The parameter $value 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

53
    public function addDefaultValue(string $name, string $table, string $column, /** @scrutinizer ignore-unused */ mixed $value): 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...
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

53
    public function addDefaultValue(string $name, string $table, /** @scrutinizer ignore-unused */ string $column, mixed $value): 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...
Unused Code introduced by
The parameter $table 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

53
    public function addDefaultValue(string $name, /** @scrutinizer ignore-unused */ string $table, string $column, mixed $value): 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...
54
    {
55
        throw new NotSupportedException(static::class . ' does not support adding default value constraints.');
56
    }
57
58
    public function addForeignKey(
59
        string $name,
60
        string $table,
61
        array|string $columns,
62
        string $refTable,
63
        array|string $refColumns,
64
        ?string $delete = null,
65
        ?string $update = null
66
    ): string {
67
        $sql = 'ALTER TABLE '
68
            . $this->queryBuilder->quoter()->quoteTableName($table)
69
            . ' ADD CONSTRAINT ' . $this->queryBuilder->quoter()->quoteColumnName($name)
70
            . ' FOREIGN KEY (' . $this->queryBuilder->buildColumns($columns) . ')'
71
            . ' REFERENCES ' . $this->queryBuilder->quoter()->quoteTableName($refTable)
72
            . ' (' . $this->queryBuilder->buildColumns($refColumns) . ')';
73
74
        if ($delete !== null) {
75
            $sql .= ' ON DELETE ' . $delete;
76
        }
77
78
        if ($update !== null) {
79
            $sql .= ' ON UPDATE ' . $update;
80
        }
81
82
        return $sql;
83
    }
84
85
    public function addPrimaryKey(string $name, string $table, array|string $columns): string
86
    {
87
        if (is_string($columns)) {
0 ignored issues
show
introduced by
The condition is_string($columns) is always false.
Loading history...
88
            $columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY);
89
        }
90
91
        foreach ($columns as $i => $col) {
92
            $columns[$i] = $this->queryBuilder->quoter()->quoteColumnName($col);
93
        }
94
95
        return 'ALTER TABLE '
96
            . $this->queryBuilder->quoter()->quoteTableName($table)
97
            . ' ADD CONSTRAINT ' . $this->queryBuilder->quoter()->quoteColumnName($name)
98
            . ' PRIMARY KEY (' . implode(', ', $columns) . ')';
99
    }
100
101
    public function addUnique(string $name, string $table, array|string $columns): string
102
    {
103
        if (is_string($columns)) {
0 ignored issues
show
introduced by
The condition is_string($columns) is always false.
Loading history...
104
            $columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY);
105
        }
106
107
        foreach ($columns as $i => $col) {
108
            $columns[$i] = $this->queryBuilder->quoter()->quoteColumnName($col);
109
        }
110
111
        return 'ALTER TABLE '
112
            . $this->queryBuilder->quoter()->quoteTableName($table)
113
            . ' ADD CONSTRAINT ' . $this->queryBuilder->quoter()->quoteColumnName($name)
114
            . ' UNIQUE (' . implode(', ', $columns) . ')';
115
    }
116
117
    public function alterColumn(string $table, string $column, string $type): string
118
    {
119
        return 'ALTER TABLE '
120
            . $this->queryBuilder->quoter()->quoteTableName($table)
121
            . ' CHANGE '
122
            . $this->queryBuilder->quoter()->quoteColumnName($column)
123
            . ' '
124
            . $this->queryBuilder->quoter()->quoteColumnName($column) . ' '
125
            . $this->queryBuilder->getColumnType($type);
126
    }
127
128
    public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string
0 ignored issues
show
Unused Code introduced by
The parameter $table 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

128
    public function checkIntegrity(string $schema = '', /** @scrutinizer ignore-unused */ string $table = '', bool $check = true): 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...
Unused Code introduced by
The parameter $schema 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

128
    public function checkIntegrity(/** @scrutinizer ignore-unused */ string $schema = '', string $table = '', bool $check = true): 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...
Unused Code introduced by
The parameter $check 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

128
    public function checkIntegrity(string $schema = '', string $table = '', /** @scrutinizer ignore-unused */ bool $check = true): 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...
129
    {
130
        throw new NotSupportedException(static::class . ' does not support enabling/disabling integrity check.');
131
    }
132
133
    public function createIndex(string $name, string $table, array|string $columns, bool $unique = false): string
134
    {
135
        return ($unique ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX ')
136
            . $this->queryBuilder->quoter()->quoteTableName($name)
137
            . ' ON ' . $this->queryBuilder->quoter()->quoteTableName($table)
138
            . ' (' . $this->queryBuilder->buildColumns($columns) . ')';
139
    }
140
141
    public function createTable(string $table, array $columns, ?string $options = null): string
142
    {
143
        $cols = [];
144
145
        foreach ($columns as $name => $type) {
146
            if (is_string($name)) {
147
                $cols[] = "\t"
148
                    . $this->queryBuilder->quoter()->quoteColumnName($name)
149
                    . ' '
150
                    . $this->queryBuilder->getColumnType($type);
151
            } else {
152
                $cols[] = "\t" . $type;
153
            }
154
        }
155
156
        $sql = 'CREATE TABLE '
157
            . $this->queryBuilder->quoter()->quoteTableName($table) . " (\n" . implode(",\n", $cols) . "\n)";
158
159
        return $options === null ? $sql : $sql . ' ' . $options;
160
    }
161
162
    public function createView(string $viewName, Query|string $subQuery): string
163
    {
164
        if ($subQuery instanceof Query) {
165
            /** @psalm-var array<array-key, int|string> $params */
166
            [$rawQuery, $params] = $this->queryBuilder->build($subQuery);
167
168
            foreach ($params as $key => $value) {
169
                $params[$key] = $this->queryBuilder->quoter()->quoteValue($value);
170
            }
171
172
            $subQuery = strtr($rawQuery, $params);
173
        }
174
175
        return 'CREATE VIEW ' . $this->queryBuilder->quoter()->quoteTableName($viewName) . ' AS ' . $subQuery;
176
    }
177
178
    public function dropCheck(string $name, string $table): string
179
    {
180
        return 'ALTER TABLE '
181
            . $this->queryBuilder->quoter()->quoteTableName($table)
182
            . ' DROP CONSTRAINT '
183
            . $this->queryBuilder->quoter()->quoteColumnName($name);
184
    }
185
186
    public function dropColumn(string $table, string $column): string
187
    {
188
        return 'ALTER TABLE '
189
            . $this->queryBuilder->quoter()->quoteTableName($table)
190
            . ' DROP COLUMN '
191
            . $this->queryBuilder->quoter()->quoteColumnName($column);
192
    }
193
194
    public function dropCommentFromColumn(string $table, string $column): string
195
    {
196
        return 'COMMENT ON COLUMN '
197
            . $this->queryBuilder->quoter()->quoteTableName($table)
198
            . '.'
199
            . $this->queryBuilder->quoter()->quoteColumnName($column)
200
            . ' IS NULL';
201
    }
202
203
    public function dropCommentFromTable(string $table): string
204
    {
205
        return 'COMMENT ON TABLE '
206
             . $this->queryBuilder->quoter()->quoteTableName($table)
207
             . ' IS NULL';
208
    }
209
210
    public function dropDefaultValue(string $name, string $table): string
0 ignored issues
show
Unused Code introduced by
The parameter $name 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

210
    public function dropDefaultValue(/** @scrutinizer ignore-unused */ string $name, string $table): 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...
Unused Code introduced by
The parameter $table 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

210
    public function dropDefaultValue(string $name, /** @scrutinizer ignore-unused */ string $table): 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...
211
    {
212
        throw new NotSupportedException(static::class . ' does not support dropping default value constraints.');
213
    }
214
215
    public function dropForeignKey(string $name, string $table): string
216
    {
217
        return 'ALTER TABLE '
218
            . $this->queryBuilder->quoter()->quoteTableName($table)
219
            . ' DROP CONSTRAINT '
220
            . $this->queryBuilder->quoter()->quoteColumnName($name);
221
    }
222
223
    public function dropIndex(string $name, string $table): string
224
    {
225
        return 'DROP INDEX '
226
            . $this->queryBuilder->quoter()->quoteTableName($name)
227
            . ' ON '
228
            . $this->queryBuilder->quoter()->quoteTableName($table);
229
    }
230
231
    public function dropPrimaryKey(string $name, string $table): string
232
    {
233
        return 'ALTER TABLE '
234
            . $this->queryBuilder->quoter()->quoteTableName($table)
235
            . ' DROP CONSTRAINT '
236
            . $this->queryBuilder->quoter()->quoteColumnName($name);
237
    }
238
239
    public function dropTable(string $table): string
240
    {
241
        return 'DROP TABLE ' . $this->queryBuilder->quoter()->quoteTableName($table);
242
    }
243
244
    public function dropUnique(string $name, string $table): string
245
    {
246
        return 'ALTER TABLE '
247
            . $this->queryBuilder->quoter()->quoteTableName($table)
248
            . ' DROP CONSTRAINT '
249
            . $this->queryBuilder->quoter()->quoteColumnName($name);
250
    }
251
252
    public function dropView(string $viewName): string
253
    {
254
        return 'DROP VIEW ' . $this->queryBuilder->quoter()->quoteTableName($viewName);
255
    }
256
257
    public function renameColumn(string $table, string $oldName, string $newName): string
258
    {
259
        return 'ALTER TABLE '
260
            . $this->queryBuilder->quoter()->quoteTableName($table)
261
            . ' RENAME COLUMN ' . $this->queryBuilder->quoter()->quoteColumnName($oldName)
262
            . ' TO ' . $this->queryBuilder->quoter()->quoteColumnName($newName);
263
    }
264
265
    public function renameTable(string $oldName, string $newName): string
266
    {
267
        return 'RENAME TABLE '
268
            . $this->queryBuilder->quoter()->quoteTableName($oldName)
269
            . ' TO ' . $this->queryBuilder->quoter()->quoteTableName($newName);
270
    }
271
272
    public function truncateTable(string $table): string
273
    {
274
        return 'TRUNCATE TABLE ' . $this->queryBuilder->quoter()->quoteTableName($table);
275
    }
276
}
277