Skip to content

Commit

Permalink
Merge pull request #1288 from smita786/gsoc_automated_normalizaion
Browse files Browse the repository at this point in the history
Gsoc automated normalizaion: new step to 1NF that move the repeating group of column to new table
  • Loading branch information
lem9 committed Jul 17, 2014
2 parents d56aa56 + 54a413f commit 764b0bc
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 8 deletions.
5 changes: 5 additions & 0 deletions js/messages.php
Expand Up @@ -317,6 +317,11 @@
$js_messages['strHidePd'] = __('Hide partial dependencies list');
$js_messages['strWaitForPd'] = __('Sit tight! It may take few seconds depending on data size and column count of the table.');
$js_messages['strStep'] = __('Step');
$js_messages['strMoveRepeatingGroup'] = '<ol><b>' . __('The following actions will be performed:') . '</b>'
. '<li>' . __('DROP columns %1s from the table %2s') . '</li>'
. '<li>' . __('Create the following new table') . '</li>';
$js_messages['strNewTablePlaceholder'] = 'Enter new table name';
$js_messages['strNewColumnPlaceholder'] = 'Enter column name';

/* For server_variables.js */
$js_messages['strSave'] = __('Save');
Expand Down
86 changes: 84 additions & 2 deletions js/normalization.js
Expand Up @@ -12,7 +12,7 @@
*/

var normalizeto = '1nf';

var primary_key;
function appendHtmlColumnsList()
{
$.get(
Expand Down Expand Up @@ -70,6 +70,30 @@ function goToFinish1NF()
$('.tblFooters').html('');
}

function goToStep4()
{
$.post(
"normalization.php",
{
"token": PMA_commonParams.get('token'),
"ajax_request": true,
"db": PMA_commonParams.get('db'),
"table": PMA_commonParams.get('table'),
"step4": true
}, function(data) {
$("#mainContent legend").html(data.legendText);
$("#mainContent h4").html(data.headText);
$("#mainContent p").html(data.subText);
$("#mainContent #extra").html(data.extra);
$("#mainContent #newCols").html('');
$('.tblFooters').html('');
for(var pk in primary_key) {
$("#extra input[value='"+primary_key[pk]+"']").attr("disabled","disabled");
}
}
);
}

function goToStep3()
{
$.post(
Expand All @@ -87,6 +111,10 @@ function goToStep3()
$("#mainContent #extra").html(data.extra);
$("#mainContent #newCols").html('');
$('.tblFooters').html('');
primary_key = $.parseJSON(data.primary_key);
for(var pk in primary_key) {
$("#extra input[value='"+primary_key[pk]+"']").attr("disabled","disabled");
}
}
);
}
Expand Down Expand Up @@ -238,7 +266,44 @@ function processPartialDependancies(primary_key)
goTo2NFStep2(pd, primary_key);
return false;
}

