30 $sql_len = strlen($sql);
35 for ($i = 0; $i < $sql_len; ++$i) {
42 $i = strpos($sql, $string_start, $i);
50 if (
'`' === $string_start ||
'\\' !== $sql[$i - 1]) {
63 $escaped_backslash =
false;
64 while ($i-$j > 0 &&
'\\' === $sql[$i - $j]) {
65 $escaped_backslash = !$escaped_backslash;
70 if ($escaped_backslash) {
81 elseif (
';' === $char) {
83 $ret[] = substr($sql, 0, $i);
84 $sql = ltrim(substr($sql, min($i + 1, $sql_len)));
85 $sql_len = strlen($sql);
94 elseif ((
'"' === $char) || (
'\'' === $char) || (
'`' === $char)) {
96 $string_start = $char;
100 elseif (
'#' === $char || (
' ' === $char && $i > 1 &&
'--' == $sql[$i - 2] . $sql[$i - 1])) {
102 $start_of_comment = ((
'#' === $sql[$i]) ? $i : $i - 2);
105 $end_of_comment = (strpos(
' ' . $sql,
"\012", $i+2))
106 ?: strpos(
' ' . $sql,
"\015", $i + 2);
107 if (!$end_of_comment) {
111 $last = trim(substr($sql, 0, $i-1));
118 $sql = substr($sql, 0, $start_of_comment) . ltrim(substr($sql, $end_of_comment));
119 $sql_len = strlen($sql);
125 if (!empty($sql) &&
'' !== trim($sql)) {
140 $pattern =
"/^(INSERT INTO|CREATE TABLE|ALTER TABLE|UPDATE)(\s)+([`]?)([^`\s]+)\\3(\s)+/siU";
141 $pattern2 =
"/^(DROP TABLE)(\s)+([`]?)([^`\s]+)\\3(\s)?$/siU";
142 if (preg_match($pattern, $query, $matches) || preg_match($pattern2, $query, $matches)) {
143 $replace =
"\\1 ".$prefix.
"_\\4\\5";
144 $matches[0] = preg_replace($pattern, $replace, $query);
148 if ($matches[1] ===
'CREATE TABLE') {
149 $matches[0] = preg_replace(
'/ ENGINE=MyISAM/i',
'', $matches[0]);
150 if (!preg_match(
"/ CHARACTER SET /i", $matches[0])) {
152 $matches[0] .=
' CHARACTER SET utf8mb4 ';