Skip to content

Commit c1a41cd

Browse files
committed
Fix comment handling - allow them to start anywhere, don't match them in strings
1 parent 6c22b98 commit c1a41cd

2 files changed

Lines changed: 50 additions & 16 deletions

File tree

‎features/sqlite-import.feature‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,30 @@ Feature: WP-CLI SQLite Import Command
8787
Test that a string containing
8888
a newline character and some whitespace works
8989
"""
90+
91+
@require-sqlite
92+
Scenario: Import a file with comments
93+
Given a SQL dump file named "test_import.sql" with content:
94+
"""
95+
CREATE TABLE test_table (id INTEGER PRIMARY KEY AUTO_INCREMENT, name TEXT);
96+
-- This is an inline comment.
97+
# This is an inline comment.
98+
INSERT INTO test_table (name) VALUES ('one'); -- This is an inline comment.
99+
/* This is a block comment */
100+
INSERT INTO test_table (name) VALUES ('two'); /* This
101+
is a block comment
102+
on multiple lines */ INSERT INTO test_table (name) VALUES ('three');
103+
INSERT INTO test_table (name) VALUES ('fo -- this looks like a comment ur');
104+
INSERT INTO test_table (name) VALUES ('fi/* this looks like a comment */ve');
105+
"""
106+
When I run `wp sqlite --enable-ast-driver import test_import.sql`
107+
Then STDOUT should contain:
108+
"""
109+
Success: Imported from 'test_import.sql'.
110+
"""
111+
And the SQLite database should contain a table named "test_table"
112+
And the "test_table" should contain a row with name "one"
113+
And the "test_table" should contain a row with name "two"
114+
And the "test_table" should contain a row with name "three"
115+
And the "test_table" should contain a row with name "fo -- this looks like a comment ur"
116+
And the "test_table" should contain a row with name "fi/* this looks like a comment */ve"

‎src/Import.php‎

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,6 @@ public function parse_statements( $sql_file_path ) {
8686
$line = mb_convert_encoding( $line, 'UTF-8', $detected_encoding );
8787
}
8888

89-
// Skip empty lines and comments
90-
if ( empty( $line ) || strpos( $line, '--' ) === 0 || strpos( $line, '#' ) === 0 ) {
91-
continue;
92-
}
93-
94-
// Handle multi-line comments
95-
if ( ! $in_comment && strpos( $line, '/*' ) === 0 ) {
96-
$in_comment = true;
97-
}
98-
if ( $in_comment ) {
99-
if ( strpos( $line, '*/' ) !== false ) {
100-
$in_comment = false;
101-
}
102-
continue;
103-
}
104-
10589
$strlen = strlen( $line );
10690
for ( $i = 0; $i < $strlen; $i++ ) {
10791
$ch = $line[ $i ];
@@ -120,6 +104,29 @@ public function parse_statements( $sql_file_path ) {
120104
continue;
121105
}
122106

107+
// Handle comments.
108+
if ( 0 === $single_quotes && 0 === $double_quotes ) {
109+
$prev_ch = isset( $line[ $i - 1 ] ) ? $line[ $i - 1 ] : null;
110+
$next_ch = isset( $line[ $i + 1 ] ) ? $line[ $i + 1 ] : null;
111+
112+
// Skip inline comments.
113+
if ( ( '-' === $ch && '-' === $next_ch ) || '#' === $ch ) {
114+
break; // Stop for the current line.
115+
}
116+
117+
// Skip multi-line comments.
118+
if ( ! $in_comment && '/' === $ch && '*' === $next_ch ) {
119+
$in_comment = true;
120+
continue;
121+
}
122+
if ( $in_comment ) {
123+
if ( '*' === $prev_ch && '/' === $ch ) {
124+
$in_comment = false;
125+
}
126+
continue;
127+
}
128+
}
129+
123130
// Handle quotes
124131
if ( "'" === $ch && 0 === $double_quotes ) {
125132
$single_quotes = 1 - $single_quotes;

0 commit comments

Comments
 (0)