Commit b3bc02f9 authored by Monty's avatar Monty Committed by Sergei Golubchik

Added ErrConvString.lex_cstring() to simplify code

This allows us to use String::append() without using strlen().

The changes to the ErrConvString class where done by Alexander Barkov
parent 5c7d243b
...@@ -3816,7 +3816,7 @@ void Item_string::print(String *str, enum_query_type query_type) ...@@ -3816,7 +3816,7 @@ void Item_string::print(String *str, enum_query_type query_type)
changed. changed.
*/ */
ErrConvString tmp(str_value.ptr(), str_value.length(), &my_charset_bin); ErrConvString tmp(str_value.ptr(), str_value.length(), &my_charset_bin);
str->append(tmp.ptr()); str->append(tmp.lex_cstring());
} }
else else
{ {
......
...@@ -393,7 +393,7 @@ void field_unpack(String *to, Field *field, const uchar *rec, uint max_length, ...@@ -393,7 +393,7 @@ void field_unpack(String *to, Field *field, const uchar *rec, uint max_length,
if (max_length < field->pack_length()) if (max_length < field->pack_length())
tmp.length(MY_MIN(tmp.length(),max_length)); tmp.length(MY_MIN(tmp.length(),max_length));
ErrConvString err(&tmp); ErrConvString err(&tmp);
to->append(err.ptr()); to->append(err.lex_cstring());
} }
else else
to->append(STRING_WITH_LEN("???")); to->append(STRING_WITH_LEN("???"));
......
...@@ -7558,10 +7558,10 @@ class ErrConvDQName: public ErrConv ...@@ -7558,10 +7558,10 @@ class ErrConvDQName: public ErrConv
ErrConvDQName(const Database_qualified_name *name) ErrConvDQName(const Database_qualified_name *name)
:m_name(name) :m_name(name)
{ } { }
const char *ptr() const LEX_CSTRING lex_cstring() const override
{ {
m_name->make_qname(err_buffer, sizeof(err_buffer)); size_t length= m_name->make_qname(err_buffer, sizeof(err_buffer));
return err_buffer; return {err_buffer, length};
} }
}; };
......
...@@ -864,11 +864,11 @@ extern "C" int my_wc_mb_utf8_null_terminated(CHARSET_INFO *cs, ...@@ -864,11 +864,11 @@ extern "C" int my_wc_mb_utf8_null_terminated(CHARSET_INFO *cs,
@param from_cs charset from convert @param from_cs charset from convert
@retval @retval
result string result string length
*/ */
char *err_conv(char *buff, uint to_length, const char *from, size_t err_conv(char *buff, uint to_length, const char *from,
uint from_length, CHARSET_INFO *from_cs) uint from_length, CHARSET_INFO *from_cs)
{ {
char *to= buff; char *to= buff;
const char *from_start= from; const char *from_start= from;
...@@ -919,7 +919,7 @@ char *err_conv(char *buff, uint to_length, const char *from, ...@@ -919,7 +919,7 @@ char *err_conv(char *buff, uint to_length, const char *from,
&errors); &errors);
to[res]= 0; to[res]= 0;
} }
return buff; return res;
} }
......
...@@ -811,8 +811,8 @@ class Warning_info ...@@ -811,8 +811,8 @@ class Warning_info
}; };
extern char *err_conv(char *buff, uint to_length, const char *from, extern size_t err_conv(char *buff, uint to_length, const char *from,
uint from_length, CHARSET_INFO *from_cs); uint from_length, CHARSET_INFO *from_cs);
class ErrBuff class ErrBuff
{ {
...@@ -824,31 +824,37 @@ class ErrBuff ...@@ -824,31 +824,37 @@ class ErrBuff
err_buffer[0]= '\0'; err_buffer[0]= '\0';
} }
const char *ptr() const { return err_buffer; } const char *ptr() const { return err_buffer; }
const char *set_longlong(const Longlong_hybrid &nr) const LEX_CSTRING set_longlong(const Longlong_hybrid &nr) const
{ {
return nr.is_unsigned() ? ullstr(nr.value(), err_buffer) : int radix= nr.is_unsigned() ? 10 : -10;
llstr(nr.value(), err_buffer); const char *end= longlong10_to_str(nr.value(), err_buffer, radix);
DBUG_ASSERT(end >= err_buffer);
return {err_buffer, (size_t) (end - err_buffer)};
} }
const char *set_double(double nr) const LEX_CSTRING set_double(double nr) const
{ {
my_gcvt(nr, MY_GCVT_ARG_DOUBLE, sizeof(err_buffer), err_buffer, 0); size_t length= my_gcvt(nr, MY_GCVT_ARG_DOUBLE,
return err_buffer; sizeof(err_buffer), err_buffer, 0);
return {err_buffer, length};
} }
const char *set_decimal(const decimal_t *d) const LEX_CSTRING set_decimal(const decimal_t *d) const
{ {
int len= sizeof(err_buffer); int length= sizeof(err_buffer);
decimal2string(d, err_buffer, &len, 0, 0, ' '); decimal2string(d, err_buffer, &length, 0, 0, ' ');
return err_buffer; DBUG_ASSERT(length >= 0);
return {err_buffer, (size_t) length};
} }
const char *set_str(const char *str, size_t len, CHARSET_INFO *cs) const LEX_CSTRING set_str(const char *str, size_t len, CHARSET_INFO *cs) const
{ {
DBUG_ASSERT(len < UINT_MAX32); DBUG_ASSERT(len < UINT_MAX32);
return err_conv(err_buffer, (uint) sizeof(err_buffer), str, (uint) len, cs); len= err_conv(err_buffer, (uint) sizeof(err_buffer), str, (uint) len, cs);
return {err_buffer, len};
} }
const char *set_mysql_time(const MYSQL_TIME *ltime) const LEX_CSTRING set_mysql_time(const MYSQL_TIME *ltime) const
{ {
my_TIME_to_str(ltime, err_buffer, AUTO_SEC_PART_DIGITS); int length= my_TIME_to_str(ltime, err_buffer, AUTO_SEC_PART_DIGITS);
return err_buffer; DBUG_ASSERT(length >= 0);
return {err_buffer, (size_t) length};
} }
}; };
...@@ -858,7 +864,11 @@ class ErrConv: public ErrBuff ...@@ -858,7 +864,11 @@ class ErrConv: public ErrBuff
public: public:
ErrConv() {} ErrConv() {}
virtual ~ErrConv() {} virtual ~ErrConv() {}
virtual const char *ptr() const = 0; virtual LEX_CSTRING lex_cstring() const= 0;
inline const char *ptr() const
{
return lex_cstring().str;
}
}; };
class ErrConvString : public ErrConv class ErrConvString : public ErrConv
...@@ -873,7 +883,7 @@ class ErrConvString : public ErrConv ...@@ -873,7 +883,7 @@ class ErrConvString : public ErrConv
: ErrConv(), str(str_arg), len(strlen(str_arg)), cs(cs_arg) {} : ErrConv(), str(str_arg), len(strlen(str_arg)), cs(cs_arg) {}
ErrConvString(const String *s) ErrConvString(const String *s)
: ErrConv(), str(s->ptr()), len(s->length()), cs(s->charset()) {} : ErrConv(), str(s->ptr()), len(s->length()), cs(s->charset()) {}
const char *ptr() const LEX_CSTRING lex_cstring() const override
{ {
return set_str(str, len, cs); return set_str(str, len, cs);
} }
...@@ -884,7 +894,7 @@ class ErrConvInteger : public ErrConv, public Longlong_hybrid ...@@ -884,7 +894,7 @@ class ErrConvInteger : public ErrConv, public Longlong_hybrid
public: public:
ErrConvInteger(const Longlong_hybrid &nr) ErrConvInteger(const Longlong_hybrid &nr)
: ErrConv(), Longlong_hybrid(nr) { } : ErrConv(), Longlong_hybrid(nr) { }
const char *ptr() const LEX_CSTRING lex_cstring() const override
{ {
return set_longlong(static_cast<Longlong_hybrid>(*this)); return set_longlong(static_cast<Longlong_hybrid>(*this));
} }
...@@ -895,7 +905,7 @@ class ErrConvDouble: public ErrConv ...@@ -895,7 +905,7 @@ class ErrConvDouble: public ErrConv
double num; double num;
public: public:
ErrConvDouble(double num_arg) : ErrConv(), num(num_arg) {} ErrConvDouble(double num_arg) : ErrConv(), num(num_arg) {}
const char *ptr() const LEX_CSTRING lex_cstring() const override
{ {
return set_double(num); return set_double(num);
} }
...@@ -906,7 +916,7 @@ class ErrConvTime : public ErrConv ...@@ -906,7 +916,7 @@ class ErrConvTime : public ErrConv
const MYSQL_TIME *ltime; const MYSQL_TIME *ltime;
public: public:
ErrConvTime(const MYSQL_TIME *ltime_arg) : ErrConv(), ltime(ltime_arg) {} ErrConvTime(const MYSQL_TIME *ltime_arg) : ErrConv(), ltime(ltime_arg) {}
const char *ptr() const LEX_CSTRING lex_cstring() const override
{ {
return set_mysql_time(ltime); return set_mysql_time(ltime);
} }
...@@ -917,7 +927,7 @@ class ErrConvDecimal : public ErrConv ...@@ -917,7 +927,7 @@ class ErrConvDecimal : public ErrConv
const decimal_t *d; const decimal_t *d;
public: public:
ErrConvDecimal(const decimal_t *d_arg) : ErrConv(), d(d_arg) {} ErrConvDecimal(const decimal_t *d_arg) : ErrConv(), d(d_arg) {}
const char *ptr() const LEX_CSTRING lex_cstring() const override
{ {
return set_decimal(d); return set_decimal(d);
} }
......
...@@ -3480,11 +3480,11 @@ bool ha_connect::get_error_message(int error, String* buf) ...@@ -3480,11 +3480,11 @@ bool ha_connect::get_error_message(int error, String* buf)
if (trace(1)) if (trace(1))
htrc("GEM(%d): %s\n", error, g->Message); htrc("GEM(%d): %s\n", error, g->Message);
buf->append(ErrConvString(g->Message,
buf->append(ErrConvString(g->Message, strlen(g->Message), strlen(g->Message),
&my_charset_latin1).ptr()); &my_charset_latin1).lex_cstring());
} else } else
buf->append("Cannot retrieve error message"); buf->append(STRING_WITH_LEN("Cannot retrieve error message"));
DBUG_RETURN(false); DBUG_RETURN(false);
} // end of get_error_message } // end of get_error_message
......
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