1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* The MIT License (MIT) |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2015 Daniel Popiniuc |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
11
|
|
|
* in the Software without restriction, including without limitation the rights |
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
14
|
|
|
* furnished to do so, subject to the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
17
|
|
|
* copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25
|
|
|
* SOFTWARE. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
namespace danielgp\fk_scale_mysql; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Description of FKchange |
33
|
|
|
* |
34
|
|
|
* @author Daniel Popiniuc <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
class FKchange |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
use ConfigurationMySQL, |
40
|
|
|
ConfigurationForAction, |
41
|
|
|
FKinterface, |
42
|
|
|
\danielgp\common_lib\MySQLiAdvancedOutput; |
43
|
|
|
|
44
|
|
|
private $applicationSpecificArray; |
45
|
|
|
|
46
|
|
|
public function __construct() |
47
|
|
|
{ |
48
|
|
|
$rqst = new \Symfony\Component\HttpFoundation\Request; |
49
|
|
|
echo $this->setApplicationHeader() |
50
|
|
|
. $this->buildApplicationInterface($rqst->createFromGlobals()) |
51
|
|
|
. $this->setApplicationFooter(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function buildApplicationInterface($sGb) |
55
|
|
|
{ |
56
|
|
|
$mysqlConfig = $this->configuredMySqlServer(); |
57
|
|
|
$elToModify = $this->targetElementsToModify($sGb); |
58
|
|
|
$transmitedParameters = $this->countTransmitedParameters(['db', 'tbl', 'fld', 'dt']); |
59
|
|
|
$mConnection = $this->connectToMySql($mysqlConfig); |
60
|
|
|
$sReturn = []; |
61
|
|
|
$sReturn[] = $this->buildInputFormTab($mysqlConfig, $transmitedParameters, $sGb); |
62
|
|
|
$sReturn[] = $this->buildResultsTab($mConnection, $elToModify, $transmitedParameters); |
63
|
|
|
return implode('', $sReturn); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function buildResultsTab($mConnection, $elToModify, $tParams) |
67
|
|
|
{ |
68
|
|
|
$sReturn = []; |
69
|
|
|
$targetTableTextFlds = $this->getForeignKeys($elToModify); |
70
|
|
|
$sReturn[] = '<div class="tabbertab' . ($tParams ? ' tabbertabdefault' : '') |
71
|
|
|
. '" id="FKscaleMySQLresults" title="Results">'; |
72
|
|
|
if (is_array($targetTableTextFlds)) { |
73
|
|
|
$sReturn[] = $this->createDropForeignKeysAndGetTargetColumnDefinition($targetTableTextFlds); |
74
|
|
|
$mainColArray = $this->packParameteresForMainChangeColumn($elToModify, $targetTableTextFlds); |
75
|
|
|
$sReturn[] = $this->createChangeColumn($mainColArray, [ |
76
|
|
|
'style' => 'color:blue;font-weight:bold;', |
77
|
|
|
'includeOldColumnType' => true, |
78
|
|
|
]); |
79
|
|
|
$sReturn[] = $this->recreateFKs($elToModify, $targetTableTextFlds); |
80
|
|
|
} else { |
81
|
|
|
$sReturn[] = $this->returnMessagesInCaseOfNoResults($mConnection); |
82
|
|
|
} |
83
|
|
|
$sReturn[] = '</div><!-- end of FKscaleMySQLresults tab -->' |
84
|
|
|
. '</div><!-- tabberFKscaleMySQL -->'; |
85
|
|
|
return implode('', $sReturn); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function createChangeColumn($params, $adtnlFeatures = null) |
89
|
|
|
{ |
90
|
|
|
return '<div style="' . (isset($adtnlFeatures['style']) ? $adtnlFeatures['style'] : 'color:blue;') . '">' |
91
|
|
|
. 'ALTER TABLE `' . $params['Database'] . '`.`' . $params['Table'] . '` ' |
92
|
|
|
. 'CHANGE `' . $params['Column'] . '` `' . $params['Column'] . '` ' . $params['NewDataType'] . ' ' |
93
|
|
|
. $this->setColumnDefinition($params) . ';' |
94
|
|
|
. (isset($adtnlFeatures['includeOldColumnType']) ? ' /* from ' . $params['OldDataType'] . ' */' : '') |
95
|
|
|
. '</div>'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function createChangeColumnPackForChange($key, $value, $elToModify) |
99
|
|
|
{ |
100
|
|
|
$sReturn[] = $this->createChangeColumn([ |
|
|
|
|
101
|
|
|
'Database' => $value['TABLE_SCHEMA'], |
102
|
|
|
'Table' => $value['TABLE_NAME'], |
103
|
|
|
'Column' => $value['COLUMN_NAME'], |
104
|
|
|
'NewDataType' => $elToModify['NewDataType'], |
105
|
|
|
'IS_NULLABLE' => $this->applicationSpecificArray['Cols'][$key][0]['IS_NULLABLE'], |
106
|
|
|
'COLUMN_DEFAULT' => $this->applicationSpecificArray['Cols'][$key][0]['COLUMN_DEFAULT'], |
107
|
|
|
'EXTRA' => $this->applicationSpecificArray['Cols'][$key][0]['EXTRA'], |
108
|
|
|
'COLUMN_COMMENT' => $this->applicationSpecificArray['Cols'][$key][0]['COLUMN_COMMENT'], |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function createDropForeignKey($parameters) |
113
|
|
|
{ |
114
|
|
|
return '<div style="color:red;">' |
115
|
|
|
. 'ALTER TABLE `' . $parameters['Database'] . '`.`' . $parameters['Table'] |
116
|
|
|
. '` DROP FOREIGN KEY `' . $parameters['ForeignKeyName'] . '`;' |
117
|
|
|
. '</div>'; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function createDropForeignKeysAndGetTargetColumnDefinition($targetTableTextFlds) |
121
|
|
|
{ |
122
|
|
|
$sReturn = []; |
123
|
|
|
foreach ($targetTableTextFlds as $key => $value) { |
124
|
|
|
$sReturn[] = $this->createDropForeignKey([ |
125
|
|
|
'Database' => $value['TABLE_SCHEMA'], |
126
|
|
|
'Table' => $value['TABLE_NAME'], |
127
|
|
|
'ForeignKeyName' => $value['CONSTRAINT_NAME'], |
128
|
|
|
]); |
129
|
|
|
$this->applicationSpecificArray['Cols'][$key] = $this->getMySQLlistColumns([ |
130
|
|
|
'TABLE_SCHEMA' => $value['TABLE_SCHEMA'], |
131
|
|
|
'TABLE_NAME' => $value['TABLE_NAME'], |
132
|
|
|
'COLUMN_NAME' => $value['COLUMN_NAME'], |
133
|
|
|
]); |
134
|
|
|
} |
135
|
|
|
return implode('', $sReturn); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
private function createForeignKey($params) |
139
|
|
|
{ |
140
|
|
|
return '<div style="color:green;">' |
141
|
|
|
. 'ALTER TABLE `' . $params['Database'] . '`.`' . $params['Table'] . '` ' |
142
|
|
|
. 'ADD CONSTRAINT `' . $params['ForeignKeyName'] . '` ' |
143
|
|
|
. 'FOREIGN KEY (`' . $params['Column'] . '`) REFERENCES `' . $params['ReferencedDatabase'] . '`.`' |
144
|
|
|
. $params['ReferencedTable'] . '` (`' . $params['ReferencedColumn'] . '`) ' |
145
|
|
|
. 'ON DELETE ' . ($params['RuleDelete'] == 'NULL' ? 'SET NULL' : $params['RuleDelete']) . ' ' |
146
|
|
|
. 'ON UPDATE ' . ($params['RuleUpdate'] == 'NULL' ? 'SET NULL' : $params['RuleUpdate']) . ';' |
147
|
|
|
. '</div>'; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
private function createForeignKeyPacked($inArray) |
151
|
|
|
{ |
152
|
|
|
$fkParams = [ |
153
|
|
|
'Database' => $inArray['TABLE_SCHEMA'], |
154
|
|
|
'Table' => $inArray['TABLE_NAME'], |
155
|
|
|
'Column' => $inArray['COLUMN_NAME'], |
156
|
|
|
'ForeignKeyName' => $inArray['CONSTRAINT_NAME'], |
157
|
|
|
'ReferencedDatabase' => $inArray['REFERENCED_TABLE_SCHEMA'], |
158
|
|
|
'ReferencedTable' => $inArray['REFERENCED_TABLE_NAME'], |
159
|
|
|
'ReferencedColumn' => $inArray['REFERENCED_COLUMN_NAME'], |
160
|
|
|
'RuleDelete' => $inArray['DELETE_RULE'], |
161
|
|
|
'RuleUpdate' => $inArray['UPDATE_RULE'], |
162
|
|
|
]; |
163
|
|
|
return $this->createForeignKey($fkParams); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
private function getForeignKeys($elToModify) |
167
|
|
|
{ |
168
|
|
|
$additionalFeatures = [ |
169
|
|
|
'REFERENCED_TABLE_SCHEMA' => $elToModify['Database'], |
170
|
|
|
'REFERENCED_TABLE_NAME' => $elToModify['Table'], |
171
|
|
|
'REFERENCED_COLUMN_NAME' => $elToModify['Column'], |
172
|
|
|
'REFERENCED_TABLE_NAME' => 'NOT NULL', |
173
|
|
|
]; |
174
|
|
|
$query = $this->sQueryMySqlIndexes($additionalFeatures); |
175
|
|
|
return $this->setMySQLquery2Server($query, 'full_array_key_numbered')['result']; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
private function packParameteresForMainChangeColumn($elToModify, $targetTableTextFlds) |
179
|
|
|
{ |
180
|
|
|
$colToIdentify = [ |
181
|
|
|
'TABLE_SCHEMA' => $elToModify['Database'], |
182
|
|
|
'TABLE_NAME' => $elToModify['Table'], |
183
|
|
|
'COLUMN_NAME' => $elToModify['Column'], |
184
|
|
|
]; |
185
|
|
|
$col = $this->getMySQLlistColumns($colToIdentify); |
186
|
|
|
return [ |
187
|
|
|
'Database' => $targetTableTextFlds[0]['REFERENCED_TABLE_SCHEMA'], |
188
|
|
|
'Table' => $targetTableTextFlds[0]['REFERENCED_TABLE_NAME'], |
189
|
|
|
'Column' => $targetTableTextFlds[0]['REFERENCED_COLUMN_NAME'], |
190
|
|
|
'OldDataType' => strtoupper($col[0]['COLUMN_TYPE']) . ' ' . $this->setColumnDefinition($col[0]), |
191
|
|
|
'NewDataType' => $elToModify['NewDataType'], |
192
|
|
|
'IS_NULLABLE' => $col[0]['IS_NULLABLE'], |
193
|
|
|
'COLUMN_DEFAULT' => $col[0]['COLUMN_DEFAULT'], |
194
|
|
|
'EXTRA' => $col[0]['EXTRA'], |
195
|
|
|
'COLUMN_COMMENT' => $col[0]['COLUMN_COMMENT'], |
196
|
|
|
]; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
private function recreateFKs($elToModify, $targetTableTextFlds) |
200
|
|
|
{ |
201
|
|
|
$sReturn = []; |
202
|
|
|
foreach ($targetTableTextFlds as $key => $value) { |
203
|
|
|
$sReturn[] = $this->createChangeColumnPackForChange($key, $value, $elToModify); |
|
|
|
|
204
|
|
|
$sReturn[] = $this->createForeignKeyPacked($value); |
205
|
|
|
} |
206
|
|
|
return implode('', $sReturn); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
private function setColumnDefinition($inArray) |
210
|
|
|
{ |
211
|
|
|
$colDefinition = $this->setColumnDefinitionPrefix($inArray['IS_NULLABLE'], $inArray['COLUMN_DEFAULT']); |
212
|
|
|
if ($inArray['EXTRA'] == 'auto_increment') { |
213
|
|
|
$colDefinition .= ' AUTO_INCREMENT'; |
214
|
|
|
} |
215
|
|
|
if (strlen($inArray['COLUMN_COMMENT']) > 0) { |
216
|
|
|
$colDefinition .= ' COMMENT "' . $inArray['COLUMN_COMMENT'] . '"'; |
217
|
|
|
} |
218
|
|
|
return $colDefinition; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
private function setColumnDefinitionPrefix($nullableYesNo, $defaultValue) |
222
|
|
|
{ |
223
|
|
|
$colDefinition = 'NOT NULL DEFAULT "' . $defaultValue . '"'; |
224
|
|
|
if (is_null($defaultValue)) { |
225
|
|
|
$colDefinition = 'NOT NULL'; |
226
|
|
|
} |
227
|
|
|
if ($nullableYesNo == 'YES') { |
228
|
|
|
$colDefinition = 'DEFAULT "' . $defaultValue . '"'; |
229
|
|
|
if ($defaultValue === null) { |
230
|
|
|
$colDefinition = 'DEFAULT NULL'; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
return $colDefinition; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.