Passed
Pull Request — master (#800)
by
unknown
02:32
created

AbstractDDLQueryBuilder::addPrimaryKey()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\QueryBuilder;
6
7
use Yiisoft\Db\Exception\NotSupportedException;
8
use Yiisoft\Db\Query\QueryInterface;
9
use Yiisoft\Db\Schema\Builder\ColumnInterface;
10
use Yiisoft\Db\Schema\QuoterInterface;
11
use Yiisoft\Db\Schema\SchemaInterface;
12
13
use function implode;
14
use function is_bool;
15
use function is_string;
16
use function preg_split;
17
18
/**
19
 * It's used to create and change the structure of database objects in a database.
20
 *
21
 * These database objects include views, schemas, tables, indexes, etc.
22
 *
23
 * @link https://en.wikipedia.org/wiki/Data_definition_language
24
 */
25
abstract class AbstractDDLQueryBuilder implements DDLQueryBuilderInterface
26
{
27
    public function __construct(
28
        protected QueryBuilderInterface $queryBuilder,
29
        protected QuoterInterface $quoter,
30
        protected SchemaInterface $schema
31
    ) {
32
    }
33
34
    public function addCheck(string $table, string $name, string $expression): string
35
    {
36
        return 'ALTER TABLE '
37
            . $this->quoter->quoteTableName($table)
38
            . ' ADD CONSTRAINT '
39
            . $this->quoter->quoteColumnName($name)
40
            . ' CHECK (' . $this->quoter->quoteSql($expression) . ')';
41
    }
42
43
    public function addColumn(string $table, string $column, string $type): string
44
    {
45
        return 'ALTER TABLE '
46
            . $this->quoter->quoteTableName($table)
47
            . ' ADD '
48
            . $this->quoter->quoteColumnName($column)
49
            . ' '
50
            . $this->queryBuilder->getColumnType($type);
51
    }
52
53
    public function addCommentOnColumn(string $table, string $column, string $comment): string
54
    {
55
        return 'COMMENT ON COLUMN '
56
            . $this->quoter->quoteTableName($table)
57
            . '.'
58
            . $this->quoter->quoteColumnName($column)
59
            . ' IS '
60
            . (string) $this->quoter->quoteValue($comment);
61
    }
62
63
    public function addCommentOnTable(string $table, string $comment): string
64
    {
65
        return 'COMMENT ON TABLE '
66
            . $this->quoter->quoteTableName($table)
67
            . ' IS '
68
            . (string) $this->quoter->quoteValue($comment);
69
    }
70
71
    public function addDefaultValue(string $table, string $name, string $column, mixed $value): string
72
    {
73
        throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.');
74
    }
75
76
    public function addForeignKey(
77
        string $table,
78
        string $name,
79
        array|string $columns,
80
        string $referenceTable,
81
        array|string $referenceColumns,
82
        string|null $delete = null,
83
        string|null $update = null
84
    ): string {
85
        $sql = 'ALTER TABLE '
86
            . $this->quoter->quoteTableName($table)
87
            . ' ADD CONSTRAINT ' . $this->quoter->quoteColumnName($name)
88
            . ' FOREIGN KEY (' . $this->queryBuilder->buildColumns($columns) . ')'
89
            . ' REFERENCES ' . $this->quoter->quoteTableName($referenceTable)
90
            . ' (' . $this->queryBuilder->buildColumns($referenceColumns) . ')';
91
92
        if ($delete !== null) {
93
            $sql .= ' ON DELETE ' . $delete;
94
        }
95
96
        if ($update !== null) {
97
            $sql .= ' ON UPDATE ' . $update;
98
        }
99
100
        return $sql;
101
    }
102
103
    public function addPrimaryKey(string $table, string $name, array|string $columns): string
104
    {
105
        if (is_string($columns)) {
0 ignored issues
show
introduced by
The condition is_string($columns) is always false.
Loading history...
106
            $columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY);
107
        }
108
109
        /** @psalm-var string[] $columns */
110
        foreach ($columns as $i => $col) {
111
            $columns[$i] = $this->quoter->quoteColumnName($col);
112
        }
113
114
        return 'ALTER TABLE '
115
            . $this->quoter->quoteTableName($table)
116
            . ' ADD CONSTRAINT ' . $this->quoter->quoteColumnName($name)
117
            . ' PRIMARY KEY (' . implode(', ', $columns) . ')';
118
    }
119
120
    public function addUnique(string $table, string $name, array|string $columns): string
121
    {
122
        if (is_string($columns)) {
0 ignored issues
show
introduced by
The condition is_string($columns) is always false.
Loading history...
123
            $columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY);
124
        }
125
126
        /** @psalm-var string[] $columns */
127
        foreach ($columns as $i => $col) {
128
            $columns[$i] = $this->quoter->quoteColumnName($col);
129
        }
130
131
        return 'ALTER TABLE '
132
            . $this->quoter->quoteTableName($table)
133
            . ' ADD CONSTRAINT ' . $this->quoter->quoteColumnName($name)
134
            . ' UNIQUE (' . implode(', ', $columns) . ')';
135
    }
136
137
    public function alterColumn(
138
        string $table,
139
        string $column,
140
        ColumnInterface|string $type
141
    ): string {
142
        return 'ALTER TABLE '
143
            . $this->quoter->quoteTableName($table)
144
            . ' CHANGE '
145
            . $this->quoter->quoteColumnName($column)
146
            . ' '
147
            . $this->quoter->quoteColumnName($column) . ' '
148
            . $this->queryBuilder->getColumnType($type);
149
    }
150
151
    public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string
152
    {
153
        throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.');
154
    }
