* 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.
This commit is contained in:
Loïc Chapeaux
2001-09-11 09:26:40 +00:00
parent 615986dada
commit e34a463b3c
2 changed files with 134 additions and 142 deletions

View File

@@ -1,132 +0,0 @@
<?php
/* $Id$ */
/**
* Set of functions used to get commands from imported file
*/
if (!defined('__LIB_READ_DUMP__')){
define('__LIB_READ_DUMP__', 1);
/**
* Splits up large sql files into individual queries
*
* Last revision: 22 August 2001 - loic1
*
* @param string the sql commands
* @param string the end of command line delimiter
*
* @return array the splitted sql commands
*
* @access public
*/
function split_sql_file($sql, $delimiter)
{
$sql = trim($sql);
$char = '';
$last_char = '';
$ret = array();
$string_start = '';
$in_string = FALSE;
$in_comment = FALSE;
$escaped_backslash = FALSE;
for ($i = 0; $i < strlen($sql); ++$i) {
$char = $sql[$i];
// if delimiter found, add the parsed part to the returned array
if ($char == $delimiter && !$in_string && !$in_comment) {
$ret[] = substr($sql, 0, $i);
$sql = substr($sql, $i + 1);
$i = 0;
$last_char = '';
}
// if in comment, add the parsed part to the returned array and
// remove the comment (till the first end of line)
else if ($in_comment) {
$ret[] = substr($sql, 0, $i);
$pos = strpos($sql, "\n");
$sql = substr($sql, $pos + 1);
$i = 0;
$last_char = '';
$in_comment = FALSE;
}
if ($in_string) {
// We are in a string, first check for escaped backslashes
if ($char == '\\') {
if ($last_char != '\\') {
$escaped_backslash = FALSE;
} else {
$escaped_backslash = !$escaped_backslash;
}
}
// then check for not escaped end of strings except for
// backquotes than cannot be escaped
if (($char == $string_start)
&& ($char == '`' || !(($last_char == '\\') && !$escaped_backslash))) {
$in_string = FALSE;
$string_start = '';
}
} else {
// we are not in a string, check for start of strings
if (($char == '"') || ($char == '\'') || ($char == '`')) {
$in_string = TRUE;
$string_start = $char;
}
// not start of a string, check for start of a "eol comment"
else if ($char == '#') {
$in_comment = TRUE;
}
}
$last_char = $char;
} // end for
// add any rest to the returned array
if (!empty($sql)) {
$ret[] = $sql;
}
return $ret;
} // end of the 'split_sql_file()' function
/**
* Removes # type remarks from large sql files
*
* Version 3 20th May 2001 - Last Modified By Pete Kelly
*
* @param string the sql commands
*
* @return string the cleaned sql commands
*
* @access public
*/
function remove_remarks($sql)
{
$i = 0;
while ($i < strlen($sql)) {
// Patch from Chee Wai
// (otherwise, if $i == 0 and $sql[$i] == "#", the original order
// in the second part of the AND bit will fail with illegal index)
if ($sql[$i] == '#' && ($i == 0 || $sql[$i-1] == "\n")) {
$j = 1;
while ($sql[$i+$j] != "\n") {
$j++;
if ($j+$i >= 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__
?>

View File

@@ -2,6 +2,129 @@
/* $Id$ */ /* $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 * Increases the max. allowed time to run a script
*/ */
@@ -85,20 +208,22 @@ if (!$cfgAllowUserDropDatabase
} }
define('PMA_CHK_DROP', 1); define('PMA_CHK_DROP', 1);
// Copy the query, used for display purposes only
$sql_query_cpy = $sql_query;
/** /**
* Executes the query * Executes the query
*/ */
if ($sql_query != '') { if ($sql_query != '') {
include('./libraries/read_dump.lib.php3'); $pieces = split_sql_file($sql_query, ';', MYSQL_INT_VERSION);
$sql_query = remove_remarks($sql_query);
$pieces = split_sql_file($sql_query, ';');
$pieces_count = count($pieces); $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 // Only one query to run
if ($pieces_count == 1 && !empty($pieces[0]) && $view_bookmark == 0) { if ($pieces_count == 1 && !empty($pieces[0]) && $view_bookmark == 0) {
// loic1: remove non alphabetic characters from the beginning of the // loic1: remove non alphabetic characters from the beginning of the
@@ -132,6 +257,7 @@ if ($sql_query != '') {
} }
} // end for } // end for
} // end else if } // end else if
unset($pieces);
} // end if } // end if
@@ -144,9 +270,7 @@ if (isset($my_die)) {
mysql_die('', $my_die); mysql_die('', $my_die);
} }
// Be nice with bandwidth... // Be nice with bandwidth...
if ($sql_file != 'none' && $pieces_count > 10) { if ($sql_query_cpy == '') {
$sql_query = '';
unset($sql_query_cpy);
$message = "$strSuccess&nbsp:<br />$strTheContent ($pieces_count $strInstructions)&nbsp;"; $message = "$strSuccess&nbsp:<br />$strTheContent ($pieces_count $strInstructions)&nbsp;";
} else { } else {
$message = $strSuccess; $message = $strSuccess;