From e34a463b3c36876caeb806cc864a116c0d49ad61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Chapeaux?= Date: Tue, 11 Sep 2001 09:26:40 +0000 Subject: [PATCH] * moved the 'split_sql_file()' function from the library to the main script, then removed the library; * tried some fixes for bugs with comments characters in dump files (see bug #444279) and taked into account "-- " styled comments; * disactivated the "/*!...*/" syntax for MySQL < 3.22.07; * optimized the 'split_sql_file()' function. --- libraries/read_dump.lib.php3 | 132 -------------------------------- read_dump.php3 | 144 ++++++++++++++++++++++++++++++++--- 2 files changed, 134 insertions(+), 142 deletions(-) delete mode 100644 libraries/read_dump.lib.php3 diff --git a/libraries/read_dump.lib.php3 b/libraries/read_dump.lib.php3 deleted file mode 100644 index 0bc00c1c8..000000000 --- a/libraries/read_dump.lib.php3 +++ /dev/null @@ -1,132 +0,0 @@ -= strlen($sql)) { - break; - } - } // end while - $sql = substr($sql, 0, $i) . substr($sql, $i+$j); - } // end if - $i++; - } // end while - - return $sql; - } // end of the 'remove_remarks()' function - -} // $__LIB_READ_DUMP__ -?> diff --git a/read_dump.php3 b/read_dump.php3 index e96932a02..c93f3c8ed 100644 --- a/read_dump.php3 +++ b/read_dump.php3 @@ -2,6 +2,129 @@ /* $Id$ */ +/** + * Removes comment lines and splits up large sql files into individual queries + * + * Last revision: September 11, 2001 - loic1 + * + * @param string the sql commands + * @param string the end of command line delimiter + * @param integer the MySQL release number (because certains php3 versions + * can't get the value of a constant from within a function) + * + * @return array the splitted sql commands + * + * @access public + */ +function split_sql_file($sql, $delimiter, $release) +{ + $sql = trim($sql); + $sql_len = strlen($sql); + $char = ''; + $ret = array(); + $string_start = ''; + $in_string = FALSE; + $in_comment = FALSE; + + for ($i = 0; $i < $sql_len; ++$i) { + $char = $sql[$i]; + + // We are in a string, check for not escaped end of strings except for + // backquotes than cannot be escaped + if ($in_string) { + while (1) { + $i = strpos($sql, $string_start, $i); + // No end of string found -> add the current substring to the + // returned array + if (!$i) { + $ret[] = $sql; + return $ret; + } + // It's trully the end of the string -> move to the next + // character + else if (($string_start == '`') + || (($i > 1 && $sql[$i-1] . $sql[$i-2] != '\\\\') + || ($sql[0] != '\\'))) { + $string_start = ''; + $in_string = FALSE; + break; + } // end if... elseif + } // end while + } // end if ($in_string) + + // We are in a comment, add the parsed part to the returned array and + // move to the next end of line + else if ($in_comment) { + // comment starting position in string depends on the comment type + $ret_end = (($sql[$i-1] == '#') ? $i-1 : $i-3); + if (ereg('[^[:space:]]+', substr($sql, 0, $ret_end))) { + $ret[] = substr($sql, 0, $ret_end); + } + // if no "\n" exits in the remaining string, checks for "\r" (Mac + // eol style) + $eol_to_find = (strpos($sql, "\012", $i)) ? "\012" : "\015"; + $sql = strstr($sql, $eol_to_find); + if ($sql == '' || empty($sql[1])) { + // The submited statement(s) end(s) by a comment -> stop + // parsing + return $ret; + } else { + $sql = ltrim(substr($sql, 1)); + $sql_len = strlen($sql); + if ($sql_len) { + $i = -1; + $in_comment = FALSE; + } else { + // The submited statement(s) end(s) here + return $ret; + } // end if...else + } // end if...else + } // end if ($in_comment) + + // If delimiter found, add the parsed part to the returned array + else if ($char == $delimiter) { + $ret[] = substr($sql, 0, $i); + $sql = ltrim(substr($sql, min($i + 2, $sql_len))); + $sql_len = strlen($sql); + if ($sql_len) { + $i = -1; + } else { + // The submited statement(s) end(s) here + return $ret; + } + } // end if ($char == $delimiter) + + // We are neither in a string nor in a comment, and nor the current + // character is a delimiter... + else { + // ... first check for start of strings... + if (($char == '"') || ($char == '\'') || ($char == '`')) { + $in_string = TRUE; + $string_start = $char; + } + // ... then check for start of a comment... + else if ($char == '#' + || ($char == ' ' && $i > 1 && $sql[$i-2] . $sql[$i-1] == '--')) { + $in_comment = TRUE; + } + // ... and finally disactivate the "/*!...*/" syntax if + // MySQL < 3.22.07 + else if ($release < 32270 + && ($char == '!' && $i > 1 && $sql[$i-2] . $sql[$i-1] == '/*')) { + $sql[$i] = ' '; + } + } // end else + } // end for + + // add any rest to the returned array + if (!empty($sql)) { + $ret[] = $sql; + } + return $ret; +} // end of the 'split_sql_file()' function + + + /** * Increases the max. allowed time to run a script */ @@ -85,20 +208,22 @@ if (!$cfgAllowUserDropDatabase } define('PMA_CHK_DROP', 1); -// Copy the query, used for display purposes only -$sql_query_cpy = $sql_query; - /** * Executes the query */ if ($sql_query != '') { - include('./libraries/read_dump.lib.php3'); - - $sql_query = remove_remarks($sql_query); - $pieces = split_sql_file($sql_query, ';'); + $pieces = split_sql_file($sql_query, ';', MYSQL_INT_VERSION); $pieces_count = count($pieces); + // Copy of the cleaned sql statement for display purpose only (see near the + // beginning of "db_details.php3" & "tbl_properties.php3") + if ($sql_file != 'none' && $pieces_count > 10) { + $sql_query_cpy = $sql_query = ''; + } else { + $sql_query_cpy = implode(";\n", $pieces) . ';'; + } + // Only one query to run if ($pieces_count == 1 && !empty($pieces[0]) && $view_bookmark == 0) { // loic1: remove non alphabetic characters from the beginning of the @@ -132,6 +257,7 @@ if ($sql_query != '') { } } // end for } // end else if + unset($pieces); } // end if @@ -144,9 +270,7 @@ if (isset($my_die)) { mysql_die('', $my_die); } // Be nice with bandwidth... -if ($sql_file != 'none' && $pieces_count > 10) { - $sql_query = ''; - unset($sql_query_cpy); +if ($sql_query_cpy == '') { $message = "$strSuccess :
$strTheContent ($pieces_count $strInstructions) "; } else { $message = $strSuccess;