Skip to content

Commit e372bed

Browse files
Feedback updates
1 parent cab475e commit e372bed

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

features/db.feature

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,16 +327,20 @@ Feature: Perform database operations
327327
latin1_spanish_ci
328328
"""
329329
330-
Scenario: Running an UPDATE query should return the number of affected rows
330+
Scenario: Row modifying queries should return the number of affected rows
331331
Given a WP install
332332
When I run `wp db query "UPDATE wp_users SET user_status = 1 WHERE ID = 1"`
333333
Then STDOUT should contain:
334334
"""
335335
Query succeeded. Rows affected: 1
336336
"""
337+
338+
When I run `wp db query "SELECT * FROM wp_users WHERE ID = 1"`
339+
Then STDOUT should not contain:
340+
"""
341+
Rows affected
342+
"""
337343
338-
Scenario: Running an DELETE query should return the number of affected rows
339-
Given a WP install
340344
When I run `wp db query "DELETE FROM wp_users WHERE ID = 1"`
341345
Then STDOUT should contain:
342346
"""

src/DB_Command.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)