diff options
-rw-r--r-- | include/mysql.h | 2 | ||||
-rw-r--r-- | libmysql/libmysql.c | 4 | ||||
-rw-r--r-- | tests/client_test.c | 80 |
3 files changed, 85 insertions, 1 deletions
diff --git a/include/mysql.h b/include/mysql.h index 0bb42c7f5eb..cd5d01d6f44 100644 --- a/include/mysql.h +++ b/include/mysql.h @@ -534,6 +534,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 */ diff --git a/libmysql/libmysql.c b/libmysql/libmysql.c index 5c5898dfd7d..859ec92f793 100644 --- a/libmysql/libmysql.c +++ b/libmysql/libmysql.c @@ -2010,6 +2010,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); } @@ -2106,7 +2107,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; } @@ -3173,6 +3174,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; diff --git a/tests/client_test.c b/tests/client_test.c index 681d4013eec..eda168d7230 100644 --- a/tests/client_test.c +++ b/tests/client_test.c @@ -8112,6 +8112,83 @@ static void test_parse_error_and_bad_length() fprintf(stdout, "Got error (as expected): '%s'\n", mysql_error(mysql)); } + +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"); +} + /* Test for bug#2248 "mysql_fetch without prior mysql_execute hangs" */ @@ -8417,6 +8494,9 @@ int main(int argc, char **argv) test_bug2248(); /* BUG#2248 */ test_parse_error_and_bad_length(); /* test if bad length param in mysql_prepare() triggers error */ + 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); |