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' ];
31 if (! isset ( $this->driverMetaDatas )) {
32 if (isset ( $this->driversMetasClasses [$this->dbType] )) {
34 $this->driverMetaDatas =
new $metaClass ( $this->dbInstance );
36 throw new DBException (
"{$this->dbType} driver is not yet implemented!" );
43 $this->quote = self::$quotes [
$dbType] ??
'';
47 public function fetchAllColumn($statement, array $values =
null,
string $column =
null) {
49 if ($statement->execute ( $values )) {
50 $result = $statement->fetchAll ( \PDO::FETCH_COLUMN, $column );
52 $statement->closeCursor ();
57 return $this->dbInstance->lastInsertId ( $name );
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 ();
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 ();
81 return \PDO::getAvailableDrivers ();
85 return $this->dbInstance->prepare ( $sql );
88 public function fetchColumn($statement, array $values =
null,
int $columnNumber =
null) {
89 if ($statement->execute ( $values )) {
90 return $statement->fetchColumn ( $columnNumber );
96 $st = $this->dbInstance->prepare ( $sql );
97 $st->setFetchMode ( \PDO::FETCH_ASSOC );
102 return $this->dbInstance->exec ( $sql );
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'] );
111 $this->dbInstance = new \PDO ( $this->
getDSN ( $serverName, $port, $dbName,
$dbType ), $user, $password, $options );
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';
117 return "sqlite:{$dbName}";
119 return $dbType .
":dbname={$dbName};host={$serverName};{$charsetString};port=" . $port;
123 return $statement->bindValue (
":" . $parameter, $value );
126 public function query(
string $sql) {
127 return $this->dbInstance->query ( $sql );
130 public function queryAll(
string $sql,
int $fetchStyle =
null) {
131 return $this->dbInstance->query ( $sql )->fetchAll ( $fetchStyle );
134 public function queryColumn(
string $sql,
int $columnNumber =
null) {
135 return $this->dbInstance->query ( $sql )->fetchColumn ( $columnNumber );
139 return $statement->execute ( $values );
147 return $statement->rowCount ();
151 return $this->dbInstance->inTransaction ();
155 if($this->dbInstance->inTransaction()) {
156 return $this->dbInstance->commit();
162 if($this->dbInstance->inTransaction()) {
163 return $this->dbInstance->rollBack ();
169 return $this->dbInstance->beginTransaction ();
173 $this->dbInstance->exec (
'SAVEPOINT LEVEL' . $level );
177 $this->dbInstance->exec (
'RELEASE SAVEPOINT LEVEL' . $level );
181 $this->dbInstance->exec (
'ROLLBACK TO SAVEPOINT LEVEL' . $level );
185 return isset ( self::$savepointsDrivers [$this->dbType] );
189 return ($this->dbInstance !=
null) && (1 === \intval ( $this->
queryColumn (
'SELECT 1', 0 ) ));
207 if ($statement->execute ( $values )) {
209 $result = $statement->fetch ( \PDO::FETCH_ASSOC );
211 $result = $statement->fetchAll ( \PDO::FETCH_ASSOC );
214 $statement->closeCursor ();
219 if ($statement->execute ( $values )) {
221 $row = $statement->fetch ( \PDO::FETCH_ASSOC );
223 $row = $statement->fetchAll ( \PDO::FETCH_ASSOC );
225 $statement->closeCursor ();
232 return $this->dbInstance->quote ( $value, $type );
235 public function getRowNum(
string $tableName,
string $pkName,
string $condition): int {
239 public function groupConcat(
string $fields,
string $separator): string {
240 return $this->getDriverMetaDatas ()->groupConcat ( $fields, $separator );
244 return $this->getDriverMetaDatas ()->toStringOperator ();
253 return $this->getDriverMetaDatas()->migrateOperation($operation)??parent::migrateOperation($operation);
263 return $this->getDriverMetaDatas()->setIsolationLevel($isolationLevel);
Ubiquity\db\providers$AbstractDbWrapper This class is part of Ubiquity.
_getStatement(string $sql)
executeStatement($statement, array $values=null)
fetchColumn($statement, array $values=null, int $columnNumber=null)
getDSN(string $serverName, string $port, string $dbName, string $dbType='mysql')
static $savepointsDrivers
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)
prepareStatement(string $sql)
fetchAll($statement, array $values=null, $mode=null)
getRowNum(string $tableName, string $pkName, string $condition)
getFieldsInfos($tableName)
statementRowCount($statement)
quoteValue($value, $type=2)
fetchAllColumn($statement, array $values=null, string $column=null)
__construct($dbType='mysql')
getPrimaryKeys($tableName)
static getAvailableDrivers()
fetchOne($statement, array $values=null, $mode=null)
_optExecuteAndFetch($statement, array $values=null, $one=false)
queryAll(string $sql, int $fetchStyle=null)