Passed
Branch dev (f56f10)
by Wilmer
04:41 queued 01:34
created

DDLQueryBuilder::truncateTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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
    public function addCommentOnTable(string $table, string $comment): string
45
    {
46
        return 'COMMENT ON TABLE '
47
            . $this->queryBuilder->quoter()->quoteTableName($table)
48
            . ' IS '
49
            . $this->queryBuilder->quoter()->quoteValue($comment);
50
    }
51
52
    public function addDefaultValue(string $name, string $table, string $column, mixed $value): string
0 ignored issues
show
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

52
    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 $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

52
    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...
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

52
    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 $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

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

127
    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...
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

127
    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 $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

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

209
    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...
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

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