|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Phuria SQL Builder package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2016 Beniamin Jonatan Šimko |
|
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
|
|
|
namespace Phuria\SQLBuilder\Connection; |
|
13
|
|
|
|
|
14
|
|
|
use Phuria\SQLBuilder\Parameter\QueryParameterInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class PDOConnection implements ConnectionInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var \PDO $wrappedConnection |
|
23
|
|
|
*/ |
|
24
|
|
|
private $wrappedConnection; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param \PDO $connection |
|
28
|
|
|
*/ |
|
29
|
2 |
|
public function __construct(\PDO $connection) |
|
30
|
|
|
{ |
|
31
|
2 |
|
$this->wrappedConnection = $connection; |
|
32
|
2 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param $SQL |
|
36
|
|
|
* |
|
37
|
|
|
* @return \PDOStatement |
|
38
|
|
|
*/ |
|
39
|
2 |
|
private function prepareStatement($SQL) |
|
40
|
|
|
{ |
|
41
|
2 |
|
return $this->wrappedConnection->prepare($SQL); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $SQL |
|
46
|
|
|
* @param QueryParameterInterface[] $parameters |
|
47
|
|
|
* |
|
48
|
|
|
* @return \PDOStatement |
|
49
|
|
|
*/ |
|
50
|
2 |
|
private function getExecutedStatement($SQL, array $parameters = []) |
|
51
|
|
|
{ |
|
52
|
2 |
|
$preparedStmt = $this->prepareStatement($SQL); |
|
53
|
|
|
|
|
54
|
2 |
|
foreach ($parameters as $parameter) { |
|
55
|
|
|
$preparedStmt->bindValue($parameter->getName(), $parameter->getValue()); |
|
56
|
2 |
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
$preparedStmt->execute(); |
|
59
|
|
|
|
|
60
|
2 |
|
return $preparedStmt; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritdoc |
|
65
|
|
|
*/ |
|
66
|
|
|
public function execute($SQL, array $parameters = []) |
|
67
|
|
|
{ |
|
68
|
|
|
$stmt = $this->getExecutedStatement($SQL, $parameters); |
|
69
|
|
|
|
|
70
|
|
|
return $stmt->rowCount(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @inheritdoc |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function fetchScalar($SQL, array $parameters = []) |
|
77
|
|
|
{ |
|
78
|
1 |
|
$stmt = $this->getExecutedStatement($SQL, $parameters); |
|
79
|
|
|
|
|
80
|
1 |
|
if (0 < $stmt->rowCount()) { |
|
81
|
1 |
|
return $stmt->fetch(\PDO::FETCH_COLUMN); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return null; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @inheritdoc |
|
89
|
|
|
*/ |
|
90
|
|
|
public function fetchRow($SQL, array $parameters = []) |
|
91
|
|
|
{ |
|
92
|
|
|
$stmt = $this->getExecutedStatement($SQL, $parameters); |
|
93
|
|
|
|
|
94
|
|
|
if (0 < $stmt->rowCount()) { |
|
95
|
|
|
return $stmt->fetch(\PDO::FETCH_ASSOC); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return []; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @inheritdoc |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function fetchAll($SQL, array $parameters = []) |
|
105
|
|
|
{ |
|
106
|
1 |
|
$stmt = $this->getExecutedStatement($SQL, $parameters); |
|
107
|
|
|
|
|
108
|
1 |
|
if (0 < $stmt->rowCount()) { |
|
109
|
1 |
|
return $stmt->fetchAll(\PDO::FETCH_ASSOC); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return []; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @inheritdoc |
|
117
|
|
|
*/ |
|
118
|
|
|
public function rowCount($SQL, array $parameters = []) |
|
119
|
|
|
{ |
|
120
|
|
|
$stmt = $this->getExecutedStatement($SQL, $parameters); |
|
121
|
|
|
|
|
122
|
|
|
return $stmt->rowCount() ?: 0; |
|
123
|
|
|
} |
|
124
|
|
|
} |