support for EXTRACT FROM

This commit is contained in:
Marc Delisle
2003-01-09 15:33:37 +00:00
parent d94d4ded61
commit 8f6bef6fdc
3 changed files with 73 additions and 23 deletions

View File

@@ -5,6 +5,11 @@ phpMyAdmin - Changelog
$Id$ $Id$
$Source$ $Source$
2003-01-09 Marc Delisle <lem9@users.sourceforge.net>
* sql.php3, libraries/sqlparser.lib.php3: bug 664951, add
support for the EXTRACT ... FROM syntax which is not a real
SELECT ... FROM
2003-01-09 Alexander M. Turek <rabus@users.sourceforge.net> 2003-01-09 Alexander M. Turek <rabus@users.sourceforge.net>
* server_privileges.php3, lang/*.inc.php3: * server_privileges.php3, lang/*.inc.php3:
- Forgot to implement "This host"; - Forgot to implement "This host";

View File

@@ -572,9 +572,14 @@ if (!defined('PMA_SQP_LIB_INCLUDED')) {
'table_ref' => array() 'table_ref' => array()
); );
$subresult_empty = $subresult; $subresult_empty = $subresult;
$seek_queryend = FALSE; $seek_queryend = FALSE;
$seen_end_of_table_ref = FALSE; $seen_end_of_table_ref = FALSE;
// for SELECT EXTRACT(YEAR_MONTH FROM CURDATE())
// we must not use CURDATE as a table_ref
// so we track wether we are in the EXTRACT()
$in_extract = FALSE;
/* Description of analyzer results /* Description of analyzer results
* *
* lem9: db, table, column, alias * lem9: db, table, column, alias
@@ -663,7 +668,7 @@ if (!defined('PMA_SQP_LIB_INCLUDED')) {
// loop #1 for each token: select_expr, table_ref for SELECT // loop #1 for each token: select_expr, table_ref for SELECT
for ($i = 0; $i < $size; $i++) { for ($i = 0; $i < $size; $i++) {
//echo "trace 1<b>" . $arr[$i]['data'] . "</b> (" . $arr[$i]['type'] . ")<br>"; //echo "trace <b>" . $arr[$i]['data'] . "</b> (" . $arr[$i]['type'] . ")<br>";
// High speed seek for locating the end of the current query // High speed seek for locating the end of the current query
if ($seek_queryend == TRUE) { if ($seek_queryend == TRUE) {
@@ -682,6 +687,31 @@ if (!defined('PMA_SQP_LIB_INCLUDED')) {
continue; continue;
} // end if (type == punct_queryend) } // end if (type == punct_queryend)
// ==============================================================
if ($arr[$i]['type'] == 'punct_bracket_open_round') {
if ($in_extract) {
$number_of_brackets_in_extract++;
}
}
// ==============================================================
if ($arr[$i]['type'] == 'punct_bracket_close_round') {
if ($in_extract) {
$number_of_brackets_in_extract--;
if ($number_of_brackets_in_extract == 0) {
$in_extract = FALSE;
}
}
}
// ==============================================================
if ($arr[$i]['type'] == 'alpha_functionName') {
$upper_data = strtoupper($arr[$i]['data']);
if ($upper_data =='EXTRACT') {
$in_extract = TRUE;
$number_of_brackets_in_extract = 0;
}
}
// ==============================================================
if ($arr[$i]['type'] == 'alpha_reservedWord') { if ($arr[$i]['type'] == 'alpha_reservedWord') {
// We don't know what type of query yet, so run this // We don't know what type of query yet, so run this
if ($subresult['querytype'] == '') { if ($subresult['querytype'] == '') {
@@ -704,10 +734,9 @@ if (!defined('PMA_SQP_LIB_INCLUDED')) {
$previous_was_identifier = FALSE; $previous_was_identifier = FALSE;
$current_select_expr = -1; $current_select_expr = -1;
$seen_end_of_table_ref = FALSE; $seen_end_of_table_ref = FALSE;
//$save_table_ref = TRUE;
} // end if ( data == SELECT) } // end if ( data == SELECT)
if ($upper_data =='FROM') { if ($upper_data =='FROM' && !$in_extract) {
$current_table_ref = -1; $current_table_ref = -1;
$seen_from = TRUE; $seen_from = TRUE;
$previous_was_identifier = FALSE; $previous_was_identifier = FALSE;
@@ -966,6 +995,7 @@ if (!defined('PMA_SQP_LIB_INCLUDED')) {
// This is a big hunk of debugging code by Marc for this. // This is a big hunk of debugging code by Marc for this.
// ------------------------------------------------------- // -------------------------------------------------------
/* /*
if (isset($current_select_expr)) {
for ($trace=0; $trace<=$current_select_expr; $trace++) { for ($trace=0; $trace<=$current_select_expr; $trace++) {
echo "<br>"; echo "<br>";
@@ -973,6 +1003,9 @@ if (!defined('PMA_SQP_LIB_INCLUDED')) {
while (list ($key, $val) = each ($subresult['select_expr'][$trace])) while (list ($key, $val) = each ($subresult['select_expr'][$trace]))
echo "sel expr $trace $key => $val<br />\n"; echo "sel expr $trace $key => $val<br />\n";
} }
}
if (isset($current_table_ref)) {
for ($trace=0; $trace<=$current_table_ref; $trace++) { for ($trace=0; $trace<=$current_table_ref; $trace++) {
echo "<br>"; echo "<br>";
@@ -980,6 +1013,7 @@ if (!defined('PMA_SQP_LIB_INCLUDED')) {
while (list ($key, $val) = each ($subresult['table_ref'][$trace])) while (list ($key, $val) = each ($subresult['table_ref'][$trace]))
echo "table ref $trace $key => $val<br />\n"; echo "table ref $trace $key => $val<br />\n";
} }
}
*/ */
// ------------------------------------------------------- // -------------------------------------------------------
@@ -1020,22 +1054,19 @@ if (!defined('PMA_SQP_LIB_INCLUDED')) {
if ($first_reserved_word=='DROP' if ($first_reserved_word=='DROP'
|| $first_reserved_word == 'DELETE') { || $first_reserved_word == 'DELETE') {
$subresult['queryflags']['need_confirm'] = 1; $subresult['queryflags']['need_confirm'] = 1;
break;
} }
} else { } else {
if ($upper_data=='DROP' && $first_reserved_word=='ALTER') { if ($upper_data=='DROP' && $first_reserved_word=='ALTER') {
$subresult['queryflags']['need_confirm'] = 1; $subresult['queryflags']['need_confirm'] = 1;
break;
}
if ($upper_data=='FROM' && $first_reserved_word=='SELECT') {
$subresult['queryflags']['select_from'] = 1;
break;
} }
} }
} }
} // end for $i (loop #2) } // end for $i (loop #2)
if (isset($current_table_ref) && $current_table_ref > -1) {
$subresult['queryflags']['select_from'] = 1;
}
// They are naughty and didn't have a trailing semi-colon, // They are naughty and didn't have a trailing semi-colon,
// then still handle it properly // then still handle it properly

