diff options
author | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-09-11 23:32:44 +0300 |
---|---|---|
committer | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-09-11 23:32:44 +0300 |
commit | e96fab654df405595b9f42c5a4eba04f80344cc1 (patch) | |
tree | 983c3c570d2e6ea5114d84123dc4fca20ec0b38c /libraries/classes | |
parent | 7975f5fb05befd63e10408a9c34a3ffff56a2515 (diff) |
Move DBI's getProceduresOrFunctions into Routines class
Splits the method into Routines::getFunctionNames and
Routines::getProcedureNames and adds tests.
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
Diffstat (limited to 'libraries/classes')
-rw-r--r-- | libraries/classes/Database/Routines.php | 40 | ||||
-rw-r--r-- | libraries/classes/DatabaseInterface.php | 27 | ||||
-rw-r--r-- | libraries/classes/Dbal/DbalInterface.php | 15 | ||||
-rw-r--r-- | libraries/classes/Operations.php | 4 | ||||
-rw-r--r-- | libraries/classes/Plugins/Export/ExportSql.php | 4 | ||||
-rw-r--r-- | libraries/classes/Plugins/Export/ExportXml.php | 4 |
6 files changed, 46 insertions, 48 deletions
diff --git a/libraries/classes/Database/Routines.php b/libraries/classes/Database/Routines.php index a8e9a0860a..9e41943696 100644 --- a/libraries/classes/Database/Routines.php +++ b/libraries/classes/Database/Routines.php @@ -1692,4 +1692,44 @@ class Routines return is_string($result) ? $result : null; } + + /** + * @return array<int, string> + * @psalm-return list<non-empty-string> + */ + 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;'); + $names = []; + foreach ($functions as $function) { + if ($function['Db'] !== $db || $function['Type'] !== 'FUNCTION' || $function['Name'] === '') { + continue; + } + + $names[] = $function['Name']; + } + + return $names; + } + + /** + * @return array<int, string> + * @psalm-return list<non-empty-string> + */ + 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;'); + $names = []; + foreach ($procedures as $procedure) { + if ($procedure['Db'] !== $db || $procedure['Type'] !== 'PROCEDURE' || $procedure['Name'] === '') { + continue; + } + + $names[] = $procedure['Name']; + } + + return $names; + } } diff --git a/libraries/classes/DatabaseInterface.php b/libraries/classes/DatabaseInterface.php index 1f7454513d..9fcc247508 100644 --- a/libraries/classes/DatabaseInterface.php +++ b/libraries/classes/DatabaseInterface.php @@ -1466,33 +1466,6 @@ class DatabaseInterface implements DbalInterface } /** - * returns an array of PROCEDURE or FUNCTION names for a db - * - * @param string $db db name - * @param string $which PROCEDURE | FUNCTION - * @param int $link link type - * - * @return string[] the procedure names or function names - */ - public function getProceduresOrFunctions( - string $db, - string $which, - $link = self::CONNECT_USER - ): array { - $shows = $this->fetchResult('SHOW ' . $which . ' STATUS;', null, null, $link); - $result = []; - foreach ($shows as $oneShow) { - if ($oneShow['Db'] != $db || $oneShow['Type'] != $which) { - continue; - } - - $result[] = $oneShow['Name']; - } - - return $result; - } - - /** * gets the current user with host * * @return string the current user i.e. user@host diff --git a/libraries/classes/Dbal/DbalInterface.php b/libraries/classes/Dbal/DbalInterface.php index bf073afe9a..e51a504c53 100644 --- a/libraries/classes/Dbal/DbalInterface.php +++ b/libraries/classes/Dbal/DbalInterface.php @@ -430,21 +430,6 @@ interface DbalInterface public function getWarnings($link = DatabaseInterface::CONNECT_USER): array; /** - * returns an array of PROCEDURE or FUNCTION names for a db - * - * @param string $db db name - * @param string $which PROCEDURE | FUNCTION - * @param int $link link type - * - * @return string[] the procedure names or function names - */ - public function getProceduresOrFunctions( - string $db, - string $which, - $link = DatabaseInterface::CONNECT_USER - ): array; - - /** * gets the current user with host * * @return string the current user i.e. user@host diff --git a/libraries/classes/Operations.php b/libraries/classes/Operations.php index 3e88d03c7d..18a8572387 100644 --- a/libraries/classes/Operations.php +++ b/libraries/classes/Operations.php @@ -57,7 +57,7 @@ class Operations */ public function runProcedureAndFunctionDefinitions($db): void { - $procedure_names = $this->dbi->getProceduresOrFunctions($db, 'PROCEDURE'); + $procedure_names = Routines::getProcedureNames($this->dbi, $db); if ($procedure_names) { foreach ($procedure_names as $procedure_name) { $this->dbi->selectDb($db); @@ -73,7 +73,7 @@ class Operations } } - $function_names = $this->dbi->getProceduresOrFunctions($db, 'FUNCTION'); + $function_names = Routines::getFunctionNames($this->dbi, $db); if (! $function_names) { return; } diff --git a/libraries/classes/Plugins/Export/ExportSql.php b/libraries/classes/Plugins/Export/ExportSql.php index 0a48ed46d0..ed29f70463 100644 --- a/libraries/classes/Plugins/Export/ExportSql.php +++ b/libraries/classes/Plugins/Export/ExportSql.php @@ -611,8 +611,8 @@ class ExportSql extends ExportPlugin $text = ''; $delimiter = '$$'; - $procedureNames = $GLOBALS['dbi']->getProceduresOrFunctions($db, 'PROCEDURE'); - $functionNames = $GLOBALS['dbi']->getProceduresOrFunctions($db, 'FUNCTION'); + $procedureNames = Routines::getProcedureNames($GLOBALS['dbi'], $db); + $functionNames = Routines::getFunctionNames($GLOBALS['dbi'], $db); if ($procedureNames || $functionNames) { $text .= $GLOBALS['crlf'] diff --git a/libraries/classes/Plugins/Export/ExportXml.php b/libraries/classes/Plugins/Export/ExportXml.php index f31ecd496b..473e8ed2ba 100644 --- a/libraries/classes/Plugins/Export/ExportXml.php +++ b/libraries/classes/Plugins/Export/ExportXml.php @@ -323,7 +323,7 @@ class ExportXml extends ExportPlugin $head .= $this->exportDefinitions( $GLOBALS['db'], 'function', - $GLOBALS['dbi']->getProceduresOrFunctions($GLOBALS['db'], 'FUNCTION') + Routines::getFunctionNames($GLOBALS['dbi'], $GLOBALS['db']) ); } @@ -331,7 +331,7 @@ class ExportXml extends ExportPlugin $head .= $this->exportDefinitions( $GLOBALS['db'], 'procedure', - $GLOBALS['dbi']->getProceduresOrFunctions($GLOBALS['db'], 'PROCEDURE') + Routines::getProcedureNames($GLOBALS['dbi'], $GLOBALS['db']) ); } |