|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Schema; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Platforms\MariaDb102Platform; |
|
23
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform; |
|
24
|
|
|
use Doctrine\DBAL\Types\Type; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Schema manager for the MySql RDBMS. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Konsta Vesterinen <[email protected]> |
|
30
|
|
|
* @author Lukas Smith <[email protected]> (PEAR MDB2 library) |
|
31
|
|
|
* @author Roman Borschel <[email protected]> |
|
32
|
|
|
* @author Benjamin Eberlei <[email protected]> |
|
33
|
|
|
* @since 2.0 |
|
34
|
|
|
*/ |
|
35
|
|
|
class MySqlSchemaManager extends AbstractSchemaManager |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function _getPortableViewDefinition($view) |
|
41
|
|
|
{ |
|
42
|
|
|
return new View($view['TABLE_NAME'], $view['VIEW_DEFINITION']); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function _getPortableTableDefinition($table) |
|
49
|
|
|
{ |
|
50
|
|
|
return array_shift($table); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function _getPortableUserDefinition($user) |
|
57
|
|
|
{ |
|
58
|
|
|
return [ |
|
59
|
|
|
'user' => $user['User'], |
|
60
|
|
|
'password' => $user['Password'], |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function _getPortableTableIndexesList($tableIndexes, $tableName=null) |
|
68
|
|
|
{ |
|
69
|
|
|
foreach ($tableIndexes as $k => $v) { |
|
70
|
|
|
$v = array_change_key_case($v, CASE_LOWER); |
|
71
|
|
|
if ($v['key_name'] === 'PRIMARY') { |
|
72
|
|
|
$v['primary'] = true; |
|
73
|
|
|
} else { |
|
74
|
|
|
$v['primary'] = false; |
|
75
|
|
|
} |
|
76
|
|
|
if (strpos($v['index_type'], 'FULLTEXT') !== false) { |
|
77
|
|
|
$v['flags'] = ['FULLTEXT']; |
|
78
|
|
|
} elseif (strpos($v['index_type'], 'SPATIAL') !== false) { |
|
79
|
|
|
$v['flags'] = ['SPATIAL']; |
|
80
|
|
|
} |
|
81
|
|
|
$tableIndexes[$k] = $v; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return parent::_getPortableTableIndexesList($tableIndexes, $tableName); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function _getPortableSequenceDefinition($sequence) |
|
91
|
|
|
{ |
|
92
|
|
|
return end($sequence); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function _getPortableDatabaseDefinition($database) |
|
99
|
|
|
{ |
|
100
|
|
|
return $database['Database']; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function _getPortableTableColumnDefinition($tableColumn) |
|
107
|
|
|
{ |
|
108
|
|
|
$tableColumn = array_change_key_case($tableColumn, CASE_LOWER); |
|
109
|
|
|
|
|
110
|
|
|
$dbType = strtolower($tableColumn['type']); |
|
111
|
|
|
$dbType = strtok($dbType, '(), '); |
|
112
|
|
|
if (isset($tableColumn['length'])) { |
|
113
|
|
|
$length = $tableColumn['length']; |
|
114
|
|
|
} else { |
|
115
|
|
|
$length = strtok('(), '); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$fixed = null; |
|
119
|
|
|
|
|
120
|
|
|
if ( ! isset($tableColumn['name'])) { |
|
121
|
|
|
$tableColumn['name'] = ''; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$scale = null; |
|
125
|
|
|
$precision = null; |
|
126
|
|
|
|
|
127
|
|
|
$type = $this->_platform->getDoctrineTypeMapping($dbType); |
|
128
|
|
|
|
|
129
|
|
|
// In cases where not connected to a database DESCRIBE $table does not return 'Comment' |
|
130
|
|
|
if (isset($tableColumn['comment'])) { |
|
131
|
|
|
$type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type); |
|
132
|
|
|
$tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
switch ($dbType) { |
|
136
|
|
|
case 'char': |
|
137
|
|
|
case 'binary': |
|
138
|
|
|
$fixed = true; |
|
139
|
|
|
break; |
|
140
|
|
|
case 'float': |
|
141
|
|
|
case 'double': |
|
142
|
|
|
case 'real': |
|
143
|
|
|
case 'numeric': |
|
144
|
|
|
case 'decimal': |
|
145
|
|
|
if (preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['type'], $match)) { |
|
146
|
|
|
$precision = $match[1]; |
|
147
|
|
|
$scale = $match[2]; |
|
148
|
|
|
$length = null; |
|
149
|
|
|
} |
|
150
|
|
|
break; |
|
151
|
|
|
case 'tinytext': |
|
152
|
|
|
$length = MySqlPlatform::LENGTH_LIMIT_TINYTEXT; |
|
153
|
|
|
break; |
|
154
|
|
|
case 'text': |
|
155
|
|
|
$length = MySqlPlatform::LENGTH_LIMIT_TEXT; |
|
156
|
|
|
break; |
|
157
|
|
|
case 'mediumtext': |
|
158
|
|
|
$length = MySqlPlatform::LENGTH_LIMIT_MEDIUMTEXT; |
|
159
|
|
|
break; |
|
160
|
|
|
case 'tinyblob': |
|
161
|
|
|
$length = MySqlPlatform::LENGTH_LIMIT_TINYBLOB; |
|
162
|
|
|
break; |
|
163
|
|
|
case 'blob': |
|
164
|
|
|
$length = MySqlPlatform::LENGTH_LIMIT_BLOB; |
|
165
|
|
|
break; |
|
166
|
|
|
case 'mediumblob': |
|
167
|
|
|
$length = MySqlPlatform::LENGTH_LIMIT_MEDIUMBLOB; |
|
168
|
|
|
break; |
|
169
|
|
|
case 'tinyint': |
|
170
|
|
|
case 'smallint': |
|
171
|
|
|
case 'mediumint': |
|
172
|
|
|
case 'int': |
|
173
|
|
|
case 'integer': |
|
174
|
|
|
case 'bigint': |
|
175
|
|
|
case 'year': |
|
176
|
|
|
$length = null; |
|
177
|
|
|
break; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$length = ((int) $length) ?: null; |
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
$isNotNull = ($tableColumn['null'] !== 'YES'); |
|
184
|
|
|
|
|
185
|
|
|
if ($this->_platform instanceof MariaDb102Platform) { |
|
186
|
|
|
$columnDefault = $this->getMariaDb102ColumnDefault($tableColumn['default']); |
|
187
|
|
|
} else { |
|
188
|
|
|
$columnDefault = (isset($tableColumn['default'])) ? $tableColumn['default'] : null; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$options = [ |
|
192
|
|
|
'length' => $length, |
|
193
|
|
|
'unsigned' => (strpos($tableColumn['type'], 'unsigned') !== false), |
|
194
|
|
|
'fixed' => (bool) $fixed, |
|
195
|
|
|
'default' => $columnDefault, |
|
196
|
|
|
'notnull' => $isNotNull, |
|
197
|
|
|
'scale' => null, |
|
198
|
|
|
'precision' => null, |
|
199
|
|
|
'autoincrement' => (strpos($tableColumn['extra'], 'auto_increment') !== false), |
|
200
|
|
|
'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== '' |
|
201
|
|
|
? $tableColumn['comment'] |
|
202
|
|
|
: null, |
|
203
|
|
|
]; |
|
204
|
|
|
|
|
205
|
|
|
if ($scale !== null && $precision !== null) { |
|
206
|
|
|
$options['scale'] = $scale; |
|
207
|
|
|
$options['precision'] = $precision; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
$column = new Column($tableColumn['field'], Type::getType($type), $options); |
|
211
|
|
|
|
|
212
|
|
|
if (isset($tableColumn['collation'])) { |
|
213
|
|
|
$column->setPlatformOption('collation', $tableColumn['collation']); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
return $column; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Get a doctrine/mysql compatible column default from |
|
222
|
|
|
* the new information_schema behaviour introduced in MariaDb >= 10.2.7. |
|
223
|
|
|
* |
|
224
|
|
|
* @param null|string $columnDefault default value as stored in information_schema for MariaDB >= 10.2.7 |
|
225
|
|
|
*/ |
|
226
|
|
|
private function getMariaDb102ColumnDefault(?string $columnDefault): ?string { |
|
227
|
|
|
$defaultValue = ($columnDefault === 'NULL') ? null : $columnDefault; |
|
228
|
|
|
if ($defaultValue !== null && ($defaultValue[0] ?? '') === "'") { |
|
229
|
|
|
// Get an unquoted version |
|
230
|
|
|
$defaultValue = preg_replace('/^\'(.*)\'$/', '$1', $columnDefault); |
|
231
|
|
|
} |
|
232
|
|
|
return $defaultValue; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* {@inheritdoc} |
|
237
|
|
|
*/ |
|
238
|
1 |
|
protected function _getPortableTableForeignKeysList($tableForeignKeys) |
|
239
|
|
|
{ |
|
240
|
1 |
|
$list = []; |
|
241
|
1 |
|
foreach ($tableForeignKeys as $value) { |
|
242
|
1 |
|
$value = array_change_key_case($value, CASE_LOWER); |
|
243
|
1 |
|
if (!isset($list[$value['constraint_name']])) { |
|
244
|
1 |
|
if (!isset($value['delete_rule']) || $value['delete_rule'] === "RESTRICT") { |
|
245
|
1 |
|
$value['delete_rule'] = null; |
|
246
|
|
|
} |
|
247
|
1 |
|
if (!isset($value['update_rule']) || $value['update_rule'] === "RESTRICT") { |
|
248
|
1 |
|
$value['update_rule'] = null; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
1 |
|
$list[$value['constraint_name']] = [ |
|
252
|
1 |
|
'name' => $value['constraint_name'], |
|
253
|
|
|
'local' => [], |
|
254
|
|
|
'foreign' => [], |
|
255
|
1 |
|
'foreignTable' => $value['referenced_table_name'], |
|
256
|
1 |
|
'onDelete' => $value['delete_rule'], |
|
257
|
1 |
|
'onUpdate' => $value['update_rule'], |
|
258
|
|
|
]; |
|
259
|
|
|
} |
|
260
|
1 |
|
$list[$value['constraint_name']]['local'][] = $value['column_name']; |
|
261
|
1 |
|
$list[$value['constraint_name']]['foreign'][] = $value['referenced_column_name']; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
1 |
|
$result = []; |
|
265
|
1 |
|
foreach ($list as $constraint) { |
|
266
|
1 |
|
$result[] = new ForeignKeyConstraint( |
|
267
|
1 |
|
array_values($constraint['local']), $constraint['foreignTable'], |
|
268
|
1 |
|
array_values($constraint['foreign']), $constraint['name'], |
|
269
|
|
|
[ |
|
270
|
1 |
|
'onDelete' => $constraint['onDelete'], |
|
271
|
1 |
|
'onUpdate' => $constraint['onUpdate'], |
|
272
|
|
|
] |
|
273
|
|
|
); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
1 |
|
return $result; |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.