diff options
author | Monty <monty@mariadb.org> | 2020-07-16 16:30:06 +0300 |
---|---|---|
committer | Monty <monty@mariadb.org> | 2020-07-23 10:54:32 +0300 |
commit | 61c15ebe323d4d6f02fab86c405b2613e5784961 (patch) | |
tree | 1b8eccd555f01f8819594bbbc373aba55a2c4694 | |
parent | 26824581281fcb2b2251585d8081de38d561ca93 (diff) | |
download | mariadb-git-61c15ebe323d4d6f02fab86c405b2613e5784961.tar.gz |
Remove String::lex_string() and String::lex_cstring()
- Better to use 'String *' directly.
- Added String::get_value(LEX_STRING*) for the few cases where we want to
convert a String to LEX_CSTRING.
Other things:
- Use StringBuffer for some functions to avoid mallocs
-rw-r--r-- | sql/field.cc | 9 | ||||
-rw-r--r-- | sql/filesort.cc | 16 | ||||
-rw-r--r-- | sql/item.cc | 9 | ||||
-rw-r--r-- | sql/item.h | 6 | ||||
-rw-r--r-- | sql/item_strfunc.cc | 4 | ||||
-rw-r--r-- | sql/item_timefunc.cc | 7 | ||||
-rw-r--r-- | sql/protocol.cc | 5 | ||||
-rw-r--r-- | sql/protocol.h | 19 | ||||
-rw-r--r-- | sql/sp.cc | 6 | ||||
-rw-r--r-- | sql/sql_class.h | 3 | ||||
-rw-r--r-- | sql/sql_show.cc | 6 | ||||
-rw-r--r-- | sql/sql_string.h | 24 | ||||
-rw-r--r-- | sql/sql_type.cc | 4 | ||||
-rw-r--r-- | sql/sql_yacc.yy | 4 | ||||
-rw-r--r-- | sql/unireg.cc | 19 | ||||
-rw-r--r-- | sql/wsrep_mysqld.cc | 2 | ||||
-rw-r--r-- | sql/wsrep_schema.cc | 11 | ||||
-rw-r--r-- | storage/spider/spd_malloc.cc | 3 |
18 files changed, 85 insertions, 72 deletions
diff --git a/sql/field.cc b/sql/field.cc index a067baee460..c5d72abc526 100644 --- a/sql/field.cc +++ b/sql/field.cc @@ -1078,17 +1078,16 @@ Field_longstr::make_packed_sort_key_part(uchar *buff, *buff++=1; } uchar *end= pack_sort_string(buff, sort_field); - return static_cast<int>(end-buff); + return (uint) (end-buff); } uchar* Field_longstr::pack_sort_string(uchar *to, const SORT_FIELD_ATTR *sort_field) { - String buf; + StringBuffer<LONGLONG_BUFFER_SIZE> buf; val_str(&buf, &buf); - return to + sort_field->pack_sort_string(to, buf.lex_cstring(), - field_charset()); + return to + sort_field->pack_sort_string(to, &buf); } @@ -2106,7 +2105,7 @@ void Field::make_send_field(Send_field *field) field->org_table_name= field->db_name= empty_clex_str; if (orig_table && orig_table->alias.ptr()) { - field->table_name= orig_table->alias.lex_cstring(); + orig_table->alias.get_value(&field->table_name); field->org_col_name= field_name; } else diff --git a/sql/filesort.cc b/sql/filesort.cc index 7a3f16e0ee4..3f99bebd0cc 100644 --- a/sql/filesort.cc +++ b/sql/filesort.cc @@ -2546,7 +2546,6 @@ Type_handler_string_result::make_packed_sort_key_part(uchar *to, Item *item, const SORT_FIELD_ATTR *sort_field, Sort_param *param) const { - CHARSET_INFO *cs= item->collation.collation; bool maybe_null= item->maybe_null; if (maybe_null) @@ -2576,7 +2575,7 @@ Type_handler_string_result::make_packed_sort_key_part(uchar *to, Item *item, return sort_field->original_length; } } - return sort_field->pack_sort_string(to, res->lex_cstring(), cs); + return sort_field->pack_sort_string(to, res); } @@ -2932,13 +2931,12 @@ int compare_packed_sort_keys(void *sort_param, */ uint -SORT_FIELD_ATTR::pack_sort_string(uchar *to, const LEX_CSTRING &str, - CHARSET_INFO *cs) const +SORT_FIELD_ATTR::pack_sort_string(uchar *to, String *str) const { uchar *orig_to= to; uint32 length, data_length; - DBUG_ASSERT(str.length <= UINT32_MAX); - length= (uint32)str.length; + DBUG_ASSERT(str->length() <= UINT32_MAX); + length= (uint32) str->length(); if (length + suffix_length <= original_length) data_length= length; @@ -2949,13 +2947,13 @@ SORT_FIELD_ATTR::pack_sort_string(uchar *to, const LEX_CSTRING &str, store_key_part_length(data_length + suffix_length, to, length_bytes); to+= length_bytes; // copying data length bytes to the buffer - memcpy(to, (uchar*)str.str, data_length); + memcpy(to, (uchar*)str->ptr(), data_length); to+= data_length; - if (cs == &my_charset_bin && suffix_length) + if (str->charset() == &my_charset_bin && suffix_length) { // suffix length stored in bigendian form - store_bigendian(str.length, to, suffix_length); + store_bigendian(length, to, suffix_length); to+= suffix_length; } return static_cast<uint>(to - orig_to); diff --git a/sql/item.cc b/sql/item.cc index ca23fddff34..5c662e5e6ff 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -1394,7 +1394,7 @@ bool Item::get_date_from_real(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate bool Item::get_date_from_string(THD *thd, MYSQL_TIME *to, date_mode_t mode) { - StringBuffer<40> tmp; + StringBuffer<MAX_DATETIME_FULL_WIDTH+1> tmp; Temporal::Warn_push warn(thd, field_table_or_null(), field_name_or_null(), to, mode); Temporal_hybrid *t= new(to) Temporal_hybrid(thd, &warn, val_str(&tmp), mode); @@ -2076,7 +2076,7 @@ Item_name_const::Item_name_const(THD *thd, Item *name_arg, Item *val): Item::maybe_null= TRUE; if (name_item->basic_const_item() && (name_str= name_item->val_str(&name_buffer))) // Can't have a NULL name - set_name(thd, name_str->lex_cstring(), name_str->charset()); + set_name(thd, name_str); } @@ -6674,8 +6674,9 @@ int Item_string::save_in_field(Field *field, bool no_conversions) Item *Item_string::clone_item(THD *thd) { - return new (thd->mem_root) - Item_string(thd, name, str_value.lex_cstring(), collation.collation); + LEX_CSTRING val; + str_value.get_value(&val); + return new (thd->mem_root) Item_string(thd, name, val, collation.collation); } diff --git a/sql/item.h b/sql/item.h index 71d7549f72f..150d9cd215e 100644 --- a/sql/item.h +++ b/sql/item.h @@ -962,6 +962,10 @@ public: #endif } /*lint -e1509 */ void set_name(THD *thd, const char *str, size_t length, CHARSET_INFO *cs); + void set_name(THD *thd, String *str) + { + set_name(thd, str->ptr(), str->length(), str->charset()); + } void set_name(THD *thd, const LEX_CSTRING &str, CHARSET_INFO *cs= system_charset_info) { @@ -4377,7 +4381,7 @@ protected: const Metadata metadata) { fix_from_value(dv, metadata); - set_name(thd, str_value.lex_cstring(), str_value.charset()); + set_name(thd, &str_value); } protected: /* Just create an item and do not fill string representation */ diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index bcc041ae9c6..7f853a73c71 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -1391,8 +1391,8 @@ String *Item_func_regexp_replace::val_str(String *str) !(replace= re.convert_if_needed(replace, &re.replace_converter))) goto err; - src= source->lex_cstring(); - rpl= replace->lex_cstring(); + source->get_value(&src); + replace->get_value(&rpl); str->length(0); str->set_charset(collation.collation); diff --git a/sql/item_timefunc.cc b/sql/item_timefunc.cc index 3425b293193..53fc7efac06 100644 --- a/sql/item_timefunc.cc +++ b/sql/item_timefunc.cc @@ -473,7 +473,7 @@ err: Create a formated date/time value in a string. */ -static bool make_date_time(const LEX_CSTRING &format, MYSQL_TIME *l_time, +static bool make_date_time(String *format, MYSQL_TIME *l_time, timestamp_type type, const MY_LOCALE *locale, String *str) { @@ -488,7 +488,7 @@ static bool make_date_time(const LEX_CSTRING &format, MYSQL_TIME *l_time, if (l_time->neg) str->append('-'); - end= (ptr= format.str) + format.length; + end= (ptr= format->ptr()) + format->length(); for (; ptr != end ; ptr++) { if (*ptr != '%' || ptr+1 == end) @@ -1877,6 +1877,7 @@ String *Item_func_date_format::val_str(String *str) DBUG_ASSERT(fixed == 1); date_conv_mode_t mode= is_time_format ? TIME_TIME_ONLY : TIME_CONV_NONE; THD *thd= current_thd; + if ((null_value= args[0]->get_date(thd, &l_time, Temporal::Options(mode, thd)))) return 0; @@ -1901,7 +1902,7 @@ String *Item_func_date_format::val_str(String *str) /* Create the result string */ str->set_charset(collation.collation); - if (!make_date_time(format->lex_cstring(), &l_time, + if (!make_date_time(format, &l_time, is_time_format ? MYSQL_TIMESTAMP_TIME : MYSQL_TIMESTAMP_DATE, lc, str)) diff --git a/sql/protocol.cc b/sql/protocol.cc index a97e0d0997d..55373bcd2b5 100644 --- a/sql/protocol.cc +++ b/sql/protocol.cc @@ -864,12 +864,13 @@ bool Protocol_text::store_field_metadata(const THD * thd, { Send_field_packed_extended_metadata metadata; metadata.pack(field); + /* Don't apply character set conversion: extended metadata is a binary encoded data. */ - if (store_lex_cstring(metadata.lex_cstring(), cs, - MY_REPERTOIRE_UNICODE30, &my_charset_bin)) + if (store_binary_string(&metadata, cs, + MY_REPERTOIRE_UNICODE30)) return true; } if (packet->realloc(packet->length() + 12)) diff --git a/sql/protocol.h b/sql/protocol.h index 08f416fc9bf..18a01255708 100644 --- a/sql/protocol.h +++ b/sql/protocol.h @@ -149,18 +149,25 @@ public: // Various useful wrappers for the virtual store*() methods. // Backward wrapper for store_str() - bool store(const char *from, size_t length, CHARSET_INFO *cs, - my_repertoire_t repertoire= MY_REPERTOIRE_UNICODE30) + inline bool store(const char *from, size_t length, CHARSET_INFO *cs, + my_repertoire_t repertoire= MY_REPERTOIRE_UNICODE30) { return store_str(from, length, cs, repertoire, character_set_results()); } - bool store_lex_cstring(const LEX_CSTRING &s, - CHARSET_INFO *fromcs, - my_repertoire_t from_repertoire, - CHARSET_INFO *tocs) + inline bool store_lex_cstring(const LEX_CSTRING &s, + CHARSET_INFO *fromcs, + my_repertoire_t from_repertoire, + CHARSET_INFO *tocs) { return store_str(s.str, (uint) s.length, fromcs, from_repertoire, tocs); } + inline bool store_binary_string(Binary_string *str, + CHARSET_INFO *fromcs, + my_repertoire_t from_repertoire) + { + return store_str(str->ptr(), (uint) str->length(), fromcs, from_repertoire, + &my_charset_bin); + } bool store_ident(const LEX_CSTRING &s, my_repertoire_t repertoire= MY_REPERTOIRE_UNICODE30) { diff --git a/sql/sp.cc b/sql/sp.cc index 187e4b52394..3737bd11740 100644 --- a/sql/sp.cc +++ b/sql/sp.cc @@ -1286,7 +1286,7 @@ Sp_handler::sp_create_routine(THD *thd, const sp_head *sp) const if (type() == SP_TYPE_FUNCTION) { sp_returns_type(thd, retstr, sp); - returns= retstr.lex_cstring(); + retstr.get_value(&returns); } goto log; } @@ -1369,7 +1369,7 @@ Sp_handler::sp_create_routine(THD *thd, const sp_head *sp) const if (type() == SP_TYPE_FUNCTION) { sp_returns_type(thd, retstr, sp); - returns= retstr.lex_cstring(); + retstr.get_value(&returns); store_failed= store_failed || table->field[MYSQL_PROC_FIELD_RETURNS]-> @@ -2061,7 +2061,7 @@ Sp_handler::sp_clone_and_link_routine(THD *thd, if (type() == SP_TYPE_FUNCTION) { sp_returns_type(thd, retstr, sp); - returns= retstr.lex_cstring(); + retstr.get_value(&returns); } if (sp->m_parent) diff --git a/sql/sql_class.h b/sql/sql_class.h index 2e388ef7d72..36f98ce1a66 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -6447,8 +6447,7 @@ struct SORT_FIELD_ATTR */ bool maybe_null; CHARSET_INFO *cs; - uint pack_sort_string(uchar *to, const LEX_CSTRING &str, - CHARSET_INFO *cs) const; + uint pack_sort_string(uchar *to, String *str) const; int compare_packed_fixed_size_vals(uchar *a, size_t *a_len, uchar *b, size_t *b_len); int compare_packed_varstrings(uchar *a, size_t *a_len, diff --git a/sql/sql_show.cc b/sql/sql_show.cc index 64076197cf8..f72aa23a381 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -8215,7 +8215,7 @@ int make_schemata_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table) buffer.append(lex->wild->ptr()); buffer.append(')'); } - field->set_name(thd, buffer.lex_cstring()); + field->set_name(thd, &buffer); } return 0; } @@ -8224,7 +8224,7 @@ int make_schemata_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table) int make_table_names_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table) { char tmp[128]; - String buffer(tmp,sizeof(tmp), thd->charset()); + String buffer(tmp, sizeof(tmp), system_charset_info); LEX *lex= thd->lex; Name_resolution_context *context= &lex->first_select_lex()->context; ST_FIELD_INFO *field_info= &schema_table->fields_info[2]; @@ -8242,7 +8242,7 @@ int make_table_names_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table) Item_field *field= new (thd->mem_root) Item_field(thd, context, field_name); if (add_item_to_list(thd, field)) return 1; - field->set_name(thd, buffer.lex_cstring()); + field->set_name(thd, &buffer); if (thd->lex->verbose) { field_info= &schema_table->fields_info[3]; diff --git a/sql/sql_string.h b/sql/sql_string.h index 2d38f6d5d13..274b1d9a5df 100644 --- a/sql/sql_string.h +++ b/sql/sql_string.h @@ -222,18 +222,6 @@ public: inline bool is_empty() const { return (str_length == 0); } inline const char *ptr() const { return Ptr; } inline const char *end() const { return Ptr + str_length; } - - LEX_STRING lex_string() const - { - LEX_STRING str = { (char*) ptr(), length() }; - return str; - } - LEX_CSTRING lex_cstring() const - { - LEX_CSTRING skr = { ptr(), length() }; - return skr; - } - bool has_8bit_bytes() const { for (const char *c= ptr(), *c_end= end(); c < c_end; c++) @@ -488,6 +476,12 @@ public: if (str.Alloced_length) Alloced_length= (uint32) (str.Alloced_length - offset); } + inline LEX_CSTRING *get_value(LEX_CSTRING *res) + { + res->str= Ptr; + res->length= str_length; + return res; + } /* Take over handling of buffer from some other object */ void reset(char *ptr_arg, size_t length_arg, size_t alloced_length_arg) @@ -888,13 +882,13 @@ public: { return Binary_string::append_hex((const char*)src, srclen); } - bool append_introducer_and_hex(CHARSET_INFO *cs, const LEX_CSTRING &str) + bool append_introducer_and_hex(String *str) { return append(STRING_WITH_LEN("_")) || - append(cs->csname) || + append(str->charset()->csname) || append(STRING_WITH_LEN(" 0x")) || - append_hex(str.str, (uint32) str.length); + append_hex(str->ptr(), (uint32) str->length()); } bool append(IO_CACHE* file, uint32 arg_length) { diff --git a/sql/sql_type.cc b/sql/sql_type.cc index aee9b4c165c..7345b1fb529 100644 --- a/sql/sql_type.cc +++ b/sql/sql_type.cc @@ -8981,8 +8981,8 @@ bool Type_handler::partition_field_append_value( uint cnverr2= 0; buf2.copy(res->ptr(), res->length(), res->charset(), field_cs, &cnverr2); if (!cnverr2) - return str->append_introducer_and_hex(buf2.charset(), buf2.lex_cstring()); - return str->append_introducer_and_hex(res->charset(), res->lex_cstring()); + return str->append_introducer_and_hex(&buf2); + return str->append_introducer_and_hex(res); } StringBuffer<64> val(system_charset_info); diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index d8ad3aef7c3..b42d68c26e1 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -14698,13 +14698,15 @@ literal: | UNDERSCORE_CHARSET hex_or_bin_String { Item_string_with_introducer *item_str; + LEX_CSTRING tmp; + $2->get_value(&tmp); /* Pass NULL as name. Name will be set in the "select_item" rule and will include the introducer and the original hex/bin notation. */ item_str= new (thd->mem_root) Item_string_with_introducer(thd, null_clex_str, - $2->lex_cstring(), $1); + tmp, $1); if (unlikely(!item_str || !item_str->check_well_formed_result(true))) MYSQL_YYABORT; diff --git a/sql/unireg.cc b/sql/unireg.cc index 525e7a8a56a..ae860f0143b 100644 --- a/sql/unireg.cc +++ b/sql/unireg.cc @@ -86,6 +86,13 @@ static uchar* extra2_write_str(uchar *pos, const LEX_CSTRING &str) return pos + str.length; } +static uchar* extra2_write_str(uchar *pos, Binary_string *str) +{ + pos= extra2_write_len(pos, str->length()); + memcpy(pos, str->ptr(), str->length()); + return pos + str->length(); +} + static uchar *extra2_write(uchar *pos, enum extra2_frm_value_type type, const LEX_CSTRING &str) { @@ -178,11 +185,11 @@ class Field_data_type_info_image: public BinaryStringBuffer<512> { return net_store_length(pos, length); } - static uchar *store_string(uchar *pos, const LEX_CSTRING &str) + static uchar *store_string(uchar *pos, Binary_string *str) { - pos= store_length(pos, str.length); - memcpy(pos, str.str, str.length); - return pos + str.length; + pos= store_length(pos, str->length()); + memcpy(pos, str->ptr(), str->length()); + return pos + str->length(); } static uint store_length_required_length(ulonglong length) { @@ -206,7 +213,7 @@ public: return true; // Error uchar *pos= (uchar *) end(); pos= store_length(pos, fieldnr); - pos= store_string(pos, type_info.lex_cstring()); + pos= store_string(pos, &type_info); size_t new_length= (const char *) pos - ptr(); DBUG_ASSERT(new_length < alloced_length()); length((uint32) new_length); @@ -471,7 +478,7 @@ LEX_CUSTRING build_frm_image(THD *thd, const LEX_CSTRING &table, goto err; } *pos= EXTRA2_FIELD_DATA_TYPE_INFO; - pos= extra2_write_str(pos + 1, field_data_type_info_image.lex_cstring()); + pos= extra2_write_str(pos + 1, &field_data_type_info_image); } // PERIOD diff --git a/sql/wsrep_mysqld.cc b/sql/wsrep_mysqld.cc index 0c31631d19b..9a2d5e635a5 100644 --- a/sql/wsrep_mysqld.cc +++ b/sql/wsrep_mysqld.cc @@ -1968,7 +1968,7 @@ static int wsrep_create_sp(THD *thd, uchar** buf, size_t* buf_len) if (sp->m_handler->type() == SP_TYPE_FUNCTION) { sp_returns_type(thd, retstr, sp); - returns= retstr.lex_cstring(); + retstr.get_value(&returns); } if (sp->m_handler-> show_create_sp(thd, &log_query, diff --git a/sql/wsrep_schema.cc b/sql/wsrep_schema.cc index 80bc53b1790..7a3519d51f3 100644 --- a/sql/wsrep_schema.cc +++ b/sql/wsrep_schema.cc @@ -492,12 +492,11 @@ static int scan(TABLE* table, uint field, INTTYPE& val) static int scan(TABLE* table, uint field, char* strbuf, uint strbuf_len) { - String str; - (void)table->field[field]->val_str(&str); - LEX_CSTRING tmp= str.lex_cstring(); - uint len = tmp.length; - strncpy(strbuf, tmp.str, std::min(len, strbuf_len)); - strbuf[strbuf_len - 1]= '\0'; + uint len; + StringBuffer<STRING_BUFFER_USUAL_SIZE> str; + (void) table->field[field]->val_str(&str); + len= str.length(); + strmake(strbuf, str.ptr(), MY_MIN(len, strbuf_len-1)); return 0; } diff --git a/storage/spider/spd_malloc.cc b/storage/spider/spd_malloc.cc index 9f4203ae33d..a9438f2ac58 100644 --- a/storage/spider/spd_malloc.cc +++ b/storage/spider/spd_malloc.cc @@ -476,9 +476,10 @@ char *spider_string::c_ptr_safe() LEX_STRING spider_string::lex_string() const { + LEX_STRING res= { (char*) str.ptr(), str.length() }; DBUG_ENTER("spider_string::lex_string"); DBUG_PRINT("info",("spider this=%p", this)); - DBUG_RETURN(str.lex_string()); + DBUG_RETURN(res); } void spider_string::set( |