Commit 813dff17 authored by unknown's avatar unknown

SCRUM - adding client into embedded server

error handling fixed
fetch_lengths made to work differently in embedded and client cases


include/mysql.h:
  removed 'embedded' error containers - they're unnecessary now
  added declarations for fetch_lengths to be 'virtual'
include/sql_common.h:
  fetch_lengths declaration removed
libmysql/libmysql.c:
  implementations for fetch_lengths to be 'virtual' added
libmysqld/lib_sql.cc:
  error informations now is moved from thd->net to mysql-net
libmysqld/libmysqld.c:
  error data is in mysql->net now
sql-common/client.c:
  we have to return old fetch_lengths implementation for 'client' case
sql/protocol.cc:
  handling of sqlstate for embedded library added
parent 62a3aa82
......@@ -250,9 +250,6 @@ typedef struct st_mysql
const struct st_mysql_methods *methods;
struct st_mysql_res *result;
void *thd;
unsigned int last_errno;
char *last_error;
char sqlstate[SQLSTATE_LENGTH+1]; /* Used by embedded server */
} MYSQL;
typedef struct st_mysql_res {
......@@ -267,6 +264,7 @@ typedef struct st_mysql_res {
MYSQL_ROW row; /* If unbuffered read */
MYSQL_ROW current_row; /* buffer to current row */
my_bool eof; /* Used by mysql_fetch_row */
const struct st_mysql_methods *methods;
} MYSQL_RES;
#define MAX_MYSQL_MANAGER_ERR 256
......@@ -552,6 +550,7 @@ typedef struct st_mysql_methods
ulong arg_length, my_bool skip_check);
MYSQL_RES * STDCALL (*store_result)(MYSQL *mysql);
MYSQL_RES * STDCALL (*use_result)(MYSQL *mysql);
void STDCALL (*fetch_lengths)(ulong *to, MYSQL_ROW column, uint field_count);
} MYSQL_METHODS;
MYSQL_STMT * STDCALL mysql_prepare(MYSQL * mysql, const char *query,
......
......@@ -31,7 +31,6 @@ void free_rows(MYSQL_DATA *cur);
MYSQL_DATA *read_rows (MYSQL *mysql,MYSQL_FIELD *fields,
uint field_count);
my_bool mysql_autenticate(MYSQL *mysql, const char *passwd);
void fetch_lengths(ulong *to, MYSQL_ROW column, uint field_count);
void free_old_query(MYSQL *mysql);
void end_server(MYSQL *mysql);
my_bool mysql_reconnect(MYSQL *mysql);
......
......@@ -868,7 +868,7 @@ mysql_fetch_lengths(MYSQL_RES *res)
if (!(column=res->current_row))
return 0; /* Something is wrong */
if (res->data)
fetch_lengths(res->lengths, column, res->field_count);
(*res->methods->fetch_lengths)(res->lengths, column, res->field_count);
return res->lengths;
}
......@@ -979,6 +979,7 @@ mysql_list_fields(MYSQL *mysql, const char *table, const char *wild)
free_rows(query);
DBUG_RETURN(NULL);
}
result->methods= mysql->methods;
result->field_alloc=mysql->field_alloc;
mysql->fields=0;
result->field_count = (uint) query->rows;
......@@ -1704,6 +1705,7 @@ mysql_prepare_result(MYSQL_STMT *stmt)
MYF(MY_WME | MY_ZEROFILL))))
return 0;
result->methods= stmt->mysql->methods;
result->eof=1; /* Marker for buffered */
result->fields= stmt->fields;
result->field_count= stmt->field_count;
......@@ -3193,6 +3195,7 @@ int STDCALL mysql_stmt_store_result(MYSQL_STMT *stmt)
set_stmt_error(stmt, CR_OUT_OF_MEMORY, unknown_sqlstate);
DBUG_RETURN(1);
}
result->methods= mysql->methods;
stmt->result_buffered= 1;
if (!(result->data= read_binary_rows(stmt)))
{
......
......@@ -76,8 +76,13 @@ emb_advanced_command(MYSQL *mysql, enum enum_server_command command,
if (!skip_check)
result= thd->net.last_errno ? -1 : 0;
mysql->last_error= thd->net.last_error;
mysql->net.last_errno= thd->net.last_errno;
if ((mysql->net.last_errno= thd->net.last_errno))
{
memcpy(mysql->net.last_error, thd->net.last_error,
sizeof(mysql->net.last_error));
memcpy(mysql->net.sqlstate, thd->net.sqlstate,
sizeof(mysql->net.sqlstate));
}
mysql->warning_count= ((THD*)mysql->thd)->total_warn_count;
return result;
}
......@@ -292,7 +297,6 @@ void init_embedded_mysql(MYSQL *mysql, int client_flag, char *db)
{
THD *thd = (THD *)mysql->thd;
thd->mysql= mysql;
mysql->last_error= thd->net.last_error;
}
void *create_embedded_thd(int client_flag, char *db)
......
......@@ -144,25 +144,42 @@ static inline int mysql_init_charset(MYSQL *mysql)
if (!mysql->charset)
{
mysql->last_errno=CR_CANT_READ_CHARSET;
strmov(mysql->sqlstate, "HY0000");
mysql->net.last_errno=CR_CANT_READ_CHARSET;
strmov(mysql->net.sqlstate, "HY0000");
if (mysql->options.charset_dir)
sprintf(mysql->last_error,ER(mysql->last_errno),
sprintf(mysql->net.last_error,ER(mysql->net.last_errno),
charset_name ? charset_name : "unknown",
mysql->options.charset_dir);
else
{
char cs_dir_name[FN_REFLEN];
get_charsets_dir(cs_dir_name);
sprintf(mysql->last_error,ER(mysql->last_errno),
sprintf(mysql->net.last_error,ER(mysql->net.last_errno),
charset_name ? charset_name : "unknown",
cs_dir_name);
}
return mysql->last_errno;
return mysql->net.last_errno;
}
return 0;
}
/**************************************************************************
Get column lengths of the current row
If one uses mysql_use_result, res->lengths contains the length information,
else the lengths are calculated from the offset between pointers.
**************************************************************************/
static void emb_fetch_lengths(ulong *to, MYSQL_ROW column, uint field_count)
{
MYSQL_ROW end;
for (end=column + field_count; column != end ; column++,to++)
{
*to= *column ? strlen(*column) : 0;
}
}
/*
** Note that the mysql argument must be initialized with mysql_init()
** before calling mysql_real_connect !
......@@ -178,6 +195,7 @@ static MYSQL_METHODS embedded_methods=
emb_advanced_command,
emb_mysql_store_result,
emb_mysql_use_result,
emb_fetch_lengths
};
MYSQL * STDCALL
......@@ -259,7 +277,7 @@ mysql_real_connect(MYSQL *mysql,const char *host, const char *user,
DBUG_RETURN(mysql);
error:
DBUG_PRINT("error",("message: %u (%s)",mysql->last_errno,mysql->last_error));
DBUG_PRINT("error",("message: %u (%s)",mysql->net.last_errno,mysql->net.last_error));
{
/* Free alloced memory */
my_bool free_me=mysql->free_me;
......@@ -320,7 +338,7 @@ void STDCALL mysql_close(MYSQL *mysql)
static my_bool STDCALL emb_mysql_read_query_result(MYSQL *mysql)
{
if (mysql->last_errno)
if (mysql->net.last_errno)
return -1;
if (mysql->field_count)
......@@ -343,6 +361,7 @@ static MYSQL_RES * STDCALL emb_mysql_store_result(MYSQL *mysql)
if (!result)
return 0;
result->methods= mysql->methods;
mysql->result= NULL;
*result->data->prev_ptr= 0;
result->eof= 1;
......
......@@ -965,11 +965,25 @@ void mysql_read_default_options(struct st_mysql_options *options,
else the lengths are calculated from the offset between pointers.
**************************************************************************/
void fetch_lengths(ulong *to, MYSQL_ROW column, uint field_count)
static void cli_fetch_lengths(ulong *to, MYSQL_ROW column, uint field_count)
{
ulong *prev_length;
byte *start=0;
MYSQL_ROW end;
for (end=column + field_count; column != end ; column++, to++)
*to= *column ? strlen(*column) : 0;
prev_length=0; /* Keep gcc happy */
for (end=column + field_count + 1 ; column != end ; column++, to++)
{
if (!*column)
{
*to= 0; /* Null */
continue;
}
if (start) /* Found end of prev string */
*prev_length= (ulong) (*column-start-1);
start= *column;
prev_length= to;
}
}
/***************************************************************************
......@@ -999,7 +1013,7 @@ unpack_fields(MYSQL_DATA *data,MEM_ROOT *alloc,uint fields,
for (row=data->data; row ; row = row->next,field++)
{
uchar *pos;
fetch_lengths(&lengths[0], row->data, default_value ? 8 : 7);
cli_fetch_lengths(&lengths[0], row->data, default_value ? 8 : 7);
field->catalog = strdup_root(alloc,(char*) row->data[0]);
field->db = strdup_root(alloc,(char*) row->data[1]);
field->table = strdup_root(alloc,(char*) row->data[2]);
......@@ -1040,7 +1054,7 @@ unpack_fields(MYSQL_DATA *data,MEM_ROOT *alloc,uint fields,
/* old protocol, for backward compatibility */
for (row=data->data; row ; row = row->next,field++)
{
fetch_lengths(&lengths[0], row->data, default_value ? 6 : 5);
cli_fetch_lengths(&lengths[0], row->data, default_value ? 6 : 5);
field->org_table= field->table= strdup_root(alloc,(char*) row->data[0]);
field->name= strdup_root(alloc,(char*) row->data[1]);
field->length= (uint) uint3korr(row->data[2]);
......@@ -1418,7 +1432,8 @@ static MYSQL_METHODS client_methods=
cli_mysql_read_query_result,
cli_advanced_command,
cli_mysql_store_result,
cli_mysql_use_result
cli_mysql_use_result,
cli_fetch_lengths
};
MYSQL * STDCALL
......@@ -2320,6 +2335,7 @@ static MYSQL_RES * STDCALL cli_mysql_store_result(MYSQL *mysql)
strmov(mysql->net.last_error, ER(mysql->net.last_errno));
DBUG_RETURN(0);
}
result->methods= mysql->methods;
result->eof=1; /* Marker for buffered */
result->lengths=(ulong*) (result+1);
if (!(result->data=read_rows(mysql,mysql->fields,mysql->field_count)))
......@@ -2370,6 +2386,7 @@ static MYSQL_RES * STDCALL cli_mysql_use_result(MYSQL *mysql)
MYF(MY_WME | MY_ZEROFILL))))
DBUG_RETURN(0);
result->lengths=(ulong*) (result+1);
result->methods= mysql->methods;
if (!(result->row=(MYSQL_ROW)
my_malloc(sizeof(result->row[0])*(mysql->field_count+1), MYF(MY_WME))))
{ /* Ptrs: to one row */
......
......@@ -83,6 +83,7 @@ void send_error(THD *thd, uint sql_errno, const char *err)
#ifdef EMBEDDED_LIBRARY
net->last_errno= sql_errno;
strmake(net->last_error, err, sizeof(net->last_error)-1);
strmov(net->sqlstate, mysql_errno_to_sqlstate(sql_errno));
#else
if (net->vio == 0)
......@@ -230,6 +231,7 @@ net_printf(THD *thd, uint errcode, ...)
#else
net->last_errno= errcode;
strmake(net->last_error, text_pos, length);
strmake(net->sqlstate, mysql_errno_to_sqlstate(errcode), SQLSTATE_LENGTH);
#endif
thd->is_fatal_error=0; // Error message is given
DBUG_VOID_RETURN;
......
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