From 36d56fb2ea56e47b3979cfb07ff3ad5a1b4f1b2f Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Tue, 15 May 2012 18:53:28 +0530 Subject: [PATCH 01/13] Refactoring tbl_select.php --- tbl_select.php | 414 ++++++++++++++++++++++++++++++------------------- 1 file changed, 256 insertions(+), 158 deletions(-) diff --git a/tbl_select.php b/tbl_select.php index 611b124e53..a860dd1bbb 100644 --- a/tbl_select.php +++ b/tbl_select.php @@ -24,8 +24,6 @@ $GLOBALS['js_include'][] = 'tbl_change.js'; $GLOBALS['js_include'][] = 'jquery/timepicker.js'; $GLOBALS['js_include'][] = 'gis_data_editor.js'; -$titles['Browse'] = PMA_getIcon('b_browse.png', __('Browse foreign values')); - $geom_types = PMA_getGISDatatypes(); $post_params = array( @@ -81,164 +79,12 @@ if (! isset($param) || $param[0] == '') { // check also foreigners even if relwork is FALSE (to get // foreign keys from innodb) $foreigners = PMA_getForeigners($db, $table); - ?> -
- - -
> - - - - - -
- - -
-
-
-
- '; + // if a geometry column is present + if (in_array($fields_type[$i], $geom_types)) { + $retval .= ''; + } else { + $retval .= ' '; + } + $retval .= ''; + return $retval; +} + +/** + * Displays formatted HTML for extra search options (slider) in table search form + * + * @param array $fields_list array containing types of all columns + * in the table + * @param integer $fields_cnt number of fields in the table + * + * @return void + */ +function PMA_tbl_search_displaySliderOptions($fields_list, $fields_cnt) +{ + PMA_generateSliderEffect('searchoptions', __('Options')); + $output_html = ''; + + /** + * Displays columns select list for selecting distinct columns in the search + */ + $output_html = '
+ ' . __('Select columns (at least one):') . ' + + +
'; + + /** + * Displays input box for custom 'Where' clause to be used in the search + */ + $output_html .= '
+ ' . '' . __('Or') . ' ' . + __('Add search conditions (body of the "where" clause):') . ''; + $output_html .= PMA_showMySQLDocu('SQL-Syntax', 'Functions'); + $output_html .= ' +
'; + + /** + * Displays option of changing default number of rows displayed per page + */ + $output_html .= '
+ ' . __('Number of rows per page') . ' + +
'; + + /** + * Displays option for ordering search results by a column value (Asc or Desc) + */ + $output_html .= '
+ ' . __('Display order:') . ' + '; + $choices = array( + 'ASC' => __('Ascending'), + 'DESC' => __('Descending') + ); + echo $output_html; + PMA_displayHtmlRadio('order', $choices, 'ASC', false, true, "formelement"); + unset($choices); +} + +/** + * Generates HTML for displaying fields table in search form + * + * @param array $fields_list Names of columns in the table + * @param array $fields_type Types of columns in the table + * @param array $fields_collation Collation of all columns + * @param array $fields_null Null information of columns + * @param boolean $geom_column_present Whether a geometry column is present + * @param array $geom_types array of GIS data types + * @param integer $fields_cnt Number of columns in the table + * @param array $foreigners Array of foreign keys + * @param string $db Selected database + * @param string $table Selected table + * + * @return string the generated HTML + */ +function PMA_tbl_search_getFieldsTableHtml($fields_list, $fields_type, +$fields_collation, $fields_null, $geom_column_present, $geom_types, $fields_cnt, +$foreigners, $db, $table) +{ + $retval = ''; + $retval .= ''; + $retval .= PMA_tbl_setTableHeader($geom_column_present) . ''; + $odd_row = true; + $titles['Browse'] = PMA_getIcon('b_browse.png', __('Browse foreign values')); + + // for every column present in table + for ($i = 0; $i < $fields_cnt; $i++) { + $retval .= ''; + $odd_row = !$odd_row; + + /** + * If 'Function' column is present + */ + $retval .= PMA_tbl_search_getGeomFuncHtml( + $geom_column_present, $fields_type, $geom_types, $i + ); + /** + * Displays column's name, type, collation + */ + $retval .= ''; + $retval .= ''; + $retval .= ''; + /** + * Displays column's comparison operators depending on column type + */ + $retval .= ''; + } // end for + + $retval .= '
' . htmlspecialchars($fields_list[$i]) . '' . htmlspecialchars($fields_type[$i]) . '' . $fields_collation[$i] . ''; + /** + * Displays column's foreign relations if any + */ + $field = $fields_list[$i]; + $foreignData = PMA_getForeignData($foreigners, $field, false, '', ''); + $retval .= PMA_getForeignFields_Values( + $foreigners, $foreignData, $field, $fields_type, $i, $db, $table, + $titles, $GLOBALS['cfg']['ForeignKeyMaxLimit'], '', true + ); + + $retval .= '' + . '
'; + return $retval; +} + +/** + * Displays the table search form under table search tab + * + * @param array $fields_list Names of columns in the table + * @param array $fields_type Types of columns in the table + * @param array $fields_collation Collation of all columns + * @param array $fields_null Null information of columns + * @param boolean $geom_column_present Whether a geometry column is present + * @param array $geom_types array of GIS data types + * @param integer $fields_cnt Number of columns in the table + * @param array $foreigners Array of foreign keys + * @param string $db Selected database + * @param string $table Selected table + * + * @return void + */ +function PMA_tbl_search_displaySelectionForm($fields_list, $fields_type, +$fields_collation, $fields_null, $geom_column_present, $geom_types, $fields_cnt, +$foreigners, $db, $table) +{ + $output_html = ''; + $output_html .= '
'; + $url_params = array(); + $url_params['db'] = $db; + $url_params['table'] = $table; + + $output_html .= PMA_generateHtmlTabs(PMA_tbl_getSubTabs(), $url_params, 'topmenu2'); + $output_html .= '
'; + $output_html .= PMA_generate_common_hidden_inputs($db, $table); + $output_html .= ''; + $output_html .= '' + . '
'; + + /** + * Displays selection form's footer elements + */ + $output_html .= '
' + . '' + . '
'; + echo $output_html; +} ?> From 0265058ca7166e48996baa374904299d3e6333bd Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Tue, 15 May 2012 21:46:49 +0530 Subject: [PATCH 02/13] Fix doc comment in 36d56fb2ea56e47b3979cfb07ff3ad5a1b4f1b2f --- tbl_select.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tbl_select.php b/tbl_select.php index a860dd1bbb..ddb45e1af5 100644 --- a/tbl_select.php +++ b/tbl_select.php @@ -153,7 +153,7 @@ if (! isset($param) || $param[0] == '') { * @param array $fields_type array containing types of all columns * in the table * @param array $geom_types array of GIS data types - * @param array $i column index + * @param integer $i column index * * @return string the generated HTML */ From f9a0537fe479aa205e7ef34a8b223af1b6b1b23b Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Wed, 16 May 2012 14:53:35 +0530 Subject: [PATCH 03/13] Improvements and fixes in tbl_select.php refactoring --- tbl_select.php | 121 +++++++++++++++++++++++++------------------------ 1 file changed, 61 insertions(+), 60 deletions(-) diff --git a/tbl_select.php b/tbl_select.php index ddb45e1af5..5f3c995dbc 100644 --- a/tbl_select.php +++ b/tbl_select.php @@ -80,8 +80,8 @@ if (! isset($param) || $param[0] == '') { // foreign keys from innodb) $foreigners = PMA_getForeigners($db, $table); - PMA_tbl_search_displaySelectionForm( - $fields_list, $fields_type, $fields_collation, $fields_null, + PMA_tblSearchDisplaySelectionForm( + $goto, $fields_list, $fields_type, $fields_collation, $fields_null, $geom_column_present, $geom_types, $fields_cnt, $foreigners, $db, $table ); @@ -153,42 +153,42 @@ if (! isset($param) || $param[0] == '') { * @param array $fields_type array containing types of all columns * in the table * @param array $geom_types array of GIS data types - * @param integer $i column index + * @param integer $column_index index of current column in $fields_type array * * @return string the generated HTML */ -function PMA_tbl_search_getGeomFuncHtml($geom_column_present, $fields_type, -$geom_types, $i) +function PMA_tblSearchGetGeomFuncHtml($geom_column_present, $fields_type, +$geom_types, $column_index) { - $retval = ''; + $html_output = ''; // return if geometrical column is not present if (!$geom_column_present) { - return $retval; + return $html_output; } /** * Displays 'Function' column if it is present */ - $retval .= ''; + $html_output .= ''; // if a geometry column is present - if (in_array($fields_type[$i], $geom_types)) { - $retval .= ''; // get the relevant list of GIS functions - $funcs = PMA_getGISFunctions($fields_type[$i], true, true); + $funcs = PMA_getGISFunctions($fields_type[$column_index], true, true); /** * For each function in the list of functions, add an option to select list */ foreach ($funcs as $func_name => $func) { $name = isset($func['display']) ? $func['display'] : $func_name; - $retval .= ''; } - $retval .= ''; + $html_output .= ''; } else { - $retval .= ' '; + $html_output .= ' '; } - $retval .= ''; - return $retval; + $html_output .= ''; + return $html_output; } /** @@ -200,42 +200,42 @@ $geom_types, $i) * * @return void */ -function PMA_tbl_search_displaySliderOptions($fields_list, $fields_cnt) +function PMA_tblSearchDisplaySliderOptions($fields_list, $fields_cnt) { PMA_generateSliderEffect('searchoptions', __('Options')); - $output_html = ''; + $html_output = ''; /** * Displays columns select list for selecting distinct columns in the search */ - $output_html = '
+ $html_output = '
' . __('Select columns (at least one):') . ' + $html_output .= '
'; /** * Displays input box for custom 'Where' clause to be used in the search */ - $output_html .= '
+ $html_output .= '
' . '' . __('Or') . ' ' . __('Add search conditions (body of the "where" clause):') . ''; - $output_html .= PMA_showMySQLDocu('SQL-Syntax', 'Functions'); - $output_html .= ' + $html_output .= PMA_showMySQLDocu('SQL-Syntax', 'Functions'); + $html_output .= '
'; /** * Displays option of changing default number of rows displayed per page */ - $output_html .= '
+ $html_output .= '
' . __('Number of rows per page') . ' @@ -244,20 +244,20 @@ function PMA_tbl_search_displaySliderOptions($fields_list, $fields_cnt) /** * Displays option for ordering search results by a column value (Asc or Desc) */ - $output_html .= '
+ $html_output .= '
' . __('Display order:') . ' '; + $html_output .= ''; $choices = array( 'ASC' => __('Ascending'), 'DESC' => __('Descending') ); - echo $output_html; + echo $html_output; PMA_displayHtmlRadio('order', $choices, 'ASC', false, true, "formelement"); unset($choices); } @@ -278,65 +278,66 @@ function PMA_tbl_search_displaySliderOptions($fields_list, $fields_cnt) * * @return string the generated HTML */ -function PMA_tbl_search_getFieldsTableHtml($fields_list, $fields_type, +function PMA_tblSearchGetFieldsTableHtml($fields_list, $fields_type, $fields_collation, $fields_null, $geom_column_present, $geom_types, $fields_cnt, $foreigners, $db, $table) { - $retval = ''; - $retval .= ''; - $retval .= PMA_tbl_setTableHeader($geom_column_present) . ''; + $html_output = ''; + $html_output .= '
'; + $html_output .= PMA_tbl_setTableHeader($geom_column_present) . ''; $odd_row = true; $titles['Browse'] = PMA_getIcon('b_browse.png', __('Browse foreign values')); // for every column present in table for ($i = 0; $i < $fields_cnt; $i++) { - $retval .= ''; + $html_output .= ''; $odd_row = !$odd_row; /** * If 'Function' column is present */ - $retval .= PMA_tbl_search_getGeomFuncHtml( + $html_output .= PMA_tblSearchGetGeomFuncHtml( $geom_column_present, $fields_type, $geom_types, $i ); /** * Displays column's name, type, collation */ - $retval .= ''; - $retval .= ''; - $retval .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; /** * Displays column's comparison operators depending on column type */ - $retval .= ''; } // end for - $retval .= '
' . htmlspecialchars($fields_list[$i]) . '' . htmlspecialchars($fields_type[$i]) . '' . $fields_collation[$i] . '' . htmlspecialchars($fields_list[$i]) . '' . htmlspecialchars($fields_type[$i]) . '' . $fields_collation[$i] . ''; + $html_output .= ''; /** * Displays column's foreign relations if any */ $field = $fields_list[$i]; $foreignData = PMA_getForeignData($foreigners, $field, false, '', ''); - $retval .= PMA_getForeignFields_Values( + $html_output .= PMA_getForeignFields_Values( $foreigners, $foreignData, $field, $fields_type, $i, $db, $table, $titles, $GLOBALS['cfg']['ForeignKeyMaxLimit'], '', true ); - $retval .= '' . '
'; - return $retval; + $html_output .= ''; + return $html_output; } /** * Displays the table search form under table search tab * + * @param string $goto * @param array $fields_list Names of columns in the table * @param array $fields_type Types of columns in the table * @param array $fields_collation Collation of all columns @@ -350,51 +351,51 @@ $foreigners, $db, $table) * * @return void */ -function PMA_tbl_search_displaySelectionForm($fields_list, $fields_type, +function PMA_tblSearchDisplaySelectionForm($goto, $fields_list, $fields_type, $fields_collation, $fields_null, $geom_column_present, $geom_types, $fields_cnt, $foreigners, $db, $table) { - $output_html = ''; - $output_html .= '
'; + $html_output = ''; + $html_output .= '
'; $url_params = array(); $url_params['db'] = $db; $url_params['table'] = $table; - $output_html .= PMA_generateHtmlTabs(PMA_tbl_getSubTabs(), $url_params, 'topmenu2'); - $output_html .= '
'; - $output_html .= PMA_generate_common_hidden_inputs($db, $table); - $output_html .= ''; - $output_html .= '' + $html_output .= PMA_generate_common_hidden_inputs($db, $table); + $html_output .= ''; + $html_output .= '' . '
'; + PMA_tblSearchDisplaySliderOptions($fields_list, $fields_cnt); + $html_output = '

'; /** * Displays selection form's footer elements */ - $output_html .= '
' . '' . '
'; - echo $output_html; + echo $html_output; } ?> From 360e2063ea271030d3042e3d0d2b6b5b2eccc938 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Fri, 18 May 2012 23:18:48 +0530 Subject: [PATCH 04/13] More improvements in tbl_select.php --- tbl_select.php | 162 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 108 insertions(+), 54 deletions(-) diff --git a/tbl_select.php b/tbl_select.php index 5f3c995dbc..8212a6c0a8 100644 --- a/tbl_select.php +++ b/tbl_select.php @@ -60,7 +60,7 @@ if (! isset($param) || $param[0] == '') { $url_query .= '&goto=tbl_select.php&back=tbl_select.php'; /** - * Gets tables informations + * Gets table's information */ include_once 'libraries/tbl_info.inc.php'; @@ -80,6 +80,7 @@ if (! isset($param) || $param[0] == '') { // foreign keys from innodb) $foreigners = PMA_getForeigners($db, $table); + // Display the table search form PMA_tblSearchDisplaySelectionForm( $goto, $fields_list, $fields_type, $fields_collation, $fields_null, $geom_column_present, $geom_types, $fields_cnt, $foreigners, $db, $table @@ -90,15 +91,45 @@ if (! isset($param) || $param[0] == '') { /** * Selection criteria have been submitted -> do the work */ + $is_distinct = (isset($distinct)) ? 'true' : 'false'; + $sql_query = PMA_tblSearchBuildSqlQuery( + $table, $fields, $names, $types, $param, $max_number_of_fields, $is_distinct, + $where, $collations, $func, $orderField, $order + ); + unset($is_distinct); + include 'sql.php'; +} - // Builds the query - - $sql_query = 'SELECT ' . (isset($distinct) ? 'DISTINCT ' : ''); +/** + * Builds the sql search query from the post parameters + * + * @param string $table Selected table + * @param array $fields Entered values of the columns + * @param array $names Names of all columns + * @param array $types Types of all columns + * @param array $param Columns to be displayed in search results + * @param integer $max_number_of_fields Total number of columns in the table + * @param bool $is_distinct If only distinct values are needed + * @param string $where The custom where clause + * @param array $collations Collations of all columns + * @param array $func Operators for given column type + * @param string $orderField Column by which results are to be ordered + * @param string $order Whether ASC or DESC + * + * @return string the generated SQL query + */ +function PMA_tblSearchBuildSqlQuery($table, $fields, $names, $types, $param, + $max_number_of_fields, $is_distinct, $where, $collations, $func, $orderField, + $order) +{ + $sql_query = 'SELECT '; + if($is_distinct) { + $sql_query .= 'DISTINCT '; + } // if all fields were selected to display, we do a SELECT * // (more efficient and this helps prevent a problem in IE // if one of the rows is edited and we come back to the Select results) - if (count($param) == $max_number_of_fields) { $sql_query .= '* '; } else { @@ -111,38 +142,60 @@ if (! isset($param) || $param[0] == '') { unset($param); $sql_query .= ' FROM ' . PMA_backquote($table); - - // The where clause - if (trim($where) != '') { - $sql_query .= ' WHERE ' . $where; - } else { - $w = $charsets = array(); - $cnt_func = count($func); - reset($func); - while (list($i, $func_type) = each($func)) { - - list($charsets[$i]) = explode('_', $collations[$i]); - $unaryFlag = $GLOBALS['PMA_Types']->isUnaryOperator($func_type); - - $tmp_geom_func = isset($geom_func[$i]) ? $geom_func[$i] : null; - $whereClause = PMA_tbl_search_getWhereClause( - $fields[$i], $names[$i], $types[$i], $collations[$i], - $func_type, $unaryFlag, $tmp_geom_func - ); - - if ($whereClause) { - $w[] = $whereClause; - } - } // end while - if ($w) { - $sql_query .= ' WHERE ' . implode(' AND ', $w); - } - } // end if - + $whereClause = PMA_tblSearchGenerateWhereClause( + $fields, $names, $types, $where, $collations, $func + ); + $sql_query .= $whereClause; + + // if the search results are to be ordered if ($orderField != '--nil--') { $sql_query .= ' ORDER BY ' . PMA_backquote($orderField) . ' ' . $order; } // end if - include 'sql.php'; + return $sql_query; +} + +/** + * Generates the where clause for the sql search query to be executed + * + * @param array $fields Entered values of the columns + * @param array $names Names of all columns + * @param array $types Types of all columns + * @param string $where The custom where clause + * @param array $collations Collations of all columns + * @param array $func Operators for given column type + * + * @return string the generated where clause + */ +function PMA_tblSearchGenerateWhereClause($fields, $names, $types, $where, + $collations, $func) +{ + // If the custom where clause is set + if (trim($where) != '') { + $fullWhereClause .= ' WHERE ' . $where; + return $fullWhereClause; + } + + $fullWhereClause = $charsets = array(); + reset($func); + while (list($i, $func_type) = each($func)) { + list($charsets[$i]) = explode('_', $collations[$i]); + $unaryFlag = $GLOBALS['PMA_Types']->isUnaryOperator($func_type); + $tmp_geom_func = isset($geom_func[$i]) ? $geom_func[$i] : null; + + $whereClause = PMA_tbl_search_getWhereClause( + $fields[$i], $names[$i], $types[$i], $collations[$i], + $func_type, $unaryFlag, $tmp_geom_func + ); + + if ($whereClause) { + $fullWhereClause[] = $whereClause; + } + } // end while + + if ($fullWhereClause) { + $fullWhereClause = ' WHERE ' . implode(' AND ', $fullWhereClause); + } + return $fullWhereClause; } /** @@ -159,7 +212,7 @@ if (! isset($param) || $param[0] == '') { */ function PMA_tblSearchGetGeomFuncHtml($geom_column_present, $fields_type, $geom_types, $column_index) -{ +{ $html_output = ''; // return if geometrical column is not present if (!$geom_column_present) { @@ -208,9 +261,10 @@ function PMA_tblSearchDisplaySliderOptions($fields_list, $fields_cnt) /** * Displays columns select list for selecting distinct columns in the search */ - $html_output = '
- ' . __('Select columns (at least one):') . ' - '; // Displays the list of the fields foreach ($fields_list as $each_field) { $html_output .= ' ' @@ -218,35 +272,35 @@ function PMA_tblSearchDisplaySliderOptions($fields_list, $fields_cnt) . ' selected="selected">' . htmlspecialchars($each_field) . '' . "\n"; } // end for - $html_output .= ' - -
'; + $html_output .= '' + . '' + . '
'; /** * Displays input box for custom 'Where' clause to be used in the search */ - $html_output .= '
- ' . '' . __('Or') . ' ' . - __('Add search conditions (body of the "where" clause):') . ''; + $html_output .= '
' + . '' . '' . __('Or') . ' ' + . __('Add search conditions (body of the "where" clause):') . ''; $html_output .= PMA_showMySQLDocu('SQL-Syntax', 'Functions'); - $html_output .= ' -
'; + $html_output .= '' + . '
'; /** * Displays option of changing default number of rows displayed per page */ - $html_output .= '
- ' . __('Number of rows per page') . ' - -
'; + $html_output .= '
' + . '' . __('Number of rows per page') . '' + . '' + . '
'; /** * Displays option for ordering search results by a column value (Asc or Desc) */ - $html_output .= '
- ' . __('Display order:') . ' - '; foreach ($fields_list as $each_field) { $html_output .= ' ' . '