1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-16 11:34:10 +02:00

Use common parent for Db

This commit is contained in:
Jakub Vrana
2025-03-27 15:28:14 +01:00
parent 0578b5c490
commit e2deed9a02
18 changed files with 167 additions and 298 deletions

View File

@@ -70,6 +70,7 @@ if (function_exists("get_magic_quotes_runtime") && get_magic_quotes_runtime()) {
include "../adminer/include/lang.inc.php";
include "../adminer/lang/$LANG.inc.php";
include "../adminer/include/db.inc.php";
include "../adminer/include/pdo.inc.php";
include "../adminer/include/driver.inc.php";
include "../adminer/drivers/sqlite.inc.php";

View File

@@ -0,0 +1,78 @@
<?php
namespace Adminer;
// this could be interface when "Db extends \mysqli" can have compatible type declarations (PHP 7)
// interfaces can include properties only since PHP 8.4
abstract class SqlDb {
/** @var string */ public $extension; // extension name
/** @var string */ public $flavor; // different vendor with the same API, e.g. MariaDB; usually stays empty
/** @var string */ public $server_info; // server version
/** @var int */ public $affected_rows; // number of affected rows
/** @var string */ public $info; // see https://php.net/mysql_info
/** @var int */ public $errno; // last error code
/** @var string */ public $error; // last error message
/** @var Result|bool */ protected $multi; // used for multiquery
/** Connect to server
* @param string
* @param string
* @param string
* @return bool
*/
abstract function connect($server, $username, $password);
/** Quote string to use in SQL
* @param string
* @return string escaped string enclosed in '
*/
abstract function quote($string);
/** Select database
* @param string
* @return bool
*/
abstract function select_db($database);
/** Send query
* @param string
* @param bool
* @return Result|bool
*/
abstract function query($query, $unbuffered = false);
/** Send query with more resultsets
* @param string
* @return Result|bool
*/
function multi_query($query) {
return $this->multi = $this->query($query);
}
/** Get current resultset
* @return Result|bool
*/
function store_result() {
return $this->multi;
}
/** Fetch next resultset
* @return bool
*/
function next_result() {
return false;
}
/** Get single field from result
* @param string
* @param int
* @return string|bool
*/
function result($query, $field = 0) {
$result = $this->query($query);
if (!is_object($result)) {
return false;
}
$row = $result->fetch_row();
return ($row ? $row[$field] : false);
}
}

View File

@@ -79,7 +79,7 @@ abstract class SqlDriver {
* @param list<string> result of $adminer->selectSearchProcess()
* @param list<string> result of $adminer->selectColumnsProcess()[1]
* @param list<string> result of $adminer->selectOrderProcess()
* @param int result of $adminer->selectLimitProcess()
* @param int|numeric-string result of $adminer->selectLimitProcess()
* @param int index of page starting at zero
* @param bool whether to print the query
* @return Result|false

View File

@@ -64,8 +64,8 @@ function get_lang() {
* @param float|string
* @return string
*/
// this is matched by compile.php
function lang($idf, $number = null) {
// this is matched by compile.php
global $LANG, $translations;
$translation = ($translations[$idf] ?: $idf);
if (is_array($translation)) {

View File

@@ -3,14 +3,19 @@ namespace Adminer;
// PDO can be used in several database drivers
if (extension_loaded('pdo')) {
abstract class PdoDb {
public $flavor = '', $server_info, $affected_rows, $errno, $error;
protected $pdo;
private $result;
abstract class PdoDb extends SqlDb {
/** @var \PDO */ protected $pdo;
/** Connect to server using DSN
* @param string
* @param string
* @param string
* @param mixed[]
* @return void
*/
function dsn($dsn, $username, $password, $options = array()) {
$options[\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_SILENT;
$options[\PDO::ATTR_STATEMENT_CLASS] = array('Adminer\PdoDbStatement');
$options[\PDO::ATTR_STATEMENT_CLASS] = array('Adminer\PdoResult');
try {
$this->pdo = new \PDO($dsn, $username, $password, $options);
} catch (\Exception $ex) {
@@ -19,13 +24,12 @@ if (extension_loaded('pdo')) {
$this->server_info = @$this->pdo->getAttribute(\PDO::ATTR_SERVER_VERSION);
}
abstract function select_db($database);
function quote($string) {
return $this->pdo->quote($string);
}
function query($query, $unbuffered = false) {
/** @var Result|bool */
$result = $this->pdo->query($query);
$this->error = "";
if (!$result) {
@@ -39,13 +43,9 @@ if (extension_loaded('pdo')) {
return $result;
}
function multi_query($query) {
return $this->result = $this->query($query);
}
function store_result($result = null) {
if (!$result) {
$result = $this->result;
$result = $this->multi;
if (!$result) {
return false;
}
@@ -59,24 +59,15 @@ if (extension_loaded('pdo')) {
}
function next_result() {
if (!$this->result) {
if (!is_object($this->multi)) {
return false;
}
$this->result->_offset = 0;
return @$this->result->nextRowset(); // @ - PDO_PgSQL doesn't support it
}
function result($query, $field = 0) {
$result = $this->query($query);
if (!$result) {
return false;
}
$row = $result->fetch();
return $row ? $row[$field] : false;
$this->multi->_offset = 0;
return @$this->multi->nextRowset(); // @ - PDO_PgSQL doesn't support it
}
}
class PdoDbStatement extends \PDOStatement {
class PdoResult extends \PDOStatement {
public $_offset = 0, $num_rows;
function fetch_assoc() {
@@ -87,10 +78,6 @@ if (extension_loaded('pdo')) {
return $this->fetch(\PDO::FETCH_NUM);
}
function fetch_column($field) {
return $this->fetchColumn($field);
}
function fetch_field() {
$row = (object) $this->getColumnMeta($this->_offset++);
$type = $row->pdo_type;