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 PDO; |
9
|
|
|
use PDOStatement; |
10
|
|
|
use Phiber\ORM\Config; |
11
|
|
|
use Phiber\ORM\factories\PhiberPersistenceFactory; |
12
|
|
|
use Phiber\ORM\Queries\PhiberQueryWriter; |
13
|
|
|
use Phiber\ORM\Queries\Restrictions; |
14
|
|
|
use Phiber\Util\FuncoesReflections; |
15
|
|
|
use Phiber\Util\JsonReader; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Classe responsável por persistir os objetos no banco |
19
|
|
|
* |
20
|
|
|
* @package bin |
21
|
|
|
*/ |
22
|
|
|
class PhiberPersistence extends PhiberPersistenceFactory |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Variável da configuração do Phiber |
26
|
|
|
* |
27
|
|
|
* @var JsonReader |
28
|
|
|
*/ |
29
|
|
|
private $phiberConfig; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Variável da tabela do objeto trabalhado |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $table = ""; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Campos/colunas do objeto trabalhado |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $fields = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Valores/colunas dos campos |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private $fieldsValues = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Informações para a criação da SQL. |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
private $infos = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Informações mergidas |
61
|
|
|
* |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
private $infosMergeds = []; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Informações mergidas |
68
|
|
|
* |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
private $rowCount = 0; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
private $sql = ""; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Array que vai ter os joins. |
80
|
|
|
* |
81
|
|
|
* @var array |
82
|
|
|
*/ |
83
|
|
|
private $joins = []; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Variável que instancia PDO |
87
|
|
|
* |
88
|
|
|
* @var PDOStatement |
89
|
|
|
*/ |
90
|
|
|
private $PDO = null; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var Restrictions |
94
|
|
|
*/ |
95
|
|
|
public $restrictions; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @var bool |
99
|
|
|
*/ |
100
|
|
|
private $returnSelectWithArray = false; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @deprecated use restriction sem o parênteses. |
104
|
|
|
* @return Restrictions |
105
|
|
|
*/ |
106
|
|
|
public function restrictions() |
107
|
|
|
{ |
108
|
|
|
return $this->restrictions; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param bool $isArray |
113
|
|
|
*/ |
114
|
|
|
public function returnArray(bool $isArray = false) |
115
|
|
|
{ |
116
|
|
|
$this->returnSelectWithArray = $isArray; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Seleciona a tabela manualmente para a escrita da SQL |
121
|
|
|
* |
122
|
|
|
* @param string $table |
123
|
|
|
*/ |
124
|
|
|
public function setTable(string $table) |
125
|
|
|
{ |
126
|
|
|
$this->table = $table; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Seleciona os campos manualmente para a escrita da SQL |
131
|
|
|
* |
132
|
|
|
* @param array $fields |
133
|
|
|
*/ |
134
|
|
|
public function setFields(array $fields) |
135
|
|
|
{ |
136
|
|
|
$this->fields = $fields; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Seleciona os valores dos campos manualmente para o binding após a escrita da SQL |
141
|
|
|
* |
142
|
|
|
* @param array $fieldsValues |
143
|
|
|
*/ |
144
|
|
|
public function setValues(array $fieldsValues) |
145
|
|
|
{ |
146
|
|
|
$this->fieldsValues = $fieldsValues; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Faz o rowCount (contagem de linhas) objeto especificado, se caso a opção execute_queries |
151
|
|
|
* estiver habilitada |
152
|
|
|
* |
153
|
|
|
* @return int|mixed |
154
|
|
|
* @internal param null $infos |
155
|
|
|
* @internal param Object $obj |
156
|
|
|
* @internal param array $condicoes |
157
|
|
|
* @internal param array $conjuncoes |
158
|
|
|
*/ |
159
|
|
|
public function rowCount() |
160
|
|
|
{ |
161
|
|
|
return $this->rowCount; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* PhiberPersistence constructor. |
166
|
|
|
* |
167
|
|
|
* @param $obj |
168
|
|
|
*/ |
169
|
|
|
public function __construct($obj = "") |
170
|
|
|
{ |
171
|
|
|
$this->restrictions = new Restrictions(); |
172
|
|
|
$funcoesReflections = new FuncoesReflections(); |
173
|
|
|
$this->phiberConfig = new Config(); |
|
|
|
|
174
|
|
|
$this->PDO = $this->getConnection(); |
|
|
|
|
175
|
|
|
|
176
|
|
|
if ($obj != "") { |
177
|
|
|
$this->table = strtolower($funcoesReflections->pegaNomeClasseObjeto($obj)); |
178
|
|
|
$this->fields = $funcoesReflections->pegaAtributosDoObjeto($obj); |
179
|
|
|
|
180
|
|
|
$this->fieldsValues = $funcoesReflections->pegaValoresAtributoDoObjeto($obj); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Faz a criação do objeto especificado no banco de dados, caso a opção |
186
|
|
|
* execute_queries na configuração esteja habilitada. |
187
|
|
|
* |
188
|
|
|
* @return bool|mixed |
189
|
|
|
*/ |
190
|
|
|
public function create() |
191
|
|
|
{ |
192
|
|
|
$this->sql = new PhiberQueryWriter("create", [ |
|
|
|
|
193
|
|
|
"table" => $this->table, |
194
|
|
|
"fields" => $this->fields, |
195
|
|
|
"values" => $this->fieldsValues |
196
|
|
|
]); |
197
|
|
|
|
198
|
|
|
if ($this->phiberConfig->verifyExecuteQueries()) { |
|
|
|
|
199
|
|
|
$stmt = $this->PDO->prepare($this->sql); |
200
|
|
|
|
201
|
|
View Code Duplication |
for ($i = 0; $i < count($this->fields); $i++) { |
|
|
|
|
202
|
|
|
if ($this->fieldsValues[$i] != null) { |
203
|
|
|
$stmt->bindValue($this->fields[$i], $this->fieldsValues[$i]); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
if ($stmt->execute()) { |
207
|
|
|
return $this->PDO->lastInsertId(); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return false; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Faz o update no banco do objeto especificado, se caso a opção execute_queries |
216
|
|
|
* estiver habilitada |
217
|
|
|
* |
218
|
|
|
* @return mixed |
219
|
|
|
* @internal param array $conditions |
220
|
|
|
* @internal param array $conjunctions |
221
|
|
|
*/ |
222
|
|
|
public function update() |
223
|
|
|
{ |
224
|
|
|
$conditions = $this->infosMergeds['fields_and_values']; |
225
|
|
|
|
226
|
|
|
$this->sql = new PhiberQueryWriter("update", [ |
|
|
|
|
227
|
|
|
"table" => $this->table, |
228
|
|
|
"fields" => $this->fields, |
229
|
|
|
"values" => $this->fieldsValues, |
230
|
|
|
"where" => isset($this->infosMergeds['where']) ? |
231
|
|
|
$this->infosMergeds['where'] : |
232
|
|
|
null, |
233
|
|
|
"limit" => isset($this->infosMergeds['limit']) ? |
234
|
|
|
$this->infosMergeds['limit'] : |
235
|
|
|
null, |
236
|
|
|
"offset" => isset($this->infosMergeds['offset']) ? |
237
|
|
|
$this->infosMergeds['offset'] : |
238
|
|
|
null |
239
|
|
|
]); |
240
|
|
|
|
241
|
|
|
if ($this->phiberConfig->verifyExecuteQueries()) { |
|
|
|
|
242
|
|
|
|
243
|
|
|
$this->PDO = $this->PDO->prepare($this->sql); |
244
|
|
View Code Duplication |
for ($i = 0; $i < count($this->fields); $i++) { |
|
|
|
|
245
|
|
|
if (!empty($this->fieldsValues[$i])) { |
246
|
|
|
$this->PDO->bindValue($this->fields[$i], $this->fieldsValues[$i]); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
while (current($conditions)) { |
251
|
|
|
$this->PDO->bindValue( |
252
|
|
|
"condition_" . key($conditions), |
253
|
|
|
$conditions[key($conditions)] |
254
|
|
|
); |
255
|
|
|
|
256
|
|
|
next($conditions); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
if ($this->PDO->execute()) { |
260
|
|
|
return true; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return false; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Faz o delete no banco do objeto especificado, se caso a opção execute_queries |
269
|
|
|
* estiver habilitada |
270
|
|
|
* |
271
|
|
|
* @return mixed |
272
|
|
|
* @internal param $ <T> $obj |
273
|
|
|
* @internal param null $infos |
274
|
|
|
*/ |
275
|
|
|
public function delete() |
276
|
|
|
{ |
277
|
|
|
$this->sql = new PhiberQueryWriter("delete", [ |
|
|
|
|
278
|
|
|
"table" => $this->table, |
279
|
|
|
"where" => isset($this->infosMergeds['where']) ? |
280
|
|
|
$this->infosMergeds['where'] : |
281
|
|
|
null, |
282
|
|
|
"limit" => isset($this->infosMergeds['limit']) ? |
283
|
|
|
$this->infosMergeds['limit'] : |
284
|
|
|
null, |
285
|
|
|
"offset" => isset($this->infosMergeds['offset']) ? |
286
|
|
|
$this->infosMergeds['offset'] : |
287
|
|
|
null |
288
|
|
|
]); |
289
|
|
|
|
290
|
|
|
if ($this->phiberConfig->verifyExecuteQueries()) { |
|
|
|
|
291
|
|
|
|
292
|
|
|
$this->PDO = $this->PDO->prepare($this->sql); |
293
|
|
|
|
294
|
|
View Code Duplication |
if (isset($this->infosMergeds['fields_and_values'])) { |
|
|
|
|
295
|
|
|
|
296
|
|
|
while (current($this->infosMergeds['fields_and_values'])) { |
297
|
|
|
$this->PDO->bindValue( |
298
|
|
|
"condition_" . key($this->infosMergeds['fields_and_values']), |
299
|
|
|
$this->infosMergeds['fields_and_values'][key($this->infosMergeds['fields_and_values'])] |
300
|
|
|
); |
301
|
|
|
|
302
|
|
|
next($this->infosMergeds['fields_and_values']); |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
if ($this->PDO->execute()) { |
306
|
|
|
return true; |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return false; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Faz a seleção no banco do objeto especificado, se caso a opção execute_queries |
315
|
|
|
* estiver habilitada |
316
|
|
|
* |
317
|
|
|
* @return array |
318
|
|
|
* @internal param null $infos |
319
|
|
|
*/ |
320
|
|
|
public function select() |
321
|
|
|
{ |
322
|
|
|
$fields = !empty($this->fields) ? $this->fields : ["*"]; |
323
|
|
|
if (empty($this->fields)) { |
324
|
|
|
$fields = isset($this->infosMergeds['fields']) ? |
325
|
|
|
implode(", ", $this->infosMergeds['fields']) : |
326
|
|
|
"*"; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
$this->sql = new PhiberQueryWriter("select", [ |
|
|
|
|
330
|
|
|
"table" => $this->table, |
331
|
|
|
"fields" => $fields, |
332
|
|
|
"where" => isset($this->infosMergeds['where']) ? |
333
|
|
|
$this->infosMergeds['where'] : |
334
|
|
|
null, |
335
|
|
|
"limit" => isset($this->infosMergeds['limit']) ? |
336
|
|
|
$this->infosMergeds['limit'] : |
337
|
|
|
null, |
338
|
|
|
"offset" => isset($this->infosMergeds['offset']) ? |
339
|
|
|
$this->infosMergeds['offset'] : |
340
|
|
|
null, |
341
|
|
|
"orderby" => isset($this->infosMergeds['orderby']) ? |
342
|
|
|
$this->infosMergeds['orderby'] : |
343
|
|
|
null, |
344
|
|
|
"join" => isset($this->joins) ? |
345
|
|
|
$this->joins : |
346
|
|
|
null |
347
|
|
|
]); |
348
|
|
|
|
349
|
|
|
$result = []; |
350
|
|
|
if ($this->phiberConfig->verifyExecuteQueries()) { |
|
|
|
|
351
|
|
|
$this->PDO = $this->PDO->prepare($this->sql); |
352
|
|
View Code Duplication |
if (isset($this->infosMergeds['fields_and_values'])) { |
|
|
|
|
353
|
|
|
|
354
|
|
|
while (current($this->infosMergeds['fields_and_values'])) { |
355
|
|
|
$this->PDO->bindValue( |
356
|
|
|
"condition_" . key($this->infosMergeds['fields_and_values']), |
357
|
|
|
$this->infosMergeds['fields_and_values'][key($this->infosMergeds['fields_and_values'])]); |
358
|
|
|
next($this->infosMergeds['fields_and_values']); |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
$this->PDO->execute(); |
362
|
|
|
|
363
|
|
|
$result = $this->PDO->fetchAll((PDO::FETCH_ASSOC)); |
364
|
|
|
|
365
|
|
|
$this->rowCount = $this->PDO->rowCount(); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
return $result; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Adiciona parâmetros da classe restriction nas informações para buildar o SQL. |
373
|
|
|
* |
374
|
|
|
* @param $infos |
375
|
|
|
*/ |
376
|
|
|
public function add($infos) |
377
|
|
|
{ |
378
|
|
|
array_push($this->infos, $infos); |
379
|
|
|
|
380
|
|
|
if (!isset($this->infos['fields'])) { |
381
|
|
|
$this->infos['fields'] = ["*"]; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
if (isset($infos['join'])) { |
385
|
|
|
array_push($this->joins, $infos['join']); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
$this->mergeSqlInformation(); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Função responsável por mostrar o string da SQL gerada a partir do objeto |
393
|
|
|
* |
394
|
|
|
* @return string |
395
|
|
|
*/ |
396
|
|
|
public function show() |
397
|
|
|
{ |
398
|
|
|
return $this->sql; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Função para escrever SQL manualmente |
403
|
|
|
* |
404
|
|
|
* @param string $sql |
405
|
|
|
*/ |
406
|
|
|
public function writeSQL($sql) |
407
|
|
|
{ |
408
|
|
|
$this->sql = $sql; |
409
|
|
|
$this->PDO = $this->PDO->prepare($this->sql); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* @param $parameter |
414
|
|
|
* @param $value |
415
|
|
|
* @param int $data_type |
416
|
|
|
*/ |
417
|
|
|
public function bindValue($parameter, $value, $data_type = PDO::PARAM_STR) |
418
|
|
|
{ |
419
|
|
|
$this->PDO->bindValue($parameter, $value, $data_type); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Responsável por executar a query. |
424
|
|
|
* |
425
|
|
|
* @return void |
426
|
|
|
*/ |
427
|
|
|
public function execute() |
428
|
|
|
{ |
429
|
|
|
$this->PDO->execute(); |
430
|
|
|
$this->rowCount = $this->PDO->rowCount(); |
|
|
|
|
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Responsável por retornar todos os dados da tabela. |
435
|
|
|
* |
436
|
|
|
* @param int $fetch_style |
437
|
|
|
* @return array |
438
|
|
|
*/ |
439
|
|
|
public function fetchAll($fetch_style = PDO::FETCH_ASSOC) |
440
|
|
|
{ |
441
|
|
|
return $this->PDO->fetchAll($fetch_style); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Responsável por retornar um range de dados desejados. |
446
|
|
|
* |
447
|
|
|
* @param null $fetch_style |
448
|
|
|
* @param int $cursor_orientation |
449
|
|
|
* @param int $cursor_offset |
450
|
|
|
* @return mixed |
451
|
|
|
*/ |
452
|
|
|
public function fetch($fetch_style = PDO::FETCH_ASSOC, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0) |
453
|
|
|
{ |
454
|
|
|
return $this->PDO->fetch($fetch_style, $cursor_orientation, $cursor_offset); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* Função utilizada para mergir informações novas com as antigas da Restrictions |
459
|
|
|
* |
460
|
|
|
* @return void |
461
|
|
|
*/ |
462
|
|
|
private function mergeSqlInformation() |
463
|
|
|
{ |
464
|
|
|
array_push($this->infos, $this->restrictions->getFieldsAndValues()); |
465
|
|
|
|
466
|
|
|
$iterator = 0; |
467
|
|
|
$limit = count($this->infos) - 1; |
468
|
|
|
for ($iterator; $iterator < $limit; $iterator++) { |
469
|
|
|
if (isset(array_keys($this->infos[$iterator])[0])) { |
470
|
|
|
$this->infosMergeds[array_keys($this->infos[$iterator])[0]] = |
471
|
|
|
$this->infos[$iterator][array_keys($this->infos[$iterator])[0]]; |
472
|
|
|
} |
473
|
|
|
} |
474
|
|
|
} |
475
|
|
|
} |
476
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..