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