Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
PDOWrapper.php
Go to the documentation of this file.
1<?php
2
4
7
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;
24
30 protected function getDriverMetaDatas() {
31 if (! isset ( $this->driverMetaDatas )) {
32 if (isset ( $this->driversMetasClasses [$this->dbType] )) {
33 $metaClass = $this->driversMetasClasses [$this->dbType];
34 $this->driverMetaDatas = new $metaClass ( $this->dbInstance );
35 } else {
36 throw new DBException ( "{$this->dbType} driver is not yet implemented!" );
37 }
38 }
40 }
41
42 public function __construct($dbType = 'mysql') {
43 $this->quote = self::$quotes [$dbType] ?? '';
44 $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 public function lastInsertId($name = null) {
57 return $this->dbInstance->lastInsertId ( $name );
58 }
59
60 public function fetchAll($statement, array $values = null, $mode = null) {
61 if ($statement->execute ( $values )) {
62 $result = $statement->fetchAll ( $mode ?? \PDO::FETCH_ASSOC);
63 $statement->closeCursor ();
64 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 public static function getAvailableDrivers() {
81 return \PDO::getAvailableDrivers ();
82 }
83
84 public function prepareStatement(string $sql) {
85 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 public function getStatement($sql) {
96 $st = $this->dbInstance->prepare ( $sql );
97 $st->setFetchMode ( \PDO::FETCH_ASSOC );
98 return $st;
99 }
100
101 public function execute($sql) {
102 return $this->dbInstance->exec ( $sql );
103 }
104
105 public function connect(string $dbType, $dbName, $serverName, string $port, string $user, string $password, array $options) {
106 $options [\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION;
107 if (isset ( $options ['quote'] )) {
108 $this->quote = $options ['quote'];
109 unset ( $options ['quote'] );
110 }
111 $this->dbInstance = new \PDO ( $this->getDSN ( $serverName, $port, $dbName, $dbType ), $user, $password, $options );
112 }
113
114 public function getDSN(string $serverName, string $port, string $dbName, string $dbType = 'mysql') {
115 $charsetString = [ 'mysql' => 'charset=UTF8','pgsql' => 'options=\'--client_encoding=UTF8\'','sqlite' => '' ] [$dbType] ?? 'charset=UTF8';
116 if ($dbType === 'sqlite') {
117 return "sqlite:{$dbName}";
118 }
119 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 public function queryColumn(string $sql, int $columnNumber = null) {
135 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 public function getTablesName() {
143 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 public function commit() {
155 if($this->dbInstance->inTransaction()) {
156 return $this->dbInstance->commit();
157 }
158 return false;
159 }
160
161 public function rollBack() {
162 if($this->dbInstance->inTransaction()) {
163 return $this->dbInstance->rollBack ();
164 }
165 return false;
166 }
167
168 public function beginTransaction() {
169 return $this->dbInstance->beginTransaction ();
170 }
171
172 public function savePoint($level) {
173 $this->dbInstance->exec ( 'SAVEPOINT LEVEL' . $level );
174 }
175
176 public function releasePoint($level) {
177 $this->dbInstance->exec ( 'RELEASE SAVEPOINT LEVEL' . $level );
178 }
179
180 public function rollbackPoint($level) {
181 $this->dbInstance->exec ( 'ROLLBACK TO SAVEPOINT LEVEL' . $level );
182 }
183
184 public function nestable() {
185 return isset ( self::$savepointsDrivers [$this->dbType] );
186 }
187
188 public function ping() {
189 return ($this->dbInstance != null) && (1 === \intval ( $this->queryColumn ( 'SELECT 1', 0 ) ));
190 }
191
192 public function getPrimaryKeys($tableName) {
193 return $this->getDriverMetaDatas ()->getPrimaryKeys ( $tableName );
194 }
195
196 public function getForeignKeys($tableName, $pkName, $dbName = null) {
197 return $this->getDriverMetaDatas ()->getForeignKeys ( $tableName, $pkName, $dbName );
198 }
199
200 public function getFieldsInfos($tableName) {
201 return $this->getDriverMetaDatas ()->getFieldsInfos ( $tableName );
202 }
203
204 public function _optPrepareAndExecute($sql, array $values = null, $one = false) {
205 $statement = $this->_getStatement ( $sql );
206 $result = false;
207 if ($statement->execute ( $values )) {
208 if ($one) {
209 $result = $statement->fetch ( \PDO::FETCH_ASSOC );
210 } else {
211 $result = $statement->fetchAll ( \PDO::FETCH_ASSOC );
212 }
213 }
214 $statement->closeCursor ();
215 return $result;
216 }
217
218 public function _optExecuteAndFetch($statement, array $values = null, $one = false) {
219 if ($statement->execute ( $values )) {
220 if ($one) {
221 $row = $statement->fetch ( \PDO::FETCH_ASSOC );
222 } else {
223 $row = $statement->fetchAll ( \PDO::FETCH_ASSOC );
224 }
225 $statement->closeCursor ();
226 return $row;
227 }
228 return false;
229 }
230
231 public function quoteValue($value, $type = 2) {
232 return $this->dbInstance->quote ( $value, $type );
233 }
234
235 public function getRowNum(string $tableName, string $pkName, string $condition): int {
236 return $this->getDriverMetaDatas ()->getRowNum ( $tableName, $pkName, $condition );
237 }
238
239 public function groupConcat(string $fields, string $separator): string {
240 return $this->getDriverMetaDatas ()->groupConcat ( $fields, $separator );
241 }
242
243 public function toStringOperator() {
244 return $this->getDriverMetaDatas ()->toStringOperator ();
245 }
246
252 public function migrateOperation(string $operation):?string{
253 return $this->getDriverMetaDatas()->migrateOperation($operation)??parent::migrateOperation($operation);
254 }
255
262 public function setIsolationLevel($isolationLevel) {
263 return $this->getDriverMetaDatas()->setIsolationLevel($isolationLevel);
264 }
265}
Ubiquity\db\providers$AbstractDbWrapper This class is part of Ubiquity.
executeStatement($statement, array $values=null)
fetchColumn($statement, array $values=null, int $columnNumber=null)
getDSN(string $serverName, string $port, string $dbName, string $dbType='mysql')
setIsolationLevel($isolationLevel)
Sets the isolation level for transactions.
migrateOperation(string $operation)
Returns the SQL string for a migration operation.
getForeignKeys($tableName, $pkName, $dbName=null)
_optPrepareAndExecute($sql, array $values=null, $one=false)
groupConcat(string $fields, string $separator)
connect(string $dbType, $dbName, $serverName, string $port, string $user, string $password, array $options)
bindValueFromStatement($statement, $parameter, $value)
Used by DAO.
queryColumn(string $sql, int $columnNumber=null)
fetchAll($statement, array $values=null, $mode=null)
getRowNum(string $tableName, string $pkName, string $condition)
fetchAllColumn($statement, array $values=null, string $column=null)
fetchOne($statement, array $values=null, $mode=null)
_optExecuteAndFetch($statement, array $values=null, $one=false)
queryAll(string $sql, int $fetchStyle=null)