1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FMUP\Db\Driver; |
4
|
|
|
|
5
|
|
|
use FMUP\Db\Exception; |
6
|
|
|
use FMUP\Logger; |
7
|
|
|
|
8
|
|
|
class Pdo extends PdoConfiguration |
9
|
|
|
{ |
10
|
|
|
const CHARSET_UTF8 = 'utf8'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Charset to use |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
2 |
|
protected function getCharset() |
17
|
|
|
{ |
18
|
2 |
|
$charset = (string)$this->getSettings('charset'); |
19
|
2 |
|
return $charset ?: self::CHARSET_UTF8; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param \Pdo $instance |
24
|
|
|
* @return $this |
25
|
|
|
*/ |
26
|
1 |
|
protected function defaultConfiguration(\Pdo $instance) |
27
|
|
|
{ |
28
|
1 |
|
$charset = $this->getCharset(); |
29
|
1 |
|
$instance->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
30
|
1 |
|
$instance->setAttribute(\PDO::ATTR_TIMEOUT, 10.0); |
31
|
1 |
|
$instance->exec('SET NAMES ' . $charset); |
32
|
1 |
|
$instance->exec('SET CHARACTER SET ' . $charset); |
33
|
1 |
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Fetch all rows for a given statement |
38
|
|
|
* @param object $statement |
39
|
|
|
* @return array |
40
|
|
|
* @throws Exception |
41
|
|
|
*/ |
42
|
3 |
View Code Duplication |
public function fetchAll($statement) |
|
|
|
|
43
|
|
|
{ |
44
|
3 |
|
if (!$statement instanceof \PDOStatement) { |
45
|
1 |
|
$this->log(Logger::ERROR, 'Statement not in right format', array('statement' => $statement)); |
46
|
1 |
|
throw new Exception('Statement not in right format'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
try { |
50
|
2 |
|
return $statement->fetchAll($this->getFetchMode()); |
51
|
1 |
|
} catch (\PDOException $e) { |
52
|
1 |
|
$this->log(Logger::ERROR, $e->getMessage(), array('error' => $e)); |
53
|
1 |
|
throw new Exception($e->getMessage(), (int)$e->getCode(), $e); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Begin a transaction |
59
|
|
|
* @return bool |
60
|
|
|
* @throws Exception |
61
|
|
|
*/ |
62
|
3 |
|
public function beginTransaction() |
63
|
|
|
{ |
64
|
|
|
try { |
65
|
3 |
|
if ($this->getDriver()->inTransaction()) { |
66
|
1 |
|
$this->log(Logger::CRITICAL, 'Transaction already opened', $this->getSettings()); |
|
|
|
|
67
|
1 |
|
throw new Exception('Transaction already opened'); |
68
|
|
|
} |
69
|
1 |
|
$this->log(Logger::DEBUG, 'Transaction start'); |
70
|
1 |
|
return $this->getDriver()->beginTransaction(); |
71
|
2 |
|
} catch (\PDOException $e) { |
72
|
1 |
|
$this->log(Logger::ERROR, $e->getMessage(), array('settings' => $this->getSettings())); |
73
|
1 |
|
throw new Exception($e->getMessage(), (int)$e->getCode(), $e); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return bool |
79
|
|
|
* @throws Exception |
80
|
|
|
*/ |
81
|
2 |
View Code Duplication |
public function rollback() |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
try { |
84
|
2 |
|
$this->log(Logger::DEBUG, 'Transaction rollback'); |
85
|
2 |
|
return $this->getDriver()->rollBack(); |
86
|
1 |
|
} catch (\PDOException $e) { |
87
|
1 |
|
$this->log(Logger::ERROR, $e->getMessage(), array('error' => $e)); |
88
|
1 |
|
throw new Exception($e->getMessage(), (int)$e->getCode(), $e); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return mixed |
94
|
|
|
* @throws Exception |
95
|
|
|
*/ |
96
|
2 |
|
public function errorCode() |
97
|
|
|
{ |
98
|
|
|
try { |
99
|
2 |
|
return $this->getDriver()->errorCode(); |
100
|
1 |
|
} catch (\PDOException $e) { |
101
|
1 |
|
$this->log(Logger::ERROR, $e->getMessage(), array('error' => $e)); |
102
|
1 |
|
throw new Exception($e->getMessage(), (int)$e->getCode(), $e); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return array |
108
|
|
|
* @throws Exception |
109
|
|
|
*/ |
110
|
2 |
|
public function errorInfo() |
111
|
|
|
{ |
112
|
|
|
try { |
113
|
2 |
|
return $this->getDriver()->errorInfo(); |
114
|
1 |
|
} catch (\PDOException $e) { |
115
|
1 |
|
$this->log(Logger::ERROR, $e->getMessage(), array('error' => $e)); |
116
|
1 |
|
throw new Exception($e->getMessage(), (int)$e->getCode(), $e); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return bool |
122
|
|
|
* @throws Exception |
123
|
|
|
*/ |
124
|
2 |
View Code Duplication |
public function commit() |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
try { |
127
|
2 |
|
$this->log(Logger::DEBUG, 'Transaction commit'); |
128
|
2 |
|
return $this->getDriver()->commit(); |
129
|
1 |
|
} catch (\PDOException $e) { |
130
|
1 |
|
$this->log(Logger::ERROR, $e->getMessage(), array('error' => $e)); |
131
|
1 |
|
throw new Exception($e->getMessage(), (int)$e->getCode(), $e); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Execute a statement for given values |
137
|
|
|
* @param object $statement |
138
|
|
|
* @param array $values |
139
|
|
|
* @return bool |
140
|
|
|
* @throws Exception |
141
|
|
|
*/ |
142
|
3 |
|
public function execute($statement, $values = array()) |
143
|
|
|
{ |
144
|
3 |
|
if (!$statement instanceof \PDOStatement) { |
145
|
1 |
|
$this->log(Logger::ERROR, 'Statement not in right format', array('statement' => $statement)); |
146
|
1 |
|
throw new Exception('Statement not in right format'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
try { |
150
|
2 |
|
$this->log(Logger::DEBUG, 'Executing query', array('values' => $values)); |
151
|
2 |
|
return $statement->execute($values); |
152
|
1 |
|
} catch (\PDOException $e) { |
153
|
1 |
|
$this->log(Logger::ERROR, $e->getMessage(), array('exception' => $e, 'values' => $values)); |
154
|
1 |
|
throw new Exception($e->getMessage(), (int)$e->getCode(), $e); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Prepare a SQL string to a statement |
160
|
|
|
* @param string $sql |
161
|
|
|
* @param array $options |
162
|
|
|
* @return \PDOStatement |
163
|
|
|
* @throws Exception |
164
|
|
|
*/ |
165
|
3 |
|
public function prepare($sql, array $options = array()) |
166
|
|
|
{ |
167
|
|
|
try { |
168
|
3 |
|
$this->log(Logger::DEBUG, 'Preparing query', array('sql' => $sql, 'options' => $options)); |
169
|
3 |
|
return $this->getDriver()->prepare($sql, $options); |
170
|
1 |
|
} catch (\PDOException $e) { |
171
|
1 |
|
$this->log(Logger::ERROR, $e->getMessage(), array('exception' => $e, 'sql' => $sql)); |
172
|
1 |
|
throw new Exception($e->getMessage(), (int)$e->getCode(), $e); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Retrieve id inserted |
178
|
|
|
* @param string $name optional |
179
|
|
|
* @return string |
180
|
|
|
* @throws Exception |
181
|
|
|
*/ |
182
|
2 |
View Code Duplication |
public function lastInsertId($name = null) |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
try { |
185
|
2 |
|
return $this->getDriver()->lastInsertId($name); |
186
|
1 |
|
} catch (\PDOException $e) { |
187
|
1 |
|
$this->log(Logger::ERROR, $e->getMessage(), array('error' => $e)); |
188
|
1 |
|
throw new Exception($e->getMessage(), (int)$e->getCode(), $e); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Retrieve number of affected rows of a statement |
194
|
|
|
* @param mixed $statement |
195
|
|
|
* @return int |
196
|
|
|
* @throws Exception |
197
|
|
|
*/ |
198
|
|
View Code Duplication |
public function count($statement) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
if (!$statement instanceof \PDOStatement) { |
201
|
|
|
$this->log(Logger::ERROR, 'Statement not in right format', array('statement' => $statement)); |
202
|
|
|
throw new Exception('Statement not in right format'); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
try { |
206
|
|
|
return $statement->rowCount(); |
207
|
|
|
} catch (\PDOException $e) { |
208
|
|
|
$this->log(Logger::ERROR, $e->getMessage(), array('error' => $e)); |
209
|
|
|
throw new Exception($e->getMessage(), (int)$e->getCode(), $e); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.