From 964b8f63e73314c0453cafcc198ac5eb16e08ab0 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 16 Dec 2002 19:09:48 +0400 Subject: [PATCH 1/5] extended resolve() and reverse_resolve() UDFs for FreeBSD (SCRUM) --- sql/udf_example.cc | 55 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/sql/udf_example.cc b/sql/udf_example.cc index 176ddeb10a3..dfe8177bfce 100644 --- a/sql/udf_example.cc +++ b/sql/udf_example.cc @@ -126,6 +126,8 @@ typedef long long longlong; #include #include // To get strmov() +static pthread_mutex_t LOCK_hostname; + #ifdef HAVE_DLOPEN /* These must be right or mysqld will not find the symbol! */ @@ -282,8 +284,8 @@ char *metaphon(UDF_INIT *initid, UDF_ARGS *args, char *result, for (n = ntrans + 1, n_end = ntrans + sizeof(ntrans)-2; word != w_end && n < n_end; word++ ) - if ( isalpha ( *word )) - *n++ = toupper ( *word ); + if ( my_isalpha ( my_charset_latin1, *word )) + *n++ = my_toupper ( my_charset_latin1, *word ); if ( n == ntrans + 1 ) /* return empty string if 0 bytes */ { @@ -583,6 +585,8 @@ longlong myfunc_int(UDF_INIT *initid, UDF_ARGS *args, char *is_null, case REAL_RESULT: // Add numers as longlong val += (longlong) *((double*) args->args[i]); break; + default: + break; } } return val; @@ -642,8 +646,6 @@ longlong sequence(UDF_INIT *initid, UDF_ARGS *args, char *is_null, ** ****************************************************************************/ -#if defined(HAVE_GETHOSTBYADDR_R) && defined(HAVE_SOLARIS_STYLE_GETHOST) - #include #include #include @@ -651,9 +653,11 @@ longlong sequence(UDF_INIT *initid, UDF_ARGS *args, char *is_null, extern "C" { my_bool lookup_init(UDF_INIT *initid, UDF_ARGS *args, char *message); +void lookup_deinit(UDF_INIT *initid); char *lookup(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *null_value, char *error); my_bool reverse_lookup_init(UDF_INIT *initid, UDF_ARGS *args, char *message); +void reverse_lookup_deinit(UDF_INIT *initid); char *reverse_lookup(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *null_value, char *error); } @@ -676,9 +680,19 @@ my_bool lookup_init(UDF_INIT *initid, UDF_ARGS *args, char *message) } initid->max_length=11; initid->maybe_null=1; +#if !defined(HAVE_GETHOSTBYADDR_R) && defined(HAVE_SOLARIS_STYLE_GETHOST) + (void) pthread_mutex_init(&LOCK_hostname,MY_MUTEX_INIT_SLOW); +#endif return 0; } +void lookup_deinit(UDF_INIT *initid) +{ +#if !defined(HAVE_GETHOSTBYADDR_R) && defined(HAVE_SOLARIS_STYLE_GETHOST) + (void) pthread_mutex_destroy(&LOCK_hostname); +#endif +} + char *lookup(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *res_length, char *null_value, char *error) { @@ -696,13 +710,23 @@ char *lookup(UDF_INIT *initid, UDF_ARGS *args, char *result, length=sizeof(name_buff)-1; memcpy(name_buff,args->args[0],length); name_buff[length]=0; - +#if defined(HAVE_GETHOSTBYADDR_R) && defined(HAVE_SOLARIS_STYLE_GETHOST) if (!(hostent=gethostbyname_r(name_buff,&tmp_hostent,hostname_buff, sizeof(hostname_buff), &tmp_errno))) { *null_value=1; return 0; } +#else + VOID(pthread_mutex_lock(&LOCK_hostname)); + if (!(hostent= gethostbyname((char*) name_buff))) + { + VOID(pthread_mutex_unlock(&LOCK_hostname)); + *null_value= 1; + return 0; + } + VOID(pthread_mutex_unlock(&LOCK_hostname)); +#endif struct in_addr in; memcpy_fixed((char*) &in,(char*) *hostent->h_addr_list, sizeof(in.s_addr)); *res_length= (ulong) (strmov(result, inet_ntoa(in)) - result); @@ -731,9 +755,18 @@ my_bool reverse_lookup_init(UDF_INIT *initid, UDF_ARGS *args, char *message) } initid->max_length=32; initid->maybe_null=1; +#if !defined(HAVE_GETHOSTBYADDR_R) && defined(HAVE_SOLARIS_STYLE_GETHOST) + (void) pthread_mutex_init(&LOCK_hostname,MY_MUTEX_INIT_SLOW); +#endif return 0; } +void reverse_lookup_deinit(UDF_INIT *initid) +{ +#if !defined(HAVE_GETHOSTBYADDR_R) && defined(HAVE_SOLARIS_STYLE_GETHOST) + (void) pthread_mutex_destroy(&LOCK_hostname); +#endif +} char *reverse_lookup(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *res_length, char *null_value, char *error) @@ -776,6 +809,7 @@ char *reverse_lookup(UDF_INIT *initid, UDF_ARGS *args, char *result, return 0; } struct hostent *hp; +#if defined(HAVE_GETHOSTBYADDR_R) && defined(HAVE_SOLARIS_STYLE_GETHOST) int tmp_errno; if (!(hp=gethostbyaddr_r((char*) &taddr,sizeof(taddr), AF_INET, &tmp_hostent, name_buff,sizeof(name_buff), @@ -784,10 +818,19 @@ char *reverse_lookup(UDF_INIT *initid, UDF_ARGS *args, char *result, *null_value=1; return 0; } +#else + VOID(pthread_mutex_lock(&LOCK_hostname)); + if (!(hp= gethostbyaddr((char*) &taddr, sizeof(taddr), AF_INET))) + { + VOID(pthread_mutex_unlock(&LOCK_hostname)); + *null_value= 1; + return 0; + } + VOID(pthread_mutex_unlock(&LOCK_hostname)); +#endif *res_length=(ulong) (strmov(result,hp->h_name) - result); return result; } -#endif // defined(HAVE_GETHOSTBYADDR_R) && defined(HAVE_SOLARIS_STYLE_GETHOST) /* ** Syntax for the new aggregate commands are: From c22b49078905d84ba8ff2f10c19881b4bb7639e5 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 17 Dec 2002 11:25:27 +0400 Subject: [PATCH 2/5] Move all sql_type() to the same file --- sql/field.cc | 10 ++++++++++ sql/field.h | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/sql/field.cc b/sql/field.cc index e3db572eb57..565df659245 100644 --- a/sql/field.cc +++ b/sql/field.cc @@ -317,6 +317,16 @@ bool Field::optimize_range(uint idx) return test(table->file->index_flags(idx) & HA_READ_NEXT); } +/**************************************************************************** + Functions for the Field_null +****************************************************************************/ + +void Field_null::sql_type(String &res) const +{ + res.set("null",4,system_charset_info); +} + + /**************************************************************************** Functions for the Field_decimal class This is an number stored as a pre-space (or pre-zero) string diff --git a/sql/field.h b/sql/field.h index 4c0af6e807b..826b0a36fcf 100644 --- a/sql/field.h +++ b/sql/field.h @@ -556,7 +556,7 @@ public: int cmp(const char *a, const char *b) { return 0;} void sort_string(char *buff, uint length) {} uint32 pack_length() const { return 0; } - void sql_type(String &str) const { str.set("null",4,my_thd_charset); } + void sql_type(String &str) const; uint size_of() const { return sizeof(*this); } }; From d888e97e56f89abee91e90b8cfc946320566dd66 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 17 Dec 2002 14:35:19 +0400 Subject: [PATCH 3/5] Some fixes to get closer to UCS2 compatible SHOW FIELDS --- sql/field.cc | 103 +++++++++++++++++++++++++----------------------- sql/field.h | 1 + sql/sql_show.cc | 2 +- 3 files changed, 55 insertions(+), 51 deletions(-) diff --git a/sql/field.cc b/sql/field.cc index 565df659245..230480b9cfe 100644 --- a/sql/field.cc +++ b/sql/field.cc @@ -199,7 +199,7 @@ void Field::copy_from_tmp(int row_offset) bool Field::send_binary(Protocol *protocol) { char buff[MAX_FIELD_WIDTH]; - String tmp(buff,sizeof(buff),default_charset_info); + String tmp(buff,sizeof(buff),charset()); val_str(&tmp,&tmp); return protocol->store(tmp.ptr(), tmp.length()); } @@ -207,7 +207,6 @@ bool Field::send_binary(Protocol *protocol) void Field_num::add_zerofill_and_unsigned(String &res) const { - res.length((uint) strlen(res.ptr())); // Fix length if (unsigned_flag) res.append(" unsigned"); if (zerofill) @@ -241,6 +240,17 @@ void Field_str::make_field(Send_field *field) field->decimals=0; } +void Field_str::add_binary_or_charset(String &res) const +{ + if (binary()) + res.append(" binary"); + else + { + res.append(" character set "); + res.append(field_charset->name); + } +} + uint Field::fill_cache_field(CACHE_FIELD *copy) { @@ -323,7 +333,7 @@ bool Field::optimize_range(uint idx) void Field_null::sql_type(String &res) const { - res.set("null",4,system_charset_info); + res.copy("null",4,my_charset_latin1,res.charset()); } @@ -900,11 +910,13 @@ void Field_decimal::sort_string(char *to,uint length) void Field_decimal::sql_type(String &res) const { uint tmp=field_length; + uint len; if (!unsigned_flag) tmp--; if (dec) tmp--; - sprintf((char*) res.ptr(),"decimal(%d,%d)",tmp,dec); + len=sprintf((char*) res.ptr(),"decimal(%d,%d)",tmp,dec); + res.length(len); add_zerofill_and_unsigned(res); } @@ -1103,7 +1115,8 @@ void Field_tiny::sort_string(char *to,uint length __attribute__((unused))) void Field_tiny::sql_type(String &res) const { - sprintf((char*) res.ptr(),"tinyint(%d)",(int) field_length); + uint len=sprintf((char*) res.ptr(),"tinyint(%d)",(int) field_length); + res.length(len); add_zerofill_and_unsigned(res); } @@ -1374,7 +1387,8 @@ void Field_short::sort_string(char *to,uint length __attribute__((unused))) void Field_short::sql_type(String &res) const { - sprintf((char*) res.ptr(),"smallint(%d)",(int) field_length); + uint len=sprintf((char*) res.ptr(),"smallint(%d)",(int) field_length); + res.length(len); add_zerofill_and_unsigned(res); } @@ -1589,7 +1603,8 @@ void Field_medium::sort_string(char *to,uint length __attribute__((unused))) void Field_medium::sql_type(String &res) const { - sprintf((char*) res.ptr(),"mediumint(%d)",(int) field_length); + uint len=sprintf((char*) res.ptr(),"mediumint(%d)",(int) field_length); + res.length(len); add_zerofill_and_unsigned(res); } @@ -1850,7 +1865,8 @@ void Field_long::sort_string(char *to,uint length __attribute__((unused))) void Field_long::sql_type(String &res) const { - sprintf((char*) res.ptr(),"int(%d)",(int) field_length); + uint len=sprintf((char*) res.ptr(),"int(%d)",(int) field_length); + res.length(len); add_zerofill_and_unsigned(res); } @@ -2079,7 +2095,8 @@ void Field_longlong::sort_string(char *to,uint length __attribute__((unused))) void Field_longlong::sql_type(String &res) const { - sprintf((char*) res.ptr(),"bigint(%d)",(int) field_length); + uint len=sprintf((char*) res.ptr(),"bigint(%d)",(int) field_length); + res.length(len); add_zerofill_and_unsigned(res); } @@ -2343,10 +2360,17 @@ bool Field_float::send_binary(Protocol *protocol) void Field_float::sql_type(String &res) const { + uint len; if (dec == NOT_FIXED_DEC) + { strmov((char*) res.ptr(),"float"); + len=5; + } else - sprintf((char*) res.ptr(),"float(%d,%d)",(int) field_length,dec); + { + len=sprintf((char*) res.ptr(),"float(%d,%d)",(int) field_length,dec); + } + res.length(len); add_zerofill_and_unsigned(res); } @@ -2595,10 +2619,17 @@ void Field_double::sort_string(char *to,uint length __attribute__((unused))) void Field_double::sql_type(String &res) const { + uint len; if (dec == NOT_FIXED_DEC) + { strmov((char*) res.ptr(),"double"); + len=6; + } else - sprintf((char*) res.ptr(),"double(%d,%d)",(int) field_length,dec); + { + len=sprintf((char*) res.ptr(),"double(%d,%d)",(int) field_length,dec); + } + res.length(len); add_zerofill_and_unsigned(res); } @@ -2929,7 +2960,7 @@ void Field_timestamp::sort_string(char *to,uint length __attribute__((unused))) void Field_timestamp::sql_type(String &res) const { - res.set("timestamp", 9, default_charset_info); + res.copy("timestamp", 9, my_charset_latin1, res.charset()); } @@ -3121,7 +3152,7 @@ void Field_time::sort_string(char *to,uint length __attribute__((unused))) void Field_time::sql_type(String &res) const { - res.set("time",4,default_charset_info); + res.copy("time",4,my_charset_latin1, res.charset()); } /**************************************************************************** @@ -3383,7 +3414,7 @@ void Field_date::sort_string(char *to,uint length __attribute__((unused))) void Field_date::sql_type(String &res) const { - res.set("date",4,default_charset_info); + res.copy("date",4,my_charset_latin1, res.charset()); } /**************************************************************************** @@ -3546,7 +3577,7 @@ void Field_newdate::sort_string(char *to,uint length __attribute__((unused))) void Field_newdate::sql_type(String &res) const { - res.set("date",4,default_charset_info); + res.copy("date",4,my_charset_latin1, res.charset()); } @@ -3770,7 +3801,7 @@ void Field_datetime::sort_string(char *to,uint length __attribute__((unused))) void Field_datetime::sql_type(String &res) const { - res.set("datetime",8,default_charset_info); + res.copy("datetime",8,my_charset_latin1, res.charset()); } /**************************************************************************** @@ -3906,14 +3937,8 @@ void Field_string::sql_type(String &res) const HA_OPTION_PACK_RECORD) ? "varchar" : "char"), (int) field_length)); - res.length((uint) length); - if (binary()) - res.append(" binary"); - else - { - res.append(" character set "); - res.append(field_charset->name); - } + res.length(length); + add_binary_or_charset(res); } @@ -4104,14 +4129,8 @@ void Field_varstring::sql_type(String &res) const ulong length= my_sprintf((char*) res.ptr(), ((char*) res.ptr(),"varchar(%u)", field_length)); - res.length((uint) length); - if (binary()) - res.append(" binary"); - else - { - res.append(" character set "); - res.append(field_charset->name); - } + res.length(length); + add_binary_or_charset(res); } char *Field_varstring::pack(char *to, const char *from, uint max_length) @@ -4985,15 +5004,7 @@ void Field_enum::sql_type(String &res) const flag=1; } res.append(')'); - if (binary()) - { - res.append(" binary"); - } - else - { - res.append(" character set "); - res.append(field_charset->name); - } + add_binary_or_charset(res); } @@ -5115,15 +5126,7 @@ void Field_set::sql_type(String &res) const flag=1; } res.append(')'); - if (binary()) - { - res.append(" binary"); - } - else - { - res.append(" character set "); - res.append(field_charset->name); - } + add_binary_or_charset(res); } /* returns 1 if the fields are equally defined */ diff --git a/sql/field.h b/sql/field.h index 826b0a36fcf..ecb5fab121b 100644 --- a/sql/field.h +++ b/sql/field.h @@ -262,6 +262,7 @@ public: unireg_check_arg, field_name_arg, table_arg) { field_charset=charset; } Item_result result_type () const { return STRING_RESULT; } + void add_binary_or_charset(String &res) const; uint decimals() const { return NOT_FIXED_DEC; } void make_field(Send_field *); uint size_of() const { return sizeof(*this); } diff --git a/sql/sql_show.cc b/sql/sql_show.cc index f60cba54e84..791e08dffda 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -713,7 +713,7 @@ mysqld_show_fields(THD *thd, TABLE_LIST *table_list,const char *wild, { byte *pos; uint flags=field->flags; - String type(tmp,sizeof(tmp),default_charset_info); + String type(tmp,sizeof(tmp),current_thd->thd_charset); uint col_access; bool null_default_value=0; From 07f3ca938ab62d7dae822380034aad443b9e89c9 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 17 Dec 2002 19:13:07 +0400 Subject: [PATCH 4/5] Many init commands on connect BitKeeper/etc/logging_ok: Logging to logging@openlogging.org accepted --- BitKeeper/etc/logging_ok | 1 + include/mysql.h | 3 +- libmysql/libmysql.c | 60 ++++++++++++++++++++++++++++++++-------- libmysqld/libmysqld.c | 60 ++++++++++++++++++++++++++++++++-------- 4 files changed, 99 insertions(+), 25 deletions(-) diff --git a/BitKeeper/etc/logging_ok b/BitKeeper/etc/logging_ok index 89391563f72..15f884302a1 100644 --- a/BitKeeper/etc/logging_ok +++ b/BitKeeper/etc/logging_ok @@ -90,6 +90,7 @@ tonu@x153.internalnet tonu@x3.internalnet venu@myvenu.com venu@work.mysql.com +vva@eagle.mysql.r18.ru vva@genie.(none) walrus@mysql.com wax@mysql.com diff --git a/include/mysql.h b/include/mysql.h index 1d4b923f4f2..063d7227351 100644 --- a/include/mysql.h +++ b/include/mysql.h @@ -119,7 +119,8 @@ typedef struct st_mysql_data { struct st_mysql_options { unsigned int connect_timeout,client_flag; unsigned int port; - char *host,*init_command,*user,*password,*unix_socket,*db; + char *host,*user,*password,*unix_socket,*db; + struct st_dynamic_array *init_commands; char *my_cnf_file,*my_cnf_group, *charset_dir, *charset_name; char *ssl_key; /* PEM key file */ char *ssl_cert; /* PEM cert file */ diff --git a/libmysql/libmysql.c b/libmysql/libmysql.c index ad97d5b1442..9bd5eaf581e 100644 --- a/libmysql/libmysql.c +++ b/libmysql/libmysql.c @@ -935,6 +935,27 @@ static const char *default_options[]= static TYPELIB option_types={array_elements(default_options)-1, "options",default_options}; +static int add_init_command(struct st_mysql_options *options, const char *cmd) +{ + char **ptr, *tmp; + + if (!options->init_commands) + { + options->init_commands= (DYNAMIC_ARRAY*)my_malloc(sizeof(DYNAMIC_ARRAY), + MYF(MY_WME)); + init_dynamic_array(options->init_commands,sizeof(char*),0,5 CALLER_INFO); + } + + if (!(tmp= my_strdup(cmd,MYF(MY_WME))) || + insert_dynamic(options->init_commands, &tmp)) + { + my_free(tmp, MYF(MY_ALLOW_ZERO_PTR)); + return 1; + } + + return 0; +} + static void mysql_read_default_options(struct st_mysql_options *options, const char *filename,const char *group) { @@ -1003,11 +1024,7 @@ static void mysql_read_default_options(struct st_mysql_options *options, } break; case 8: /* init-command */ - if (opt_arg) - { - my_free(options->init_command,MYF(MY_ALLOW_ZERO_PTR)); - options->init_command=my_strdup(opt_arg,MYF(MY_WME)); - } + add_init_command(options,opt_arg); break; case 9: /* host */ if (opt_arg) @@ -2328,13 +2345,29 @@ Try also with PIPE or TCP/IP net->compress=1; if (db && mysql_select_db(mysql,db)) goto error; - if (mysql->options.init_command) + + if (mysql->options.init_commands) { + DYNAMIC_ARRAY *init_commands= mysql->options.init_commands; + char **ptr= (char**)init_commands->buffer; + char **end= ptr + init_commands->elements; + my_bool reconnect=mysql->reconnect; mysql->reconnect=0; - if (mysql_query(mysql,mysql->options.init_command)) - goto error; - mysql_free_result(mysql_use_result(mysql)); + + for (; ptrfields) + { + if (!(res= mysql_use_result(mysql))) + goto error; + mysql_free_result(res); + } + } + mysql->reconnect=reconnect; } @@ -2579,7 +2612,6 @@ mysql_close(MYSQL *mysql) my_free(mysql->user,MYF(MY_ALLOW_ZERO_PTR)); my_free(mysql->passwd,MYF(MY_ALLOW_ZERO_PTR)); my_free(mysql->db,MYF(MY_ALLOW_ZERO_PTR)); - my_free(mysql->options.init_command,MYF(MY_ALLOW_ZERO_PTR)); my_free(mysql->options.user,MYF(MY_ALLOW_ZERO_PTR)); my_free(mysql->options.host,MYF(MY_ALLOW_ZERO_PTR)); my_free(mysql->options.password,MYF(MY_ALLOW_ZERO_PTR)); @@ -2589,6 +2621,11 @@ mysql_close(MYSQL *mysql) my_free(mysql->options.my_cnf_group,MYF(MY_ALLOW_ZERO_PTR)); my_free(mysql->options.charset_dir,MYF(MY_ALLOW_ZERO_PTR)); my_free(mysql->options.charset_name,MYF(MY_ALLOW_ZERO_PTR)); + if (mysql->options.init_commands) + { + delete_dynamic(mysql->options.init_commands); + my_free((char*)mysql->options.init_commands,MYF(MY_WME)); + } #ifdef HAVE_OPENSSL mysql_ssl_free(mysql); #endif /* HAVE_OPENSSL */ @@ -3334,8 +3371,7 @@ mysql_options(MYSQL *mysql,enum mysql_option option, const char *arg) mysql->options.client_flag&= ~CLIENT_LOCAL_FILES; break; case MYSQL_INIT_COMMAND: - my_free(mysql->options.init_command,MYF(MY_ALLOW_ZERO_PTR)); - mysql->options.init_command=my_strdup(arg,MYF(MY_WME)); + add_init_command(&mysql->options,arg); break; case MYSQL_READ_DEFAULT_FILE: my_free(mysql->options.my_cnf_file,MYF(MY_ALLOW_ZERO_PTR)); diff --git a/libmysqld/libmysqld.c b/libmysqld/libmysqld.c index 059ed184f13..cfbd3157476 100644 --- a/libmysqld/libmysqld.c +++ b/libmysqld/libmysqld.c @@ -451,6 +451,27 @@ static const char *default_options[]= static TYPELIB option_types={array_elements(default_options)-1, "options",default_options}; +static int add_init_command(struct st_mysql_options *options, const char *cmd) +{ + char **ptr, *tmp; + + if (!options->init_commands) + { + options->init_commands= (DYNAMIC_ARRAY*)my_malloc(sizeof(DYNAMIC_ARRAY), + MYF(MY_WME)); + init_dynamic_array(options->init_commands,sizeof(char*),0,5 CALLER_INFO); + } + + if (!(tmp= my_strdup(cmd,MYF(MY_WME))) || + insert_dynamic(options->init_commands, &tmp)) + { + my_free(tmp, MYF(MY_ALLOW_ZERO_PTR)); + return 1; + } + + return 0; +} + static void mysql_read_default_options(struct st_mysql_options *options, const char *filename,const char *group) { @@ -520,11 +541,7 @@ static void mysql_read_default_options(struct st_mysql_options *options, } break; case 8: /* init-command */ - if (opt_arg) - { - my_free(options->init_command,MYF(MY_ALLOW_ZERO_PTR)); - options->init_command=my_strdup(opt_arg,MYF(MY_WME)); - } + add_init_command(options,opt_arg); break; case 9: /* host */ if (opt_arg) @@ -1005,13 +1022,29 @@ mysql_real_connect(MYSQL *mysql,const char *host, const char *user, goto error; if (db && mysql_select_db(mysql,db)) goto error; - if (mysql->options.init_command) + + if (mysql->options.init_commands) { + DYNAMIC_ARRAY *init_commands= mysql->options.init_commands; + char **ptr= (char**)init_commands->buffer; + char **end= ptr + init_commands->elements; + my_bool reconnect=mysql->reconnect; mysql->reconnect=0; - if (mysql_query(mysql,mysql->options.init_command)) - goto error; - mysql_free_result(mysql_use_result(mysql)); + + for (; ptrfields) + { + if (!(res= mysql_use_result(mysql))) + goto error; + mysql_free_result(res); + } + } + mysql->reconnect=reconnect; } @@ -1108,7 +1141,6 @@ mysql_close(MYSQL *mysql) my_free(mysql->user,MYF(MY_ALLOW_ZERO_PTR)); my_free(mysql->passwd,MYF(MY_ALLOW_ZERO_PTR)); my_free(mysql->db,MYF(MY_ALLOW_ZERO_PTR)); - my_free(mysql->options.init_command,MYF(MY_ALLOW_ZERO_PTR)); my_free(mysql->options.user,MYF(MY_ALLOW_ZERO_PTR)); my_free(mysql->options.host,MYF(MY_ALLOW_ZERO_PTR)); my_free(mysql->options.password,MYF(MY_ALLOW_ZERO_PTR)); @@ -1118,6 +1150,11 @@ mysql_close(MYSQL *mysql) my_free(mysql->options.my_cnf_group,MYF(MY_ALLOW_ZERO_PTR)); my_free(mysql->options.charset_dir,MYF(MY_ALLOW_ZERO_PTR)); my_free(mysql->options.charset_name,MYF(MY_ALLOW_ZERO_PTR)); + if (mysql->options.init_commands) + { + delete_dynamic(mysql->options.init_commands); + my_free((char*)mysql->options.init_commands,MYF(MY_WME)); + } /* Clear pointers for better safety */ mysql->host_info=mysql->user=mysql->passwd=mysql->db=0; bzero((char*) &mysql->options,sizeof(mysql->options)); @@ -1820,8 +1857,7 @@ mysql_options(MYSQL *mysql,enum mysql_option option, const char *arg) mysql->options.client_flag&= ~CLIENT_LOCAL_FILES; break; case MYSQL_INIT_COMMAND: - my_free(mysql->options.init_command,MYF(MY_ALLOW_ZERO_PTR)); - mysql->options.init_command=my_strdup(arg,MYF(MY_WME)); + add_init_command(&mysql->options,arg); break; case MYSQL_READ_DEFAULT_FILE: my_free(mysql->options.my_cnf_file,MYF(MY_ALLOW_ZERO_PTR)); From fddbc989a4c42a58f5986fb158145763ce47107a Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 17 Dec 2002 19:57:22 +0400 Subject: [PATCH 5/5] sql_type() is now ucs2 compatible --- sql/field.cc | 130 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 87 insertions(+), 43 deletions(-) diff --git a/sql/field.cc b/sql/field.cc index 230480b9cfe..abcfd6158ac 100644 --- a/sql/field.cc +++ b/sql/field.cc @@ -207,10 +207,17 @@ bool Field::send_binary(Protocol *protocol) void Field_num::add_zerofill_and_unsigned(String &res) const { - if (unsigned_flag) - res.append(" unsigned"); - if (zerofill) - res.append(" zerofill"); + uint oldlen=res.length(); + if (oldlen < res.alloced_length()) + { + uint len=res.alloced_length()-oldlen; + char *end=(char*)(res.ptr()+oldlen); + CHARSET_INFO *cs=res.charset(); + len=cs->snprintf(cs,end,len,"%s%s", + unsigned_flag ? " unsigned" : "", + zerofill ? " zerofill" : ""); + res.length(len+oldlen); + } } void Field_num::make_field(Send_field *field) @@ -242,12 +249,17 @@ void Field_str::make_field(Send_field *field) void Field_str::add_binary_or_charset(String &res) const { - if (binary()) - res.append(" binary"); - else + uint oldlen=res.length(); + if (oldlen < res.alloced_length()) { - res.append(" character set "); - res.append(field_charset->name); + CHARSET_INFO *cs=res.charset(); + uint len=res.alloced_length() - oldlen; + char *end=(char*)(res.ptr()+oldlen); + if (binary()) + len=cs->snprintf(cs,end,len," binary"); + else + len=cs->snprintf(cs,end,len," character set %s",field_charset->name); + res.length(oldlen+len); } } @@ -333,7 +345,10 @@ bool Field::optimize_range(uint idx) void Field_null::sql_type(String &res) const { - res.copy("null",4,my_charset_latin1,res.charset()); + CHARSET_INFO *cs=res.charset(); + uint len; + len=cs->snprintf(cs,(char*)res.ptr(),res.alloced_length(),"null"); + res.length(len); } @@ -909,13 +924,15 @@ void Field_decimal::sort_string(char *to,uint length) void Field_decimal::sql_type(String &res) const { + CHARSET_INFO *cs=res.charset(); uint tmp=field_length; uint len; if (!unsigned_flag) tmp--; if (dec) tmp--; - len=sprintf((char*) res.ptr(),"decimal(%d,%d)",tmp,dec); + len=cs->snprintf(cs,(char*) res.ptr(),res.alloced_length(), + "decimal(%d,%d)",tmp,dec); res.length(len); add_zerofill_and_unsigned(res); } @@ -1115,7 +1132,9 @@ void Field_tiny::sort_string(char *to,uint length __attribute__((unused))) void Field_tiny::sql_type(String &res) const { - uint len=sprintf((char*) res.ptr(),"tinyint(%d)",(int) field_length); + CHARSET_INFO *cs=res.charset(); + uint len=cs->snprintf(cs,(char*) res.ptr(),res.alloced_length(), + "tinyint(%d)",(int) field_length); res.length(len); add_zerofill_and_unsigned(res); } @@ -1387,7 +1406,9 @@ void Field_short::sort_string(char *to,uint length __attribute__((unused))) void Field_short::sql_type(String &res) const { - uint len=sprintf((char*) res.ptr(),"smallint(%d)",(int) field_length); + CHARSET_INFO *cs=res.charset(); + uint len=cs->snprintf(cs,(char*) res.ptr(),res.alloced_length(), + "smallint(%d)",(int) field_length); res.length(len); add_zerofill_and_unsigned(res); } @@ -1603,7 +1624,9 @@ void Field_medium::sort_string(char *to,uint length __attribute__((unused))) void Field_medium::sql_type(String &res) const { - uint len=sprintf((char*) res.ptr(),"mediumint(%d)",(int) field_length); + CHARSET_INFO *cs=res.charset(); + uint len=cs->snprintf(cs,(char*) res.ptr(),res.alloced_length(), + "mediumint(%d)",(int) field_length); res.length(len); add_zerofill_and_unsigned(res); } @@ -1865,7 +1888,9 @@ void Field_long::sort_string(char *to,uint length __attribute__((unused))) void Field_long::sql_type(String &res) const { - uint len=sprintf((char*) res.ptr(),"int(%d)",(int) field_length); + CHARSET_INFO *cs=res.charset(); + uint len=cs->snprintf(cs,(char*) res.ptr(),res.alloced_length(), + "int(%d)",(int) field_length); res.length(len); add_zerofill_and_unsigned(res); } @@ -2095,7 +2120,9 @@ void Field_longlong::sort_string(char *to,uint length __attribute__((unused))) void Field_longlong::sql_type(String &res) const { - uint len=sprintf((char*) res.ptr(),"bigint(%d)",(int) field_length); + CHARSET_INFO *cs=res.charset(); + uint len=cs->snprintf(cs,(char*) res.ptr(),res.alloced_length(), + "bigint(%d)",(int) field_length); res.length(len); add_zerofill_and_unsigned(res); } @@ -2360,15 +2387,16 @@ bool Field_float::send_binary(Protocol *protocol) void Field_float::sql_type(String &res) const { + CHARSET_INFO *cs=res.charset(); uint len; if (dec == NOT_FIXED_DEC) { - strmov((char*) res.ptr(),"float"); - len=5; + len=cs->snprintf(cs,(char*) res.ptr(),res.alloced_length(),"float"); } else { - len=sprintf((char*) res.ptr(),"float(%d,%d)",(int) field_length,dec); + len=cs->snprintf(cs,(char*) res.ptr(),res.alloced_length(), + "float(%d,%d)",(int) field_length,dec); } res.length(len); add_zerofill_and_unsigned(res); @@ -2619,15 +2647,16 @@ void Field_double::sort_string(char *to,uint length __attribute__((unused))) void Field_double::sql_type(String &res) const { + CHARSET_INFO *cs=res.charset(); uint len; if (dec == NOT_FIXED_DEC) { - strmov((char*) res.ptr(),"double"); - len=6; + len=cs->snprintf(cs,(char*) res.ptr(),res.alloced_length(),"double"); } else { - len=sprintf((char*) res.ptr(),"double(%d,%d)",(int) field_length,dec); + len=cs->snprintf(cs,(char*) res.ptr(),res.alloced_length(), + "double(%d,%d)",(int) field_length,dec); } res.length(len); add_zerofill_and_unsigned(res); @@ -2960,7 +2989,9 @@ void Field_timestamp::sort_string(char *to,uint length __attribute__((unused))) void Field_timestamp::sql_type(String &res) const { - res.copy("timestamp", 9, my_charset_latin1, res.charset()); + CHARSET_INFO *cs=res.charset(); + uint len=cs->snprintf(cs,(char*)res.ptr(),res.alloced_length(),"timestamp"); + res.length(len); } @@ -3152,7 +3183,9 @@ void Field_time::sort_string(char *to,uint length __attribute__((unused))) void Field_time::sql_type(String &res) const { - res.copy("time",4,my_charset_latin1, res.charset()); + CHARSET_INFO *cs=res.charset(); + uint len=cs->snprintf(cs,(char*)res.ptr(),res.alloced_length(),"time"); + res.length(len); } /**************************************************************************** @@ -3242,9 +3275,10 @@ String *Field_year::val_str(String *val_buffer, void Field_year::sql_type(String &res) const { - ulong length=my_sprintf((char*) res.ptr(), - ((char*) res.ptr(),"year(%d)",(int) field_length)); - res.length(length); + CHARSET_INFO *cs=res.charset(); + ulong len=cs->snprintf(cs,(char*)res.ptr(),res.alloced_length(), + "year(%d)",(int) field_length); + res.length(len); } @@ -3414,7 +3448,9 @@ void Field_date::sort_string(char *to,uint length __attribute__((unused))) void Field_date::sql_type(String &res) const { - res.copy("date",4,my_charset_latin1, res.charset()); + CHARSET_INFO *cs=res.charset(); + uint len=cs->snprintf(cs,(char*)res.ptr(),res.alloced_length(),"date"); + res.length(len); } /**************************************************************************** @@ -3577,7 +3613,9 @@ void Field_newdate::sort_string(char *to,uint length __attribute__((unused))) void Field_newdate::sql_type(String &res) const { - res.copy("date",4,my_charset_latin1, res.charset()); + CHARSET_INFO *cs=res.charset(); + uint len=cs->snprintf(cs,(char*)res.ptr(),res.alloced_length(),"date"); + res.length(len); } @@ -3801,7 +3839,9 @@ void Field_datetime::sort_string(char *to,uint length __attribute__((unused))) void Field_datetime::sql_type(String &res) const { - res.copy("datetime",8,my_charset_latin1, res.charset()); + CHARSET_INFO *cs=res.charset(); + uint len=cs->snprintf(cs,(char*)res.ptr(),res.alloced_length(),"datetime"); + res.length(len); } /**************************************************************************** @@ -3930,13 +3970,14 @@ void Field_string::sort_string(char *to,uint length) void Field_string::sql_type(String &res) const { - ulong length= my_sprintf((char*) res.ptr(), - ((char*) res.ptr(), "%s(%d)", + CHARSET_INFO *cs=res.charset(); + ulong length= cs->snprintf(cs,(char*) res.ptr(), + res.alloced_length(), "%s(%d)", (field_length > 3 && (table->db_options_in_use & HA_OPTION_PACK_RECORD) ? "varchar" : "char"), - (int) field_length)); + (int) field_length); res.length(length); add_binary_or_charset(res); } @@ -4126,9 +4167,10 @@ void Field_varstring::sort_string(char *to,uint length) void Field_varstring::sql_type(String &res) const { - ulong length= my_sprintf((char*) res.ptr(), - ((char*) res.ptr(),"varchar(%u)", - field_length)); + CHARSET_INFO *cs=res.charset(); + ulong length= cs->snprintf(cs,(char*) res.ptr(), + res.alloced_length(),"varchar(%u)", + field_length); res.length(length); add_binary_or_charset(res); } @@ -4619,20 +4661,22 @@ void Field_blob::sort_string(char *to,uint length) void Field_blob::sql_type(String &res) const { + CHARSET_INFO *cs=res.charset(); const char *str; + uint len; switch (packlength) { default: str="tiny"; break; case 2: str=""; break; case 3: str="medium"; break; case 4: str="long"; break; } - res.set(str,(uint) strlen(str),my_charset_latin1); - res.append(binary() ? "blob" : "text"); - if (!binary()) - { - res.append(" character set "); - res.append(field_charset->name); - } + + len=cs->snprintf(cs,(char*)res.ptr(),res.alloced_length(),"%s%s%s%s", + str, + binary() ? "blob" : "text", + binary() ? "" : " character set ", + binary() ? "" : field_charset->name); + res.length(len); }