From 81192a142bbfde4f0795a6496c62ffc58dec5700 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 9 Sep 2022 23:29:26 -0300 Subject: Move DBI getRoutines method to the Routines class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DatabaseInterface::getRoutines -> Database\Routines::getDetails Signed-off-by: MaurĂ­cio Meneghini Fauth --- .../Controllers/Database/RoutinesController.php | 2 +- libraries/classes/Database/Routines.php | 73 +++++++++++++++++++++- libraries/classes/DatabaseInterface.php | 68 -------------------- libraries/classes/Dbal/DbalInterface.php | 12 ---- libraries/classes/Server/Privileges.php | 5 +- 5 files changed, 76 insertions(+), 84 deletions(-) (limited to 'libraries') diff --git a/libraries/classes/Controllers/Database/RoutinesController.php b/libraries/classes/Controllers/Database/RoutinesController.php index b75b6ace84..e827e5a6f1 100644 --- a/libraries/classes/Controllers/Database/RoutinesController.php +++ b/libraries/classes/Controllers/Database/RoutinesController.php @@ -113,7 +113,7 @@ class RoutinesController extends AbstractController $type = null; } - $items = $this->dbi->getRoutines($GLOBALS['db'], $type); + $items = Routines::getDetails($this->dbi, $GLOBALS['db'], $type); $isAjax = $this->response->isAjax() && empty($_REQUEST['ajax_page_request']); $rows = ''; diff --git a/libraries/classes/Database/Routines.php b/libraries/classes/Database/Routines.php index 31a116d7c0..729bcc1492 100644 --- a/libraries/classes/Database/Routines.php +++ b/libraries/classes/Database/Routines.php @@ -8,6 +8,7 @@ use PhpMyAdmin\Charsets; use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Html\Generator; use PhpMyAdmin\Message; +use PhpMyAdmin\Query\Generator as QueryGenerator; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\SqlParser\Parser; use PhpMyAdmin\SqlParser\Statements\CreateStatement; @@ -18,7 +19,9 @@ use PhpMyAdmin\Util; use function __; use function _ngettext; +use function array_column; use function array_merge; +use function array_multisort; use function count; use function explode; use function htmlentities; @@ -38,6 +41,7 @@ use function substr; use function trim; use const ENT_QUOTES; +use const SORT_ASC; /** * Functions for routine management. @@ -289,7 +293,7 @@ class Routines exit; } - $routines = $this->dbi->getRoutines($db, $_POST['item_type'], $_POST['item_name']); + $routines = self::getDetails($this->dbi, $db, $_POST['item_type'], $_POST['item_name']); $routine = $routines[0]; $this->response->addJSON( 'name', @@ -1597,4 +1601,71 @@ class Routines $this->response->addHTML($message->getDisplay()); } + + /** + * returns details about the PROCEDUREs or FUNCTIONs for a specific database + * or details about a specific routine + * + * @param string $db db name + * @param string|null $which PROCEDURE | FUNCTION or null for both + * @param string $name name of the routine (to fetch a specific routine) + * + * @return array information about PROCEDUREs or FUNCTIONs + */ + public static function getDetails( + DatabaseInterface $dbi, + string $db, + ?string $which = null, + string $name = '' + ): array { + if (! $GLOBALS['cfg']['Server']['DisableIS']) { + $query = QueryGenerator::getInformationSchemaRoutinesRequest( + $dbi->escapeString($db), + isset($which) && in_array($which, ['FUNCTION', 'PROCEDURE']) ? $which : null, + empty($name) ? null : $dbi->escapeString($name) + ); + $routines = $dbi->fetchResult($query); + } else { + $routines = []; + + if ($which === 'FUNCTION' || $which == null) { + $query = 'SHOW FUNCTION STATUS' + . " WHERE `Db` = '" . $dbi->escapeString($db) . "'"; + if ($name) { + $query .= " AND `Name` = '" + . $dbi->escapeString($name) . "'"; + } + + $routines = $dbi->fetchResult($query); + } + + if ($which === 'PROCEDURE' || $which == null) { + $query = 'SHOW PROCEDURE STATUS' + . " WHERE `Db` = '" . $dbi->escapeString($db) . "'"; + if ($name) { + $query .= " AND `Name` = '" + . $dbi->escapeString($name) . "'"; + } + + $routines = array_merge($routines, $dbi->fetchResult($query)); + } + } + + $ret = []; + foreach ($routines as $routine) { + $ret[] = [ + 'db' => $routine['Db'], + 'name' => $routine['Name'], + 'type' => $routine['Type'], + 'definer' => $routine['Definer'], + 'returns' => $routine['DTD_IDENTIFIER'] ?? '', + ]; + } + + // Sort results by name + $name = array_column($ret, 'name'); + array_multisort($name, SORT_ASC, $ret); + + return $ret; + } } diff --git a/libraries/classes/DatabaseInterface.php b/libraries/classes/DatabaseInterface.php index 9b40bb5040..a38e976a63 100644 --- a/libraries/classes/DatabaseInterface.php +++ b/libraries/classes/DatabaseInterface.php @@ -30,7 +30,6 @@ use function array_column; use function array_diff; use function array_keys; use function array_map; -use function array_merge; use function array_multisort; use function array_reverse; use function array_shift; @@ -41,7 +40,6 @@ use function count; use function defined; use function explode; use function implode; -use function in_array; use function is_array; use function is_int; use function is_string; @@ -1525,72 +1523,6 @@ class DatabaseInterface implements DbalInterface return is_string($result) ? $result : null; } - /** - * returns details about the PROCEDUREs or FUNCTIONs for a specific database - * or details about a specific routine - * - * @param string $db db name - * @param string|null $which PROCEDURE | FUNCTION or null for both - * @param string $name name of the routine (to fetch a specific routine) - * - * @return array information about PROCEDUREs or FUNCTIONs - */ - public function getRoutines( - string $db, - ?string $which = null, - string $name = '' - ): array { - if (! $GLOBALS['cfg']['Server']['DisableIS']) { - $query = QueryGenerator::getInformationSchemaRoutinesRequest( - $this->escapeString($db), - isset($which) && in_array($which, ['FUNCTION', 'PROCEDURE']) ? $which : null, - empty($name) ? null : $this->escapeString($name) - ); - $routines = $this->fetchResult($query); - } else { - $routines = []; - - if ($which === 'FUNCTION' || $which == null) { - $query = 'SHOW FUNCTION STATUS' - . " WHERE `Db` = '" . $this->escapeString($db) . "'"; - if ($name) { - $query .= " AND `Name` = '" - . $this->escapeString($name) . "'"; - } - - $routines = $this->fetchResult($query); - } - - if ($which === 'PROCEDURE' || $which == null) { - $query = 'SHOW PROCEDURE STATUS' - . " WHERE `Db` = '" . $this->escapeString($db) . "'"; - if ($name) { - $query .= " AND `Name` = '" - . $this->escapeString($name) . "'"; - } - - $routines = array_merge($routines, $this->fetchResult($query)); - } - } - - $ret = []; - foreach ($routines as $routine) { - $ret[] = [ - 'db' => $routine['Db'], - 'name' => $routine['Name'], - 'type' => $routine['Type'], - 'definer' => $routine['Definer'], - 'returns' => $routine['DTD_IDENTIFIER'] ?? '', - ]; - } - - // Sort results by name - $name = array_column($ret, 'name'); - array_multisort($name, SORT_ASC, $ret); - - return $ret; - } - /** * returns details about the EVENTs for a specific database * diff --git a/libraries/classes/Dbal/DbalInterface.php b/libraries/classes/Dbal/DbalInterface.php index d5722162c5..999f973c3d 100644 --- a/libraries/classes/Dbal/DbalInterface.php +++ b/libraries/classes/Dbal/DbalInterface.php @@ -461,18 +461,6 @@ interface DbalInterface $link = DatabaseInterface::CONNECT_USER ): ?string; - /** - * returns details about the PROCEDUREs or FUNCTIONs for a specific database - * or details about a specific routine - * - * @param string $db db name - * @param string|null $which PROCEDURE | FUNCTION or null for both - * @param string $name name of the routine (to fetch a specific routine) - * - * @return array information about ROCEDUREs or FUNCTIONs - */ - public function getRoutines(string $db, ?string $which = null, string $name = ''): array; - /** * returns details about the EVENTs for a specific database * diff --git a/libraries/classes/Server/Privileges.php b/libraries/classes/Server/Privileges.php index 27198284f6..5bec85a267 100644 --- a/libraries/classes/Server/Privileges.php +++ b/libraries/classes/Server/Privileges.php @@ -11,6 +11,7 @@ use mysqli_stmt; use PhpMyAdmin\ConfigStorage\Features\ConfigurableMenusFeature; use PhpMyAdmin\ConfigStorage\Relation; use PhpMyAdmin\ConfigStorage\RelationCleanup; +use PhpMyAdmin\Database\Routines; use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Dbal\MysqliResult; use PhpMyAdmin\Dbal\ResultInterface; @@ -1966,7 +1967,7 @@ class Privileges $data['tables'] = $tables; } else { // routine - $routineData = $this->dbi->getRoutines($dbname); + $routineData = Routines::getDetails($this->dbi, $dbname); $routines = []; foreach ($routineData as $routine) { @@ -3660,7 +3661,7 @@ class Privileges */ public function getRoutineType(string $dbname, string $routineName) { - $routineData = $this->dbi->getRoutines($dbname); + $routineData = Routines::getDetails($this->dbi, $dbname); $routineName = mb_strtolower($routineName); foreach ($routineData as $routine) { -- cgit v1.2.3