@@ -495,22 +495,25 @@ public function cli( $_, $assoc_args ) {
495
495
* +---------+-----------------------+
496
496
*/
497
497
public function query ( $ args , $ assoc_args ) {
498
+
498
499
$ command = sprintf ( '/usr/bin/env mysql%s --no-auto-rehash ' , $ this ->get_defaults_flag_string ( $ assoc_args ) );
499
500
WP_CLI ::debug ( "Running shell command: {$ command }" , 'db ' );
501
+
500
502
$ assoc_args ['database ' ] = DB_NAME ;
501
503
504
+ // The query might come from STDIN.
502
505
if ( ! empty ( $ args ) ) {
503
506
$ assoc_args ['execute ' ] = $ args [0 ];
504
507
}
505
508
506
509
if ( isset ( $ assoc_args ['execute ' ] ) ) {
510
+ // Ensure that the SQL mode is compatible with WPDB.
507
511
$ assoc_args ['execute ' ] = $ this ->get_sql_mode_query ( $ assoc_args ) . $ assoc_args ['execute ' ];
508
512
}
509
513
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 ' ] );
511
515
512
- if ( $ is_update_or_delete ) {
513
- // Append `SELECT ROW_COUNT()` to the query.
516
+ if ( $ is_row_modifying_query ) {
514
517
$ assoc_args ['execute ' ] .= '; SELECT ROW_COUNT(); ' ;
515
518
}
516
519
@@ -521,8 +524,7 @@ public function query( $args, $assoc_args ) {
521
524
WP_CLI ::error ( "Query failed: {$ stderr }" );
522
525
}
523
526
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 ) {
526
528
$ output_lines = explode ( "\n" , trim ( $ stdout ) );
527
529
$ affected_rows = (int ) trim ( end ( $ output_lines ) );
528
530
WP_CLI ::success ( "Query succeeded. Rows affected: {$ affected_rows }" );
0 commit comments