@@ -507,104 +507,25 @@ public function query( $args, $assoc_args ) {
507507 $ assoc_args ['execute ' ] = $ this ->get_sql_mode_query ( $ assoc_args ) . $ assoc_args ['execute ' ];
508508 }
509509
510- // Get the original query for tracking.
511- $ original_query = isset ( $ assoc_args ['execute ' ] ) ? $ assoc_args ['execute ' ] : '' ;
512-
513- // Check if this is a test environment.
514- $ is_test_environment = $ this ->is_in_test_environment ();
515-
516- // Only add ROW_COUNT() query for real-world UPDATE/DELETE operations.
517- $ is_update_delete = isset ( $ assoc_args ['execute ' ] ) && preg_match ( '/\b(UPDATE|DELETE)\b/i ' , $ assoc_args ['execute ' ] );
518- $ show_affected_rows = $ is_update_delete && ! $ is_test_environment && ! isset ( $ assoc_args ['skip-affected-rows ' ] );
519-
520- // Modify the query only when we want to show affected rows.
521- if ( $ show_affected_rows ) {
522- $ assoc_args ['execute ' ] .= '; SELECT ROW_COUNT() AS affected_rows; ' ;
510+ // Check if the query is an UPDATE or DELETE.
511+ if ( isset ( $ assoc_args ['execute ' ] ) && preg_match ( '/\b(UPDATE|DELETE|INSERT)\b/i ' , $ assoc_args ['execute ' ] ) ) {
512+ // Append `SELECT ROW_COUNT()` to the query.
513+ $ assoc_args ['execute ' ] .= '; SELECT ROW_COUNT(); ' ;
523514 }
524515
525516 WP_CLI ::debug ( 'Associative arguments: ' . json_encode ( $ assoc_args ), 'db ' );
526- list ($ stdout , $ stderr , $ exit_code ) = self ::run ( $ command , $ assoc_args , false );
517+ list ( $ stdout , $ stderr , $ exit_code ) = self ::run ( $ command , $ assoc_args , false );
527518
528519 if ( $ exit_code ) {
529520 WP_CLI ::error ( "Query failed: {$ stderr }" );
530521 }
531522
532- // For test environments, keep the output exactly as expected by tests .
533- if ( $ is_test_environment ) {
534- WP_CLI :: log ( $ stdout );
535- WP_CLI :: success ( ' Query executed successfully. ' );
536- return ;
523+ // For UPDATE/DELETE queries, parse the output to get the number of rows affected .
524+ if ( isset ( $ assoc_args [ ' execute ' ] ) && preg_match ( ' /\b(UPDATE|DELETE|INSERT)\b/i ' , $ assoc_args [ ' execute ' ] ) ) {
525+ $ output_lines = explode ( "\n" , trim ( $ stdout ) );
526+ $ affected_rows = ( int ) trim ( end ( $ output_lines ) );
527+ WP_CLI :: success ( " Query succeeded. Rows affected: { $ affected_rows }" ) ;
537528 }
538-
539- // Process the output differently depending on whether we're showing affected rows.
540- if ( $ show_affected_rows ) {
541- // Extract the affected rows count from the output.
542- $ output_lines = explode ( "\n" , $ stdout );
543-
544- // Find the line with "affected_rows" if it exists.
545- $ affected_rows = 0 ;
546- $ row_count_header_index = -1 ;
547-
548- for ( $ i = 0 ; $ i < count ( $ output_lines ); $ i ++ ) {
549- if ( strpos ( $ output_lines [ $ i ], 'affected_rows ' ) !== false ) {
550- $ row_count_header_index = $ i ;
551- // The value should be in the next line.
552- if ( isset ( $ output_lines [ $ i + 1 ] ) ) {
553- $ affected_rows = (int ) trim ( $ output_lines [ $ i + 1 ] );
554- }
555- break ;
556- }
557- }
558-
559- // Remove the affected_rows part from output.
560- if ( $ row_count_header_index >= 0 ) {
561- // Remove the header and the value line.
562- array_splice ( $ output_lines , $ row_count_header_index , 2 );
563- $ stdout = implode ( "\n" , $ output_lines );
564- }
565-
566- // Show the output with affected rows info.
567- WP_CLI ::log ( $ stdout );
568- WP_CLI ::success ( "Query executed successfully. Rows affected: {$ affected_rows }" );
569- } else {
570- // Standard output for non-UPDATE/DELETE queries.
571- WP_CLI ::log ( $ stdout );
572- WP_CLI ::success ( 'Query executed successfully. ' );
573- }
574- }
575-
576- /**
577- * Determines if we're in a test environment
578- *
579- * @return bool Whether we're in a test environment
580- */
581- private function is_in_test_environment () {
582- // Check if we're running in a Behat test environment.
583-
584- // Option 1: Look for environment variables that might indicate testing.
585- if ( getenv ( 'WP_CLI_TEST_MODE ' ) ) {
586- return true ;
587- }
588-
589- // Option 2: Check if we're in a test directory path.
590- $ cwd = getcwd ();
591- if ( strpos ( $ cwd , '/tmp/wp-cli-test-run- ' ) !== false ) {
592- return true ;
593- }
594-
595- // Option 3: Look for test files in backtrace.
596- $ backtrace = debug_backtrace ( DEBUG_BACKTRACE_IGNORE_ARGS , 10 );
597- foreach ( $ backtrace as $ frame ) {
598- if ( isset ( $ frame ['file ' ] ) && (
599- strpos ( $ frame ['file ' ], 'features/ ' ) !== false ||
600- strpos ( $ frame ['file ' ], 'behat ' ) !== false ||
601- strpos ( $ frame ['file ' ], 'test ' ) !== false
602- ) ) {
603- return true ;
604- }
605- }
606-
607- return false ;
608529 }
609530
610531 /**
0 commit comments