new split_sql() that fixes many bugs

This commit is contained in:
Benjamin Gandon
2001-08-02 18:53:03 +00:00
parent 5bfc7d6cb2
commit a8aa2a9f54
2 changed files with 39 additions and 19 deletions

View File

@@ -9,6 +9,12 @@ $Source$
* sql.php3: fix confirm bug with the new backquotes * sql.php3: fix confirm bug with the new backquotes
* db_readdump.php3: mysql_die2 to mysql_die * db_readdump.php3: mysql_die2 to mysql_die
2001-08-02 Benjamin Gandon <gandon@isia.cma.fr>
* lib.inc.php3: corrected the original bug of split_sql() (present
much before 2.1.0). Removed the quick hack to
handle queries containing "('\\')". The alorithm
is ok now.
2001-08-01 Marc Delisle <lem9@users.sourceforge.net> 2001-08-01 Marc Delisle <lem9@users.sourceforge.net>
* executed remove_control_m.sh on *.php3 * executed remove_control_m.sh on *.php3

View File

@@ -1451,46 +1451,60 @@ var errorMsg2 = '<?php echo(str_replace('\'', '\\\'', $GLOBALS['strNotValidNumbe
/** /**
* Splits up large sql files into individual queries * Splits up large sql files into individual queries
* *
* Last revision: 16th July 2001 - loic1 * Last revision: 2nd August 2001 - Benjamin Gandon
* *
* @param string the sql commands * @param string the sql commands
* @param string the end of command line delimiter * @param char the end of command line delimiter
* *
* @return array the splitted sql commands * @return array the splitted sql commands
*/ */
function split_sql_file($sql, $delimiter) function split_sql_file($sql, $delimiter)
{ {
$sql = trim($sql); $sql = trim($sql);
$char = ''; $char = '';
$last_char = ''; $last_char = '';
$ret = array(); $ret = array();
$in_string = TRUE; $string_start = '';
$in_string = FALSE;
$escaped_backslash = FALSE;
for ($i = 0; $i < strlen($sql); $i++) { for ($i = 0; $i < strlen($sql); ++$i) {
$char = $sql[$i]; $char = $sql[$i];
// if delimiter found, add the parsed part to the returned array // if delimiter found, add the parsed part to the returned array
if ($char == $delimiter && !$in_string) { if (($char == $delimiter) && !$in_string) {
$ret[] = substr($sql, 0, $i); $ret[] = substr($sql, 0, $i);
$sql = substr($sql, $i + 1); $sql = substr($sql, $i + 1);
$i = 0; $i = 0;
$last_char = ''; $last_char = '';
} }
if ($last_char == $in_string && $char == ')') { if ($in_string) {
$in_string = FALSE; // we are in a string, first check for escaped backslashes
} if ($char == '\\') {
if ($char == $in_string && $last_char != '\\') { if ($last_char != '\\') {
$in_string = FALSE; $escaped_backslash = FALSE;
} } else {
else if (!$in_string $escaped_backslash = !$escaped_backslash;
&& ($char == '"' || $char == '\'' || $char == '`') }
&& ($last_char != '\\')) { }
$in_string = $char; // then check for not escaped end of strings
if (($char == $string_start)
&& !(($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;
}
} }
$last_char = $char; $last_char = $char;
} // end for } // end for
// add any rest to the returned array
if (!empty($sql)) { if (!empty($sql)) {
$ret[] = $sql; $ret[] = $sql;
} }