1
0
mirror of https://github.com/mariadb-corporation/mariadb-connector-c.git synced 2025-08-08 14:02:17 +03:00

Fix for prepared statment multi results:

Reallocate buffers (fields and binds) for new resultsets
This commit is contained in:
Georg Richter
2013-05-20 10:50:58 +02:00
parent a1c12629e4
commit f8e6248cc3
2 changed files with 143 additions and 7 deletions

View File

@@ -1295,6 +1295,55 @@ int STDCALL mysql_stmt_store_result(MYSQL_STMT *stmt)
DBUG_RETURN(0); DBUG_RETURN(0);
} }
static int madb_alloc_stmt_fields(MYSQL_STMT *stmt)
{
int i;
DBUG_ENTER("madb_alloc_stmt_fields");
if (stmt->mysql->field_count)
{
if (!(stmt->fields= (MYSQL_FIELD *)alloc_root(&stmt->mem_root,
sizeof(MYSQL_FIELD) * stmt->mysql->field_count)))
{
SET_CLIENT_STMT_ERROR(stmt, CR_OUT_OF_MEMORY, SQLSTATE_UNKNOWN, 0);
DBUG_RETURN(1);
}
stmt->field_count= stmt->mysql->field_count;
for (i=0; i < stmt->field_count; i++)
{
if (stmt->mysql->fields[i].db)
stmt->fields[i].db= strdup_root(&stmt->mem_root, stmt->mysql->fields[i].db);
if (stmt->mysql->fields[i].table)
stmt->fields[i].table= strdup_root(&stmt->mem_root, stmt->mysql->fields[i].table);
if (stmt->mysql->fields[i].org_table)
stmt->fields[i].org_table= strdup_root(&stmt->mem_root, stmt->mysql->fields[i].org_table);
if (stmt->mysql->fields[i].name)
stmt->fields[i].name= strdup_root(&stmt->mem_root, stmt->mysql->fields[i].name);
if (stmt->mysql->fields[i].org_name)
stmt->fields[i].org_name= strdup_root(&stmt->mem_root, stmt->mysql->fields[i].org_name);
if (stmt->mysql->fields[i].catalog)
stmt->fields[i].catalog= strdup_root(&stmt->mem_root, stmt->mysql->fields[i].catalog);
stmt->fields[i].def= stmt->mysql->fields[i].def ? strdup_root(&stmt->mem_root, stmt->mysql->fields[i].def) : NULL;
stmt->fields[i].type= stmt->mysql->fields[i].type;
stmt->fields[i].length= stmt->mysql->fields[i].length;
stmt->fields[i].flags= stmt->mysql->fields[i].flags;
stmt->fields[i].decimals= stmt->mysql->fields[i].decimals;
stmt->fields[i].charsetnr= stmt->mysql->fields[i].charsetnr;
}
if (!(stmt->bind= (MYSQL_BIND *)alloc_root(&stmt->mem_root, stmt->field_count * sizeof(MYSQL_BIND))))
{
SET_CLIENT_STMT_ERROR(stmt, CR_OUT_OF_MEMORY, SQLSTATE_UNKNOWN, 0);
DBUG_RETURN(1);
}
bzero(stmt->bind, stmt->field_count * sizeof(MYSQL_BIND));
stmt->bind_result_done= 0;
}
DBUG_RETURN(0);
}
int STDCALL mysql_stmt_execute(MYSQL_STMT *stmt) int STDCALL mysql_stmt_execute(MYSQL_STMT *stmt)
{ {
char *request; char *request;
@@ -1621,6 +1670,7 @@ MYSQL_RES* STDCALL mysql_stmt_param_metadata(MYSQL_STMT *stmt)
int STDCALL mysql_stmt_next_result(MYSQL_STMT *stmt) int STDCALL mysql_stmt_next_result(MYSQL_STMT *stmt)
{ {
int rc;
DBUG_ENTER("mysql_stmt_next_result"); DBUG_ENTER("mysql_stmt_next_result");
if (!stmt->mysql) if (!stmt->mysql)
@@ -1649,12 +1699,10 @@ int STDCALL mysql_stmt_next_result(MYSQL_STMT *stmt)
DBUG_RETURN(1); DBUG_RETURN(1);
} }
if (stmt->field_count != stmt->mysql->field_count) if (stmt->mysql->field_count)
{ rc= madb_alloc_stmt_fields(stmt);
if (stmt->bind)
stmt->bind= NULL;
stmt->field_count= stmt->mysql->field_count;
}
DBUG_RETURN(0); stmt->field_count= stmt->mysql->field_count;
DBUG_RETURN(rc);
} }

