@@ -204,6 +204,46 @@ protected function sqlite_reset() {
204204 * @param string $query SQL query to execute.
205205 */
206206 protected function sqlite_query ( $ query ) {
207+ global $ wpdb ;
208+
209+ // Use $wpdb if the SQLite drop-in is loaded.
210+ if ( isset ( $ wpdb ) && $ wpdb instanceof \WP_SQLite_DB ) {
211+ try {
212+ $ is_row_modifying_query = preg_match ( '/\b(UPDATE|DELETE|INSERT|REPLACE)\b/i ' , $ query );
213+
214+ if ( $ is_row_modifying_query ) {
215+ // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
216+ $ affected_rows = $ wpdb ->query ( $ query );
217+ if ( false === $ affected_rows ) {
218+ // phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags
219+ WP_CLI ::error ( 'Query failed: ' . strip_tags ( $ wpdb ->last_error ) );
220+ }
221+ WP_CLI ::success ( "Query succeeded. Rows affected: {$ affected_rows }" );
222+ } else {
223+ // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
224+ $ results = $ wpdb ->get_results ( $ query , ARRAY_A );
225+
226+ if ( $ wpdb ->last_error ) {
227+ // phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags
228+ WP_CLI ::error ( 'Query failed: ' . strip_tags ( $ wpdb ->last_error ) );
229+ }
230+
231+ if ( empty ( $ results ) ) {
232+ // No results to display.
233+ return ;
234+ }
235+
236+ // Display as a table similar to MySQL output.
237+ $ headers = array_keys ( $ results [0 ] );
238+ $ this ->display_table ( $ headers , $ results );
239+ }
240+ } catch ( Exception $ e ) {
241+ WP_CLI ::error ( 'Query failed: ' . $ e ->getMessage () );
242+ }
243+ return ;
244+ }
245+
246+ // Fallback to PDO if the drop-in is not loaded.
207247 $ pdo = $ this ->get_sqlite_pdo ();
208248
209249 if ( ! $ pdo ) {
@@ -499,14 +539,24 @@ protected function maybe_load_sqlite_dropin() {
499539 return ;
500540 }
501541
542+ // Constants used in wp-includes/functions.php
543+ if ( ! defined ( 'WPINC ' ) ) {
544+ // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound
545+ define ( 'WPINC ' , 'wp-includes ' );
546+ }
547+
548+ if ( ! defined ( 'WP_CONTENT_DIR ' ) ) {
549+ define ( 'WP_CONTENT_DIR ' , ABSPATH . 'wp-content ' );
550+ }
551+
502552 // Load required WordPress files if not already loaded.
503553 if ( ! function_exists ( 'add_action ' ) ) {
504- $ wpinc = defined ( 'WPINC ' ) ? WPINC : 'wp-includes ' ;
505-
506554 $ required_files = [
507- ABSPATH . $ wpinc . '/compat.php ' ,
508- ABSPATH . $ wpinc . '/plugin.php ' ,
509- ABSPATH . $ wpinc . '/class-wpdb.php ' ,
555+ ABSPATH . WPINC . '/compat.php ' ,
556+ ABSPATH . WPINC . '/plugin.php ' ,
557+ // Defines `wp_debug_backtrace_summary()` as used by wpdb.
558+ ABSPATH . WPINC . '/functions.php ' ,
559+ ABSPATH . WPINC . '/class-wpdb.php ' ,
510560 ];
511561
512562 foreach ( $ required_files as $ required_file ) {
0 commit comments