Commit 420c956d authored by konstantin@mysql.com's avatar konstantin@mysql.com

Intermediate commit of client library (cleanups + fixes of 3 items from

flaws list)
TODO: 
 * verify that no sequence of API calls produces SIGSEGV.
 That is, verify that mysql_stmt_init  -> mysql_stmt_fetch is OK,
 or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
 meaningful error.
 * remove alloc_stmt_fields call
 * revise stmt->state codes and statement states.
 * there are other items in prepared statements 'to fix' document.

Done:
 - cleanups and comments
 - revision of prepared statement error codes.
 - mysql_stmt_prepare is now can always be called (that is, you can reprepare
   a statement)
 - new implementation of mysql_stmt_close and fetch cancellation
parent e90c78ce
...@@ -67,7 +67,7 @@ extern const char *client_errors[]; /* Error messages */ ...@@ -67,7 +67,7 @@ extern const char *client_errors[]; /* Error messages */
/* new 4.1 error codes */ /* new 4.1 error codes */
#define CR_NULL_POINTER 2028 #define CR_NULL_POINTER 2028
#define CR_NO_PREPARE_STMT 2029 #define CR_NO_PREPARE_STMT 2029
#define CR_NOT_ALL_PARAMS_BOUND 2030 #define CR_PARAMS_NOT_BOUND 2030
#define CR_DATA_TRUNCATED 2031 #define CR_DATA_TRUNCATED 2031
#define CR_NO_PARAMETERS_EXISTS 2032 #define CR_NO_PARAMETERS_EXISTS 2032
#define CR_INVALID_PARAMETER_NO 2033 #define CR_INVALID_PARAMETER_NO 2033
...@@ -87,3 +87,5 @@ extern const char *client_errors[]; /* Error messages */ ...@@ -87,3 +87,5 @@ extern const char *client_errors[]; /* Error messages */
#define CR_CONN_UNKNOW_PROTOCOL 2046 #define CR_CONN_UNKNOW_PROTOCOL 2046
#define CR_INVALID_CONN_HANDLE 2047 #define CR_INVALID_CONN_HANDLE 2047
#define CR_SECURE_AUTH 2048 #define CR_SECURE_AUTH 2048
#define CR_FETCH_CANCELLED 2049
#define CR_NO_DATA 2050
...@@ -256,6 +256,11 @@ typedef struct st_mysql ...@@ -256,6 +256,11 @@ typedef struct st_mysql
LIST *stmts; /* list of all statements */ LIST *stmts; /* list of all statements */
const struct st_mysql_methods *methods; const struct st_mysql_methods *methods;
void *thd; void *thd;
/*
Points to boolean flag in MYSQL_RES or MYSQL_STMT. We set this flag
from mysql_stmt_close if close had to cancel result set of this object.
*/
my_bool *unbuffered_fetch_owner;
} MYSQL; } MYSQL;
typedef struct st_mysql_res { typedef struct st_mysql_res {
...@@ -270,6 +275,8 @@ typedef struct st_mysql_res { ...@@ -270,6 +275,8 @@ typedef struct st_mysql_res {
MYSQL_ROW row; /* If unbuffered read */ MYSQL_ROW row; /* If unbuffered read */
MYSQL_ROW current_row; /* buffer to current row */ MYSQL_ROW current_row; /* buffer to current row */
my_bool eof; /* Used by mysql_fetch_row */ my_bool eof; /* Used by mysql_fetch_row */
/* mysql_stmt_close() had to cancel this result */
my_bool unbuffered_fetch_cancelled;
const struct st_mysql_methods *methods; const struct st_mysql_methods *methods;
} MYSQL_RES; } MYSQL_RES;
...@@ -479,7 +486,11 @@ my_bool STDCALL mysql_read_query_result(MYSQL *mysql); ...@@ -479,7 +486,11 @@ my_bool STDCALL mysql_read_query_result(MYSQL *mysql);
*/ */
/* statement state */ /* statement state */
enum PREP_STMT_STATE { MY_ST_UNKNOWN, MY_ST_PREPARE, MY_ST_EXECUTE }; enum enum_mysql_stmt_state
{
MYSQL_STMT_INIT_DONE= 1, MYSQL_STMT_PREPARE_DONE, MYSQL_STMT_EXECUTE_DONE,
MYSQL_STMT_FETCH_DONE
};
/* /*
client TIME structure to handle TIME, DATE and TIMESTAMP directly in client TIME structure to handle TIME, DATE and TIMESTAMP directly in
...@@ -525,31 +536,34 @@ typedef struct st_mysql_bind ...@@ -525,31 +536,34 @@ typedef struct st_mysql_bind
/* statement handler */ /* statement handler */
typedef struct st_mysql_stmt typedef struct st_mysql_stmt
{ {
MYSQL *mysql; /* connection handle */ MEM_ROOT mem_root; /* root allocations */
MYSQL_BIND *params; /* input parameters */
MYSQL_RES *result; /* resultset */
MYSQL_BIND *bind; /* row binding */
MYSQL_FIELD *fields; /* prepare meta info */
LIST list; /* list to keep track of all stmts */ LIST list; /* list to keep track of all stmts */
unsigned char *current_row; /* unbuffered row */ MYSQL *mysql; /* connection handle */
unsigned char *last_fetched_buffer; /* last fetched column buffer */ MYSQL_BIND *params; /* input parameters */
char *query; /* query buffer */ MYSQL_BIND *bind; /* output parameters */
MEM_ROOT mem_root; /* root allocations */ MYSQL_FIELD *fields; /* result set metadata */
my_ulonglong last_fetched_column; /* last fetched column */ MYSQL_RES *result; /* cached result set */
/* copy of mysql->affected_rows after statement execution */ /* copy of mysql->affected_rows after statement execution */
my_ulonglong affected_rows; my_ulonglong affected_rows;
/*
mysql_stmt_fetch() calls this function to fetch one row (it's different
for buffered, unbuffered and cursor fetch).
*/
int (*read_row_func)(struct st_mysql_stmt *stmt,
unsigned char **row);
unsigned long stmt_id; /* Id for prepared statement */ unsigned long stmt_id; /* Id for prepared statement */
unsigned int last_errno; /* error code */ unsigned int last_errno; /* error code */
unsigned int param_count; /* parameters count */ unsigned int param_count; /* inpute parameters count */
unsigned int field_count; /* fields count */ unsigned int field_count; /* number of columns in result set */
enum PREP_STMT_STATE state; /* statement state */ enum enum_mysql_stmt_state state; /* statement state */
char last_error[MYSQL_ERRMSG_SIZE]; /* error message */ char last_error[MYSQL_ERRMSG_SIZE]; /* error message */
char sqlstate[SQLSTATE_LENGTH+1]; char sqlstate[SQLSTATE_LENGTH+1];
my_bool long_alloced; /* flag to indicate long alloced */ /* Types of input parameters should be sent to server */
my_bool send_types_to_server; /* Types sent to server */ my_bool send_types_to_server;
my_bool param_buffers; /* param bound buffers */ my_bool bind_param_done; /* input buffers were supplied */
my_bool res_buffers; /* output bound buffers */ my_bool bind_result_done; /* output buffers were supplied */
my_bool result_buffered; /* Results buffered */ /* mysql_stmt_close() had to cancel this result */
my_bool unbuffered_fetch_cancelled;
} MYSQL_STMT; } MYSQL_STMT;
......
...@@ -25,6 +25,7 @@ extern "C" { ...@@ -25,6 +25,7 @@ extern "C" {
MYSQL_FIELD *unpack_fields(MYSQL_DATA *data,MEM_ROOT *alloc,uint fields, MYSQL_FIELD *unpack_fields(MYSQL_DATA *data,MEM_ROOT *alloc,uint fields,
my_bool default_value, uint server_capabilities); my_bool default_value, uint server_capabilities);
void free_rows(MYSQL_DATA *cur); void free_rows(MYSQL_DATA *cur);
void flush_use_result(MYSQL *mysql);
my_bool mysql_autenticate(MYSQL *mysql, const char *passwd); my_bool mysql_autenticate(MYSQL *mysql, const char *passwd);
void free_old_query(MYSQL *mysql); void free_old_query(MYSQL *mysql);
void end_server(MYSQL *mysql); void end_server(MYSQL *mysql);
......
...@@ -22,7 +22,6 @@ extern my_string mysql_unix_port; ...@@ -22,7 +22,6 @@ extern my_string mysql_unix_port;
CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION) CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION)
sig_handler pipe_sig_handler(int sig __attribute__((unused))); sig_handler pipe_sig_handler(int sig __attribute__((unused)));
my_bool stmt_close(MYSQL_STMT *stmt, my_bool skip_free);
void read_user_name(char *name); void read_user_name(char *name);
my_bool send_file_to_server(MYSQL *mysql, const char *filename); my_bool send_file_to_server(MYSQL *mysql, const char *filename);
......
...@@ -54,7 +54,7 @@ const char *client_errors[]= ...@@ -54,7 +54,7 @@ const char *client_errors[]=
"Malformed packet", "Malformed packet",
"Invalid use of null pointer", "Invalid use of null pointer",
"Statement not prepared", "Statement not prepared",
"Not all parameters data supplied", "Parameters data was not supplied",
"Data truncated", "Data truncated",
"No parameters exists in the statement", "No parameters exists in the statement",
"Invalid parameter number", "Invalid parameter number",
...@@ -72,7 +72,9 @@ const char *client_errors[]= ...@@ -72,7 +72,9 @@ const char *client_errors[]=
"Can't open shared memory. Can't send the request event to server (%lu)", "Can't open shared memory. Can't send the request event to server (%lu)",
"Wrong or unknown protocol", "Wrong or unknown protocol",
"Invalid connection handle", "Invalid connection handle",
"Connection using old (pre 4.1.1) authentication protocol refused (client option 'secure_auth' enabled)" "Connection using old (pre 4.1.1) authentication protocol refused (client option 'secure_auth' enabled)",
"Row retrieval was cancelled by mysql_stmt_close() call",
"Attempt to read column without prior row fetch"
}; };
/* Start of code added by Roberto M. Serqueira - martinsc@uol.com.br - 05.24.2001 */ /* Start of code added by Roberto M. Serqueira - martinsc@uol.com.br - 05.24.2001 */
...@@ -110,7 +112,7 @@ const char *client_errors[]= ...@@ -110,7 +112,7 @@ const char *client_errors[]=
"Malformed packet", "Malformed packet",
"Invalid use of null pointer", "Invalid use of null pointer",
"Statement not prepared", "Statement not prepared",
"Not all parameters data supplied", "Parameters data was not supplied",
"Data truncated", "Data truncated",
"No parameters exists in the statement", "No parameters exists in the statement",
"Invalid parameter number", "Invalid parameter number",
...@@ -128,7 +130,9 @@ const char *client_errors[]= ...@@ -128,7 +130,9 @@ const char *client_errors[]=
"Can't open shared memory. Can't send the request event to server (%lu)", "Can't open shared memory. Can't send the request event to server (%lu)",
"Wrong or unknown protocol", "Wrong or unknown protocol",
"Invalid connection handle", "Invalid connection handle",
"Connection using old (pre 4.1.1) authentication protocol refused (client option 'secure_auth' enabled)" "Connection using old (pre 4.1.1) authentication protocol refused (client option 'secure_auth' enabled)",
"Row retrieval was cancelled by mysql_stmt_close() call",
"Attempt to read column without prior row fetch"
}; };
#else /* ENGLISH */ #else /* ENGLISH */
...@@ -182,7 +186,9 @@ const char *client_errors[]= ...@@ -182,7 +186,9 @@ const char *client_errors[]=
"Can't open shared memory. Can't send the request event to server (%lu)", "Can't open shared memory. Can't send the request event to server (%lu)",
"Wrong or unknown protocol", "Wrong or unknown protocol",
"Invalid connection handle", "Invalid connection handle",
"Connection using old (pre 4.1.1) authentication protocol refused (client option 'secure_auth' enabled)" "Connection using old (pre 4.1.1) authentication protocol refused (client option 'secure_auth' enabled)",
"Row retrieval was cancelled by mysql_stmt_close() call",
"Attempt to read column without prior row fetch"
}; };
#endif #endif
......
This diff is collapsed.
...@@ -712,6 +712,34 @@ void set_mysql_error(MYSQL *mysql, int errcode, const char *sqlstate) ...@@ -712,6 +712,34 @@ void set_mysql_error(MYSQL *mysql, int errcode, const char *sqlstate)
net->last_errno= errcode; net->last_errno= errcode;
strmov(net->last_error, ER(errcode)); strmov(net->last_error, ER(errcode));
strmov(net->sqlstate, sqlstate); strmov(net->sqlstate, sqlstate);
DBUG_VOID_RETURN;
}
/*
Flush result set sent from server
*/
void flush_use_result(MYSQL *mysql)
{
/* Clear the current execution status */
DBUG_PRINT("warning",("Not all packets read, clearing them"));
for (;;)
{
ulong pkt_len;
if ((pkt_len=net_safe_read(mysql)) == packet_error)
break;
if (pkt_len <= 8 && mysql->net.read_pos[0] == 254)
{
if (protocol_41(mysql))
{
char *pos= (char*) mysql->net.read_pos;
mysql->warning_count=uint2korr(pos); pos+=2;
mysql->server_status=uint2korr(pos); pos+=2;
}
break; /* End of data */
}
}
} }
...@@ -752,26 +780,16 @@ mysql_free_result(MYSQL_RES *result) ...@@ -752,26 +780,16 @@ mysql_free_result(MYSQL_RES *result)
DBUG_PRINT("enter",("mysql_res: %lx",result)); DBUG_PRINT("enter",("mysql_res: %lx",result));
if (result) if (result)
{ {
if (result->handle && result->handle->status == MYSQL_STATUS_USE_RESULT) MYSQL *mysql= result->handle;
if (mysql)
{ {
DBUG_PRINT("warning",("Not all rows in set where read; Ignoring rows")); if (mysql->unbuffered_fetch_owner == &result->unbuffered_fetch_cancelled)
for (;;) mysql->unbuffered_fetch_owner= 0;
if (mysql->status == MYSQL_STATUS_USE_RESULT)
{ {
ulong pkt_len; flush_use_result(mysql);
if ((pkt_len=net_safe_read(result->handle)) == packet_error) mysql->status=MYSQL_STATUS_READY;
break;
if (pkt_len <= 8 && result->handle->net.read_pos[0] == 254)
{
if (protocol_41(result->handle))
{
char *pos= (char*) result->handle->net.read_pos;
result->handle->warning_count=uint2korr(pos); pos+=2;
result->handle->server_status=uint2korr(pos); pos+=2;
}
break; /* End of data */
}
} }
result->handle->status=MYSQL_STATUS_READY;
} }
free_rows(result->data); free_rows(result->data);
if (result->fields) if (result->fields)
...@@ -2177,12 +2195,13 @@ void STDCALL mysql_close(MYSQL *mysql) ...@@ -2177,12 +2195,13 @@ void STDCALL mysql_close(MYSQL *mysql)
#ifdef MYSQL_CLIENT #ifdef MYSQL_CLIENT
if (mysql->stmts) if (mysql->stmts)
{ {
/* Free any open prepared statements */ /* Reset connection handle in all prepared statements. */
LIST *element, *next_element; LIST *element;
for (element= mysql->stmts; element; element= next_element) for (element= mysql->stmts; element; element= element->next)
{ {
next_element= element->next; MYSQL_STMT *stmt= (MYSQL_STMT *) element->data;
stmt_close((MYSQL_STMT *)element->data, 1); stmt->mysql= 0;
/* No need to call list_delete for statement here */
} }
mysql->stmts= 0; mysql->stmts= 0;
} }
...@@ -2372,9 +2391,10 @@ MYSQL_RES * STDCALL mysql_store_result(MYSQL *mysql) ...@@ -2372,9 +2391,10 @@ MYSQL_RES * STDCALL mysql_store_result(MYSQL *mysql)
result->fields= mysql->fields; result->fields= mysql->fields;
result->field_alloc= mysql->field_alloc; result->field_alloc= mysql->field_alloc;
result->field_count= mysql->field_count; result->field_count= mysql->field_count;
result->current_field=0; /* The rest of result members is bzeroed in malloc */
result->current_row=0; /* Must do a fetch first */
mysql->fields=0; /* fields is now in result */ mysql->fields=0; /* fields is now in result */
/* just in case this was mistakenly called after mysql_stmt_execute() */
mysql->unbuffered_fetch_owner= 0;
DBUG_RETURN(result); /* Data fetched */ DBUG_RETURN(result); /* Data fetched */
} }
...@@ -2423,6 +2443,7 @@ static MYSQL_RES * cli_use_result(MYSQL *mysql) ...@@ -2423,6 +2443,7 @@ static MYSQL_RES * cli_use_result(MYSQL *mysql)
result->current_row= 0; result->current_row= 0;
mysql->fields=0; /* fields is now in result */ mysql->fields=0; /* fields is now in result */
mysql->status=MYSQL_STATUS_USE_RESULT; mysql->status=MYSQL_STATUS_USE_RESULT;
mysql->unbuffered_fetch_owner= &result->unbuffered_fetch_cancelled;
DBUG_RETURN(result); /* Data is read to be fetched */ DBUG_RETURN(result); /* Data is read to be fetched */
} }
...@@ -2439,19 +2460,30 @@ mysql_fetch_row(MYSQL_RES *res) ...@@ -2439,19 +2460,30 @@ mysql_fetch_row(MYSQL_RES *res)
{ /* Unbufferred fetch */ { /* Unbufferred fetch */
if (!res->eof) if (!res->eof)
{ {
if (!(read_one_row(res->handle,res->field_count,res->row, res->lengths))) MYSQL *mysql= res->handle;
if (mysql->status != MYSQL_STATUS_USE_RESULT)
{ {
res->row_count++; set_mysql_error(mysql,
DBUG_RETURN(res->current_row=res->row); res->unbuffered_fetch_cancelled ?
CR_FETCH_CANCELLED : CR_COMMANDS_OUT_OF_SYNC,
unknown_sqlstate);
} }
else else if (!(read_one_row(mysql, res->field_count, res->row, res->lengths)))
{ {
DBUG_PRINT("info",("end of data")); res->row_count++;
res->eof=1; DBUG_RETURN(res->current_row=res->row);
res->handle->status=MYSQL_STATUS_READY;
/* Don't clear handle in mysql_free_results */
res->handle=0;
} }
DBUG_PRINT("info",("end of data"));
res->eof=1;
mysql->status=MYSQL_STATUS_READY;
/*
Reset only if owner points to us: there is a chance that somebody
started new query after mysql_stmt_close():
*/
if (mysql->unbuffered_fetch_owner == &res->unbuffered_fetch_cancelled)
mysql->unbuffered_fetch_owner= 0;
/* Don't clear handle in mysql_free_result */
res->handle=0;
} }
DBUG_RETURN((MYSQL_ROW) NULL); DBUG_RETURN((MYSQL_ROW) NULL);
} }
......
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