From 67ff3aee1b806f9d12d7f886f624016fb14b6513 Mon Sep 17 00:00:00 2001 From: Spun Nakandala Date: Thu, 27 Jun 2013 15:50:50 +0530 Subject: [PATCH 1/7] Added methods PMA_getDefaultSqlQueryForBrowse, PMA_goBackFurtherPage, PMA_handleQueryExecuteError, PMA_storeTheQueryAsBookmark, PMA_executeQueryAndStoreResults --- libraries/sql.lib.php | 158 ++++++++++++++++++++++++++++++++++++++++++ sql.php | 117 ++++--------------------------- 2 files changed, 171 insertions(+), 104 deletions(-) diff --git a/libraries/sql.lib.php b/libraries/sql.lib.php index f6b83a19dc..a2442b9026 100644 --- a/libraries/sql.lib.php +++ b/libraries/sql.lib.php @@ -1052,4 +1052,162 @@ function PMA_appendLimitClause($full_sql_query, $analyzed_sql, $display_query) isset($display_query) ? $display_query : null ); } + +/** + * Function to get the default sql query for browsing page + * + * @param String $db the current database + * @param String $table the current table + * @return String $sql_query the default $sql_query for browse page + */ +function PMA_getDefaultSqlQueryForBrowse($db, $table) +{ + include_once 'libraries/bookmark.lib.php'; + $book_sql_query = PMA_Bookmark_get( + $db, + '\'' . PMA_Util::sqlAddSlashes($table) . '\'', + 'label', + false, + true + ); + + if (! empty($book_sql_query)) { + $GLOBALS['using_bookmark_message'] = PMA_message::notice( + __('Using bookmark "%s" as default browse query.') + ); + $GLOBALS['using_bookmark_message']->addParam($table); + $GLOBALS['using_bookmark_message']->addMessage( + PMA_Util::showDocu('faq', 'faq6-22') + ); + $sql_query = $book_sql_query; + } else { + $sql_query = 'SELECT * FROM ' . PMA_Util::backquote($table); + } + unset($book_sql_query); + + return $sql_query; +} +/** + * Function to go further a page + * @param String $back go back page url + * @param boolean $is_gotofile whether to go to a file or not + * @param String $table the current table + * @param String $pmaAbsoluteUri PMA absolute URI + */ +function PMA_goBackFurtherPage($back, $is_gotofile, $table, + $pmaAbsoluteUri +) { + if (! empty($back)) { + $goto = $back; + } + if ($is_gotofile) { + if (strpos($goto, 'db_') === 0 && strlen($table)) { + $table = ''; + } + $active_page = $goto; + include '' . PMA_securePath($goto); + } else { + PMA_sendHeaderLocation( + $pmaAbsoluteUri . str_replace('&', '&', $goto) + ); + } + exit(); +} + +function PMA_handleQueryExecuteError($is_gotofile, $goto, $table, $active_page, + $error, $err_url, $sql_query, $full_sql_query +) { + if ($is_gotofile) { + if (strpos($goto, 'db_') === 0 && strlen($table)) { + $table = ''; + } + $active_page = $goto; + $message = PMA_Message::rawError($error); + + if ($GLOBALS['is_ajax_request'] == true) { + $response = PMA_Response::getInstance(); + $response->isSuccess(false); + $response->addJSON('message', $message); + exit; + } + + /** + * Go to target path. + */ + include '' . PMA_securePath($goto); + } else { + $full_err_url = $err_url; + if (preg_match('@^(db|tbl)_@', $err_url)) { + $full_err_url .= '&show_query=1&sql_query=' + . urlencode($sql_query); + } + PMA_Util::mysqlDie($error, $full_sql_query, '', $full_err_url); + } + exit; +} + +/** + * Function to store the query as a bookmark + * + * @param String $db the current database + * @param String $bkm_user the bookmarking user + * @param String $import_text import text + * @param String $bkm_label bookmark label + * @param boolean $bkm_replace whether to rep;ace existing bookmarks + */ +function PMA_storeTheQueryAsBookmark($db, $bkm_user, $import_text, + $bkm_label, $bkm_replace +) { + include_once 'libraries/bookmark.lib.php'; + $bfields = array( + 'dbase' => $db, + 'user' => $bkm_user, + 'query' => urlencode($import_text), + 'label' => $bkm_label + ); + + // Should we replace bookmark? + if (isset($bkm_replace)) { + $bookmarks = PMA_Bookmark_getList($db); + foreach ($bookmarks as $key => $val) { + if ($val == $bkm_label) { + PMA_Bookmark_delete($db, $key); + } + } + } + + PMA_Bookmark_save($bfields, isset($_POST['bkm_all_users'])); + +} + +/** + * Function to execute the SQL query and set the execution time + * + * @param String $full_sql_query the full sql query + * @return mixed $result the results after running the query + */ +function PMA_executeQueryAndStoreResults($full_sql_query){ + // Measure query time. + $querytime_before = array_sum(explode(' ', microtime())); + + $result = @$GLOBALS['dbi']->tryQuery( + $full_sql_query, null, PMA_DatabaseInterface::QUERY_STORE + ); + $querytime_after = array_sum(explode(' ', microtime())); + + $GLOBALS['querytime'] = $querytime_after - $querytime_before; + + // If a stored procedure was called, there may be more results that are + // queued up and waiting to be flushed from the buffer. So let's do that. + do { + $GLOBALS['dbi']->storeResult(); + if (! $GLOBALS['dbi']->moreResults()) { + break; + } + } while ($GLOBALS['dbi']->nextResult()); + + return $result; +} + + ?> diff --git a/sql.php b/sql.php index fc15406fc8..77b4efcb8e 100644 --- a/sql.php +++ b/sql.php @@ -107,29 +107,8 @@ if (isset($_REQUEST['set_col_prefs']) && $_REQUEST['set_col_prefs'] == true) { // Default to browse if no query set and we have table // (needed for browsing from DefaultTabTable) if (empty($sql_query) && strlen($table) && strlen($db)) { - include_once 'libraries/bookmark.lib.php'; - $book_sql_query = PMA_Bookmark_get( - $db, - '\'' . PMA_Util::sqlAddSlashes($table) . '\'', - 'label', - false, - true - ); - - if (! empty($book_sql_query)) { - $GLOBALS['using_bookmark_message'] = PMA_message::notice( - __('Using bookmark "%s" as default browse query.') - ); - $GLOBALS['using_bookmark_message']->addParam($table); - $GLOBALS['using_bookmark_message']->addMessage( - PMA_Util::showDocu('faq', 'faq6-22') - ); - $sql_query = $book_sql_query; - } else { - $sql_query = 'SELECT * FROM ' . PMA_Util::backquote($table); - } - unset($book_sql_query); - + $sql_query = PMA_getDefaultSqlQueryForBrowse($db, $table); + // set $goto to what will be displayed if query returns 0 rows $goto = ''; } else { @@ -202,21 +181,10 @@ if ($goto == 'sql.php') { * Go back to further page if table should not be dropped */ if (isset($_REQUEST['btnDrop']) && $_REQUEST['btnDrop'] == __('No')) { - if (! empty($back)) { - $goto = $back; - } - if ($is_gotofile) { - if (strpos($goto, 'db_') === 0 && strlen($table)) { - $table = ''; - } - $active_page = $goto; - include '' . PMA_securePath($goto); - } else { - PMA_sendHeaderLocation( - $cfg['PmaAbsoluteUri'] . str_replace('&', '&', $goto) - ); - } - exit(); + PMA_goBackFurtherPage( + isset($back) ? $back : null, $is_gotofile, + $table, $cfg['PmaAbsoluteUri'] + ); } // end if @@ -261,22 +229,8 @@ if (isset($GLOBALS['show_as_php']) || ! empty($GLOBALS['validatequery'])) { $GLOBALS['dbi']->query('SET PROFILING=1;'); } - // Measure query time. - $querytime_before = array_sum(explode(' ', microtime())); - - $result = @$GLOBALS['dbi']->tryQuery( - $full_sql_query, null, PMA_DatabaseInterface::QUERY_STORE - ); - - // If a stored procedure was called, there may be more results that are - // queued up and waiting to be flushed from the buffer. So let's do that. - do { - $GLOBALS['dbi']->storeResult(); - if (! $GLOBALS['dbi']->moreResults()) { - break; - } - } while ($GLOBALS['dbi']->nextResult()); - + $result = PMA_executeQueryAndStoreResults($full_sql_query); + $is_procedure = false; // Since multiple query execution is anyway handled, @@ -288,66 +242,21 @@ if (isset($GLOBALS['show_as_php']) || ! empty($GLOBALS['validatequery'])) { $is_procedure = true; } - $querytime_after = array_sum(explode(' ', microtime())); - - $GLOBALS['querytime'] = $querytime_after - $querytime_before; - // Displays an error message if required and stop parsing the script $error = $GLOBALS['dbi']->getError(); if ($error) { - if ($is_gotofile) { - if (strpos($goto, 'db_') === 0 && strlen($table)) { - $table = ''; - } - $active_page = $goto; - $message = PMA_Message::rawError($error); - - if ($GLOBALS['is_ajax_request'] == true) { - $response = PMA_Response::getInstance(); - $response->isSuccess(false); - $response->addJSON('message', $message); - exit; - } - - /** - * Go to target path. - */ - include '' . PMA_securePath($goto); - } else { - $full_err_url = $err_url; - if (preg_match('@^(db|tbl)_@', $err_url)) { - $full_err_url .= '&show_query=1&sql_query=' - . urlencode($sql_query); - } - PMA_Util::mysqlDie($error, $full_sql_query, '', $full_err_url); - } - exit; + PMA_handleQueryExecuteError($is_gotofile, $goto, $table, $active_page, + $error, $err_url, $sql_query, $full_sql_query + ); } unset($error); // If there are no errors and bookmarklabel was given, // store the query as a bookmark if (! empty($bkm_label) && ! empty($import_text)) { - include_once 'libraries/bookmark.lib.php'; - $bfields = array( - 'dbase' => $db, - 'user' => $cfg['Bookmark']['user'], - 'query' => urlencode($import_text), - 'label' => $bkm_label + PMA_storeTheQueryAsBookmark($db, $cfg['Bookmark']['user'], + $import_text, $bkm_label, $bkm_replace ); - - // Should we replace bookmark? - if (isset($bkm_replace)) { - $bookmarks = PMA_Bookmark_getList($db); - foreach ($bookmarks as $key => $val) { - if ($val == $bkm_label) { - PMA_Bookmark_delete($db, $key); - } - } - } - - PMA_Bookmark_save($bfields, isset($_POST['bkm_all_users'])); - $bookmark_created = true; } // end store bookmarks From e40d24f52f772124bb5c7534ac9b2097cc35d967 Mon Sep 17 00:00:00 2001 From: Spun Nakandala Date: Thu, 27 Jun 2013 19:18:50 +0530 Subject: [PATCH 2/7] created method PMA_getNumberOfRowsAffectedOrChanged --- libraries/sql.lib.php | 18 ++++++++++++++++++ sql.php | 8 +++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/libraries/sql.lib.php b/libraries/sql.lib.php index a2442b9026..d825e8ae23 100644 --- a/libraries/sql.lib.php +++ b/libraries/sql.lib.php @@ -1209,5 +1209,23 @@ function PMA_executeQueryAndStoreResults($full_sql_query){ return $result; } +/** + * Function to get the affected or changed number of rows after executing a query + * + * @param boolean $is_affected whether the query affected a table + * @param mixed $result results of executing the query + * @param int $num_rows number of rows affected or changed + * @return int $num_rows number of rows affected or changed + */ +function PMA_getNumberOfRowsAffectedOrChanged($is_affected, $result, $num_rows) +{ + if (! $is_affected) { + $num_rows = ($result) ? @$GLOBALS['dbi']->numRows($result) : 0; + } elseif (! isset($num_rows)) { + $num_rows = @$GLOBALS['dbi']->affectedRows(); + } + + return $num_rows; +} ?> diff --git a/sql.php b/sql.php index 77b4efcb8e..f1cec5d91a 100644 --- a/sql.php +++ b/sql.php @@ -264,11 +264,9 @@ if (isset($GLOBALS['show_as_php']) || ! empty($GLOBALS['validatequery'])) { // (This must be done immediately after the query because // mysql_affected_rows() reports about the last query done) - if (! $is_affected) { - $num_rows = ($result) ? @$GLOBALS['dbi']->numRows($result) : 0; - } elseif (! isset($num_rows)) { - $num_rows = @$GLOBALS['dbi']->affectedRows(); - } + $num_rows = PMA_getNumberOfRowsAffectedOrChanged($is_affected, $result, + isset($num_rows) ? $num_rows : null + ); // Grabs the profiling results if (isset($_SESSION['profiling']) && PMA_Util::profilingSupported()) { From 5a2b0b2581590abf9149b6b6987c7e729cbba4ce Mon Sep 17 00:00:00 2001 From: Spun Nakandala Date: Thu, 27 Jun 2013 19:29:10 +0530 Subject: [PATCH 3/7] added method PMA_hasCurrentDBChanged --- libraries/sql.lib.php | 23 +++++++++++++++++++++++ sql.php | 11 +---------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/libraries/sql.lib.php b/libraries/sql.lib.php index d825e8ae23..3c04221831 100644 --- a/libraries/sql.lib.php +++ b/libraries/sql.lib.php @@ -1228,4 +1228,27 @@ function PMA_getNumberOfRowsAffectedOrChanged($is_affected, $result, $num_rows) return $num_rows; } +/** + * Checks if the current database has changed + * This could happen if the user sends a query like "USE `database`;" + * + * @param String $db the database in the query + * @return int $reload whether to reload the navigation(1) or not(0) + */ +function PMA_hasCurrentDBChanged($db) +{ + // Checks if the current database has changed + // This could happen if the user sends a query like "USE `database`;" + $reload = 0; + if (strlen($db)) { + $current_db = $GLOBALS['dbi']->fetchValue('SELECT DATABASE()'); + if ($db !== $current_db) { + $reload = 1; + } + unset($current_db); + $GLOBALS['dbi']->selectDb($db); + } + + return $reload; +} ?> diff --git a/sql.php b/sql.php index f1cec5d91a..b4400a661f 100644 --- a/sql.php +++ b/sql.php @@ -206,16 +206,7 @@ if (PMA_isAppendLimitClause($analyzed_sql_results)) { ); } -if (strlen($db)) { - // Checks if the current database has changed - // This could happen if the user sends a query like "USE `database`;" - $current_db = $GLOBALS['dbi']->fetchValue('SELECT DATABASE()'); - if ($db !== $current_db) { - $reload = 1; - } - unset($current_db); - $GLOBALS['dbi']->selectDb($db); -} +$reload = PMA_hasCurrentDBChanged($db); // E x e c u t e t h e q u e r y From d3e6df82d384cb8315433006c47967791fdcbf3b Mon Sep 17 00:00:00 2001 From: Spun Nakandala Date: Thu, 27 Jun 2013 22:43:56 +0530 Subject: [PATCH 4/7] function renamed --- 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 3c04221831..41f6cfd7aa 100644 --- a/libraries/sql.lib.php +++ b/libraries/sql.lib.php @@ -1235,7 +1235,7 @@ function PMA_getNumberOfRowsAffectedOrChanged($is_affected, $result, $num_rows) * @param String $db the database in the query * @return int $reload whether to reload the navigation(1) or not(0) */ -function PMA_hasCurrentDBChanged($db) +function PMA_hasCurrentDbChanged($db) { // Checks if the current database has changed // This could happen if the user sends a query like "USE `database`;" diff --git a/sql.php b/sql.php index b4400a661f..2d676600a7 100644 --- a/sql.php +++ b/sql.php @@ -206,7 +206,7 @@ if (PMA_isAppendLimitClause($analyzed_sql_results)) { ); } -$reload = PMA_hasCurrentDBChanged($db); +$reload = PMA_hasCurrentDbChanged($db); // E x e c u t e t h e q u e r y From 8c40ba768c9f813beb3c65b7bf587c9d3c15ecbe Mon Sep 17 00:00:00 2001 From: Spun Nakandala Date: Thu, 27 Jun 2013 22:44:25 +0530 Subject: [PATCH 5/7] removed unnecessary code --- libraries/sql.lib.php | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/sql.lib.php b/libraries/sql.lib.php index 41f6cfd7aa..a30f6bb6df 100644 --- a/libraries/sql.lib.php +++ b/libraries/sql.lib.php @@ -1245,7 +1245,6 @@ function PMA_hasCurrentDbChanged($db) if ($db !== $current_db) { $reload = 1; } - unset($current_db); $GLOBALS['dbi']->selectDb($db); } From d1bbfdcf29baf012568ab6ce16143e1376dbd4c7 Mon Sep 17 00:00:00 2001 From: Spun Nakandala Date: Thu, 27 Jun 2013 22:51:01 +0530 Subject: [PATCH 6/7] missing doc added --- libraries/sql.lib.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libraries/sql.lib.php b/libraries/sql.lib.php index a30f6bb6df..7fb4d036f9 100644 --- a/libraries/sql.lib.php +++ b/libraries/sql.lib.php @@ -1114,6 +1114,18 @@ function PMA_goBackFurtherPage($back, $is_gotofile, $table, exit(); } +/** + * Responds an error when an error happens when executing the query + * + * @param boolean $is_gotofile whether goto file or not + * @param String $goto goto page url + * @param String $table current table + * @param String $active_page active page url + * @param String $error error after executing the query + * @param String $err_url the error page url + * @param String $sql_query sql query + * @param String $full_sql_query full sql query + */ function PMA_handleQueryExecuteError($is_gotofile, $goto, $table, $active_page, $error, $err_url, $sql_query, $full_sql_query ) { From c86d80b8c73dfc5c1d000fb864c91fb3f39ee05c Mon Sep 17 00:00:00 2001 From: Spun Nakandala Date: Thu, 27 Jun 2013 22:53:20 +0530 Subject: [PATCH 7/7] irrelevant for current master. So removed. --- libraries/sql.lib.php | 26 -------------------------- sql.php | 10 ---------- 2 files changed, 36 deletions(-) diff --git a/libraries/sql.lib.php b/libraries/sql.lib.php index 7fb4d036f9..b06f834986 100644 --- a/libraries/sql.lib.php +++ b/libraries/sql.lib.php @@ -1087,32 +1087,6 @@ function PMA_getDefaultSqlQueryForBrowse($db, $table) return $sql_query; } -/** - * Function to go further a page - * @param String $back go back page url - * @param boolean $is_gotofile whether to go to a file or not - * @param String $table the current table - * @param String $pmaAbsoluteUri PMA absolute URI - */ -function PMA_goBackFurtherPage($back, $is_gotofile, $table, - $pmaAbsoluteUri -) { - if (! empty($back)) { - $goto = $back; - } - if ($is_gotofile) { - if (strpos($goto, 'db_') === 0 && strlen($table)) { - $table = ''; - } - $active_page = $goto; - include '' . PMA_securePath($goto); - } else { - PMA_sendHeaderLocation( - $pmaAbsoluteUri . str_replace('&', '&', $goto) - ); - } - exit(); -} /** * Responds an error when an error happens when executing the query diff --git a/sql.php b/sql.php index 2d676600a7..a6af547267 100644 --- a/sql.php +++ b/sql.php @@ -177,16 +177,6 @@ if ($goto == 'sql.php') { . '&sql_query=' . urlencode($sql_query); } // end if -/** - * Go back to further page if table should not be dropped - */ -if (isset($_REQUEST['btnDrop']) && $_REQUEST['btnDrop'] == __('No')) { - PMA_goBackFurtherPage( - isset($back) ? $back : null, $is_gotofile, - $table, $cfg['PmaAbsoluteUri'] - ); -} // end if - // assign default full_sql_query $full_sql_query = $sql_query;