Commit 3aef1d2c authored by unknown's avatar unknown

Fix for bug #2247: "mysql_stmt_affected_rows returns affected rows from

last command"


include/mysql.h:
  Fix for bug #2247: 
  added affected_rows variable to MYSQL_STMT
libmysql/libmysql.c:
  Fix for bug #2247: 
  save mysql->affected_rows in stmt->affected_rows
  after mysql_execute() and mysql_stmt_store_result().
tests/client_test.c:
  Fix for bug #2247: test added
parent 453a32e9
......@@ -537,6 +537,8 @@ typedef struct st_mysql_stmt
char *query; /* query buffer */
MEM_ROOT mem_root; /* root allocations */
my_ulonglong last_fetched_column; /* last fetched column */
my_ulonglong affected_rows; /* copy of mysql->affected_rows
after statement execution */
unsigned long stmt_id; /* Id for prepared statement */
unsigned int last_errno; /* error code */
unsigned int param_count; /* parameters count */
......
......@@ -2018,6 +2018,7 @@ static my_bool execute(MYSQL_STMT * stmt, char *packet, ulong length)
set_stmt_errmsg(stmt, net->last_error, net->last_errno, net->sqlstate);
DBUG_RETURN(1);
}
stmt->affected_rows= mysql->affected_rows;
DBUG_RETURN(0);
}
......@@ -2127,7 +2128,7 @@ ulong STDCALL mysql_param_count(MYSQL_STMT * stmt)
my_ulonglong STDCALL mysql_stmt_affected_rows(MYSQL_STMT *stmt)
{
return stmt->mysql->last_used_con->affected_rows;
return stmt->affected_rows;
}
......@@ -3223,6 +3224,7 @@ int STDCALL mysql_stmt_store_result(MYSQL_STMT *stmt)
DBUG_RETURN(0);
}
mysql->affected_rows= result->row_count= result->data->rows;
stmt->affected_rows= result->row_count;
result->data_cursor= result->data->data;
result->fields= stmt->fields;
result->field_count= stmt->field_count;
......
......@@ -8095,6 +8095,82 @@ static void test_bug1946()
rc= mysql_query(mysql,"DROP TABLE prepare_command");
}
static void test_bug2247()
{
MYSQL_STMT *stmt;
MYSQL_RES *res;
int rc;
int i;
const char *create= "CREATE TABLE bug2247(id INT UNIQUE AUTO_INCREMENT)";
const char *insert= "INSERT INTO bug2247 VALUES (NULL)";
const char *select= "SELECT id FROM bug2247";
const char *update= "UPDATE bug2247 SET id=id+10";
const char *drop= "DROP TABLE IF EXISTS bug2247";
ulonglong exp_count;
enum { NUM_ROWS= 5 };
myheader("test_bug2247");
fprintf(stdout, "\nChecking if stmt_affected_rows is not affected by\n"
"mysql_query ... ");
/* create table and insert few rows */
rc = mysql_query(mysql, drop);
myquery(rc);
rc= mysql_query(mysql, create);
myquery(rc);
stmt= mysql_prepare(mysql, insert, strlen(insert));
mystmt_init(stmt);
for (i= 0; i < NUM_ROWS; ++i)
{
rc= mysql_execute(stmt);
mystmt(stmt, rc);
}
exp_count= mysql_stmt_affected_rows(stmt);
assert(exp_count == 1);
rc= mysql_query(mysql, select);
myquery(rc);
/*
mysql_store_result overwrites mysql->affected_rows. Check that
mysql_stmt_affected_rows() returns the same value, whereas
mysql_affected_rows() value is correct.
*/
res= mysql_store_result(mysql);
mytest(res);
assert(mysql_affected_rows(mysql) == NUM_ROWS);
assert(exp_count == mysql_stmt_affected_rows(stmt));
rc= mysql_query(mysql, update);
myquery(rc);
assert(mysql_affected_rows(mysql) == NUM_ROWS);
assert(exp_count == mysql_stmt_affected_rows(stmt));
mysql_free_result(res);
mysql_stmt_close(stmt);
/* check that mysql_stmt_store_result modifies mysql_stmt_affected_rows */
stmt= mysql_prepare(mysql, select, strlen(select));
mystmt_init(stmt);
rc= mysql_execute(stmt);
mystmt(stmt, rc);
rc= mysql_stmt_store_result(stmt);
mystmt(stmt, rc);
exp_count= mysql_stmt_affected_rows(stmt);
assert(exp_count == NUM_ROWS);
rc= mysql_query(mysql, insert);
myquery(rc);
assert(mysql_affected_rows(mysql) == 1);
assert(mysql_stmt_affected_rows(stmt) == exp_count);
mysql_stmt_close(stmt);
fprintf(stdout, "OK");
}
/*
Read and parse arguments and MySQL options from my.cnf
......@@ -8340,6 +8416,10 @@ int main(int argc, char **argv)
test_bug1644(); /* BUG#1644 */
test_bug1946(); /* test that placeholders are allowed only in
prepared queries */
test_bug2247(); /* test that mysql_stmt_affected_rows() returns
number of rows affected by last prepared
statement execution
*/
end_time= time((time_t *)0);
total_time+= difftime(end_time, start_time);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment