Skip to content

Commit

Permalink
Extract SQL query from DatabaseInterface::getColumnsFull
Browse files Browse the repository at this point in the history
Signed-off-by: William Desportes <williamdes@wdes.fr>
  • Loading branch information
williamdes committed May 31, 2020
1 parent 73fb678 commit 36fa925
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 41 deletions.
47 changes: 6 additions & 41 deletions libraries/classes/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -817,48 +817,13 @@ public function getColumnsFull(
$link = self::CONNECT_USER
): array {
if (! $GLOBALS['cfg']['Server']['DisableIS']) {
$sql_wheres = [];
$array_keys = [];

// get columns information from information_schema
if ($database !== null) {
$sql_wheres[] = '`TABLE_SCHEMA` = \''
. $this->escapeString($database, $link) . '\' ';
} else {
$array_keys[] = 'TABLE_SCHEMA';
}
if ($table !== null) {
$sql_wheres[] = '`TABLE_NAME` = \''
. $this->escapeString($table, $link) . '\' ';
} else {
$array_keys[] = 'TABLE_NAME';
}
if ($column !== null) {
$sql_wheres[] = '`COLUMN_NAME` = \''
. $this->escapeString($column, $link) . '\' ';
} else {
$array_keys[] = 'COLUMN_NAME';
}

// for PMA bc:
// `[SCHEMA_FIELD_NAME]` AS `[SHOW_FULL_COLUMNS_FIELD_NAME]`
$sql = 'SELECT *,'
. ' `COLUMN_NAME` AS `Field`,'
. ' `COLUMN_TYPE` AS `Type`,'
. ' `COLLATION_NAME` AS `Collation`,'
. ' `IS_NULLABLE` AS `Null`,'
. ' `COLUMN_KEY` AS `Key`,'
. ' `COLUMN_DEFAULT` AS `Default`,'
. ' `EXTRA` AS `Extra`,'
. ' `PRIVILEGES` AS `Privileges`,'
. ' `COLUMN_COMMENT` AS `Comment`'
. ' FROM `information_schema`.`COLUMNS`';

if (count($sql_wheres)) {
$sql .= "\n" . ' WHERE ' . implode(' AND ', $sql_wheres);
}
[$sql, $arrayKeys] = QueryGenerator::getInformationSchemaColumnsFullRequest(
$database !== null ? $this->escapeString($database, $link) : null,
$table !== null ? $this->escapeString($table, $link) : null,
$column !== null ? $this->escapeString($column, $link) : null
);

return $this->fetchResult($sql, $array_keys, null, $link);
return $this->fetchResult($sql, $arrayKeys, null, $link);
}

$columns = [];
Expand Down
50 changes: 50 additions & 0 deletions libraries/classes/Query/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpMyAdmin\Query;

use PhpMyAdmin\Util;
use function count;
use function implode;
use function is_array;

Expand Down Expand Up @@ -306,4 +307,53 @@ public static function getInformationSchemaDatabasesFullRequest(

return $sql;
}

public static function getInformationSchemaColumnsFullRequest(
?string $escapedDatabase,
?string $escapedTable,
?string $escapedColumn
): array {
$sqlWheres = [];
$arrayKeys = [];

// get columns information from information_schema
if ($escapedDatabase !== null) {
$sqlWheres[] = '`TABLE_SCHEMA` = \''
. $escapedDatabase . '\' ';
} else {
$arrayKeys[] = 'TABLE_SCHEMA';
}
if ($escapedTable !== null) {
$sqlWheres[] = '`TABLE_NAME` = \''
. $escapedTable . '\' ';
} else {
$arrayKeys[] = 'TABLE_NAME';
}
if ($escapedColumn !== null) {
$sqlWheres[] = '`COLUMN_NAME` = \''
. $escapedColumn . '\' ';
} else {
$arrayKeys[] = 'COLUMN_NAME';
}

// for PMA bc:
// `[SCHEMA_FIELD_NAME]` AS `[SHOW_FULL_COLUMNS_FIELD_NAME]`
$sql = 'SELECT *,'
. ' `COLUMN_NAME` AS `Field`,'
. ' `COLUMN_TYPE` AS `Type`,'
. ' `COLLATION_NAME` AS `Collation`,'
. ' `IS_NULLABLE` AS `Null`,'
. ' `COLUMN_KEY` AS `Key`,'
. ' `COLUMN_DEFAULT` AS `Default`,'
. ' `EXTRA` AS `Extra`,'
. ' `PRIVILEGES` AS `Privileges`,'
. ' `COLUMN_COMMENT` AS `Comment`'
. ' FROM `information_schema`.`COLUMNS`';

if (count($sqlWheres)) {
$sql .= "\n" . ' WHERE ' . implode(' AND ', $sqlWheres);
}

return [$sql, $arrayKeys];
}
}

0 comments on commit 36fa925

Please sign in to comment.