From d3aff8b31ad463d74f9b42d56779f183a14529df Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Fri, 25 May 2012 22:53:27 +0530 Subject: [PATCH] Move functions from tbl_select.php to libraries/tbl_select.lib.php --- libraries/tbl_select.lib.php | 355 +++++++++++++++++++++++++++++++++++ tbl_select.php | 355 ----------------------------------- 2 files changed, 355 insertions(+), 355 deletions(-) diff --git a/libraries/tbl_select.lib.php b/libraries/tbl_select.lib.php index d25d53333e..30aa0e002a 100644 --- a/libraries/tbl_select.lib.php +++ b/libraries/tbl_select.lib.php @@ -412,4 +412,359 @@ function PMA_tbl_search_getWhereClause($fields, $names, $types, $collations, return $w; } + +/** + * Builds the sql search query from the post parameters + * + * @param string $table Selected table + * @param array $fields Entered values of the columns + * @param array $criteriaColumnNames Names of all columns + * @param array $criteriaColumnTypes Types of all columns + * @param array $columnsToDisplay Columns to be displayed in search results + * @param bool $is_distinct If only distinct values are needed + * @param string $customWhereClause The custom where clause + * @param array $criteriaColumnCollations Collations of all columns + * @param array $criteriaColumnOperators Operators for given column type + * @param string $orderByColumn 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, $criteriaColumnNames, + $criteriaColumnTypes, $columnsToDisplay, $is_distinct, $customWhereClause, + $criteriaColumnCollations, $criteriaColumnOperators, $orderByColumn, $order) +{ + $sql_query = 'SELECT '; + if ($is_distinct == 'true') { + $sql_query .= 'DISTINCT '; + } + + // if all column names 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($columnsToDisplay) == count($criteriaColumnNames)) { + $sql_query .= '* '; + } else { + $columnsToDisplay = PMA_backquote($columnsToDisplay); + $sql_query .= implode(', ', $columnsToDisplay); + } // end if + + // avoid a loop, for example when $cfg['DefaultTabTable'] is set + // to 'tbl_select.php' + unset($columnsToDisplay); + + $sql_query .= ' FROM ' . PMA_backquote($table); + $whereClause = PMA_tblSearchGenerateWhereClause( + $fields, $criteriaColumnNames, $criteriaColumnTypes, $customWhereClause, + $criteriaColumnCollations, $criteriaColumnOperators + ); + $sql_query .= $whereClause; + + // if the search results are to be ordered + if ($orderByColumn != '--nil--') { + $sql_query .= ' ORDER BY ' . PMA_backquote($orderByColumn) . ' ' . $order; + } // end if + 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 $criteriaColumnNames Names of all columns + * @param array $criteriaColumnTypes Types of all columns + * @param string $customWhereClause The custom where clause + * @param array $criteriaColumnCollations Collations of all columns + * @param array $criteriaColumnOperators Operators for given column type + * + * @return string the generated where clause + */ +function PMA_tblSearchGenerateWhereClause($fields, $criteriaColumnNames, + $criteriaColumnTypes, $customWhereClause, $criteriaColumnCollations, + $criteriaColumnOperators) +{ + $fullWhereClause = ''; + + if (trim($customWhereClause) != '') { + $fullWhereClause .= ' WHERE ' . $customWhereClause; + return $fullWhereClause; + } + // If there are no search criterias set, return + if (!array_filter($fields)) { + return $fullWhereClause; + } + // else continue to form the where clause from column criteria values + $fullWhereClause = $charsets = array(); + reset($criteriaColumnOperators); + while (list($i, $operator) = each($criteriaColumnOperators)) { + list($charsets[$i]) = explode('_', $criteriaColumnCollations[$i]); + $unaryFlag = $GLOBALS['PMA_Types']->isUnaryOperator($operator); + $tmp_geom_func = isset($geom_func[$i]) ? $geom_func[$i] : null; + + $whereClause = PMA_tbl_search_getWhereClause( + $fields[$i], $criteriaColumnNames[$i], $criteriaColumnTypes[$i], + $criteriaColumnCollations[$i], $operator, $unaryFlag, $tmp_geom_func + ); + + if ($whereClause) { + $fullWhereClause[] = $whereClause; + } + } // end while + + if ($fullWhereClause) { + $fullWhereClause = ' WHERE ' . implode(' AND ', $fullWhereClause); + } + return $fullWhereClause; +} + +/** + * Generates HTML for a geometrical function column to be displayed in table + * search selection form + * + * @param boolean $geomColumnFlag whether a geometry column is present + * @param array $columnTypes array containing types of all columns in the table + * @param array $geom_types array of GIS data types + * @param integer $column_index index of current column in $columnTypes array + * + * @return string the generated HTML + */ +function PMA_tblSearchGetGeomFuncHtml($geomColumnFlag, $columnTypes, +$geom_types, $column_index) +{ + $html_output = ''; + // return if geometrical column is not present + if (!$geomColumnFlag) { + return $html_output; + } + + /** + * Displays 'Function' column if it is present + */ + $html_output .= ''; + // if a geometry column is present + if (in_array($columnTypes[$column_index], $geom_types)) { + $html_output .= ''; + } else { + $html_output .= ' '; + } + $html_output .= ''; + return $html_output; +} + +/** + * Generates formatted HTML for extra search options (slider) in table search form + * + * @param array $columnNames Array containing types of all columns in the table + * @param integer $columnCount Number of columns in the table + * + * @return string the generated HTML + */ +function PMA_tblSearchGetSliderOptions($columnNames, $columnCount) +{ + $html_output = ''; + $html_output .= PMA_getDivForSliderEffect('searchoptions', __('Options')); + /** + * Displays columns select list for selecting distinct columns in the search + */ + $html_output .= '
' + . '' . __('Select columns (at least one):') . '' + . '' + . '' + . '
'; + + /** + * 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 .= PMA_showMySQLDocu('SQL-Syntax', 'Functions'); + $html_output .= '' + . '
'; + + /** + * Displays option of changing default number of rows displayed 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:') . '' + . ''; + $choices = array( + 'ASC' => __('Ascending'), + 'DESC' => __('Descending') + ); + $html_output .= PMA_getRadioFields('order', $choices, 'ASC', false, true, "formelement"); + unset($choices); + + $html_output .= '

