Merge pull request #13655 from mauriciofauth/normalization

Refactor normalization functions to static methods
This commit is contained in:
Maurício Meneghini Fauth 2017-09-06 07:22:53 -03:00 committed by GitHub
commit 503a711af2
4 changed files with 1065 additions and 1050 deletions

View File

@ -0,0 +1,997 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* set of functions used for normalization
*
* @package PhpMyAdmin
*/
namespace PhpMyAdmin;
use PhpMyAdmin\Index;
use PhpMyAdmin\Message;
use PhpMyAdmin\Relation;
use PhpMyAdmin\Template;
use PhpMyAdmin\Transformations;
use PhpMyAdmin\Url;
use PhpMyAdmin\Util;
/**
* PhpMyAdmin\Normalization class
*
* @package PhpMyAdmin
*/
class Normalization
{
/**
* build the html for columns of $colTypeCategory category
* in form of given $listType in a table
*
* @param string $db current database
* @param string $table current table
* @param string $colTypeCategory supported all|Numeric|String|Spatial
* |Date and time using the _pgettext() format
* @param string $listType type of list to build, supported dropdown|checkbox
*
* @return string HTML for list of columns in form of given list types
*/
public static function getHtmlForColumnsList(
$db, $table, $colTypeCategory='all', $listType='dropdown'
) {
$columnTypeList = array();
if ($colTypeCategory != 'all') {
$types = $GLOBALS['PMA_Types']->getColumns();
$columnTypeList = $types[$colTypeCategory];
}
$GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
$columns = $GLOBALS['dbi']->getColumns(
$db, $table, null,
true, $GLOBALS['userlink']
);
$type = "";
$selectColHtml = "";
foreach ($columns as $column => $def) {
if (isset($def['Type'])) {
$extracted_columnspec = Util::extractColumnSpec($def['Type']);
$type = $extracted_columnspec['type'];
}
if (empty($columnTypeList)
|| in_array(mb_strtoupper($type), $columnTypeList)
) {
if ($listType == 'checkbox') {
$selectColHtml .= '<input type="checkbox" value="'
. htmlspecialchars($column) . '"/>'
. htmlspecialchars($column) . ' [ '
. htmlspecialchars($def['Type']) . ' ]</br>';
} else {
$selectColHtml .= '<option value="' . htmlspecialchars($column) . ''
. '">' . htmlspecialchars($column)
. ' [ ' . htmlspecialchars($def['Type']) . ' ]'
. '</option>';
}
}
}
return $selectColHtml;
}
/**
* get the html of the form to add the new column to given table
*
* @param integer $num_fields number of columns to add
* @param string $db current database
* @param string $table current table
* @param array $columnMeta array containing default values for the fields
*
* @return string HTML
*/
public static function getHtmlForCreateNewColumn(
$num_fields, $db, $table, $columnMeta=array()
) {
$cfgRelation = Relation::getRelationsParam();
$content_cells = array();
$available_mime = array();
$mime_map = array();
if ($cfgRelation['mimework'] && $GLOBALS['cfg']['BrowseMIME']) {
$mime_map = Transformations::getMIME($db, $table);
$available_mime = Transformations::getAvailableMIMEtypes();
}
$comments_map = Relation::getComments($db, $table);
for ($columnNumber = 0; $columnNumber < $num_fields; $columnNumber++) {
$content_cells[$columnNumber] = array(
'columnNumber' => $columnNumber,
'columnMeta' => $columnMeta,
'type_upper' => '',
'length_values_input_size' => 8,
'length' => '',
'extracted_columnspec' => array(),
'submit_attribute' => null,
'comments_map' => $comments_map,
'fields_meta' => null,
'is_backup' => true,
'move_columns' => array(),
'cfgRelation' => $cfgRelation,
'available_mime' => isset($available_mime)?$available_mime:array(),
'mime_map' => $mime_map
);
}
return Template::get(
'columns_definitions/table_fields_definitions'
)
->render(
array(
'is_backup' => true,
'fields_meta' => null,
'mimework' => $cfgRelation['mimework'],
'content_cells' => $content_cells
)
);
}
/**
* build the html for step 1.1 of normalization
*
* @param string $db current database
* @param string $table current table
* @param string $normalizedTo up to which step normalization will go,
* possible values 1nf|2nf|3nf
*
* @return string HTML for step 1.1
*/
public static function getHtmlFor1NFStep1($db, $table, $normalizedTo)
{
$step = 1;
$stepTxt = __('Make all columns atomic');
$html = "<h3 class='center'>"
. __('First step of normalization (1NF)') . "</h3>";
$html .= "<div id='mainContent' data-normalizeto='" . $normalizedTo . "'>" .
"<fieldset>" .
"<legend>" . __('Step 1.') . $step . " " . $stepTxt . "</legend>" .
"<h4>" . __(
'Do you have any column which can be split into more than'
. ' one column? '
. 'For example: address can be split into street, city, country and zip.'
)
. "</br>(<a class='central_columns_dialog' data-maxrows='25' "
. "data-pick=false href='#'> "
. __(
'Show me the central list of columns that are not already in this table'
) . " </a>)</h4>"
. "<p class='cm-em'>" . __(
'Select a column which can be split into more '
. 'than one (on select of \'no such column\', it\'ll move to next step).'
)
. "</p>"
. "<div id='extra'>"
. "<select id='selectNonAtomicCol' name='makeAtomic'>"
. '<option selected="selected" disabled="disabled">'
. __('Select one…') . "</option>"
. "<option value='no_such_col'>" . __('No such column') . "</option>"
. self::getHtmlForColumnsList(
$db,
$table,
_pgettext('string types', 'String')
)
. "</select>"
. "<span>" . __('split into ')
. "</span><input id='numField' type='number' value='2'>"
. "<input type='submit' id='splitGo' value='" . __('Go') . "'/></div>"
. "<div id='newCols'></div>"
. "</fieldset><fieldset class='tblFooters'>"
. "</fieldset>"
. "</div>";
return $html;
}
/**
* build the html contents of various html elements in step 1.2
*
* @param string $db current database
* @param string $table current table
*
* @return string HTML contents for step 1.2
*/
public static function getHtmlContentsFor1NFStep2($db, $table)
{
$step = 2;
$stepTxt = __('Have a primary key');
$primary = Index::getPrimary($table, $db);
$hasPrimaryKey = "0";
$legendText = __('Step 1.') . $step . " " . $stepTxt;
$extra = '';
if ($primary) {
$headText = __("Primary key already exists.");
$subText = __("Taking you to next step…");
$hasPrimaryKey = "1";
} else {
$headText = __(
"There is no primary key; please add one.<br/>"
. "Hint: A primary key is a column "
. "(or combination of columns) that uniquely identify all rows."
);
$subText = '<a href="#" id="createPrimaryKey">'
. Util::getIcon(
'b_index_add.png', __(
'Add a primary key on existing column(s)'
)
)
. '</a>';
$extra = __(
"If it's not possible to make existing "
. "column combinations as primary key"
) . "<br/>"
. '<a href="#" id="addNewPrimary">'
. __('+ Add a new primary key column') . '</a>';
}
$res = array(
'legendText' => $legendText,
'headText' => $headText,
'subText' => $subText,
'hasPrimaryKey' => $hasPrimaryKey,
'extra' => $extra
);
return $res;
}
/**
* build the html contents of various html elements in step 1.4
*
* @param string $db current database
* @param string $table current table
*
* @return string HTML contents for step 1.4
*/
public static function getHtmlContentsFor1NFStep4($db, $table)
{
$step = 4;
$stepTxt = __('Remove redundant columns');
$legendText = __('Step 1.') . $step . " " . $stepTxt;
$headText = __(
"Do you have a group of columns which on combining gives an existing"
. " column? For example, if you have first_name, last_name and"
. " full_name then combining first_name and last_name gives full_name"
. " which is redundant."
);
$subText = __(
"Check the columns which are redundant and click on remove. "
. "If no redundant column, click on 'No redundant column'"
);
$extra = self::getHtmlForColumnsList($db, $table, 'all', "checkbox") . "</br>"
. '<input type="submit" id="removeRedundant" value="'
. __('Remove selected') . '"/>'
. '<input type="submit" value="' . __('No redundant column')
. '" onclick="goToFinish1NF();"'
. '/>';
$res = array(
'legendText' => $legendText,
'headText' => $headText,
'subText' => $subText,
'extra' => $extra
);
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 string HTML contents for step 1.3
*/
public static function getHtmlContentsFor1NFStep3($db, $table)
{
$step = 3;
$stepTxt = __('Move repeating groups');
$legendText = __('Step 1.') . $step . " " . $stepTxt;
$headText = __(
"Do you have a group of two or more columns that are closely "
. "related and are all repeating the same attribute? For example, "
. "a table that holds data on books might have columns such as book_id, "
. "author1, author2, author3 and so on which form a "
. "repeating group. In this case a new table (book_id, author) should "
. "be created."
);
$subText = __(
"Check the columns which form a repeating group. "
. "If no such group, click on 'No repeating group'"
);
$extra = self::getHtmlForColumnsList($db, $table, 'all', "checkbox") . "</br>"
. '<input type="submit" id="moveRepeatingGroup" value="'
. __('Done') . '"/>'
. '<input type="submit" value="' . __('No repeating group')
. '" onclick="goToStep4();"'
. '/>';
$primary = 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
*
* @param string $db current database
* @param string $table current table
*
* @return string HTML contents for 2NF step 2.1
*/
public static function getHtmlFor2NFstep1($db, $table)
{
$legendText = __('Step 2.') . "1 " . __('Find partial dependencies');
$primary = Index::getPrimary($table, $db);
$primarycols = $primary->getColumns();
$pk = array();
$subText = '';
$selectPkForm = "";
$extra = "";
foreach ($primarycols as $col) {
$pk[] = $col->getName();
$selectPkForm .= '<input type="checkbox" name="pd" value="'
. htmlspecialchars($col->getName()) . '">'
. htmlspecialchars($col->getName());
}
$key = implode(', ', $pk);
if (count($primarycols) > 1) {
$GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
$columns = (array) $GLOBALS['dbi']->getColumnNames(
$db, $table, $GLOBALS['userlink']
);
if (count($pk) == count($columns)) {
$headText = sprintf(
__(
'No partial dependencies possible as '
. 'no non-primary column exists since primary key ( %1$s ) '
. 'is composed of all the columns in the table.'
), htmlspecialchars($key)
) . '<br/>';
$extra = '<h3>' . __('Table is already in second normal form.')
. '</h3>';
} else {
$headText = sprintf(
__(
'The primary key ( %1$s ) consists of more than one column '
. 'so we need to find the partial dependencies.'
), htmlspecialchars($key)
) . '<br/>' . __(
'Please answer the following question(s) '
. 'carefully to obtain a correct normalization.'
)
. '<br/><a href="#" id="showPossiblePd">' . __(
'+ Show me the possible partial dependencies '
. 'based on data in the table'
) . '</a>';
$subText = __(
'For each column below, '
. 'please select the <b>minimal set</b> of columns among given set '
. 'whose values combined together are sufficient'
. ' to determine the value of the column.'
);
$cnt = 0;
foreach ($columns as $column) {
if (!in_array($column, $pk)) {
$cnt++;
$extra .= "<b>" . sprintf(
__('\'%1$s\' depends on:'), htmlspecialchars($column)
) . "</b><br>";
$extra .= '<form id="pk_' . $cnt . '" data-colname="'
. htmlspecialchars($column) . '" class="smallIndent">'
. $selectPkForm . '</form><br/><br/>';
}
}
}
} else {
$headText = sprintf(
__(
'No partial dependencies possible as the primary key'
. ' ( %1$s ) has just one column.'
), htmlspecialchars($key)
) . '<br/>';
$extra = '<h3>' . __('Table is already in second normal form.') . '</h3>';
}
$res = array(
'legendText' => $legendText,
'headText' => $headText,
'subText' => $subText,
'extra' => $extra,
'primary_key' => $key
);
return $res;
}
/**
* build the html for showing the tables to have in order to put current table in 2NF
*
* @param array $partialDependencies array containing all the dependencies
* @param string $table current table
*
* @return string HTML
*/
public static function getHtmlForNewTables2NF($partialDependencies,$table)
{
$html = '<p><b>' . sprintf(
__(
'In order to put the '
. 'original table \'%1$s\' into Second normal form we need '
. 'to create the following tables:'
), htmlspecialchars($table)
) . '</b></p>';
$tableName = $table;
$i = 1;
foreach ($partialDependencies as $key=>$dependents) {
$html .= '<p><input type="text" name="' . htmlspecialchars($key)
. '" value="' . htmlspecialchars($tableName) . '"/>'
. '( <u>' . htmlspecialchars($key) . '</u>'
. (count($dependents)>0?', ':'')
. htmlspecialchars(implode(', ', $dependents)) . ' )';
$i++;
$tableName = 'table' . $i;
}
return $html;
}
/**
* create/alter the tables needed for 2NF
*
* @param array $partialDependencies array containing all the partial dependencies
* @param object $tablesName name of new tables
* @param string $table current table
* @param string $db current database
*
* @return array
*/
public static function createNewTablesFor2NF($partialDependencies, $tablesName, $table, $db)
{
$dropCols = false;
$nonPKCols = array();
$queries = array();
$error = false;
$headText = '<h3>' . sprintf(
__('The second step of normalization is complete for table \'%1$s\'.'),
htmlspecialchars($table)
) . '</h3>';
if (count((array)$partialDependencies) == 1) {
return array(
'legendText'=>__('End of step'), 'headText'=>$headText,
'queryError'=>$error
);
}
$message = '';
$GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
foreach ($partialDependencies as $key=>$dependents) {
if ($tablesName->$key != $table) {
$backquotedKey = implode(', ', Util::backquote(explode(', ', $key)));
$queries[] = 'CREATE TABLE ' . Util::backquote($tablesName->$key)
. ' SELECT DISTINCT ' . $backquotedKey
. (count($dependents)>0?', ':'')
. implode(',', Util::backquote($dependents))
. ' FROM ' . Util::backquote($table) . ';';
$queries[] = 'ALTER TABLE ' . Util::backquote($tablesName->$key)
. ' ADD PRIMARY KEY(' . $backquotedKey . ');';
$nonPKCols = array_merge($nonPKCols, $dependents);
} else {
$dropCols = true;
}
}
if ($dropCols) {
$query = 'ALTER TABLE ' . Util::backquote($table);
foreach ($nonPKCols as $col) {
$query .= ' DROP ' . Util::backquote($col) . ',';
}
$query = trim($query, ', ');
$query .= ';';
$queries[] = $query;
} else {
$queries[] = 'DROP TABLE ' . Util::backquote($table);
}
foreach ($queries as $query) {
if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['userlink'])) {
$message = Message::error(__('Error in processing!'));
$message->addMessage(
Message::rawError(
$GLOBALS['dbi']->getError($GLOBALS['userlink'])
),
'<br /><br />'
);
$error = true;
break;
}
}
return array(
'legendText' => __('End of step'),
'headText' => $headText,
'queryError' => $error,
'extra' => $message
);
}
/**
* build the html for showing the new tables to have in order
* to put given tables in 3NF
*
* @param object $dependencies containing all the dependencies
* @param array $tables tables formed after 2NF and need to convert to 3NF
* @param string $db current database
*
* @return array containing html and the list of new tables
*/
public static function getHtmlForNewTables3NF($dependencies, $tables, $db)
{
$html = "";
$i = 1;
$newTables = array();
foreach ($tables as $table=>$arrDependson) {
if (count(array_unique($arrDependson)) == 1) {
continue;
}
$primary = Index::getPrimary($table, $db);
$primarycols = $primary->getColumns();
$pk = array();
foreach ($primarycols as $col) {
$pk[] = $col->getName();
}
$html .= '<p><b>' . sprintf(
__(
'In order to put the '
. 'original table \'%1$s\' into Third normal form we need '
. 'to create the following tables:'
), htmlspecialchars($table)
) . '</b></p>';
$tableName = $table;
$columnList = array();
foreach ($arrDependson as $key) {
$dependents = $dependencies->$key;
if ($key == $table) {
$key = implode(', ', $pk);
}
$tmpTableCols =array_merge(explode(', ', $key), $dependents);
sort($tmpTableCols);
if (!in_array($tmpTableCols, $columnList)) {
$columnList[] = $tmpTableCols;
$html .= '<p><input type="text" name="'
. htmlspecialchars($tableName)
. '" value="' . htmlspecialchars($tableName) . '"/>'
. '( <u>' . htmlspecialchars($key) . '</u>'
. (count($dependents)>0?', ':'')
. htmlspecialchars(implode(', ', $dependents)) . ' )';
$newTables[$table][$tableName] = array(
"pk"=>$key, "nonpk"=>implode(', ', $dependents)
);
$i++;
$tableName = 'table' . $i;
}
}
}
return array('html' => $html, 'newTables' => $newTables, 'success' => true);
}
/**
* create new tables or alter existing to get 3NF
*
* @param array $newTables list of new tables to be created
* @param string $db current database
*
* @return array
*/
public static function createNewTablesFor3NF($newTables, $db)
{
$queries = array();
$dropCols = false;
$error = false;
$headText = '<h3>' .
__('The third step of normalization is complete.')
. '</h3>';
if (count((array)$newTables) == 0) {
return array(
'legendText'=>__('End of step'), 'headText'=>$headText,
'queryError'=>$error
);
}
$message = '';
$GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
foreach ($newTables as $originalTable=>$tablesList) {
foreach ($tablesList as $table=>$cols) {
if ($table != $originalTable) {
$quotedPk = implode(
', ', Util::backquote(explode(', ', $cols->pk))
);
$quotedNonpk = implode(
', ', Util::backquote(explode(', ', $cols->nonpk))
);
$queries[] = 'CREATE TABLE ' . Util::backquote($table)
. ' SELECT DISTINCT ' . $quotedPk
. ', ' . $quotedNonpk
. ' FROM ' . Util::backquote($originalTable) . ';';
$queries[] = 'ALTER TABLE ' . Util::backquote($table)
. ' ADD PRIMARY KEY(' . $quotedPk . ');';
} else {
$dropCols = $cols;
}
}
if ($dropCols) {
$columns = (array) $GLOBALS['dbi']->getColumnNames(
$db, $originalTable, $GLOBALS['userlink']
);
$colPresent = array_merge(
explode(', ', $dropCols->pk), explode(', ', $dropCols->nonpk)
);
$query = 'ALTER TABLE ' . Util::backquote($originalTable);
foreach ($columns as $col) {
if (!in_array($col, $colPresent)) {
$query .= ' DROP ' . Util::backquote($col) . ',';
}
}
$query = trim($query, ', ');
$query .= ';';
$queries[] = $query;
} else {
$queries[] = 'DROP TABLE ' . Util::backquote($originalTable);
}
$dropCols = false;
}
foreach ($queries as $query) {
if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['userlink'])) {
$message = Message::error(__('Error in processing!'));
$message->addMessage(
Message::rawError(
$GLOBALS['dbi']->getError($GLOBALS['userlink'])
),
'<br /><br />'
);
$error = true;
break;
}
}
return array(
'legendText' => __('End of step'),
'headText' => $headText,
'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
*/
public static function moveRepeatingGroup(
$repeatingColumns, $primary_columns, $newTable, $newColumn, $table, $db
) {
$repeatingColumnsArr = (array)Util::backquote(
explode(', ', $repeatingColumns)
);
$primary_columns = implode(
',', Util::backquote(explode(',', $primary_columns))
);
$query1 = 'CREATE TABLE ' . Util::backquote($newTable);
$query2 = 'ALTER TABLE ' . Util::backquote($table);
$message = Message::success(
sprintf(
__('Selected repeating group has been moved 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 ' . Util::backquote($newColumn)
. ' FROM ' . 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 = Message::error(__('Error in processing!'));
$message->addMessage(
Message::rawError(
$GLOBALS['dbi']->getError($GLOBALS['userlink'])
),
'<br /><br />'
);
$error = true;
break;
}
}
return array(
'queryError' => $error, 'message' => $message
);
}
/**
* build html for 3NF step 1 to find the transitive dependencies
*
* @param string $db current database
* @param array $tables tables formed after 2NF and need to process for 3NF
*
* @return string
*/
public static function getHtmlFor3NFstep1($db, $tables)
{
$legendText = __('Step 3.') . "1 " . __('Find transitive dependencies');
$extra = "";
$headText = __(
'Please answer the following question(s) '
. 'carefully to obtain a correct normalization.'
);
$subText = __(
'For each column below, '
. 'please select the <b>minimal set</b> of columns among given set '
. 'whose values combined together are sufficient'
. ' to determine the value of the column.<br />'
. 'Note: A column may have no transitive dependency, '
. 'in that case you don\'t have to select any.'
);
$cnt = 0;
foreach ($tables as $table) {
$primary = Index::getPrimary($table, $db);
$primarycols = $primary->getColumns();
$selectTdForm = "";
$pk = array();
foreach ($primarycols as $col) {
$pk[] = $col->getName();
}
$GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
$columns = (array) $GLOBALS['dbi']->getColumnNames(
$db, $table, $GLOBALS['userlink']
);
if (count($columns) - count($pk) <= 1) {
continue;
}
foreach ($columns as $column) {
if (!in_array($column, $pk)) {
$selectTdForm .= '<input type="checkbox" name="pd" value="'
. htmlspecialchars($column) . '">'
. '<span>' . htmlspecialchars($column) . '</span>';
}
}
foreach ($columns as $column) {
if (!in_array($column, $pk)) {
$cnt++;
$extra .= "<b>" . sprintf(
__('\'%1$s\' depends on:'), htmlspecialchars($column)
)
. "</b><br>";
$extra .= '<form id="td_' . $cnt . '" data-colname="'
. htmlspecialchars($column) . '" data-tablename="'
. htmlspecialchars($table) . '" class="smallIndent">'
. $selectTdForm
. '</form><br/><br/>';
}
}
}
if ($extra == "") {
$headText = __(
"No Transitive dependencies possible as the table "
. "doesn't have any non primary key columns"
);
$subText = "";
$extra = "<h3>" . __("Table is already in Third normal form!") . "</h3>";
}
$res = array(
'legendText' => $legendText,
'headText' => $headText,
'subText' => $subText,
'extra' => $extra
);
return $res;
}
/**
* get html for options to normalize table
*
* @return string HTML
*/
public static function getHtmlForNormalizetable()
{
$html_output = '<form method="post" action="normalization.php" '
. 'name="normalize" '
. 'id="normalizeTable" '
. '>'
. Url::getHiddenInputs($GLOBALS['db'], $GLOBALS['table'])
. '<input type="hidden" name="step1" value="1">';
$html_output .= '<fieldset>';
$html_output .= '<legend>'
. __('Improve table structure (Normalization):') . '</legend>';
$html_output .= '<h3>' . __('Select up to what step you want to normalize')
. '</h3>';
$choices = array(
'1nf' => __('First step of normalization (1NF)'),
'2nf' => __('Second step of normalization (1NF+2NF)'),
'3nf' => __('Third step of normalization (1NF+2NF+3NF)'));
$html_output .= Util::getRadioFields(
'normalizeTo', $choices, '1nf', true
);
$html_output .= '</fieldset><fieldset class="tblFooters">'
. "<span class='floatleft'>" . __(
'Hint: Please follow the procedure carefully in order '
. 'to obtain correct normalization'
) . "</span>"
. '<input type="submit" name="submit_normalize" value="' . __('Go') . '" />'
. '</fieldset>'
. '</form>'
. '</div>';
return $html_output;
}
/**
* find all the possible partial dependencies based on data in the table.
*
* @param string $table current table
* @param string $db current database
*
* @return string HTML containing the list of all the possible partial dependencies
*/
public static function findPartialDependencies($table, $db)
{
$dependencyList = array();
$GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
$columns = (array) $GLOBALS['dbi']->getColumnNames(
$db, $table, $GLOBALS['userlink']
);
$columns = (array)Util::backquote($columns);
$totalRowsRes = $GLOBALS['dbi']->fetchResult(
'SELECT COUNT(*) FROM (SELECT * FROM '
. Util::backquote($table) . ' LIMIT 500) as dt;'
);
$totalRows = $totalRowsRes[0];
$primary = Index::getPrimary($table, $db);
$primarycols = $primary->getColumns();
$pk = array();
foreach ($primarycols as $col) {
$pk[] = Util::backquote($col->getName());
}
$partialKeys = self::getAllCombinationPartialKeys($pk);
$distinctValCount = self::findDistinctValuesCount(
array_unique(
array_merge($columns, $partialKeys)
), $table
);
foreach ($columns as $column) {
if (!in_array($column, $pk)) {
foreach ($partialKeys as $partialKey) {
if ($partialKey
&& self::checkPartialDependency(
$partialKey, $column, $table,
$distinctValCount[$partialKey],
$distinctValCount[$column], $totalRows
)
) {
$dependencyList[$partialKey][] = $column;
}
}
}
}
$html = __(
'This list is based on a subset of the table\'s data '
. 'and is not necessarily accurate. '
)
. '<div class="dependencies_box">';
foreach ($dependencyList as $dependon=>$colList) {
$html .= '<span class="displayblock">'
. '<input type="button" class="pickPd" value="' . __('Pick') . '"/>'
. '<span class="determinants">'
. htmlspecialchars(str_replace('`', '', $dependon)) . '</span> -> '
. '<span class="dependents">'
. htmlspecialchars(str_replace('`', '', implode(', ', $colList)))
. '</span>'
. '</span>';
}
if (empty($dependencyList)) {
$html .= '<p class="displayblock desc">'
. __('No partial dependencies found!') . '</p>';
}
$html .= '</div>';
return $html;
}
/**
* check whether a particular column is dependent on given subset of primary key
*
* @param string $partialKey the partial key, subset of primary key,
* each column in key supposed to be backquoted
* @param string $column backquoted column on whose dependency being checked
* @param string $table current table
* @param integer $pkCnt distinct value count for given partial key
* @param integer $colCnt distinct value count for given column
* @param integer $totalRows total distinct rows count of the table
*
* @return boolean TRUE if $column is dependent on $partialKey, False otherwise
*/
public static function checkPartialDependency(
$partialKey, $column, $table, $pkCnt, $colCnt, $totalRows
) {
$query = 'SELECT '
. 'COUNT(DISTINCT ' . $partialKey . ',' . $column . ') as pkColCnt '
. 'FROM (SELECT * FROM ' . Util::backquote($table)
. ' LIMIT 500) as dt' . ';';
$res = $GLOBALS['dbi']->fetchResult($query, null, null, $GLOBALS['userlink']);
$pkColCnt = $res[0];
if ($pkCnt && $pkCnt == $colCnt && $colCnt == $pkColCnt) {
return true;
}
if ($totalRows && $totalRows == $pkCnt) {
return true;
}
return false;
}
/**
* function to get distinct values count of all the column in the array $columns
*
* @param array $columns array of backquoted columns whose distinct values
* need to be counted.
* @param string $table table to which these columns belong
*
* @return array associative array containing the count
*/
public static function findDistinctValuesCount($columns, $table)
{
$result = array();
$query = 'SELECT ';
foreach ($columns as $column) {
if ($column) { //each column is already backquoted
$query .= 'COUNT(DISTINCT ' . $column . ') as \''
. $column . '_cnt\', ';
}
}
$query = trim($query, ', ');
$query .= ' FROM (SELECT * FROM ' . Util::backquote($table)
. ' LIMIT 500) as dt' . ';';
$res = $GLOBALS['dbi']->fetchResult($query, null, null, $GLOBALS['userlink']);
foreach ($columns as $column) {
if ($column) {
$result[$column] = $res[0][$column . '_cnt'];
}
}
return $result;
}
/**
* find all the possible partial keys
*
* @param array $primaryKey array containing all the column present in primary key
*
* @return array containing all the possible partial keys(subset of primary key)
*/
public static function getAllCombinationPartialKeys($primaryKey)
{
$results = array('');
foreach ($primaryKey as $element) {
foreach ($results as $combination) {
array_push(
$results, trim($element . ',' . $combination, ',')
);
}
}
array_pop($results); //remove key which consist of all primary key columns
return $results;
}
}

View File

@ -1,982 +0,0 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* set of functions used for normalization
*
* @package PhpMyAdmin
*/
use PhpMyAdmin\Message;
use PhpMyAdmin\Relation;
use PhpMyAdmin\Transformations;
use PhpMyAdmin\Url;
use PhpMyAdmin\Util;
/**
* build the html for columns of $colTypeCategory category
* in form of given $listType in a table
*
* @param string $db current database
* @param string $table current table
* @param string $colTypeCategory supported all|Numeric|String|Spatial
* |Date and time using the _pgettext() format
* @param string $listType type of list to build, supported dropdown|checkbox
*
* @return string HTML for list of columns in form of given list types
*/
function PMA_getHtmlForColumnsList(
$db, $table, $colTypeCategory='all', $listType='dropdown'
) {
$columnTypeList = array();
if ($colTypeCategory != 'all') {
$types = $GLOBALS['PMA_Types']->getColumns();
$columnTypeList = $types[$colTypeCategory];
}
$GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
$columns = $GLOBALS['dbi']->getColumns(
$db, $table, null,
true, $GLOBALS['userlink']
);
$type = "";
$selectColHtml = "";
foreach ($columns as $column => $def) {
if (isset($def['Type'])) {
$extracted_columnspec = Util::extractColumnSpec($def['Type']);
$type = $extracted_columnspec['type'];
}
if (empty($columnTypeList)
|| in_array(mb_strtoupper($type), $columnTypeList)
) {
if ($listType == 'checkbox') {
$selectColHtml .= '<input type="checkbox" value="'
. htmlspecialchars($column) . '"/>'
. htmlspecialchars($column) . ' [ '
. htmlspecialchars($def['Type']) . ' ]</br>';
} else {
$selectColHtml .= '<option value="' . htmlspecialchars($column) . ''
. '">' . htmlspecialchars($column)
. ' [ ' . htmlspecialchars($def['Type']) . ' ]'
. '</option>';
}
}
}
return $selectColHtml;
}
/**
* get the html of the form to add the new column to given table
*
* @param integer $num_fields number of columns to add
* @param string $db current database
* @param string $table current table
* @param array $columnMeta array containing default values for the fields
*
* @return string HTML
*/
function PMA_getHtmlForCreateNewColumn(
$num_fields, $db, $table, $columnMeta=array()
) {
$cfgRelation = Relation::getRelationsParam();
$content_cells = array();
$available_mime = array();
$mime_map = array();
if ($cfgRelation['mimework'] && $GLOBALS['cfg']['BrowseMIME']) {
$mime_map = Transformations::getMIME($db, $table);
$available_mime = Transformations::getAvailableMIMEtypes();
}
$comments_map = Relation::getComments($db, $table);
for ($columnNumber = 0; $columnNumber < $num_fields; $columnNumber++) {
$content_cells[$columnNumber] = array(
'columnNumber' => $columnNumber,
'columnMeta' => $columnMeta,
'type_upper' => '',
'length_values_input_size' => 8,
'length' => '',
'extracted_columnspec' => array(),
'submit_attribute' => null,
'comments_map' => $comments_map,
'fields_meta' => null,
'is_backup' => true,
'move_columns' => array(),
'cfgRelation' => $cfgRelation,
'available_mime' => isset($available_mime)?$available_mime:array(),
'mime_map' => $mime_map
);
}
return PhpMyAdmin\Template::get(
'columns_definitions/table_fields_definitions'
)
->render(
array(
'is_backup' => true,
'fields_meta' => null,
'mimework' => $cfgRelation['mimework'],
'content_cells' => $content_cells
)
);
}
/**
* build the html for step 1.1 of normalization
*
* @param string $db current database
* @param string $table current table
* @param string $normalizedTo up to which step normalization will go,
* possible values 1nf|2nf|3nf
*
* @return string HTML for step 1.1
*/
function PMA_getHtmlFor1NFStep1($db, $table, $normalizedTo)
{
$step = 1;
$stepTxt = __('Make all columns atomic');
$html = "<h3 class='center'>"
. __('First step of normalization (1NF)') . "</h3>";
$html .= "<div id='mainContent' data-normalizeto='" . $normalizedTo . "'>" .
"<fieldset>" .
"<legend>" . __('Step 1.') . $step . " " . $stepTxt . "</legend>" .
"<h4>" . __(
'Do you have any column which can be split into more than'
. ' one column? '
. 'For example: address can be split into street, city, country and zip.'
)
. "</br>(<a class='central_columns_dialog' data-maxrows='25' "
. "data-pick=false href='#'> "
. __(
'Show me the central list of columns that are not already in this table'
) . " </a>)</h4>"
. "<p class='cm-em'>" . __(
'Select a column which can be split into more '
. 'than one (on select of \'no such column\', it\'ll move to next step).'
)
. "</p>"
. "<div id='extra'>"
. "<select id='selectNonAtomicCol' name='makeAtomic'>"
. '<option selected="selected" disabled="disabled">'
. __('Select one…') . "</option>"
. "<option value='no_such_col'>" . __('No such column') . "</option>"
. PMA_getHtmlForColumnsList(
$db,
$table,
_pgettext('string types', 'String')
)
. "</select>"
. "<span>" . __('split into ')
. "</span><input id='numField' type='number' value='2'>"
. "<input type='submit' id='splitGo' value='" . __('Go') . "'/></div>"
. "<div id='newCols'></div>"
. "</fieldset><fieldset class='tblFooters'>"
. "</fieldset>"
. "</div>";
return $html;
}
/**
* build the html contents of various html elements in step 1.2
*
* @param string $db current database
* @param string $table current table
*
* @return string HTML contents for step 1.2
*/
function PMA_getHtmlContentsFor1NFStep2($db, $table)
{
$step = 2;
$stepTxt = __('Have a primary key');
$primary = PhpMyAdmin\Index::getPrimary($table, $db);
$hasPrimaryKey = "0";
$legendText = __('Step 1.') . $step . " " . $stepTxt;
$extra = '';
if ($primary) {
$headText = __("Primary key already exists.");
$subText = __("Taking you to next step…");
$hasPrimaryKey = "1";
} else {
$headText = __(
"There is no primary key; please add one.<br/>"
. "Hint: A primary key is a column "
. "(or combination of columns) that uniquely identify all rows."
);
$subText = '<a href="#" id="createPrimaryKey">'
. Util::getIcon(
'b_index_add.png', __(
'Add a primary key on existing column(s)'
)
)
. '</a>';
$extra = __(
"If it's not possible to make existing "
. "column combinations as primary key"
) . "<br/>"
. '<a href="#" id="addNewPrimary">'
. __('+ Add a new primary key column') . '</a>';
}
$res = array(
'legendText' => $legendText,
'headText' => $headText,
'subText' => $subText,
'hasPrimaryKey' => $hasPrimaryKey,
'extra' => $extra
);
return $res;
}
/**
* build the html contents of various html elements in step 1.4
*
* @param string $db current database
* @param string $table current table
*
* @return string HTML contents for step 1.4
*/
function PMA_getHtmlContentsFor1NFStep4($db, $table)
{
$step = 4;
$stepTxt = __('Remove redundant columns');
$legendText = __('Step 1.') . $step . " " . $stepTxt;
$headText = __(
"Do you have a group of columns which on combining gives an existing"
. " column? For example, if you have first_name, last_name and"
. " full_name then combining first_name and last_name gives full_name"
. " which is redundant."
);
$subText = __(
"Check the columns which are redundant and click on remove. "
. "If no redundant column, click on 'No redundant column'"
);
$extra = PMA_getHtmlForColumnsList($db, $table, 'all', "checkbox") . "</br>"
. '<input type="submit" id="removeRedundant" value="'
. __('Remove selected') . '"/>'
. '<input type="submit" value="' . __('No redundant column')
. '" onclick="goToFinish1NF();"'
. '/>';
$res = array(
'legendText' => $legendText,
'headText' => $headText,
'subText' => $subText,
'extra' => $extra
);
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 string 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 a group of two or more columns that are closely "
. "related and are all repeating the same attribute? For example, "
. "a table that holds data on books might have columns such as book_id, "
. "author1, author2, author3 and so on which form a "
. "repeating group. In this case a new table (book_id, author) should "
. "be created."
);
$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 = PhpMyAdmin\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
*
* @param string $db current database
* @param string $table current table
*
* @return string HTML contents for 2NF step 2.1
*/
function PMA_getHtmlFor2NFstep1($db, $table)
{
$legendText = __('Step 2.') . "1 " . __('Find partial dependencies');
$primary = PhpMyAdmin\Index::getPrimary($table, $db);
$primarycols = $primary->getColumns();
$pk = array();
$subText = '';
$selectPkForm = "";
$extra = "";
foreach ($primarycols as $col) {
$pk[] = $col->getName();
$selectPkForm .= '<input type="checkbox" name="pd" value="'
. htmlspecialchars($col->getName()) . '">'
. htmlspecialchars($col->getName());
}
$key = implode(', ', $pk);
if (count($primarycols) > 1) {
$GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
$columns = (array) $GLOBALS['dbi']->getColumnNames(
$db, $table, $GLOBALS['userlink']
);
if (count($pk) == count($columns)) {
$headText = sprintf(
__(
'No partial dependencies possible as '
. 'no non-primary column exists since primary key ( %1$s ) '
. 'is composed of all the columns in the table.'
), htmlspecialchars($key)
) . '<br/>';
$extra = '<h3>' . __('Table is already in second normal form.')
. '</h3>';
} else {
$headText = sprintf(
__(
'The primary key ( %1$s ) consists of more than one column '
. 'so we need to find the partial dependencies.'
), htmlspecialchars($key)
) . '<br/>' . __(
'Please answer the following question(s) '
. 'carefully to obtain a correct normalization.'
)
. '<br/><a href="#" id="showPossiblePd">' . __(
'+ Show me the possible partial dependencies '
. 'based on data in the table'
) . '</a>';
$subText = __(
'For each column below, '
. 'please select the <b>minimal set</b> of columns among given set '
. 'whose values combined together are sufficient'
. ' to determine the value of the column.'
);
$cnt = 0;
foreach ($columns as $column) {
if (!in_array($column, $pk)) {
$cnt++;
$extra .= "<b>" . sprintf(
__('\'%1$s\' depends on:'), htmlspecialchars($column)
) . "</b><br>";
$extra .= '<form id="pk_' . $cnt . '" data-colname="'
. htmlspecialchars($column) . '" class="smallIndent">'
. $selectPkForm . '</form><br/><br/>';
}
}
}
} else {
$headText = sprintf(
__(
'No partial dependencies possible as the primary key'
. ' ( %1$s ) has just one column.'
), htmlspecialchars($key)
) . '<br/>';
$extra = '<h3>' . __('Table is already in second normal form.') . '</h3>';
}
$res = array(
'legendText' => $legendText,
'headText' => $headText,
'subText' => $subText,
'extra' => $extra,
'primary_key' => $key
);
return $res;
}
/**
* build the html for showing the tables to have in order to put current table in 2NF
*
* @param array $partialDependencies array containing all the dependencies
* @param string $table current table
*
* @return string HTML
*/
function PMA_getHtmlForNewTables2NF($partialDependencies,$table)
{
$html = '<p><b>' . sprintf(
__(
'In order to put the '
. 'original table \'%1$s\' into Second normal form we need '
. 'to create the following tables:'
), htmlspecialchars($table)
) . '</b></p>';
$tableName = $table;
$i = 1;
foreach ($partialDependencies as $key=>$dependents) {
$html .= '<p><input type="text" name="' . htmlspecialchars($key)
. '" value="' . htmlspecialchars($tableName) . '"/>'
. '( <u>' . htmlspecialchars($key) . '</u>'
. (count($dependents)>0?', ':'')
. htmlspecialchars(implode(', ', $dependents)) . ' )';
$i++;
$tableName = 'table' . $i;
}
return $html;
}
/**
* create/alter the tables needed for 2NF
*
* @param array $partialDependencies array containing all the partial dependencies
* @param object $tablesName name of new tables
* @param string $table current table
* @param string $db current database
*
* @return array
*/
function PMA_createNewTablesFor2NF($partialDependencies, $tablesName, $table, $db)
{
$dropCols = false;
$nonPKCols = array();
$queries = array();
$error = false;
$headText = '<h3>' . sprintf(
__('The second step of normalization is complete for table \'%1$s\'.'),
htmlspecialchars($table)
) . '</h3>';
if (count((array)$partialDependencies) == 1) {
return array(
'legendText'=>__('End of step'), 'headText'=>$headText,
'queryError'=>$error
);
}
$message = '';
$GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
foreach ($partialDependencies as $key=>$dependents) {
if ($tablesName->$key != $table) {
$backquotedKey = implode(', ', Util::backquote(explode(', ', $key)));
$queries[] = 'CREATE TABLE ' . Util::backquote($tablesName->$key)
. ' SELECT DISTINCT ' . $backquotedKey
. (count($dependents)>0?', ':'')
. implode(',', Util::backquote($dependents))
. ' FROM ' . Util::backquote($table) . ';';
$queries[] = 'ALTER TABLE ' . Util::backquote($tablesName->$key)
. ' ADD PRIMARY KEY(' . $backquotedKey . ');';
$nonPKCols = array_merge($nonPKCols, $dependents);
} else {
$dropCols = true;
}
}
if ($dropCols) {
$query = 'ALTER TABLE ' . Util::backquote($table);
foreach ($nonPKCols as $col) {
$query .= ' DROP ' . Util::backquote($col) . ',';
}
$query = trim($query, ', ');
$query .= ';';
$queries[] = $query;
} else {
$queries[] = 'DROP TABLE ' . Util::backquote($table);
}
foreach ($queries as $query) {
if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['userlink'])) {
$message = Message::error(__('Error in processing!'));
$message->addMessage(
Message::rawError(
$GLOBALS['dbi']->getError($GLOBALS['userlink'])
),
'<br /><br />'
);
$error = true;
break;
}
}
return array(
'legendText' => __('End of step'),
'headText' => $headText,
'queryError' => $error,
'extra' => $message
);
}
/**
* build the html for showing the new tables to have in order
* to put given tables in 3NF
*
* @param object $dependencies containing all the dependencies
* @param array $tables tables formed after 2NF and need to convert to 3NF
* @param string $db current database
*
* @return array containing html and the list of new tables
*/
function PMA_getHtmlForNewTables3NF($dependencies, $tables, $db)
{
$html = "";
$i = 1;
$newTables = array();
foreach ($tables as $table=>$arrDependson) {
if (count(array_unique($arrDependson)) == 1) {
continue;
}
$primary = PhpMyAdmin\Index::getPrimary($table, $db);
$primarycols = $primary->getColumns();
$pk = array();
foreach ($primarycols as $col) {
$pk[] = $col->getName();
}
$html .= '<p><b>' . sprintf(
__(
'In order to put the '
. 'original table \'%1$s\' into Third normal form we need '
. 'to create the following tables:'
), htmlspecialchars($table)
) . '</b></p>';
$tableName = $table;
$columnList = array();
foreach ($arrDependson as $key) {
$dependents = $dependencies->$key;
if ($key == $table) {
$key = implode(', ', $pk);
}
$tmpTableCols =array_merge(explode(', ', $key), $dependents);
sort($tmpTableCols);
if (!in_array($tmpTableCols, $columnList)) {
$columnList[] = $tmpTableCols;
$html .= '<p><input type="text" name="'
. htmlspecialchars($tableName)
. '" value="' . htmlspecialchars($tableName) . '"/>'
. '( <u>' . htmlspecialchars($key) . '</u>'
. (count($dependents)>0?', ':'')
. htmlspecialchars(implode(', ', $dependents)) . ' )';
$newTables[$table][$tableName] = array(
"pk"=>$key, "nonpk"=>implode(', ', $dependents)
);
$i++;
$tableName = 'table' . $i;
}
}
}
return array('html' => $html, 'newTables' => $newTables, 'success' => true);
}
/**
* create new tables or alter existing to get 3NF
*
* @param array $newTables list of new tables to be created
* @param string $db current database
*
* @return array
*/
function PMA_createNewTablesFor3NF($newTables, $db)
{
$queries = array();
$dropCols = false;
$error = false;
$headText = '<h3>' .
__('The third step of normalization is complete.')
. '</h3>';
if (count((array)$newTables) == 0) {
return array(
'legendText'=>__('End of step'), 'headText'=>$headText,
'queryError'=>$error
);
}
$message = '';
$GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
foreach ($newTables as $originalTable=>$tablesList) {
foreach ($tablesList as $table=>$cols) {
if ($table != $originalTable) {
$quotedPk = implode(
', ', Util::backquote(explode(', ', $cols->pk))
);
$quotedNonpk = implode(
', ', Util::backquote(explode(', ', $cols->nonpk))
);
$queries[] = 'CREATE TABLE ' . Util::backquote($table)
. ' SELECT DISTINCT ' . $quotedPk
. ', ' . $quotedNonpk
. ' FROM ' . Util::backquote($originalTable) . ';';
$queries[] = 'ALTER TABLE ' . Util::backquote($table)
. ' ADD PRIMARY KEY(' . $quotedPk . ');';
} else {
$dropCols = $cols;
}
}
if ($dropCols) {
$columns = (array) $GLOBALS['dbi']->getColumnNames(
$db, $originalTable, $GLOBALS['userlink']
);
$colPresent = array_merge(
explode(', ', $dropCols->pk), explode(', ', $dropCols->nonpk)
);
$query = 'ALTER TABLE ' . Util::backquote($originalTable);
foreach ($columns as $col) {
if (!in_array($col, $colPresent)) {
$query .= ' DROP ' . Util::backquote($col) . ',';
}
}
$query = trim($query, ', ');
$query .= ';';
$queries[] = $query;
} else {
$queries[] = 'DROP TABLE ' . Util::backquote($originalTable);
}
$dropCols = false;
}
foreach ($queries as $query) {
if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['userlink'])) {
$message = Message::error(__('Error in processing!'));
$message->addMessage(
Message::rawError(
$GLOBALS['dbi']->getError($GLOBALS['userlink'])
),
'<br /><br />'
);
$error = true;
break;
}
}
return array(
'legendText' => __('End of step'),
'headText' => $headText,
'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)Util::backquote(
explode(', ', $repeatingColumns)
);
$primary_columns = implode(
',', Util::backquote(explode(',', $primary_columns))
);
$query1 = 'CREATE TABLE ' . Util::backquote($newTable);
$query2 = 'ALTER TABLE ' . Util::backquote($table);
$message = Message::success(
sprintf(
__('Selected repeating group has been moved 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 ' . Util::backquote($newColumn)
. ' FROM ' . 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 = Message::error(__('Error in processing!'));
$message->addMessage(
Message::rawError(
$GLOBALS['dbi']->getError($GLOBALS['userlink'])
),
'<br /><br />'
);
$error = true;
break;
}
}
return array(
'queryError' => $error, 'message' => $message
);
}
/**
* build html for 3NF step 1 to find the transitive dependencies
*
* @param string $db current database
* @param array $tables tables formed after 2NF and need to process for 3NF
*
* @return string
*/
function PMA_getHtmlFor3NFstep1($db, $tables)
{
$legendText = __('Step 3.') . "1 " . __('Find transitive dependencies');
$extra = "";
$headText = __(
'Please answer the following question(s) '
. 'carefully to obtain a correct normalization.'
);
$subText = __(
'For each column below, '
. 'please select the <b>minimal set</b> of columns among given set '
. 'whose values combined together are sufficient'
. ' to determine the value of the column.<br />'
. 'Note: A column may have no transitive dependency, '
. 'in that case you don\'t have to select any.'
);
$cnt = 0;
foreach ($tables as $table) {
$primary = PhpMyAdmin\Index::getPrimary($table, $db);
$primarycols = $primary->getColumns();
$selectTdForm = "";
$pk = array();
foreach ($primarycols as $col) {
$pk[] = $col->getName();
}
$GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
$columns = (array) $GLOBALS['dbi']->getColumnNames(
$db, $table, $GLOBALS['userlink']
);
if (count($columns) - count($pk) <= 1) {
continue;
}
foreach ($columns as $column) {
if (!in_array($column, $pk)) {
$selectTdForm .= '<input type="checkbox" name="pd" value="'
. htmlspecialchars($column) . '">'
. '<span>' . htmlspecialchars($column) . '</span>';
}
}
foreach ($columns as $column) {
if (!in_array($column, $pk)) {
$cnt++;
$extra .= "<b>" . sprintf(
__('\'%1$s\' depends on:'), htmlspecialchars($column)
)
. "</b><br>";
$extra .= '<form id="td_' . $cnt . '" data-colname="'
. htmlspecialchars($column) . '" data-tablename="'
. htmlspecialchars($table) . '" class="smallIndent">'
. $selectTdForm
. '</form><br/><br/>';
}
}
}
if ($extra == "") {
$headText = __(
"No Transitive dependencies possible as the table "
. "doesn't have any non primary key columns"
);
$subText = "";
$extra = "<h3>" . __("Table is already in Third normal form!") . "</h3>";
}
$res = array(
'legendText' => $legendText,
'headText' => $headText,
'subText' => $subText,
'extra' => $extra
);
return $res;
}
/**
* get html for options to normalize table
*
* @return string HTML
*/
function PMA_getHtmlForNormalizetable()
{
$html_output = '<form method="post" action="normalization.php" '
. 'name="normalize" '
. 'id="normalizeTable" '
. '>'
. Url::getHiddenInputs($GLOBALS['db'], $GLOBALS['table'])
. '<input type="hidden" name="step1" value="1">';
$html_output .= '<fieldset>';
$html_output .= '<legend>'
. __('Improve table structure (Normalization):') . '</legend>';
$html_output .= '<h3>' . __('Select up to what step you want to normalize')
. '</h3>';
$choices = array(
'1nf' => __('First step of normalization (1NF)'),
'2nf' => __('Second step of normalization (1NF+2NF)'),
'3nf' => __('Third step of normalization (1NF+2NF+3NF)'));
$html_output .= Util::getRadioFields(
'normalizeTo', $choices, '1nf', true
);
$html_output .= '</fieldset><fieldset class="tblFooters">'
. "<span class='floatleft'>" . __(
'Hint: Please follow the procedure carefully in order '
. 'to obtain correct normalization'
) . "</span>"
. '<input type="submit" name="submit_normalize" value="' . __('Go') . '" />'
. '</fieldset>'
. '</form>'
. '</div>';
return $html_output;
}
/**
* find all the possible partial dependencies based on data in the table.
*
* @param string $table current table
* @param string $db current database
*
* @return string HTML containing the list of all the possible partial dependencies
*/
function PMA_findPartialDependencies($table, $db)
{
$dependencyList = array();
$GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
$columns = (array) $GLOBALS['dbi']->getColumnNames(
$db, $table, $GLOBALS['userlink']
);
$columns = (array)Util::backquote($columns);
$totalRowsRes = $GLOBALS['dbi']->fetchResult(
'SELECT COUNT(*) FROM (SELECT * FROM '
. Util::backquote($table) . ' LIMIT 500) as dt;'
);
$totalRows = $totalRowsRes[0];
$primary = PhpMyAdmin\Index::getPrimary($table, $db);
$primarycols = $primary->getColumns();
$pk = array();
foreach ($primarycols as $col) {
$pk[] = Util::backquote($col->getName());
}
$partialKeys = PMA_getAllCombinationPartialKeys($pk);
$distinctValCount = PMA_findDistinctValuesCount(
array_unique(
array_merge($columns, $partialKeys)
), $table
);
foreach ($columns as $column) {
if (!in_array($column, $pk)) {
foreach ($partialKeys as $partialKey) {
if ($partialKey
&& PMA_checkPartialDependency(
$partialKey, $column, $table,
$distinctValCount[$partialKey],
$distinctValCount[$column], $totalRows
)
) {
$dependencyList[$partialKey][] = $column;
}
}
}
}
$html = __(
'This list is based on a subset of the table\'s data '
. 'and is not necessarily accurate. '
)
. '<div class="dependencies_box">';
foreach ($dependencyList as $dependon=>$colList) {
$html .= '<span class="displayblock">'
. '<input type="button" class="pickPd" value="' . __('Pick') . '"/>'
. '<span class="determinants">'
. htmlspecialchars(str_replace('`', '', $dependon)) . '</span> -> '
. '<span class="dependents">'
. htmlspecialchars(str_replace('`', '', implode(', ', $colList)))
. '</span>'
. '</span>';
}
if (empty($dependencyList)) {
$html .= '<p class="displayblock desc">'
. __('No partial dependencies found!') . '</p>';
}
$html .= '</div>';
return $html;
}
/**
* check whether a particular column is dependent on given subset of primary key
*
* @param string $partialKey the partial key, subset of primary key,
* each column in key supposed to be backquoted
* @param string $column backquoted column on whose dependency being checked
* @param string $table current table
* @param integer $pkCnt distinct value count for given partial key
* @param integer $colCnt distinct value count for given column
* @param integer $totalRows total distinct rows count of the table
*
* @return boolean TRUE if $column is dependent on $partialKey, False otherwise
*/
function PMA_checkPartialDependency(
$partialKey, $column, $table, $pkCnt, $colCnt, $totalRows
) {
$query = 'SELECT '
. 'COUNT(DISTINCT ' . $partialKey . ',' . $column . ') as pkColCnt '
. 'FROM (SELECT * FROM ' . Util::backquote($table)
. ' LIMIT 500) as dt' . ';';
$res = $GLOBALS['dbi']->fetchResult($query, null, null, $GLOBALS['userlink']);
$pkColCnt = $res[0];
if ($pkCnt && $pkCnt == $colCnt && $colCnt == $pkColCnt) {
return true;
}
if ($totalRows && $totalRows == $pkCnt) {
return true;
}
return false;
}
/**
* function to get distinct values count of all the column in the array $columns
*
* @param array $columns array of backquoted columns whose distinct values
* need to be counted.
* @param string $table table to which these columns belong
*
* @return array associative array containing the count
*/
function PMA_findDistinctValuesCount($columns, $table)
{
$result = array();
$query = 'SELECT ';
foreach ($columns as $column) {
if ($column) { //each column is already backquoted
$query .= 'COUNT(DISTINCT ' . $column . ') as \''
. $column . '_cnt\', ';
}
}
$query = trim($query, ', ');
$query .= ' FROM (SELECT * FROM ' . Util::backquote($table)
. ' LIMIT 500) as dt' . ';';
$res = $GLOBALS['dbi']->fetchResult($query, null, null, $GLOBALS['userlink']);
foreach ($columns as $column) {
if ($column) {
$result[$column] = $res[0][$column . '_cnt'];
}
}
return $result;
}
/**
* find all the possible partial keys
*
* @param array $primaryKey array containing all the column present in primary key
*
* @return array containing all the possible partial keys(subset of primary key)
*/
function PMA_getAllCombinationPartialKeys($primaryKey)
{
$results = array('');
foreach ($primaryKey as $element) {
foreach ($results as $combination) {
array_push(
$results, trim($element . ',' . $combination, ',')
);
}
}
array_pop($results); //remove key which consist of all primary key columns
return $results;
}

