Skip to content

Commit 61c15eb

Browse files
committed
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
1 parent 2682458 commit 61c15eb

18 files changed

+85
-72
lines changed

sql/field.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,17 +1078,16 @@ Field_longstr::make_packed_sort_key_part(uchar *buff,
10781078
*buff++=1;
10791079
}
10801080
uchar *end= pack_sort_string(buff, sort_field);
1081-
return static_cast<int>(end-buff);
1081+
return (uint) (end-buff);
10821082
}
10831083

10841084

10851085
uchar*
10861086
Field_longstr::pack_sort_string(uchar *to, const SORT_FIELD_ATTR *sort_field)
10871087
{
1088-
String buf;
1088+
StringBuffer<LONGLONG_BUFFER_SIZE> buf;
10891089
val_str(&buf, &buf);
1090-
return to + sort_field->pack_sort_string(to, buf.lex_cstring(),
1091-
field_charset());
1090+
return to + sort_field->pack_sort_string(to, &buf);
10921091
}
10931092

10941093

@@ -2106,7 +2105,7 @@ void Field::make_send_field(Send_field *field)
21062105
field->org_table_name= field->db_name= empty_clex_str;
21072106
if (orig_table && orig_table->alias.ptr())
21082107
{
2109-
field->table_name= orig_table->alias.lex_cstring();
2108+
orig_table->alias.get_value(&field->table_name);
21102109
field->org_col_name= field_name;
21112110
}
21122111
else

sql/filesort.cc

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,7 +2546,6 @@ Type_handler_string_result::make_packed_sort_key_part(uchar *to, Item *item,
25462546
const SORT_FIELD_ATTR *sort_field,
25472547
Sort_param *param) const
25482548
{
2549-
CHARSET_INFO *cs= item->collation.collation;
25502549
bool maybe_null= item->maybe_null;
25512550

25522551
if (maybe_null)
@@ -2576,7 +2575,7 @@ Type_handler_string_result::make_packed_sort_key_part(uchar *to, Item *item,
25762575
return sort_field->original_length;
25772576
}
25782577
}
2579-
return sort_field->pack_sort_string(to, res->lex_cstring(), cs);
2578+
return sort_field->pack_sort_string(to, res);
25802579
}
25812580

25822581

