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
|
1 |
|
public function fetchScalar($SQL, array $parameters = []) |
67
|
|
|
{ |
68
|
1 |
|
$stmt = $this->getExecutedStatement($SQL, $parameters); |
69
|
|
|
|
70
|
1 |
|
if (0 < $stmt->rowCount()) { |
71
|
1 |
|
return $stmt->fetch(\PDO::FETCH_COLUMN); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritdoc |
79
|
|
|
*/ |
80
|
|
|
public function fetchRow($SQL, array $parameters = []) |
81
|
|
|
{ |
82
|
|
|
$stmt = $this->getExecutedStatement($SQL, $parameters); |
83
|
|
|
|
84
|
|
|
if (0 < $stmt->rowCount()) { |
85
|
|
|
return $stmt->fetch(\PDO::FETCH_ASSOC); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return []; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
1 |
|
public function fetchAll($SQL, array $parameters = []) |
95
|
|
|
{ |
96
|
1 |
|
$stmt = $this->getExecutedStatement($SQL, $parameters); |
97
|
|
|
|
98
|
1 |
|
if (0 < $stmt->rowCount()) { |
99
|
1 |
|
return $stmt->fetchAll(\PDO::FETCH_ASSOC); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return []; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritdoc |
107
|
|
|
*/ |
108
|
|
|
public function rowCount($SQL, array $parameters = []) |
109
|
|
|
{ |
110
|
|
|
$stmt = $this->getExecutedStatement($SQL, $parameters); |
111
|
|
|
|
112
|
|
|
return $stmt->rowCount() ?: 0; |
113
|
|
|
} |
114
|
|
|
} |