Files
adminer/adminer/include/driver.inc.php

250 lines
7.9 KiB
PHP
Raw Normal View History

<?php
2025-03-05 11:28:53 +01:00
namespace Adminer;
$drivers = array();
2025-03-28 09:13:36 +01:00
/** Add a driver */
function add_driver(string $id, string $name): void {
global $drivers;
$drivers[$id] = $name;
}
2025-03-28 09:13:36 +01:00
/** Get driver name */
function get_driver(string $id): string {
global $drivers;
return $drivers[$id];
}
2025-03-05 14:31:53 +01:00
abstract class SqlDriver {
2025-03-28 09:20:10 +01:00
/** @var list<string> */ static array $possibleDrivers = array();
static string $jush; // JUSH identifier
protected Db $conn;
/** @var int[][] */ protected array $types = array(); // [$group => [$type => $maximum_unsigned_length, ...], ...]
2025-03-28 14:53:53 +01:00
/** @var string[] */ public array $insertFunctions = array(); // ["$type|$type2" => "$function/$function2"] functions used in edit and insert
/** @var string[] */ public array $editFunctions = array(); // ["$type|$type2" => "$function/$function2"] functions used in edit only
2025-03-28 09:20:10 +01:00
/** @var list<string> */ public array $unsigned = array(); // number variants
/** @var list<string> */ public array $operators = array(); // operators used in select
/** @var list<string> */ public array $functions = array(); // functions used in select
/** @var list<string> */ public array $grouping = array(); // grouping functions used in select
public string $onActions = "RESTRICT|NO ACTION|CASCADE|SET NULL|SET DEFAULT"; // used in foreign_keys()
public string $inout = "IN|OUT|INOUT"; // used in routines
public string $enumLength = "'(?:''|[^'\\\\]|\\\\.)*'"; // regular expression for parsing enum lengths
/** @var list<string> */ public array $generated = array(); // allowed types of generated columns
2025-02-21 13:53:18 +01:00
2025-03-28 09:13:36 +01:00
/** Create object for performing database operations */
function __construct(Db $connection) {
2025-03-11 07:49:10 +01:00
$this->conn = $connection;
}
2025-02-21 13:53:18 +01:00
2025-03-06 12:35:20 +01:00
/** Get all types
2025-03-25 13:08:03 +01:00
* @return int[] [$type => $maximum_unsigned_length, ...]
2025-03-06 12:35:20 +01:00
*/
function types(): array {
2025-03-06 12:35:20 +01:00
return call_user_func_array('array_merge', array_values($this->types));
}
/** Get structured types
2025-03-26 16:57:58 +01:00
* @return list<string>[]|list<string> [$description => [$type, ...], ...]
2025-03-06 12:35:20 +01:00
*/
2025-03-28 09:57:33 +01:00
function structuredTypes(): array {
2025-03-06 12:35:20 +01:00
return array_map('array_keys', $this->types);
}
/** Get enum values
2025-03-28 07:06:34 +01:00
* @param Field $field
2025-03-26 15:38:04 +01:00
* @return string|void
*/
function enumLength(array $field) {
}
/** Function used to convert the value inputted by user
2025-03-28 07:06:34 +01:00
* @param Field $field
2025-03-26 15:38:04 +01:00
* @return string|void
*/
function unconvertFunction(array $field) {
}
2013-07-09 17:38:13 -07:00
/** Select data from table
2025-03-28 07:06:34 +01:00
* @param list<string> $select result of $adminer->selectColumnsProcess()[0]
* @param list<string> $where result of $adminer->selectSearchProcess()
* @param list<string> $group result of $adminer->selectColumnsProcess()[1]
* @param list<string> $order result of $adminer->selectOrderProcess()
* @param int|numeric-string $limit result of $adminer->selectLimitProcess()
* @param int $page index of page starting at zero
* @param bool $print whether to print the query
2025-03-26 16:57:58 +01:00
* @return Result|false
2013-07-09 17:38:13 -07:00
*/
2025-03-28 11:46:17 +01:00
function select(string $table, array $select, array $where, array $group, array $order = array(), $limit = 1, ?int $page = 0, bool $print = false) {
2025-03-06 17:51:20 +01:00
global $adminer;
2013-07-09 17:38:13 -07:00
$is_group = (count($group) < count($select));
$query = $adminer->selectQueryBuild($select, $where, $group, $order, $limit, $page);
if (!$query) {
$query = "SELECT" . limit(
2025-03-06 17:51:20 +01:00
($_GET["page"] != "last" && $limit != "" && $group && $is_group && JUSH == "sql" ? "SQL_CALC_FOUND_ROWS " : "") . implode(", ", $select) . "\nFROM " . table($table),
2013-07-09 17:38:13 -07:00
($where ? "\nWHERE " . implode(" AND ", $where) : "") . ($group && $is_group ? "\nGROUP BY " . implode(", ", $group) : "") . ($order ? "\nORDER BY " . implode(", ", $order) : ""),
($limit != "" ? +$limit : null),
($page ? $limit * $page : 0),
"\n"
);
}
2014-02-23 18:34:00 -08:00
$start = microtime(true);
2025-03-11 07:49:10 +01:00
$return = $this->conn->query($query);
2014-01-08 23:14:37 -08:00
if ($print) {
echo $adminer->selectQuery($query, $start, !$return);
2014-01-08 23:14:37 -08:00
}
2014-02-23 18:34:00 -08:00
return $return;
2013-07-09 17:38:13 -07:00
}
2025-02-21 13:53:18 +01:00
/** Delete data from table
2025-03-28 07:06:34 +01:00
* @param string $queryWhere " WHERE ..."
* @param int $limit 0 or 1
2025-03-26 16:57:58 +01:00
* @return Result|bool
*/
function delete(string $table, string $queryWhere, int $limit = 0) {
$query = "FROM " . table($table);
return queries("DELETE" . ($limit ? limit1($table, $query, $queryWhere) : " $query$queryWhere"));
}
2025-02-21 13:53:18 +01:00
2013-07-05 15:32:15 -07:00
/** Update data in table
2025-03-28 07:06:34 +01:00
* @param string[] $set escaped columns in keys, quoted data in values
* @param string $queryWhere " WHERE ..."
* @param int $limit 0 or 1
2025-03-26 16:57:58 +01:00
* @return Result|bool
2013-07-05 15:32:15 -07:00
*/
function update(string $table, array $set, string $queryWhere, int $limit = 0, string $separator = "\n") {
2013-07-06 10:31:21 -07:00
$values = array();
foreach ($set as $key => $val) {
$values[] = "$key = $val";
}
$query = table($table) . " SET$separator" . implode(",$separator", $values);
2018-02-01 18:53:53 +01:00
return queries("UPDATE" . ($limit ? limit1($table, $query, $queryWhere, $separator) : " $query$queryWhere"));
2013-07-05 15:32:15 -07:00
}
2025-02-21 13:53:18 +01:00
/** Insert data into table
2025-03-28 07:06:34 +01:00
* @param string[] $set escaped columns in keys, quoted data in values
2025-03-26 16:57:58 +01:00
* @return Result|bool
*/
function insert(string $table, array $set) {
2025-03-22 20:55:51 +01:00
return queries("INSERT INTO " . table($table) . ($set
? " (" . implode(", ", array_keys($set)) . ")\nVALUES (" . implode(", ", $set) . ")"
: " DEFAULT VALUES"
) . $this->insertReturning($table));
}
2025-03-28 09:13:36 +01:00
/** Get RETURNING clause for INSERT queries (PostgreSQL specific) */
function insertReturning(string $table): string {
2025-03-22 20:55:51 +01:00
return "";
}
2025-02-21 13:53:18 +01:00
/** Insert or update data in table
2025-03-28 07:06:34 +01:00
* @param list<string[]> $rows of arrays with escaped columns in keys and quoted data in values
* @param int[] $primary column names in keys
2025-03-26 16:57:58 +01:00
* @return Result|bool
*/
function insertUpdate(string $table, array $rows, array $primary) {
return false;
}
2025-02-21 13:53:18 +01:00
2013-07-09 11:43:01 -07:00
/** Begin transaction
2025-03-26 16:57:58 +01:00
* @return Result|bool
2013-07-09 11:43:01 -07:00
*/
function begin() {
return queries("BEGIN");
}
2025-02-21 13:53:18 +01:00
2018-01-31 17:28:12 +01:00
/** Commit transaction
2025-03-26 16:57:58 +01:00
* @return Result|bool
2018-01-31 17:28:12 +01:00
*/
2013-07-09 11:43:01 -07:00
function commit() {
return queries("COMMIT");
}
2025-02-21 13:53:18 +01:00
2018-01-31 17:28:12 +01:00
/** Rollback transaction
2025-03-26 16:57:58 +01:00
* @return Result|bool
2018-01-31 17:28:12 +01:00
*/
2013-07-09 11:43:01 -07:00
function rollback() {
return queries("ROLLBACK");
}
2025-02-21 13:53:18 +01:00
2018-03-09 18:06:19 +01:00
/** Return query with a timeout
2025-03-28 07:06:34 +01:00
* @param int $timeout seconds
2025-03-26 15:38:04 +01:00
* @return string|void null if the driver doesn't support query timeouts
2018-03-09 18:06:19 +01:00
*/
function slowQuery(string $query, int $timeout) {
2018-03-09 18:06:19 +01:00
}
2025-02-21 13:53:18 +01:00
/** Convert column to be searchable
2025-03-28 07:06:34 +01:00
* @param string $idf escaped column name
* @param array{op:string, val:string} $val
* @param Field $field
*/
function convertSearch(string $idf, array $val, array $field): string {
return $idf;
}
2018-02-06 16:05:39 +01:00
2025-03-28 09:13:36 +01:00
/** Convert operator so it can be used in search */
function convertOperator(string $operator): string {
2021-04-05 00:13:07 +02:00
return $operator;
}
2018-02-06 16:05:39 +01:00
/** Convert value returned by database to actual value
2025-03-28 07:06:34 +01:00
* @param Field $field
2018-02-06 16:05:39 +01:00
*/
2025-03-28 11:46:17 +01:00
function value(?string $val, array $field): ?string {
2025-03-11 07:49:10 +01:00
return (method_exists($this->conn, 'value')
? $this->conn->value($val, $field)
2018-02-20 22:31:49 +01:00
: (is_resource($val) ? stream_get_contents($val) : $val)
);
2018-02-06 16:05:39 +01:00
}
2025-03-28 09:13:36 +01:00
/** Quote binary string */
function quoteBinary(string $s): string {
return q($s);
}
2025-02-21 13:53:18 +01:00
2018-01-31 17:28:12 +01:00
/** Get warnings about the last command
2025-03-26 15:38:04 +01:00
* @return string|void HTML
2018-01-31 17:28:12 +01:00
*/
function warnings() {
}
2025-02-21 13:53:18 +01:00
2018-02-08 11:21:33 +01:00
/** Get help link for table
2025-03-26 15:38:04 +01:00
* @return string|void relative URL
2018-02-08 11:21:33 +01:00
*/
function tableHelp(string $name, bool $is_view = false) {
2018-02-08 11:21:33 +01:00
}
2025-02-21 13:53:18 +01:00
2025-03-28 09:13:36 +01:00
/** Check if C-style escapes are supported */
function hasCStyleEscapes(): bool {
2025-02-25 07:26:08 +01:00
return false;
}
2025-03-18 13:41:24 +01:00
/** Get supported engines
2025-03-25 13:08:03 +01:00
* @return list<string>
2025-03-18 13:41:24 +01:00
*/
function engines(): array {
2025-03-18 13:41:24 +01:00
return array();
}
/** Check whether table supports indexes
2025-03-28 12:03:56 +01:00
* @param TableStatus $table_status
*/
function supportsIndex(array $table_status): bool {
return !is_view($table_status);
}
2025-02-26 11:59:50 +01:00
/** Get defined check constraints
2025-03-25 13:08:03 +01:00
* @return string[] [$name => $clause]
2025-02-26 11:59:50 +01:00
*/
function checkConstraints(string $table): array {
2025-02-26 11:59:50 +01:00
// MariaDB contains CHECK_CONSTRAINTS.TABLE_NAME, MySQL and PostrgreSQL not
return get_key_vals("SELECT c.CONSTRAINT_NAME, CHECK_CLAUSE
FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS c
JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS t ON c.CONSTRAINT_SCHEMA = t.CONSTRAINT_SCHEMA AND c.CONSTRAINT_NAME = t.CONSTRAINT_NAME
WHERE c.CONSTRAINT_SCHEMA = " . q($_GET["ns"] != "" ? $_GET["ns"] : DB) . "
AND t.TABLE_NAME = " . q($table) . "
AND CHECK_CLAUSE NOT LIKE '% IS NOT NULL'"); // ignore default IS NOT NULL checks in PostrgreSQL
}
}