|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MocOrm\Model; |
|
4
|
|
|
|
|
5
|
|
|
use MocOrm\Connection\ConnectionManager; |
|
6
|
|
|
use MocOrm\Support\Log; |
|
7
|
|
|
|
|
8
|
|
|
abstract class Model extends Query implements \JsonSerializable |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var Model $_instance Save current instance |
|
12
|
|
|
*/ |
|
13
|
|
|
private static $_instance; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var Connection $Connection Save connection instance |
|
|
|
|
|
|
17
|
|
|
*/ |
|
18
|
|
|
private $Connection; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array $_data Save data on object |
|
22
|
|
|
*/ |
|
23
|
|
|
private $_data = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array $_newData Set attributes for update |
|
27
|
|
|
*/ |
|
28
|
|
|
private $_newData = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var closure $triggerAfter Actived trigger after |
|
|
|
|
|
|
32
|
|
|
*/ |
|
33
|
|
|
private $triggerAfter = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var closure $triggerBefore Actived trigger after |
|
37
|
|
|
*/ |
|
38
|
|
|
private $triggerBefore = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var array $_current_custom_query_values Save the value to custom query |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $_current_custom_query_values = []; |
|
44
|
|
|
|
|
45
|
|
|
private $Result; |
|
46
|
|
|
|
|
47
|
|
|
protected static $table_name; |
|
48
|
|
|
protected static $primary_key; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Model constructor. |
|
52
|
|
|
* set connection in var and set this instance in var for interator |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct($object = null) |
|
55
|
|
|
{ |
|
56
|
|
|
$methods = get_class_methods(get_called_class()); |
|
57
|
|
|
|
|
58
|
|
|
try { |
|
59
|
|
|
$this->Connection = ConnectionManager::initialize()->current(); |
|
|
|
|
|
|
60
|
|
|
self::$_instance = $this; |
|
61
|
|
|
|
|
62
|
|
|
$this->cleanNewData(); |
|
63
|
|
|
|
|
64
|
|
|
if (in_array('onLoad', $methods)) { |
|
65
|
|
|
$this->onLoad(); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (!is_null($object)) { |
|
69
|
|
|
if (!is_array($object)) throw new \InvalidArgumentException('Accept only array from object'); |
|
70
|
|
|
|
|
71
|
|
|
$this->_data = $object; |
|
72
|
|
|
$this->_newData = $object; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->Result = new Result(); |
|
76
|
|
|
|
|
77
|
|
|
if($this->Result && !$this->Result->getResults()) { |
|
78
|
|
|
$clone = clone $this; |
|
79
|
|
|
$this->Result->setResults($clone); |
|
80
|
|
|
} |
|
81
|
|
|
} catch (\Exception $e) { |
|
82
|
|
|
throw $e; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Using if call isset on attributes |
|
88
|
|
|
* @param $name |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public function __isset($name) |
|
92
|
|
|
{ |
|
93
|
|
|
return key_exists($name, $this->_data); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get value on array data |
|
98
|
|
|
* @param String $name |
|
99
|
|
|
* @return Value result |
|
|
|
|
|
|
100
|
|
|
*/ |
|
101
|
|
|
public function __get($name) |
|
102
|
|
|
{ |
|
103
|
|
|
if (strtolower($name) == 'errors') return Error::instance(); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
if (!key_exists($name, $this->_data)) throw new \Exception("The attribute $name not found."); |
|
106
|
|
|
|
|
107
|
|
|
return $this->_data[$name]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* This set values in attribute data and newData |
|
112
|
|
|
* @param $name |
|
113
|
|
|
* @param $value |
|
114
|
|
|
*/ |
|
115
|
|
|
public function __set($name, $value) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->_newData[$name] = $value; |
|
118
|
|
|
$this->_data[$name] = $value; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return array Save for info in debug only attributes in _data |
|
123
|
|
|
*/ |
|
124
|
|
|
public function __debugInfo() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->_data; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param $name |
|
131
|
|
|
*/ |
|
132
|
|
|
function __unset($name) |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
unset($this->_data[$name]); |
|
135
|
|
|
unset($this->_newData[$name]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
function __clone() |
|
|
|
|
|
|
139
|
|
|
{ |
|
140
|
|
|
unset($this->_data[static::$primary_key]); |
|
141
|
|
|
$this->_newData = $this->_data; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Insert the new data in database not static mode. |
|
146
|
|
|
* @param array $attributes Parameters, this is a mirror on database. |
|
147
|
|
|
* @return bool|Model False if not save in databse, and this instance if save. |
|
148
|
|
|
* @throws \Exception If the parameters isn't one array. |
|
149
|
|
|
*/ |
|
150
|
|
|
public function save() |
|
151
|
|
|
{ |
|
152
|
|
|
try { |
|
153
|
|
|
$this->verifyConnection(); |
|
154
|
|
|
} catch (\Exception $e) { |
|
155
|
|
|
throw $e; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$update = false; |
|
159
|
|
|
|
|
160
|
|
|
$key = static::$primary_key; |
|
161
|
|
|
$_tableName = static::$table_name; |
|
162
|
|
|
|
|
163
|
|
|
$repeat = substr(str_repeat(' ?, ', count($this->_newData)), 0, -2); |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Edit case have primary key value |
|
167
|
|
|
*/ |
|
168
|
|
|
if (!empty($this->_data[$key])) { |
|
169
|
|
|
$logger = [ |
|
170
|
|
|
'function' => 'Update', |
|
171
|
|
|
'next' => $this->_data, |
|
172
|
|
|
'prev' => $this->Result->getResults() |
|
173
|
|
|
]; |
|
174
|
|
|
|
|
175
|
|
|
$this->saveLogger($logger); |
|
176
|
|
|
$this->Result->setResults(clone $this); |
|
177
|
|
|
|
|
178
|
|
|
$sql = 'UPDATE ' . $_tableName . ' SET '; |
|
179
|
|
|
|
|
180
|
|
|
if (count($this->_newData) <= 0) { |
|
181
|
|
|
Error::create('Don\'t have alter data.', 1, 'InvalidArgumentException'); |
|
182
|
|
|
return false; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$sql .= implode(" = ?, ", array_keys($this->_newData)) . ' = ?'; |
|
186
|
|
|
$sql .= " WHERE $key = ? "; |
|
187
|
|
|
|
|
188
|
|
|
$this->_newData[] = $this->{$key}; |
|
189
|
|
|
|
|
190
|
|
|
$update = true; |
|
191
|
|
|
} else { |
|
192
|
|
|
$logger = [ |
|
193
|
|
|
'function' => 'Save', |
|
194
|
|
|
'next' => $this->_data, |
|
195
|
|
|
]; |
|
196
|
|
|
|
|
197
|
|
|
$this->saveLogger($logger); |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Insert case don't have primary key |
|
201
|
|
|
*/ |
|
202
|
|
|
$sql = 'INSERT INTO ' . $_tableName; |
|
203
|
|
|
$sql .= ' (' . implode(', ', array_keys($this->_data)) . ') '; |
|
204
|
|
|
$sql .= " VALUES "; |
|
205
|
|
|
$sql .= " ($repeat); "; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
if (is_callable($this->triggerBefore)) ($this->triggerBefore)(); |
|
210
|
|
|
|
|
211
|
|
|
$start = microtime(true); |
|
212
|
|
|
$insert = $this->Connection->getConnection()->prepare($sql); |
|
213
|
|
|
$this->burnError($insert); |
|
214
|
|
|
|
|
215
|
|
|
$this->_newData = array_map(function ($data) { |
|
216
|
|
|
if (is_bool($data) and $data === false) $data = 0; |
|
217
|
|
|
|
|
218
|
|
|
return $data; |
|
219
|
|
|
}, $this->_newData); |
|
220
|
|
|
|
|
221
|
|
|
$insert->execute(array_values($this->_newData)); |
|
222
|
|
|
$end = microtime(true); |
|
223
|
|
|
$this->burnError($insert); |
|
224
|
|
|
|
|
225
|
|
|
$this->Connection->setPerformedQuery($insert->queryString, round(($end - $start), 5)); |
|
226
|
|
|
|
|
227
|
|
|
if (is_callable($this->triggerAfter)) ($this->triggerAfter)(); |
|
228
|
|
|
|
|
229
|
|
|
if ($update) { |
|
230
|
|
|
$this->burnError($insert); |
|
231
|
|
|
|
|
232
|
|
|
$this->cleanNewData(); |
|
233
|
|
|
return true; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
$this->_data[$key] = $this->Connection->getConnection()->lastInsertId(); |
|
237
|
|
|
|
|
238
|
|
|
if ($insert->rowCount() == 0) { |
|
239
|
|
|
throw new \Exception($insert->errorInfo()[2], $insert->errorInfo()[1]); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
return true; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
private function saveLogger($params) { |
|
246
|
|
|
$class = get_called_class(); |
|
247
|
|
|
|
|
248
|
|
|
if ( |
|
249
|
|
|
$this->Connection->getAppLogger() and |
|
250
|
|
|
$class != 'MocOrm\Support\Log' |
|
251
|
|
|
) { |
|
252
|
|
|
|
|
253
|
|
|
$log = (new Log) |
|
254
|
|
|
->setNext(@$params['next']) |
|
255
|
|
|
->setName($class) |
|
256
|
|
|
->setFunction($params['function']) |
|
257
|
|
|
->setPrev(@$params['prev']); |
|
258
|
|
|
|
|
259
|
|
|
$log->save(); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
|
|
264
|
|
|
private function burnError($statment) |
|
265
|
|
|
{ |
|
266
|
|
|
if (!is_null($statment->errorInfo()[1])) throw new \Exception($statment->errorInfo()[2], $statment->errorInfo()[1]); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Delete the register |
|
271
|
|
|
* @return bool |
|
272
|
|
|
*/ |
|
273
|
|
|
public function delete() |
|
274
|
|
|
{ |
|
275
|
|
|
try { |
|
276
|
|
|
$this->verifyConnection(); |
|
277
|
|
|
} catch (\Exception $e) { |
|
278
|
|
|
throw new \Exception($e->getMessage()); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
if (!isset(static::$primary_key)) throw new \Exception('Primary key don\'t set'); |
|
282
|
|
|
|
|
283
|
|
|
if (!is_numeric($this->{static::$primary_key})) throw new \Exception('Primary key value don\'t is valid'); |
|
284
|
|
|
|
|
285
|
|
|
$sql = ' DELETE FROM ' . static::$table_name; |
|
286
|
|
|
$sql .= ' WHERE ' . static::$primary_key . ' = ? '; |
|
287
|
|
|
|
|
288
|
|
|
$instance = self::$_instance; |
|
289
|
|
|
|
|
290
|
|
|
if (is_callable($this->triggerBefore)) ($this->triggerBefore)();; |
|
291
|
|
|
|
|
292
|
|
|
$start = microtime(true); |
|
293
|
|
|
|
|
294
|
|
|
$insert = $instance->Connection->getConnection()->prepare($sql); |
|
295
|
|
|
$insert->execute([$this->{static::$primary_key}]); |
|
296
|
|
|
|
|
297
|
|
|
$end = microtime(true); |
|
298
|
|
|
|
|
299
|
|
|
$instance->Connection->setPerformedQuery($insert->queryString, round(($end - $start), 5)); |
|
300
|
|
|
|
|
301
|
|
|
if (is_callable($this->triggerAfter)) ($this->triggerAfter)(); |
|
302
|
|
|
|
|
303
|
|
|
if ($insert->rowCount() > 0) { |
|
304
|
|
|
$logger = [ |
|
305
|
|
|
'function' => 'Delete', |
|
306
|
|
|
'prev' => $this->_data, |
|
307
|
|
|
]; |
|
308
|
|
|
|
|
309
|
|
|
$this->saveLogger($logger); |
|
310
|
|
|
|
|
311
|
|
|
return true; |
|
312
|
|
|
} |
|
313
|
|
|
else{ return false; }; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* JsonSerializable Interface. |
|
318
|
|
|
*/ |
|
319
|
|
|
public function jsonSerialize() |
|
320
|
|
|
{ |
|
321
|
|
|
return $this->_data; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* Get all data on database needed table name in Model |
|
326
|
|
|
* @return array all data in format Object |
|
327
|
|
|
* @throws \Exception Don't set table name in model. |
|
328
|
|
|
*/ |
|
329
|
|
|
public static function all() |
|
330
|
|
|
{ |
|
331
|
|
|
self::instance(); |
|
332
|
|
|
|
|
333
|
|
|
if (!isset(static::$table_name)) throw new \Exception('Don\'t set table name in model.'); |
|
334
|
|
|
|
|
335
|
|
|
$currentTable = static::$table_name; |
|
336
|
|
|
|
|
337
|
|
|
$instance = self::$_instance; |
|
338
|
|
|
|
|
339
|
|
|
try { |
|
340
|
|
|
self::$_instance->verifyConnection(); |
|
341
|
|
|
} catch (\Exception $e) { |
|
342
|
|
|
throw new \Exception($e->getMessage()); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
$sql = "SELECT * FROM $currentTable "; |
|
346
|
|
|
|
|
347
|
|
|
return $instance->query($sql); |
|
|
|
|
|
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
/** |
|
351
|
|
|
* Execute one procedure |
|
352
|
|
|
* @param $procedureName Name of the procedure |
|
|
|
|
|
|
353
|
|
|
* @param array $param Parameters needed in procedure |
|
354
|
|
|
* @return mixed Result on procedure |
|
355
|
|
|
* @throws \Exception Case procedureName don't is string or param not is array |
|
356
|
|
|
*/ |
|
357
|
|
|
public static function procedure($procedureName, $param = []) |
|
358
|
|
|
{ |
|
359
|
|
|
self::instance(); |
|
360
|
|
|
|
|
361
|
|
|
if (!is_string($procedureName)) throw new \Exception("Procedure name is invalid."); |
|
362
|
|
|
if (!is_array($param)) throw new \Exception("Tipo de parâmetros inválidos."); |
|
363
|
|
|
|
|
364
|
|
|
$currentTable = static::$table_name; |
|
365
|
|
|
|
|
366
|
|
|
$instance = self::$_instance; |
|
367
|
|
|
|
|
368
|
|
|
try { |
|
369
|
|
|
self::$_instance->verifyConnection(); |
|
370
|
|
|
} catch (\Exception $e) { |
|
371
|
|
|
Throw new \Exception($e->getMessage()); |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
$repeat = substr(str_repeat(' ?, ', count($param)), 0, -2); |
|
375
|
|
|
|
|
376
|
|
|
$drivers = $instance->Connection->getDriver(); |
|
377
|
|
|
|
|
378
|
|
|
switch ($drivers) { |
|
379
|
|
|
case 'mysql': |
|
380
|
|
|
$sql = "call $currentTable ($repeat)"; |
|
381
|
|
|
break; |
|
382
|
|
|
case 'pgsql': |
|
383
|
|
|
$sql = "select $procedureName($repeat)"; |
|
384
|
|
|
break; |
|
385
|
|
|
default: |
|
386
|
|
|
throw new \Exception('Don\'t exists implementation on this driver.'); |
|
387
|
|
|
break; |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
$start = microtime(true); |
|
391
|
|
|
$consulta = $instance->Connection->getConnection()->prepare($sql); |
|
392
|
|
|
$consulta->execute($param); |
|
393
|
|
|
$objetos = $consulta->fetchAll(\PDO::FETCH_CLASS, get_class($instance)); |
|
394
|
|
|
$end = microtime(true); |
|
395
|
|
|
|
|
396
|
|
|
$instance->Connection->setPerformedQuery($sql, round(($end - $start), 5)); |
|
397
|
|
|
|
|
398
|
|
|
return $instance->_data = $objetos; |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
/** |
|
402
|
|
|
* Insert the new data in database static mode. |
|
403
|
|
|
* @param array $attributes Parameters, this is a mirror on database. |
|
404
|
|
|
* @return bool|Model|Save False if not save in databse, and this instance if save. |
|
|
|
|
|
|
405
|
|
|
* @throws \Exception If the parameters isn't one array. |
|
406
|
|
|
*/ |
|
407
|
|
|
public static function create($attributes = []) |
|
408
|
|
|
{ |
|
409
|
|
|
self::instance(); |
|
410
|
|
|
|
|
411
|
|
|
$instance = self::$_instance; |
|
412
|
|
|
|
|
413
|
|
|
$instance->_data = $attributes; |
|
414
|
|
|
$instance->_newData = $attributes; |
|
415
|
|
|
|
|
416
|
|
|
return $instance->save(); |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
/** |
|
420
|
|
|
* Find the data on primary key |
|
421
|
|
|
* @param Array or Integer $parameters Value on primary key |
|
|
|
|
|
|
422
|
|
|
* @return mixed Data or false |
|
423
|
|
|
* @throws \Exception if Parameter is invalid |
|
424
|
|
|
*/ |
|
425
|
|
|
public static function find($parameters = null) |
|
426
|
|
|
{ |
|
427
|
|
|
self::instance(); |
|
428
|
|
|
|
|
429
|
|
|
$instance = self::$_instance; |
|
430
|
|
|
|
|
431
|
|
|
try { |
|
432
|
|
|
$instance->verifyConnection(); |
|
433
|
|
|
} catch (\Exception $e) { |
|
434
|
|
|
throw new \Exception($e->getMessage()); |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
if (!is_array($parameters) and !is_numeric($parameters)) throw new \Exception('Invalid parameter type on model ' . get_called_class() . '.'); |
|
438
|
|
|
|
|
439
|
|
|
$instance->_current_custom_query[] = 'SELECT * FROM ' . static::$table_name . ' '; |
|
440
|
|
|
|
|
441
|
|
|
switch ($parameters) { |
|
442
|
|
|
case is_numeric($parameters): |
|
443
|
|
|
if (!isset(static::$primary_key)) throw new \Exception("Invalid parameter type."); |
|
444
|
|
|
|
|
445
|
|
|
$instance->_current_custom_query_values[] = $parameters; |
|
446
|
|
|
$instance->_current_custom_query[] = ' WHERE ' . static::$primary_key . ' = ?'; |
|
447
|
|
|
|
|
448
|
|
|
break; |
|
449
|
|
|
case is_array($parameters): |
|
450
|
|
|
break; |
|
451
|
|
|
default: |
|
452
|
|
|
throw new \Exception('Invalid parameter type.'); |
|
453
|
|
|
break; |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
$done = $instance->done(); |
|
457
|
|
|
|
|
458
|
|
|
if(count($done) > 0) { |
|
459
|
|
|
$done = $done[0]; |
|
460
|
|
|
|
|
461
|
|
|
$clone = clone $done; |
|
462
|
|
|
|
|
463
|
|
|
$done->Result->setResults($clone); |
|
464
|
|
|
|
|
465
|
|
|
return $done; |
|
466
|
|
|
}else { |
|
467
|
|
|
return null; |
|
468
|
|
|
} |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
/** |
|
472
|
|
|
* Init the get data dynamic, the last method to use this is done() for execute |
|
473
|
|
|
* @param String $colunm This is all colunm from select |
|
474
|
|
|
* @return Model|Save A model if success and false if don't have success |
|
475
|
|
|
* @throws \Exception If colunm isn't String |
|
476
|
|
|
*/ |
|
477
|
|
|
final public static function select($colunm = '*') |
|
478
|
|
|
{ |
|
479
|
|
|
self::instance(); |
|
480
|
|
|
|
|
481
|
|
|
try { |
|
482
|
|
|
self::$_instance->verifyConnection(); |
|
483
|
|
|
} catch (\Exception $e) { |
|
484
|
|
|
throw new \Exception($e->getMessage()); |
|
485
|
|
|
} |
|
486
|
|
|
|
|
487
|
|
|
if (!is_string($colunm)) throw new \Exception("Invalid parameter type."); |
|
488
|
|
|
|
|
489
|
|
|
self::$_instance->_current_custom_query[] = "SELECT $colunm FROM " . static::$table_name . ' '; |
|
490
|
|
|
|
|
491
|
|
|
if (!isset(static::$primary_key)) throw new \Exception("Invalid parameter type."); |
|
492
|
|
|
|
|
493
|
|
|
return self::$_instance; |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
|
|
/** |
|
497
|
|
|
* Get current connection name |
|
498
|
|
|
* @return string |
|
499
|
|
|
*/ |
|
500
|
|
|
protected function getCurrentConnectionName() |
|
501
|
|
|
{ |
|
502
|
|
|
return $this->Connection->getCurrentConnectionString(); |
|
503
|
|
|
} |
|
504
|
|
|
|
|
505
|
|
|
/** |
|
506
|
|
|
* Define the current connection on name |
|
507
|
|
|
* @param $connectionName This is connection name |
|
|
|
|
|
|
508
|
|
|
* @return $this This from other implementations |
|
509
|
|
|
*/ |
|
510
|
|
|
protected function setConnection($connectionName) |
|
511
|
|
|
{ |
|
512
|
|
|
try { |
|
513
|
|
|
$this->Connection->setConnection($connectionName); |
|
514
|
|
|
|
|
515
|
|
|
return $this; |
|
516
|
|
|
} catch (\Exception $e) { |
|
517
|
|
|
throw new $e; |
|
518
|
|
|
} |
|
519
|
|
|
} |
|
520
|
|
|
|
|
521
|
|
|
/** |
|
522
|
|
|
* Execute the query |
|
523
|
|
|
* @param $query The query for execute |
|
|
|
|
|
|
524
|
|
|
* @return $objects Results on query executed |
|
525
|
|
|
*/ |
|
|
|
|
|
|
526
|
|
|
protected function query($query, $param = []) |
|
527
|
|
|
{ |
|
528
|
|
|
$this->_data = []; |
|
529
|
|
|
|
|
530
|
|
|
$select = trim($query); |
|
531
|
|
|
$select = strtolower($select); |
|
532
|
|
|
|
|
533
|
|
|
$match = preg_match('/^select|return|^with\srecursive/', $select); |
|
534
|
|
|
|
|
535
|
|
|
try { |
|
536
|
|
|
$this->verifyConnection(); |
|
537
|
|
|
} catch (\Exception $e) { |
|
538
|
|
|
throw new \Exception($e->getMessage()); |
|
539
|
|
|
} |
|
540
|
|
|
|
|
541
|
|
|
if (!is_array($param)) throw new \Exception('Tipo de parâmetro inválido.'); |
|
542
|
|
|
|
|
543
|
|
|
$start = microtime(true); |
|
544
|
|
|
|
|
545
|
|
|
$consulta = $this->Connection->getConnection()->prepare($query); |
|
546
|
|
|
$this->burnError($consulta); |
|
547
|
|
|
|
|
548
|
|
|
$consulta->execute($param); |
|
549
|
|
|
$this->burnError($consulta); |
|
550
|
|
|
|
|
551
|
|
|
if (!$consulta) { |
|
552
|
|
|
$this->_data = false; |
|
|
|
|
|
|
553
|
|
|
return $this; |
|
554
|
|
|
} |
|
555
|
|
|
|
|
556
|
|
|
$end = microtime(true); |
|
557
|
|
|
|
|
558
|
|
|
if ($match) { |
|
559
|
|
|
$this->_data = $objetos = $consulta->fetchAll(\PDO::FETCH_CLASS, get_called_class()); |
|
560
|
|
|
} else { |
|
561
|
|
|
$objetos = $consulta->rowCount(); |
|
562
|
|
|
} |
|
563
|
|
|
|
|
564
|
|
|
$this->Connection->setPerformedQuery($query, round(($end - $start), 5)); |
|
565
|
|
|
|
|
566
|
|
|
$this->burnError($consulta); |
|
567
|
|
|
|
|
568
|
|
|
return $objetos; |
|
569
|
|
|
} |
|
570
|
|
|
|
|
571
|
|
|
/** |
|
572
|
|
|
* Execute the query on static method |
|
573
|
|
|
* @param $query The query for execute |
|
574
|
|
|
* @return $objects Results on query executed |
|
575
|
|
|
*/ |
|
|
|
|
|
|
576
|
|
|
public static function sql($query, $param = []) |
|
577
|
|
|
{ |
|
578
|
|
|
self::instance(); |
|
579
|
|
|
|
|
580
|
|
|
try { |
|
581
|
|
|
self::$_instance->verifyConnection(); |
|
582
|
|
|
} catch (\Exception $e) { |
|
583
|
|
|
throw new \Exception($e->getMessage()); |
|
584
|
|
|
} |
|
585
|
|
|
|
|
586
|
|
|
if (!is_array($param)) throw new \Exception('Invalid parameter type.'); |
|
587
|
|
|
|
|
588
|
|
|
self::$_instance->_current_custom_query_values = $param; |
|
589
|
|
|
|
|
590
|
|
|
$objetos = self::$_instance->query($query, self::$_instance->_current_custom_query_values); |
|
591
|
|
|
|
|
592
|
|
|
self::$_instance->_current_custom_query_values = []; |
|
593
|
|
|
|
|
594
|
|
|
self::$_instance->cleanNewData(); |
|
595
|
|
|
|
|
596
|
|
|
return $objetos; |
|
597
|
|
|
} |
|
598
|
|
|
|
|
599
|
|
|
/** |
|
600
|
|
|
* Get the last execute query from ORM |
|
601
|
|
|
* @return string on last query |
|
602
|
|
|
*/ |
|
603
|
|
|
final protected function getLastQuery() |
|
604
|
|
|
{ |
|
605
|
|
|
return $this->Connection->getLastPerformedQuery(); |
|
606
|
|
|
} |
|
607
|
|
|
|
|
608
|
|
|
/** |
|
609
|
|
|
* Get all execute query from ORM |
|
|
|
|
|
|
610
|
|
|
* @return all query executed |
|
611
|
|
|
*/ |
|
612
|
|
|
final protected function getAllPerfomedQuery() |
|
613
|
|
|
{ |
|
614
|
|
|
return $this->Connection->getPerformedQuery(); |
|
615
|
|
|
} |
|
616
|
|
|
|
|
617
|
|
|
/** |
|
618
|
|
|
* Execute an closure after insert, update or delete. |
|
619
|
|
|
* @param $closure |
|
620
|
|
|
* @throws \Exception If colunm isn't a closure |
|
621
|
|
|
* @return $this |
|
622
|
|
|
*/ |
|
623
|
|
|
final protected function setTriggerAfter($closure = null) |
|
624
|
|
|
{ |
|
625
|
|
|
if (!is_callable($closure)) throw new Exception('The parameter don\'t is an closure.'); |
|
|
|
|
|
|
626
|
|
|
|
|
627
|
|
|
$this->triggerAfter = $closure; |
|
628
|
|
|
|
|
629
|
|
|
return $this; |
|
630
|
|
|
} |
|
631
|
|
|
|
|
632
|
|
|
/** |
|
633
|
|
|
* Execute an closure before insert, update or delete. |
|
634
|
|
|
* @param $closure |
|
635
|
|
|
* @throws \Exception If colunm isn't a closure |
|
636
|
|
|
* @return $this |
|
637
|
|
|
*/ |
|
638
|
|
|
final protected function setTriggerBefore($closure = null) |
|
639
|
|
|
{ |
|
640
|
|
|
if (!is_callable($closure)) throw new Exception('The parameter don\'t is an closure.'); |
|
641
|
|
|
|
|
642
|
|
|
$this->triggerBefore = $closure; |
|
643
|
|
|
|
|
644
|
|
|
return $this; |
|
645
|
|
|
} |
|
646
|
|
|
|
|
647
|
|
|
/** |
|
648
|
|
|
* Change schema on postgres |
|
|
|
|
|
|
649
|
|
|
* @param $schema schema name |
|
650
|
|
|
* @return $this |
|
651
|
|
|
*/ |
|
652
|
|
|
final protected function changeSchema($schema) |
|
653
|
|
|
{ |
|
654
|
|
|
if (!is_string($schema)) throw new \Exception('The parameter don\'t is an String.'); |
|
655
|
|
|
|
|
656
|
|
|
$this->Connection->changeSchema($schema); |
|
657
|
|
|
return $this; |
|
658
|
|
|
} |
|
659
|
|
|
|
|
660
|
|
|
/** |
|
661
|
|
|
* Clean data |
|
662
|
|
|
* @return $this |
|
663
|
|
|
*/ |
|
664
|
|
|
final protected function cleanNewData() |
|
665
|
|
|
{ |
|
666
|
|
|
$this->_newData = []; |
|
667
|
|
|
return $this; |
|
668
|
|
|
} |
|
669
|
|
|
|
|
670
|
|
|
protected function getData() |
|
671
|
|
|
{ |
|
672
|
|
|
return $this->_data; |
|
673
|
|
|
} |
|
674
|
|
|
|
|
675
|
|
|
/** |
|
676
|
|
|
* Auxiliar method for current instance is set |
|
|
|
|
|
|
677
|
|
|
* @return current class |
|
678
|
|
|
*/ |
|
679
|
|
|
final private static function instance() |
|
680
|
|
|
{ |
|
681
|
|
|
$calledClass = get_called_class(); |
|
682
|
|
|
|
|
683
|
|
|
self::$_instance = new $calledClass(); |
|
684
|
|
|
|
|
685
|
|
|
return self::$_instance; |
|
686
|
|
|
} |
|
687
|
|
|
|
|
688
|
|
|
final private function verifyConnection() |
|
689
|
|
|
{ |
|
690
|
|
|
if (is_null($this->Connection->getCurrentConnectionString())) throw new \Exception('Not set connection.'); |
|
691
|
|
|
} |
|
692
|
|
|
|
|
693
|
|
|
protected function setTableName($tableName) |
|
694
|
|
|
{ |
|
695
|
|
|
self::$table_name = $tableName; |
|
696
|
|
|
|
|
697
|
|
|
return $this; |
|
698
|
|
|
} |
|
699
|
|
|
|
|
700
|
|
|
protected function setPrimaryKey($primaryKey = 'id') |
|
701
|
|
|
{ |
|
702
|
|
|
self::$primary_key = $primaryKey; |
|
703
|
|
|
|
|
704
|
|
|
return $this; |
|
705
|
|
|
} |
|
706
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths