|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ubiquity\db\providers\pdo; |
|
3
|
|
|
|
|
4
|
|
|
use Ubiquity\db\providers\AbstractDbWrapper; |
|
5
|
|
|
use Ubiquity\exceptions\DBException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Ubiquity\db\providers$PDOWrapper |
|
9
|
|
|
* This class is part of Ubiquity |
|
10
|
|
|
* |
|
11
|
|
|
* @author jcheron <[email protected]> |
|
12
|
|
|
* @version 1.0.3 |
|
13
|
|
|
* @property \PDO $dbInstance |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
class PDOWrapper extends AbstractDbWrapper { |
|
17
|
|
|
|
|
18
|
|
|
protected static $savepointsDrivers = [ |
|
19
|
|
|
'pgsql' => true, |
|
20
|
|
|
'mysql' => true, |
|
21
|
|
|
'sqlite' => true |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
private static $quotes = [ |
|
25
|
|
|
'mysql' => '`', |
|
26
|
|
|
'sqlite' => '"', |
|
27
|
|
|
'pgsql' => '"' |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
30 |
|
protected $driversMetasClasses = [ |
|
31
|
30 |
|
'mysql' => '\\Ubiquity\\db\\providers\\pdo\\drivers\\MysqlDriverMetas', |
|
32
|
30 |
|
'pgsql' => '\\Ubiquity\\db\\providers\\pdo\\drivers\\PgsqlDriverMetas', |
|
33
|
30 |
|
'sqlite' => '\\Ubiquity\\db\\providers\\pdo\\drivers\\SqliteDriverMetas' |
|
34
|
30 |
|
]; |
|
35
|
|
|
|
|
36
|
|
|
protected $transactionLevel = 0; |
|
37
|
|
|
|
|
38
|
|
|
protected $dbType; |
|
39
|
30 |
|
|
|
40
|
|
|
protected $driverMetaDatas; |
|
41
|
|
|
|
|
42
|
81 |
|
/** |
|
43
|
81 |
|
* |
|
44
|
81 |
|
* @throws DBException |
|
45
|
81 |
|
* @return \Ubiquity\db\providers\pdo\drivers\AbstractDriverMetaDatas |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function getDriverMetaDatas() { |
|
48
|
|
|
if (! isset($this->driverMetaDatas)) { |
|
49
|
|
|
if (isset($this->driversMetasClasses[$this->dbType])) { |
|
50
|
|
|
$metaClass = $this->driversMetasClasses[$this->dbType]; |
|
51
|
|
|
$this->driverMetaDatas = new $metaClass($this->dbInstance); |
|
52
|
|
|
} else { |
|
53
|
|
|
throw new DBException("{$this->dbType} driver is not yet implemented!"); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
10 |
|
return $this->driverMetaDatas; |
|
57
|
10 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function __construct($dbType = 'mysql') { |
|
60
|
23 |
|
$this->quote = self::$quotes[$dbType] ?? ''; |
|
61
|
23 |
|
$this->dbType = $dbType; |
|
62
|
23 |
|
} |
|
63
|
23 |
|
|
|
64
|
|
|
public function fetchAllColumn($statement, array $values = null, string $column = null) { |
|
65
|
23 |
|
$result = false; |
|
66
|
23 |
|
if ($statement->execute($values)) { |
|
67
|
|
|
$result = $statement->fetchAll(\PDO::FETCH_COLUMN, $column); |
|
68
|
|
|
} |
|
69
|
|
|
$statement->closeCursor(); |
|
70
|
|
|
return $result; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function lastInsertId($name = null) { |
|
74
|
|
|
return $this->dbInstance->lastInsertId($name); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function fetchAll($statement, array $values = null, $mode = null) { |
|
78
|
2 |
|
if ($statement->execute($values)) { |
|
79
|
2 |
|
$result = $statement->fetchAll($mode ?? \PDO::FETCH_ASSOC); |
|
80
|
|
|
$statement->closeCursor(); |
|
81
|
|
|
return $result; |
|
82
|
7 |
|
} |
|
83
|
7 |
|
|
|
84
|
|
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function fetchOne($statement, array $values = null, $mode = null) { |
|
88
|
|
|
if ($statement->execute($values)) { |
|
89
|
|
|
$result = $statement->fetch($mode ?? \PDO::FETCH_ASSOC); |
|
90
|
|
|
$statement->closeCursor(); |
|
91
|
|
|
return $result; |
|
92
|
|
|
} |
|
93
|
69 |
|
|
|
94
|
69 |
|
return false; |
|
95
|
69 |
|
} |
|
96
|
69 |
|
|
|
97
|
|
|
public static function getAvailableDrivers() { |
|
98
|
|
|
return \PDO::getAvailableDrivers(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function prepareStatement(string $sql) { |
|
102
|
|
|
return $this->dbInstance->prepare($sql); |
|
103
|
76 |
|
} |
|
104
|
76 |
|
|
|
105
|
76 |
|
public function fetchColumn($statement, array $values = null, int $columnNumber = null) { |
|
106
|
76 |
|
if ($statement->execute($values)) { |
|
107
|
|
|
return $statement->fetchColumn($columnNumber); |
|
108
|
76 |
|
} |
|
109
|
76 |
|
return false; |
|
110
|
76 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function getStatement($sql) { |
|
113
|
76 |
|
$st = $this->dbInstance->prepare($sql); |
|
114
|
|
|
$st->setFetchMode(\PDO::FETCH_ASSOC); |
|
115
|
|
|
return $st; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function execute($sql) { |
|
119
|
|
|
return $this->dbInstance->exec($sql); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function connect(string $dbType, $dbName, $serverName, string $port, string $user, string $password, array $options) { |
|
123
|
|
|
$options[\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION; |
|
124
|
|
|
$this->dbInstance = new \PDO($this->getDSN($serverName, $port, $dbName, $dbType), $user, $password, $options); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function getDSN(string $serverName, string $port, string $dbName, string $dbType = 'mysql') { |
|
128
|
4 |
|
$charsetString = [ |
|
129
|
4 |
|
'mysql' => 'charset=UTF8', |
|
130
|
|
|
'pgsql' => 'options=\'--client_encoding=UTF8\'', |
|
131
|
|
|
'sqlite' => '' |
|
132
|
|
|
][$dbType] ?? 'charset=UTF8'; |
|
133
|
|
|
if ($dbType === 'sqlite') { |
|
134
|
|
|
return "sqlite:{$dbName}"; |
|
135
|
|
|
} |
|
136
|
3 |
|
return $dbType . ":dbname={$dbName};host={$serverName};{$charsetString};port=" . $port; |
|
137
|
3 |
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function bindValueFromStatement($statement, $parameter, $value) { |
|
140
|
|
|
return $statement->bindValue(":" . $parameter, $value); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function query(string $sql) { |
|
144
|
|
|
return $this->dbInstance->query($sql); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function queryAll(string $sql, int $fetchStyle = null) { |
|
148
|
4 |
|
return $this->dbInstance->query($sql)->fetchAll($fetchStyle); |
|
149
|
4 |
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function queryColumn(string $sql, int $columnNumber = null) { |
|
152
|
3 |
|
return $this->dbInstance->query($sql)->fetchColumn($columnNumber); |
|
153
|
3 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function executeStatement($statement, array $values = null) { |
|
156
|
8 |
|
return $statement->execute($values); |
|
157
|
8 |
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function getTablesName() { |
|
160
|
4 |
|
return $this->getDriverMetaDatas()->getTablesName(); |
|
161
|
4 |
|
} |
|
162
|
4 |
|
|
|
163
|
|
|
public function statementRowCount($statement) { |
|
164
|
2 |
|
return $statement->rowCount(); |
|
165
|
2 |
|
} |
|
166
|
2 |
|
|
|
167
|
|
|
public function inTransaction() { |
|
168
|
2 |
|
return $this->dbInstance->inTransaction(); |
|
169
|
2 |
|
} |
|
170
|
2 |
|
|
|
171
|
|
|
public function commit() { |
|
172
|
4 |
|
return $this->dbInstance->commit(); |
|
173
|
4 |
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function rollBack() { |
|
176
|
4 |
|
return $this->dbInstance->rollBack(); |
|
177
|
4 |
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function beginTransaction() { |
|
180
|
1 |
|
return $this->dbInstance->beginTransaction(); |
|
181
|
1 |
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function savePoint($level) { |
|
184
|
1 |
|
$this->dbInstance->exec('SAVEPOINT LEVEL' . $level); |
|
185
|
1 |
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function releasePoint($level) { |
|
188
|
1 |
|
$this->dbInstance->exec('RELEASE SAVEPOINT LEVEL' . $level); |
|
189
|
1 |
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function rollbackPoint($level) { |
|
192
|
61 |
|
$this->dbInstance->exec('ROLLBACK TO SAVEPOINT LEVEL' . $level); |
|
193
|
61 |
|
} |
|
194
|
61 |
|
|
|
195
|
61 |
|
public function nestable() { |
|
196
|
61 |
|
return isset(self::$savepointsDrivers[$this->dbType]); |
|
197
|
|
|
} |
|
198
|
61 |
|
|
|
199
|
61 |
|
public function ping() { |
|
200
|
|
|
return ($this->dbInstance != null) && (1 === \intval($this->queryColumn('SELECT 1', 0))); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function getPrimaryKeys($tableName) { |
|
204
|
|
|
return $this->getDriverMetaDatas()->getPrimaryKeys($tableName); |
|
205
|
|
|
} |
|
206
|
3 |
|
|
|
207
|
3 |
|
public function getForeignKeys($tableName, $pkName, $dbName = null) { |
|
208
|
|
|
return $this->getDriverMetaDatas()->getForeignKeys($tableName, $pkName, $dbName); |
|
209
|
|
|
} |
|
210
|
23 |
|
|
|
211
|
23 |
|
public function getFieldsInfos($tableName) { |
|
212
|
|
|
return $this->getDriverMetaDatas()->getFieldsInfos($tableName); |
|
213
|
|
|
} |
|
214
|
24 |
|
|
|
215
|
24 |
|
public function _optPrepareAndExecute($sql, array $values = null, $one = false) { |
|
216
|
|
|
$statement = $this->_getStatement($sql); |
|
217
|
|
|
$result = false; |
|
218
|
|
|
if ($statement->execute($values)) { |
|
219
|
|
|
if ($one) { |
|
220
|
|
|
$result = $statement->fetch(\PDO::FETCH_ASSOC); |
|
221
|
|
|
} else { |
|
222
|
|
|
$result = $statement->fetchAll(\PDO::FETCH_ASSOC); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
$statement->closeCursor(); |
|
226
|
|
|
return $result; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
public function _optExecuteAndFetch($statement, array $values = null, $one = false) { |
|
230
|
|
|
if ($statement->execute($values)) { |
|
231
|
|
|
if ($one) { |
|
232
|
|
|
$row = $statement->fetch(\PDO::FETCH_ASSOC); |
|
233
|
|
|
} else { |
|
234
|
|
|
$row = $statement->fetchAll(\PDO::FETCH_ASSOC); |
|
235
|
|
|
} |
|
236
|
|
|
$statement->closeCursor(); |
|
237
|
|
|
return $row; |
|
238
|
|
|
} |
|
239
|
|
|
return false; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
public function quoteValue($value, $type = 2) { |
|
243
|
|
|
return $this->dbInstance->quote($value, $type); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
public function getRowNum(string $tableName, string $pkName, string $condition): int { |
|
247
|
|
|
return $this->getDriverMetaDatas()->getRowNum($tableName, $pkName, $condition); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
public function groupConcat(string $fields, string $separator): string { |
|
251
|
|
|
return $this->getDriverMetaDatas()->groupConcat($fields, $separator); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
public function toStringOperator() { |
|
255
|
|
|
return $this->getDriverMetaDatas()->toStringOperator(); |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|