1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Db |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Db\Driver\Pdo; |
16
|
|
|
|
17
|
|
|
use Phossa2\Db\Message\Message; |
18
|
|
|
use Phossa2\Db\Driver\DriverAbstract; |
19
|
|
|
use Phossa2\Db\Exception\LogicException; |
20
|
|
|
use Phossa2\Db\Interfaces\StatementInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Driver |
24
|
|
|
* |
25
|
|
|
* PDO driver |
26
|
|
|
* |
27
|
|
|
* @package Phossa2\Db |
28
|
|
|
* @author Hong Zhang <[email protected]> |
29
|
|
|
* @see DriverAbstract |
30
|
|
|
* @version 2.0.0 |
31
|
|
|
* @since 2.0.0 added |
32
|
|
|
*/ |
33
|
|
|
class Driver extends DriverAbstract |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* the connection link |
37
|
|
|
* |
38
|
|
|
* @var \PDO |
39
|
|
|
* @access protected |
40
|
|
|
*/ |
41
|
|
|
protected $link; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Default PDO attributes |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
* @access protected |
48
|
|
|
*/ |
49
|
|
|
protected $attributes = [ |
50
|
|
|
'PDO::ATTR_ERRMODE' => \PDO::ERRMODE_SILENT, |
51
|
|
|
'PDO::ATTR_CASE' => \PDO::CASE_NATURAL, |
52
|
|
|
'PDO::ATTR_ORACLE_NULLS' => \PDO::NULL_NATURAL, |
53
|
|
|
'PDO::ATTR_DEFAULT_FETCH_MODE' => \PDO::FETCH_ASSOC, |
54
|
|
|
'PDO::ATTR_EMULATE_PREPARES' => false, |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Driver constructor |
59
|
|
|
* |
60
|
|
|
* @param array $connectInfo |
61
|
|
|
* @param StatementInterface $statementPrototype |
62
|
|
|
* @throws InvalidArgumentException if link type not right |
63
|
|
|
* @throws LogicException driver specific extension not loaded |
64
|
|
|
* @access public |
65
|
|
|
*/ |
66
|
|
|
public function __construct( |
67
|
|
|
$connectInfo, |
68
|
|
|
StatementInterface $statementPrototype = null |
69
|
|
|
) { |
70
|
|
|
parent::__construct($connectInfo); |
71
|
|
|
|
72
|
|
|
// set prototypes |
73
|
|
|
$this->statement_prototype = $statementPrototype ?: new Statement(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritDoc} |
78
|
|
|
*/ |
79
|
|
|
protected function extensionLoaded()/*# : bool */ |
80
|
|
|
{ |
81
|
|
|
return extension_loaded('PDO'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
|
|
protected function realLastId($name) |
88
|
|
|
{ |
89
|
|
|
return $this->link->lastInsertId($name); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
|
|
protected function realConnect(array $parameters) |
96
|
|
|
{ |
97
|
|
|
$this->link = new \PDO( |
98
|
|
|
$parameters['dsn'], |
99
|
|
|
isset($parameters['username']) ? $parameters['username'] : 'root', |
100
|
|
|
isset($parameters['password']) ? $parameters['password'] : null, |
101
|
|
|
isset($parameters['options']) ? $parameters['options'] : null |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
// set default attributes |
105
|
|
|
$this->setDefaultAttributes(); |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Disconnect the \PDO link |
112
|
|
|
* |
113
|
|
|
* {@inheritDoc} |
114
|
|
|
*/ |
115
|
|
|
protected function realDisconnect() |
116
|
|
|
{ |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritDoc} |
121
|
|
|
*/ |
122
|
|
|
protected function realPing()/*# : bool */ |
123
|
|
|
{ |
124
|
|
|
try { |
125
|
|
|
return (bool) $this->link->query('SELECT 1'); |
126
|
|
|
} catch (\Exception $e) { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritDoc} |
133
|
|
|
*/ |
134
|
|
View Code Duplication |
protected function realSetAttribute(/*# string */ $attribute, $value) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
if (is_string($attribute)) { |
137
|
|
|
$this->checkAttribute($attribute); |
138
|
|
|
$this->link->setAttribute(constant($attribute), $value); |
139
|
|
|
} else { |
140
|
|
|
$this->link->setAttribute($attribute, $value); |
141
|
|
|
} |
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritDoc} |
147
|
|
|
*/ |
148
|
|
View Code Duplication |
protected function realGetAttribute(/*# string */ $attribute) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
if (is_string($attribute)) { |
151
|
|
|
$this->checkAttribute($attribute); |
152
|
|
|
return $this->link->getAttribute(constant($attribute)); |
153
|
|
|
} else { |
154
|
|
|
return $this->link->getAttribute($attribute); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritDoc} |
160
|
|
|
*/ |
161
|
|
|
protected function realBegin() |
162
|
|
|
{ |
163
|
|
|
$this->link->beginTransaction(); |
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritDoc} |
169
|
|
|
*/ |
170
|
|
|
protected function realCommit() |
171
|
|
|
{ |
172
|
|
|
$this->link->commit(); |
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritDoc} |
178
|
|
|
*/ |
179
|
|
|
protected function realRollback() |
180
|
|
|
{ |
181
|
|
|
$this->link->rollBack(); |
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Set default attributes |
187
|
|
|
* |
188
|
|
|
* @access protected |
189
|
|
|
*/ |
190
|
|
|
protected function setDefaultAttributes() |
191
|
|
|
{ |
192
|
|
|
if (!empty($this->attributes)) { |
193
|
|
|
foreach ($this->attributes as $attr => $val) { |
194
|
|
|
$this->realSetAttribute($attr, $val); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Is attribute defined ? |
201
|
|
|
* |
202
|
|
|
* @param string $attribute |
203
|
|
|
* @throws LogicException |
204
|
|
|
* @access protected |
205
|
|
|
*/ |
206
|
|
|
protected function checkAttribute(/*# string */ $attribute) |
207
|
|
|
{ |
208
|
|
|
if (!defined($attribute)) { |
209
|
|
|
throw new LogicException( |
210
|
|
|
Message::get(Message::DB_ATTRIBUTE_UNKNOWN, $attribute), |
211
|
|
|
Message::DB_ATTRIBUTE_UNKNOWN |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
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.