Skip to content

Commit a84c954

Browse files
committed
Simplify quote matching state, add tests
1 parent c1a41cd commit a84c954

3 files changed

Lines changed: 33 additions & 12 deletions

File tree

‎features/bootstrap/SQLiteFeatureContext.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function theSqliteDatabaseShouldContainATableNamed( $table_name ) {
3838
*/
3939
public function theTableShouldContainARowWithName( $table_name, $name ) {
4040
$this->connectToDatabase();
41-
$result = $this->db->query( "SELECT * FROM $table_name WHERE name='$name'" );
41+
$result = $this->db->query( "SELECT * FROM $table_name WHERE name='" . $this->db->escapeString( $name ) . "'" );
4242
$row = $result->fetchArray();
4343
if ( ! $row ) {
4444
throw new Exception( "Row with name '$name' not found in table '$table_name'." );

‎features/sqlite-import.feature‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,26 @@ Feature: WP-CLI SQLite Import Command
114114
And the "test_table" should contain a row with name "three"
115115
And the "test_table" should contain a row with name "fo -- this looks like a comment ur"
116116
And the "test_table" should contain a row with name "fi/* this looks like a comment */ve"
117+
118+
@require-sqlite
119+
Scenario: Import a file quoted strings
120+
Given a SQL dump file named "test_import.sql" with content:
121+
"""
122+
CREATE TABLE test_table (id INTEGER PRIMARY KEY AUTO_INCREMENT, name TEXT);
123+
INSERT INTO test_table (name) VALUES ('a single-quoted string with \' '' some " tricky ` chars');
124+
INSERT INTO test_table (name) VALUES ("a double-quoted string with ' some \" "" tricky ` chars");
125+
"""
126+
When I run `wp sqlite --enable-ast-driver import test_import.sql`
127+
Then STDOUT should contain:
128+
"""
129+
Success: Imported from 'test_import.sql'.
130+
"""
131+
And the SQLite database should contain a table named "test_table"
132+
And the "test_table" should contain a row with name:
133+
"""
134+
a single-quoted string with ' ' some " tricky ` chars
135+
"""
136+
And the "test_table" should contain a row with name:
137+
"""
138+
a double-quoted string with ' some " " tricky ` chars
139+
"""

‎src/Import.php‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ public function parse_statements( $sql_file_path ) {
7373
WP_CLI::error( "Unable to open file: $sql_file_path" );
7474
}
7575

76-
$single_quotes = 0;
77-
$double_quotes = 0;
78-
$in_comment = false;
79-
$buffer = '';
76+
$starting_quote = null;
77+
$in_comment = false;
78+
$buffer = '';
8079

8180
// phpcs:ignore
8281
while ( ( $line = fgets( $handle ) ) !== false ) {
@@ -105,7 +104,7 @@ public function parse_statements( $sql_file_path ) {
105104
}
106105

107106
// Handle comments.
108-
if ( 0 === $single_quotes && 0 === $double_quotes ) {
107+
if ( null === $starting_quote ) {
109108
$prev_ch = isset( $line[ $i - 1 ] ) ? $line[ $i - 1 ] : null;
110109
$next_ch = isset( $line[ $i + 1 ] ) ? $line[ $i + 1 ] : null;
111110

@@ -128,15 +127,14 @@ public function parse_statements( $sql_file_path ) {
128127
}
129128

130129
// Handle quotes
131-
if ( "'" === $ch && 0 === $double_quotes ) {
132-
$single_quotes = 1 - $single_quotes;
133-
}
134-
if ( '"' === $ch && 0 === $single_quotes ) {
135-
$double_quotes = 1 - $double_quotes;
130+
if ( null === $starting_quote && ( "'" === $ch || '"' === $ch ) ) {
131+
$starting_quote = $ch;
132+
} elseif ( null !== $starting_quote && $ch === $starting_quote ) {
133+
$starting_quote = null;
136134
}
137135

138136
// Process statement end
139-
if ( ';' === $ch && 0 === $single_quotes && 0 === $double_quotes ) {
137+
if ( ';' === $ch && null === $starting_quote ) {
140138
yield trim( $buffer );
141139
$buffer = '';
142140
} else {

0 commit comments

Comments
 (0)