Merge pull request #19298 from kamil-tekiela/Routines-getParameters

Move getParameters() from sql-parser
This commit is contained in:
Maurício Meneghini Fauth 2024-09-23 23:30:56 -03:00 committed by GitHub
commit dc889691d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 75 additions and 30 deletions

View File

@ -5912,12 +5912,7 @@ parameters:
-
message: "#^Cannot access offset 'value' on mixed\\.$#"
count: 1
path: src/Database/Routines.php
-
message: "#^Cannot access offset int\\<0, max\\> on array\\<int, array\\|string\\|null\\>\\|int\\.$#"
count: 1
count: 2
path: src/Database/Routines.php
-
@ -5932,22 +5927,22 @@ parameters:
-
message: "#^Cannot access property \\$name on PhpMyAdmin\\\\SqlParser\\\\Components\\\\DataType\\|null\\.$#"
count: 1
count: 2
path: src/Database/Routines.php
-
message: "#^Cannot access property \\$options on PhpMyAdmin\\\\SqlParser\\\\Components\\\\DataType\\|null\\.$#"
count: 1
count: 2
path: src/Database/Routines.php
-
message: "#^Cannot access property \\$options on PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
count: 1
count: 2
path: src/Database/Routines.php
-
message: "#^Cannot access property \\$parameters on PhpMyAdmin\\\\SqlParser\\\\Components\\\\DataType\\|null\\.$#"
count: 1
count: 3
path: src/Database/Routines.php
-
@ -5955,6 +5950,11 @@ parameters:
count: 1
path: src/Database/Routines.php
-
message: "#^Cannot assign new offset to array\\<int\\<0, max\\>, mixed\\>\\|string\\.$#"
count: 1
path: src/Database/Routines.php
-
message: "#^Cannot call method has\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
count: 1
@ -6045,6 +6045,11 @@ parameters:
count: 1
path: src/Database/Routines.php
-
message: "#^Parameter \\#2 \\$array of function implode expects array\\|null, array\\<int\\<0, max\\>, mixed\\>\\|string given\\.$#"
count: 1
path: src/Database/Routines.php
-
message: "#^Parameter \\#2 \\$itemParamDir of method PhpMyAdmin\\\\Database\\\\Routines\\:\\:processParamsAndBuild\\(\\) expects array\\<string\\>, array given\\.$#"
count: 1

View File

@ -4862,9 +4862,11 @@
<MixedArgumentTypeCoercion>
<code><![CDATA[$options]]></code>
<code><![CDATA[$options]]></code>
<code><![CDATA[$retval['item_param_opts_num'][$idx]]]></code>
<code><![CDATA[$value]]></code>
</MixedArgumentTypeCoercion>
<MixedArrayAccess>
<code><![CDATA[$opt['value']]]></code>
<code><![CDATA[$opt['value']]]></code>
<code><![CDATA[$routine['item_param_dir'][$i]]]></code>
<code><![CDATA[$routine['item_param_dir'][$i]]]></code>
@ -4922,8 +4924,10 @@
<code><![CDATA[$GLOBALS['errors']]]></code>
<code><![CDATA[$i]]></code>
<code><![CDATA[$opt]]></code>
<code><![CDATA[$opt]]></code>
<code><![CDATA[$options[]]]></code>
<code><![CDATA[$retval['item_definer']]]></code>
<code><![CDATA[$retval['item_param_opts_num'][$idx][]]]></code>
<code><![CDATA[$string]]></code>
<code><![CDATA[$value]]></code>
<code><![CDATA[$value]]></code>
@ -4964,9 +4968,6 @@
<code><![CDATA[$itemType]]></code>
<code><![CDATA[$itemType]]></code>
</PossiblyInvalidArgument>
<PossiblyInvalidArrayAccess>
<code><![CDATA[$params['dir'][$i]]]></code>
</PossiblyInvalidArrayAccess>
<PossiblyInvalidCast>
<code><![CDATA[$_POST['item_comment']]]></code>
<code><![CDATA[$_POST['item_isdeterministic']]]></code>
@ -4995,18 +4996,24 @@
<PossiblyNullArgument>
<code><![CDATA[$GLOBALS['message']]]></code>
<code><![CDATA[$createRoutine]]></code>
<code><![CDATA[$param->type->parameters]]></code>
<code><![CDATA[$routine['SPECIFIC_NAME']]]></code>
<code><![CDATA[$routine['SPECIFIC_NAME']]]></code>
<code><![CDATA[$stmt->return->parameters]]></code>
</PossiblyNullArgument>
<PossiblyNullIterator>
<code><![CDATA[$GLOBALS['errors']]]></code>
<code><![CDATA[$param->type->options->options]]></code>
<code><![CDATA[$stmt->return->options->options]]></code>
</PossiblyNullIterator>
<PossiblyNullOperand>
<code><![CDATA[$_POST['item_original_type']]]></code>
</PossiblyNullOperand>
<PossiblyNullPropertyFetch>
<code><![CDATA[$param->type->name]]></code>
<code><![CDATA[$param->type->options]]></code>
<code><![CDATA[$param->type->options->options]]></code>
<code><![CDATA[$param->type->parameters]]></code>
<code><![CDATA[$stmt->return->name]]></code>
<code><![CDATA[$stmt->return->options]]></code>
<code><![CDATA[$stmt->return->options->options]]></code>

