Skip to content

Commit ca2f090

Browse files
committed
Bypass SET comments instead of removing all the comments
1 parent e7e8570 commit ca2f090

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

src/Import.php

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -139,37 +139,30 @@ public function parse_statements( $sql_file_path ) {
139139
}
140140

141141
/**
142-
* Execute SQL statements from an SQL dump file using the AST parser.
143-
*
144-
* @param $import_file
145-
*
146-
* @return void
147-
* @throws Exception
148-
*/
142+
* Execute SQL statements from an SQL dump file using the AST parser.
143+
*
144+
* @param $import_file
145+
*
146+
* @return void
147+
* @throws Exception
148+
*/
149149
protected function execute_statements_with_ast_parser( $import_file ) {
150-
$raw_queries = file_get_contents( $import_file );
151-
$queries_text = $this->remove_comments( $raw_queries );
152-
$parser = $this->driver->create_parser( $queries_text );
150+
$raw_queries = file_get_contents( $import_file );
151+
$parser = $this->driver->create_parser( $raw_queries );
153152
while ( $parser->next_query() ) {
154153
$ast = $parser->get_query_ast();
155-
$statement = substr( $queries_text, $ast->get_start(), $ast->get_length() );
154+
$statement = substr( $raw_queries, $ast->get_start(), $ast->get_length() );
156155
try {
157156
$this->driver->query( $statement );
158157
} catch ( Exception $e ) {
158+
// Skip errors when executing SET comment statements
159+
if ( 0 === strpos( $statement, 'SET ' ) && false !== strpos( $statement, '*/' ) ) {
160+
WP_CLI::warning( 'SQLite import SET comment statement: ' . $statement );
161+
continue;
162+
}
159163
WP_CLI::error( 'SQLite import could not execute statement: ' . $statement );
160164
echo $this->driver->get_error_message();
161165
}
162166
}
163167
}
164-
165-
/**
166-
* Remove comments from the input.
167-
*
168-
* @param string $input
169-
*
170-
* @return string
171-
*/
172-
protected function remove_comments( $text ) {
173-
return preg_replace( '/\/\*.*?\*\/(;)?/s', '', $text );
174-
}
175168
}

0 commit comments

Comments
 (0)