1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Shared Kernel library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016-present LIN3S <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace LIN3S\SharedKernel\Infrastructure\Persistence\Sql; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Beñat Espiña <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
final class Pdo |
20
|
|
|
{ |
21
|
|
|
public const DATE_FORMAT = 'Y-m-d H:i:s'; |
22
|
|
|
|
23
|
|
|
private $pdo; |
24
|
|
|
|
25
|
|
|
public function __construct(\PDO $pdo) |
26
|
|
|
{ |
27
|
|
|
$this->pdo = $pdo; |
28
|
|
|
$this->pdo->setAttribute($this->pdo::ATTR_ERRMODE, $this->pdo::ERRMODE_EXCEPTION); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function query(string $sql, array $parameters) : array |
32
|
|
|
{ |
33
|
|
|
return $this->execute($sql, $parameters)->fetchAll(\PDO::FETCH_ASSOC); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function singleQuery(string $sql, array $parameters) : ?array |
37
|
|
|
{ |
38
|
|
|
$result = $this->execute($sql, $parameters)->fetch(\PDO::FETCH_ASSOC); |
39
|
|
|
|
40
|
|
|
return false === $result ? null : $result; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function insert($table, $columns, $numberOfInsertions, callable $prepareData) : void |
44
|
|
|
{ |
45
|
|
|
$rowPlaces = '(' . implode(', ', array_fill(0, count($columns), '?')) . ')'; |
46
|
|
|
$allPlaces = implode(', ', array_fill(0, $numberOfInsertions, $rowPlaces)); |
47
|
|
|
|
48
|
|
|
$sql = 'INSERT INTO ' . $table . ' (' . implode(', ', $columns) . ') VALUES ' . $allPlaces; |
49
|
|
|
|
50
|
|
|
$this->execute($sql, call_user_func($prepareData)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function update($table, $columns, $numberOfUpdates, callable $prepareData) : void |
54
|
|
|
{ |
55
|
|
|
$sql = 'UPDATE ' . $table . ' SET('; |
56
|
|
|
$updates = 0; |
57
|
|
|
foreach ($columns as $column => $value) { |
58
|
|
|
if ($updates !== 0) { |
59
|
|
|
$sql .= ','; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($updates === $numberOfUpdates) { |
63
|
|
|
break; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$sql .= $column . '=' . $value; |
67
|
|
|
$updates++; |
68
|
|
|
} |
69
|
|
|
$sql .= ")"; |
70
|
|
|
|
71
|
|
|
$this->execute($sql, call_user_func($prepareData)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function executeAtomically(callable $function) |
75
|
|
|
{ |
76
|
|
|
$this->pdo->beginTransaction(); |
77
|
|
|
|
78
|
|
|
try { |
79
|
|
|
$return = call_user_func($function, $this); |
80
|
|
|
|
81
|
|
|
$this->pdo->commit(); |
82
|
|
|
|
83
|
|
|
return $return ?: true; |
84
|
|
|
} catch (\Exception | \Throwable $exception) { |
85
|
|
|
$this->pdo->rollback(); |
86
|
|
|
|
87
|
|
|
throw $exception; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function execute(string $sql, array $parameters) : \PDOStatement |
92
|
|
|
{ |
93
|
|
|
$statement = $this->pdo->prepare($sql); |
94
|
|
|
$statement->execute($parameters); |
95
|
|
|
|
96
|
|
|
return $statement; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|