View File

@@ -129,7 +129,95 @@ static int test_multi_result(MYSQL *mysql)
check_stmt_rc(rc, stmt); check_stmt_rc(rc, stmt);
} }
int test_sp_params(MYSQL *mysql)
{
int i, rc;
MYSQL_STMT *stmt;
rc= mysql_query(mysql, "DROP PROCEDURE IF EXISTS p1");
check_mysql_rc(rc, mysql);
int a[] = {10,20,30};
MYSQL_BIND bind[3];
char *stmtstr= "CALL P1(?,?,?)";
char res[3][20];
rc= mysql_query(mysql, "CREATE PROCEDURE p1(OUT p_out VARCHAR(19), IN p_in INT, INOUT p_inout INT)"
"BEGIN "
" SET p_in = 300, p_out := 'This is OUT param', p_inout = 200; "
" SELECT p_inout, p_in, substring(p_out, 9);"
"END");
check_mysql_rc(rc, mysql);
stmt= mysql_stmt_init(mysql);
check_mysql_rc(rc, mysql);
rc= mysql_stmt_prepare(stmt, stmtstr, strlen(stmtstr));
check_stmt_rc(rc, stmt);
FAIL_IF(mysql_stmt_param_count(stmt) != 3, "expected param_count=3");
memset(bind, 0, sizeof(MYSQL_BIND) * 3);
for (i=0; i < 3; i++)
{
bind[i].buffer= &a[i];
bind[i].buffer_type= MYSQL_TYPE_LONG;
}
bind[0].buffer_type= MYSQL_TYPE_NULL;
rc= mysql_stmt_bind_param(stmt, bind);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
memset(res, 0, 60);
memset(bind, 0, sizeof(MYSQL_BIND) * 3);
for (i=0; i < 3; i++)
{
bind[i].buffer_type= MYSQL_TYPE_STRING;
bind[i].buffer_length= 20;
bind[i].buffer= res[i];
}
do {
if (mysql->server_status & SERVER_PS_OUT_PARAMS)
{
diag("out param result set");
FAIL_IF(mysql_stmt_field_count(stmt) != 2, "expected 2 columns");
FAIL_IF(strcmp(stmt->fields[0].org_name, "p_out") != 0, "wrong field name");
FAIL_IF(strcmp(stmt->fields[1].org_name, "p_inout") != 0, "wrong field name");
rc= mysql_stmt_bind_result(stmt, bind);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_fetch(stmt);
check_stmt_rc(rc, stmt);
FAIL_IF(strcmp(res[0],"This is OUT param") != 0, "comparison failed");
FAIL_IF(strcmp(res[1],"200") != 0, "comparison failed");
}
else
if (mysql_stmt_field_count(stmt))
{
diag("sp result set");
FAIL_IF(mysql_stmt_field_count(stmt) != 3, "expected 3 columns");
rc= mysql_stmt_bind_result(stmt, bind);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_fetch(stmt);
check_stmt_rc(rc, stmt);
FAIL_IF(strcmp(res[0],"200") != 0, "comparison failed");
FAIL_IF(strcmp(res[1],"300") != 0, "comparison failed");
FAIL_IF(strcmp(res[2],"OUT param") != 0, "comparison failed");
}
} while (mysql_stmt_next_result(stmt) == 0);
rc= mysql_stmt_close(stmt);
return OK;
}
struct my_tests_st my_tests[] = { struct my_tests_st my_tests[] = {
{"test_sp_params", test_sp_params, TEST_CONNECTION_NEW, CLIENT_MULTI_STATEMENTS, NULL , NULL},
{"test_multi_result", test_multi_result, TEST_CONNECTION_NEW, CLIENT_MULTI_STATEMENTS, NULL , NULL}, {"test_multi_result", test_multi_result, TEST_CONNECTION_NEW, CLIENT_MULTI_STATEMENTS, NULL , NULL},
{NULL, NULL, 0, 0, NULL, NULL} {NULL, NULL, 0, 0, NULL, NULL}
}; };