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 realQuote( |
96
|
|
|
$string, |
97
|
|
|
/*# int */ $type |
98
|
|
|
)/*# : string */ { |
99
|
|
|
return $this->link->quote($string, $type); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritDoc} |
104
|
|
|
*/ |
105
|
|
|
protected function realConnect(array $parameters) |
106
|
|
|
{ |
107
|
|
|
$link = new \PDO( |
108
|
|
|
$parameters['dsn'], |
109
|
|
|
isset($parameters['username']) ? $parameters['username'] : 'root', |
110
|
|
|
isset($parameters['password']) ? $parameters['password'] : null, |
111
|
|
|
isset($parameters['options']) ? $parameters['options'] : null |
112
|
|
|
); |
113
|
|
|
return $link; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Disconnect the \PDO link |
118
|
|
|
* |
119
|
|
|
* {@inheritDoc} |
120
|
|
|
*/ |
121
|
|
|
protected function realDisconnect() |
122
|
|
|
{ |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritDoc} |
127
|
|
|
*/ |
128
|
|
|
protected function realPing()/*# : bool */ |
129
|
|
|
{ |
130
|
|
|
try { |
131
|
|
|
return (bool) $this->link->query('SELECT 1'); |
132
|
|
|
} catch (\Exception $e) { |
133
|
|
|
return $this->setError($e->getMessage(), $e->getCode()); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritDoc} |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
protected function realSetAttribute(/*# string */ $attribute, $value) |
141
|
|
|
{ |
142
|
|
|
if (is_string($attribute)) { |
143
|
|
|
$this->checkAttribute($attribute); |
144
|
|
|
$this->link->setAttribute(constant($attribute), $value); |
145
|
|
|
} else { |
146
|
|
|
$this->link->setAttribute($attribute, $value); |
147
|
|
|
} |
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritDoc} |
153
|
|
|
*/ |
154
|
|
View Code Duplication |
protected function realGetAttribute(/*# string */ $attribute) |
155
|
|
|
{ |
156
|
|
|
if (is_string($attribute)) { |
157
|
|
|
$this->checkAttribute($attribute); |
158
|
|
|
return $this->link->getAttribute(constant($attribute)); |
159
|
|
|
} else { |
160
|
|
|
return $this->link->getAttribute($attribute); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritDoc} |
166
|
|
|
*/ |
167
|
|
|
protected function realBegin() |
168
|
|
|
{ |
169
|
|
|
$this->link->beginTransaction(); |
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritDoc} |
175
|
|
|
*/ |
176
|
|
|
protected function realCommit() |
177
|
|
|
{ |
178
|
|
|
$this->link->commit(); |
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritDoc} |
184
|
|
|
*/ |
185
|
|
|
protected function realRollback() |
186
|
|
|
{ |
187
|
|
|
$this->link->rollBack(); |
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Is attribute defined ? |
193
|
|
|
* |
194
|
|
|
* @param string $attribute |
195
|
|
|
* @throws LogicException |
196
|
|
|
* @access protected |
197
|
|
|
*/ |
198
|
|
|
protected function checkAttribute(/*# string */ $attribute) |
199
|
|
|
{ |
200
|
|
|
if (!defined($attribute)) { |
201
|
|
|
throw new LogicException( |
202
|
|
|
Message::get(Message::DB_ATTRIBUTE_UNKNOWN, $attribute), |
203
|
|
|
Message::DB_ATTRIBUTE_UNKNOWN |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|