From 88e3e5fc041930524b898069d125fe6f85fe0eb5 Mon Sep 17 00:00:00 2001 From: Spun Nakandala Date: Tue, 25 Jun 2013 21:21:56 +0530 Subject: [PATCH 1/6] Added the methods PMA_setColumnOrder, PMA_setColumnVisibility, PMA_setColumnOrderOrVisibility, PMA_addBookmark,PMA_findRealEndOfRows, PMA_getRelationalValues, PMA_getEnumOrSetValues, PMA_appendLimitClaues --- libraries/sql.lib.php | 215 ++++++++++++++++++++++++++++++++++++++++++ sql.php | 131 ++----------------------- 2 files changed, 225 insertions(+), 121 deletions(-) diff --git a/libraries/sql.lib.php b/libraries/sql.lib.php index 0c845f01c4..5216c76644 100644 --- a/libraries/sql.lib.php +++ b/libraries/sql.lib.php @@ -830,4 +830,219 @@ function PMA_hasNoRightsToDropDatabase($analyzed_sql_results, return false; } } + +/** + * Function to set the column order + * + * @param PMA_Table $pmatable PMA_Table instance + */ +function PMA_setColumnOrder($pmatable) +{ + $col_order = explode(',', $_REQUEST['col_order']); + $retval = $pmatable->setUiProp( + PMA_Table::PROP_COLUMN_ORDER, + $col_order, + $_REQUEST['table_create_time'] + ); + if (gettype($retval) != 'boolean') { + $response = PMA_Response::getInstance(); + $response->isSuccess(false); + $response->addJSON('message', $retval->getString()); + exit; + } +} + +/** + * Function to set the column visibility + * + * @param PMA_Table $pmatable PMA_Table instance + */ +function PMA_setColumnVisibility($pmatable) +{ + $col_visib = explode(',', $_REQUEST['col_visib']); + $retval = $pmatable->setUiProp( + PMA_Table::PROP_COLUMN_VISIB, $col_visib, + $_REQUEST['table_create_time'] + ); + if (gettype($retval) != 'boolean') { + $response = PMA_Response::getInstance(); + $response->isSuccess(false); + $response->addJSON('message', $retval->getString()); + exit; + } +} + +/** + * Function to check the request for setting the column order or visibility + * + * @param String $table the current table + * @param String $db the current database + */ +function PMA_setColumnOrderOrVisibility($table, $db) +{ + $pmatable = new PMA_Table($table, $db); + $retval = false; + + // set column order + if (isset($_REQUEST['col_order'])) { + PMA_setColumnOrder($pmatable); + } + + // set column visibility + if ($retval === true && isset($_REQUEST['col_visib'])) { + PMA_setColumnVisibility($pmatable); + } + + $response = PMA_Response::getInstance(); + $response->isSuccess($retval == true); + exit; +} + +/** + * Function to add a bookmark + * + * @param String $pmaAbsoluteUri absolute URL + * @param String $goto goto page url + */ +function PMA_addBookmark($pmaAbsoluteUri, $goto) +{ + $result = PMA_Bookmark_save( + $_POST['bkm_fields'], + (isset($_POST['bkm_all_users']) + && $_POST['bkm_all_users'] == 'true' ? true : false + ) + ); + $response = PMA_Response::getInstance(); + if ($response->isAjax()) { + if ($result) { + $msg = PMA_message::success(__('Bookmark %s created')); + $msg->addParam($_POST['bkm_fields']['bkm_label']); + $response->addJSON('message', $msg); + } else { + $msg = PMA_message::error(__('Bookmark not created')); + $response->isSuccess(false); + $response->addJSON('message', $msg); + } + exit; + } else { + // go back to sql.php to redisplay query; do not use & in this case: + PMA_sendHeaderLocation( + $pmaAbsoluteUri . $goto + . '&label=' . $_POST['bkm_fields']['bkm_label'] + ); + } +} + +/** + * Function to find the real end of rows + * + * @param String $db the current database + * @param String $table the current table + * @return mixed the number of records if "retain" param is true, otherwise true + */ +function PMA_findRealEndOfRows($db, $table) +{ + $unlim_num_rows = PMA_Table::countRecords($db, $table, true); + $_SESSION['tmp_user_values']['pos'] = @((ceil( + $unlim_num_rows / $_SESSION['tmp_user_values']['max_rows'] + ) - 1) * $_SESSION['tmp_user_values']['max_rows']); + + return $unlim_num_rows; +} + +/** + * Function to get values for the redational columns + * + * @param String $db the current database + * @param String $table the current table + * @param String $display_field + */ +function PMA_getRelationalValues($db, $table, $display_field) +{ + $column = $_REQUEST['column']; + if ($_SESSION['tmp_user_values']['relational_display'] == 'D' + && isset($display_field) + && strlen($display_field) + && isset($_REQUEST['relation_key_or_display_column']) + && $_REQUEST['relation_key_or_display_column'] + ) { + $curr_value = $_REQUEST['relation_key_or_display_column']; + } else { + $curr_value = $_REQUEST['curr_value']; + } + $dropdown = PMA_getHtmlForRelationalColumnDropdown( + $db, $table, $column, $curr_value + ); + $response = PMA_Response::getInstance(); + $response->addJSON('dropdown', $dropdown); + exit; +} + +/** + * Function to get values for Enum or Set Columns + * + * @param String $db the current database + * @param String $table the current table + * @param String $columnType whether enum or set + */ +function PMA_getEnumOrSetValues($db, $table, $columnType) +{ + $column = $_REQUEST['column']; + $curr_value = $_REQUEST['curr_value']; + $response = PMA_Response::getInstance(); + if ($columnType == "enum") { + $dropdown = PMA_getHtmlForEnumColumnDropdown( + $db, $table, $column, $curr_value + ); + $response->addJSON('dropdown', $dropdown); + } else { + $select = PMA_getHtmlForSetColumn($db, $table, $column, $curr_value); + $response->addJSON('select', $select); + } + exit; +} + +/** + * Function to append the limit clause + * + * @param String $full_sql_query + * @param array $analyzed_sql + * @param String $display_query + * @return array + */ +function PMA_appendLimitClaues($full_sql_query, $analyzed_sql, $display_query) +{ + $sql_limit_to_append = ' LIMIT ' . $_SESSION['tmp_user_values']['pos'] + . ', ' . $_SESSION['tmp_user_values']['max_rows'] . " "; + $full_sql_query = PMA_getSqlWithLimitClause( + $full_sql_query, + $analyzed_sql, + $sql_limit_to_append + ); + + /** + * @todo pretty printing of this modified query + */ + if ($display_query) { + // if the analysis of the original query revealed that we found + // a section_after_limit, we now have to analyze $display_query + // to display it correctly + + if (! empty($analyzed_sql[0]['section_after_limit']) + && trim($analyzed_sql[0]['section_after_limit']) != ';' + ) { + $analyzed_display_query = PMA_SQP_analyze( + PMA_SQP_parse($display_query) + ); + $display_query = $analyzed_display_query[0]['section_before_limit'] + . "\n" . $sql_limit_to_append + . $analyzed_display_query[0]['section_after_limit']; + } + } + + return array($sql_limit_to_append, $full_sql_query, isset($analyzed_display_query) + ? $analyzed_display_query : null, + isset($display_query) ? $display_query : null + ); +} ?> diff --git a/sql.php b/sql.php index a86b76e53a..6b0f1e4a6a 100644 --- a/sql.php +++ b/sql.php @@ -83,87 +83,25 @@ if (isset($_POST['bkm_fields']['bkm_database'])) { if (isset($_REQUEST['get_relational_values']) && $_REQUEST['get_relational_values'] == true ) { - $column = $_REQUEST['column']; - if ($_SESSION['tmp_user_values']['relational_display'] == 'D' - && isset($display_field) - && strlen($display_field) - && isset($_REQUEST['relation_key_or_display_column']) - && $_REQUEST['relation_key_or_display_column'] - ) { - $curr_value = $_REQUEST['relation_key_or_display_column']; - } else { - $curr_value = $_REQUEST['curr_value']; - } - $dropdown = PMA_getHtmlForRelationalColumnDropdown( - $db, $table, $column, $curr_value - ); - $response = PMA_Response::getInstance(); - $response->addJSON('dropdown', $dropdown); - exit; + PMA_getRelationalValues($db, $table, $display_field); } // Just like above, find possible values for enum fields during grid edit. if (isset($_REQUEST['get_enum_values']) && $_REQUEST['get_enum_values'] == true) { - $column = $_REQUEST['column']; - $curr_value = $_REQUEST['curr_value']; - $dropdown = PMA_getHtmlForEnumColumnDropdown($db, $table, $column, $curr_value); - $response = PMA_Response::getInstance(); - $response->addJSON('dropdown', $dropdown); - exit; + PMA_getEnumOrSetValues($db, $table, "enum"); } // Find possible values for set fields during grid edit. if (isset($_REQUEST['get_set_values']) && $_REQUEST['get_set_values'] == true) { - $column = $_REQUEST['column']; - $curr_value = $_REQUEST['curr_value']; - $select = PMA_getHtmlForSetColumn($db, $table, $column, $curr_value); - $response = PMA_Response::getInstance(); - $response->addJSON('select', $select); - exit; + PMA_getEnumOrSetValues($db, $table, "set"); } /** * Check ajax request to set the column order */ if (isset($_REQUEST['set_col_prefs']) && $_REQUEST['set_col_prefs'] == true) { - $pmatable = new PMA_Table($table, $db); - $retval = false; - - // set column order - if (isset($_REQUEST['col_order'])) { - $col_order = explode(',', $_REQUEST['col_order']); - $retval = $pmatable->setUiProp( - PMA_Table::PROP_COLUMN_ORDER, - $col_order, - $_REQUEST['table_create_time'] - ); - if (gettype($retval) != 'boolean') { - $response = PMA_Response::getInstance(); - $response->isSuccess(false); - $response->addJSON('message', $retval->getString()); - exit; - } - } - - // set column visibility - if ($retval === true && isset($_REQUEST['col_visib'])) { - $col_visib = explode(',', $_REQUEST['col_visib']); - $retval = $pmatable->setUiProp( - PMA_Table::PROP_COLUMN_VISIB, $col_visib, - $_REQUEST['table_create_time'] - ); - if (gettype($retval) != 'boolean') { - $response = PMA_Response::getInstance(); - $response->isSuccess(false); - $response->addJSON('message', $retval->getString()); - exit; - } - } - - $response = PMA_Response::getInstance(); - $response->isSuccess($retval == true); - exit; + PMA_setColumnOrderOrVisibility($table, $db); } // Default to browse if no query set and we have table @@ -238,10 +176,7 @@ $displayResultsObject->setConfigParamsForDisplayTable(); * Need to find the real end of rows? */ if (isset($find_real_end) && $find_real_end) { - $unlim_num_rows = PMA_Table::countRecords($db, $table, true); - $_SESSION['tmp_user_values']['pos'] = @((ceil( - $unlim_num_rows / $_SESSION['tmp_user_values']['max_rows'] - ) - 1) * $_SESSION['tmp_user_values']['max_rows']); + $unlim_num_rows = PMA_findRealEndOfRows($db, $table); } @@ -249,31 +184,7 @@ if (isset($find_real_end) && $find_real_end) { * Bookmark add */ if (isset($_POST['store_bkm'])) { - $result = PMA_Bookmark_save( - $_POST['bkm_fields'], - (isset($_POST['bkm_all_users']) - && $_POST['bkm_all_users'] == 'true' ? true : false - ) - ); - $response = PMA_Response::getInstance(); - if ($response->isAjax()) { - if ($result) { - $msg = PMA_message::success(__('Bookmark %s created')); - $msg->addParam($_POST['bkm_fields']['bkm_label']); - $response->addJSON('message', $msg); - } else { - $msg = PMA_message::error(__('Bookmark not created')); - $response->isSuccess(false); - $response->addJSON('message', $msg); - } - exit; - } else { - // go back to sql.php to redisplay query; do not use & in this case: - PMA_sendHeaderLocation( - $cfg['PmaAbsoluteUri'] . $goto - . '&label=' . $_POST['bkm_fields']['bkm_label'] - ); - } + PMA_addBookmark($cfg['PmaAbsoluteUri'], $goto); } // end if @@ -320,33 +231,11 @@ if (PMA_isRememberSortingOrder($analyzed_sql_results)) { $sql_limit_to_append = ''; // Do append a "LIMIT" clause? if (PMA_isAppendLimitClause($analyzed_sql_results)) { - $sql_limit_to_append = ' LIMIT ' . $_SESSION['tmp_user_values']['pos'] - . ', ' . $_SESSION['tmp_user_values']['max_rows'] . " "; - $full_sql_query = PMA_getSqlWithLimitClause( - $full_sql_query, - $analyzed_sql, - $sql_limit_to_append + list($sql_limit_to_append, + $full_sql_query, $analyzed_display_query, $display_query + ) = PMA_appendLimitClaues( + $full_sql_query, $analyzed_sql, isset($display_query) ); - - /** - * @todo pretty printing of this modified query - */ - if (isset($display_query)) { - // if the analysis of the original query revealed that we found - // a section_after_limit, we now have to analyze $display_query - // to display it correctly - - if (! empty($analyzed_sql[0]['section_after_limit']) - && trim($analyzed_sql[0]['section_after_limit']) != ';' - ) { - $analyzed_display_query = PMA_SQP_analyze( - PMA_SQP_parse($display_query) - ); - $display_query = $analyzed_display_query[0]['section_before_limit'] - . "\n" . $sql_limit_to_append - . $analyzed_display_query[0]['section_after_limit']; - } - } } if (strlen($db)) { From 9d429aacb4070a13be7525c07ec54dabd4be1787 Mon Sep 17 00:00:00 2001 From: Spun Nakandala Date: Tue, 25 Jun 2013 21:38:42 +0530 Subject: [PATCH 2/6] error corrected --- libraries/sql.lib.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/libraries/sql.lib.php b/libraries/sql.lib.php index 5216c76644..57b69d3567 100644 --- a/libraries/sql.lib.php +++ b/libraries/sql.lib.php @@ -835,6 +835,8 @@ function PMA_hasNoRightsToDropDatabase($analyzed_sql_results, * Function to set the column order * * @param PMA_Table $pmatable PMA_Table instance + * + * @retrun boolean $retval */ function PMA_setColumnOrder($pmatable) { @@ -850,12 +852,16 @@ function PMA_setColumnOrder($pmatable) $response->addJSON('message', $retval->getString()); exit; } + + return $retval; } /** * Function to set the column visibility * * @param PMA_Table $pmatable PMA_Table instance + * + * @return boolean $retval */ function PMA_setColumnVisibility($pmatable) { @@ -870,6 +876,7 @@ function PMA_setColumnVisibility($pmatable) $response->addJSON('message', $retval->getString()); exit; } + return $retval; } /** @@ -885,12 +892,12 @@ function PMA_setColumnOrderOrVisibility($table, $db) // set column order if (isset($_REQUEST['col_order'])) { - PMA_setColumnOrder($pmatable); + $retval = PMA_setColumnOrder($pmatable); } // set column visibility if ($retval === true && isset($_REQUEST['col_visib'])) { - PMA_setColumnVisibility($pmatable); + $retval = PMA_setColumnVisibility($pmatable); } $response = PMA_Response::getInstance(); From b05a0fdfe864f096caa4438920446e070190aef1 Mon Sep 17 00:00:00 2001 From: Spun Nakandala Date: Wed, 26 Jun 2013 16:18:16 +0530 Subject: [PATCH 3/6] indentation corrected. comment corrected --- libraries/sql.lib.php | 2 +- sql.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/sql.lib.php b/libraries/sql.lib.php index 57b69d3567..e7b52e355c 100644 --- a/libraries/sql.lib.php +++ b/libraries/sql.lib.php @@ -842,7 +842,7 @@ function PMA_setColumnOrder($pmatable) { $col_order = explode(',', $_REQUEST['col_order']); $retval = $pmatable->setUiProp( - PMA_Table::PROP_COLUMN_ORDER, + PMA_Table::PROP_COLUMN_ORDER, $col_order, $_REQUEST['table_create_time'] ); diff --git a/sql.php b/sql.php index 6b0f1e4a6a..1ab5ca2961 100644 --- a/sql.php +++ b/sql.php @@ -98,7 +98,7 @@ if (isset($_REQUEST['get_set_values']) && $_REQUEST['get_set_values'] == true) { } /** - * Check ajax request to set the column order + * Check ajax request to set the column order and visibility */ if (isset($_REQUEST['set_col_prefs']) && $_REQUEST['set_col_prefs'] == true) { PMA_setColumnOrderOrVisibility($table, $db); From c349581d9a6ef1e35535fd5b1ff1d09cecb0e661 Mon Sep 17 00:00:00 2001 From: Spun Nakandala Date: Wed, 26 Jun 2013 16:23:01 +0530 Subject: [PATCH 4/6] typo corrected --- libraries/sql.lib.php | 2 +- sql.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/sql.lib.php b/libraries/sql.lib.php index e7b52e355c..64398d4b6f 100644 --- a/libraries/sql.lib.php +++ b/libraries/sql.lib.php @@ -1017,7 +1017,7 @@ function PMA_getEnumOrSetValues($db, $table, $columnType) * @param String $display_query * @return array */ -function PMA_appendLimitClaues($full_sql_query, $analyzed_sql, $display_query) +function PMA_appendLimitClause($full_sql_query, $analyzed_sql, $display_query) { $sql_limit_to_append = ' LIMIT ' . $_SESSION['tmp_user_values']['pos'] . ', ' . $_SESSION['tmp_user_values']['max_rows'] . " "; diff --git a/sql.php b/sql.php index 1ab5ca2961..fc15406fc8 100644 --- a/sql.php +++ b/sql.php @@ -233,7 +233,7 @@ $sql_limit_to_append = ''; if (PMA_isAppendLimitClause($analyzed_sql_results)) { list($sql_limit_to_append, $full_sql_query, $analyzed_display_query, $display_query - ) = PMA_appendLimitClaues( + ) = PMA_appendLimitClause( $full_sql_query, $analyzed_sql, isset($display_query) ); } From 406dbed4d92ae1c1cb866a62f5657fe3abb92db6 Mon Sep 17 00:00:00 2001 From: Spun Nakandala Date: Wed, 26 Jun 2013 16:23:53 +0530 Subject: [PATCH 5/6] corrected the comment --- libraries/sql.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/sql.lib.php b/libraries/sql.lib.php index 64398d4b6f..6cc7f162e9 100644 --- a/libraries/sql.lib.php +++ b/libraries/sql.lib.php @@ -945,7 +945,7 @@ function PMA_addBookmark($pmaAbsoluteUri, $goto) * * @param String $db the current database * @param String $table the current table - * @return mixed the number of records if "retain" param is true, otherwise true + * @return mixed the number of rows if "retain" param is true, otherwise true */ function PMA_findRealEndOfRows($db, $table) { From a25f96ebd821722fdb608169cf52524f95c91e16 Mon Sep 17 00:00:00 2001 From: Spun Nakandala Date: Wed, 26 Jun 2013 16:25:44 +0530 Subject: [PATCH 6/6] typo corrected --- libraries/sql.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/sql.lib.php b/libraries/sql.lib.php index 6cc7f162e9..f6b83a19dc 100644 --- a/libraries/sql.lib.php +++ b/libraries/sql.lib.php @@ -958,7 +958,7 @@ function PMA_findRealEndOfRows($db, $table) } /** - * Function to get values for the redational columns + * Function to get values for the relational columns * * @param String $db the current database * @param String $table the current table