Skip to content

Commit

Permalink
Merge pull request phpmyadmin#19461 from kamil-tekiela/fetchResult
Browse files Browse the repository at this point in the history
Creation of fetchResultSimple and fetchSingleColumn
  • Loading branch information
MauricioFauth authored Dec 22, 2024
2 parents 0390fd2 + a07292e commit bcd2b94
Show file tree
Hide file tree
Showing 44 changed files with 362 additions and 681 deletions.
258 changes: 18 additions & 240 deletions phpstan-baseline.neon

Large diffs are not rendered by default.

192 changes: 43 additions & 149 deletions psalm-baseline.xml

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/Bookmarks/BookmarkRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function getList(

$query .= ' ORDER BY label ASC';

$result = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
$result = $this->dbi->fetchResultSimple($query, ConnectionType::ControlUser);

$bookmarks = [];
foreach ($result as $row) {
Expand Down Expand Up @@ -134,7 +134,7 @@ public function get(
$query .= ' LIMIT 1';

$result = $this->dbi->fetchSingleRow($query, DatabaseInterface::FETCH_ASSOC, ConnectionType::ControlUser);
if ($result !== null) {
if ($result !== []) {
return $this->createFromRow($result);
}

Expand Down Expand Up @@ -162,7 +162,7 @@ public function getByLabel(
. ' LIMIT 1';

$result = $this->dbi->fetchSingleRow($query, DatabaseInterface::FETCH_ASSOC, ConnectionType::ControlUser);
if ($result !== null) {
if ($result !== []) {
return $this->createFromRow($result);
}

Expand Down
8 changes: 4 additions & 4 deletions src/ConfigStorage/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ public function getDisplayField(string $db, string $table): string
. ' AND `table_name` = ' . $this->dbi->quoteString($table);

$row = $this->dbi->fetchSingleRow($dispQuery, DatabaseInterface::FETCH_ASSOC, ConnectionType::ControlUser);
if (isset($row['display_field'])) {
if ($row['display_field'] !== null) {
return $row['display_field'];
}
}
Expand Down Expand Up @@ -662,9 +662,9 @@ public function setHistory(string $db, string $table, string $username, string $
*
* @param string $username the username
*
* @return mixed[]|bool list of history items
* @return mixed[]|false list of history items
*/
public function getHistory(string $username): array|bool
public function getHistory(string $username): array|false
{
$sqlHistoryFeature = $this->getRelationParameters()->sqlHistoryFeature;
if ($sqlHistoryFeature === null) {
Expand Down Expand Up @@ -693,7 +693,7 @@ public function getHistory(string $username): array|bool
WHERE `username` = ' . $this->dbi->quoteString($username) . '
ORDER BY `id` DESC';

return $this->dbi->fetchResult($histQuery, null, null, ConnectionType::ControlUser);
return $this->dbi->fetchResultSimple($histQuery, ConnectionType::ControlUser);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __invoke(ServerRequest $request): Response

$tablesListForQuery = array_map($this->dbi->quoteString(...), $tables);

$constrains = $this->dbi->fetchResult(
$constrains = $this->dbi->fetchResultSimple(
QueryGenerator::getInformationSchemaForeignKeyConstraintsRequest(
$this->dbi->quoteString($db),
implode(',', $tablesListForQuery),
Expand Down
4 changes: 2 additions & 2 deletions src/Controllers/Table/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,9 @@ private function rangeSearchAction(): void
*
* @param string $column Column name
*
* @return mixed[]|null
* @return array<string|null>
*/
private function getColumnMinMax(string $column): array|null
private function getColumnMinMax(string $column): array
{
$sqlQuery = 'SELECT MIN(' . Util::backquote($column) . ') AS `min`, '
. 'MAX(' . Util::backquote($column) . ') AS `max` '
Expand Down
42 changes: 20 additions & 22 deletions src/Database/CentralColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use function in_array;
use function is_array;
use function is_bool;
use function is_string;
use function mb_strtoupper;
use function sprintf;
use function trim;
Expand Down Expand Up @@ -100,7 +101,7 @@ public function getParams(): array|false
* @param int $from starting offset of first result
* @param int $num maximum number of results to return
*
* @return mixed[] list of $num columns present in central columns list
* @return list<array<string|null>> list of $num columns present in central columns list
* starting at offset $from for the given database
*/
public function getColumnsList(string $db, int $from = 0, int $num = 25): array
Expand All @@ -122,10 +123,9 @@ public function getColumnsList(string $db, int $from = 0, int $num = 25): array
. 'LIMIT ' . $from . ', ' . $num . ';';
}

$hasList = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
$this->handleColumnExtra($hasList);
$hasList = $this->dbi->fetchResultSimple($query, ConnectionType::ControlUser);

return $hasList;
return $this->handleColumnExtra($hasList);
}

/**
Expand All @@ -147,12 +147,8 @@ public function getCount(string $db): int
$query = 'SELECT count(db_name) FROM '
. Util::backquote($pmadb) . '.' . Util::backquote($centralListTable) . ' '
. 'WHERE db_name = ' . $this->dbi->quoteString($db, ConnectionType::ControlUser) . ';';
$res = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
if (isset($res[0])) {
return (int) $res[0];
}

return 0;
return (int) $this->dbi->fetchValue($query, 0, ConnectionType::ControlUser);
}

/**
Expand Down Expand Up @@ -180,7 +176,7 @@ public function findExistingColNames(
. Util::backquote($pmadb) . '.' . Util::backquote($centralListTable) . ' WHERE db_name = '
. $this->dbi->quoteString($db, ConnectionType::ControlUser) . ' AND col_name IN (' . $cols . ');';

return $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
return $this->dbi->fetchSingleColumn($query, ConnectionType::ControlUser);
}

/**
Expand All @@ -207,10 +203,9 @@ private function findExistingColumns(
$query = 'SELECT * FROM '
. Util::backquote($pmadb) . '.' . Util::backquote($centralListTable) . ' WHERE db_name = '
. $this->dbi->quoteString($db, ConnectionType::ControlUser) . ' AND col_name IN (' . $cols . ');';
$hasList = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
$this->handleColumnExtra($hasList);
$hasList = $this->dbi->fetchResultSimple($query, ConnectionType::ControlUser);

return $hasList;
return $this->handleColumnExtra($hasList);
}

/**
Expand Down Expand Up @@ -667,7 +662,7 @@ private function getHtmlForEditTableRow(array $row, int $rowNum): string
* @param string $db selected database
* @param string $table current table name
*
* @return mixed[] encoded list of columns present in central list for the given database
* @return list<array<string|null>> encoded list of columns present in central list for the given database
*/
public function getListRaw(string $db, string $table): array
{
Expand All @@ -692,19 +687,20 @@ public function getListRaw(string $db, string $table): array
$query .= ';';
}

$columnsList = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
$this->handleColumnExtra($columnsList);
$columnsList = $this->dbi->fetchResultSimple($query, ConnectionType::ControlUser);

return $columnsList;
return $this->handleColumnExtra($columnsList);
}

/**
* Column `col_extra` is used to store both extra and attributes for a column.
* This method separates them.
*
* @param mixed[] $columnsList columns list
* @param list<array<string|null>> $columnsList columns list
*
* @return list<array<string|null>>
*/
private function handleColumnExtra(array &$columnsList): void
private function handleColumnExtra(array $columnsList): array
{
foreach ($columnsList as &$row) {
$vals = explode(',', $row['col_extra']);
Expand All @@ -723,6 +719,8 @@ private function handleColumnExtra(array &$columnsList): void

$row['col_extra'] = in_array('auto_increment', $vals, true) ? 'auto_increment' : '';
}

return $columnsList;
}

/**
Expand Down Expand Up @@ -771,10 +769,10 @@ public function getColumnsCount(string $db, int $from = 0, int $num = 25): int
$query = 'SELECT COUNT(db_name) FROM ' . Util::backquote($pmadb) . '.' . Util::backquote($centralListTable)
. ' WHERE db_name = ' . $this->dbi->quoteString($db, ConnectionType::ControlUser)
. ($num === 0 ? '' : 'LIMIT ' . $from . ', ' . $num) . ';';
$result = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
$result = $this->dbi->fetchValue($query, 0, ConnectionType::ControlUser);

if (isset($result[0])) {
return (int) $result[0];
if (is_string($result)) {
return (int) $result;
}

return -1;
Expand Down
3 changes: 1 addition & 2 deletions src/Database/Designer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use stdClass;

use function __;
use function is_array;
use function json_decode;
use function str_contains;

Expand Down Expand Up @@ -146,7 +145,7 @@ private function getSideMenuParamsArray(): array
. ';';

$result = $this->dbi->fetchSingleRow($query);
if (is_array($result)) {
if ($result !== []) {
$params = json_decode((string) $result['settings_data'], true);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Designer/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public function getPageExists(string $pg): bool
. ' FROM ' . Util::backquote($pdfFeature->database)
. '.' . Util::backquote($pdfFeature->pdfPages)
. ' WHERE `page_descr` = ' . $this->dbi->quoteString($pg, ConnectionType::ControlUser);
$pageNos = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
$pageNos = $this->dbi->fetchResultSimple($query, ConnectionType::ControlUser);

return $pageNos !== [];
}
Expand Down Expand Up @@ -669,7 +669,7 @@ public function saveSetting(string $index, string $value): bool
ConnectionType::ControlUser,
);

if ($origData !== null && $origData !== []) {
if ($origData !== []) {
$origData = json_decode($origData['settings_data'], true);
$origData[$index] = $value;
$origData = json_encode($origData);
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function getDataFromName(string $name): array|null
. ' AND EVENT_NAME=' . $this->dbi->quoteString($name);
$query = 'SELECT ' . $columns . ' FROM `INFORMATION_SCHEMA`.`EVENTS` WHERE ' . $where . ';';
$item = $this->dbi->fetchSingleRow($query);
if ($item === null || $item === []) {
if ($item === []) {
return null;
}

Expand Down Expand Up @@ -367,7 +367,7 @@ public function getDetails(string $db, string $name = ''): array
}

$result = [];
$events = $this->dbi->fetchResult($query);
$events = $this->dbi->fetchResultSimple($query);

/** @var string[] $event */
foreach ($events as $event) {
Expand Down
12 changes: 6 additions & 6 deletions src/Database/Routines.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public function getDataFromName(string $name, string $type, bool $all = true): a

$routine = $this->dbi->fetchSingleRow($query);

if ($routine === null || $routine === []) {
if ($routine === []) {
return null;
}

Expand Down Expand Up @@ -1256,7 +1256,7 @@ public static function getDetails(
in_array($which, ['FUNCTION', 'PROCEDURE'], true) ? $which : null,
$name === '' ? null : $dbi->quoteString($name),
);
$routines = $dbi->fetchResult($query);
$routines = $dbi->fetchResultSimple($query);
} else {
$routines = [];

Expand All @@ -1266,7 +1266,7 @@ public static function getDetails(
$query .= ' AND `Name` = ' . $dbi->quoteString($name);
}

$routines = $dbi->fetchResult($query);
$routines = $dbi->fetchResultSimple($query);
}

if ($which === 'PROCEDURE' || $which == null) {
Expand All @@ -1275,7 +1275,7 @@ public static function getDetails(
$query .= ' AND `Name` = ' . $dbi->quoteString($name);
}

$routines = array_merge($routines, $dbi->fetchResult($query));
$routines = array_merge($routines, $dbi->fetchResultSimple($query));
}
}

Expand Down Expand Up @@ -1319,7 +1319,7 @@ public static function getProcedureDefinition(DatabaseInterface $dbi, string $db
public static function getFunctionNames(DatabaseInterface $dbi, string $db): array
{
/** @psalm-var list<array{Db: string, Name: string, Type: string}> $functions */
$functions = $dbi->fetchResult('SHOW FUNCTION STATUS;');
$functions = $dbi->fetchResultSimple('SHOW FUNCTION STATUS;');
$names = [];
foreach ($functions as $function) {
if ($function['Db'] !== $db || $function['Type'] !== 'FUNCTION' || $function['Name'] === '') {
Expand All @@ -1339,7 +1339,7 @@ public static function getFunctionNames(DatabaseInterface $dbi, string $db): arr
public static function getProcedureNames(DatabaseInterface $dbi, string $db): array
{
/** @psalm-var list<array{Db: string, Name: string, Type: string}> $procedures */
$procedures = $dbi->fetchResult('SHOW PROCEDURE STATUS;');
$procedures = $dbi->fetchResultSimple('SHOW PROCEDURE STATUS;');
$names = [];
foreach ($procedures as $procedure) {
if ($procedure['Db'] !== $db || $procedure['Type'] !== 'PROCEDURE' || $procedure['Name'] === '') {
Expand Down
Loading

0 comments on commit bcd2b94

Please sign in to comment.