'; + return $html_output; +} + +/** + * Generates HTML for displaying fields table in search form + * + * @param array $columnNames Names of columns in the table + * @param array $columnTypes Types of columns in the table + * @param array $columnCollations Collation of all columns + * @param array $columnNullFlags Null information of columns + * @param boolean $geomColumnFlag Whether a geometry column is present + * @param integer $columnCount 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_tblSearchGetFieldsTableHtml($columnNames, $columnTypes, +$columnCollations, $columnNullFlags, $geomColumnFlag, $columnCount, +$foreigners, $db, $table) +{ + $html_output = ''; + $html_output .= ''; + $html_output .= PMA_tbl_setTableHeader($geomColumnFlag) . ''; + $odd_row = true; + $titles['Browse'] = PMA_getIcon('b_browse.png', __('Browse foreign values')); + $geom_types = PMA_getGISDatatypes(); + + // for every column present in table + for ($i = 0; $i < $columnCount; $i++) { + $html_output .= ''; + $odd_row = !$odd_row; + + /** + * If 'Function' column is present + */ + $html_output .= PMA_tblSearchGetGeomFuncHtml( + $geomColumnFlag, $columnTypes, $geom_types, $i + ); + /** + * Displays column's name, type, collation + */ + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + /** + * Displays column's comparison operators depending on column type + */ + $html_output .= ''; + } // end for + + $html_output .= '
' . htmlspecialchars($columnNames[$i]) . '' . htmlspecialchars($columnTypes[$i]) . '' . $columnCollations[$i] . ''; + /** + * Displays column's foreign relations if any + */ + $field = $columnNames[$i]; + $foreignData = PMA_getForeignData($foreigners, $field, false, '', ''); + $html_output .= PMA_getForeignFields_Values( + $foreigners, $foreignData, $field, $columnTypes, $i, $db, $table, + $titles, $GLOBALS['cfg']['ForeignKeyMaxLimit'], '', true + ); + + $html_output .= '' + . '
'; + return $html_output; +} + +/** + * Generates the table search form under table search tab + * + * @param string $goto Goto URL + * @param array $columnNames Names of columns in the table + * @param array $columnTypes Types of columns in the table + * @param array $columnCollations Collation of all columns + * @param array $columnNullFlags Null information of columns + * @param boolean $geomColumnFlag Whether a geometry column is present + * @param integer $columnCount 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 for table search form + */ +function PMA_tblSearchGetSelectionForm($goto, $columnNames, $columnTypes, +$columnCollations, $columnNullFlags, $geomColumnFlag, $columnCount, +$foreigners, $db, $table) +{ + $html_output = ''; + $html_output .= '
'; + $url_params = array(); + $url_params['db'] = $db; + $url_params['table'] = $table; + + $html_output .= PMA_generateHtmlTabs(PMA_tbl_getSubTabs(), $url_params, 'topmenu2'); + $html_output .= '
'; + $html_output .= PMA_generate_common_hidden_inputs($db, $table); + $html_output .= ''; + $html_output .= '' + . '
'; + return $html_output; +} ?> diff --git a/tbl_select.php b/tbl_select.php index a3745fe40c..34c13c99fc 100644 --- a/tbl_select.php +++ b/tbl_select.php @@ -97,359 +97,4 @@ if (! isset($columnsToDisplay) || $columnsToDisplay[0] == '') { unset($is_distinct); include 'sql.php'; } - -/** - * Builds the sql search query from the post parameters - * - * @param string $table Selected table - * @param array $fields Entered values of the columns - * @param array $criteriaColumnNames Names of all columns - * @param array $criteriaColumnTypes Types of all columns - * @param array $columnsToDisplay Columns to be displayed in search results - * @param bool $is_distinct If only distinct values are needed - * @param string $customWhereClause The custom where clause - * @param array $criteriaColumnCollations Collations of all columns - * @param array $criteriaColumnOperators Operators for given column type - * @param string $orderByColumn 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, $criteriaColumnNames, - $criteriaColumnTypes, $columnsToDisplay, $is_distinct, $customWhereClause, - $criteriaColumnCollations, $criteriaColumnOperators, $orderByColumn, $order) -{ - $sql_query = 'SELECT '; - if ($is_distinct == 'true') { - $sql_query .= 'DISTINCT '; - } - - // if all column names 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($columnsToDisplay) == count($criteriaColumnNames)) { - $sql_query .= '* '; - } else { - $columnsToDisplay = PMA_backquote($columnsToDisplay); - $sql_query .= implode(', ', $columnsToDisplay); - } // end if - - // avoid a loop, for example when $cfg['DefaultTabTable'] is set - // to 'tbl_select.php' - unset($columnsToDisplay); - - $sql_query .= ' FROM ' . PMA_backquote($table); - $whereClause = PMA_tblSearchGenerateWhereClause( - $fields, $criteriaColumnNames, $criteriaColumnTypes, $customWhereClause, - $criteriaColumnCollations, $criteriaColumnOperators - ); - $sql_query .= $whereClause; - - // if the search results are to be ordered - if ($orderByColumn != '--nil--') { - $sql_query .= ' ORDER BY ' . PMA_backquote($orderByColumn) . ' ' . $order; - } // end if - 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 $criteriaColumnNames Names of all columns - * @param array $criteriaColumnTypes Types of all columns - * @param string $customWhereClause The custom where clause - * @param array $criteriaColumnCollations Collations of all columns - * @param array $criteriaColumnOperators Operators for given column type - * - * @return string the generated where clause - */ -function PMA_tblSearchGenerateWhereClause($fields, $criteriaColumnNames, - $criteriaColumnTypes, $customWhereClause, $criteriaColumnCollations, - $criteriaColumnOperators) -{ - $fullWhereClause = ''; - - if (trim($customWhereClause) != '') { - $fullWhereClause .= ' WHERE ' . $customWhereClause; - return $fullWhereClause; - } - // If there are no search criterias set, return - if (!array_filter($fields)) { - return $fullWhereClause; - } - // else continue to form the where clause from column criteria values - $fullWhereClause = $charsets = array(); - reset($criteriaColumnOperators); - while (list($i, $operator) = each($criteriaColumnOperators)) { - list($charsets[$i]) = explode('_', $criteriaColumnCollations[$i]); - $unaryFlag = $GLOBALS['PMA_Types']->isUnaryOperator($operator); - $tmp_geom_func = isset($geom_func[$i]) ? $geom_func[$i] : null; - - $whereClause = PMA_tbl_search_getWhereClause( - $fields[$i], $criteriaColumnNames[$i], $criteriaColumnTypes[$i], - $criteriaColumnCollations[$i], $operator, $unaryFlag, $tmp_geom_func - ); - - if ($whereClause) { - $fullWhereClause[] = $whereClause; - } - } // end while - - if ($fullWhereClause) { - $fullWhereClause = ' WHERE ' . implode(' AND ', $fullWhereClause); - } - return $fullWhereClause; -} - -/** - * Generates HTML for a geometrical function column to be displayed in table - * search selection form - * - * @param boolean $geomColumnFlag whether a geometry column is present - * @param array $columnTypes array containing types of all columns in the table - * @param array $geom_types array of GIS data types - * @param integer $column_index index of current column in $columnTypes array - * - * @return string the generated HTML - */ -function PMA_tblSearchGetGeomFuncHtml($geomColumnFlag, $columnTypes, -$geom_types, $column_index) -{ - $html_output = ''; - // return if geometrical column is not present - if (!$geomColumnFlag) { - return $html_output; - } - - /** - * Displays 'Function' column if it is present - */ - $html_output .= ''; - // if a geometry column is present - if (in_array($columnTypes[$column_index], $geom_types)) { - $html_output .= ''; - } else { - $html_output .= ' '; - } - $html_output .= ''; - return $html_output; -} - -/** - * Generates formatted HTML for extra search options (slider) in table search form - * - * @param array $columnNames Array containing types of all columns in the table - * @param integer $columnCount Number of columns in the table - * - * @return string the generated HTML - */ -function PMA_tblSearchGetSliderOptions($columnNames, $columnCount) -{ - $html_output = ''; - $html_output .= PMA_getDivForSliderEffect('searchoptions', __('Options')); - /** - * Displays columns select list for selecting distinct columns in the search - */ - $html_output .= '
' - . '' . __('Select columns (at least one):') . '' - . '' - . '' - . '
'; - - /** - * 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 .= PMA_showMySQLDocu('SQL-Syntax', 'Functions'); - $html_output .= '' - . '
'; - - /** - * Displays option of changing default number of rows displayed 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:') . '' - . ''; - $choices = array( - 'ASC' => __('Ascending'), - 'DESC' => __('Descending') - ); - $html_output .= PMA_getRadioFields('order', $choices, 'ASC', false, true, "formelement"); - unset($choices); - - $html_output .= '

