Passed
Pull Request — master (#133)
by Wilmer
17:16 queued 12:38
created

DDLQueryBuilder::alterColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite;
6
7
use Yiisoft\Db\Exception\Exception;
8
use Yiisoft\Db\Exception\InvalidArgumentException;
9
use Yiisoft\Db\Exception\NotSupportedException;
10
use Yiisoft\Db\QueryBuilder\DDLQueryBuilder as AbstractDDLQueryBuilder;
11
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
12
use Yiisoft\Db\Schema\ColumnSchemaBuilder;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Db\Sqlite\ColumnSchemaBuilder. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use Yiisoft\Db\Schema\QuoterInterface;
14
use Yiisoft\Db\Schema\SchemaInterface;
15
16
use function count;
17
18
final class DDLQueryBuilder extends AbstractDDLQueryBuilder
19
{
20 455
    public function __construct(
21
        private QueryBuilderInterface $queryBuilder,
22
        private QuoterInterface $quoter,
23
        SchemaInterface $schema
24
    ) {
25 455
        parent::__construct($queryBuilder, $quoter, $schema);
26
    }
27
28
    /**
29
     * @throws NotSupportedException
30
     */
31 2
    public function addCheck(string $name, string $table, string $expression): string
32
    {
33 2
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
34
    }
35
36
    /**
37
     * @throws NotSupportedException
38
     */
39 2
    public function addCommentOnColumn(string $table, string $column, string $comment): string
40
    {
41 2
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
42
    }
43
44
    /**
45
     * @throws NotSupportedException
46
     */
47 2
    public function addCommentOnTable(string $table, string $comment): string
48
    {
49 2
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
50
    }
51
52
    /**
53
     * @throws NotSupportedException
54
     */
55 4
    public function addForeignKey(
56
        string $name,
57
        string $table,
58
        array|string $columns,
59
        string $refTable,
60
        array|string $refColumns,
61
        string $delete = null,
62
        string $update = null
63
    ): string {
64 4
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
65
    }
66
67
    /**
68
     * @throws NotSupportedException
69
     */
70 2
    public function addPrimaryKey(string $name, string $table, array|string $columns): string
71
    {
72 2
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
73
    }
74
75
    /**
76
     * @throws NotSupportedException
77
     */
78 2
    public function addUnique(string $name, string $table, array|string $columns): string
79
    {
80 2
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
81
    }
82
83
    /**
84
     * @throws NotSupportedException
85
     */
86 17
    public function alterColumn(string $table, string $column, ColumnSchemaBuilder|string $type): string
87
    {
88 17
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
89
    }
90
91 3
    public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string
92
    {
93 3
        return 'PRAGMA foreign_keys=' . (int) $check;
94
    }
95
96
    /**
97
     * @throws Exception|InvalidArgumentException
98
     */
99 13
    public function createIndex(
100
        string $name,
101
        string $table,
102
        array|string $columns,
103
        string $indexType = null,
104
        string $indexMethod = null
105
    ): string {
106 13
        $tableParts = explode('.', $table);
107
108 13
        $schema = null;
109 13
        if (count($tableParts) === 2) {
110 1
            [$schema, $table] = $tableParts;
111
        }
112
113 13
        return 'CREATE ' . ($indexType ? ($indexType . ' ') : '') . 'INDEX '
114 13
            . $this->quoter->quoteTableName(($schema ? $schema . '.' : '') . $name)
115
            . ' ON '
116 13
            . $this->quoter->quoteTableName($table)
117 13
            . ' (' . $this->queryBuilder->buildColumns($columns) . ')';
118
    }
119
120
    /**
121
     * @throws NotSupportedException
122
     */
123 4
    public function dropCheck(string $name, string $table): string
124
    {
125 4
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
126
    }
127
128
    /**
129
     * @throws NotSupportedException
130
     */
131 2
    public function dropColumn(string $table, string $column): string
132
    {
133 2
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
134
    }
135
136
    /**
137
     * @throws NotSupportedException
138
     */
139 2
    public function dropCommentFromColumn(string $table, string $column): string
140
    {
141 2
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
142
    }
143
144
    /**
145
     * @throws NotSupportedException
146
     */
147 2
    public function dropCommentFromTable(string $table): string
148
    {
149 2
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
150
    }
151
152
    /**
153
     * @throws NotSupportedException
154
     */
155 5
    public function dropForeignKey(string $name, string $table): string
156
    {
157 5
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
158
    }
159
160 3
    public function dropIndex(string $name, string $table): string
161
    {
162 3
        return 'DROP INDEX ' . $this->quoter->quoteTableName($name);
163
    }
164
165
    /**
166
     * @throws NotSupportedException
167
     */
168 5
    public function dropPrimaryKey(string $name, string $table): string
169
    {
170 5
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
171
    }
172
173
    /**
174
     * @throws NotSupportedException
175
     */
176 4
    public function dropUnique(string $name, string $table): string
177
    {
178 4
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
179
    }
180
181
    /**
182
     * @throws NotSupportedException
183
     */
184 2
    public function renameColumn(string $table, string $oldName, string $newName): string
185
    {
186 2
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
187
    }
188
189 3
    public function renameTable(string $oldName, string $newName): string
190
    {
191
        return 'ALTER TABLE '
192 3
            . $this->quoter->quoteTableName($oldName)
193
            . ' RENAME TO '
194 3
            . $this->quoter->quoteTableName($newName);
195
    }
196
197 2
    public function truncateTable(string $table): string
198
    {
199 2
        return 'DELETE FROM ' . $this->quoter->quoteTableName($table);
200
    }
201
}
202