1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hgraca\MicroDbal\Raw; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Hgraca\Helper\ArrayHelper; |
7
|
|
|
use Hgraca\MicroDbal\Raw\Exception\BindingException; |
8
|
|
|
use Hgraca\MicroDbal\Raw\Exception\ExecutionException; |
9
|
|
|
use Hgraca\MicroDbal\Raw\Exception\TypeResolutionException; |
10
|
|
|
use Hgraca\MicroDbal\RawClientInterface; |
11
|
|
|
use PDO; |
12
|
|
|
use PDOStatement; |
13
|
|
|
|
14
|
|
|
final class PdoClient implements RawClientInterface |
15
|
|
|
{ |
16
|
|
|
/** @var PDO */ |
17
|
|
|
private $pdo; |
18
|
|
|
|
19
|
17 |
|
public function __construct(PDO $pdo) |
20
|
|
|
{ |
21
|
17 |
|
$this->pdo = $pdo; |
22
|
17 |
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
23
|
17 |
|
} |
24
|
|
|
|
25
|
13 |
|
public function executeQuery(string $sql, array $bindingsList = []): array |
26
|
|
|
{ |
27
|
13 |
|
return $this->execute($sql, $bindingsList)->fetchAll(PDO::FETCH_ASSOC); |
28
|
|
|
} |
29
|
|
|
|
30
|
9 |
|
public function executeCommand(string $sql, array $bindingsList) |
31
|
|
|
{ |
32
|
9 |
|
if (!ArrayHelper::isTwoDimensional($bindingsList)) { |
33
|
8 |
|
$bindingsList = [$bindingsList]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
try { |
37
|
9 |
|
$this->pdo->beginTransaction(); |
38
|
9 |
|
foreach ($bindingsList as $bindings) { |
39
|
9 |
|
$preparedStatement = $this->execute($sql, $bindings, $preparedStatement ?? null); |
40
|
|
|
} |
41
|
6 |
|
$this->pdo->commit(); |
42
|
3 |
|
} catch (Exception $e) { |
43
|
3 |
|
$this->pdo->rollBack(); |
44
|
3 |
|
throw $e; |
45
|
|
|
} |
46
|
6 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @throws ExecutionException |
50
|
|
|
*/ |
51
|
17 |
|
private function execute(string $sql, array $bindings = [], PDOStatement $preparedStatement = null): PDOStatement |
52
|
|
|
{ |
53
|
17 |
|
$preparedStatement = $preparedStatement ?? $this->pdo->prepare($sql); |
54
|
|
|
|
55
|
17 |
|
$this->bindParameterList($preparedStatement, $bindings); |
56
|
|
|
|
57
|
14 |
|
if (!$preparedStatement->execute()) { |
58
|
2 |
|
throw new ExecutionException( |
59
|
2 |
|
"Could not execute query: '$sql'" |
60
|
2 |
|
. ' Error code: ' . $preparedStatement->errorCode() |
61
|
2 |
|
. ' Error Info: ' . json_encode($preparedStatement->errorInfo()) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
12 |
|
return $preparedStatement; |
66
|
|
|
} |
67
|
|
|
|
68
|
16 |
|
private function bindParameterList(PDOStatement $stmt, array $parameterList) |
69
|
|
|
{ |
70
|
16 |
|
foreach ($parameterList as $name => $value) { |
71
|
12 |
|
$this->bindParameter($stmt, $name, $value); |
72
|
|
|
} |
73
|
13 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @throws BindingException |
77
|
|
|
*/ |
78
|
12 |
|
private function bindParameter(PDOStatement $stmt, string $name, $value) |
79
|
|
|
{ |
80
|
12 |
|
$pdoType = $this->resolvePdoType($value); |
81
|
11 |
|
$bound = $stmt->bindValue( |
82
|
|
|
$name, |
83
|
11 |
|
$pdoType === PDO::PARAM_STR ? strval($value) : $value, |
84
|
|
|
$pdoType |
85
|
|
|
); |
86
|
|
|
|
87
|
11 |
|
if (false === $bound) { |
88
|
2 |
|
throw new BindingException( |
89
|
2 |
|
'Could not bind value: ' . json_encode(['name' => $name, 'value' => $value, 'type' => $pdoType]) |
90
|
|
|
); |
91
|
|
|
} |
92
|
9 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param mixed $value |
96
|
|
|
* |
97
|
|
|
* @throws TypeResolutionException |
98
|
|
|
*/ |
99
|
12 |
|
private function resolvePdoType($value): int |
100
|
|
|
{ |
101
|
12 |
|
$type = gettype($value); |
102
|
|
|
switch ($type) { |
103
|
12 |
|
case 'boolean': |
|
|
|
|
104
|
5 |
|
$pdoType = PDO::PARAM_BOOL; // 5 |
105
|
5 |
|
break; |
106
|
8 |
|
case 'string': |
|
|
|
|
107
|
5 |
|
case 'double': // float |
108
|
7 |
|
$pdoType = PDO::PARAM_STR; // 2 |
109
|
7 |
|
break; |
110
|
5 |
|
case 'integer': |
111
|
1 |
|
$pdoType = PDO::PARAM_INT; // 1 |
112
|
1 |
|
break; |
113
|
5 |
|
case 'NULL': |
114
|
4 |
|
$pdoType = PDO::PARAM_NULL; // 0 |
115
|
4 |
|
break; |
116
|
1 |
|
case 'object': |
117
|
1 |
|
$class = get_class($value); |
118
|
1 |
|
throw new TypeResolutionException("Invalid type '$class' for query filter."); |
119
|
|
|
default: |
120
|
|
|
throw new TypeResolutionException("Invalid type '$type' for query filter."); |
121
|
|
|
} |
122
|
|
|
|
123
|
11 |
|
return $pdoType; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.