View File

@ -14,7 +14,6 @@ use PhpMyAdmin\Query\Generator as QueryGenerator;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\Utils\Routine as RoutineUtils;
use PhpMyAdmin\UserPrivileges;
use PhpMyAdmin\Util;
@ -459,15 +458,8 @@ class Routines
$body = (string) $routine['ROUTINE_DEFINITION'];
}
$params = RoutineUtils::getParameters($stmt);
$retval['item_num_params'] = $params['num'];
$retval['item_param_dir'] = $params['dir'];
$retval['item_param_name'] = $params['name'];
$retval['item_param_type'] = $params['type'];
$retval['item_param_length'] = $params['length'];
$retval['item_param_length_arr'] = $params['length_arr'];
$retval['item_param_opts_num'] = $params['opts'];
$retval['item_param_opts_text'] = $params['opts'];
$retval = array_merge($retval, $this->getParameters($stmt));
$retval['item_param_opts_text'] = $retval['item_param_opts_num'];
// Get extra data
if (! $all) {
@ -518,6 +510,49 @@ class Routines
return $retval;
}
/**
* Gets the parameters of a routine from the parse tree.
*
* @param CreateStatement $statement the statement to be processed
*
* @return array<string, int|array<int, mixed[]|string|null>>
*/
private function getParameters(CreateStatement $statement): array
{
$retval = [
'item_num_params' => 0,
'item_param_dir' => [],
'item_param_name' => [],
'item_param_type' => [],
'item_param_length' => [],
'item_param_length_arr' => [],
'item_param_opts_num' => [],
];
if ($statement->parameters !== null) {
$idx = 0;
foreach ($statement->parameters as $param) {
$retval['item_param_dir'][$idx] = $param->inOut;
$retval['item_param_name'][$idx] = $param->name;
$retval['item_param_type'][$idx] = $param->type->name;
$retval['item_param_length'][$idx] = implode(',', $param->type->parameters);
$retval['item_param_length_arr'][$idx] = $param->type->parameters;
$retval['item_param_opts_num'][$idx] = [];
foreach ($param->type->options->options as $opt) {
$retval['item_param_opts_num'][$idx][] = is_string($opt) ?
$opt : $opt['value'];
}
$retval['item_param_opts_num'][$idx] = implode(' ', $retval['item_param_opts_num'][$idx]);
++$idx;
}
$retval['item_num_params'] = $idx;
}
return $retval;
}
/**
* Creates one row for the parameter table used in the routine editor.
*
@ -1169,18 +1204,16 @@ class Routines
$executeAction = '';
if ($definition !== null) {
if ($definition !== null && $hasExecutePrivilege) {
$parser = new Parser('DELIMITER $$' . "\n" . $definition);
/** @var CreateStatement $stmt */
$stmt = $parser->statements[0];
$params = RoutineUtils::getParameters($stmt);
if ($hasExecutePrivilege) {
$executeAction = 'execute_routine';
for ($i = 0; $i < $params['num']; $i++) {
if ($routine->type === 'PROCEDURE' && $params['dir'][$i] === 'OUT') {
$executeAction = 'execute_routine';
if ($stmt->parameters !== null) {
foreach ($stmt->parameters as $param) {
if ($routine->type === 'PROCEDURE' && $param->inOut === 'OUT') {
continue;
}