diff --git a/libraries/sql.lib.php b/libraries/sql.lib.php index f6b83a19dc..b06f834986 100644 --- a/libraries/sql.lib.php +++ b/libraries/sql.lib.php @@ -1052,4 +1052,188 @@ 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; +} + +/** + * 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 +) { + 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; +} + +/** + * 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; +} + +/** + * 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; + } + $GLOBALS['dbi']->selectDb($db); + } + + return $reload; +} ?> diff --git a/sql.php b/sql.php index fc15406fc8..a6af547267 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 { @@ -198,27 +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')) { - 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(); -} // end if - // assign default full_sql_query $full_sql_query = $sql_query; @@ -238,16 +196,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 @@ -261,22 +210,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 +223,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 @@ -355,11 +245,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()) {