Skip to content

Commit b6489cb

Browse files
Copilotswissspidy
andauthored
Fix split_sql_statements() to execute MySQL conditional comments (/*!...*/)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent e1d6e78 commit b6489cb

1 file changed

Lines changed: 35 additions & 9 deletions

File tree

src/DB_Command.php

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2635,13 +2635,14 @@ protected function wpdb_import( $sql_content, $assoc_args = [] ) {
26352635
* @return string[] Array of individual SQL statements.
26362636
*/
26372637
private function split_sql_statements( $sql ) {
2638-
$statements = [];
2639-
$current = '';
2640-
$in_single_quote = false;
2641-
$in_double_quote = false;
2642-
$in_comment = false;
2643-
$in_line_comment = false;
2644-
$length = strlen( $sql );
2638+
$statements = [];
2639+
$current = '';
2640+
$in_single_quote = false;
2641+
$in_double_quote = false;
2642+
$in_comment = false;
2643+
$in_line_comment = false;
2644+
$in_conditional_comment = false;
2645+
$length = strlen( $sql );
26452646

26462647
for ( $i = 0; $i < $length; $i++ ) {
26472648
$char = $sql[ $i ];
@@ -2662,9 +2663,34 @@ private function split_sql_statements( $sql ) {
26622663
continue;
26632664
}
26642665

2666+
// Handle end of MySQL conditional comment (/*!...*/): resume normal parsing.
2667+
if ( $in_conditional_comment && ! $in_single_quote && ! $in_double_quote ) {
2668+
if ( '*' === $char && '/' === $next ) {
2669+
$in_conditional_comment = false;
2670+
++$i;
2671+
continue;
2672+
}
2673+
// Fall through: treat content as regular SQL (handled below).
2674+
}
2675+
26652676
if ( '/' === $char && '*' === $next && ! $in_single_quote && ! $in_double_quote ) {
2666-
$in_comment = true;
2667-
++$i;
2677+
$peek = ( $i + 2 < $length ) ? $sql[ $i + 2 ] : '';
2678+
if ( '!' === $peek ) {
2679+
// MySQL conditional comment (/*!...*/): execute its SQL content.
2680+
$in_conditional_comment = true;
2681+
$i += 2; // skip /*!
2682+
// Skip optional version digits (e.g. 40101 in /*!40101 SET ... */).
2683+
while ( $i + 1 < $length && ctype_digit( $sql[ $i + 1 ] ) ) {
2684+
++$i;
2685+
}
2686+
// Skip one space following version digits, if present.
2687+
if ( $i + 1 < $length && ' ' === $sql[ $i + 1 ] ) {
2688+
++$i;
2689+
}
2690+
} else {
2691+
$in_comment = true;
2692+
++$i;
2693+
}
26682694
continue;
26692695
}
26702696

0 commit comments

Comments
 (0)