Skip to content

Commit 0d1fb30

Browse files
Copilotswissspidy
andcommitted
Improve identifier escaping in SQLite export
Co-authored-by: swissspidy <[email protected]>
1 parent f7c1165 commit 0d1fb30

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/DB_Command_SQLite.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,23 +314,26 @@ protected function sqlite_export( $file, $assoc_args ) {
314314
$tables = $pdo->query( "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name" )->fetchAll( PDO::FETCH_COLUMN );
315315

316316
foreach ( $tables as $table ) {
317+
// Escape table name for identifiers.
318+
$escaped_table = '"' . str_replace( '"', '""', $table ) . '"';
319+
317320
// Get CREATE TABLE statement.
318321
$create_stmt = $pdo->query( "SELECT sql FROM sqlite_master WHERE type='table' AND name=" . $pdo->quote( $table ) )->fetchColumn();
319322

320323
if ( isset( $assoc_args['add-drop-table'] ) ) {
321-
fwrite( $output, "DROP TABLE IF EXISTS {$table};\n" );
324+
fwrite( $output, "DROP TABLE IF EXISTS {$escaped_table};\n" );
322325
}
323326

324327
fwrite( $output, $create_stmt . ";\n\n" );
325328

326329
// Export data.
327-
$rows = $pdo->query( "SELECT * FROM {$table}" )->fetchAll( PDO::FETCH_ASSOC );
330+
$rows = $pdo->query( "SELECT * FROM {$escaped_table}" )->fetchAll( PDO::FETCH_ASSOC );
328331

329332
foreach ( $rows as $row ) {
330333
$columns = array_keys( $row );
331334
$values = array_map( [ $pdo, 'quote' ], array_values( $row ) );
332335

333-
fwrite( $output, "INSERT INTO {$table} (" . implode( ', ', $columns ) . ') VALUES (' . implode( ', ', $values ) . ");\n" );
336+
fwrite( $output, "INSERT INTO {$escaped_table} (" . implode( ', ', $columns ) . ') VALUES (' . implode( ', ', $values ) . ");\n" );
334337
}
335338

336339
fwrite( $output, "\n" );

0 commit comments

Comments
 (0)