View File

@@ -94,27 +94,40 @@ $is_select = eregi('^SELECT[[:space:]]+', $sql_query);
// $db and $table, to have correct page headers, links and left frame. // $db and $table, to have correct page headers, links and left frame.
// db and table name may be enclosed with backquotes, db is optionnal, // db and table name may be enclosed with backquotes, db is optionnal,
// query may contain aliases. // query may contain aliases.
// (todo: check for embedded comments...)
// (todo: if there are more than one table name in the Select: // (TODO: if there are more than one table name in the Select:
// - do not extract the first table name // - do not extract the first table name
// - do not show a table name in the page header // - do not show a table name in the page header
// - do not display the sub-pages links) // - do not display the sub-pages links)
if ($is_select) { //if ($is_select) {
eregi('^SELECT[[:space:]]+(.*)[[:space:]]+FROM[[:space:]]+(`[^`]+`|[A-Za-z0-9_$]+)([\.]*)(`[^`]*`|[A-Za-z0-9_$]*)', $sql_query, $tmp); // eregi('^SELECT[[:space:]]+(.*)[[:space:]]+FROM[[:space:]]+(`[^`]+`|[A-Za-z0-9_$]+)([\.]*)(`[^`]*`|[A-Za-z0-9_$]*)', $sql_query, $tmp);
//
// if ($tmp[3] == '.') {
// $prev_db = $db;
// $db = str_replace('`', '', $tmp[2]);
// $reload = ($db == $prev_db) ? 0 : 1;
// $table = str_replace('`', '', $tmp[4]);
// }
// else {
// $table = str_replace('`', '', $tmp[2]);
// }
//} // end if
if ($tmp[3] == '.') { if ($is_select) {
$prev_db = $db; $prev_db = $db;
$db = str_replace('`', '', $tmp[2]); if (isset($analyzed_sql[0]['table_ref'][0]['table_true_name'])) {
$reload = ($db == $prev_db) ? 0 : 1; $table = $analyzed_sql[0]['table_ref'][0]['table_true_name'];
$table = str_replace('`', '', $tmp[4]); }
if (isset($analyzed_sql[0]['table_ref'][0]['db'])
&& !empty($analyzed_sql[0]['table_ref'][0]['db'])) {
$db = $analyzed_sql[0]['table_ref'][0]['db'];
} }
else { else {
$table = str_replace('`', '', $tmp[2]); $db = $prev_db;
} }
} // end if $reload = ($db == $prev_db) ? 0 : 1;
}
/** /**
* Sets or modifies the $goto variable if required * Sets or modifies the $goto variable if required
@@ -382,6 +395,7 @@ else {
} // end rows total count } // end rows total count
} // end else "didn't ask to see php code" } // end else "didn't ask to see php code"
// No rows returned -> move back to the calling page // No rows returned -> move back to the calling page
if ($num_rows < 1 || $is_affected) { if ($num_rows < 1 || $is_affected) {
if ($is_delete) { if ($is_delete) {