New function to fetch the database driver

This commit is contained in:
Michael 2020-11-07 09:02:02 +00:00
parent 43749c3069
commit 3a5cdecb62
2 changed files with 56 additions and 33 deletions

View file

@ -72,6 +72,16 @@ class DBA
return DI::dba()->getConnection(); return DI::dba()->getConnection();
} }
/**
* Return the database driver string
*
* @return string with either "pdo" or "mysqli"
*/
public static function getDriver()
{
return DI::dba()->getDriver();
}
/** /**
* Returns the MySQL server version string * Returns the MySQL server version string
* *

View file

@ -39,6 +39,9 @@ use Psr\Log\LoggerInterface;
*/ */
class Database class Database
{ {
const PDO = 'pdo';
const MYSQLI = 'mysqli';
protected $connected = false; protected $connected = false;
/** /**
@ -119,7 +122,7 @@ class Database
$this->pdo_emulate_prepares = (bool)$this->configCache->get('database', 'pdo_emulate_prepares'); $this->pdo_emulate_prepares = (bool)$this->configCache->get('database', 'pdo_emulate_prepares');
if (!$this->configCache->get('database', 'disable_pdo') && class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) { if (!$this->configCache->get('database', 'disable_pdo') && class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
$this->driver = 'pdo'; $this->driver = self::PDO;
$connect = "mysql:host=" . $server . ";dbname=" . $db; $connect = "mysql:host=" . $server . ";dbname=" . $db;
if ($port > 0) { if ($port > 0) {
@ -140,7 +143,7 @@ class Database
} }
if (!$this->connected && class_exists('\mysqli')) { if (!$this->connected && class_exists('\mysqli')) {
$this->driver = 'mysqli'; $this->driver = self::MYSQLI;
if ($port > 0) { if ($port > 0) {
$this->connection = @new mysqli($server, $user, $pass, $db, $port); $this->connection = @new mysqli($server, $user, $pass, $db, $port);
@ -201,10 +204,10 @@ class Database
{ {
if (!is_null($this->connection)) { if (!is_null($this->connection)) {
switch ($this->driver) { switch ($this->driver) {
case 'pdo': case self::PDO:
$this->connection = null; $this->connection = null;
break; break;
case 'mysqli': case self::MYSQLI:
$this->connection->close(); $this->connection->close();
$this->connection = null; $this->connection = null;
break; break;
@ -234,6 +237,16 @@ class Database
return $this->connection; return $this->connection;
} }
/**
* Return the database driver string
*
* @return string with either "pdo" or "mysqli"
*/
public function getDriver()
{
return $this->driver;
}
/** /**
* Returns the MySQL server version string * Returns the MySQL server version string
* *
@ -246,10 +259,10 @@ class Database
{ {
if ($this->server_info == '') { if ($this->server_info == '') {
switch ($this->driver) { switch ($this->driver) {
case 'pdo': case self::PDO:
$this->server_info = $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION); $this->server_info = $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
break; break;
case 'mysqli': case self::MYSQLI:
$this->server_info = $this->connection->server_info; $this->server_info = $this->connection->server_info;
break; break;
} }
@ -346,10 +359,10 @@ class Database
{ {
if ($this->connected) { if ($this->connected) {
switch ($this->driver) { switch ($this->driver) {
case 'pdo': case self::PDO:
return substr(@$this->connection->quote($str, PDO::PARAM_STR), 1, -1); return substr(@$this->connection->quote($str, PDO::PARAM_STR), 1, -1);
case 'mysqli': case self::MYSQLI:
return @$this->connection->real_escape_string($str); return @$this->connection->real_escape_string($str);
} }
} else { } else {
@ -371,14 +384,14 @@ class Database
} }
switch ($this->driver) { switch ($this->driver) {
case 'pdo': case self::PDO:
$r = $this->p("SELECT 1"); $r = $this->p("SELECT 1");
if ($this->isResult($r)) { if ($this->isResult($r)) {
$row = $this->toArray($r); $row = $this->toArray($r);
$connected = ($row[0]['1'] == '1'); $connected = ($row[0]['1'] == '1');
} }
break; break;
case 'mysqli': case self::MYSQLI:
$connected = $this->connection->ping(); $connected = $this->connection->ping();
break; break;
} }
@ -508,7 +521,7 @@ class Database
} }
switch ($this->driver) { switch ($this->driver) {
case 'pdo': case self::PDO:
// If there are no arguments we use "query" // If there are no arguments we use "query"
if ($this->emulate_prepares || count($args) == 0) { if ($this->emulate_prepares || count($args) == 0) {
if (!$retval = $this->connection->query($this->replaceParameters($sql, $args))) { if (!$retval = $this->connection->query($this->replaceParameters($sql, $args))) {
@ -553,7 +566,7 @@ class Database
$this->affected_rows = $retval->rowCount(); $this->affected_rows = $retval->rowCount();
} }
break; break;
case 'mysqli': case self::MYSQLI:
// There are SQL statements that cannot be executed with a prepared statement // There are SQL statements that cannot be executed with a prepared statement
$parts = explode(' ', $orig_sql); $parts = explode(' ', $orig_sql);
$command = strtolower($parts[0]); $command = strtolower($parts[0]);
@ -861,9 +874,9 @@ class Database
return 0; return 0;
} }
switch ($this->driver) { switch ($this->driver) {
case 'pdo': case self::PDO:
return $stmt->columnCount(); return $stmt->columnCount();
case 'mysqli': case self::MYSQLI:
return $stmt->field_count; return $stmt->field_count;
} }
return 0; return 0;
@ -882,9 +895,9 @@ class Database
return 0; return 0;
} }
switch ($this->driver) { switch ($this->driver) {
case 'pdo': case self::PDO:
return $stmt->rowCount(); return $stmt->rowCount();
case 'mysqli': case self::MYSQLI:
return $stmt->num_rows; return $stmt->num_rows;
} }
return 0; return 0;
@ -909,10 +922,10 @@ class Database
} }
switch ($this->driver) { switch ($this->driver) {
case 'pdo': case self::PDO:
$columns = $stmt->fetch(PDO::FETCH_ASSOC); $columns = $stmt->fetch(PDO::FETCH_ASSOC);
break; break;
case 'mysqli': case self::MYSQLI:
if (get_class($stmt) == 'mysqli_result') { if (get_class($stmt) == 'mysqli_result') {
$columns = $stmt->fetch_assoc(); $columns = $stmt->fetch_assoc();
break; break;
@ -1023,10 +1036,10 @@ class Database
public function lastInsertId() public function lastInsertId()
{ {
switch ($this->driver) { switch ($this->driver) {
case 'pdo': case self::PDO:
$id = $this->connection->lastInsertId(); $id = $this->connection->lastInsertId();
break; break;
case 'mysqli': case self::MYSQLI:
$id = $this->connection->insert_id; $id = $this->connection->insert_id;
break; break;
} }
@ -1046,7 +1059,7 @@ class Database
public function lock($table) public function lock($table)
{ {
// See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
if ($this->driver == 'pdo') { if ($this->driver == self::PDO) {
$this->e("SET autocommit=0"); $this->e("SET autocommit=0");
$this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
} else { } else {
@ -1055,12 +1068,12 @@ class Database
$success = $this->e("LOCK TABLES " . DBA::buildTableString($table) . " WRITE"); $success = $this->e("LOCK TABLES " . DBA::buildTableString($table) . " WRITE");
if ($this->driver == 'pdo') { if ($this->driver == self::PDO) {
$this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares); $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares);
} }
if (!$success) { if (!$success) {
if ($this->driver == 'pdo') { if ($this->driver == self::PDO) {
$this->e("SET autocommit=1"); $this->e("SET autocommit=1");
} else { } else {
$this->connection->autocommit(true); $this->connection->autocommit(true);
@ -1082,13 +1095,13 @@ class Database
// See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
$this->performCommit(); $this->performCommit();
if ($this->driver == 'pdo') { if ($this->driver == self::PDO) {
$this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
} }
$success = $this->e("UNLOCK TABLES"); $success = $this->e("UNLOCK TABLES");
if ($this->driver == 'pdo') { if ($this->driver == self::PDO) {
$this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares); $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares);
$this->e("SET autocommit=1"); $this->e("SET autocommit=1");
} else { } else {
@ -1111,13 +1124,13 @@ class Database
} }
switch ($this->driver) { switch ($this->driver) {
case 'pdo': case self::PDO:
if (!$this->connection->inTransaction() && !$this->connection->beginTransaction()) { if (!$this->connection->inTransaction() && !$this->connection->beginTransaction()) {
return false; return false;
} }
break; break;
case 'mysqli': case self::MYSQLI:
if (!$this->connection->begin_transaction()) { if (!$this->connection->begin_transaction()) {
return false; return false;
} }
@ -1131,14 +1144,14 @@ class Database
protected function performCommit() protected function performCommit()
{ {
switch ($this->driver) { switch ($this->driver) {
case 'pdo': case self::PDO:
if (!$this->connection->inTransaction()) { if (!$this->connection->inTransaction()) {
return true; return true;
} }
return $this->connection->commit(); return $this->connection->commit();
case 'mysqli': case self::MYSQLI:
return $this->connection->commit(); return $this->connection->commit();
} }
@ -1169,7 +1182,7 @@ class Database
$ret = false; $ret = false;
switch ($this->driver) { switch ($this->driver) {
case 'pdo': case self::PDO:
if (!$this->connection->inTransaction()) { if (!$this->connection->inTransaction()) {
$ret = true; $ret = true;
break; break;
@ -1177,7 +1190,7 @@ class Database
$ret = $this->connection->rollBack(); $ret = $this->connection->rollBack();
break; break;
case 'mysqli': case self::MYSQLI:
$ret = $this->connection->rollback(); $ret = $this->connection->rollback();
break; break;
} }
@ -1634,10 +1647,10 @@ class Database
} }
switch ($this->driver) { switch ($this->driver) {
case 'pdo': case self::PDO:
$ret = $stmt->closeCursor(); $ret = $stmt->closeCursor();
break; break;
case 'mysqli': case self::MYSQLI:
// MySQLi offers both a mysqli_stmt and a mysqli_result class. // MySQLi offers both a mysqli_stmt and a mysqli_result class.
// We should be careful not to assume the object type of $stmt // We should be careful not to assume the object type of $stmt
// because DBA::p() has been able to return both types. // because DBA::p() has been able to return both types.