Skip to content

Commit 342d3df

Browse files
committed
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()".
1 parent 443b9a4 commit 342d3df

File tree

2 files changed

+72
-96
lines changed

2 files changed

+72
-96
lines changed

sql/field.cc

Lines changed: 24 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,7 @@ longlong Field::convert_decimal2longlong(const my_decimal *val,
20312031
!=0 error
20322032
*/
20332033

2034-
int Field_num::store_decimal(const my_decimal *val)
2034+
int Field_int::store_decimal(const my_decimal *val)
20352035
{
20362036
ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
20372037
int err= 0;
@@ -2054,17 +2054,16 @@ int Field_num::store_decimal(const my_decimal *val)
20542054
pointer to decimal buffer with value of field
20552055
*/
20562056

2057-
my_decimal* Field_num::val_decimal(my_decimal *decimal_value)
2057+
my_decimal* Field_int::val_decimal(my_decimal *decimal_value)
20582058
{
20592059
ASSERT_COLUMN_MARKED_FOR_READ;
2060-
DBUG_ASSERT(result_type() == INT_RESULT);
20612060
longlong nr= val_int();
20622061
int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
20632062
return decimal_value;
20642063
}
20652064

20662065

2067-
bool Field_num::get_date(MYSQL_TIME *ltime,ulonglong fuzzydate)
2066+
bool Field_int::get_date(MYSQL_TIME *ltime,ulonglong fuzzydate)
20682067
{
20692068
ASSERT_COLUMN_MARKED_FOR_READ;
20702069
longlong nr= val_int();
@@ -2283,7 +2282,7 @@ int Field::store_time_dec(const MYSQL_TIME *ltime, uint dec)
22832282
}
22842283

22852284

2286-
bool Field::optimize_range(uint idx, uint part)
2285+
bool Field::optimize_range(uint idx, uint part) const
22872286
{
22882287
return MY_TEST(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
22892288
}
@@ -3559,7 +3558,7 @@ Item *Field_new_decimal::get_equal_const_item(THD *thd, const Context &ctx,
35593558
}
35603559

35613560

3562-
int Field_num::store_time_dec(const MYSQL_TIME *ltime, uint dec_arg)
3561+
int Field_int::store_time_dec(const MYSQL_TIME *ltime, uint dec_arg)
35633562
{
35643563
longlong v= TIME_to_ulonglong(ltime);
35653564
if (ltime->neg == 0)
@@ -3694,24 +3693,8 @@ String *Field_tiny::val_str(String *val_buffer,
36943693
String *val_ptr __attribute__((unused)))
36953694
{
36963695
ASSERT_COLUMN_MARKED_FOR_READ;
3697-
CHARSET_INFO *cs= &my_charset_numeric;
3698-
uint length;
3699-
uint mlength=MY_MAX(field_length+1,5*cs->mbmaxlen);
3700-
val_buffer->alloc(mlength);
3701-
char *to=(char*) val_buffer->ptr();
3702-
3703-
if (unsigned_flag)
3704-
length= (uint) cs->cset->long10_to_str(cs,to,mlength, 10,
3705-
(long) *ptr);
3706-
else
3707-
length= (uint) cs->cset->long10_to_str(cs,to,mlength,-10,
3708-
(long) *((signed char*) ptr));
3709-
3710-
val_buffer->length(length);
3711-
if (zerofill)
3712-
prepend_zeros(val_buffer);
3713-
val_buffer->set_charset(cs);
3714-
return val_buffer;
3696+
long nr= unsigned_flag ? (long) ptr[0] : (long) ((signed char*) ptr)[0];
3697+
return val_str_from_long(val_buffer, 5, -10, nr);
37153698
}
37163699

37173700
bool Field_tiny::send_binary(Protocol *protocol)
@@ -3876,24 +3859,9 @@ String *Field_short::val_str(String *val_buffer,
38763859
String *val_ptr __attribute__((unused)))
38773860
{
38783861
ASSERT_COLUMN_MARKED_FOR_READ;
3879-
CHARSET_INFO *cs= &my_charset_numeric;
3880-
uint length;
3881-
uint mlength=MY_MAX(field_length+1,7*cs->mbmaxlen);
3882-
val_buffer->alloc(mlength);
3883-
char *to=(char*) val_buffer->ptr();
3884-
short j;
3885-
j=sint2korr(ptr);
3886-
3887-
if (unsigned_flag)
3888-
length=(uint) cs->cset->long10_to_str(cs, to, mlength, 10,
3889-
(long) (uint16) j);
3890-
else
3891-
length=(uint) cs->cset->long10_to_str(cs, to, mlength,-10, (long) j);
3892-
val_buffer->length(length);
3893-
if (zerofill)
3894-
prepend_zeros(val_buffer);
3895-
val_buffer->set_charset(cs);
3896-
return val_buffer;
3862+
short j= sint2korr(ptr);
3863+
long nr= unsigned_flag ? (long) (unsigned short) j : (long) j;
3864+
return val_str_from_long(val_buffer, 7, -10, nr);
38973865
}
38983866

38993867

@@ -4066,14 +4034,21 @@ String *Field_medium::val_str(String *val_buffer,
40664034
String *val_ptr __attribute__((unused)))
40674035
{
40684036
ASSERT_COLUMN_MARKED_FOR_READ;
4037+
long nr= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
4038+
return val_str_from_long(val_buffer, 10, -10, nr);
4039+
}
4040+
4041+
4042+
String *Field_int::val_str_from_long(String *val_buffer,
4043+
uint max_char_length,
4044+
int radix, long nr)
4045+
{
40694046
CHARSET_INFO *cs= &my_charset_numeric;
40704047
uint length;
4071-
uint mlength=MY_MAX(field_length+1,10*cs->mbmaxlen);
4048+
uint mlength= MY_MAX(field_length + 1, max_char_length * cs->mbmaxlen);
40724049
val_buffer->alloc(mlength);
40734050
char *to=(char*) val_buffer->ptr();
4074-
long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
4075-
4076-
length=(uint) cs->cset->long10_to_str(cs,to,mlength,-10,j);
4051+
length= (uint) cs->cset->long10_to_str(cs, to, mlength, radix, nr);
40774052
val_buffer->length(length);
40784053
if (zerofill)
40794054
prepend_zeros(val_buffer); /* purecov: inspected */
@@ -4251,27 +4226,13 @@ longlong Field_long::val_int(void)
42514226
return unsigned_flag ? (longlong) (uint32) j : (longlong) j;
42524227
}
42534228

4229+
42544230
String *Field_long::val_str(String *val_buffer,
42554231
String *val_ptr __attribute__((unused)))
42564232
{
42574233
ASSERT_COLUMN_MARKED_FOR_READ;
4258-
CHARSET_INFO *cs= &my_charset_numeric;
4259-
size_t length;
4260-
size_t mlength=MY_MAX(field_length+1,12*cs->mbmaxlen);
4261-
val_buffer->alloc(mlength);
4262-
char *to=(char*) val_buffer->ptr();
4263-
int32 j;
4264-
j=sint4korr(ptr);
4265-
4266-
if (unsigned_flag)
4267-
length=cs->cset->long10_to_str(cs,to,mlength, 10,(uint32) j);
4268-
else
4269-
length=cs->cset->long10_to_str(cs,to,mlength,-10,j);
4270-
val_buffer->length(length);
4271-
if (zerofill)
4272-
prepend_zeros(val_buffer);
4273-
val_buffer->set_charset(cs);
4274-
return val_buffer;
4234+
long nr= unsigned_flag ? (long) uint4korr(ptr) : sint4korr(ptr);
4235+
return val_str_from_long(val_buffer, 12, unsigned_flag ? 10 : -10, nr);
42754236
}
42764237

42774238

sql/field.h

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ class Field: public Value_source
12211221
void make_sort_key(uchar *buff, uint length);
12221222
virtual void make_field(Send_field *);
12231223
virtual void sort_string(uchar *buff,uint length)=0;
1224-
virtual bool optimize_range(uint idx, uint part);
1224+
virtual bool optimize_range(uint idx, uint part) const;
12251225
virtual void free() {}
12261226
virtual Field *make_new_field(MEM_ROOT *root, TABLE *new_table,
12271227
bool keep_type);
@@ -1678,9 +1678,6 @@ class Field_num :public Field {
16781678
!((flags & UNSIGNED_FLAG) && !(from->flags & UNSIGNED_FLAG)) &&
16791679
decimals() == from->decimals();
16801680
}
1681-
int store_decimal(const my_decimal *);
1682-
my_decimal *val_decimal(my_decimal *);
1683-
bool val_bool() { return val_int() != 0; }
16841681
uint is_equal(Create_field *new_field);
16851682
uint row_pack_length() const { return pack_length(); }
16861683
uint32 pack_length_from_metadata(uint field_metadata) {
@@ -1689,12 +1686,10 @@ class Field_num :public Field {
16891686
field_metadata, length));
16901687
return length;
16911688
}
1692-
int store_time_dec(const MYSQL_TIME *ltime, uint dec);
16931689
double pos_in_interval(Field *min, Field *max)
16941690
{
16951691
return pos_in_interval_val_real(min, max);
16961692
}
1697-
bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
16981693
};
16991694

17001695

@@ -1954,15 +1949,35 @@ class Field_new_decimal :public Field_num {
19541949
};
19551950

19561951

1957-
class Field_tiny :public Field_num {
1952+
class Field_int :public Field_num
1953+
{
1954+
protected:
1955+
String *val_str_from_long(String *val_buffer, uint max_char_length,
1956+
int radix, long nr);
1957+
public:
1958+
Field_int(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1959+
uchar null_bit_arg, enum utype unireg_check_arg,
1960+
const LEX_CSTRING *field_name_arg, bool zero_arg, bool unsigned_arg)
1961+
:Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1962+
unireg_check_arg, field_name_arg, 0, zero_arg, unsigned_arg)
1963+
{}
1964+
int store_decimal(const my_decimal *);
1965+
my_decimal *val_decimal(my_decimal *);
1966+
bool val_bool() { return val_int() != 0; }
1967+
int store_time_dec(const MYSQL_TIME *ltime, uint dec);
1968+
bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
1969+
};
1970+
1971+
1972+
class Field_tiny :public Field_int
1973+
{
19581974
public:
19591975
Field_tiny(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
19601976
uchar null_bit_arg,
19611977
enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
19621978
bool zero_arg, bool unsigned_arg)
1963-
:Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1964-
unireg_check_arg, field_name_arg,
1965-
0, zero_arg,unsigned_arg)
1979+
:Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1980+
unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
19661981
{}
19671982
const Type_handler *type_handler() const { return &type_handler_tiny; }
19681983
enum ha_base_keytype key_type() const
@@ -1998,21 +2013,21 @@ class Field_tiny :public Field_num {
19982013
};
19992014

20002015

2001-
class Field_short :public Field_num {
2016+
class Field_short :public Field_int
2017+
{
20022018
public:
20032019
Field_short(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
20042020
uchar null_bit_arg,
20052021
enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
20062022
bool zero_arg, bool unsigned_arg)
2007-
:Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2008-
unireg_check_arg, field_name_arg,
2009-
0, zero_arg,unsigned_arg)
2023+
:Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2024+
unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
20102025
{}
20112026
Field_short(uint32 len_arg,bool maybe_null_arg,
20122027
const LEX_CSTRING *field_name_arg,
20132028
bool unsigned_arg)
2014-
:Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
2015-
NONE, field_name_arg, 0, 0, unsigned_arg)
2029+
:Field_int((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
2030+
NONE, field_name_arg, 0, unsigned_arg)
20162031
{}
20172032
const Type_handler *type_handler() const { return &type_handler_short; }
20182033
enum ha_base_keytype key_type() const
@@ -2039,15 +2054,15 @@ class Field_short :public Field_num {
20392054
{ return unpack_int16(to, from, from_end); }
20402055
};
20412056

2042-
class Field_medium :public Field_num {
2057+
class Field_medium :public Field_int
2058+
{
20432059
public:
20442060
Field_medium(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
20452061
uchar null_bit_arg,
20462062
enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
20472063
bool zero_arg, bool unsigned_arg)
2048-
:Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2049-
unireg_check_arg, field_name_arg,
2050-
0, zero_arg,unsigned_arg)
2064+
:Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2065+
unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
20512066
{}
20522067
const Type_handler *type_handler() const { return &type_handler_int24; }
20532068
enum ha_base_keytype key_type() const
@@ -2073,21 +2088,21 @@ class Field_medium :public Field_num {
20732088
};
20742089

20752090

2076-
class Field_long :public Field_num {
2091+
class Field_long :public Field_int
2092+
{
20772093
public:
20782094
Field_long(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
20792095
uchar null_bit_arg,
20802096
enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
20812097
bool zero_arg, bool unsigned_arg)
2082-
:Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2083-
unireg_check_arg, field_name_arg,
2084-
0, zero_arg,unsigned_arg)
2098+
:Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2099+
unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
20852100
{}
20862101
Field_long(uint32 len_arg,bool maybe_null_arg,
20872102
const LEX_CSTRING *field_name_arg,
20882103
bool unsigned_arg)
2089-
:Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
2090-
NONE, field_name_arg,0,0,unsigned_arg)
2104+
:Field_int((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
2105+
NONE, field_name_arg, 0, unsigned_arg)
20912106
{}
20922107
const Type_handler *type_handler() const { return &type_handler_long; }
20932108
enum ha_base_keytype key_type() const
@@ -2119,21 +2134,21 @@ class Field_long :public Field_num {
21192134
};
21202135

21212136

2122-
class Field_longlong :public Field_num {
2137+
class Field_longlong :public Field_int
2138+
{
21232139
public:
21242140
Field_longlong(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
21252141
uchar null_bit_arg,
21262142
enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
21272143
bool zero_arg, bool unsigned_arg)
2128-
:Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2129-
unireg_check_arg, field_name_arg,
2130-
0, zero_arg,unsigned_arg)
2144+
:Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2145+
unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
21312146
{}
21322147
Field_longlong(uint32 len_arg,bool maybe_null_arg,
21332148
const LEX_CSTRING *field_name_arg,
21342149
bool unsigned_arg)
2135-
:Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
2136-
NONE, field_name_arg,0,0,unsigned_arg)
2150+
:Field_int((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
2151+
NONE, field_name_arg, 0, unsigned_arg)
21372152
{}
21382153
const Type_handler *type_handler() const { return &type_handler_longlong; }
21392154
enum ha_base_keytype key_type() const
@@ -3807,7 +3822,7 @@ class Field_enum :public Field_str {
38073822
{ return (field_metadata & 0x00ff); }
38083823
uint row_pack_length() const { return pack_length(); }
38093824
virtual bool zero_pack() const { return 0; }
3810-
bool optimize_range(uint idx, uint part) { return 0; }
3825+
bool optimize_range(uint idx, uint part) const { return 0; }
38113826
bool eq_def(const Field *field) const;
38123827
bool has_charset(void) const { return TRUE; }
38133828
/* enum and set are sorted as integers */

0 commit comments

Comments
 (0)