Skip to content

Commit

Permalink
Refactor high-level parser calling into a function
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Delisle <marc@infomarc.info>
  • Loading branch information
lem9 committed Oct 22, 2015
1 parent 52de5b1 commit faaec8e
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 66 deletions.
9 changes: 8 additions & 1 deletion db_qbe.php
Expand Up @@ -82,7 +82,14 @@
$goto = 'db_sql.php';

// Parse and analyze the query
include_once 'libraries/parse_analyze.inc.php';
include_once 'libraries/parse_analyze.lib.php';
list(
$analyzed_sql_results,
$db,
$table
) = PMA_parseAnalyze($sql_query, $db);
// @todo: possibly refactor
extract($analyzed_sql_results);

PMA_executeQueryAndSendQueryResponse(
$analyzed_sql_results, // analyzed_sql_results
Expand Down
20 changes: 18 additions & 2 deletions import.php
Expand Up @@ -732,7 +732,15 @@
// can choke on it so avoid parsing)
$sqlLength = /*overload*/mb_strlen($sql_query);
if ($sqlLength <= $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
include_once 'libraries/parse_analyze.inc.php';
include_once 'libraries/parse_analyze.lib.php';

list(
$analyzed_sql_results,
$db,
$table
) = PMA_parseAnalyze($sql_query, $db);
// @todo: possibly refactor
extract($analyzed_sql_results);
}

// There was an error?
Expand All @@ -755,8 +763,16 @@

$html_output = '';
foreach ($sql_queries as $sql_query) {

// parse sql query
include 'libraries/parse_analyze.inc.php';
include_once 'libraries/parse_analyze.lib.php';
list(
$analyzed_sql_results,
$db,
$table
) = PMA_parseAnalyze($sql_query, $db);
// @todo: possibly refactor
extract($analyzed_sql_results);

$html_output .= PMA_executeQueryAndGetQueryResponse(
$analyzed_sql_results, // analyzed_sql_results
Expand Down
10 changes: 8 additions & 2 deletions libraries/controllers/TableStructureController.php
Expand Up @@ -801,9 +801,15 @@ protected function displayTableBrowseForSelectedColumns($goto, $pmaThemeImage)
);

// Parse and analyze the query
// @todo Refactor parse_analyze.inc to protected function
$db = &$this->db;
include_once 'libraries/parse_analyze.inc.php';
include_once 'libraries/parse_analyze.lib.php';
list(
$analyzed_sql_results,
$db,
$table
) = PMA_parseAnalyze($sql_query, $db);
// @todo: possibly refactor
extract($analyzed_sql_results);

include_once 'libraries/sql.lib.php';

Expand Down
9 changes: 8 additions & 1 deletion libraries/controllers/table/TableSearchController.php
Expand Up @@ -528,7 +528,14 @@ public function doSelectionAction()
/**
* Parse and analyze the query
*/
include_once 'libraries/parse_analyze.inc.php';
include_once 'libraries/parse_analyze.lib.php';
list(
$analyzed_sql_results,
$db,
$table
) = PMA_parseAnalyze($sql_query, $db);
// @todo: possibly refactor
extract($analyzed_sql_results);

PMA_executeQueryAndSendQueryResponse(
$analyzed_sql_results, // analyzed_sql_results
Expand Down
9 changes: 8 additions & 1 deletion libraries/mult_submits.inc.php
Expand Up @@ -258,7 +258,14 @@
/**
* Parse and analyze the query
*/
include_once 'libraries/parse_analyze.inc.php';
include_once 'libraries/parse_analyze.lib.php';
list(
$analyzed_sql_results,
$db,
$table
) = PMA_parseAnalyze($sql_query, $db);
// @todo: possibly refactor
extract($analyzed_sql_results);

PMA_executeQueryAndSendQueryResponse(
$analyzed_sql_results, // analyzed_sql_results
Expand Down
55 changes: 0 additions & 55 deletions libraries/parse_analyze.inc.php

This file was deleted.

68 changes: 68 additions & 0 deletions libraries/parse_analyze.lib.php
@@ -0,0 +1,68 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Parse and analyse a SQL query
*
* @package PhpMyAdmin
*/
if (! defined('PHPMYADMIN')) {
exit;
}

/**
* Calls the parser on a query
*
* @param string $sql_query the query to parse
* @param string $db the current database
*
* @return array
*
* @access public
*/
function PMA_parseAnalyze($sql_query, $db)
{
// @todo: move to returned results (also in all the calling chain)
$GLOBALS['unparsed_sql'] = $sql_query;

// Get details about the SQL query.
$analyzed_sql_results = SqlParser\Utils\Query::getAll($sql_query);

extract($analyzed_sql_results);
$table = '';

// If the targeted table (and database) are different than the ones that is
// currently browsed, edit `$db` and `$table` to match them so other elements
// (page headers, links, navigation panel) can be updated properly.
if (!empty($analyzed_sql_results['select_tables'])) {

// Previous table and database name is stored to check if it changed.
$prev_db = $db;

if (count($analyzed_sql_results['select_tables']) > 1) {

/**
* @todo if there are more than one table name in the Select:
* - do not extract the first table name
* - do not show a table name in the page header
* - do not display the sub-pages links)
*/
$table = '';
} else {
$table = $analyzed_sql_results['select_tables'][0][0];
if (!empty($analyzed_sql_results['select_tables'][0][1])) {
$db = $analyzed_sql_results['select_tables'][0][1];
}
}
// There is no point checking if a reload is required if we already decided
// to reload. Also, no reload is required for AJAX requests.
if ((empty($reload)) && (empty($GLOBALS['is_ajax_request']))) {
// NOTE: Database names are case-insensitive.
$reload = strcasecmp($db, $prev_db) != 0;
}

// Updating the array.
$analyzed_sql_results['reload'] = $reload;
}

return array($analyzed_sql_results, $db, $table);
}
10 changes: 8 additions & 2 deletions libraries/sql.lib.php
Expand Up @@ -28,8 +28,14 @@ function PMA_parseAndAnalyze($sql_query, $db = null)
$db = $GLOBALS['db'];
}

// `$sql_query` is being used inside `parse_analyze.inc.php`.
return include 'libraries/parse_analyze.inc.php';
include_once 'libraries/parse_analyze.lib.php';
list(
$analyzed_sql_results,
$db,
$table
) = PMA_parseAnalyze($sql_query, $db);

return $analyzed_sql_results;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion sql.php
Expand Up @@ -145,7 +145,14 @@
/**
* Parse and analyze the query
*/
require_once 'libraries/parse_analyze.inc.php';
require_once 'libraries/parse_analyze.lib.php';
list(
$analyzed_sql_results,
$db,
$table
) = PMA_parseAnalyze($sql_query, $db);
// @todo: possibly refactor
extract($analyzed_sql_results);


/**
Expand Down
9 changes: 8 additions & 1 deletion tbl_row_action.php
Expand Up @@ -153,7 +153,14 @@
/**
* Parse and analyze the query
*/
include_once 'libraries/parse_analyze.inc.php';
include_once 'libraries/parse_analyze.lib.php';
list(
$analyzed_sql_results,
$db,
$table
) = PMA_parseAnalyze($sql_query, $db);
// @todo: possibly refactor
extract($analyzed_sql_results);

PMA_executeQueryAndSendQueryResponse(
$analyzed_sql_results, // analyzed_sql_results
Expand Down

0 comments on commit faaec8e

Please sign in to comment.