2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * set of functions used for normalization
8 use PMA\libraries\Message
;
9 use PMA\libraries\Util
;
10 use PMA\libraries\URL
;
13 * build the html for columns of $colTypeCategory category
14 * in form of given $listType in a table
16 * @param string $db current database
17 * @param string $table current table
18 * @param string $colTypeCategory supported all|Numeric|String|Spatial
19 * |Date and time using the _pgettext() format
20 * @param string $listType type of list to build, supported dropdown|checkbox
22 * @return string HTML for list of columns in form of given list types
24 function PMA_getHtmlForColumnsList(
25 $db, $table, $colTypeCategory='all', $listType='dropdown'
27 $columnTypeList = array();
28 if ($colTypeCategory != 'all') {
29 $types = $GLOBALS['PMA_Types']->getColumns();
30 $columnTypeList = $types[$colTypeCategory];
32 $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
33 $columns = $GLOBALS['dbi']->getColumns(
35 true, $GLOBALS['userlink']
39 foreach ($columns as $column => $def) {
40 if (isset($def['Type'])) {
41 $extracted_columnspec = Util
::extractColumnSpec($def['Type']);
42 $type = $extracted_columnspec['type'];
44 if (empty($columnTypeList)
45 ||
in_array(mb_strtoupper($type), $columnTypeList)
47 if ($listType == 'checkbox') {
48 $selectColHtml .= '<input type="checkbox" value="'
49 . htmlspecialchars($column) . '"/>'
50 . htmlspecialchars($column) . ' [ '
51 . htmlspecialchars($def['Type']) . ' ]</br>';
53 $selectColHtml .= '<option value="' . htmlspecialchars($column) . ''
54 . '">' . htmlspecialchars($column)
55 . ' [ ' . htmlspecialchars($def['Type']) . ' ]'
60 return $selectColHtml;
64 * get the html of the form to add the new column to given table
66 * @param integer $num_fields number of columns to add
67 * @param string $db current database
68 * @param string $table current table
69 * @param array $columnMeta array containing default values for the fields
73 function PMA_getHtmlForCreateNewColumn(
74 $num_fields, $db, $table, $columnMeta=array()
76 $cfgRelation = PMA_getRelationsParam();
77 $content_cells = array();
78 $available_mime = array();
80 if ($cfgRelation['mimework'] && $GLOBALS['cfg']['BrowseMIME']) {
81 $mime_map = PMA_getMIME($db, $table);
82 $available_mime = PMA_getAvailableMIMEtypes();
84 $comments_map = PMA_getComments($db, $table);
85 for ($columnNumber = 0; $columnNumber < $num_fields; $columnNumber++
) {
86 $content_cells[$columnNumber] = array(
87 'columnNumber' => $columnNumber,
88 'columnMeta' => $columnMeta,
90 'length_values_input_size' => 8,
92 'extracted_columnspec' => array(),
93 'submit_attribute' => null,
94 'comments_map' => $comments_map,
95 'fields_meta' => null,
97 'move_columns' => array(),
98 'cfgRelation' => $cfgRelation,
99 'available_mime' => isset($available_mime)?
$available_mime:array(),
100 'mime_map' => $mime_map
104 return PMA\libraries\Template
::get(
105 'columns_definitions/table_fields_definitions'
110 'fields_meta' => null,
111 'mimework' => $cfgRelation['mimework'],
112 'content_cells' => $content_cells
117 * build the html for step 1.1 of normalization
119 * @param string $db current database
120 * @param string $table current table
121 * @param string $normalizedTo up to which step normalization will go,
122 * possible values 1nf|2nf|3nf
124 * @return string HTML for step 1.1
126 function PMA_getHtmlFor1NFStep1($db, $table, $normalizedTo)
129 $stepTxt = __('Make all columns atomic');
130 $html = "<h3 class='center'>"
131 . __('First step of normalization (1NF)') . "</h3>";
132 $html .= "<div id='mainContent' data-normalizeto='" . $normalizedTo . "'>" .
134 "<legend>" . __('Step 1.') . $step . " " . $stepTxt . "</legend>" .
136 'Do you have any column which can be split into more than'
138 . 'For example: address can be split into street, city, country and zip.'
140 . "</br>(<a class='central_columns_dialog' data-maxrows='25' "
141 . "data-pick=false href='#'> "
143 'Show me the central list of columns that are not already in this table'
145 . "<p class='cm-em'>" . __(
146 'Select a column which can be split into more '
147 . 'than one (on select of \'no such column\', it\'ll move to next step).'
151 . "<select id='selectNonAtomicCol' name='makeAtomic'>"
152 . '<option selected="selected" disabled="disabled">'
153 . __('Select one…') . "</option>"
154 . "<option value='no_such_col'>" . __('No such column') . "</option>"
155 . PMA_getHtmlForColumnsList(
158 _pgettext('string types', 'String')
161 . "<span>" . __('split into ')
162 . "</span><input id='numField' type='number' value='2'>"
163 . "<input type='submit' id='splitGo' value='" . __('Go') . "'/></div>"
164 . "<div id='newCols'></div>"
165 . "</fieldset><fieldset class='tblFooters'>"
172 * build the html contents of various html elements in step 1.2
174 * @param string $db current database
175 * @param string $table current table
177 * @return string HTML contents for step 1.2
179 function PMA_getHtmlContentsFor1NFStep2($db, $table)
182 $stepTxt = __('Have a primary key');
183 $primary = PMA\libraries\Index
::getPrimary($table, $db);
184 $hasPrimaryKey = "0";
185 $legendText = __('Step 1.') . $step . " " . $stepTxt;
188 $headText = __("Primary key already exists.");
189 $subText = __("Taking you to next step…");
190 $hasPrimaryKey = "1";
193 "There is no primary key; please add one.<br/>"
194 . "Hint: A primary key is a column "
195 . "(or combination of columns) that uniquely identify all rows."
197 $subText = '<a href="#" id="createPrimaryKey">'
199 'b_index_add.png', __(
200 'Add a primary key on existing column(s)'
205 "If it's not possible to make existing "
206 . "column combinations as primary key"
208 . '<a href="#" id="addNewPrimary">'
209 . __('+ Add a new primary key column') . '</a>';
212 'legendText' => $legendText,
213 'headText' => $headText,
214 'subText' => $subText,
215 'hasPrimaryKey' => $hasPrimaryKey,
222 * build the html contents of various html elements in step 1.4
224 * @param string $db current database
225 * @param string $table current table
227 * @return string HTML contents for step 1.4
229 function PMA_getHtmlContentsFor1NFStep4($db, $table)
232 $stepTxt = __('Remove redundant columns');
233 $legendText = __('Step 1.') . $step . " " . $stepTxt;
235 "Do you have a group of columns which on combining gives an existing"
236 . " column? For example, if you have first_name, last_name and"
237 . " full_name then combining first_name and last_name gives full_name"
238 . " which is redundant."
241 "Check the columns which are redundant and click on remove. "
242 . "If no redundant column, click on 'No redundant column'"
244 $extra = PMA_getHtmlForColumnsList($db, $table, 'all', "checkbox") . "</br>"
245 . '<input type="submit" id="removeRedundant" value="'
246 . __('Remove selected') . '"/>'
247 . '<input type="submit" value="' . __('No redundant column')
248 . '" onclick="goToFinish1NF();"'
251 'legendText' => $legendText,
252 'headText' => $headText,
253 'subText' => $subText,
260 * build the html contents of various html elements in step 1.3
262 * @param string $db current database
263 * @param string $table current table
265 * @return string HTML contents for step 1.3
267 function PMA_getHtmlContentsFor1NFStep3($db, $table)
270 $stepTxt = __('Move repeating groups');
271 $legendText = __('Step 1.') . $step . " " . $stepTxt;
273 "Do you have a group of two or more columns that are closely "
274 . "related and are all repeating the same attribute? For example, "
275 . "a table that holds data on books might have columns such as book_id, "
276 . "author1, author2, author3 and so on which form a "
277 . "repeating group. In this case a new table (book_id, author) should "
281 "Check the columns which form a repeating group. "
282 . "If no such group, click on 'No repeating group'"
284 $extra = PMA_getHtmlForColumnsList($db, $table, 'all', "checkbox") . "</br>"
285 . '<input type="submit" id="moveRepeatingGroup" value="'
287 . '<input type="submit" value="' . __('No repeating group')
288 . '" onclick="goToStep4();"'
290 $primary = PMA\libraries\Index
::getPrimary($table, $db);
291 $primarycols = $primary->getColumns();
293 foreach ($primarycols as $col) {
294 $pk[] = $col->getName();
297 'legendText' => $legendText,
298 'headText' => $headText,
299 'subText' => $subText,
301 'primary_key' => json_encode($pk)
307 * build html contents for 2NF step 2.1
309 * @param string $db current database
310 * @param string $table current table
312 * @return string HTML contents for 2NF step 2.1
314 function PMA_getHtmlFor2NFstep1($db, $table)
316 $legendText = __('Step 2.') . "1 " . __('Find partial dependencies');
317 $primary = PMA\libraries\Index
::getPrimary($table, $db);
318 $primarycols = $primary->getColumns();
323 foreach ($primarycols as $col) {
324 $pk[] = $col->getName();
325 $selectPkForm .= '<input type="checkbox" name="pd" value="'
326 . htmlspecialchars($col->getName()) . '">'
327 . htmlspecialchars($col->getName());
329 $key = implode(', ', $pk);
330 if (count($primarycols) > 1) {
331 $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
332 $columns = (array) $GLOBALS['dbi']->getColumnNames(
333 $db, $table, $GLOBALS['userlink']
335 if (count($pk) == count($columns)) {
338 'No partial dependencies possible as '
339 . 'no non-primary column exists since primary key ( %1$s ) '
340 . 'is composed of all the columns in the table.'
341 ), htmlspecialchars($key)
343 $extra = '<h3>' . __('Table is already in second normal form.')
348 'The primary key ( %1$s ) consists of more than one column '
349 . 'so we need to find the partial dependencies.'
350 ), htmlspecialchars($key)
352 'Please answer the following question(s) '
353 . 'carefully to obtain a correct normalization.'
355 . '<br/><a href="#" id="showPossiblePd">' . __(
356 '+ Show me the possible partial dependencies '
357 . 'based on data in the table'
360 'For each column below, '
361 . 'please select the <b>minimal set</b> of columns among given set '
362 . 'whose values combined together are sufficient'
363 . ' to determine the value of the column.'
366 foreach ($columns as $column) {
367 if (!in_array($column, $pk)) {
369 $extra .= "<b>" . sprintf(
370 __('\'%1$s\' depends on:'), htmlspecialchars($column)
372 $extra .= '<form id="pk_' . $cnt . '" data-colname="'
373 . htmlspecialchars($column) . '" class="smallIndent">'
374 . $selectPkForm . '</form><br/><br/>';
381 'No partial dependencies possible as the primary key'
382 . ' ( %1$s ) has just one column.'
383 ), htmlspecialchars($key)
385 $extra = '<h3>' . __('Table is already in second normal form.') . '</h3>';
388 'legendText' => $legendText,
389 'headText' => $headText,
390 'subText' => $subText,
392 'primary_key' => $key
398 * build the html for showing the tables to have in order to put current table in 2NF
400 * @param array $partialDependencies array containing all the dependencies
401 * @param string $table current table
403 * @return string HTML
405 function PMA_getHtmlForNewTables2NF($partialDependencies,$table)
407 $html = '<p><b>' . sprintf(
409 'In order to put the '
410 . 'original table \'%1$s\' into Second normal form we need '
411 . 'to create the following tables:'
412 ), htmlspecialchars($table)
416 foreach ($partialDependencies as $key=>$dependents) {
417 $html .= '<p><input type="text" name="' . htmlspecialchars($key)
418 . '" value="' . htmlspecialchars($tableName) . '"/>'
419 . '( <u>' . htmlspecialchars($key) . '</u>'
420 . (count($dependents)>0?
', ':'')
421 . htmlspecialchars(implode(', ', $dependents)) . ' )';
423 $tableName = 'table' . $i;
429 * create/alter the tables needed for 2NF
431 * @param array $partialDependencies array containing all the partial dependencies
432 * @param object $tablesName name of new tables
433 * @param string $table current table
434 * @param string $db current database
438 function PMA_createNewTablesFor2NF($partialDependencies, $tablesName, $table, $db)
441 $nonPKCols = array();
444 $headText = '<h3>' . sprintf(
445 __('The second step of normalization is complete for table \'%1$s\'.'),
446 htmlspecialchars($table)
448 if (count((array)$partialDependencies) == 1) {
450 'legendText'=>__('End of step'), 'headText'=>$headText,
455 $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
456 foreach ($partialDependencies as $key=>$dependents) {
457 if ($tablesName->$key != $table) {
458 $backquotedKey = implode(', ', Util
::backquote(explode(', ', $key)));
459 $queries[] = 'CREATE TABLE ' . Util
::backquote($tablesName->$key)
460 . ' SELECT DISTINCT ' . $backquotedKey
461 . (count($dependents)>0?
', ':'')
462 . implode(',', Util
::backquote($dependents))
463 . ' FROM ' . Util
::backquote($table) . ';';
464 $queries[] = 'ALTER TABLE ' . Util
::backquote($tablesName->$key)
465 . ' ADD PRIMARY KEY(' . $backquotedKey . ');';
466 $nonPKCols = array_merge($nonPKCols, $dependents);
473 $query = 'ALTER TABLE ' . Util
::backquote($table);
474 foreach ($nonPKCols as $col) {
475 $query .= ' DROP ' . Util
::backquote($col) . ',';
477 $query = trim($query, ', ');
481 $queries[] = 'DROP TABLE ' . Util
::backquote($table);
483 foreach ($queries as $query) {
484 if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['userlink'])) {
485 $message = Message
::error(__('Error in processing!'));
486 $message->addMessage(
488 $GLOBALS['dbi']->getError($GLOBALS['userlink'])
497 'legendText' => __('End of step'),
498 'headText' => $headText,
499 'queryError' => $error,
505 * build the html for showing the new tables to have in order
506 * to put given tables in 3NF
508 * @param object $dependencies containing all the dependencies
509 * @param array $tables tables formed after 2NF and need to convert to 3NF
510 * @param string $db current database
512 * @return array containing html and the list of new tables
514 function PMA_getHtmlForNewTables3NF($dependencies, $tables, $db)
518 $newTables = array();
519 foreach ($tables as $table=>$arrDependson) {
520 if (count(array_unique($arrDependson)) == 1) {
523 $primary = PMA\libraries\Index
::getPrimary($table, $db);
524 $primarycols = $primary->getColumns();
526 foreach ($primarycols as $col) {
527 $pk[] = $col->getName();
529 $html .= '<p><b>' . sprintf(
531 'In order to put the '
532 . 'original table \'%1$s\' into Third normal form we need '
533 . 'to create the following tables:'
534 ), htmlspecialchars($table)
537 $columnList = array();
538 foreach ($arrDependson as $key) {
539 $dependents = $dependencies->$key;
540 if ($key == $table) {
541 $key = implode(', ', $pk);
543 $tmpTableCols =array_merge(explode(', ', $key), $dependents);
545 if (!in_array($tmpTableCols, $columnList)) {
546 $columnList[] = $tmpTableCols;
547 $html .= '<p><input type="text" name="'
548 . htmlspecialchars($tableName)
549 . '" value="' . htmlspecialchars($tableName) . '"/>'
550 . '( <u>' . htmlspecialchars($key) . '</u>'
551 . (count($dependents)>0?
', ':'')
552 . htmlspecialchars(implode(', ', $dependents)) . ' )';
553 $newTables[$table][$tableName] = array(
554 "pk"=>$key, "nonpk"=>implode(', ', $dependents)
557 $tableName = 'table' . $i;
561 return array('html' => $html, 'newTables' => $newTables, 'success' => true);
565 * create new tables or alter existing to get 3NF
567 * @param array $newTables list of new tables to be created
568 * @param string $db current database
572 function PMA_createNewTablesFor3NF($newTables, $db)
578 __('The third step of normalization is complete.')
580 if (count((array)$newTables) == 0) {
582 'legendText'=>__('End of step'), 'headText'=>$headText,
587 $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
588 foreach ($newTables as $originalTable=>$tablesList) {
589 foreach ($tablesList as $table=>$cols) {
590 if ($table != $originalTable) {
592 ', ', Util
::backquote(explode(', ', $cols->pk
))
594 $quotedNonpk = implode(
595 ', ', Util
::backquote(explode(', ', $cols->nonpk
))
597 $queries[] = 'CREATE TABLE ' . Util
::backquote($table)
598 . ' SELECT DISTINCT ' . $quotedPk
599 . ', ' . $quotedNonpk
600 . ' FROM ' . Util
::backquote($originalTable) . ';';
601 $queries[] = 'ALTER TABLE ' . Util
::backquote($table)
602 . ' ADD PRIMARY KEY(' . $quotedPk . ');';
608 $columns = (array) $GLOBALS['dbi']->getColumnNames(
609 $db, $originalTable, $GLOBALS['userlink']
611 $colPresent = array_merge(
612 explode(', ', $dropCols->pk
), explode(', ', $dropCols->nonpk
)
614 $query = 'ALTER TABLE ' . Util
::backquote($originalTable);
615 foreach ($columns as $col) {
616 if (!in_array($col, $colPresent)) {
617 $query .= ' DROP ' . Util
::backquote($col) . ',';
620 $query = trim($query, ', ');
624 $queries[] = 'DROP TABLE ' . Util
::backquote($originalTable);
628 foreach ($queries as $query) {
629 if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['userlink'])) {
630 $message = Message
::error(__('Error in processing!'));
631 $message->addMessage(
633 $GLOBALS['dbi']->getError($GLOBALS['userlink'])
642 'legendText' => __('End of step'),
643 'headText' => $headText,
644 'queryError' => $error,
649 * move the repeating group of columns to a new table
651 * @param string $repeatingColumns comma separated list of repeating group columns
652 * @param string $primary_columns comma separated list of column in primary key
654 * @param string $newTable name of the new table to be created
655 * @param string $newColumn name of the new column in the new table
656 * @param string $table current table
657 * @param string $db current database
661 function PMA_moveRepeatingGroup(
662 $repeatingColumns, $primary_columns, $newTable, $newColumn, $table, $db
664 $repeatingColumnsArr = (array)Util
::backquote(
665 explode(', ', $repeatingColumns)
667 $primary_columns = implode(
668 ',', Util
::backquote(explode(',', $primary_columns))
670 $query1 = 'CREATE TABLE ' . Util
::backquote($newTable);
671 $query2 = 'ALTER TABLE ' . Util
::backquote($table);
672 $message = Message
::success(
674 __('Selected repeating group has been moved to the table \'%s\''),
675 htmlspecialchars($table)
680 foreach ($repeatingColumnsArr as $repeatingColumn) {
682 $query1 .= ' UNION ';
685 $query1 .= ' SELECT ' . $primary_columns . ',' . $repeatingColumn
686 . ' as ' . Util
::backquote($newColumn)
687 . ' FROM ' . Util
::backquote($table);
688 $query2 .= ' DROP ' . $repeatingColumn . ',';
690 $query2 = trim($query2, ',');
691 $queries = array($query1, $query2);
692 $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
693 foreach ($queries as $query) {
694 if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['userlink'])) {
695 $message = Message
::error(__('Error in processing!'));
696 $message->addMessage(
698 $GLOBALS['dbi']->getError($GLOBALS['userlink'])
707 'queryError' => $error, 'message' => $message
712 * build html for 3NF step 1 to find the transitive dependencies
714 * @param string $db current database
715 * @param array $tables tables formed after 2NF and need to process for 3NF
719 function PMA_getHtmlFor3NFstep1($db, $tables)
721 $legendText = __('Step 3.') . "1 " . __('Find transitive dependencies');
724 'Please answer the following question(s) '
725 . 'carefully to obtain a correct normalization.'
728 'For each column below, '
729 . 'please select the <b>minimal set</b> of columns among given set '
730 . 'whose values combined together are sufficient'
731 . ' to determine the value of the column.<br />'
732 . 'Note: A column may have no transitive dependency, '
733 . 'in that case you don\'t have to select any.'
736 foreach ($tables as $table) {
737 $primary = PMA\libraries\Index
::getPrimary($table, $db);
738 $primarycols = $primary->getColumns();
741 foreach ($primarycols as $col) {
742 $pk[] = $col->getName();
744 $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
745 $columns = (array) $GLOBALS['dbi']->getColumnNames(
746 $db, $table, $GLOBALS['userlink']
748 if (count($columns) - count($pk) <= 1) {
751 foreach ($columns as $column) {
752 if (!in_array($column, $pk)) {
753 $selectTdForm .= '<input type="checkbox" name="pd" value="'
754 . htmlspecialchars($column) . '">'
755 . '<span>' . htmlspecialchars($column) . '</span>';
758 foreach ($columns as $column) {
759 if (!in_array($column, $pk)) {
761 $extra .= "<b>" . sprintf(
762 __('\'%1$s\' depends on:'), htmlspecialchars($column)
765 $extra .= '<form id="td_' . $cnt . '" data-colname="'
766 . htmlspecialchars($column) . '" data-tablename="'
767 . htmlspecialchars($table) . '" class="smallIndent">'
769 . '</form><br/><br/>';
775 "No Transitive dependencies possible as the table "
776 . "doesn't have any non primary key columns"
779 $extra = "<h3>" . __("Table is already in Third normal form!") . "</h3>";
782 'legendText' => $legendText,
783 'headText' => $headText,
784 'subText' => $subText,
790 * get html for options to normalize table
792 * @return string HTML
794 function PMA_getHtmlForNormalizetable()
796 $html_output = '<form method="post" action="normalization.php" '
797 . 'name="normalize" '
798 . 'id="normalizeTable" '
800 . URL
::getHiddenInputs($GLOBALS['db'], $GLOBALS['table'])
801 . '<input type="hidden" name="step1" value="1">';
802 $html_output .= '<fieldset>';
803 $html_output .= '<legend>'
804 . __('Improve table structure (Normalization):') . '</legend>';
805 $html_output .= '<h3>' . __('Select up to what step you want to normalize')
808 '1nf' => __('First step of normalization (1NF)'),
809 '2nf' => __('Second step of normalization (1NF+2NF)'),
810 '3nf' => __('Third step of normalization (1NF+2NF+3NF)'));
812 $html_output .= Util
::getRadioFields(
813 'normalizeTo', $choices, '1nf', true
815 $html_output .= '</fieldset><fieldset class="tblFooters">'
816 . "<span class='floatleft'>" . __(
817 'Hint: Please follow the procedure carefully in order '
818 . 'to obtain correct normalization'
820 . '<input type="submit" name="submit_normalize" value="' . __('Go') . '" />'
829 * find all the possible partial dependencies based on data in the table.
831 * @param string $table current table
832 * @param string $db current database
834 * @return string HTML containing the list of all the possible partial dependencies
836 function PMA_findPartialDependencies($table, $db)
838 $dependencyList = array();
839 $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
840 $columns = (array) $GLOBALS['dbi']->getColumnNames(
841 $db, $table, $GLOBALS['userlink']
843 $columns = (array)Util
::backquote($columns);
844 $totalRowsRes = $GLOBALS['dbi']->fetchResult(
845 'SELECT COUNT(*) FROM (SELECT * FROM '
846 . Util
::backquote($table) . ' LIMIT 500) as dt;'
848 $totalRows = $totalRowsRes[0];
849 $primary = PMA\libraries\Index
::getPrimary($table, $db);
850 $primarycols = $primary->getColumns();
852 foreach ($primarycols as $col) {
853 $pk[] = Util
::backquote($col->getName());
855 $partialKeys = PMA_getAllCombinationPartialKeys($pk);
856 $distinctValCount = PMA_findDistinctValuesCount(
858 array_merge($columns, $partialKeys)
861 foreach ($columns as $column) {
862 if (!in_array($column, $pk)) {
863 foreach ($partialKeys as $partialKey) {
865 && PMA_checkPartialDependency(
866 $partialKey, $column, $table,
867 $distinctValCount[$partialKey],
868 $distinctValCount[$column], $totalRows
871 $dependencyList[$partialKey][] = $column;
878 'This list is based on a subset of the table\'s data '
879 . 'and is not necessarily accurate. '
881 . '<div class="dependencies_box">';
882 foreach ($dependencyList as $dependon=>$colList) {
883 $html .= '<span class="displayblock">'
884 . '<input type="button" class="pickPd" value="' . __('Pick') . '"/>'
885 . '<span class="determinants">'
886 . htmlspecialchars(str_replace('`', '', $dependon)) . '</span> -> '
887 . '<span class="dependents">'
888 . htmlspecialchars(str_replace('`', '', implode(', ', $colList)))
892 if (empty($dependencyList)) {
893 $html .= '<p class="displayblock desc">'
894 . __('No partial dependencies found!') . '</p>';
900 * check whether a particular column is dependent on given subset of primary key
902 * @param string $partialKey the partial key, subset of primary key,
903 * each column in key supposed to be backquoted
904 * @param string $column backquoted column on whose dependency being checked
905 * @param string $table current table
906 * @param integer $pkCnt distinct value count for given partial key
907 * @param integer $colCnt distinct value count for given column
908 * @param integer $totalRows total distinct rows count of the table
910 * @return boolean TRUE if $column is dependent on $partialKey, False otherwise
912 function PMA_checkPartialDependency(
913 $partialKey, $column, $table, $pkCnt, $colCnt, $totalRows
916 . 'COUNT(DISTINCT ' . $partialKey . ',' . $column . ') as pkColCnt '
917 . 'FROM (SELECT * FROM ' . Util
::backquote($table)
918 . ' LIMIT 500) as dt' . ';';
919 $res = $GLOBALS['dbi']->fetchResult($query, null, null, $GLOBALS['userlink']);
921 if ($pkCnt && $pkCnt == $colCnt && $colCnt == $pkColCnt) {
924 if ($totalRows && $totalRows == $pkCnt) {
931 * function to get distinct values count of all the column in the array $columns
933 * @param array $columns array of backquoted columns whose distinct values
934 * need to be counted.
935 * @param string $table table to which these columns belong
937 * @return array associative array containing the count
939 function PMA_findDistinctValuesCount($columns, $table)
943 foreach ($columns as $column) {
944 if ($column) { //each column is already backquoted
945 $query .= 'COUNT(DISTINCT ' . $column . ') as \''
946 . $column . '_cnt\', ';
949 $query = trim($query, ', ');
950 $query .= ' FROM (SELECT * FROM ' . Util
::backquote($table)
951 . ' LIMIT 500) as dt' . ';';
952 $res = $GLOBALS['dbi']->fetchResult($query, null, null, $GLOBALS['userlink']);
953 foreach ($columns as $column) {
955 $result[$column] = $res[0][$column . '_cnt'];
962 * find all the possible partial keys
964 * @param array $primaryKey array containing all the column present in primary key
966 * @return array containing all the possible partial keys(subset of primary key)
968 function PMA_getAllCombinationPartialKeys($primaryKey)
970 $results = array('');
971 foreach ($primaryKey as $element) {
972 foreach ($results as $combination) {
974 $results, trim($element . ',' . $combination, ',')
978 array_pop($results); //remove key which consist of all primary key columns