Skip to content

Commit

Permalink
bug #1120434, comment at the end of query is applied to appended LIMI…
Browse files Browse the repository at this point in the history
…T as well
  • Loading branch information
Marc Delisle committed May 15, 2005
1 parent 305a935 commit e8be67e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ phpMyAdmin - Changelog
$Id$
$Source$

2005-05-15 Marc Delisle <lem9@users.sourceforge.net>
* sql.php, libraries/sqlparser.lib.php: bug #1120434, comment at the end
of query is applied to appended LIMIT as well

2005-05-13 Marc Delisle <lem9@users.sourceforge.net>
* tbl_printview.php: bug #1178760, header not sent when displaying
print view of multi tables, thanks to Hrvoje Novosel - interghost
Expand Down
39 changes: 37 additions & 2 deletions libraries/sqlparser.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,14 @@ function PMA_SQP_analyze($arr)
* ['default_current_timestamp'] boolean
* ['on_update_current_timestamp'] boolean
*
* section_before_limit, section_after_limit
* -----------------------------------------
*
* Marks the point of the query where we can insert a LIMIT clause;
* so the section_before_limit will contain the left part before
* a possible LIMIT clause
*
*
* End of description of analyzer results
*/

Expand Down Expand Up @@ -1263,15 +1271,19 @@ function PMA_SQP_analyze($arr)
// -------------------------------------------------------


// loop #2: for queryflags
// ,querytype (for queries != 'SELECT')
// loop #2: - queryflags
// - querytype (for queries != 'SELECT')
// - section_before_limit, section_after_limit
//
// we will also need this queryflag in loop 2
// so set it here
if (isset($current_table_ref) && $current_table_ref > -1) {
$subresult['queryflags']['select_from'] = 1;
}

$collect_section_before_limit = TRUE;
$section_before_limit = '';
$section_after_limit = '';
$seen_reserved_word = FALSE;
$seen_group = FALSE;
$seen_order = FALSE;
Expand Down Expand Up @@ -1301,6 +1313,12 @@ function PMA_SQP_analyze($arr)

// TODO: check for punct_queryend


// TODO: verify C-style comments?
if ($arr[$i]['type'] == 'comment_ansi') {
$collect_section_before_limit = FALSE;
}

if ($arr[$i]['type'] == 'alpha_reservedWord') {
$upper_data = strtoupper($arr[$i]['data']);
if (!$seen_reserved_word) {
Expand All @@ -1326,6 +1344,13 @@ function PMA_SQP_analyze($arr)
}
}

if ($upper_data == 'PROCEDURE') {
$collect_section_before_limit = FALSE;
}
// TODO: set also to FALSE if we find
// FOR UPDATE
// LOCK IN SHARE MODE

if ($upper_data == 'SELECT') {
$in_select_expr = TRUE;
$select_expr_clause = '';
Expand Down Expand Up @@ -1495,8 +1520,16 @@ function PMA_SQP_analyze($arr)
// clear $upper_data for next iteration
$upper_data='';

if ($collect_section_before_limit) {
$section_before_limit .= $arr[$i]['data'] . $sep;
} else {
$section_after_limit .= $arr[$i]['data'] . $sep;
}


} // end for $i (loop #2)


// -----------------------------------------------------
// loop #3: foreign keys and MySQL 4.1.2+ TIMESTAMP options
// (for now, check only the first query)
Expand Down Expand Up @@ -1753,6 +1786,8 @@ function PMA_SQP_analyze($arr)

if (isset($position_of_first_select)) {
$subresult['position_of_first_select'] = $position_of_first_select;
$subresult['section_before_limit'] = $section_before_limit;
$subresult['section_after_limit'] = $section_after_limit;
}

// They are naughty and didn't have a trailing semi-colon,
Expand Down
34 changes: 24 additions & 10 deletions sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,20 +327,34 @@
&& isset($analyzed_sql[0]['queryflags']['select_from'])
&& !isset($analyzed_sql[0]['queryflags']['offset'])
&& !preg_match('@[[:space:]]LIMIT[[:space:]0-9,-]+$@i', $sql_query)) {
$sql_limit_to_append = " LIMIT $pos, ".$cfg['MaxRows'];
if (preg_match('@(.*)([[:space:]](PROCEDURE[[:space:]](.*)|FOR[[:space:]]+UPDATE|LOCK[[:space:]]+IN[[:space:]]+SHARE[[:space:]]+MODE))$@i', $sql_query, $regs)) {
$full_sql_query = $regs[1] . $sql_limit_to_append . $regs[2];
} else {
$full_sql_query = $sql_query . $sql_limit_to_append;
}
$sql_limit_to_append = " LIMIT $pos, ".$cfg['MaxRows'] . " ";

// if (preg_match('@(.*)([[:space:]](PROCEDURE[[:space:]](.*)|FOR[[:space:]]+UPDATE|LOCK[[:space:]]+IN[[:space:]]+SHARE[[:space:]]+MODE))$@i', $sql_query, $regs)) {
// $full_sql_query = $regs[1] . $sql_limit_to_append . $regs[2];
// } else {
// $full_sql_query = $sql_query . $sql_limit_to_append;
// }

$full_sql_query = $analyzed_sql[0]['section_before_limit'] . $sql_limit_to_append . $analyzed_sql[0]['section_after_limit'];
// FIXME: pretty printing of this modified query

if (isset($display_query)) {
if (preg_match('@((.|\n)*)(([[:space:]](PROCEDURE[[:space:]](.*)|FOR[[:space:]]+UPDATE|LOCK[[:space:]]+IN[[:space:]]+SHARE[[:space:]]+MODE))|;)[[:space:]]*$@i', $display_query, $regs)) {
$display_query = $regs[1] . $sql_limit_to_append . $regs[3];
} else {
$display_query = $display_query . $sql_limit_to_append;
// if (preg_match('@((.|\n)*)(([[:space:]](PROCEDURE[[:space:]](.*)|FOR[[:space:]]+UPDATE|LOCK[[:space:]]+IN[[:space:]]+SHARE[[:space:]]+MODE))|;)[[:space:]]*$@i', $display_query, $regs)) {
// $display_query = $regs[1] . $sql_limit_to_append . $regs[3];
// } else {
// $display_query = $display_query . $sql_limit_to_append;
// }

// 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'])) {
$analyzed_display_query = PMA_SQP_analyze(PMA_SQP_parse($display_query));
$display_query = $analyzed_display_query[0]['section_before_limit'] . $sql_limit_to_append . $analyzed_display_query[0]['section_after_limit'];
}
}

} else {
$full_sql_query = $sql_query;
} // end if...else
Expand Down

0 comments on commit e8be67e

Please sign in to comment.