@@ -495,22 +495,25 @@ public function cli( $_, $assoc_args ) {
495495 * +---------+-----------------------+
496496 */
497497 public function query ( $ args , $ assoc_args ) {
498+
498499 $ command = sprintf ( '/usr/bin/env mysql%s --no-auto-rehash ' , $ this ->get_defaults_flag_string ( $ assoc_args ) );
499500 WP_CLI ::debug ( "Running shell command: {$ command }" , 'db ' );
501+
500502 $ assoc_args ['database ' ] = DB_NAME ;
501503
504+ // The query might come from STDIN.
502505 if ( ! empty ( $ args ) ) {
503506 $ assoc_args ['execute ' ] = $ args [0 ];
504507 }
505508
506509 if ( isset ( $ assoc_args ['execute ' ] ) ) {
510+ // Ensure that the SQL mode is compatible with WPDB.
507511 $ assoc_args ['execute ' ] = $ this ->get_sql_mode_query ( $ assoc_args ) . $ assoc_args ['execute ' ];
508512 }
509513
510- $ is_update_or_delete = isset ( $ assoc_args ['execute ' ] ) && preg_match ( '/\b(UPDATE|DELETE|INSERT)\b/i ' , $ assoc_args ['execute ' ] );
514+ $ is_row_modifying_query = isset ( $ assoc_args ['execute ' ] ) && preg_match ( '/\b(UPDATE|DELETE|INSERT|REPLACE|LOAD DATA )\b/i ' , $ assoc_args ['execute ' ] );
511515
512- if ( $ is_update_or_delete ) {
513- // Append `SELECT ROW_COUNT()` to the query.
516+ if ( $ is_row_modifying_query ) {
514517 $ assoc_args ['execute ' ] .= '; SELECT ROW_COUNT(); ' ;
515518 }
516519
@@ -521,8 +524,7 @@ public function query( $args, $assoc_args ) {
521524 WP_CLI ::error ( "Query failed: {$ stderr }" );
522525 }
523526
524- // For UPDATE/DELETE queries, parse the output to get the number of rows affected.
525- if ( $ is_update_or_delete ) {
527+ if ( $ is_row_modifying_query ) {
526528 $ output_lines = explode ( "\n" , trim ( $ stdout ) );
527529 $ affected_rows = (int ) trim ( end ( $ output_lines ) );
528530 WP_CLI ::success ( "Query succeeded. Rows affected: {$ affected_rows }" );
0 commit comments