|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2017. Este código foi feito por @marciioluucas, sob licença MIT |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Phiber\ORM\Persistence; |
|
7
|
|
|
|
|
8
|
|
|
use Phiber\ORM\factories\TableFactory; |
|
9
|
|
|
use Phiber\Util\Annotations; |
|
10
|
|
|
use Phiber\Util\FuncoesReflections; |
|
11
|
|
|
use Phiber\Util\FuncoesString; |
|
12
|
|
|
use Phiber\Util\JsonReader; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Classe responsável por criar as tabelas do banco |
|
16
|
|
|
* |
|
17
|
|
|
* @package bin |
|
18
|
|
|
*/ |
|
19
|
|
|
class TableMySql extends TableFactory |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Sincroniza o banco com o código em tempo de instanciação. |
|
23
|
|
|
* |
|
24
|
|
|
* @param Object $obj |
|
25
|
|
|
* @return mixed|void |
|
26
|
|
|
*/ |
|
27
|
|
|
static function sync($obj) |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
if (self::exists($obj)) { |
|
30
|
|
|
|
|
31
|
|
|
self::drop($obj); |
|
32
|
|
|
self::alter($obj); |
|
33
|
|
|
|
|
34
|
|
|
} else { |
|
35
|
|
|
self::create($obj); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Verifica se a tabela existe, se caso não existir, a função retornará false. |
|
41
|
|
|
* |
|
42
|
|
|
* @param Object $obj |
|
43
|
|
|
* @return bool|string |
|
44
|
|
|
*/ |
|
45
|
|
|
static function exists($obj) |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
$tabela = strtolower(FuncoesReflections::pegaNomeClasseObjeto($obj)); |
|
48
|
|
|
$schema = JsonReader::read(BASE_DIR . '/phiber_config.json')->phiber->link->database_name; |
|
|
|
|
|
|
49
|
|
|
$sql = "SELECT * FROM information_schema.tables WHERE table_schema = ? AND table_name = ?"; |
|
50
|
|
|
|
|
51
|
|
|
if (JsonReader::read(BASE_DIR . "/phiber_config.json")->phiber->execute_querys == 1 ? true : false) { |
|
|
|
|
|
|
52
|
|
|
$pdo = self::getConnection()->prepare($sql); |
|
53
|
|
|
$pdo->bindValue(1, $schema); |
|
54
|
|
|
$pdo->bindValue(2, $tabela); |
|
55
|
|
|
$pdo->execute(); |
|
56
|
|
|
|
|
57
|
|
|
if ($pdo->rowCount() > 0) { |
|
58
|
|
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return false; |
|
62
|
|
|
|
|
63
|
|
|
} else { |
|
64
|
|
|
return $sql; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Deleta a tabela do banco de dados. |
|
70
|
|
|
* |
|
71
|
|
|
* @param Object $obj |
|
72
|
|
|
* @return bool|string |
|
73
|
|
|
*/ |
|
74
|
|
|
static function drop($obj) |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
$tabela = strtolower(FuncoesReflections::pegaNomeClasseObjeto($obj)); |
|
77
|
|
|
$atributosObjeto = FuncoesReflections::pegaAtributosDoObjeto($obj); |
|
78
|
|
|
$columnsTabela = self::columns($tabela); |
|
79
|
|
|
$arrayCamposTabela = []; |
|
80
|
|
View Code Duplication |
for ($i = 0; $i < count($columnsTabela); $i++) { |
|
|
|
|
|
|
81
|
|
|
array_push($arrayCamposTabela, $columnsTabela[$i]['Field']); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$arrayDiff = array_diff($arrayCamposTabela, $atributosObjeto); |
|
85
|
|
|
$arrayDiff = array_values($arrayDiff); |
|
86
|
|
|
$sqlDrop = "ALTER TABLE $tabela \n"; |
|
87
|
|
|
for ($j = 0; $j < count($arrayDiff); $j++) { |
|
|
|
|
|
|
88
|
|
View Code Duplication |
if ($j != count($arrayDiff) - 1) { |
|
|
|
|
|
|
89
|
|
|
$sqlDrop .= "DROP " . $arrayDiff[$j] . ", "; |
|
90
|
|
|
} else { |
|
91
|
|
|
$sqlDrop .= "DROP " . $arrayDiff[$j] . ";"; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$contentFile = JsonReader::read(BASE_DIR."/phiber_config.json")->phiber->code_sync == 1 ? true : false; |
|
|
|
|
|
|
96
|
|
|
if ($contentFile) { |
|
97
|
|
|
$pdo = self::getConnection()->prepare($sqlDrop); |
|
98
|
|
|
if ($pdo->execute()) { |
|
99
|
|
|
return true; |
|
100
|
|
|
}; |
|
101
|
|
|
|
|
102
|
|
|
} else { |
|
103
|
|
|
return $sqlDrop; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Mostra as colunas daquela tabela. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $table |
|
113
|
|
|
* @return array|bool |
|
114
|
|
|
*/ |
|
115
|
|
|
static function columns($table) |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
$sql = "show columns from " . strtolower($table); |
|
118
|
|
|
|
|
119
|
|
|
$contentFile = JsonReader::read(BASE_DIR . "/phiber_config.json")->phiber->execute_querys == 1 ? true : false; |
|
|
|
|
|
|
120
|
|
|
if ($contentFile) { |
|
121
|
|
|
$pdo = self::getConnection()->prepare($sql); |
|
122
|
|
|
if ($pdo->execute()) { |
|
123
|
|
|
return $pdo->fetchAll(\PDO::FETCH_ASSOC); |
|
124
|
|
|
} else { |
|
125
|
|
|
return false; |
|
126
|
|
|
} |
|
127
|
|
|
} else { |
|
128
|
|
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Altera a tabela do banco |
|
134
|
|
|
* |
|
135
|
|
|
* @param Object $obj |
|
136
|
|
|
* @return bool|string |
|
137
|
|
|
*/ |
|
138
|
|
|
static function alter($obj) |
|
|
|
|
|
|
139
|
|
|
{ |
|
140
|
|
|
$tabela = strtolower(FuncoesReflections::pegaNomeClasseObjeto($obj)); |
|
141
|
|
|
$atributosTabela = FuncoesReflections::pegaAtributosDoObjeto($obj); |
|
142
|
|
|
$annotationsTabela = Annotations::getAnnotation($obj); |
|
143
|
|
|
$arrFormatado = []; |
|
144
|
|
|
$arrFinal = []; |
|
145
|
|
|
$stringAlterTable = ""; |
|
146
|
|
|
for ($i = 0; $i < count($atributosTabela); $i++) { |
|
|
|
|
|
|
147
|
|
View Code Duplication |
for ($j = 0; $j < count($annotationsTabela[$atributosTabela[$i]]); $j++) { |
|
|
|
|
|
|
148
|
|
|
$arrAtual = explode("=", $annotationsTabela[$atributosTabela[$i]][$j]); |
|
149
|
|
|
for ($k = 0; $k < count($arrAtual) - 1; $k++) { |
|
150
|
|
|
$arrFormatado[FuncoesString::substituiOcorrenciasDeUmaString($arrAtual[$k], "@_", "")] = $arrAtual[$k + 1]; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$arrFinal[$i] = $arrFormatado; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$stringAlterTable .= "ALTER TABLE $tabela \n"; |
|
158
|
|
|
$primKey = false; |
|
159
|
|
|
$columnPrimaryKey = ""; |
|
160
|
|
|
|
|
161
|
|
|
$columnsTabela = self::columns($tabela); |
|
162
|
|
|
|
|
163
|
|
|
$arrayCamposTabela = []; |
|
164
|
|
View Code Duplication |
for ($i = 0; $i < count($columnsTabela); $i++) { |
|
|
|
|
|
|
165
|
|
|
array_push($arrayCamposTabela, $columnsTabela[$i]['Field']); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$arrayDiff = array_diff($atributosTabela, $arrayCamposTabela); |
|
169
|
|
|
$arrayDiff = array_values($arrayDiff); |
|
170
|
|
|
|
|
171
|
|
|
$stringSql = $stringAlterTable; |
|
172
|
|
|
for ($j = 0; $j < count($arrayDiff); $j++) { |
|
|
|
|
|
|
173
|
|
View Code Duplication |
if ($j != count($arrayDiff) - 1) { |
|
|
|
|
|
|
174
|
|
|
$stringSql .= "ADD " . $arrayDiff[$j]; |
|
175
|
|
|
} else { |
|
176
|
|
|
$stringSql .= "ADD " . $arrayDiff[$j]; |
|
177
|
|
|
} |
|
178
|
|
View Code Duplication |
if (array_key_exists('type', $arrFormatado)) { |
|
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
$stringSql .= " " . $arrFormatado['type'] . ""; |
|
181
|
|
|
|
|
182
|
|
|
} else { |
|
183
|
|
|
$stringSql .= ""; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
View Code Duplication |
if (array_key_exists('size', $arrFormatado)) { |
|
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
$stringSql .= "(" . $arrFormatado['size'] . ") "; |
|
189
|
|
|
|
|
190
|
|
|
} else { |
|
191
|
|
|
$stringSql .= ""; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @todo NOT NULL AQUI |
|
196
|
|
|
*/ |
|
197
|
|
|
|
|
198
|
|
View Code Duplication |
if (array_key_exists('notNull', $arrFormatado)) { |
|
|
|
|
|
|
199
|
|
|
if ($arrFormatado['notNull'] === "true") { |
|
200
|
|
|
$stringSql .= " NOT NULL "; |
|
201
|
|
|
} else { |
|
202
|
|
|
$stringSql .= ""; |
|
203
|
|
|
}; |
|
204
|
|
|
} else { |
|
205
|
|
|
$stringSql .= ""; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
View Code Duplication |
if (array_key_exists('primaryKey', $arrFormatado)) { |
|
|
|
|
|
|
209
|
|
|
if ($arrFormatado['primaryKey'] === "true") { |
|
210
|
|
|
$stringSql .= " PRIMARY KEY "; |
|
211
|
|
|
} else { |
|
212
|
|
|
$stringSql .= ""; |
|
213
|
|
|
}; |
|
214
|
|
|
} else { |
|
215
|
|
|
$stringSql .= ""; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
View Code Duplication |
if (array_key_exists('autoIncrement', $arrFormatado)) { |
|
|
|
|
|
|
219
|
|
|
if ($arrFormatado['autoIncrement'] === "true") { |
|
220
|
|
|
$stringSql .= " AUTO_INCREMENT "; |
|
221
|
|
|
} else { |
|
222
|
|
|
$stringSql .= ""; |
|
223
|
|
|
}; |
|
224
|
|
|
} else { |
|
225
|
|
|
$stringSql .= ""; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
$fileContent = JsonReader::read(BASE_DIR."/phiber_config.json")->phiber->code_sync == 1 ? true : false; |
|
|
|
|
|
|
229
|
|
|
if ($fileContent) { |
|
230
|
|
|
$pdo = self::getConnection()->prepare($stringSql); |
|
231
|
|
|
$pdo->execute(); |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$columnsTabela = self::columns($tabela); |
|
236
|
|
|
for ($i = 0; $i < count($arrFinal); $i++) { |
|
|
|
|
|
|
237
|
|
|
if ($columnsTabela[$i]['Field'] != $atributosTabela[$i]) { |
|
238
|
|
|
$stringAlterTable .= "CHANGE `" . $columnsTabela[$i]['Field'] . "` `$atributosTabela[$i]` "; |
|
239
|
|
|
$stringAlterTable .= strtoupper($arrFinal[$i]['type']) . "(" . $arrFinal[$i]['size'] . ")\n"; |
|
240
|
|
|
|
|
241
|
|
|
if ($arrFinal[$i]) |
|
242
|
|
|
|
|
243
|
|
|
if ($i != count($arrFinal) - 1) { |
|
244
|
|
|
$stringAlterTable .= ", \n"; |
|
245
|
|
|
} |
|
246
|
|
|
} else { |
|
247
|
|
|
$stringAlterTable .= "CHANGE `$atributosTabela[$i]` `$atributosTabela[$i]` "; |
|
248
|
|
|
|
|
249
|
|
|
$stringAlterTable .= strtoupper($arrFinal[$i]['type']) . "(" . $arrFinal[$i]['size'] . ")"; |
|
250
|
|
|
|
|
251
|
|
|
$respIfNotNull = $columnsTabela[$i]['Null'] == 'NO' ? 'false' : 'true'; |
|
252
|
|
|
|
|
253
|
|
|
if ($arrFinal[$i]['notNull'] == $respIfNotNull) { |
|
254
|
|
|
$stringAlterTable .= " NOT NULL "; |
|
255
|
|
|
} else { |
|
256
|
|
|
$stringAlterTable .= " NULL "; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
if (array_key_exists('default', $arrFinal[$i]) && $arrFinal[$i]['default'] != "none") { |
|
260
|
|
|
$stringAlterTable .= "DEFAULT '" . $arrFinal[$i]['default'] . "'"; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
if (array_key_exists('autoIncrement', $arrFinal[$i]) && $arrFinal[$i]['autoIncrement'] != "false") { |
|
264
|
|
|
$stringAlterTable .= " AUTO_INCREMENT "; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
$stringAlterTable .= ", \n"; |
|
268
|
|
|
|
|
269
|
|
|
if ($arrFinal[$i]['primaryKey'] == "true" and $primKey != true) { |
|
|
|
|
|
|
270
|
|
|
$primKey = true; |
|
271
|
|
|
$columnPrimaryKey = $atributosTabela[$i]; |
|
272
|
|
|
} |
|
273
|
|
|
if ($i == count($arrFinal) - 1) { |
|
274
|
|
|
if ($primKey) { |
|
275
|
|
|
$stringAlterTable .= " DROP PRIMARY KEY, ADD PRIMARY KEY(`$columnPrimaryKey`);"; |
|
276
|
|
|
} else { |
|
277
|
|
|
$stringAlterTable .= " DROP PRIMARY KEY"; |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
$jsonFile = JsonReader::read(BASE_DIR."/phiber_config.json")->phiber->code_sync == 1 ? true : false; |
|
|
|
|
|
|
284
|
|
|
if ($jsonFile) { |
|
285
|
|
|
$pdo = self::getConnection()->prepare($stringAlterTable); |
|
286
|
|
|
if ($pdo->execute()) { |
|
287
|
|
|
return true; |
|
288
|
|
|
}; |
|
289
|
|
|
} else { |
|
290
|
|
|
return $stringAlterTable; |
|
291
|
|
|
} |
|
292
|
|
|
return false; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Cria a tabela |
|
297
|
|
|
* |
|
298
|
|
|
* @param Object $obj |
|
299
|
|
|
* @return bool|string |
|
300
|
|
|
*/ |
|
301
|
|
|
public static function create($obj) |
|
302
|
|
|
{ |
|
303
|
|
|
$nomeTabela = FuncoesReflections::pegaNomeClasseObjeto($obj); |
|
304
|
|
|
$atributosTabela = FuncoesReflections::pegaAtributosDoObjeto($obj); |
|
305
|
|
|
$annotationsTabela = Annotations::getAnnotation($obj); |
|
306
|
|
|
$arrFormatado = []; |
|
307
|
|
|
$stringSql = "C" . "REATE TABLE IF NOT EXISTS `" . strtolower($nomeTabela) . "` ("; |
|
308
|
|
|
|
|
309
|
|
|
for ($i = 0; $i < count($atributosTabela); $i++) { |
|
|
|
|
|
|
310
|
|
|
|
|
311
|
|
|
$stringSql .= $atributosTabela[$i] . " "; |
|
312
|
|
View Code Duplication |
for ($j = 0; $j < count($annotationsTabela[$atributosTabela[$i]]); $j++) { |
|
|
|
|
|
|
313
|
|
|
|
|
314
|
|
|
$arrAtual = explode("=", $annotationsTabela[$atributosTabela[$i]][$j]); |
|
315
|
|
|
for ($k = 0; $k < count($arrAtual) - 1; $k++) { |
|
316
|
|
|
$arrFormatado[FuncoesString::substituiOcorrenciasDeUmaString($arrAtual[$k], "@_", "")] = $arrAtual[$k + 1]; |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
View Code Duplication |
if (array_key_exists('type', $arrFormatado)) { |
|
|
|
|
|
|
320
|
|
|
$stringSql .= " " . $arrFormatado['type'] . ""; |
|
321
|
|
|
|
|
322
|
|
|
} else { |
|
323
|
|
|
$stringSql .= ""; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
View Code Duplication |
if (array_key_exists('size', $arrFormatado)) { |
|
|
|
|
|
|
327
|
|
|
if ($arrFormatado['size'] != 'none') { |
|
328
|
|
|
$stringSql .= "(" . $arrFormatado['size'] . ") "; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
} else { |
|
332
|
|
|
$stringSql .= ""; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* @todo NOT NULL AQUI |
|
337
|
|
|
*/ |
|
338
|
|
|
|
|
339
|
|
View Code Duplication |
if (array_key_exists('notNull', $arrFormatado)) { |
|
|
|
|
|
|
340
|
|
|
if ($arrFormatado['notNull'] === "true") { |
|
341
|
|
|
$stringSql .= " NOT NULL "; |
|
342
|
|
|
} else { |
|
343
|
|
|
$stringSql .= ""; |
|
344
|
|
|
}; |
|
345
|
|
|
} else { |
|
346
|
|
|
$stringSql .= ""; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
View Code Duplication |
if (array_key_exists('primaryKey', $arrFormatado)) { |
|
|
|
|
|
|
350
|
|
|
if ($arrFormatado['primaryKey'] === "true") { |
|
351
|
|
|
$stringSql .= " PRIMARY KEY "; |
|
352
|
|
|
} else { |
|
353
|
|
|
$stringSql .= ""; |
|
354
|
|
|
}; |
|
355
|
|
|
} else { |
|
356
|
|
|
$stringSql .= ""; |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
View Code Duplication |
if (array_key_exists('autoIncrement', $arrFormatado)) { |
|
|
|
|
|
|
360
|
|
|
if ($arrFormatado['autoIncrement'] === "true") { |
|
361
|
|
|
$stringSql .= " AUTO_INCREMENT "; |
|
362
|
|
|
} else { |
|
363
|
|
|
$stringSql .= ""; |
|
364
|
|
|
}; |
|
365
|
|
|
} else { |
|
366
|
|
|
$stringSql .= ""; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
if ($i != count($atributosTabela) - 1) { |
|
370
|
|
|
|
|
371
|
|
|
$stringSql .= " , "; |
|
372
|
|
|
} |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
$stringSql .= ") ENGINE = InnoDB;"; |
|
376
|
|
|
|
|
377
|
|
|
echo $stringSql; |
|
378
|
|
|
if (JsonReader::read(BASE_DIR . "/phiber_config.json")->phiber->code_sync == 1 ? true : false) { |
|
|
|
|
|
|
379
|
|
|
$pdo = self::getConnection()->prepare($stringSql); |
|
380
|
|
|
if ($pdo->execute()) { |
|
381
|
|
|
return true; |
|
382
|
|
|
}; |
|
383
|
|
|
} else { |
|
384
|
|
|
return $stringSql; |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
return false; |
|
388
|
|
|
} |
|
389
|
|
|
} |
|
390
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.