close
Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
DRY: Extract condition
  • Loading branch information
swissspidy committed Mar 6, 2025
commit 7d3ddb87c331054f4e21d62142f7062406ac2c29
9 changes: 5 additions & 4 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,10 @@ public function query( $args, $assoc_args ) {
if ( isset( $assoc_args['execute'] ) ) {
$assoc_args['execute'] = $this->get_sql_mode_query( $assoc_args ) . $assoc_args['execute'];
}

// Check if the query is an UPDATE or DELETE.
if ( isset( $assoc_args['execute'] ) && preg_match( '/\b(UPDATE|DELETE|INSERT)\b/i', $assoc_args['execute'] ) ) {

$is_update_or_delete = isset( $assoc_args['execute'] ) && preg_match( '/\b(UPDATE|DELETE|INSERT)\b/i', $assoc_args['execute'] );
Comment thread
mrsdizzie marked this conversation as resolved.
Outdated

if ( $is_update_or_delete ) {
// Append `SELECT ROW_COUNT()` to the query.
Comment thread
mrsdizzie marked this conversation as resolved.
Outdated
$assoc_args['execute'] .= '; SELECT ROW_COUNT();';
}
Expand All @@ -521,7 +522,7 @@ public function query( $args, $assoc_args ) {
}

// For UPDATE/DELETE queries, parse the output to get the number of rows affected.
Comment thread
mrsdizzie marked this conversation as resolved.
Outdated
if ( isset( $assoc_args['execute'] ) && preg_match( '/\b(UPDATE|DELETE|INSERT)\b/i', $assoc_args['execute'] ) ) {
if ( $is_update_or_delete ) {
$output_lines = explode( "\n", trim( $stdout ) );
$affected_rows = (int) trim( end( $output_lines ) );
WP_CLI::success( "Query succeeded. Rows affected: {$affected_rows}" );
Expand Down