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,21 @@ 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 ) ) {
8593 continue ;
8694 }
8795
8896 // Skip tables that are in the exclude_tables list
89- if ( in_array ( $ table -> name , $ exclude_tables , true ) ) {
97+ if ( in_array ( $ table_name , $ exclude_tables , true ) ) {
9098 continue ;
9199 }
92100
93- $ this ->write_create_table_statement ( $ handle , $ table -> name );
94- $ this ->write_insert_statements ( $ handle , $ table -> name );
101+ $ this ->write_create_table_statement ( $ handle , $ table_name );
102+ $ this ->write_insert_statements ( $ handle , $ table_name );
95103 }
96104
97105 fwrite ( $ handle , sprintf ( '-- Dump completed on %s ' , gmdate ( 'c ' ) ) );
@@ -144,8 +152,12 @@ protected function write_insert_statements( $handle, $table_name ) {
144152 * @throws Exception
145153 */
146154 protected function get_create_statement ( $ table_name ) {
147- $ create = $ this ->translator ->query ( 'SHOW CREATE TABLE ' . $ table_name );
148- return $ create [0 ]->{'Create Table ' } . "\n" ;
155+ $ table_name = $ this ->driver instanceof WP_SQLite_Driver
156+ ? $ this ->driver ->get_connection ()->quote_identifier ( $ table_name )
157+ : $ table_name ;
158+
159+ $ create = $ this ->driver ->query ( 'SHOW CREATE TABLE ' . $ table_name );
160+ return $ create [0 ]->{'Create Table ' } . "; \n" ;
149161 }
150162
151163 /**
@@ -156,7 +168,7 @@ protected function get_create_statement( $table_name ) {
156168 * @return \Generator
157169 */
158170 protected function get_insert_statements ( $ table_name ) {
159- $ pdo = $ this ->translator -> get_pdo ();
171+ $ pdo = $ this ->get_pdo ();
160172 $ stmt = $ pdo ->prepare ( 'SELECT * FROM ' . $ table_name );
161173 $ stmt ->execute ();
162174 // phpcs:ignore
@@ -235,7 +247,7 @@ protected function escape_values( PDO $pdo, $values ) {
235247 * @return string
236248 */
237249 protected function escape_string ( $ value ) {
238- $ pdo = $ this ->translator -> get_pdo ();
250+ $ pdo = $ this ->get_pdo ();
239251 return addcslashes ( $ pdo ->quote ( $ value ), "\\\n" );
240252 }
241253
@@ -261,9 +273,21 @@ protected function get_dump_comment( $comment ) {
261273 * @return bool
262274 */
263275 protected function table_has_records ( $ table_name ) {
264- $ pdo = $ this ->translator -> get_pdo ();
276+ $ pdo = $ this ->get_pdo ();
265277 $ stmt = $ pdo ->prepare ( 'SELECT COUNT(*) FROM ' . $ table_name );
266278 $ stmt ->execute ();
267279 return $ stmt ->fetchColumn () > 0 ;
268280 }
281+
282+ /**
283+ * Get the PDO instance.
284+ *
285+ * @return PDO
286+ */
287+ protected function get_pdo () {
288+ if ( $ this ->driver instanceof WP_SQLite_Translator ) {
289+ return $ this ->driver ->get_pdo ();
290+ }
291+ return $ this ->driver ->get_connection ()->get_pdo ();
292+ }
269293}
0 commit comments