Bug #36004 mysql_stmt_prepare resets the list of warnings
Submitted: 11 Apr 21:15 Modified: 25 Apr 19:21
Reporter: Davi Arnaut
Status: Closed
Category:Server: Errors Severity:S3 (Non-critical)
Version:6.0-BK OS:Any
Assigned to: Davi Arnaut Target Version:5.0+
Tags: show warnings, warning_count, mysql_stmt_prepare
Triage: D4 (Minor)

[11 Apr 21:15] Davi Arnaut
Description:
Although the manual says that "the list of messages is reset for each new statement that
uses a table", the list of messages is being reset for COM_STMT_PREPARE
(mysql_stmt_prepare) commands.

How to repeat:
static void test_bug_prepare()
{
  int rc, warning_count= 0;
  MYSQL_STMT *stmt;
  const char hello[]= "hello world!";

  DBUG_ENTER("test_bug28386");
  myheader("test_bug28386");

  rc= mysql_query(mysql, "drop table if exists inexistant");
  myquery(rc);

  DIE_UNLESS(mysql_warning_count(mysql) == 1);
  query_int_variable(mysql, "@@warning_count", &warning_count);
  DIE_UNLESS(warning_count);

  stmt= mysql_simple_prepare(mysql, "select 1");
  check_stmt(stmt);

  DIE_UNLESS(mysql_warning_count(mysql) == 0);
  query_int_variable(mysql, "@@warning_count", &warning_count);
  DIE_UNLESS(warning_count);

  rc= mysql_stmt_execute(stmt);
  check_execute(stmt, rc);

  DIE_UNLESS(mysql_warning_count(mysql) == 0);
  query_int_variable(mysql, "@@warning_count", &warning_count);
  DIE_UNLESS(warning_count);

  mysql_stmt_close(stmt);

  DBUG_VOID_RETURN;
}

Suggested fix:
Instead of remove all warnings, the stmt prepare command should only reset the list of
warnings if the statement being prepared uses a table. The fix should be something similar
to what we already have in mysql_execute_command:

  /*
    Reset warning count for each query that uses tables
    A better approach would be to reset this for any commands
    that is not a SHOW command or a select that only access local
    variables, but for now this is probably good enough.
    Don't reset warnings when executing a stored routine.
  */
  if ((all_tables || !lex->is_single_level_stmt()) && !thd->spcont)
    mysql_reset_errors(thd, 0);
[15 Apr 22:30] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/45455

ChangeSet@1.2642, 2008-04-15 17:29:42-03:00, davi@mysql.com +2 -0
  Bug#36004 mysql_stmt_prepare resets the list of warnings
  
  Although the manual says that "the list of messages is reset
  for each new statement that uses a table", the list of messages
  is being unconditionally reset for prepare commands.
  
  The solution is to enforce that the prepare command will only
  reset the message list if the statement being prepared uses
  a table or a warning is pushed.
[15 Apr 22:52] Davi Arnaut
Queued to 6.0-runtime
[20 Apr 15:01] Bugs System
Pushed into 6.0.6-alpha
[25 Apr 19:21] Paul DuBois
Noted in 6.0.6 changelog.

mysql_stmt_prepare() did not reset the list of messages (those
messages available via SHOW WARNINGS).