@@ -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_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_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 PDO instance.
324329 *
0 commit comments