From 093d62922b326cec1a05bd8baba2d9ed96137488 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 25 May 2004 02:03:49 +0400 Subject: Support for character set conversion in binary protocol: another go after Monty's review. - Item_param was rewritten. - it turns out that we can't convert string data to character set of connection on the fly, because they first should be written to the binary log. To support efficient conversion we need to rewrite prepared statements binlogging code first. include/my_global.h: Macro swap(a, b, c) was renamed to resolve name conflict with String::swap() method. include/my_sys.h: Added declaration of escape_string_for_mysql() include/mysql_com.h: Removed and moved back: a macro which is visible to libmysql user but has sence only in prepared statement protocol implementation. isam/_search.c: swap -> swap_variables isam/test2.c: swap -> swap_variables libmysql/libmysql.c: - sub_escape_string moved to mysys/charset.c to be visible in sql/ - few cleanups myisam/mi_test2.c: swap -> swap_variables mysys/charset.c: sub_escape_string was moved from libmysql.c to be able to use it in sql/ code. mysys/my_chsize.c: rename: swap -> swap_variables mysys/my_compress.c: swap -> swap_variables mysys/my_handler.c: swap -> swap_variables sql/field.cc: Field::store_time refactored to use TIME_to_string function from time.cc sql/item.cc: New implementation of Item_param class: added support for character sets conversion. sql/item.h: Item_param: - 'state' member introduced instead of many boolean variables. - put ltime, int_value and real_value into union to save space. - remove unimplemented members - set_value renamed to set_str sql/item_timefunc.cc: Refactored to use functions from time.cc sql/lock.cc: rename: swap -> swap_variables sql/mysql_priv.h: - added declarations for TIME_to_ulonglong_*, TIME_to_string functions - const specifiers for make_date, make_time, make_datetime arguments sql/opt_range.cc: rename: swap -> swap_variables sql/protocol.cc: - added character set conversion support to binary protocol. - Protocol::convert changed to point at shared buffer in THD. This lets us use one convert buffer for binary and simple protocol. The same buffer is used for client->server conversions in prepared statements code. - string conversion code refactored to Protocol::store_string_aux function. - few more comments sql/protocol.h: - Protocol::convert now points at THD::convert_buffer: we want to share one buffer between all protocol implementations. sql/sql_class.cc: - implementation of THD::convert_string using THD::convert_buffer (conversion of strings allocated in the system heap). sql/sql_class.h: - THD::convert_buffer is shared between THD and network Protocols and used for character set conversion of strings. - new function to convert String object from one charset to another using THD::convert_buffer sql/sql_insert.cc: A little fix in a comment. sql/sql_parse.cc: Shrink convert buffer in the end of each statement. sql/sql_prepare.cc: Many changes: - static specifier for set_param_* family of functions. - FIELD_TYPE -> MYSQL_TYPE - added set_param_binary as handler for BLOB types. - added character set support - added support for param typecode in mysql_stmt_get_longdata (mysql_stmt_send_long_data handler) - changes in Item_param deployed - few cleanups sql/sql_select.cc: rename: swap -> swap_variables sql/sql_string.cc: - String::append rewritten to support character set conversion for single-byte encodings. - added String::swap method to efficiently exchange two string objects. sql/sql_string.h: Declraration for String::swap(). sql/time.cc: - function TIME_to_string to convert TIME to String in default MySQL format - family of functions TIME_to_ulonglong_* tests/client_test.c: Test for support for character set conversions in prepared statements (binary and text data). --- sql/sql_string.cc | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) (limited to 'sql/sql_string.cc') diff --git a/sql/sql_string.cc b/sql/sql_string.cc index 8a093738e2b..fb2d1661357 100644 --- a/sql/sql_string.cc +++ b/sql/sql_string.cc @@ -450,22 +450,25 @@ bool String::append(const char *s,uint32 arg_length) bool String::append(const char *s,uint32 arg_length, CHARSET_INFO *cs) { - if (!arg_length) // Default argument - if (!(arg_length= (uint32) strlen(s))) + uint32 dummy_offset; + uint32 add_length; + + if (!arg_length && !(arg_length= (uint32)strlen(s))) return FALSE; - if (cs != str_charset && str_charset->mbmaxlen > 1) + + add_length= arg_length * str_charset->mbmaxlen; + if (realloc(str_length + add_length)) + return TRUE; + if (needs_conversion(arg_length, cs, str_charset, &dummy_offset)) { - uint32 add_length=arg_length * str_charset->mbmaxlen; - if (realloc(str_length+ add_length)) - return TRUE; str_length+= copy_and_convert(Ptr+str_length, add_length, str_charset, s, arg_length, cs); - return FALSE; } - if (realloc(str_length+arg_length)) - return TRUE; - memcpy(Ptr+str_length,s,arg_length); - str_length+=arg_length; + else + { + memcpy(Ptr + str_length, s, arg_length); + str_length+= arg_length; + } return FALSE; } @@ -858,3 +861,23 @@ void String::print(String *str) } } } + + +/* + Exchange state of this object and argument. + + SYNOPSIS + String::swap() + + RETURN + Target string will contain state of this object and vice versa. +*/ + +void String::swap(String &s) +{ + swap_variables(char *, Ptr, s.Ptr); + swap_variables(uint32, str_length, s.str_length); + swap_variables(uint32, Alloced_length, s.Alloced_length); + swap_variables(bool, alloced, s.alloced); + swap_variables(CHARSET_INFO*, str_charset, s.str_charset); +} -- cgit v1.2.1 From 9983aed876568891e07fbdeed402c9c7f1320c03 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 25 May 2004 15:54:03 +0500 Subject: 1. Some optimization when conversion is not needed. 2. One now must pass length argument into append(const char *str, uint length), length is not calculated internally anymore. --- sql/sql_string.cc | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) (limited to 'sql/sql_string.cc') diff --git a/sql/sql_string.cc b/sql/sql_string.cc index fb2d1661357..b60aee2b5df 100644 --- a/sql/sql_string.cc +++ b/sql/sql_string.cc @@ -416,16 +416,18 @@ bool String::append(const String &s) /* - Append a latin1 string to the a string of the current character set + Append an ASCII string to the a string of the current character set */ - bool String::append(const char *s,uint32 arg_length) { - if (!arg_length) // Default argument - if (!(arg_length= (uint32) strlen(s))) - return FALSE; - if (str_charset->mbmaxlen > 1) + if (!arg_length) + return FALSE; + + /* + For an ASCII incompatible string, e.g. UCS-2, we need to convert + */ + if (str_charset->mbminlen > 1) { uint32 add_length=arg_length * str_charset->mbmaxlen; if (realloc(str_length+ add_length)) @@ -434,6 +436,10 @@ bool String::append(const char *s,uint32 arg_length) s, arg_length, &my_charset_latin1); return FALSE; } + + /* + For an ASCII compatinble string we can just append. + */ if (realloc(str_length+arg_length)) return TRUE; memcpy(Ptr+str_length,s,arg_length); @@ -442,30 +448,37 @@ bool String::append(const char *s,uint32 arg_length) } +/* + Append a 0-terminated ASCII string +*/ + +bool String::append(const char *s) +{ + return append(s, strlen(s)); +} + + /* Append a string in the given charset to the string with character set recoding */ - bool String::append(const char *s,uint32 arg_length, CHARSET_INFO *cs) { uint32 dummy_offset; - uint32 add_length; - - if (!arg_length && !(arg_length= (uint32)strlen(s))) - return FALSE; - - add_length= arg_length * str_charset->mbmaxlen; - if (realloc(str_length + add_length)) - return TRUE; + if (needs_conversion(arg_length, cs, str_charset, &dummy_offset)) { + uint32 add_length= arg_length / cs->mbminlen * str_charset->mbmaxlen; + if (realloc(str_length + add_length)) + return TRUE; str_length+= copy_and_convert(Ptr+str_length, add_length, str_charset, s, arg_length, cs); } else { + if (realloc(str_length + arg_length)) + return TRUE; memcpy(Ptr + str_length, s, arg_length); str_length+= arg_length; } -- cgit v1.2.1 From 6c09db274773a36350b8a3f794a847684bd0ca30 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 25 May 2004 16:05:33 +0500 Subject: sql_string.h: Not used code. , sql_string.cc: Not used code. sql/sql_string.cc: Not used code. sql/sql_string.h: Not used code. , --- sql/sql_string.cc | 34 ---------------------------------- 1 file changed, 34 deletions(-) (limited to 'sql/sql_string.cc') diff --git a/sql/sql_string.cc b/sql/sql_string.cc index b60aee2b5df..991fb4d5c5a 100644 --- a/sql/sql_string.cc +++ b/sql/sql_string.cc @@ -569,40 +569,6 @@ skip: return -1; } -/* - Search after a string without regarding to case - This needs to be replaced when we have character sets per string -*/ - -int String::strstr_case(const String &s,uint32 offset) -{ - if (s.length()+offset <= str_length) - { - if (!s.length()) - return ((int) offset); // Empty string is always found - - register const char *str = Ptr+offset; - register const char *search=s.ptr(); - const char *end=Ptr+str_length-s.length()+1; - const char *search_end=s.ptr()+s.length(); -skip: - while (str != end) - { - if (str_charset->sort_order[*str++] == str_charset->sort_order[*search]) - { - register char *i,*j; - i=(char*) str; j=(char*) search+1; - while (j != search_end) - if (str_charset->sort_order[*i++] != - str_charset->sort_order[*j++]) - goto skip; - return (int) (str-Ptr) -1; - } - } - } - return -1; -} - /* ** Search string from end. Offset is offset to the end of string */ -- cgit v1.2.1