@@ -2932,13 +2931,12 @@ int compare_packed_sort_keys(void *sort_param,
29322931
*/
29332932

29342933
uint
2935-
SORT_FIELD_ATTR::pack_sort_string(uchar *to, const LEX_CSTRING &str,
2936-
CHARSET_INFO *cs) const
2934+
SORT_FIELD_ATTR::pack_sort_string(uchar *to, String *str) const
29372935
{
29382936
uchar *orig_to= to;
29392937
uint32 length, data_length;
2940-
DBUG_ASSERT(str.length <= UINT32_MAX);
2941-
length= (uint32)str.length;
2938+
DBUG_ASSERT(str->length() <= UINT32_MAX);
2939+
length= (uint32) str->length();
29422940

29432941
if (length + suffix_length <= original_length)
29442942
data_length= length;
@@ -2949,13 +2947,13 @@ SORT_FIELD_ATTR::pack_sort_string(uchar *to, const LEX_CSTRING &str,
29492947
store_key_part_length(data_length + suffix_length, to, length_bytes);
29502948
to+= length_bytes;
29512949
// copying data length bytes to the buffer
2952-
memcpy(to, (uchar*)str.str, data_length);
2950+
memcpy(to, (uchar*)str->ptr(), data_length);
29532951
to+= data_length;
29542952

2955-
if (cs == &my_charset_bin && suffix_length)
2953+
if (str->charset() == &my_charset_bin && suffix_length)
29562954
{
29572955
// suffix length stored in bigendian form
2958-
store_bigendian(str.length, to, suffix_length);
2956+
store_bigendian(length, to, suffix_length);
29592957
to+= suffix_length;
29602958
}
29612959
return static_cast<uint>(to - orig_to);

sql/item.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,7 @@ bool Item::get_date_from_real(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate
13941394

13951395
bool Item::get_date_from_string(THD *thd, MYSQL_TIME *to, date_mode_t mode)
13961396
{
1397-
StringBuffer<40> tmp;
1397+
StringBuffer<MAX_DATETIME_FULL_WIDTH+1> tmp;
13981398
Temporal::Warn_push warn(thd, field_table_or_null(), field_name_or_null(),
13991399
to, mode);
14001400
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):
20762076
Item::maybe_null= TRUE;
20772077
if (name_item->basic_const_item() &&
20782078
(name_str= name_item->val_str(&name_buffer))) // Can't have a NULL name
2079-
set_name(thd, name_str->lex_cstring(), name_str->charset());
2079+
set_name(thd, name_str);
20802080
}
20812081

20822082

@@ -6674,8 +6674,9 @@ int Item_string::save_in_field(Field *field, bool no_conversions)
66746674

66756675
Item *Item_string::clone_item(THD *thd)
66766676
{
6677-
return new (thd->mem_root)
6678-
Item_string(thd, name, str_value.lex_cstring(), collation.collation);
6677+
LEX_CSTRING val;
6678+
str_value.get_value(&val);
6679+
return new (thd->mem_root) Item_string(thd, name, val, collation.collation);
66796680
}
66806681

66816682

sql/item.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,10 @@ class Item: public Value_source,
962962
#endif
963963
} /*lint -e1509 */
964964
void set_name(THD *thd, const char *str, size_t length, CHARSET_INFO *cs);
965+
void set_name(THD *thd, String *str)
966+
{
967+
set_name(thd, str->ptr(), str->length(), str->charset());
968+
}
965969
void set_name(THD *thd, const LEX_CSTRING &str,
966970
CHARSET_INFO *cs= system_charset_info)
967971
{
@@ -4377,7 +4381,7 @@ class Item_string :public Item_literal
43774381
const Metadata metadata)
43784382
{
43794383
fix_from_value(dv, metadata);
4380-
set_name(thd, str_value.lex_cstring(), str_value.charset());
4384+
set_name(thd, &str_value);
43814385
}
43824386
protected:
43834387
/* Just create an item and do not fill string representation */

sql/item_strfunc.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,8 +1391,8 @@ String *Item_func_regexp_replace::val_str(String *str)
13911391
!(replace= re.convert_if_needed(replace, &re.replace_converter)))
13921392
goto err;
13931393

1394-
src= source->lex_cstring();
1395-
rpl= replace->lex_cstring();
1394+
source->get_value(&src);
1395+
replace->get_value(&rpl);
13961396

13971397
str->length(0);
13981398
str->set_charset(collation.collation);

sql/item_timefunc.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ static bool extract_date_time(THD *thd, DATE_TIME_FORMAT *format,
473473
Create a formated date/time value in a string.
474474
*/
475475

476-
static bool make_date_time(const LEX_CSTRING &format, MYSQL_TIME *l_time,
476+
static bool make_date_time(String *format, MYSQL_TIME *l_time,
477477
timestamp_type type, const MY_LOCALE *locale,
478478
String *str)
479479
{
@@ -488,7 +488,7 @@ static bool make_date_time(const LEX_CSTRING &format, MYSQL_TIME *l_time,
488488
if (l_time->neg)
489489
str->append('-');
490490

491-
end= (ptr= format.str) + format.length;
491+
end= (ptr= format->ptr()) + format->length();
492492
for (; ptr != end ; ptr++)
493493
{
494494
if (*ptr != '%' || ptr+1 == end)
@@ -1877,6 +1877,7 @@ String *Item_func_date_format::val_str(String *str)
18771877
DBUG_ASSERT(fixed == 1);
18781878
date_conv_mode_t mode= is_time_format ? TIME_TIME_ONLY : TIME_CONV_NONE;
18791879
THD *thd= current_thd;
1880+
18801881
if ((null_value= args[0]->get_date(thd, &l_time,
18811882
Temporal::Options(mode, thd))))
18821883
return 0;
@@ -1901,7 +1902,7 @@ String *Item_func_date_format::val_str(String *str)
19011902

19021903
/* Create the result string */
19031904
str->set_charset(collation.collation);
1904-
if (!make_date_time(format->lex_cstring(), &l_time,
1905+
if (!make_date_time(format, &l_time,
19051906
is_time_format ? MYSQL_TIMESTAMP_TIME :
19061907
MYSQL_TIMESTAMP_DATE,
19071908
lc, str))

sql/protocol.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -864,12 +864,13 @@ bool Protocol_text::store_field_metadata(const THD * thd,
864864
{
865865
Send_field_packed_extended_metadata metadata;
866866
metadata.pack(field);
867+
867868
/*
868869
Don't apply character set conversion:
869870
extended metadata is a binary encoded data.
870871
*/
871-
if (store_lex_cstring(metadata.lex_cstring(), cs,
872-
MY_REPERTOIRE_UNICODE30, &my_charset_bin))
872+
if (store_binary_string(&metadata, cs,
873+
MY_REPERTOIRE_UNICODE30))
873874
return true;
874875
}
875876
if (packet->realloc(packet->length() + 12))

sql/protocol.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,25 @@ class Protocol
149149

150150
// Various useful wrappers for the virtual store*() methods.
151151
// Backward wrapper for store_str()
152-
bool store(const char *from, size_t length, CHARSET_INFO *cs,
153-
my_repertoire_t repertoire= MY_REPERTOIRE_UNICODE30)
152+
inline bool store(const char *from, size_t length, CHARSET_INFO *cs,
153+
my_repertoire_t repertoire= MY_REPERTOIRE_UNICODE30)
154154
{
155155
return store_str(from, length, cs, repertoire, character_set_results());
156156
}
157-
bool store_lex_cstring(const LEX_CSTRING &s,
158-
CHARSET_INFO *fromcs,
159-
my_repertoire_t from_repertoire,
160-
CHARSET_INFO *tocs)
157+
inline bool store_lex_cstring(const LEX_CSTRING &s,
158+
CHARSET_INFO *fromcs,
159+
my_repertoire_t from_repertoire,
160+
CHARSET_INFO *tocs)
161161
{
162162
return store_str(s.str, (uint) s.length, fromcs, from_repertoire, tocs);
163163
}
164+
inline bool store_binary_string(Binary_string *str,
165+
CHARSET_INFO *fromcs,
166+
my_repertoire_t from_repertoire)
167+
{
168+
return store_str(str->ptr(), (uint) str->length(), fromcs, from_repertoire,
169+
&my_charset_bin);
170+
}
164171
bool store_ident(const LEX_CSTRING &s,
165172
my_repertoire_t repertoire= MY_REPERTOIRE_UNICODE30)
166173
{

sql/sp.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ Sp_handler::sp_create_routine(THD *thd, const sp_head *sp) const
12861286
if (type() == SP_TYPE_FUNCTION)
12871287
{
12881288
sp_returns_type(thd, retstr, sp);
1289-
returns= retstr.lex_cstring();
1289+
retstr.get_value(&returns);
12901290
}
12911291
goto log;
12921292
}
@@ -1369,7 +1369,7 @@ Sp_handler::sp_create_routine(THD *thd, const sp_head *sp) const
13691369
if (type() == SP_TYPE_FUNCTION)
13701370
{
13711371
sp_returns_type(thd, retstr, sp);
1372-
returns= retstr.lex_cstring();
1372+
retstr.get_value(&returns);
13731373

13741374
store_failed= store_failed ||
13751375
table->field[MYSQL_PROC_FIELD_RETURNS]->
@@ -2061,7 +2061,7 @@ Sp_handler::sp_clone_and_link_routine(THD *thd,
20612061
if (type() == SP_TYPE_FUNCTION)
20622062
{
20632063
sp_returns_type(thd, retstr, sp);
2064-
returns= retstr.lex_cstring();
2064+
retstr.get_value(&returns);
20652065
}
20662066

20672067
if (sp->m_parent)

sql/sql_class.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6447,8 +6447,7 @@ struct SORT_FIELD_ATTR
64476447
*/
64486448
bool maybe_null;
64496449
CHARSET_INFO *cs;
6450-
uint pack_sort_string(uchar *to, const LEX_CSTRING &str,
6451-
CHARSET_INFO *cs) const;
6450+
uint pack_sort_string(uchar *to, String *str) const;
64526451
int compare_packed_fixed_size_vals(uchar *a, size_t *a_len,
64536452
uchar *b, size_t *b_len);
64546453
int compare_packed_varstrings(uchar *a, size_t *a_len,

0 commit comments

Comments
 (0)