Skip to content

Commit

Permalink
Extract add prefix from db structure index method
Browse files Browse the repository at this point in the history
Related to #16173.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Jun 21, 2020
1 parent 7ce0ae2 commit b91e978
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
42 changes: 29 additions & 13 deletions libraries/classes/Controllers/Database/StructureController.php
Expand Up @@ -394,15 +394,6 @@ public function multiSubmitAction(): void

for ($i = 0; $i < $selectedCount; $i++) {
switch ($query_type) {
case 'add_prefix_tbl':
$newTableName = $_POST['add_prefix'] . $selected[$i];
// ADD PREFIX TO TABLE NAME
$aQuery = 'ALTER TABLE '
. Util::backquote($selected[$i])
. ' RENAME '
. Util::backquote($newTableName);
break;

case 'replace_prefix_tbl':
$current = $selected[$i];
$subFromPrefix = mb_substr(
Expand Down Expand Up @@ -1421,10 +1412,7 @@ public function addPrefix(): void
return;
}

$params = [
'query_type' => 'add_prefix_tbl',
'db' => $db,
];
$params = ['db' => $db];
foreach ($selected as $selectedValue) {
$params['selected'][] = $selectedValue;
}
Expand Down Expand Up @@ -1942,4 +1930,32 @@ public function emptyTable(): void

$this->index();
}

public function addPrefixTable(): void
{
global $db, $message, $sql_query;

$selected = $_POST['selected'] ?? [];

$sql_query = '';
$selectedCount = count($selected);

for ($i = 0; $i < $selectedCount; $i++) {
$newTableName = $_POST['add_prefix'] . $selected[$i];
$aQuery = 'ALTER TABLE ' . Util::backquote($selected[$i])
. ' RENAME ' . Util::backquote($newTableName);

$sql_query .= $aQuery . ';' . "\n";
$this->dbi->selectDb($db);
$this->dbi->query($aQuery);
}

$message = Message::success();

if (! empty($_POST['message'])) {
$_POST['message'] = $message;
}

$this->index();
}
}
1 change: 1 addition & 0 deletions libraries/routes.php
Expand Up @@ -139,6 +139,7 @@
$routes->addGroup('/structure', static function (RouteCollector $routes) {
$routes->addRoute(['GET', 'POST'], '', [StructureController::class, 'index']);
$routes->post('/add-prefix', [StructureController::class, 'addPrefix']);
$routes->post('/add-prefix-table', [StructureController::class, 'addPrefixTable']);
$routes->post('/analyze-table', [StructureController::class, 'analyzeTable']);
$routes->post('/central-columns-add', [StructureController::class, 'centralColumnsAdd']);
$routes->post('/central-columns-make-consistent', [
Expand Down
3 changes: 1 addition & 2 deletions templates/database/structure/add_prefix.twig
@@ -1,4 +1,4 @@
<form id="ajax_form" action="{{ url('/database/structure') }}" method="post">
<form id="ajax_form" action="{{ url('/database/structure/add-prefix-table') }}" method="post">
{{ get_hidden_inputs(url_params) }}

<fieldset class = "input">
Expand All @@ -12,5 +12,4 @@
<tr>
</table>
</fieldset>
<input type="hidden" name="mult_btn" value="{% trans 'Yes' %}">
</form>

0 comments on commit b91e978

Please sign in to comment.