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