From 7975f5fb05befd63e10408a9c34a3ffff56a2515 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sun, 11 Sep 2022 15:48:26 -0300 Subject: Move DBI's getDefinition into the Routines and Events classes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- libraries/classes/Database/Events.php | 15 +++++++-- libraries/classes/Database/Routines.php | 44 ++++++++++++++++++++++---- libraries/classes/DatabaseInterface.php | 30 ------------------ libraries/classes/Dbal/DbalInterface.php | 17 ---------- libraries/classes/Operations.php | 8 +++-- libraries/classes/Plugins/Export/ExportSql.php | 8 +++-- libraries/classes/Plugins/Export/ExportXml.php | 8 +++-- 7 files changed, 66 insertions(+), 64 deletions(-) (limited to 'libraries') diff --git a/libraries/classes/Database/Events.php b/libraries/classes/Database/Events.php index 1a2196d8f6..496024b4d0 100644 --- a/libraries/classes/Database/Events.php +++ b/libraries/classes/Database/Events.php @@ -20,6 +20,7 @@ use function explode; use function htmlspecialchars; use function in_array; use function intval; +use function is_string; use function mb_strtoupper; use function sprintf; use function str_contains; @@ -100,7 +101,7 @@ class Events // Execute the created query if (! empty($_POST['editor_process_edit'])) { // Backup the old trigger, in case something goes wrong - $create_item = $this->dbi->getDefinition($GLOBALS['db'], 'EVENT', $_POST['item_original_name']); + $create_item = self::getDefinition($this->dbi, $GLOBALS['db'], $_POST['item_original_name']); $drop_item = 'DROP EVENT IF EXISTS ' . Util::backquote($_POST['item_original_name']) . ";\n"; @@ -556,7 +557,7 @@ class Events } $itemName = $_GET['item_name']; - $exportData = $this->dbi->getDefinition($GLOBALS['db'], 'EVENT', $itemName); + $exportData = self::getDefinition($this->dbi, $GLOBALS['db'], $itemName); if (! $exportData) { $exportData = false; @@ -642,4 +643,14 @@ class Events return $result; } + + public static function getDefinition(DatabaseInterface $dbi, string $db, string $name): ?string + { + $result = $dbi->fetchValue( + 'SHOW CREATE EVENT ' . Util::backquote($db) . '.' . Util::backquote($name), + 'Create Event' + ); + + return is_string($result) ? $result : null; + } } diff --git a/libraries/classes/Database/Routines.php b/libraries/classes/Database/Routines.php index 2f471d73cd..a8e9a0860a 100644 --- a/libraries/classes/Database/Routines.php +++ b/libraries/classes/Database/Routines.php @@ -204,9 +204,9 @@ class Routines } else { // Backup the old routine, in case something goes wrong if ($_POST['item_original_type'] === 'FUNCTION') { - $create_routine = $this->dbi->getDefinition($db, 'FUNCTION', $_POST['item_original_name']); + $create_routine = self::getFunctionDefinition($this->dbi, $db, $_POST['item_original_name']); } else { - $create_routine = $this->dbi->getDefinition($db, 'PROCEDURE', $_POST['item_original_name']); + $create_routine = self::getProcedureDefinition($this->dbi, $db, $_POST['item_original_name']); } $privilegesBackup = $this->backupPrivileges(); @@ -575,7 +575,11 @@ class Routines $retval['item_name'] = $routine['SPECIFIC_NAME']; $retval['item_type'] = $routine['ROUTINE_TYPE']; - $definition = $this->dbi->getDefinition($GLOBALS['db'], $routine['ROUTINE_TYPE'], $routine['SPECIFIC_NAME']); + if ($routine['ROUTINE_TYPE'] === 'FUNCTION') { + $definition = self::getFunctionDefinition($this->dbi, $GLOBALS['db'], $routine['SPECIFIC_NAME']); + } else { + $definition = self::getProcedureDefinition($this->dbi, $GLOBALS['db'], $routine['SPECIFIC_NAME']); + } if ($definition === null) { return null; @@ -1474,7 +1478,12 @@ class Routines // we will show a dialog to get values for these parameters, // otherwise we can execute it directly. - $definition = $this->dbi->getDefinition($GLOBALS['db'], $routine['type'], $routine['name']); + if ($routine['type'] === 'FUNCTION') { + $definition = self::getFunctionDefinition($this->dbi, $GLOBALS['db'], $routine['name']); + } else { + $definition = self::getProcedureDefinition($this->dbi, $GLOBALS['db'], $routine['name']); + } + $executeAction = ''; if ($definition !== null) { @@ -1540,11 +1549,14 @@ class Routines return; } - if ($_GET['item_type'] !== 'FUNCTION' && $_GET['item_type'] !== 'PROCEDURE') { + if ($_GET['item_type'] === 'FUNCTION') { + $routineDefinition = self::getFunctionDefinition($this->dbi, $GLOBALS['db'], $_GET['item_name']); + } elseif ($_GET['item_type'] === 'PROCEDURE') { + $routineDefinition = self::getProcedureDefinition($this->dbi, $GLOBALS['db'], $_GET['item_name']); + } else { return; } - $routineDefinition = $this->dbi->getDefinition($GLOBALS['db'], $_GET['item_type'], $_GET['item_name']); $exportData = false; if ($routineDefinition !== null) { @@ -1660,4 +1672,24 @@ class Routines return $ret; } + + public static function getFunctionDefinition(DatabaseInterface $dbi, string $db, string $name): ?string + { + $result = $dbi->fetchValue( + 'SHOW CREATE FUNCTION ' . Util::backquote($db) . '.' . Util::backquote($name), + 'Create Function' + ); + + return is_string($result) ? $result : null; + } + + public static function getProcedureDefinition(DatabaseInterface $dbi, string $db, string $name): ?string + { + $result = $dbi->fetchValue( + 'SHOW CREATE PROCEDURE ' . Util::backquote($db) . '.' . Util::backquote($name), + 'Create Procedure' + ); + + return is_string($result) ? $result : null; + } } diff --git a/libraries/classes/DatabaseInterface.php b/libraries/classes/DatabaseInterface.php index f2bccb3eb4..1f7454513d 100644 --- a/libraries/classes/DatabaseInterface.php +++ b/libraries/classes/DatabaseInterface.php @@ -1492,36 +1492,6 @@ class DatabaseInterface implements DbalInterface return $result; } - /** - * returns the definition of a specific PROCEDURE, FUNCTION, EVENT or VIEW - * - * @param string $db db name - * @param string $which PROCEDURE | FUNCTION | EVENT | VIEW - * @param string $name the procedure|function|event|view name - * @param int $link link type - * - * @return string|null the definition - */ - public function getDefinition( - string $db, - string $which, - string $name, - $link = self::CONNECT_USER - ): ?string { - $returnedField = [ - 'PROCEDURE' => 'Create Procedure', - 'FUNCTION' => 'Create Function', - 'EVENT' => 'Create Event', - 'VIEW' => 'Create View', - ]; - $query = 'SHOW CREATE ' . $which . ' ' - . Util::backquote($db) . '.' - . Util::backquote($name); - $result = $this->fetchValue($query, $returnedField[$which], $link); - - return is_string($result) ? $result : null; - } - /** * gets the current user with host * diff --git a/libraries/classes/Dbal/DbalInterface.php b/libraries/classes/Dbal/DbalInterface.php index 8b54935871..bf073afe9a 100644 --- a/libraries/classes/Dbal/DbalInterface.php +++ b/libraries/classes/Dbal/DbalInterface.php @@ -444,23 +444,6 @@ interface DbalInterface $link = DatabaseInterface::CONNECT_USER ): array; - /** - * returns the definition of a specific PROCEDURE, FUNCTION, EVENT or VIEW - * - * @param string $db db name - * @param string $which PROCEDURE | FUNCTION | EVENT | VIEW - * @param string $name the procedure|function|event|view name - * @param int $link link type - * - * @return string|null the definition - */ - public function getDefinition( - string $db, - string $which, - string $name, - $link = DatabaseInterface::CONNECT_USER - ): ?string; - /** * gets the current user with host * diff --git a/libraries/classes/Operations.php b/libraries/classes/Operations.php index a437aaf17a..3e88d03c7d 100644 --- a/libraries/classes/Operations.php +++ b/libraries/classes/Operations.php @@ -5,6 +5,8 @@ declare(strict_types=1); namespace PhpMyAdmin; use PhpMyAdmin\ConfigStorage\Relation; +use PhpMyAdmin\Database\Events; +use PhpMyAdmin\Database\Routines; use PhpMyAdmin\Database\Triggers; use PhpMyAdmin\Engines\Innodb; use PhpMyAdmin\Partitioning\Partition; @@ -59,7 +61,7 @@ class Operations if ($procedure_names) { foreach ($procedure_names as $procedure_name) { $this->dbi->selectDb($db); - $tmp_query = $this->dbi->getDefinition($db, 'PROCEDURE', $procedure_name); + $tmp_query = Routines::getProcedureDefinition($this->dbi, $db, $procedure_name); if ($tmp_query === null) { continue; } @@ -78,7 +80,7 @@ class Operations foreach ($function_names as $function_name) { $this->dbi->selectDb($db); - $tmp_query = $this->dbi->getDefinition($db, 'FUNCTION', $function_name); + $tmp_query = Routines::getFunctionDefinition($this->dbi, $db, $function_name); if ($tmp_query === null) { continue; } @@ -266,7 +268,7 @@ class Operations foreach ($event_names as $event_name) { $this->dbi->selectDb($db); - $tmp_query = $this->dbi->getDefinition($db, 'EVENT', $event_name); + $tmp_query = Events::getDefinition($this->dbi, $db, $event_name); // collect for later display $GLOBALS['sql_query'] .= "\n" . $tmp_query; $this->dbi->selectDb($_POST['newname']); diff --git a/libraries/classes/Plugins/Export/ExportSql.php b/libraries/classes/Plugins/Export/ExportSql.php index ca4e9413c2..0a48ed46d0 100644 --- a/libraries/classes/Plugins/Export/ExportSql.php +++ b/libraries/classes/Plugins/Export/ExportSql.php @@ -8,6 +8,8 @@ declare(strict_types=1); namespace PhpMyAdmin\Plugins\Export; use PhpMyAdmin\Charsets; +use PhpMyAdmin\Database\Events; +use PhpMyAdmin\Database\Routines; use PhpMyAdmin\Database\Triggers; use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\FieldMetadata; @@ -556,9 +558,9 @@ class ExportSql extends ExportPlugin } if ($type === 'FUNCTION') { - $definition = $GLOBALS['dbi']->getDefinition($db, 'FUNCTION', $routine); + $definition = Routines::getFunctionDefinition($GLOBALS['dbi'], $db, $routine); } else { - $definition = $GLOBALS['dbi']->getDefinition($db, 'PROCEDURE', $routine); + $definition = Routines::getProcedureDefinition($GLOBALS['dbi'], $db, $routine); } $createQuery = $this->replaceWithAliases($definition, $aliases, $db, '', $flag); @@ -1012,7 +1014,7 @@ class ExportSql extends ExportPlugin . $delimiter . $GLOBALS['crlf']; } - $eventDef = $GLOBALS['dbi']->getDefinition($db, 'EVENT', $eventName); + $eventDef = Events::getDefinition($GLOBALS['dbi'], $db, $eventName); if (! empty($eventDef) && $GLOBALS['cfg']['Export']['remove_definer_from_definitions']) { // remove definer clause from the event definition $parser = new Parser($eventDef); diff --git a/libraries/classes/Plugins/Export/ExportXml.php b/libraries/classes/Plugins/Export/ExportXml.php index 349fe0abc2..f31ecd496b 100644 --- a/libraries/classes/Plugins/Export/ExportXml.php +++ b/libraries/classes/Plugins/Export/ExportXml.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace PhpMyAdmin\Plugins\Export; +use PhpMyAdmin\Database\Events; +use PhpMyAdmin\Database\Routines; use PhpMyAdmin\Database\Triggers; use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Plugins\ExportPlugin; @@ -167,11 +169,11 @@ class ExportXml extends ExportPlugin $head .= ' ' . $GLOBALS['crlf']; if ($type === 'function') { - $definition = $GLOBALS['dbi']->getDefinition($db, 'FUNCTION', $name); + $definition = Routines::getFunctionDefinition($GLOBALS['dbi'], $db, $name); } elseif ($type === 'procedure') { - $definition = $GLOBALS['dbi']->getDefinition($db, 'PROCEDURE', $name); + $definition = Routines::getProcedureDefinition($GLOBALS['dbi'], $db, $name); } else { - $definition = $GLOBALS['dbi']->getDefinition($db, 'EVENT', $name); + $definition = Events::getDefinition($GLOBALS['dbi'], $db, $name); } // Do some formatting -- cgit v1.2.3