diff --git a/sql/sql_cmd.h b/sql/sql_cmd.h index 6a43772975a..81bd66db1a6 100644 --- a/sql/sql_cmd.h +++ b/sql/sql_cmd.h @@ -20,6 +20,8 @@ #ifndef SQL_CMD_INCLUDED #define SQL_CMD_INCLUDED +#include + /* When a command is added here, be sure it's also added in mysqld.cc in "struct show_var_st status_vars[]= {" ... @@ -225,6 +227,11 @@ public: */ virtual bool is_dml() const { return false; } + virtual void get_dml_stat (ha_rows &found, ha_rows &changed) + { + found= changed= 0; + } + /** @brief Unprepare prepared statement for the command @param thd global context of the processed statement diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc index 0fb061eb553..1f9e413b994 100644 --- a/sql/sql_delete.cc +++ b/sql/sql_delete.cc @@ -328,7 +328,6 @@ bool Sql_cmd_delete::delete_from_single_table(THD *thd) SQL_SELECT *select= 0; SORT_INFO *file_sort= 0; READ_RECORD info; - ha_rows deleted= 0; bool reverse= FALSE; bool binlog_is_row; killed_state killed_status= NOT_KILLED; @@ -427,6 +426,7 @@ bool Sql_cmd_delete::delete_from_single_table(THD *thd) has_triggers= table->triggers && table->triggers->has_delete_triggers(); transactional_table= table->file->has_transactions_and_rollback(); + deleted= 0; if (!returning && !using_limit && const_cond_result && (!thd->is_current_stmt_binlog_format_row() && !has_triggers) @@ -975,7 +975,7 @@ cleanup: result->send_eof(); else my_ok(thd, deleted); - DBUG_PRINT("info",("%ld records deleted",(long) deleted)); + DBUG_PRINT("info", ("%ld records deleted", (long) deleted)); } delete file_sort; free_underlaid_joins(thd, select_lex); @@ -1861,6 +1861,9 @@ bool Sql_cmd_delete::execute_inner(THD *thd) if (result) { + /* In single table case, this->deleted set by delete_from_single_table */ + if (res && multitable) + deleted= ((multi_delete*)get_result())->num_deleted(); res= false; delete result; } diff --git a/sql/sql_delete.h b/sql/sql_delete.h index 1842c5c04c7..c3afbe8bc6c 100644 --- a/sql/sql_delete.h +++ b/sql/sql_delete.h @@ -43,6 +43,7 @@ template class SQL_I_List; class Sql_cmd_delete final : public Sql_cmd_dml { public: + ha_rows deleted{0}; Sql_cmd_delete(bool multitable_arg) : orig_multitable(multitable_arg), multitable(multitable_arg), save_protocol(NULL) @@ -66,6 +67,12 @@ public: void remove_order_by_without_limit(THD *thd); + void get_dml_stat (ha_rows &found, ha_rows &changed) override + { + found= 0; + changed= deleted; + } + protected: /** @brief Perform precheck of table privileges for delete statements diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 395fb277cc9..c16f37eb93f 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -34268,39 +34268,38 @@ static void MYSQL_DML_START(THD *thd) } -static void MYSQL_DML_DONE(THD *thd, int rc) +static void MYSQL_DML_GET_STAT(THD * thd, ha_rows &found, ha_rows &changed) { switch (thd->lex->sql_command) { - case SQLCOM_UPDATE: - MYSQL_UPDATE_DONE( - rc, - (rc ? 0 : - ((multi_update*)(((Sql_cmd_dml*)(thd->lex->m_sql_cmd))->get_result())) - ->num_found()), - (rc ? 0 : - ((multi_update*)(((Sql_cmd_dml*)(thd->lex->m_sql_cmd))->get_result())) - ->num_updated())); - break; case SQLCOM_UPDATE_MULTI: - MYSQL_MULTI_UPDATE_DONE( - rc, - (rc ? 0 : - ((multi_update*)(((Sql_cmd_dml*)(thd->lex->m_sql_cmd))->get_result())) - ->num_found()), - (rc ? 0 : - ((multi_update*)(((Sql_cmd_dml*)(thd->lex->m_sql_cmd))->get_result())) - ->num_updated())); + case SQLCOM_DELETE_MULTI: + thd->lex->m_sql_cmd->get_dml_stat(found, changed); break; case SQLCOM_DELETE: - MYSQL_DELETE_DONE(rc, (rc ? 0 : (ulong) (thd->get_row_count_func()))); + found= 0; + changed= (thd->get_row_count_func()); + break; + default: + DBUG_ASSERT(0); + } +} + + +static void MYSQL_DML_DONE(THD *thd, int rc, ha_rows found, ha_rows changed) +{ + switch (thd->lex->sql_command) { + case SQLCOM_UPDATE: + MYSQL_UPDATE_DONE(rc, found, changed); + break; + case SQLCOM_UPDATE_MULTI: + MYSQL_MULTI_UPDATE_DONE(rc, found, changed); + break; + case SQLCOM_DELETE: + MYSQL_DELETE_DONE(rc, changed); break; case SQLCOM_DELETE_MULTI: - MYSQL_MULTI_DELETE_DONE( - rc, - (rc ? 0 : - ((multi_delete*)(((Sql_cmd_dml*)(thd->lex->m_sql_cmd))->get_result())) - ->num_deleted())); + MYSQL_MULTI_DELETE_DONE(rc, changed); break; default: DBUG_ASSERT(0); @@ -34389,6 +34388,7 @@ err: bool Sql_cmd_dml::execute(THD *thd) { lex = thd->lex; + ha_rows found= 0, changed= 0; bool res; SELECT_LEX_UNIT *unit = &lex->unit; @@ -34439,6 +34439,8 @@ bool Sql_cmd_dml::execute(THD *thd) if (res) goto err; + else + MYSQL_DML_GET_STAT(thd, found, changed); res= unit->cleanup(); @@ -34447,13 +34449,13 @@ bool Sql_cmd_dml::execute(THD *thd) THD_STAGE_INFO(thd, stage_end); - MYSQL_DML_DONE(thd, res); + MYSQL_DML_DONE(thd, 0, found, changed); return res; err: DBUG_ASSERT(thd->is_error() || thd->killed); - MYSQL_DML_DONE(thd, 1); + MYSQL_DML_DONE(thd, 1, 0, 0); THD_STAGE_INFO(thd, stage_end); (void)unit->cleanup(); if (is_prepared()) diff --git a/sql/sql_update.cc b/sql/sql_update.cc index 2e1c7428854..8e3b4a80e7a 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -366,7 +366,7 @@ bool Sql_cmd_update::update_single_table(THD *thd) ha_rows dup_key_found; bool need_sort= TRUE; bool reverse= FALSE; - ha_rows updated, updated_or_same, found; + ha_rows updated_or_same; key_map old_covering_keys; TABLE *table; SQL_SELECT *select= NULL; @@ -3140,6 +3140,13 @@ bool Sql_cmd_update::execute_inner(THD *thd) if (result) { + /* In single table case, this->updated set by update_single_table */ + if (res && multitable) + { + found= ((multi_update*)get_result())->num_found(); + updated= ((multi_update*)get_result())->num_updated(); + } + res= false; delete result; } diff --git a/sql/sql_update.h b/sql/sql_update.h index d01ecb7354a..272c35a378a 100644 --- a/sql/sql_update.h +++ b/sql/sql_update.h @@ -45,6 +45,7 @@ bool compare_record(const TABLE *table); class Sql_cmd_update final : public Sql_cmd_dml { public: + ha_rows found{0}, updated{0}; Sql_cmd_update(bool multitable_arg) : orig_multitable(multitable_arg), multitable(multitable_arg) {} @@ -65,6 +66,13 @@ public: void set_as_multitable() { multitable= true; } + void get_dml_stat (ha_rows &found, ha_rows &changed) override + { + + found= this->found; + changed= this->updated; + } + protected: /** @brief Perform precheck of table privileges for update statements