1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Mysql; |
6
|
|
|
|
7
|
|
|
use Throwable; |
8
|
|
|
use Yiisoft\Db\Exception\Exception; |
9
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
10
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
11
|
|
|
use Yiisoft\Db\QueryBuilder\DDLQueryBuilder as AbstractDDLQueryBuilder; |
12
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
13
|
|
|
use Yiisoft\Db\Schema\QuoterInterface; |
14
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
15
|
|
|
|
16
|
|
|
use function preg_match; |
17
|
|
|
use function preg_replace; |
18
|
|
|
use function trim; |
19
|
|
|
|
20
|
|
|
final class DDLQueryBuilder extends AbstractDDLQueryBuilder |
21
|
|
|
{ |
22
|
467 |
|
public function __construct( |
23
|
|
|
private QueryBuilderInterface $queryBuilder, |
24
|
|
|
private QuoterInterface $quoter, |
25
|
|
|
private SchemaInterface $schema |
26
|
|
|
) { |
27
|
467 |
|
parent::__construct($queryBuilder, $quoter, $schema); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @throws NotSupportedException |
32
|
|
|
*/ |
33
|
1 |
|
public function addCheck(string $name, string $table, string $expression): string |
34
|
|
|
{ |
35
|
1 |
|
throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @throws Exception|Throwable |
40
|
|
|
*/ |
41
|
5 |
|
public function addCommentOnColumn(string $table, string $column, string $comment): string |
42
|
|
|
{ |
43
|
|
|
/* Strip existing comment which may include escaped quotes */ |
44
|
5 |
|
$definition = trim( |
45
|
5 |
|
preg_replace( |
46
|
5 |
|
"/COMMENT '(?:''|[^'])*'/i", |
47
|
5 |
|
'', |
48
|
5 |
|
$this->getColumnDefinition($table, $column) |
49
|
5 |
|
) |
50
|
5 |
|
); |
51
|
|
|
|
52
|
5 |
|
$checkRegex = '/CHECK *(\(([^()]|(?-2))*\))/'; |
53
|
5 |
|
$check = preg_match($checkRegex, $definition, $checkMatches); |
54
|
|
|
|
55
|
5 |
|
if ($check === 1) { |
56
|
|
|
$definition = preg_replace($checkRegex, '', $definition); |
57
|
|
|
} |
58
|
|
|
|
59
|
5 |
|
$alterSql = 'ALTER TABLE ' |
60
|
5 |
|
. $this->quoter->quoteTableName($table) |
61
|
5 |
|
. ' CHANGE ' |
62
|
5 |
|
. $this->quoter->quoteColumnName($column) |
63
|
5 |
|
. ' ' |
64
|
5 |
|
. $this->quoter->quoteColumnName($column) |
65
|
5 |
|
. (empty($definition) ? '' : ' ' . $definition) |
66
|
5 |
|
. ' COMMENT ' |
67
|
5 |
|
. (string) $this->quoter->quoteValue($comment); |
68
|
|
|
|
69
|
5 |
|
if ($check === 1) { |
70
|
|
|
$alterSql .= ' ' . $checkMatches[0]; |
71
|
|
|
} |
72
|
|
|
|
73
|
5 |
|
return $alterSql; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @throws \Exception |
78
|
|
|
*/ |
79
|
4 |
|
public function addCommentOnTable(string $table, string $comment): string |
80
|
|
|
{ |
81
|
4 |
|
return 'ALTER TABLE ' |
82
|
4 |
|
. $this->quoter->quoteTableName($table) |
83
|
4 |
|
. ' COMMENT ' |
84
|
4 |
|
. (string) $this->quoter->quoteValue($comment); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @throws NotSupportedException |
89
|
|
|
*/ |
90
|
1 |
|
public function addDefaultValue(string $name, string $table, string $column, mixed $value): string |
91
|
|
|
{ |
92
|
1 |
|
throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @throws Exception|InvalidArgumentException |
97
|
|
|
*/ |
98
|
9 |
|
public function createIndex( |
99
|
|
|
string $name, |
100
|
|
|
string $table, |
101
|
|
|
array|string $columns, |
102
|
|
|
string $indexType = null, |
103
|
|
|
string $indexMethod = null |
104
|
|
|
): string { |
105
|
9 |
|
return 'CREATE ' . ($indexType ? ($indexType . ' ') : '') . 'INDEX ' |
106
|
9 |
|
. $this->quoter->quoteTableName($name) |
107
|
9 |
|
. ($indexMethod !== null ? " USING $indexMethod" : '') |
108
|
9 |
|
. ' ON ' . $this->quoter->quoteTableName($table) |
109
|
9 |
|
. ' (' . $this->queryBuilder->buildColumns($columns) . ')'; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string |
113
|
|
|
{ |
114
|
1 |
|
return 'SET FOREIGN_KEY_CHECKS = ' . ($check ? 1 : 0); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @throws NotSupportedException |
119
|
|
|
*/ |
120
|
1 |
|
public function dropCheck(string $name, string $table): string |
121
|
|
|
{ |
122
|
1 |
|
throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @throws Exception|Throwable |
127
|
|
|
*/ |
128
|
2 |
|
public function dropCommentFromColumn(string $table, string $column): string |
129
|
|
|
{ |
130
|
2 |
|
return $this->addCommentOnColumn($table, $column, ''); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @throws \Exception |
135
|
|
|
*/ |
136
|
2 |
|
public function dropCommentFromTable(string $table): string |
137
|
|
|
{ |
138
|
2 |
|
return $this->addCommentOnTable($table, ''); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @throws NotSupportedException |
143
|
|
|
*/ |
144
|
1 |
|
public function dropDefaultValue(string $name, string $table): string |
145
|
|
|
{ |
146
|
1 |
|
throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.'); |
147
|
|
|
} |
148
|
|
|
|
149
|
2 |
|
public function dropForeignKey(string $name, string $table): string |
150
|
|
|
{ |
151
|
2 |
|
return 'ALTER TABLE ' |
152
|
2 |
|
. $this->quoter->quoteTableName($table) |
153
|
2 |
|
. ' DROP FOREIGN KEY ' |
154
|
2 |
|
. $this->quoter->quoteColumnName($name); |
155
|
|
|
} |
156
|
|
|
|
157
|
2 |
|
public function dropPrimaryKey(string $name, string $table): string |
158
|
|
|
{ |
159
|
2 |
|
return 'ALTER TABLE ' . $this->quoter->quoteTableName($table) . ' DROP PRIMARY KEY'; |
160
|
|
|
} |
161
|
|
|
|
162
|
2 |
|
public function dropUnique(string $name, string $table): string |
163
|
|
|
{ |
164
|
2 |
|
return $this->dropIndex($name, $table); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @throws Exception|Throwable |
169
|
|
|
*/ |
170
|
2 |
|
public function renameColumn(string $table, string $oldName, string $newName): string |
171
|
|
|
{ |
172
|
2 |
|
$quotedTable = $this->quoter->quoteTableName($table); |
173
|
|
|
|
174
|
2 |
|
$columnDefinition = $this->getColumnDefinition($table, $oldName); |
175
|
|
|
|
176
|
|
|
/* try to give back a SQL anyway */ |
177
|
2 |
|
return "ALTER TABLE $quotedTable CHANGE " |
178
|
2 |
|
. $this->quoter->quoteColumnName($oldName) . ' ' |
179
|
2 |
|
. $this->quoter->quoteColumnName($newName) |
180
|
2 |
|
. (!empty($columnDefinition) ? ' ' . $columnDefinition : ''); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Gets column definition. |
185
|
|
|
* |
186
|
|
|
* @param string $table table name. |
187
|
|
|
* @param string $column column name. |
188
|
|
|
* |
189
|
|
|
* @throws Throwable in case when table does not contain column. |
190
|
|
|
* |
191
|
|
|
* @return string the column definition. |
192
|
|
|
*/ |
193
|
7 |
|
public function getColumnDefinition(string $table, string $column): string |
194
|
|
|
{ |
195
|
7 |
|
$result = ''; |
196
|
|
|
|
197
|
7 |
|
$sql = $this->schema->getTableSchema($table)?->getCreateSql(); |
198
|
|
|
|
199
|
7 |
|
if (empty($sql)) { |
200
|
|
|
return ''; |
201
|
|
|
} |
202
|
|
|
|
203
|
7 |
|
if (preg_match_all('/^\s*`(.*?)`\s+(.*?),?$/m', $sql, $matches)) { |
204
|
7 |
|
foreach ($matches[1] as $i => $c) { |
205
|
7 |
|
if ($c === $column) { |
206
|
7 |
|
$result = $matches[2][$i]; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
7 |
|
return $result; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|