44use Exception ;
55use PDO ;
66use WP_CLI ;
7+ use WP_SQLite_Driver ;
78use WP_SQLite_Translator ;
89
910class Export {
11+ /**
12+ * The SQLite driver instance.
13+ *
14+ * @var WP_SQLite_Driver|WP_SQLite_Translator
15+ */
16+ protected $ driver ;
1017
11- protected $ translator ;
1218 protected $ args = array ();
1319 protected $ is_stdout = false ;
1420
1521 public function __construct () {
1622 SQLiteDatabaseIntegrationLoader ::load_plugin ();
17- $ this ->translator = new WP_SQLite_Translator ();
23+ $ this ->driver = SQLiteDriverFactory :: create_driver ();
1824 }
1925
2026 /**
@@ -79,19 +85,27 @@ protected function close_output_stream( $handle ) {
7985 protected function write_sql_statements ( $ handle ) {
8086 $ include_tables = $ this ->get_include_tables ();
8187 $ exclude_tables = $ this ->get_exclude_tables ();
82- foreach ( $ this ->translator ->query ( 'SHOW TABLES ' ) as $ table ) {
88+ foreach ( $ this ->driver ->query ( 'SHOW TABLES ' ) as $ row ) {
89+ $ table_name = array_values ( (array ) $ row )[0 ];
90+
8391 // Skip tables that are not in the include_tables list if the list is defined
84- if ( ! empty ( $ include_tables ) && ! in_array ( $ table ->name , $ include_tables , true ) ) {
92+ if ( ! empty ( $ include_tables ) && ! in_array ( $ table_name , $ include_tables , true ) ) {
93+ continue ;
94+ }
95+
96+ // Skip internal tables used by the new SQLite driver
97+ // (This is only needed when exporting with the legacy driver.)
98+ if ( 0 === strpos ( $ table_name , '_wp_sqlite_ ' ) ) {
8599 continue ;
86100 }
87101
88102 // Skip tables that are in the exclude_tables list
89- if ( in_array ( $ table -> name , $ exclude_tables , true ) ) {
103+ if ( in_array ( $ table_name , $ exclude_tables , true ) ) {
90104 continue ;
91105 }
92106
93- $ this ->write_create_table_statement ( $ handle , $ table -> name );
94- $ this ->write_insert_statements ( $ handle , $ table -> name );
107+ $ this ->write_create_table_statement ( $ handle , $ table_name );
108+ $ this ->write_insert_statements ( $ handle , $ table_name );
95109 }
96110
97111 fwrite ( $ handle , sprintf ( '-- Dump completed on %s ' , gmdate ( 'c ' ) ) );
@@ -144,8 +158,14 @@ protected function write_insert_statements( $handle, $table_name ) {
144158 * @throws Exception
145159 */
146160 protected function get_create_statement ( $ table_name ) {
147- $ create = $ this ->translator ->query ( 'SHOW CREATE TABLE ' . $ table_name );
148- return $ create [0 ]->{'Create Table ' } . "\n" ;
161+ $ table_name = $ this ->driver instanceof WP_SQLite_Driver
162+ ? $ this ->driver ->get_connection ()->quote_identifier ( $ table_name )
163+ : $ table_name ;
164+
165+ $ create = $ this ->driver ->query ( 'SHOW CREATE TABLE ' . $ table_name );
166+ $ sql = $ create [0 ]->{'Create Table ' };
167+ $ sql = rtrim ( $ sql , '; ' ); // The old SQLite driver appends a semicolon.
168+ return $ sql . "; \n" ;
149169 }
150170
151171 /**
@@ -156,7 +176,7 @@ protected function get_create_statement( $table_name ) {
156176 * @return \Generator
157177 */
158178 protected function get_insert_statements ( $ table_name ) {
159- $ pdo = $ this ->translator -> get_pdo ();
179+ $ pdo = $ this ->get_pdo ();
160180 $ stmt = $ pdo ->prepare ( 'SELECT * FROM ' . $ table_name );
161181 $ stmt ->execute ();
162182 // phpcs:ignore
@@ -175,6 +195,8 @@ protected function get_exclude_tables() {
175195 return array_merge (
176196 $ exclude_tables ,
177197 [
198+ // This list is only needed when exporting with the legacy driver.
199+ // In the new SQLite driver, SHOW TABLES never returns these tables.
178200 '_mysql_data_types_cache ' ,
179201 'sqlite_master ' ,
180202 'sqlite_sequence ' ,
@@ -235,7 +257,7 @@ protected function escape_values( PDO $pdo, $values ) {
235257 * @return string
236258 */
237259 protected function escape_string ( $ value ) {
238- $ pdo = $ this ->translator -> get_pdo ();
260+ $ pdo = $ this ->get_pdo ();
239261 return addcslashes ( $ pdo ->quote ( $ value ), "\\\n" );
240262 }
241263
@@ -261,9 +283,21 @@ protected function get_dump_comment( $comment ) {
261283 * @return bool
262284 */
263285 protected function table_has_records ( $ table_name ) {
264- $ pdo = $ this ->translator -> get_pdo ();
286+ $ pdo = $ this ->get_pdo ();
265287 $ stmt = $ pdo ->prepare ( 'SELECT COUNT(*) FROM ' . $ table_name );
266288 $ stmt ->execute ();
267289 return $ stmt ->fetchColumn () > 0 ;
268290 }
291+
292+ /**
293+ * Get the PDO instance.
294+ *
295+ * @return PDO
296+ */
297+ protected function get_pdo () {
298+ if ( $ this ->driver instanceof WP_SQLite_Translator ) {
299+ return $ this ->driver ->get_pdo ();
300+ }
301+ return $ this ->driver ->get_connection ()->get_pdo ();
302+ }
269303}
0 commit comments