|
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\Query\DDLQueryBuilder as AbstractDDLQueryBuilder; |
|
11
|
|
|
use Yiisoft\Db\Query\QueryBuilderInterface; |
|
12
|
|
|
use Yiisoft\Db\Schema\ColumnSchemaBuilder; |
|
|
|
|
|
|
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
|
337 |
|
public function __construct( |
|
21
|
|
|
private QueryBuilderInterface $queryBuilder, |
|
22
|
|
|
private QuoterInterface $quoter, |
|
23
|
|
|
SchemaInterface $schema |
|
24
|
|
|
) { |
|
25
|
337 |
|
parent::__construct($queryBuilder, $quoter, $schema); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @throws NotSupportedException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function addCheck(string $name, string $table, string $expression): string |
|
32
|
|
|
{ |
|
33
|
|
|
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @throws NotSupportedException |
|
38
|
|
|
*/ |
|
39
|
|
|
public function addCommentOnColumn(string $table, string $column, string $comment): string |
|
40
|
|
|
{ |
|
41
|
|
|
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @throws NotSupportedException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function addCommentOnTable(string $table, string $comment): string |
|
48
|
|
|
{ |
|
49
|
|
|
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @throws NotSupportedException |
|
54
|
|
|
*/ |
|
55
|
3 |
|
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
|
3 |
|
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @throws NotSupportedException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function addPrimaryKey(string $name, string $table, array|string $columns): string |
|
71
|
|
|
{ |
|
72
|
|
|
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @throws NotSupportedException |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function addUnique(string $name, string $table, array|string $columns): string |
|
79
|
|
|
{ |
|
80
|
1 |
|
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @throws NotSupportedException |
|
85
|
|
|
*/ |
|
86
|
|
|
public function alterColumn(string $table, string $column, ColumnSchemaBuilder|string $type): string |
|
87
|
|
|
{ |
|
88
|
|
|
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string |
|
92
|
|
|
{ |
|
93
|
|
|
return 'PRAGMA foreign_keys=' . (int) $check; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @throws Exception|InvalidArgumentException |
|
98
|
|
|
*/ |
|
99
|
7 |
|
public function createIndex(string $name, string $table, array|string $columns, ?string $indexType = null, ?string $indexMethod = null): string |
|
100
|
|
|
{ |
|
101
|
7 |
|
$tableParts = explode('.', $table); |
|
102
|
|
|
|
|
103
|
7 |
|
$schema = null; |
|
104
|
7 |
|
if (count($tableParts) === 2) { |
|
105
|
1 |
|
[$schema, $table] = $tableParts; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
7 |
|
return 'CREATE ' . ($indexType ? ($indexType . ' ') : '') . 'INDEX ' |
|
109
|
7 |
|
. $this->quoter->quoteTableName(($schema ? $schema . '.' : '') . $name) |
|
110
|
|
|
. ' ON ' |
|
111
|
7 |
|
. $this->quoter->quoteTableName($table) |
|
112
|
7 |
|
. ' (' . $this->queryBuilder->buildColumns($columns) . ')'; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @throws NotSupportedException |
|
117
|
|
|
*/ |
|
118
|
|
|
public function dropCheck(string $name, string $table): string |
|
119
|
|
|
{ |
|
120
|
|
|
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @throws NotSupportedException |
|
125
|
|
|
*/ |
|
126
|
|
|
public function dropColumn(string $table, string $column): string |
|
127
|
|
|
{ |
|
128
|
|
|
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @throws NotSupportedException |
|
133
|
|
|
*/ |
|
134
|
|
|
public function dropCommentFromColumn(string $table, string $column): string |
|
135
|
|
|
{ |
|
136
|
|
|
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.'); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @throws NotSupportedException |
|
141
|
|
|
*/ |
|
142
|
|
|
public function dropCommentFromTable(string $table): string |
|
143
|
|
|
{ |
|
144
|
|
|
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @throws NotSupportedException |
|
149
|
|
|
*/ |
|
150
|
2 |
|
public function dropForeignKey(string $name, string $table): string |
|
151
|
|
|
{ |
|
152
|
2 |
|
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
2 |
|
public function dropIndex(string $name, string $table): string |
|
156
|
|
|
{ |
|
157
|
2 |
|
return 'DROP INDEX ' . $this->quoter->quoteTableName($name); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @throws NotSupportedException |
|
162
|
|
|
*/ |
|
163
|
|
|
public function dropPrimaryKey(string $name, string $table): string |
|
164
|
|
|
{ |
|
165
|
|
|
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.'); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @throws NotSupportedException |
|
170
|
|
|
*/ |
|
171
|
1 |
|
public function dropUnique(string $name, string $table): string |
|
172
|
|
|
{ |
|
173
|
1 |
|
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.'); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @throws NotSupportedException |
|
178
|
|
|
*/ |
|
179
|
|
|
public function renameColumn(string $table, string $oldName, string $newName): string |
|
180
|
|
|
{ |
|
181
|
|
|
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.'); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
3 |
|
public function renameTable(string $oldName, string $newName): string |
|
185
|
|
|
{ |
|
186
|
|
|
return 'ALTER TABLE ' |
|
187
|
3 |
|
. $this->quoter->quoteTableName($oldName) |
|
188
|
|
|
. ' RENAME TO ' |
|
189
|
3 |
|
. $this->quoter->quoteTableName($newName); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: