/**
  * Prepare unsorted sql query and sort by key drop down
  *
  * @param array  $analyzed_sql_results analyzed sql results
  * @param string $sort_expression      sort expression
  *
  * @return  array   two element array - $unsorted_sql_query, $drop_down_html
  *
  * @access  private
  *
  * @see     _getTableHeaders()
  */
 private function _getUnsortedSqlAndSortByKeyDropDown($analyzed_sql_results, $sort_expression)
 {
     $drop_down_html = '';
     $unsorted_sql_query = SqlParser\Utils\Query::replaceClause($analyzed_sql_results['statement'], $analyzed_sql_results['parser']->list, 'ORDER BY', '');
     // Data is sorted by indexes only if it there is only one table.
     if ($this->_isSelect($analyzed_sql_results)) {
         // grab indexes data:
         $indexes = PMA_Index::getFromTable($this->__get('table'), $this->__get('db'));
         // do we have any index?
         if (!empty($indexes)) {
             $drop_down_html = $this->_getSortByKeyDropDown($indexes, $sort_expression, $unsorted_sql_query);
         }
     }
     return array($unsorted_sql_query, $drop_down_html);
 }
Example #2
0
/**
 * Function to count the total number of rows for the same 'SELECT' query without
 * the 'LIMIT' clause that may have been programatically added
 *
 * @param int    $num_rows             number of rows affected/changed by the query
 * @param bool   $justBrowsing         whether just browsing or not
 * @param string $db                   the current database
 * @param string $table                the current table
 * @param array  $analyzed_sql_results the analyzed query and other variables set
 *                                     after analyzing the query
 *
 * @return int $unlim_num_rows unlimited number of rows
 */
function PMA_countQueryResults($num_rows, $justBrowsing, $db, $table, $analyzed_sql_results)
{
    if (!PMA_isAppendLimitClause($analyzed_sql_results)) {
        // if we did not append a limit, set this to get a correct
        // "Showing rows..." message
        // $_SESSION['tmpval']['max_rows'] = 'all';
        $unlim_num_rows = $num_rows;
    } elseif ($analyzed_sql_results['querytype'] == 'SELECT' || $analyzed_sql_results['is_subquery']) {
        //    c o u n t    q u e r y
        // If we are "just browsing", there is only one table,
        // and no WHERE clause (or just 'WHERE 1 '),
        // we do a quick count (which uses MaxExactCount) because
        // SQL_CALC_FOUND_ROWS is not quick on large InnoDB tables
        // However, do not count again if we did it previously
        // due to $find_real_end == true
        if ($justBrowsing) {
            // Get row count (is approximate for InnoDB)
            $unlim_num_rows = $GLOBALS['dbi']->getTable($db, $table)->countRecords();
            /**
             * @todo Can we know at this point that this is InnoDB,
             *       (in this case there would be no need for getting
             *       an exact count)?
             */
            if ($unlim_num_rows < $GLOBALS['cfg']['MaxExactCount']) {
                // Get the exact count if approximate count
                // is less than MaxExactCount
                /**
                 * @todo In countRecords(), MaxExactCount is also verified,
                 *       so can we avoid checking it twice?
                 */
                $unlim_num_rows = $GLOBALS['dbi']->getTable($db, $table)->countRecords(true);
            }
        } else {
            // The SQL_CALC_FOUND_ROWS option of the SELECT statement is used.
            // For UNION statements, only a SQL_CALC_FOUND_ROWS is required
            // after the first SELECT.
            $count_query = SqlParser\Utils\Query::replaceClause($analyzed_sql_results['statement'], $analyzed_sql_results['parser']->list, 'SELECT SQL_CALC_FOUND_ROWS', null, true);
            // Another LIMIT clause is added to avoid long delays.
            // A complete result will be returned anyway, but the LIMIT would
            // stop the query as soon as the result that is required has been
            // computed.
            if (empty($analyzed_sql_results['union'])) {
                $count_query .= ' LIMIT 1';
            }
            // Running the count query.
            $GLOBALS['dbi']->tryQuery($count_query);
            $unlim_num_rows = $GLOBALS['dbi']->fetchValue('SELECT FOUND_ROWS()');
        }
        // end else "just browsing"
    } else {
        // not $is_select
        $unlim_num_rows = 0;
    }
    return $unlim_num_rows;
}