Skip to content

Commit 7ef82d4

Browse files
committed
Quote MySQL identifiers consistently
Centralize backtick escaping in Export and use it for queries and generated SQL. Cover table names containing embedded backticks.
1 parent c20c446 commit 7ef82d4

3 files changed

Lines changed: 52 additions & 16 deletions

File tree

‎features/bootstrap/SQLiteFeatureContext.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,22 @@ public function theSqliteDatabaseContainsATestTableWithAlphanumericStringHashVal
295295
'run'
296296
);
297297
}
298+
299+
/**
300+
* @Given /^the SQLite database contains a table with a backtick in its name$/
301+
*/
302+
public function theSqliteDatabaseContainsATableWithABacktickInItsName() {
303+
$this->create_file(
304+
'test_export_identifier_setup.sql',
305+
"
306+
DROP TABLE IF EXISTS `test``table`;
307+
CREATE TABLE `test``table` (id INTEGER PRIMARY KEY, value TEXT);
308+
INSERT INTO `test``table` (id, value) VALUES (1, 'Test value');
309+
"
310+
);
311+
$this->wpcli_tests_invoke_proc(
312+
$this->proc( 'wp sqlite import test_export_identifier_setup.sql' ),
313+
'run'
314+
);
315+
}
298316
}

‎features/sqlite-export.feature‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,19 @@ Feature: WP-CLI SQLite Export Command
134134
INSERT INTO `test_export_alphanumeric_string` VALUES (1,123e99);
135135
"""
136136

137+
@require-sqlite
138+
Scenario: Export should quote table names containing backticks
139+
Given the SQLite database contains a table with a backtick in its name
140+
When I run `wp sqlite --enable-ast-driver export test_export_identifier.sql`
141+
Then the file "test_export_identifier.sql" should contain:
142+
"""
143+
DROP TABLE IF EXISTS `test``table`;
144+
"""
145+
And the file "test_export_identifier.sql" should contain:
146+
"""
147+
INSERT INTO `test``table` VALUES (1,'Test value');
148+
"""
149+
137150
@require-sqlite
138151
Scenario: Export should preserve serialized settings containing a NUL byte
139152
Given the SQLite database contains a serialized settings option with a NUL byte separator

‎src/Export.php‎

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@ protected function get_table_names() {
138138
* @throws Exception
139139
*/
140140
protected function write_create_table_statement( $handle, $table_name ) {
141-
$comment = $this->get_dump_comment( sprintf( 'Table structure for table `%s`', $table_name ) );
141+
$quoted_table_name = $this->quote_identifier( $table_name );
142+
$comment = $this->get_dump_comment( sprintf( 'Table structure for table %s', $quoted_table_name ) );
142143
fwrite( $handle, $comment . PHP_EOL . PHP_EOL );
143-
fwrite( $handle, sprintf( 'DROP TABLE IF EXISTS `%s`;', $table_name ) . PHP_EOL );
144+
fwrite( $handle, sprintf( 'DROP TABLE IF EXISTS %s;', $quoted_table_name ) . PHP_EOL );
144145
fwrite( $handle, $this->get_create_statement( $table_name ) . PHP_EOL );
145146
}
146147

@@ -158,7 +159,7 @@ protected function write_insert_statements( $handle, $table_name ) {
158159
return;
159160
}
160161

161-
$comment = $this->get_dump_comment( sprintf( 'Dumping data for table `%s`', $table_name ) );
162+
$comment = $this->get_dump_comment( sprintf( 'Dumping data for table %s', $this->quote_identifier( $table_name ) ) );
162163
fwrite( $handle, $comment . PHP_EOL . PHP_EOL );
163164
foreach ( $this->get_insert_statements( $table_name ) as $insert_statement ) {
164165
fwrite( $handle, $insert_statement . PHP_EOL );
@@ -176,20 +177,14 @@ protected function write_insert_statements( $handle, $table_name ) {
176177
* @throws Exception
177178
*/
178179
protected function get_create_statement( $table_name ) {
180+
$create = $this->driver->query( 'SHOW CREATE TABLE ' . $this->quote_identifier( $table_name ) );
179181
if ( $this->driver instanceof PDO ) {
180-
$table_name = '`' . str_replace( '`', '``', $table_name ) . '`';
181-
$create = $this->driver->query( 'SHOW CREATE TABLE ' . $table_name );
182-
$sql = $create->fetchColumn( 1 );
182+
$sql = $create->fetchColumn( 1 );
183183
return rtrim( $sql, ';' ) . ";\n";
184184
}
185185

186-
$table_name = $this->driver instanceof WP_SQLite_Driver
187-
? $this->driver->get_connection()->quote_identifier( $table_name )
188-
: $table_name;
189-
190-
$create = $this->driver->query( 'SHOW CREATE TABLE ' . $table_name );
191-
$sql = $create[0]->{'Create Table'};
192-
$sql = rtrim( $sql, ';' ); // The old SQLite driver appends a semicolon.
186+
$sql = $create[0]->{'Create Table'};
187+
$sql = rtrim( $sql, ';' ); // The old SQLite driver appends a semicolon.
193188
return $sql . ";\n";
194189
}
195190

@@ -201,11 +196,11 @@ protected function get_create_statement( $table_name ) {
201196
* @return \Generator
202197
*/
203198
protected function get_insert_statements( $table_name ) {
204-
$quoted_table_name = '`' . str_replace( '`', '``', $table_name ) . '`';
199+
$quoted_table_name = $this->quote_identifier( $table_name );
205200
$stmt = $this->get_sqlite_pdo()->query( 'SELECT * FROM ' . $quoted_table_name );
206201
// phpcs:ignore
207202
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT ) ) {
208-
yield sprintf( 'INSERT INTO `%1s` VALUES (%2s);', $table_name, $this->escape_values( $row ) );
203+
yield sprintf( 'INSERT INTO %1s VALUES (%2s);', $quoted_table_name, $this->escape_values( $row ) );
209204
}
210205
}
211206

@@ -314,11 +309,21 @@ protected function get_dump_comment( $comment ) {
314309
* @return bool
315310
*/
316311
protected function table_has_records( $table_name ) {
317-
$table_name = '`' . str_replace( '`', '``', $table_name ) . '`';
312+
$table_name = $this->quote_identifier( $table_name );
318313
$stmt = $this->get_sqlite_pdo()->query( 'SELECT COUNT(*) FROM ' . $table_name );
319314
return $stmt->fetchColumn() > 0;
320315
}
321316

317+
/**
318+
* Quote a MySQL identifier.
319+
*
320+
* @param string $identifier Identifier to quote.
321+
* @return string
322+
*/
323+
protected function quote_identifier( $identifier ) {
324+
return '`' . str_replace( '`', '``', $identifier ) . '`';
325+
}
326+
322327
/**
323328
* Get the underlying SQLite PDO instance.
324329
*

0 commit comments

Comments
 (0)