function moveRepeatingGroup(repeatingCols) {
newTable = $("input[name=repeatGroupTable]").val();
newColumn = $("input[name=repeatGroupColumn]").val();
if (!newTable) {
$("input[name=repeatGroupTable]").focus();
return false;
}
if (!newColumn) {
$("input[name=repeatGroupColumn]").focus();
return false;
}
datastring = {"token": PMA_commonParams.get('token'),
"ajax_request": true,
"db": PMA_commonParams.get('db'),
"table": PMA_commonParams.get('table'),
"repeatingColumns": repeatingCols,
"newTable":newTable,
"newColumn":newColumn,
"primary_columns":primary_key.toString()
};
$.ajax({
type: "POST",
url: "normalization.php",
data: datastring,
async:false,
success: function(data) {
if (data.success === true) {
if(data.queryError === false) {
goToStep3();
}
PMA_ajaxShowMessage(data.message, false);
$("#pma_navigation_reload").click();
} else {
PMA_ajaxShowMessage(data.error, false);
}
}
});
}
AJAX.registerTeardown('normalization.js', function () {
$("#extra").off("click", "#selectNonAtomicCol");
$("#splitGo").unbind('click');
Expand Down Expand Up @@ -397,7 +462,24 @@ AJAX.registerOnload('normalization.js', function() {
}
);
});
$("#extra").on("click", "#moveRepeatingGroup", function() {
var repeatingCols = '';
$("#extra input[type=checkbox]:checked").each(function() {
repeatingCols += $(this).val() + ', ';
});

if (repeatingCols !== '') {
newColName = $("#extra input[type=checkbox]:checked:first").val();
repeatingCols = repeatingCols.slice(0, -2);
confirmStr = $.sprintf(PMA_messages.strMoveRepeatingGroup, escapeHtml(repeatingCols), escapeHtml(PMA_commonParams.get('table')));
confirmStr += '<input type="text" name="repeatGroupTable" placeholder="'+PMA_messages.strNewTablePlaceholder+'"/>'+
'( '+escapeHtml(primary_key.toString())+', <input type="text" name="repeatGroupColumn" placeholder="'+PMA_messages.strNewColumnPlaceholder+'" value="'+escapeHtml(newColName)+'">)'+
'</ol>';
$("#newCols").html(confirmStr);
$('.tblFooters').html('<input type="submit" value="'+PMA_messages.strCancel+'" onclick="$(\'#newCols\').html(\'\');$(\'#extra input[type=checkbox]\').removeAttr(\'checked\')"/>'+
'<input type="submit" value="'+PMA_messages.strGo+'" onclick="moveRepeatingGroup(\''+repeatingCols+'\')"/>');
}
});
$("#mainContent p").on("click", "#createPrimaryKey", function(event) {
event.preventDefault();
var url = { create_index: 1,
Expand Down
117 changes: 113 additions & 4 deletions libraries/normalization.lib.php
Expand Up @@ -195,16 +195,16 @@ function PMA_getHtmlContentsFor1NFStep2($db, $table)
}

/**
* build the html contents of various html elements in step 1.3
* build the html contents of various html elements in step 1.4
*
* @param string $db current database
* @param string $table current table
*
* @return HTML contents for step 1.3
* @return HTML contents for step 1.4
*/
function PMA_getHtmlContentsFor1NFStep3($db, $table)
function PMA_getHtmlContentsFor1NFStep4($db, $table)
{
$step = 3;
$step = 4;
$stepTxt = __('Remove redundant columns');
$legendText = __('Step 1.') . $step . " " . $stepTxt;
$headText = __(
Expand All @@ -229,6 +229,52 @@ function PMA_getHtmlContentsFor1NFStep3($db, $table)
);
return $res;
}

/**
* build the html contents of various html elements in step 1.3
*
* @param string $db current database
* @param string $table current table
*
* @return HTML contents for step 1.3
*/
function PMA_getHtmlContentsFor1NFStep3($db, $table)
{
$step = 3;
$stepTxt = __('Move repeating groups');
$legendText = __('Step 1.') . $step . " " . $stepTxt;
$headText = __(
"Do you have group of two or more columns that are closely "
. " related and are all repeating same attribute? For example, "
. "if a table that records data on book, has "
. " columns book_id, author1, author2, author3 and so on which form a "
. "repeating group. In this case new table (book_id, author) should "
. "be created. another examples can be"
. " phone1, phone2, phone3 and so on Or picture1, picture2 etc"
);
$subText = __(
"Check the columns which form a repeating group. "
. "If no such group, click on 'No repeating group'"
);
$extra = PMA_getHtmlForColumnsList($db, $table, 'all', "checkbox") . "</br>"
. '<input type="submit" id="moveRepeatingGroup" value="'
. __('Done') . '"/>'
. '<input type="submit" value="' . __('No repeating group')
. '" onclick="goToStep4();"'
. '/>';
$primary = PMA_Index::getPrimary($table, $db);
$primarycols = $primary->getColumns();
$pk = array();
foreach ($primarycols as $col) {
$pk[] = $col->getName();
}
$res = array(
'legendText'=>$legendText, 'headText'=>$headText,
'subText'=>$subText, 'extra'=>$extra, 'primary_key'=> json_encode($pk)
);
return $res;
}

/**
* build html contents for 2NF step 2.1
*
Expand Down Expand Up @@ -420,6 +466,69 @@ function PMA_createNewTablesFor2NF($partialDependencies, $tablesName, $table, $d
'queryError'=>$error, 'extra'=>$message
);
}

/**
* move the repeating group of columns to a new table
*
* @param string $repeatingColumns comma separated list of repeating group columns
* @param string $primary_columns comma separated list of column in primary key
* of $table
* @param string $newTable name of the new table to be created
* @param string $newColumn name of the new column in the new table
* @param string $table current table
* @param string $db current database
*
* @return array
*/
function PMA_moveRepeatingGroup(
$repeatingColumns, $primary_columns, $newTable, $newColumn, $table, $db
) {
$repeatingColumnsArr = (array)PMA_Util::backquote(
explode(', ', $repeatingColumns)
);
$primary_columns = implode(
',', PMA_Util::backquote(explode(',', $primary_columns))
);
$query1 = 'CREATE TABLE ' . PMA_Util::backquote($newTable);
$query2 = 'ALTER TABLE ' . PMA_Util::backquote($table);
$message = PMA_Message::success(
sprintf(
__('Selected repeating group has been moved the to the table \'%s\''),
htmlspecialchars($table)
)
);
$first = true;
$error = false;
foreach ($repeatingColumnsArr as $repeatingColumn) {
if (!$first) {
$query1 .= ' UNION ';
}
$first = false;
$query1 .= ' SELECT ' . $primary_columns . ',' . $repeatingColumn
. ' as ' . PMA_Util::backquote($newColumn)
. ' FROM ' . PMA_Util::backquote($table);
$query2 .= ' DROP ' . $repeatingColumn . ',';
}
$query2 = trim($query2, ',');
$queries = array($query1, $query2);
$GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
foreach ($queries as $query) {
if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['userlink'])) {
$message = PMA_Message::error(__('Error in processing!'));
$message->addMessage('<br /><br />');
$message->addMessage(
PMA_Message::rawError(
$GLOBALS['dbi']->getError($GLOBALS['userlink'])
)
);
$error = true;
break;
}
}
return array(
'queryError'=>$error, 'message'=>$message
);
}
/**
* get html for options to normalize table
*
Expand Down
5 changes: 3 additions & 2 deletions libraries/tbl_indexes.lib.php
Expand Up @@ -192,8 +192,9 @@ function PMA_getNumberOfFieldsForForm($index)
if (isset($_REQUEST['index']) && is_array($_REQUEST['index'])) {
// coming already from form
$add_fields
= count($_REQUEST['index']['columns']['names'])
- $index->getColumnCount();
= isset($_REQUEST['index']['columns'])?
count($_REQUEST['index']['columns']['names'])
- $index->getColumnCount():0;
if (isset($_REQUEST['add_fields'])) {
$add_fields += $_REQUEST['added_fields'];
}
Expand Down
14 changes: 14 additions & 0 deletions normalization.php
Expand Up @@ -84,6 +84,17 @@
$response->addJSON($res);
exit;
}
if (isset($_POST['repeatingColumns'])) {
$repeatingColumns = $_POST['repeatingColumns'];
$newTable = $_POST['newTable'];
$newColumn = $_POST['newColumn'];
$primary_columns = $_POST['primary_columns'];
$res = PMA_moveRepeatingGroup(
$repeatingColumns, $primary_columns, $newTable, $newColumn, $table, $db
);
$response->addJSON($res);
exit;
}
if (isset($_REQUEST['step1'])) {
$html = PMA_getHtmlFor1NFStep1($db, $table, $normalForm);
$response->addHTML($html);
Expand All @@ -93,6 +104,9 @@
} else if (isset($_REQUEST['step3'])) {
$res = PMA_getHtmlContentsFor1NFStep3($db, $table);
$response->addJSON($res);
} else if (isset ($_REQUEST['step4'])) {
$res = PMA_getHtmlContentsFor1NFStep4($db, $table);
$response->addJSON($res);
} else if (isset($_REQUEST['step']) && $_REQUEST['step'] == 2.1) {
$res = PMA_getHtmlFor2NFstep1($db, $table);
$response->addJSON($res);
Expand Down

0 comments on commit 764b0bc

Please sign in to comment.