'; - return $html_output; -} - -/** - * Generates HTML for displaying fields table in search form - * - * @param array $columnNames Names of columns in the table - * @param array $columnTypes Types of columns in the table - * @param array $columnCollations Collation of all columns - * @param array $columnNullFlags Null information of columns - * @param boolean $geomColumnFlag Whether a geometry column is present - * @param integer $columnCount 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_tblSearchGetFieldsTableHtml($columnNames, $columnTypes, -$columnCollations, $columnNullFlags, $geomColumnFlag, $columnCount, -$foreigners, $db, $table) -{ - $html_output = ''; - $html_output .= ''; - $html_output .= PMA_tbl_setTableHeader($geomColumnFlag) . ''; - $odd_row = true; - $titles['Browse'] = PMA_getIcon('b_browse.png', __('Browse foreign values')); - $geom_types = PMA_getGISDatatypes(); - - // for every column present in table - for ($i = 0; $i < $columnCount; $i++) { - $html_output .= ''; - $odd_row = !$odd_row; - - /** - * If 'Function' column is present - */ - $html_output .= PMA_tblSearchGetGeomFuncHtml( - $geomColumnFlag, $columnTypes, $geom_types, $i - ); - /** - * Displays column's name, type, collation - */ - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - /** - * Displays column's comparison operators depending on column type - */ - $html_output .= ''; - } // end for - - $html_output .= '
' . htmlspecialchars($columnNames[$i]) . '' . htmlspecialchars($columnTypes[$i]) . '' . $columnCollations[$i] . ''; - /** - * Displays column's foreign relations if any - */ - $field = $columnNames[$i]; - $foreignData = PMA_getForeignData($foreigners, $field, false, '', ''); - $html_output .= PMA_getForeignFields_Values( - $foreigners, $foreignData, $field, $columnTypes, $i, $db, $table, - $titles, $GLOBALS['cfg']['ForeignKeyMaxLimit'], '', true - ); - - $html_output .= '' - . '
'; - return $html_output; -} - -/** - * Generates the table search form under table search tab - * - * @param string $goto Goto URL - * @param array $columnNames Names of columns in the table - * @param array $columnTypes Types of columns in the table - * @param array $columnCollations Collation of all columns - * @param array $columnNullFlags Null information of columns - * @param boolean $geomColumnFlag Whether a geometry column is present - * @param integer $columnCount 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 for table search form - */ -function PMA_tblSearchGetSelectionForm($goto, $columnNames, $columnTypes, -$columnCollations, $columnNullFlags, $geomColumnFlag, $columnCount, -$foreigners, $db, $table) -{ - $html_output = ''; - $html_output .= '
'; - $url_params = array(); - $url_params['db'] = $db; - $url_params['table'] = $table; - - $html_output .= PMA_generateHtmlTabs(PMA_tbl_getSubTabs(), $url_params, 'topmenu2'); - $html_output .= '
'; - $html_output .= PMA_generate_common_hidden_inputs($db, $table); - $html_output .= ''; - $html_output .= '' - . '
'; - return $html_output; -} ?>