Passed
Push — master ( b11800...947896 )
by Wilmer
20:19 queued 17:23
created

AbstractDDLQueryBuilder::createView()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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