*/
class Database
{
+ const PDO = 'pdo';
+ const MYSQLI = 'mysqli';
+
protected $connected = false;
/**
$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())) {
- $this->driver = 'pdo';
+ $this->driver = self::PDO;
$connect = "mysql:host=" . $server . ";dbname=" . $db;
if ($port > 0) {
}
if (!$this->connected && class_exists('\mysqli')) {
- $this->driver = 'mysqli';
+ $this->driver = self::MYSQLI;
if ($port > 0) {
$this->connection = @new mysqli($server, $user, $pass, $db, $port);
{
if (!is_null($this->connection)) {
switch ($this->driver) {
- case 'pdo':
+ case self::PDO:
$this->connection = null;
break;
- case 'mysqli':
+ case self::MYSQLI:
$this->connection->close();
$this->connection = null;
break;
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
*
{
if ($this->server_info == '') {
switch ($this->driver) {
- case 'pdo':
+ case self::PDO:
$this->server_info = $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
break;
- case 'mysqli':
+ case self::MYSQLI:
$this->server_info = $this->connection->server_info;
break;
}
{
if ($this->connected) {
switch ($this->driver) {
- case 'pdo':
+ case self::PDO:
return substr(@$this->connection->quote($str, PDO::PARAM_STR), 1, -1);
- case 'mysqli':
+ case self::MYSQLI:
return @$this->connection->real_escape_string($str);
}
} else {
}
switch ($this->driver) {
- case 'pdo':
+ case self::PDO:
$r = $this->p("SELECT 1");
if ($this->isResult($r)) {
$row = $this->toArray($r);
$connected = ($row[0]['1'] == '1');
}
break;
- case 'mysqli':
+ case self::MYSQLI:
$connected = $this->connection->ping();
break;
}
}
switch ($this->driver) {
- case 'pdo':
+ case self::PDO:
// If there are no arguments we use "query"
if ($this->emulate_prepares || count($args) == 0) {
if (!$retval = $this->connection->query($this->replaceParameters($sql, $args))) {
$this->affected_rows = $retval->rowCount();
}
break;
- case 'mysqli':
+ case self::MYSQLI:
// There are SQL statements that cannot be executed with a prepared statement
$parts = explode(' ', $orig_sql);
$command = strtolower($parts[0]);
return 0;
}
switch ($this->driver) {
- case 'pdo':
+ case self::PDO:
return $stmt->columnCount();
- case 'mysqli':
+ case self::MYSQLI:
return $stmt->field_count;
}
return 0;
return 0;
}
switch ($this->driver) {
- case 'pdo':
+ case self::PDO:
return $stmt->rowCount();
- case 'mysqli':
+ case self::MYSQLI:
return $stmt->num_rows;
}
return 0;
}
switch ($this->driver) {
- case 'pdo':
+ case self::PDO:
$columns = $stmt->fetch(PDO::FETCH_ASSOC);
break;
- case 'mysqli':
+ case self::MYSQLI:
if (get_class($stmt) == 'mysqli_result') {
$columns = $stmt->fetch_assoc();
break;
public function lastInsertId()
{
switch ($this->driver) {
- case 'pdo':
+ case self::PDO:
$id = $this->connection->lastInsertId();
break;
- case 'mysqli':
+ case self::MYSQLI:
$id = $this->connection->insert_id;
break;
}
public function lock($table)
{
// 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->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
} else {
$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);
}
if (!$success) {
- if ($this->driver == 'pdo') {
+ if ($this->driver == self::PDO) {
$this->e("SET autocommit=1");
} else {
$this->connection->autocommit(true);
// See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
$this->performCommit();
- if ($this->driver == 'pdo') {
+ if ($this->driver == self::PDO) {
$this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
}
$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->e("SET autocommit=1");
} else {
}
switch ($this->driver) {
- case 'pdo':
+ case self::PDO:
if (!$this->connection->inTransaction() && !$this->connection->beginTransaction()) {
return false;
}
break;
- case 'mysqli':
+ case self::MYSQLI:
if (!$this->connection->begin_transaction()) {
return false;
}
protected function performCommit()
{
switch ($this->driver) {
- case 'pdo':
+ case self::PDO:
if (!$this->connection->inTransaction()) {
return true;
}
return $this->connection->commit();
- case 'mysqli':
+ case self::MYSQLI:
return $this->connection->commit();
}
$ret = false;
switch ($this->driver) {
- case 'pdo':
+ case self::PDO:
if (!$this->connection->inTransaction()) {
$ret = true;
break;
$ret = $this->connection->rollBack();
break;
- case 'mysqli':
+ case self::MYSQLI:
$ret = $this->connection->rollback();
break;
}
}
switch ($this->driver) {
- case 'pdo':
+ case self::PDO:
$ret = $stmt->closeCursor();
break;
- case 'mysqli':
+ case self::MYSQLI:
// MySQLi offers both a mysqli_stmt and a mysqli_result class.
// We should be careful not to assume the object type of $stmt
// because DBA::p() has been able to return both types.