155
156
    public function createIndex(
157
        string $table,
158
        string $name,
159
        array|string $columns,
160
        string $indexType = null,
161
        string $indexMethod = null
162
    ): string {
163
        return 'CREATE ' . ($indexType ? ($indexType . ' ') : '') . 'INDEX '
164
            . $this->quoter->quoteTableName($name)
165
            . ' ON ' . $this->quoter->quoteTableName($table)
166
            . ' (' . $this->queryBuilder->buildColumns($columns) . ')';
167
    }
168
169
    public function createTable(string $table, array $columns, string $options = null): string
170
    {
171
        $cols = [];
172
173
        /** @psalm-var string[] $columns */
174
        foreach ($columns as $name => $type) {
175
            if (is_string($name)) {
176
                $cols[] = "\t"
177
                    . $this->quoter->quoteColumnName($name)
178
                    . ' '
179
                    . $this->queryBuilder->getColumnType($type);
180
            } else {
181
                $cols[] = "\t" . $type;
182
            }
183
        }
184
185
        $sql = 'CREATE TABLE '
186
            . $this->quoter->quoteTableName($table) . " (\n" . implode(",\n", $cols) . "\n)";
187
188
        return $options === null ? $sql : $sql . ' ' . $options;
189
    }
190
191
    public function createView(string $viewName, QueryInterface|string $subQuery): string
192
    {
193
        if ($subQuery instanceof QueryInterface) {
194
            [$rawQuery, $params] = $this->queryBuilder->build($subQuery);
195
196
            /** @psalm-var mixed $value */
197
            foreach ($params as $key => $value) {
198
                /** @psalm-var mixed */
199
                $params[$key] = $this->quoter->quoteValue($value);
200
            }
201
202
            $subQuery = strtr($rawQuery, $params);
203
        }
204
205
        return 'CREATE VIEW ' . $this->quoter->quoteTableName($viewName) . ' AS ' . $subQuery;
206
    }
207
208
    public function dropCheck(string $table, string $name): string
209
    {
210
        return 'ALTER TABLE '
211
            . $this->quoter->quoteTableName($table)
212
            . ' DROP CONSTRAINT '
213
            . $this->quoter->quoteColumnName($name);
214
    }
215
216
    public function dropColumn(string $table, string $column): string
217
    {
218
        return 'ALTER TABLE '
219
            . $this->quoter->quoteTableName($table)
220
            . ' DROP COLUMN '
221
            . $this->quoter->quoteColumnName($column);
222
    }
223
224
    public function dropCommentFromColumn(string $table, string $column): string
225
    {
226
        return 'COMMENT ON COLUMN '
227
            . $this->quoter->quoteTableName($table)
228
            . '.'
229
            . $this->quoter->quoteColumnName($column)
230
            . ' IS NULL';
231
    }
232
233
    public function dropCommentFromTable(string $table): string
234
    {
235
        return 'COMMENT ON TABLE '
236
             . $this->quoter->quoteTableName($table)
237
             . ' IS NULL';
238
    }
239
240
    public function dropDefaultValue(string $table, string $name): string
241
    {
242
        throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.');
243
    }
244
245
    public function dropForeignKey(string $table, string $name): string
246
    {
247
        return 'ALTER TABLE '
248
            . $this->quoter->quoteTableName($table)
249
            . ' DROP CONSTRAINT '
250
            . $this->quoter->quoteColumnName($name);
251
    }
252
253
    public function dropIndex(string $table, string $name): string
254
    {
255
        return 'DROP INDEX '
256
            . $this->quoter->quoteTableName($name)
257
            . ' ON '
258
            . $this->quoter->quoteTableName($table);
259
    }
260
261
    public function dropPrimaryKey(string $table, string $name): string
262
    {
263
        return 'ALTER TABLE '
264
            . $this->quoter->quoteTableName($table)
265
            . ' DROP CONSTRAINT '
266
            . $this->quoter->quoteColumnName($name);
267
    }
268
269
    public function dropTable(string $table): string
270
    {
271
        return 'DROP TABLE ' . $this->quoter->quoteTableName($table);
272
    }
273
274
    public function dropUnique(string $table, string $name): string
275
    {
276
        return 'ALTER TABLE '
277
            . $this->quoter->quoteTableName($table)
278
            . ' DROP CONSTRAINT '
279
            . $this->quoter->quoteColumnName($name);
280
    }
281
282
    public function dropView(string $viewName): string
283
    {
284
        return 'DROP VIEW ' . $this->quoter->quoteTableName($viewName);
285
    }
286
287
    public function renameColumn(string $table, string $oldName, string $newName): string
288
    {
289
        return 'ALTER TABLE '
290
            . $this->quoter->quoteTableName($table)
291
            . ' RENAME COLUMN ' . $this->quoter->quoteColumnName($oldName)
292
            . ' TO ' . $this->quoter->quoteColumnName($newName);
293
    }
294
295
    public function renameTable(string $oldName, string $newName): string
296
    {
297
        return 'RENAME TABLE '
298
            . $this->quoter->quoteTableName($oldName)
299
            . ' TO ' . $this->quoter->quoteTableName($newName);
300
    }
301
302
    public function truncateTable(string $table): string
303
    {
304
        return 'TRUNCATE TABLE ' . $this->quoter->quoteTableName($table);
305
    }
306
307
    public function refreshMaterializedView(string $viewName, bool $concurrently = false, ?bool $withData = null): string
308
    {
309
        $sql = 'REFRESH MATERIALIZED VIEW ';
310
311
        if ($concurrently) {
312
            $sql .= 'CONCURRENTLY ';
313
        }
314
315
        $sql .= $this->quoter->quoteTableName($viewName);
316
317
        if (is_bool($withData)) {
318
            $sql .= ' WITH ' . ($withData ? 'DATA' : 'NO DATA');
319
        }
320
321
        return $sql;
322
    }
323
}
324