|
1
|
|
|
<?php |
|
2
|
|
|
namespace BMorais\Database; |
|
3
|
|
|
|
|
4
|
|
|
use PDO; |
|
5
|
|
|
use PDOException; |
|
6
|
|
|
use PDOStatement; |
|
7
|
|
|
use stdClass; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* CLASS TRAIT DATALAYER |
|
11
|
|
|
* This class of execution methods in the database |
|
12
|
|
|
* |
|
13
|
|
|
* @author Bruno Morais <[email protected]> |
|
14
|
|
|
* @copyright MIT, bmorais.com |
|
15
|
|
|
* @package bmorais\database |
|
16
|
|
|
* @subpackage class |
|
17
|
|
|
* @access private |
|
18
|
|
|
*/ |
|
19
|
|
|
trait DatalayerTrait |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var PDO|null |
|
22
|
|
|
* @deprecated USE $this->setClassModel("NAME"); |
|
23
|
|
|
* */ |
|
24
|
|
|
protected ?PDO $instance = null; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string |
|
27
|
|
|
* @deprecated |
|
28
|
|
|
*/ |
|
29
|
|
|
protected string $fields; |
|
30
|
|
|
|
|
31
|
|
|
/** @var PDOStatement|null |
|
32
|
|
|
* @deprecated */ |
|
33
|
|
|
protected $prepare = null; |
|
34
|
|
|
|
|
35
|
|
|
/** @var string |
|
36
|
|
|
* @deprecated |
|
37
|
|
|
*/ |
|
38
|
|
|
protected string $database = CONFIG_DATA_LAYER["dbname"]; |
|
39
|
|
|
|
|
40
|
|
|
/** @var string |
|
41
|
|
|
* @deprecated USE $this->setInstance(PDO); |
|
42
|
|
|
*/ |
|
43
|
|
|
protected string $classModel; |
|
44
|
|
|
|
|
45
|
|
|
/** @var string |
|
46
|
|
|
* @deprecated USE $this->setTableName("name"); |
|
47
|
|
|
*/ |
|
48
|
|
|
protected string $tableName; |
|
49
|
|
|
|
|
50
|
|
|
/** @var string */ |
|
51
|
|
|
private string $tableAlias; |
|
52
|
|
|
|
|
53
|
|
|
/** @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
private array $data = []; |
|
56
|
|
|
|
|
57
|
|
|
/** @var string */ |
|
58
|
|
|
private string $logSQL; |
|
59
|
|
|
|
|
60
|
|
|
/** @var PDOException */ |
|
61
|
|
|
private $error; |
|
62
|
|
|
|
|
63
|
|
|
/** @var string */ |
|
64
|
|
|
private string $query = ""; |
|
65
|
|
|
|
|
66
|
|
|
/** @var array */ |
|
67
|
|
|
private array $params = []; |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return PDO|false |
|
72
|
|
|
* @throws DatabaseException |
|
73
|
|
|
*/ |
|
74
|
|
|
private function getConnect() |
|
75
|
|
|
{ |
|
76
|
|
|
try { |
|
77
|
|
|
if (strpos($_SERVER['SERVER_NAME'], mb_strtolower(CONFIG_DATA_LAYER["homologation"])) && !strpos($this->getDatabase(), ucfirst(CONFIG_DATA_LAYER["homologation"]))) { |
|
78
|
|
|
$database = $this->getDatabase().ucfirst(CONFIG_DATA_LAYER["homologation"] ?? ""); |
|
79
|
|
|
$this->setDatabase($database); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (empty($this->instance)) { |
|
|
|
|
|
|
83
|
|
|
$this->instance = new PDO( |
|
|
|
|
|
|
84
|
|
|
CONFIG_DATA_LAYER['driver'] . ':host=' . CONFIG_DATA_LAYER['host'] . ';dbname=' . $this->getDatabase() . ';port=' . CONFIG_DATA_LAYER['port'], |
|
85
|
|
|
CONFIG_DATA_LAYER['username'], |
|
86
|
|
|
CONFIG_DATA_LAYER['passwd'], |
|
87
|
|
|
CONFIG_DATA_LAYER['options'] |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $this->instance; |
|
|
|
|
|
|
92
|
|
|
} catch (PDOException $e) { |
|
93
|
|
|
throw new DatabaseException( |
|
94
|
|
|
"Database connection failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}]", |
|
95
|
|
|
$e->getCode(), |
|
96
|
|
|
$e |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param ?PDO $pdo |
|
103
|
|
|
* @return Crud |
|
104
|
|
|
* |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function setInstance(?PDO $pdo): self |
|
107
|
|
|
{ |
|
108
|
|
|
$this->instance = $pdo; |
|
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return PDO |
|
114
|
|
|
* @throws DatabaseException |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function getInstance(): PDO |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->getConnect(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $database |
|
123
|
|
|
* @return $this |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function setDatabase(string $database): self |
|
126
|
|
|
{ |
|
127
|
|
|
if (strpos($_SERVER['SERVER_NAME'], mb_strtolower(CONFIG_DATA_LAYER["homologation"])) && !strpos($database, ucfirst(CONFIG_DATA_LAYER["homologation"]))) { |
|
128
|
|
|
$database = $database.ucfirst(CONFIG_DATA_LAYER["homologation"] ?? ""); |
|
129
|
|
|
$this->database = $database; |
|
|
|
|
|
|
130
|
|
|
} else { |
|
131
|
|
|
$this->database = $database; |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if (!empty($this->instance)){ |
|
|
|
|
|
|
135
|
|
|
$this->executeSQL("USE {$this->getDatabase()}"); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function getDatabase(): string |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->database ?? ""; |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param string $fields |
|
151
|
|
|
* @return Crud |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function setFields(string $fields): self |
|
154
|
|
|
{ |
|
155
|
|
|
$this->fields = $fields; |
|
|
|
|
|
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function getFields():string |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->fields; |
|
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param string $tableName |
|
169
|
|
|
* @param string $tableAlias |
|
170
|
|
|
* @return CrudBuilder|Crud|DatalayerTrait |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function setTableName(string $tableName, string $tableAlias = ""): self |
|
173
|
|
|
{ |
|
174
|
|
|
if (!empty($tableAlias)) |
|
175
|
|
|
$this->tableAlias = $tableAlias; |
|
176
|
|
|
$this->tableName = $tableName; |
|
|
|
|
|
|
177
|
|
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
|
|
protected function getTableName(): string |
|
184
|
|
|
{ |
|
185
|
|
|
return $this->tableName ?? ""; |
|
|
|
|
|
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
protected function getTableAlias(): string |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->tableAlias ?? ""; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param string $classModel |
|
195
|
|
|
* @return Crud |
|
196
|
|
|
*/ |
|
197
|
|
|
protected function setClassModel(string $classModel): self |
|
198
|
|
|
{ |
|
199
|
|
|
$this->classModel = $classModel; |
|
|
|
|
|
|
200
|
|
|
return $this; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
protected function getClassModel(): string |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->classModel; |
|
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param string $classModel |
|
210
|
|
|
* @return Crud |
|
211
|
|
|
*/ |
|
212
|
|
|
protected function setPrepare(PDOStatement $prepare): self |
|
213
|
|
|
{ |
|
214
|
|
|
$this->prepare = $prepare; |
|
|
|
|
|
|
215
|
|
|
return $this; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
protected function getPrepare(): PDOStatement |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->prepare; |
|
|
|
|
|
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @param $params |
|
225
|
|
|
* @return self |
|
226
|
|
|
*/ |
|
227
|
|
|
protected function setParams($params): self |
|
228
|
|
|
{ |
|
229
|
|
|
$this->params = array_merge($this->params, $params); |
|
230
|
|
|
return $this; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @param $params |
|
235
|
|
|
* @return array |
|
236
|
|
|
*/ |
|
237
|
|
|
protected function getParams(): array |
|
238
|
|
|
{ |
|
239
|
|
|
return $this->params; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
protected function getData(): array |
|
243
|
|
|
{ |
|
244
|
|
|
return $this->data; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
protected function setData(array $array): self |
|
248
|
|
|
{ |
|
249
|
|
|
$this->data = $array; |
|
250
|
|
|
return $this; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
public function getQuery(): string |
|
254
|
|
|
{ |
|
255
|
|
|
return $this->query; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
public function setQuery(string $query): self |
|
259
|
|
|
{ |
|
260
|
|
|
$this->query = $query; |
|
261
|
|
|
return $this; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @param string $query |
|
266
|
|
|
* @param array|null $params |
|
267
|
|
|
* @return PDOStatement |
|
268
|
|
|
* @throws DatabaseException |
|
269
|
|
|
*/ |
|
270
|
|
|
protected function executeSQL(string $query, ?array $params = null): PDOStatement |
|
271
|
|
|
{ |
|
272
|
|
|
try { |
|
273
|
|
|
$this->setPrepare($this->getInstance()->prepare($query)); |
|
274
|
|
|
$this->setSQL($query, $params); |
|
275
|
|
|
$this->getPrepare()->execute($params); |
|
276
|
|
|
return $this->getPrepare(); |
|
277
|
|
|
} catch (PDOException $e) { |
|
278
|
|
|
throw new DatabaseException( |
|
279
|
|
|
"SQL execution failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}]", |
|
280
|
|
|
$e->getCode(), |
|
281
|
|
|
$e, |
|
282
|
|
|
$query, |
|
283
|
|
|
$params |
|
284
|
|
|
); |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @param PDOStatement|null $prepare |
|
290
|
|
|
* @return int|null |
|
291
|
|
|
* @throws DatabaseException |
|
292
|
|
|
*/ |
|
293
|
|
|
protected function rowCount(?PDOStatement $prepare = null): ?int |
|
294
|
|
|
{ |
|
295
|
|
|
try { |
|
296
|
|
|
$prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
|
297
|
|
|
return $prepare->rowCount(); |
|
298
|
|
|
} catch (PDOException $e) { |
|
299
|
|
|
throw new DatabaseException( |
|
300
|
|
|
"Row count failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}]", |
|
301
|
|
|
$e->getCode(), |
|
302
|
|
|
$e |
|
303
|
|
|
); |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* @param PDOStatement|null $prepare |
|
309
|
|
|
* @return array|null |
|
310
|
|
|
* @throws DatabaseException |
|
311
|
|
|
*/ |
|
312
|
|
|
protected function fetchArrayAssoc(?PDOStatement $prepare = null): ?array |
|
313
|
|
|
{ |
|
314
|
|
|
try { |
|
315
|
|
|
$prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
|
316
|
|
|
$dados = $prepare->fetchAll(PDO::FETCH_ASSOC); |
|
317
|
|
|
$this->setData($dados); |
|
318
|
|
|
return $dados; |
|
319
|
|
|
} catch (PDOException $e) { |
|
320
|
|
|
throw new DatabaseException( |
|
321
|
|
|
"Fetch array assoc failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}]", |
|
322
|
|
|
$e->getCode(), |
|
323
|
|
|
$e |
|
324
|
|
|
); |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* @param PDOStatement|null $prepare |
|
330
|
|
|
* @return array|null |
|
331
|
|
|
* @throws DatabaseException |
|
332
|
|
|
*/ |
|
333
|
|
|
protected function fetchArrayObj(?PDOStatement $prepare = null): ?array |
|
334
|
|
|
{ |
|
335
|
|
|
try { |
|
336
|
|
|
$prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
|
337
|
|
|
$dados = $prepare->fetchAll(PDO::FETCH_OBJ); |
|
338
|
|
|
$this->setData($dados); |
|
339
|
|
|
return $dados; |
|
340
|
|
|
} catch (PDOException $e) { |
|
341
|
|
|
throw new DatabaseException( |
|
342
|
|
|
"Fetch array object failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}]", |
|
343
|
|
|
$e->getCode(), |
|
344
|
|
|
$e |
|
345
|
|
|
); |
|
346
|
|
|
} |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* @param PDOStatement|null $prepare |
|
351
|
|
|
* @param String|null $classModel |
|
352
|
|
|
* @return array|null |
|
353
|
|
|
* @throws DatabaseException |
|
354
|
|
|
*/ |
|
355
|
|
|
protected function fetchArrayClass(?PDOStatement $prepare = null, ?string $classModel = null): ?array |
|
356
|
|
|
{ |
|
357
|
|
|
try { |
|
358
|
|
|
$prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
|
359
|
|
|
$classModel = empty($classModel) ? $this->getClassModel() : $classModel; |
|
360
|
|
|
$dados = $prepare->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, CONFIG_DATA_LAYER["directory_models"] . $classModel); |
|
361
|
|
|
$this->setData($dados); |
|
362
|
|
|
return $dados; |
|
363
|
|
|
} catch (PDOException $e) { |
|
364
|
|
|
throw new DatabaseException( |
|
365
|
|
|
"Fetch array class failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}]", |
|
366
|
|
|
$e->getCode(), |
|
367
|
|
|
$e |
|
368
|
|
|
); |
|
369
|
|
|
} |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* @param PDOStatement|null $prepare |
|
374
|
|
|
* @return array|null |
|
375
|
|
|
* @throws DatabaseException |
|
376
|
|
|
*/ |
|
377
|
|
|
protected function fetchOneAssoc(?PDOStatement $prepare = null): ?array |
|
378
|
|
|
{ |
|
379
|
|
|
try { |
|
380
|
|
|
$prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
|
381
|
|
|
return $prepare->fetch(PDO::FETCH_ASSOC); |
|
382
|
|
|
} catch (PDOException $e) { |
|
383
|
|
|
throw new DatabaseException( |
|
384
|
|
|
"Fetch one assoc failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}]", |
|
385
|
|
|
$e->getCode(), |
|
386
|
|
|
$e |
|
387
|
|
|
); |
|
388
|
|
|
} |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
/** |
|
392
|
|
|
* @param PDOStatement|null $prepare |
|
393
|
|
|
* @return stdClass|null |
|
394
|
|
|
* @throws DatabaseException |
|
395
|
|
|
*/ |
|
396
|
|
|
protected function fetchOneObj(?PDOStatement $prepare = null): ?stdClass |
|
397
|
|
|
{ |
|
398
|
|
|
try { |
|
399
|
|
|
$prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
|
400
|
|
|
return $prepare->fetch(PDO::FETCH_OBJ); |
|
401
|
|
|
} catch (PDOException $e) { |
|
402
|
|
|
throw new DatabaseException( |
|
403
|
|
|
"Fetch one object failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}]", |
|
404
|
|
|
$e->getCode(), |
|
405
|
|
|
$e |
|
406
|
|
|
); |
|
407
|
|
|
} |
|
408
|
|
|
} |
|
409
|
|
|
|
|
410
|
|
|
/** |
|
411
|
|
|
* @param PDOStatement|null $prepare |
|
412
|
|
|
* @param String|null $class |
|
413
|
|
|
* @return object|null |
|
414
|
|
|
* @throws DatabaseException |
|
415
|
|
|
*/ |
|
416
|
|
|
protected function fetchOneClass(?PDOStatement $prepare = null, ?string $class = null): ?object |
|
417
|
|
|
{ |
|
418
|
|
|
try { |
|
419
|
|
|
$prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
|
420
|
|
|
$class = empty($class) ? $this->getClassModel() : $class; |
|
421
|
|
|
return $prepare->fetchObject(CONFIG_DATA_LAYER["directory_models"] . $class); |
|
422
|
|
|
} catch (PDOException $e) { |
|
423
|
|
|
throw new DatabaseException( |
|
424
|
|
|
"Fetch one class failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}]", |
|
425
|
|
|
$e->getCode(), |
|
426
|
|
|
$e |
|
427
|
|
|
); |
|
428
|
|
|
} |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
/** |
|
432
|
|
|
* @return bool|null |
|
433
|
|
|
* @throws DatabaseException |
|
434
|
|
|
*/ |
|
435
|
|
|
protected function beginTransaction(): ?bool |
|
436
|
|
|
{ |
|
437
|
|
|
try { |
|
438
|
|
|
$this->getInstance()->beginTransaction(); |
|
439
|
|
|
return true; |
|
440
|
|
|
} catch (PDOException $e) { |
|
441
|
|
|
throw new DatabaseException( |
|
442
|
|
|
"Begin transaction failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}]", |
|
443
|
|
|
$e->getCode(), |
|
444
|
|
|
$e |
|
445
|
|
|
); |
|
446
|
|
|
} |
|
447
|
|
|
} |
|
448
|
|
|
|
|
449
|
|
|
/** |
|
450
|
|
|
* @return bool|null |
|
451
|
|
|
* @throws DatabaseException |
|
452
|
|
|
*/ |
|
453
|
|
|
protected function commitTransaction(): ?bool |
|
454
|
|
|
{ |
|
455
|
|
|
try { |
|
456
|
|
|
$this->getInstance()->commit(); |
|
457
|
|
|
return true; |
|
458
|
|
|
} catch (PDOException $e) { |
|
459
|
|
|
throw new DatabaseException( |
|
460
|
|
|
"Commit transaction failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}]", |
|
461
|
|
|
$e->getCode(), |
|
462
|
|
|
$e |
|
463
|
|
|
); |
|
464
|
|
|
} |
|
465
|
|
|
} |
|
466
|
|
|
|
|
467
|
|
|
/** |
|
468
|
|
|
* @return bool|null |
|
469
|
|
|
* @throws DatabaseException |
|
470
|
|
|
*/ |
|
471
|
|
|
protected function rollBackTransaction(): ?bool |
|
472
|
|
|
{ |
|
473
|
|
|
try { |
|
474
|
|
|
$this->getInstance()->rollBack(); |
|
475
|
|
|
return true; |
|
476
|
|
|
} catch (PDOException $e) { |
|
477
|
|
|
throw new DatabaseException( |
|
478
|
|
|
"Rollback transaction failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}]", |
|
479
|
|
|
$e->getCode(), |
|
480
|
|
|
$e |
|
481
|
|
|
); |
|
482
|
|
|
} |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
/** |
|
486
|
|
|
* @return string|null |
|
487
|
|
|
* @throws DatabaseException |
|
488
|
|
|
*/ |
|
489
|
|
|
private function lastId(): ?string |
|
490
|
|
|
{ |
|
491
|
|
|
try { |
|
492
|
|
|
return $this->getInstance()->lastInsertId(); |
|
493
|
|
|
} catch (PDOException $e) { |
|
494
|
|
|
throw new DatabaseException( |
|
495
|
|
|
"Get last insert ID failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}]", |
|
496
|
|
|
$e->getCode(), |
|
497
|
|
|
$e |
|
498
|
|
|
); |
|
499
|
|
|
} |
|
500
|
|
|
} |
|
501
|
|
|
|
|
502
|
|
|
/** |
|
503
|
|
|
* @param $sql_string |
|
504
|
|
|
* @param array|null $params |
|
505
|
|
|
* @return void |
|
506
|
|
|
* @throws DatabaseException |
|
507
|
|
|
*/ |
|
508
|
|
|
private function setSQL($sql_string, ?array $params = null) |
|
509
|
|
|
{ |
|
510
|
|
|
try { |
|
511
|
|
|
if (!empty($params)) { |
|
512
|
|
|
$indexed = $params == array_values($params); |
|
513
|
|
|
foreach ($params as $k => $v) { |
|
514
|
|
|
if (is_object($v)) { |
|
515
|
|
|
if ($v instanceof \DateTime) { |
|
516
|
|
|
$v = $v->format('Y-m-d H:i:s'); |
|
517
|
|
|
} else { |
|
518
|
|
|
continue; |
|
519
|
|
|
} |
|
520
|
|
|
} elseif (is_string($v)) { |
|
521
|
|
|
$v = "'$v'"; |
|
522
|
|
|
} elseif ($v === null) { |
|
523
|
|
|
$v = 'NULL'; |
|
524
|
|
|
} elseif (is_array($v)) { |
|
525
|
|
|
$v = implode(',', $v); |
|
526
|
|
|
} |
|
527
|
|
|
|
|
528
|
|
|
if ($indexed) { |
|
529
|
|
|
$sql_string = preg_replace('/\?/', $v, $sql_string, 1); |
|
530
|
|
|
} else { |
|
531
|
|
|
if ($k[0] != ':') { |
|
532
|
|
|
$k = ':' . $k; |
|
533
|
|
|
} //add leading colon if it was left out |
|
534
|
|
|
$sql_string = str_replace($k, $v, $sql_string); |
|
535
|
|
|
} |
|
536
|
|
|
} |
|
537
|
|
|
} |
|
538
|
|
|
$this->logSQL = $sql_string; |
|
539
|
|
|
} catch (PDOException $e) { |
|
540
|
|
|
throw new DatabaseException( |
|
541
|
|
|
"Set SQL failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}]", |
|
542
|
|
|
$e->getCode(), |
|
543
|
|
|
$e |
|
544
|
|
|
); |
|
545
|
|
|
} |
|
546
|
|
|
} |
|
547
|
|
|
|
|
548
|
|
|
/** |
|
549
|
|
|
* @return string|null |
|
550
|
|
|
* @throws DatabaseException |
|
551
|
|
|
*/ |
|
552
|
|
|
protected function getSQL(): ?string |
|
553
|
|
|
{ |
|
554
|
|
|
try { |
|
555
|
|
|
return $this->logSQL ?? ""; |
|
556
|
|
|
} catch (PDOException $e) { |
|
|
|
|
|
|
557
|
|
|
throw new DatabaseException( |
|
558
|
|
|
"Get SQL failed - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}]", |
|
559
|
|
|
$e->getCode(), |
|
560
|
|
|
$e |
|
561
|
|
|
); |
|
562
|
|
|
} |
|
563
|
|
|
} |
|
564
|
|
|
|
|
565
|
|
|
/** |
|
566
|
|
|
* @param PDOException $e |
|
567
|
|
|
* @return void |
|
568
|
|
|
* @throws DatabaseException |
|
569
|
|
|
* @deprecated This method is no longer used - exceptions are thrown directly |
|
570
|
|
|
*/ |
|
571
|
|
|
protected function setError(PDOException $e) |
|
572
|
|
|
{ |
|
573
|
|
|
$this->error = $e; |
|
574
|
|
|
throw new DatabaseException( |
|
575
|
|
|
"Database error - TABLE: [{$this->getTableName()}] MESSAGE: [{$e->getMessage()}]", |
|
576
|
|
|
$e->getCode(), |
|
577
|
|
|
$e |
|
578
|
|
|
); |
|
579
|
|
|
} |
|
580
|
|
|
} |
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.