Commit 342d3df6 authored by Alexander Barkov's avatar Alexander Barkov

Cleanup: removing duplicate code, adding "const", etc

- Adding class Field_int as a common
  parent for Field_{longlong|long|short|medium|tiny}
- Moving store_decimal(), val_decimal(), get_date(), store_time_dec(),
  get_date(), val_bool() from Field_num to Field_int
- Adding Field_int::val_str_from_long() and reusing it in
  Field_tiny::val_str(), Field_short::val_str(), Field_medium::val_str()
  and Field_long::val_str(). This removes a good amount of duplicate code
- Adding "const" qualifier to "virtual bool Field::optimize_range()".
parent 443b9a41
......@@ -2031,7 +2031,7 @@ longlong Field::convert_decimal2longlong(const my_decimal *val,
!=0 error
*/
int Field_num::store_decimal(const my_decimal *val)
int Field_int::store_decimal(const my_decimal *val)
{
ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
int err= 0;
......@@ -2054,17 +2054,16 @@ int Field_num::store_decimal(const my_decimal *val)
pointer to decimal buffer with value of field
*/
my_decimal* Field_num::val_decimal(my_decimal *decimal_value)
my_decimal* Field_int::val_decimal(my_decimal *decimal_value)
{
ASSERT_COLUMN_MARKED_FOR_READ;
DBUG_ASSERT(result_type() == INT_RESULT);
longlong nr= val_int();
int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
return decimal_value;
}
bool Field_num::get_date(MYSQL_TIME *ltime,ulonglong fuzzydate)
bool Field_int::get_date(MYSQL_TIME *ltime,ulonglong fuzzydate)
{
ASSERT_COLUMN_MARKED_FOR_READ;
longlong nr= val_int();
......@@ -2283,7 +2282,7 @@ int Field::store_time_dec(const MYSQL_TIME *ltime, uint dec)
}
bool Field::optimize_range(uint idx, uint part)
bool Field::optimize_range(uint idx, uint part) const
{
return MY_TEST(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
}
......@@ -3559,7 +3558,7 @@ Item *Field_new_decimal::get_equal_const_item(THD *thd, const Context &ctx,
}
int Field_num::store_time_dec(const MYSQL_TIME *ltime, uint dec_arg)
int Field_int::store_time_dec(const MYSQL_TIME *ltime, uint dec_arg)
{
longlong v= TIME_to_ulonglong(ltime);
if (ltime->neg == 0)
......@@ -3694,24 +3693,8 @@ String *Field_tiny::val_str(String *val_buffer,
String *val_ptr __attribute__((unused)))
{
ASSERT_COLUMN_MARKED_FOR_READ;
CHARSET_INFO *cs= &my_charset_numeric;
uint length;
uint mlength=MY_MAX(field_length+1,5*cs->mbmaxlen);
val_buffer->alloc(mlength);
char *to=(char*) val_buffer->ptr();
if (unsigned_flag)
length= (uint) cs->cset->long10_to_str(cs,to,mlength, 10,
(long) *ptr);
else
length= (uint) cs->cset->long10_to_str(cs,to,mlength,-10,
(long) *((signed char*) ptr));
val_buffer->length(length);
if (zerofill)
prepend_zeros(val_buffer);
val_buffer->set_charset(cs);
return val_buffer;
long nr= unsigned_flag ? (long) ptr[0] : (long) ((signed char*) ptr)[0];
return val_str_from_long(val_buffer, 5, -10, nr);
}
bool Field_tiny::send_binary(Protocol *protocol)
......@@ -3876,24 +3859,9 @@ String *Field_short::val_str(String *val_buffer,
String *val_ptr __attribute__((unused)))
{
ASSERT_COLUMN_MARKED_FOR_READ;
CHARSET_INFO *cs= &my_charset_numeric;
uint length;
uint mlength=MY_MAX(field_length+1,7*cs->mbmaxlen);
val_buffer->alloc(mlength);
char *to=(char*) val_buffer->ptr();
short j;
j=sint2korr(ptr);
if (unsigned_flag)
length=(uint) cs->cset->long10_to_str(cs, to, mlength, 10,
(long) (uint16) j);
else
length=(uint) cs->cset->long10_to_str(cs, to, mlength,-10, (long) j);
val_buffer->length(length);
if (zerofill)
prepend_zeros(val_buffer);
val_buffer->set_charset(cs);
return val_buffer;
short j= sint2korr(ptr);
long nr= unsigned_flag ? (long) (unsigned short) j : (long) j;
return val_str_from_long(val_buffer, 7, -10, nr);
}
......@@ -4066,14 +4034,21 @@ String *Field_medium::val_str(String *val_buffer,
String *val_ptr __attribute__((unused)))
{
ASSERT_COLUMN_MARKED_FOR_READ;
long nr= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
return val_str_from_long(val_buffer, 10, -10, nr);
}
String *Field_int::val_str_from_long(String *val_buffer,
uint max_char_length,
int radix, long nr)
{
CHARSET_INFO *cs= &my_charset_numeric;
uint length;
uint mlength=MY_MAX(field_length+1,10*cs->mbmaxlen);
uint mlength= MY_MAX(field_length + 1, max_char_length * cs->mbmaxlen);
val_buffer->alloc(mlength);
char *to=(char*) val_buffer->ptr();
long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
length=(uint) cs->cset->long10_to_str(cs,to,mlength,-10,j);
length= (uint) cs->cset->long10_to_str(cs, to, mlength, radix, nr);
val_buffer->length(length);
if (zerofill)
prepend_zeros(val_buffer); /* purecov: inspected */
......@@ -4251,27 +4226,13 @@ longlong Field_long::val_int(void)
return unsigned_flag ? (longlong) (uint32) j : (longlong) j;
}
String *Field_long::val_str(String *val_buffer,
String *val_ptr __attribute__((unused)))
{
ASSERT_COLUMN_MARKED_FOR_READ;
CHARSET_INFO *cs= &my_charset_numeric;
size_t length;
size_t mlength=MY_MAX(field_length+1,12*cs->mbmaxlen);
val_buffer->alloc(mlength);
char *to=(char*) val_buffer->ptr();
int32 j;
j=sint4korr(ptr);
if (unsigned_flag)
length=cs->cset->long10_to_str(cs,to,mlength, 10,(uint32) j);
else
length=cs->cset->long10_to_str(cs,to,mlength,-10,j);
val_buffer->length(length);
if (zerofill)
prepend_zeros(val_buffer);
val_buffer->set_charset(cs);
return val_buffer;
long nr= unsigned_flag ? (long) uint4korr(ptr) : sint4korr(ptr);
return val_str_from_long(val_buffer, 12, unsigned_flag ? 10 : -10, nr);
}
......
......@@ -1221,7 +1221,7 @@ class Field: public Value_source
void make_sort_key(uchar *buff, uint length);
virtual void make_field(Send_field *);
virtual void sort_string(uchar *buff,uint length)=0;
virtual bool optimize_range(uint idx, uint part);
virtual bool optimize_range(uint idx, uint part) const;
virtual void free() {}
virtual Field *make_new_field(MEM_ROOT *root, TABLE *new_table,
bool keep_type);
......@@ -1678,9 +1678,6 @@ class Field_num :public Field {
!((flags & UNSIGNED_FLAG) && !(from->flags & UNSIGNED_FLAG)) &&
decimals() == from->decimals();
}
int store_decimal(const my_decimal *);
my_decimal *val_decimal(my_decimal *);
bool val_bool() { return val_int() != 0; }
uint is_equal(Create_field *new_field);
uint row_pack_length() const { return pack_length(); }
uint32 pack_length_from_metadata(uint field_metadata) {
......@@ -1689,12 +1686,10 @@ class Field_num :public Field {
field_metadata, length));
return length;
}
int store_time_dec(const MYSQL_TIME *ltime, uint dec);
double pos_in_interval(Field *min, Field *max)
{
return pos_in_interval_val_real(min, max);
}
bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
};
......@@ -1954,15 +1949,35 @@ class Field_new_decimal :public Field_num {
};
class Field_tiny :public Field_num {
class Field_int :public Field_num
{
protected:
String *val_str_from_long(String *val_buffer, uint max_char_length,
int radix, long nr);
public:
Field_int(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
uchar null_bit_arg, enum utype unireg_check_arg,
const LEX_CSTRING *field_name_arg, bool zero_arg, bool unsigned_arg)
:Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unireg_check_arg, field_name_arg, 0, zero_arg, unsigned_arg)
{}
int store_decimal(const my_decimal *);
my_decimal *val_decimal(my_decimal *);
bool val_bool() { return val_int() != 0; }
int store_time_dec(const MYSQL_TIME *ltime, uint dec);
bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
};
class Field_tiny :public Field_int
{
public:
Field_tiny(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
uchar null_bit_arg,
enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
bool zero_arg, bool unsigned_arg)
:Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unireg_check_arg, field_name_arg,
0, zero_arg,unsigned_arg)
:Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
{}
const Type_handler *type_handler() const { return &type_handler_tiny; }
enum ha_base_keytype key_type() const
......@@ -1998,21 +2013,21 @@ class Field_tiny :public Field_num {
};
class Field_short :public Field_num {
class Field_short :public Field_int
{
public:
Field_short(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
uchar null_bit_arg,
enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
bool zero_arg, bool unsigned_arg)
:Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unireg_check_arg, field_name_arg,
0, zero_arg,unsigned_arg)
:Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
{}
Field_short(uint32 len_arg,bool maybe_null_arg,
const LEX_CSTRING *field_name_arg,
bool unsigned_arg)
:Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
NONE, field_name_arg, 0, 0, unsigned_arg)
:Field_int((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
NONE, field_name_arg, 0, unsigned_arg)
{}
const Type_handler *type_handler() const { return &type_handler_short; }
enum ha_base_keytype key_type() const
......@@ -2039,15 +2054,15 @@ class Field_short :public Field_num {
{ return unpack_int16(to, from, from_end); }
};
class Field_medium :public Field_num {
class Field_medium :public Field_int
{
public:
Field_medium(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
uchar null_bit_arg,
enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
bool zero_arg, bool unsigned_arg)
:Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unireg_check_arg, field_name_arg,
0, zero_arg,unsigned_arg)
:Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
{}
const Type_handler *type_handler() const { return &type_handler_int24; }
enum ha_base_keytype key_type() const
......@@ -2073,21 +2088,21 @@ class Field_medium :public Field_num {
};
class Field_long :public Field_num {
class Field_long :public Field_int
{
public:
Field_long(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
uchar null_bit_arg,
enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
bool zero_arg, bool unsigned_arg)
:Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unireg_check_arg, field_name_arg,
0, zero_arg,unsigned_arg)
:Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
{}
Field_long(uint32 len_arg,bool maybe_null_arg,
const LEX_CSTRING *field_name_arg,
bool unsigned_arg)
:Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
NONE, field_name_arg,0,0,unsigned_arg)
:Field_int((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
NONE, field_name_arg, 0, unsigned_arg)
{}
const Type_handler *type_handler() const { return &type_handler_long; }
enum ha_base_keytype key_type() const
......@@ -2119,21 +2134,21 @@ class Field_long :public Field_num {
};
class Field_longlong :public Field_num {
class Field_longlong :public Field_int
{
public:
Field_longlong(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
uchar null_bit_arg,
enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
bool zero_arg, bool unsigned_arg)
:Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unireg_check_arg, field_name_arg,
0, zero_arg,unsigned_arg)
:Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
{}
Field_longlong(uint32 len_arg,bool maybe_null_arg,
const LEX_CSTRING *field_name_arg,
bool unsigned_arg)
:Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
NONE, field_name_arg,0,0,unsigned_arg)
:Field_int((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
NONE, field_name_arg, 0, unsigned_arg)
{}
const Type_handler *type_handler() const { return &type_handler_longlong; }
enum ha_base_keytype key_type() const
......@@ -3807,7 +3822,7 @@ class Field_enum :public Field_str {
{ return (field_metadata & 0x00ff); }
uint row_pack_length() const { return pack_length(); }
virtual bool zero_pack() const { return 0; }
bool optimize_range(uint idx, uint part) { return 0; }
bool optimize_range(uint idx, uint part) const { return 0; }
bool eq_def(const Field *field) const;
bool has_charset(void) const { return TRUE; }
/* enum and set are sorted as integers */
......
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