" . __(
'Select a column which can be split into more '
. 'than one (on select of \'no such column\', it\'ll move to next step).',
)
. '
'
. "
"
. "'
. '' . __('split into ')
. ""
. '
'
. ""
. '
'
. '
';
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 array{legendText: string, headText: string, subText: string, hasPrimaryKey: string, extra: string}
*/
public function getHtmlContentsFor1NFStep2(string $db, string $table): array
{
$step = 2;
$stepTxt = __('Have a primary key');
$primary = Index::getPrimary($this->dbi, $table, $db);
$hasPrimaryKey = '0';
$legendText = __('Step 1.') . $step . ' ' . $stepTxt;
$extra = '';
if ($primary !== null) {
$headText = __('Primary key already exists.');
$subText = __('Taking you to next step…');
$hasPrimaryKey = '1';
} else {
$headText = __(
'There is no primary key; please add one. '
. 'Hint: A primary key is a column '
. '(or combination of columns) that uniquely identify all rows.',
);
$subText = ''
. Generator::getIcon(
'b_index_add',
__(
'Add a primary key on existing column(s)',
),
)
. '';
$extra = __('If it\'s not possible to make existing column combinations as primary key') . ' '
. ''
. __('+ Add a new primary key column') . '';
}
return [
'legendText' => $legendText,
'headText' => $headText,
'subText' => $subText,
'hasPrimaryKey' => $hasPrimaryKey,
'extra' => $extra,
];
}
/**
* build the html contents of various html elements in step 1.4
*
* @param string $db current database
* @param string $table current table
*
* @return array{legendText: string, headText: string, subText: string, extra: string} HTML contents for step 1.4
*/
public function getHtmlContentsFor1NFStep4(string $db, string $table): array
{
$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 = $this->getHtmlForColumnsList($db, $table, 'all', 'checkbox') . ' '
. ''
. '';
return ['legendText' => $legendText, 'headText' => $headText, 'subText' => $subText, 'extra' => $extra];
}
/**
* build the html contents of various html elements in step 1.3
*
* @param string $db current database
* @param string $table current table
*
* @return array{legendText: string, headText: string, subText: string, extra: string, primary_key: false|string}
*/
public function getHtmlContentsFor1NFStep3(string $db, string $table): array
{
$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 = $this->getHtmlForColumnsList($db, $table, 'all', 'checkbox') . ' '
. ''
. '';
$primary = Index::getPrimary($this->dbi, $table, $db);
$primarycols = $primary === null ? [] : $primary->getColumns();
$pk = [];
foreach ($primarycols as $col) {
$pk[] = $col->getName();
}
return [
'legendText' => $legendText,
'headText' => $headText,
'subText' => $subText,
'extra' => $extra,
'primary_key' => json_encode($pk),
];
}
/**
* build html contents for 2NF step 2.1
*
* @param string $db current database
* @param string $table current table
*
* @return array{legendText: string, headText: string, subText: string, extra: string, primary_key: string}
*/
public function getHtmlFor2NFstep1(string $db, string $table): array
{
$legendText = __('Step 2.') . '1 ' . __('Find partial dependencies');
$primary = Index::getPrimary($this->dbi, $table, $db);
$primarycols = $primary === null ? [] : $primary->getColumns();
$pk = [];
$subText = '';
$selectPkForm = '';
$extra = '';
foreach ($primarycols as $col) {
$pk[] = $col->getName();
$selectPkForm .= ''
. htmlspecialchars($col->getName());
}
$key = implode(', ', $pk);
if (count($primarycols) > 1) {
$this->dbi->selectDb($db);
$columns = $this->dbi->getColumnNames($db, $table);
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),
) . ' ';
$extra = '
' . __('Table is already in second normal form.')
. '
';
} else {
$headText = sprintf(
__(
'The primary key ( %1$s ) consists of more than one column '
. 'so we need to find the partial dependencies.',
),
htmlspecialchars($key),
) . ' ' . __('Please answer the following question(s) carefully to obtain a correct normalization.')
. ' ' . __(
'+ Show me the possible partial dependencies based on data in the table',
) . '';
$subText = __(
'For each column below, '
. 'please select the minimal set 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)) {
continue;
}
$cnt++;
$extra .= '' . sprintf(
__('\'%1$s\' depends on:'),
htmlspecialchars($column),
) . ' ';
$extra .= '
';
}
}
} else {
$headText = sprintf(
__(
'No partial dependencies possible as the primary key ( %1$s ) has just one column.',
),
htmlspecialchars($key),
) . ' ';
$extra = '
' . __('Table is already in second normal form.') . '
';
}
return [
'legendText' => $legendText,
'headText' => $headText,
'subText' => $subText,
'extra' => $extra,
'primary_key' => $key,
];
}
/**
* build the html for showing the tables to have in order to put current table in 2NF
*
* @param mixed[] $partialDependencies array containing all the dependencies
* @param string $table current table
*
* @return string HTML
*/
public function getHtmlForNewTables2NF(array $partialDependencies, string $table): string
{
$html = '
' . sprintf(
__(
'In order to put the '
. 'original table \'%1$s\' into Second normal form we need '
. 'to create the following tables:',
),
htmlspecialchars($table),
) . '
',
);
$error = true;
break;
}
}
return [
'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 mixed[] $tables tables formed after 2NF and need to convert to 3NF
* @param string $db current database
*
* @return mixed[] containing html and the list of new tables
*/
public function getHtmlForNewTables3NF(object $dependencies, array $tables, string $db): array
{
$html = '';
$i = 1;
$newTables = [];
foreach ($tables as $table => $arrDependson) {
if (count(array_unique($arrDependson)) === 1) {
continue;
}
$primary = Index::getPrimary($this->dbi, $table, $db);
$primarycols = $primary === null ? [] : $primary->getColumns();
$pk = [];
foreach ($primarycols as $col) {
$pk[] = $col->getName();
}
$html .= '
' . sprintf(
__(
'In order to put the '
. 'original table \'%1$s\' into Third normal form we need '
. 'to create the following tables:',
),
htmlspecialchars($table),
) . '
',
);
$error = true;
break;
}
}
return [
'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 $primaryColumns 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{queryError: bool, message: Message}
*/
public function moveRepeatingGroup(
string $repeatingColumns,
string $primaryColumns,
string $newTable,
string $newColumn,
string $table,
string $db,
): array {
$repeatingColumnsArr = explode(', ', $repeatingColumns);
$primaryColumnsArray = explode(',', $primaryColumns);
$columns = [];
foreach ($primaryColumnsArray as $column) {
$columns[] = Util::backquote($column);
}
$primaryColumns = implode(',', $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;
$quotedRepeatingColumn = Util::backquote($repeatingColumn);
$query1 .= ' SELECT ' . $primaryColumns . ',' . $quotedRepeatingColumn
. ' as ' . Util::backquote($newColumn)
. ' FROM ' . Util::backquote($table);
$query2 .= ' DROP ' . $quotedRepeatingColumn . ',';
}
$query2 = trim($query2, ',');
$queries = [$query1, $query2];
$this->dbi->selectDb($db);
foreach ($queries as $query) {
if (! $this->dbi->tryQuery($query)) {
$message = Message::error(__('Error in processing!'));
$message->addMessage(
Message::rawError($this->dbi->getError()),
'
',
);
$error = true;
break;
}
}
return ['queryError' => $error, 'message' => $message];
}
/**
* build html for 3NF step 1 to find the transitive dependencies
*
* @param string $db current database
* @param mixed[] $tables tables formed after 2NF and need to process for 3NF
*
* @return array{legendText: string, headText: string, subText: string, extra: string}
*/
public function getHtmlFor3NFstep1(string $db, array $tables): array
{
$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 minimal set of columns among given set '
. 'whose values combined together are sufficient'
. ' to determine the value of the column. '
. '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($this->dbi, $table, $db);
$primarycols = $primary === null ? [] : $primary->getColumns();
$selectTdForm = '';
$pk = [];
foreach ($primarycols as $col) {
$pk[] = $col->getName();
}
$this->dbi->selectDb($db);
$columns = $this->dbi->getColumnNames($db, $table);
if (count($columns) - count($pk) <= 1) {
continue;
}
foreach ($columns as $column) {
if (in_array($column, $pk)) {
continue;
}
$selectTdForm .= ''
. '' . htmlspecialchars($column) . '';
}
foreach ($columns as $column) {
if (in_array($column, $pk)) {
continue;
}
$cnt++;
$extra .= '' . sprintf(
__('\'%1$s\' depends on:'),
htmlspecialchars($column),
)
. ' ';
$extra .= '
';
}
}
if ($extra == '') {
$headText = __(
'No Transitive dependencies possible as the table doesn\'t have any non primary key columns',
);
$subText = '';
$extra = '
' . __('Table is already in Third normal form!') . '
';
}
return ['legendText' => $legendText, 'headText' => $headText, 'subText' => $subText, 'extra' => $extra];
}
/**
* 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 function findPartialDependencies(string $table, string $db): string
{
$dependencyList = [];
$this->dbi->selectDb($db);
$columnNames = $this->dbi->getColumnNames($db, $table);
$columns = [];
foreach ($columnNames as $column) {
$columns[] = Util::backquote($column);
}
$totalRowsRes = $this->dbi->fetchResult(
'SELECT COUNT(*) FROM (SELECT * FROM '
. Util::backquote($table) . ' LIMIT 500) as dt;',
);
$totalRows = $totalRowsRes[0];
$primary = Index::getPrimary($this->dbi, $table, $db);
$primarycols = $primary === null ? [] : $primary->getColumns();
$pk = [];
foreach ($primarycols as $col) {
$pk[] = Util::backquote($col->getName());
}
$partialKeys = $this->getAllCombinationPartialKeys($pk);
$distinctValCount = $this->findDistinctValuesCount(
array_unique(
array_merge($columns, $partialKeys),
),
$table,
);
foreach ($columns as $column) {
if (in_array($column, $pk)) {
continue;
}
foreach ($partialKeys as $partialKey) {
if (
! $partialKey
|| ! $this->checkPartialDependency(
$partialKey,
$column,
$table,
$distinctValCount[$partialKey],
$distinctValCount[$column],
$totalRows,
)
) {
continue;
}
$dependencyList[$partialKey][] = $column;
}
}
$html = __('This list is based on a subset of the table\'s data and is not necessarily accurate. ')
. '