Skip to content

Commit

Permalink
Refactor code of rte_routines.lib.php.
Browse files Browse the repository at this point in the history
Signed-off-by: Hugues Peccatte <hugues.peccatte@gmail.com>
  • Loading branch information
Tithugues committed Aug 11, 2015
1 parent 5024548 commit d7c9b2f
Showing 1 changed file with 137 additions and 118 deletions.
255 changes: 137 additions & 118 deletions libraries/rte/rte_routines.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,124 +90,7 @@ function PMA_RTN_handleEditor()
{
global $_GET, $_POST, $_REQUEST, $GLOBALS, $db, $errors;

if (! empty($_REQUEST['editor_process_add'])
|| ! empty($_REQUEST['editor_process_edit'])
) {
/**
* Handle a request to create/edit a routine
*/
$sql_query = '';
$routine_query = PMA_RTN_getQueryFromRequest();
if (! count($errors)) { // set by PMA_RTN_getQueryFromRequest()
// Execute the created query
if (! empty($_REQUEST['editor_process_edit'])) {
$isProcOrFunc = in_array(
$_REQUEST['item_original_type'],
array('PROCEDURE', 'FUNCTION')
);

if (!$isProcOrFunc) {
$errors[] = sprintf(
__('Invalid routine type: "%s"'),
htmlspecialchars($_REQUEST['item_original_type'])
);
} else {
// Backup the old routine, in case something goes wrong
$create_routine = $GLOBALS['dbi']->getDefinition(
$db, $_REQUEST['item_original_type'],
$_REQUEST['item_original_name']
);

$privilegesBackup = PMA_RTN_backupPrivileges();

$drop_routine = "DROP {$_REQUEST['item_original_type']} "
. PMA_Util::backquote($_REQUEST['item_original_name'])
. ";\n";
$result = $GLOBALS['dbi']->tryQuery($drop_routine);
if (! $result) {
$errors[] = sprintf(
__('The following query has failed: "%s"'),
htmlspecialchars($drop_routine)
)
. '<br />'
. __('MySQL said: ') . $GLOBALS['dbi']->getError(null);
} else {
list($newErrors, $message) = PMA_RTN_createRoutine(
$routine_query,
$create_routine,
$privilegesBackup
);
if (empty($newErrors)) {
$sql_query = $drop_routine . $sql_query;
} else {
$errors = array_merge($errors, $newErrors);
}
unset($newErrors);
if (null === $message) {
unset($message);
}
}
}
} else {
// 'Add a new routine' mode
$result = $GLOBALS['dbi']->tryQuery($routine_query);
if (! $result) {
$errors[] = sprintf(
__('The following query has failed: "%s"'),
htmlspecialchars($routine_query)
)
. '<br /><br />'
. __('MySQL said: ') . $GLOBALS['dbi']->getError(null);
} else {
$message = PMA_Message::success(
__('Routine %1$s has been created.')
);
$message->addParam(
PMA_Util::backquote($_REQUEST['item_name'])
);
$sql_query = $routine_query;
}
}
}

if (count($errors)) {
$message = PMA_Message::error(
__(
'One or more errors have occurred while'
. ' processing your request:'
)
);
$message->addString('<ul>');
foreach ($errors as $string) {
$message->addString('<li>' . $string . '</li>');
}
$message->addString('</ul>');
}

$output = PMA_Util::getMessage($message, $sql_query);
if ($GLOBALS['is_ajax_request']) {
$response = PMA_Response::getInstance();
if ($message->isSuccess()) {
$routines = $GLOBALS['dbi']->getRoutines(
$db, $_REQUEST['item_type'], $_REQUEST['item_name']
);
$routine = $routines[0];
$response->addJSON(
'name',
htmlspecialchars(
/*overload*/mb_strtoupper($_REQUEST['item_name'])
)
);
$response->addJSON('new_row', PMA_RTN_getRowForList($routine));
$response->addJSON('insert', ! empty($routine));
$response->addJSON('message', $output);
} else {
$response->isSuccess(false);
$response->addJSON('message', $output);
}
exit;
}
}
$errors = PMA_RTN_handleRequestCreateOrEdit($errors, $db);

/**
* Display a form used to add/edit a routine, if necessary
Expand Down Expand Up @@ -286,6 +169,142 @@ function PMA_RTN_handleEditor()
}
}

/**
* Handle request to create or edit a routine
*
* @param array $errors Errors
* @param string $db DB name
*
* @return array
*/
function PMA_RTN_handleRequestCreateOrEdit($errors, $db)
{
if (empty($_REQUEST['editor_process_add'])
&& empty($_REQUEST['editor_process_edit'])
) {
return $errors;
}

$sql_query = '';
$routine_query = PMA_RTN_getQueryFromRequest();
if (!count($errors)) { // set by PMA_RTN_getQueryFromRequest()
// Execute the created query
if (!empty($_REQUEST['editor_process_edit'])) {
$isProcOrFunc = in_array(
$_REQUEST['item_original_type'],
array('PROCEDURE', 'FUNCTION')
);

if (!$isProcOrFunc) {
$errors[] = sprintf(
__('Invalid routine type: "%s"'),
htmlspecialchars($_REQUEST['item_original_type'])
);
} else {
// Backup the old routine, in case something goes wrong
$create_routine = $GLOBALS['dbi']->getDefinition(
$db,
$_REQUEST['item_original_type'],
$_REQUEST['item_original_name']
);

$privilegesBackup = PMA_RTN_backupPrivileges();

$drop_routine = "DROP {$_REQUEST['item_original_type']} "
. PMA_Util::backquote($_REQUEST['item_original_name'])
. ";\n";
$result = $GLOBALS['dbi']->tryQuery($drop_routine);
if (!$result) {
$errors[] = sprintf(
__('The following query has failed: "%s"'),
htmlspecialchars($drop_routine)
)
. '<br />'
. __('MySQL said: ') . $GLOBALS['dbi']->getError(null);
} else {
list($newErrors, $message) = PMA_RTN_createRoutine(
$routine_query,
$create_routine,
$privilegesBackup
);
if (empty($newErrors)) {
$sql_query = $drop_routine . $sql_query;
} else {
$errors = array_merge($errors, $newErrors);
}
unset($newErrors);
if (null === $message) {
unset($message);
}
}
}
} else {
// 'Add a new routine' mode
$result = $GLOBALS['dbi']->tryQuery($routine_query);
if (!$result) {
$errors[] = sprintf(
__('The following query has failed: "%s"'),
htmlspecialchars($routine_query)
)
. '<br /><br />'
. __('MySQL said: ') . $GLOBALS['dbi']->getError(null);
} else {
$message = PMA_Message::success(
__('Routine %1$s has been created.')
);
$message->addParam(
PMA_Util::backquote($_REQUEST['item_name'])
);
$sql_query = $routine_query;
}
}
}

if (count($errors)) {
$message = PMA_Message::error(
__(
'One or more errors have occurred while'
. ' processing your request:'
)
);
$message->addString('<ul>');
foreach ($errors as $string) {
$message->addString('<li>' . $string . '</li>');
}
$message->addString('</ul>');
}

$output = PMA_Util::getMessage($message, $sql_query);
if (!$GLOBALS['is_ajax_request']) {
return $errors;
}

$response = PMA_Response::getInstance();
if (!$message->isSuccess()) {
$response->isSuccess(false);
$response->addJSON('message', $output);
exit;
}

$routines = $GLOBALS['dbi']->getRoutines(
$db,
$_REQUEST['item_type'],
$_REQUEST['item_name']
);
$routine = $routines[0];
$response->addJSON(
'name',
htmlspecialchars(
/*overload*/
mb_strtoupper($_REQUEST['item_name'])
)
);
$response->addJSON('new_row', PMA_RTN_getRowForList($routine));
$response->addJSON('insert', !empty($routine));
$response->addJSON('message', $output);
exit;
}

/**
* Backup the privileges
*
Expand Down

0 comments on commit d7c9b2f

Please sign in to comment.