From a24b4ae33a878424293b2cc0ea4eeb8166641bd8 Mon Sep 17 00:00:00 2001 From: Kasun Chathuranga Date: Mon, 13 May 2013 23:11:30 +0530 Subject: [PATCH] PMA_Table::analyzeStructure() method to centralize the analysis of the structure of a view/table --- libraries/DisplayResults.class.php | 10 +++---- libraries/Table.class.php | 45 +++++++++++++++++++++++++++++- sql.php | 38 +++++++++++-------------- tbl_change.php | 8 +----- 4 files changed, 67 insertions(+), 34 deletions(-) diff --git a/libraries/DisplayResults.class.php b/libraries/DisplayResults.class.php index 039bd13183..ef020b6227 100644 --- a/libraries/DisplayResults.class.php +++ b/libraries/DisplayResults.class.php @@ -152,7 +152,7 @@ class PMA_DisplayResults 'mime_map' => null, /** boolean */ - 'resultSetContainsUniqueKey' => null + 'editable' => null ); /** @@ -249,7 +249,7 @@ class PMA_DisplayResults * @param type $showtable array table definitions * @param type $printview string * @param type $url_query string URL query - * @param type $resultSetContainsUniqueKey boolean + * @param type $editable boolean whether the resutls set is editable * * @return void * @@ -259,7 +259,7 @@ class PMA_DisplayResults $unlim_num_rows, $fields_meta, $is_count, $is_export, $is_func, $is_analyse, $num_rows, $fields_cnt, $querytime, $pmaThemeImage, $text_dir, $is_maint, $is_explain, $is_show, $showtable, $printview, $url_query, - $resultSetContainsUniqueKey + $editable ) { $this->__set('unlim_num_rows', $unlim_num_rows); @@ -279,7 +279,7 @@ class PMA_DisplayResults $this->__set('showtable', $showtable); $this->__set('printview', $printview); $this->__set('url_query', $url_query); - $this->__set('resultSetContainsUniqueKey', $resultSetContainsUniqueKey); + $this->__set('editable', $editable); } // end of the 'setProperties()' function @@ -2469,7 +2469,7 @@ class PMA_DisplayResults // name of the class added to all grid editable elements; // if we don't have all the columns of a unique key in the result set, // do not permit grid editing - if ($is_limited_display || ! $this->__get('resultSetContainsUniqueKey')) { + if ($is_limited_display || ! $this->__get('editable')) { $grid_edit_class = ''; } else { switch ($GLOBALS['cfg']['GridEditing']) { diff --git a/libraries/Table.class.php b/libraries/Table.class.php index d15b449a7a..94c6ce43cd 100644 --- a/libraries/Table.class.php +++ b/libraries/Table.class.php @@ -214,7 +214,7 @@ class PMA_Table * @param string $db database * @param string $table table * - * @return whether the given is an updatable view + * @return boolean whether the given is an updatable view */ static public function isUpdatableView($db = null, $table = null) { @@ -232,6 +232,49 @@ class PMA_Table return $result ? true : false; } + /** + * Returns the analysis of 'SHOW CREATE TABLE' query for the table. + * In case of a view, the values are taken from the information_schema. + * + * @param string $db database + * @param string $table table + * + * @return array analysis of 'SHOW CREATE TABLE' query for the table + */ + static public function analyzeStructure($db = null, $table = null) + { + if (empty($db) || empty($table)) { + return false; + } + + $analyzed_sql = array(); + if (self::isView($db, $table)) { + // For a view, 'SHOW CREATE TABLE' returns the definition, + // but the structure of the view. So, we try to mock + // the result of analyzing 'SHOW CREATE TABLE' query. + $analyzed_sql[0] = array(); + $analyzed_sql[0]['create_table_fields'] = array(); + + $results = PMA_DBI_fetch_result( + "SELECT COLUMN_NAME, DATA_TYPE + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = '" . PMA_Util::sqlAddSlashes($db) . "' + AND TABLE_NAME = '" . PMA_Util::sqlAddSlashes($table) . "'" + ); + foreach ($results as $result) { + $analyzed_sql[0]['create_table_fields'][$result['COLUMN_NAME']] + = array('type' => strtoupper($result['DATA_TYPE'])); + } + } else { + $show_create_table = PMA_DBI_fetch_value( + 'SHOW CREATE TABLE ' . PMA_Util::backquote($db) . '.' . PMA_Util::backquote($table), + 0, + 1 + ); + $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table)); + } + } + /** * sets given $value for given $param * diff --git a/sql.php b/sql.php index 399a4306c8..6b636434ae 100644 --- a/sql.php +++ b/sql.php @@ -1240,9 +1240,8 @@ if ((0 == $num_rows && 0 == $unlim_num_rows) || $is_affected) { = $justBrowsing && trim($analyzed_sql[0]['select_expr_clause']) == '*' && PMA_Table::isUpdatableView($db, $table); - if (PMA_is_system_schema($db) - || ! ($resultSetContainsUniqueKey || $updatableView) - ) { + $editable = $resultSetContainsUniqueKey || $updatableView; + if (PMA_is_system_schema($db) || ! $editable) { $disp_mode = 'nnnn110111'; $msg = PMA_message::notice( __( @@ -1271,7 +1270,7 @@ if ((0 == $num_rows && 0 == $unlim_num_rows) || $is_affected) { echo getTableHtmlForMultipleQueries( $displayResultsObject, $db, $sql_data, $goto, $pmaThemeImage, $text_dir, $printview, $url_query, - $disp_mode, $sql_limit_to_append, $resultSetContainsUniqueKey + $disp_mode, $sql_limit_to_append, $editable ); } else { $_SESSION['is_multi_query'] = false; @@ -1279,7 +1278,7 @@ if ((0 == $num_rows && 0 == $unlim_num_rows) || $is_affected) { $unlim_num_rows, $fields_meta, $is_count, $is_export, $is_func, $is_analyse, $num_rows, $fields_cnt, $querytime, $pmaThemeImage, $text_dir, $is_maint, $is_explain, $is_show, $showtable, - $printview, $url_query, $resultSetContainsUniqueKey + $printview, $url_query, $editable ); echo $displayResultsObject->getTable($result, $disp_mode, $analyzed_sql); @@ -1471,27 +1470,24 @@ function PMA_getTableNameBySQL($sql, $tables) * Generate table html when SQL statement have multiple queries * which return displayable results * - * @param PMA_DisplayResults $displayResultsObject object - * @param string $db database name - * @param array $sql_data information about - * SQL statement - * @param string $goto URL to go back in case - * of errors - * @param string $pmaThemeImage path for theme images - * directory - * @param string $text_dir text direction - * @param string $printview whether printview is enabled - * @param string $url_query URL query - * @param array $disp_mode the display mode - * @param string $sql_limit_to_append limit clause - * @param bool $resultSetContainsUniqueKey result contains a unique key + * @param PMA_DisplayResults $displayResultsObject object + * @param string $db database name + * @param array $sql_data information about SQL statement + * @param string $goto URL to go back in case of errors + * @param string $pmaThemeImage path for theme images directory + * @param string $text_dir text direction + * @param string $printview whether printview is enabled + * @param string $url_query URL query + * @param array $disp_mode the display mode + * @param string $sql_limit_to_append limit clause + * @param bool $editable whether result set is editable * * @return string $table_html html content */ function getTableHtmlForMultipleQueries( $displayResultsObject, $db, $sql_data, $goto, $pmaThemeImage, $text_dir, $printview, $url_query, $disp_mode, $sql_limit_to_append, - $resultSetContainsUniqueKey + $editable ) { $table_html = ''; @@ -1601,7 +1597,7 @@ function getTableHtmlForMultipleQueries( $unlim_num_rows, $fields_meta, $is_count, $is_export, $is_func, $is_analyse, $num_rows, $fields_cnt, $querytime, $pmaThemeImage, $text_dir, $is_maint, $is_explain, $is_show, $showtable, - $printview, $url_query, $resultSetContainsUniqueKey + $printview, $url_query, $editable ); } diff --git a/tbl_change.php b/tbl_change.php index 608893e4cc..795eaee344 100644 --- a/tbl_change.php +++ b/tbl_change.php @@ -131,14 +131,8 @@ if (! empty($disp_message)) { /** * Get the analysis of SHOW CREATE TABLE for this table - * @todo should be handled by class Table */ -$show_create_table = PMA_DBI_fetch_value( - 'SHOW CREATE TABLE ' . PMA_Util::backquote($db) . '.' . PMA_Util::backquote($table), - 0, 1 -); -$analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table)); -unset($show_create_table); +$analyzed_sql = PMA_Table::analyzeStructure($db, $table); /** * Get the list of the fields of the current table