View File

@ -7,20 +7,20 @@
*/
use PhpMyAdmin\Core;
use PhpMyAdmin\Url;
use PhpMyAdmin\Normalization;
use PhpMyAdmin\Response;
use PhpMyAdmin\Url;
/**
*
*/
require_once 'libraries/common.inc.php';
require_once 'libraries/normalization.lib.php';
if (isset($_REQUEST['getColumns'])) {
$html = '<option selected disabled>' . __('Select one…') . '</option>'
. '<option value="no_such_col">' . __('No such column') . '</option>';
//get column whose datatype falls under string category
$html .= PMA_getHtmlForColumnsList(
$html .= Normalization::getHtmlForColumnsList(
$db,
$table,
_pgettext('string types', 'String')
@ -30,7 +30,7 @@ if (isset($_REQUEST['getColumns'])) {
}
if (isset($_REQUEST['splitColumn'])) {
$num_fields = min(4096, intval($_REQUEST['numFields']));
$html = PMA_getHtmlForCreateNewColumn($num_fields, $db, $table);
$html = Normalization::getHtmlForCreateNewColumn($num_fields, $db, $table);
$html .= Url::getHiddenInputs($db, $table);
echo $html;
exit;
@ -38,7 +38,7 @@ if (isset($_REQUEST['splitColumn'])) {
if (isset($_REQUEST['addNewPrimary'])) {
$num_fields = 1;
$columnMeta = array('Field'=>$table . "_id", 'Extra'=>'auto_increment');
$html = PMA_getHtmlForCreateNewColumn(
$html = Normalization::getHtmlForCreateNewColumn(
$num_fields, $db, $table, $columnMeta
);
$html .= Url::getHiddenInputs($db, $table);
@ -46,14 +46,14 @@ if (isset($_REQUEST['addNewPrimary'])) {
exit;
}
if (isset($_REQUEST['findPdl'])) {
$html = PMA_findPartialDependencies($table, $db);
$html = Normalization::findPartialDependencies($table, $db);
echo $html;
exit;
}
if (isset($_REQUEST['getNewTables2NF'])) {
$partialDependencies = json_decode($_REQUEST['pd']);
$html = PMA_getHtmlForNewTables2NF($partialDependencies, $table);
$html = Normalization::getHtmlForNewTables2NF($partialDependencies, $table);
echo $html;
exit;
}
@ -63,7 +63,7 @@ $response = Response::getInstance();
if (isset($_REQUEST['getNewTables3NF'])) {
$dependencies = json_decode($_REQUEST['pd']);
$tables = json_decode($_REQUEST['tables']);
$newTables = PMA_getHtmlForNewTables3NF($dependencies, $tables, $db);
$newTables = Normalization::getHtmlForNewTables3NF($dependencies, $tables, $db);
$response->disable();
Core::headerJSON();
echo json_encode($newTables);
@ -81,13 +81,13 @@ if (Core::isValid($_REQUEST['normalizeTo'], array('1nf', '2nf', '3nf'))) {
if (isset($_REQUEST['createNewTables2NF'])) {
$partialDependencies = json_decode($_REQUEST['pd']);
$tablesName = json_decode($_REQUEST['newTablesName']);
$res = PMA_createNewTablesFor2NF($partialDependencies, $tablesName, $table, $db);
$res = Normalization::createNewTablesFor2NF($partialDependencies, $tablesName, $table, $db);
$response->addJSON($res);
exit;
}
if (isset($_REQUEST['createNewTables3NF'])) {
$newtables = json_decode($_REQUEST['newTables']);
$res = PMA_createNewTablesFor3NF($newtables, $db);
$res = Normalization::createNewTablesFor3NF($newtables, $db);
$response->addJSON($res);
exit;
}
@ -96,31 +96,31 @@ if (isset($_POST['repeatingColumns'])) {
$newTable = $_POST['newTable'];
$newColumn = $_POST['newColumn'];
$primary_columns = $_POST['primary_columns'];
$res = PMA_moveRepeatingGroup(
$res = Normalization::moveRepeatingGroup(
$repeatingColumns, $primary_columns, $newTable, $newColumn, $table, $db
);
$response->addJSON($res);
exit;
}
if (isset($_REQUEST['step1'])) {
$html = PMA_getHtmlFor1NFStep1($db, $table, $normalForm);
$html = Normalization::getHtmlFor1NFStep1($db, $table, $normalForm);
$response->addHTML($html);
} else if (isset($_REQUEST['step2'])) {
$res = PMA_getHtmlContentsFor1NFStep2($db, $table);
$res = Normalization::getHtmlContentsFor1NFStep2($db, $table);
$response->addJSON($res);
} else if (isset($_REQUEST['step3'])) {
$res = PMA_getHtmlContentsFor1NFStep3($db, $table);
$res = Normalization::getHtmlContentsFor1NFStep3($db, $table);
$response->addJSON($res);
} else if (isset($_REQUEST['step4'])) {
$res = PMA_getHtmlContentsFor1NFStep4($db, $table);
$res = Normalization::getHtmlContentsFor1NFStep4($db, $table);
$response->addJSON($res);
} else if (isset($_REQUEST['step']) && $_REQUEST['step'] == '2.1') {
$res = PMA_getHtmlFor2NFstep1($db, $table);
$res = Normalization::getHtmlFor2NFstep1($db, $table);
$response->addJSON($res);
} else if (isset($_REQUEST['step']) && $_REQUEST['step'] == '3.1') {
$tables = $_REQUEST['tables'];
$res = PMA_getHtmlFor3NFstep1($db, $tables);
$res = Normalization::getHtmlFor3NFstep1($db, $tables);
$response->addJSON($res);
} else {
$response->addHTML(PMA_getHtmlForNormalizetable());
$response->addHTML(Normalization::getHtmlForNormalizetable());
}

View File

@ -1,28 +1,28 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* tests for normalization.lib.php
* tests for PhpMyAdmin\Normalization
*
* @package PhpMyAdmin-test
*/
namespace PhpMyAdmin\Tests;
/*
* Include to test.
*/
use PhpMyAdmin\Normalization;
use PhpMyAdmin\Theme;
use PhpMyAdmin\TypesMySQL;
use PhpMyAdmin\Util;
use stdClass;
$GLOBALS['server'] = 1;
require_once 'libraries/database_interface.inc.php';
require_once 'libraries/normalization.lib.php';
/**
* tests for normalization.lib.php
* tests for PhpMyAdmin\Normalization
*
* @package PhpMyAdmin-test
*/
class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
class NormalizationTest extends \PHPUnit_Framework_TestCase
{
/**
* prepares environment for tests
@ -93,7 +93,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
}
/**
* Test for PMA_getHtmlForColumnsList
* Test for Normalization::getHtmlForColumnsList
*
* @return void
*/
@ -103,16 +103,16 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
$table= "PMA_table";
$this->assertContains(
'<option value="id">id [ integer ]</option>',
PMA_getHtmlForColumnsList($table, $db)
Normalization::getHtmlForColumnsList($table, $db)
);
$this->assertEquals(
'<input type="checkbox" value="col1"/>col1 [ varchar(100) ]</br>',
PMA_getHtmlForColumnsList($table, $db, 'String', 'checkbox')
Normalization::getHtmlForColumnsList($table, $db, 'String', 'checkbox')
);
}
/**
* Test for PMA_getHtmlForCreateNewColumn
* Test for Normalization::getHtmlForCreateNewColumn
*
* @return void
*/
@ -121,7 +121,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
$db = "PMA_db";
$table= "PMA_table";
$num_fields = 1;
$result = PMA_getHtmlForCreateNewColumn($num_fields, $db, $table);
$result = Normalization::getHtmlForCreateNewColumn($num_fields, $db, $table);
$this->assertContains(
'<table id="table_columns"',
$result
@ -129,7 +129,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
}
/**
* Test for PMA_getHtmlFor1NFStep1
* Test for Normalization::getHtmlFor1NFStep1
*
* @return void
*/
@ -138,7 +138,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
$db = "PMA_db";
$table= "PMA_table";
$normalizedTo = '1nf';
$result = PMA_getHtmlFor1NFStep1($db, $table, $normalizedTo);
$result = Normalization::getHtmlFor1NFStep1($db, $table, $normalizedTo);
$this->assertContains(
"<h3 class='center'>"
. __('First step of normalization (1NF)') . "</h3>",
@ -166,7 +166,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
);
$this->assertContains(
PMA_getHtmlForColumnsList(
Normalization::getHtmlForColumnsList(
$db, $table, _pgettext('string types', 'String')
), $result
);
@ -174,7 +174,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
}
/**
* Test for PMA_getHtmlContentsFor1NFStep2
* Test for Normalization::getHtmlContentsFor1NFStep2
*
* @return void
*/
@ -182,7 +182,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
{
$db = "PMA_db";
$table= "PMA_table1";
$result = PMA_getHtmlContentsFor1NFStep2($db, $table);
$result = Normalization::getHtmlContentsFor1NFStep2($db, $table);
$this->assertInternalType('array', $result);
$this->assertArrayHasKey('legendText', $result);
$this->assertArrayHasKey('headText', $result);
@ -199,12 +199,12 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
);
$this->assertEquals('0', $result['hasPrimaryKey']);
$this->assertContains(__('Step 1.') . 2, $result['legendText']);
$result1 = PMA_getHtmlContentsFor1NFStep2($db, 'PMA_table');
$result1 = Normalization::getHtmlContentsFor1NFStep2($db, 'PMA_table');
$this->assertEquals('1', $result1['hasPrimaryKey']);
}
/**
* Test for PMA_getHtmlContentsFor1NFStep4
* Test for Normalization::getHtmlContentsFor1NFStep4
*
* @return void
*/
@ -212,7 +212,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
{
$db = "PMA_db";
$table= "PMA_table";
$result = PMA_getHtmlContentsFor1NFStep4($db, $table);
$result = Normalization::getHtmlContentsFor1NFStep4($db, $table);
$this->assertInternalType('array', $result);
$this->assertArrayHasKey('legendText', $result);
$this->assertArrayHasKey('headText', $result);
@ -220,7 +220,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
$this->assertArrayHasKey('extra', $result);
$this->assertContains(__('Step 1.') . 4, $result['legendText']);
$this->assertContains(
PMA_getHtmlForColumnsList($db, $table, 'all', "checkbox"),
Normalization::getHtmlForColumnsList($db, $table, 'all', "checkbox"),
$result['extra']
);
$this->assertContains(
@ -230,7 +230,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
}
/**
* Test for PMA_getHtmlContentsFor1NFStep3
* Test for Normalization::getHtmlContentsFor1NFStep3
*
* @return void
*/
@ -238,7 +238,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
{
$db = "PMA_db";
$table= "PMA_table";
$result = PMA_getHtmlContentsFor1NFStep3($db, $table);
$result = Normalization::getHtmlContentsFor1NFStep3($db, $table);
$this->assertInternalType('array', $result);
$this->assertArrayHasKey('legendText', $result);
$this->assertArrayHasKey('headText', $result);
@ -247,7 +247,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
$this->assertArrayHasKey('primary_key', $result);
$this->assertContains(__('Step 1.') . 3, $result['legendText']);
$this->assertContains(
PMA_getHtmlForColumnsList($db, $table, 'all', "checkbox"),
Normalization::getHtmlForColumnsList($db, $table, 'all', "checkbox"),
$result['extra']
);
$this->assertContains(
@ -258,7 +258,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
}
/**
* Test for PMA_getHtmlFor2NFstep1
* Test for Normalization::getHtmlFor2NFstep1
*
* @return void
*/
@ -266,7 +266,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
{
$db = "PMA_db";
$table= "PMA_table";
$result = PMA_getHtmlFor2NFstep1($db, $table);
$result = Normalization::getHtmlFor2NFstep1($db, $table);
$this->assertInternalType('array', $result);
$this->assertArrayHasKey('legendText', $result);
$this->assertArrayHasKey('headText', $result);
@ -275,7 +275,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
$this->assertArrayHasKey('primary_key', $result);
$this->assertContains(__('Step 2.') . 1, $result['legendText']);
$this->assertEquals('id', $result['primary_key']);
$result1 = PMA_getHtmlFor2NFstep1($db, "PMA_table2");
$result1 = Normalization::getHtmlFor2NFstep1($db, "PMA_table2");
$this->assertEquals('id, col1', $result1['primary_key']);
$this->assertContains(
'<a href="#" id="showPossiblePd"',
@ -288,7 +288,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
}
/**
* Test for PMA_getHtmlForNewTables2NF
* Test for Normalization::getHtmlForNewTables2NF
*
* @return void
*/
@ -296,7 +296,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
{
$table= "PMA_table";
$partialDependencies = array('col1'=>array('col2'));
$result = PMA_getHtmlForNewTables2NF($partialDependencies, $table);
$result = Normalization::getHtmlForNewTables2NF($partialDependencies, $table);
$this->assertContains(
'<input type="text" name="col1"',
$result
@ -304,7 +304,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
}
/**
* Test for PMA_createNewTablesFor2NF
* Test for Normalization::createNewTablesFor2NF
*
* @return void
*/
@ -316,7 +316,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
$tablesName->id = 'PMA_table';
$tablesName->col1 = 'PMA_table1';
$partialDependencies = array('id'=>array('col2'));
$result = PMA_createNewTablesFor2NF(
$result = Normalization::createNewTablesFor2NF(
$partialDependencies, $tablesName, $table, $db
);
$this->assertInternalType('array', $result);
@ -324,7 +324,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
$this->assertArrayHasKey('headText', $result);
$this->assertArrayHasKey('queryError', $result);
$partialDependencies = array('id'=>array('col2'), 'col1'=>array('col2'));
$result1 = PMA_createNewTablesFor2NF(
$result1 = Normalization::createNewTablesFor2NF(
$partialDependencies, $tablesName, $table, $db
);
$this->assertArrayHasKey('extra', $result1);
@ -333,7 +333,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
}
/**
* Test for PMA_getHtmlForNewTables3NF
* Test for Normalization::getHtmlForNewTables3NF
*
* @return void
*/
@ -343,7 +343,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
$db = 'PMA_db';
$dependencies = new stdClass();
$dependencies->col1 = array('col2');
$result = PMA_getHtmlForNewTables3NF($dependencies, $tables, $db);
$result = Normalization::getHtmlForNewTables3NF($dependencies, $tables, $db);
$this->assertEquals(
array(
'html' => '',
@ -353,7 +353,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
);
$tables= array("PMA_table"=>array('col1', 'PMA_table'));
$dependencies->PMA_table = array('col4', 'col5');
$result1 = PMA_getHtmlForNewTables3NF($dependencies, $tables, $db);
$result1 = Normalization::getHtmlForNewTables3NF($dependencies, $tables, $db);
$this->assertInternalType('array', $result1);
$this->assertContains(
'<input type="text" name="PMA_table"',
@ -376,7 +376,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
}
/**
* Test for PMA_createNewTablesFor3NF
* Test for Normalization::createNewTablesFor3NF
*
* @return void
*/
@ -390,7 +390,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
$cols1->pk = 'col2';
$cols1->nonpk = 'col3, col4';
$newTables = array('PMA_table'=>array('PMA_table'=>$cols, 'table1'=>$cols1));
$result = PMA_createNewTablesFor3NF(
$result = Normalization::createNewTablesFor3NF(
$newTables, $db
);
$this->assertInternalType('array', $result);
@ -398,7 +398,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
$this->assertArrayHasKey('headText', $result);
$this->assertArrayHasKey('queryError', $result);
$newTables1 = array();
$result1 = PMA_createNewTablesFor3NF(
$result1 = Normalization::createNewTablesFor3NF(
$newTables1, $db
);
$this->assertArrayHasKey('queryError', $result1);
@ -407,7 +407,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
}
/**
* Test for PMA_moveRepeatingGroup
* Test for Normalization::moveRepeatingGroup
*
* @return void
*/
@ -419,7 +419,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
$newColumn = 'PMA_newCol';
$table= "PMA_table";
$db = 'PMA_db';
$result = PMA_moveRepeatingGroup(
$result = Normalization::moveRepeatingGroup(
$repeatingColumns, $primary_columns, $newTable, $newColumn, $table, $db
);
$this->assertInternalType('array', $result);
@ -431,7 +431,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
}
/**
* Test for PMA_getHtmlFor3NFstep1
* Test for Normalization::getHtmlFor3NFstep1
*
* @return void
*/
@ -439,7 +439,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
{
$db = "PMA_db";
$tables= array("PMA_table");
$result = PMA_getHtmlFor3NFstep1($db, $tables);
$result = Normalization::getHtmlFor3NFstep1($db, $tables);
$this->assertInternalType('array', $result);
$this->assertArrayHasKey('legendText', $result);
$this->assertArrayHasKey('headText', $result);
@ -454,20 +454,20 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
'<input type="checkbox" name="pd" value="col1"',
$result['extra']
);
$result1 = PMA_getHtmlFor3NFstep1($db, array("PMA_table2"));
$result1 = Normalization::getHtmlFor3NFstep1($db, array("PMA_table2"));
$this->assertEquals(
'', $result1['subText']
);
}
/**
* Test for PMA_getHtmlForNormalizetable
* Test for Normalization::getHtmlForNormalizetable
*
* @return void
*/
public function testPMAGetHtmlForNormalizetable()
{
$result = PMA_getHtmlForNormalizetable();
$result = Normalization::getHtmlForNormalizetable();
$this->assertContains(
'<form method="post" action="normalization.php"'
. ' name="normalize" id="normalizeTable"',
@ -481,14 +481,14 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
'2nf' => __('Second step of normalization (1NF+2NF)'),
'3nf' => __('Third step of normalization (1NF+2NF+3NF)'));
$html_tmp = PhpMyAdmin\Util::getRadioFields(
$html_tmp = Util::getRadioFields(
'normalizeTo', $choices, '1nf', true
);
$this->assertContains($html_tmp, $result);
}
/**
* Test for PMA_findPartialDependencies
* Test for Normalization::findPartialDependencies
*
* @return void
*/
@ -496,7 +496,7 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
{
$table= "PMA_table2";
$db = 'PMA_db';
$result = PMA_findPartialDependencies($table, $db);
$result = Normalization::findPartialDependencies($table, $db);
$this->assertContains(
'<div class="dependencies_box"',
$result
@ -505,14 +505,14 @@ class PMA_Normalization_Test extends PHPUnit_Framework_TestCase
}
/**
* Test for PMA_getAllCombinationPartialKeys
* Test for Normalization::getAllCombinationPartialKeys
*
* @return void
*/
public function testPMAGetAllCombinationPartialKeys()
{
$primaryKey = array('id', 'col1', 'col2');
$result = PMA_getAllCombinationPartialKeys($primaryKey);
$result = Normalization::getAllCombinationPartialKeys($primaryKey);
$this->assertEquals(
array('', 'id', 'col1', 'col1,id', 'col2', 'col2,id', 'col2,col1'),
$result