Skip to content

Commit ecb009b

Browse files
author
Alexander Barkov
committed
Adding Type_std_attributes to reduce some duplicate code.
TODO: move some methods from Item to Type_std_attributes.
1 parent 04fb09d commit ecb009b

File tree

4 files changed

+54
-50
lines changed

4 files changed

+54
-50
lines changed

sql/item.cc

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -412,13 +412,11 @@ int Item::save_str_value_in_field(Field *field, String *result)
412412

413413
Item::Item():
414414
is_expensive_cache(-1), rsize(0), name(0), orig_name(0), name_length(0),
415-
fixed(0), is_autogenerated_name(TRUE),
416-
collation(&my_charset_bin, DERIVATION_COERCIBLE)
415+
fixed(0), is_autogenerated_name(TRUE)
417416
{
418417
marker= 0;
419-
maybe_null=null_value=with_sum_func=with_field=unsigned_flag=0;
418+
maybe_null=null_value=with_sum_func=with_field=0;
420419
in_rollup= 0;
421-
decimals= 0; max_length= 0;
422420
with_subselect= 0;
423421
cmp_context= IMPOSSIBLE_RESULT;
424422
/* Initially this item is not attached to any JOIN_TAB. */
@@ -451,26 +449,23 @@ Item::Item():
451449
tables.
452450
*/
453451
Item::Item(THD *thd, Item *item):
452+
Type_std_attributes(item),
454453
join_tab_idx(item->join_tab_idx),
455454
is_expensive_cache(-1),
456455
rsize(0),
457456
str_value(item->str_value),
458457
name(item->name),
459458
orig_name(item->orig_name),
460-
max_length(item->max_length),
461459
name_length(item->name_length),
462-
decimals(item->decimals),
463460
marker(item->marker),
464461
maybe_null(item->maybe_null),
465462
in_rollup(item->in_rollup),
466463
null_value(item->null_value),
467-
unsigned_flag(item->unsigned_flag),
468464
with_sum_func(item->with_sum_func),
469465
with_field(item->with_field),
470466
fixed(item->fixed),
471467
is_autogenerated_name(item->is_autogenerated_name),
472468
with_subselect(item->has_subquery()),
473-
collation(item->collation),
474469
cmp_context(item->cmp_context)
475470
{
476471
next= thd->free_list; // Put in free list
@@ -3713,17 +3708,14 @@ void Item_param::print(String *str, enum_query_type query_type)
37133708
void
37143709
Item_param::set_param_type_and_swap_value(Item_param *src)
37153710
{
3716-
unsigned_flag= src->unsigned_flag;
3711+
Type_std_attributes::set(src);
37173712
param_type= src->param_type;
37183713
set_param_func= src->set_param_func;
37193714
item_type= src->item_type;
37203715
item_result_type= src->item_result_type;
37213716

3722-
collation.set(src->collation);
37233717
maybe_null= src->maybe_null;
37243718
null_value= src->null_value;
3725-
max_length= src->max_length;
3726-
decimals= src->decimals;
37273719
state= src->state;
37283720
value= src->value;
37293721

@@ -6965,17 +6957,14 @@ bool Item_ref::fix_fields(THD *thd, Item **reference)
69656957

69666958
void Item_ref::set_properties()
69676959
{
6968-
max_length= (*ref)->max_length;
6960+
Type_std_attributes::set(*ref);
69696961
maybe_null= (*ref)->maybe_null;
6970-
decimals= (*ref)->decimals;
6971-
collation.set((*ref)->collation);
69726962
/*
69736963
We have to remember if we refer to a sum function, to ensure that
69746964
split_sum_func() doesn't try to change the reference.
69756965
*/
69766966
with_sum_func= (*ref)->with_sum_func;
69776967
with_field= (*ref)->with_field;
6978-
unsigned_flag= (*ref)->unsigned_flag;
69796968
fixed= 1;
69806969
if (alias_name_used)
69816970
return;
@@ -7415,13 +7404,10 @@ Item_cache_wrapper::Item_cache_wrapper(Item *item_arg)
74157404
:orig_item(item_arg), expr_cache(NULL), expr_value(NULL)
74167405
{
74177406
DBUG_ASSERT(orig_item->fixed);
7418-
max_length= orig_item->max_length;
7407+
Type_std_attributes::set(orig_item);
74197408
maybe_null= orig_item->maybe_null;
7420-
decimals= orig_item->decimals;
7421-
collation.set(orig_item->collation);
74227409
with_sum_func= orig_item->with_sum_func;
74237410
with_field= orig_item->with_field;
7424-
unsigned_flag= orig_item->unsigned_flag;
74257411
name= item_arg->name;
74267412
name_length= item_arg->name_length;
74277413
with_subselect= orig_item->with_subselect;

sql/item.h

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class DTCollation {
118118
derivation= derivation_arg;
119119
set_repertoire_from_charset(collation_arg);
120120
}
121-
void set(DTCollation &dt)
121+
void set(const DTCollation &dt)
122122
{
123123
collation= dt.collation;
124124
derivation= dt.derivation;
@@ -546,7 +546,48 @@ class String_copier_for_item: public String_copier
546546
};
547547

548548

549-
class Item {
549+
/**
550+
A class to store type attributes for the standard data types.
551+
Does not include attributes for the extended data types
552+
such as ENUM, SET, GEOMETRY.
553+
*/
554+
class Type_std_attributes
555+
{
556+
public:
557+
DTCollation collation;
558+
uint decimals;
559+
/*
560+
The maximum value length in characters multiplied by collation->mbmaxlen.
561+
Almost always it's the maximum value length in bytes.
562+
*/
563+
uint32 max_length;
564+
bool unsigned_flag;
565+
Type_std_attributes()
566+
:collation(&my_charset_bin, DERIVATION_COERCIBLE),
567+
decimals(0), max_length(0), unsigned_flag(false)
568+
{ }
569+
Type_std_attributes(const Type_std_attributes *other)
570+
:collation(other->collation),
571+
decimals(other->decimals),
572+
max_length(other->max_length),
573+
unsigned_flag(other->unsigned_flag)
574+
{ }
575+
void set(const Type_std_attributes *other)
576+
{
577+
*this= *other;
578+
}
579+
void set(const Field *field)
580+
{
581+
decimals= field->decimals();
582+
max_length= field->field_length;
583+
collation.set(field->charset());
584+
unsigned_flag= MY_TEST(field->flags & UNSIGNED_FLAG);
585+
}
586+
};
587+
588+
589+
class Item: public Type_std_attributes
590+
{
550591
Item(const Item &); /* Prevent use of these */
551592
void operator=(Item &);
552593
/**
@@ -614,24 +655,17 @@ class Item {
614655
@see Query_arena::free_list
615656
*/
616657
Item *next;
617-
/*
618-
The maximum value length in characters multiplied by collation->mbmaxlen.
619-
Almost always it's the maximum value length in bytes.
620-
*/
621-
uint32 max_length;
622658
/*
623659
TODO: convert name and name_length fields into LEX_STRING to keep them in
624660
sync (see bug #11829681/60295 etc). Then also remove some strlen(name)
625661
calls.
626662
*/
627663
uint name_length; /* Length of name */
628-
uint decimals;
629664
int8 marker;
630665
bool maybe_null; /* If item may be null */
631666
bool in_rollup; /* If used in GROUP BY list
632667
of a query with ROLLUP */
633668
bool null_value; /* if item is null */
634-
bool unsigned_flag;
635669
bool with_sum_func; /* True if item contains a sum func */
636670
/**
637671
True if any item except Item_sum contains a field. Set during parsing.
@@ -643,7 +677,6 @@ class Item {
643677
bool with_subselect; /* If this item is a subselect or some
644678
of its arguments is or contains a
645679
subselect */
646-
DTCollation collation;
647680
Item_result cmp_context; /* Comparison context */
648681
// alloc & destruct is done as start of select using sql_alloc
649682
Item();
@@ -4149,14 +4182,11 @@ class Item_copy :public Item
41494182
{
41504183
item= i;
41514184
null_value=maybe_null=item->maybe_null;
4152-
decimals=item->decimals;
4153-
max_length=item->max_length;
4185+
Type_std_attributes::set(item);
41544186
name=item->name;
41554187
cached_field_type= item->field_type();
41564188
cached_result_type= item->result_type();
4157-
unsigned_flag= item->unsigned_flag;
41584189
fixed= item->fixed;
4159-
collation.set(item->collation);
41604190
}
41614191

41624192
public:
@@ -4608,10 +4638,7 @@ class Item_cache: public Item_basic_constant
46084638
virtual bool setup(Item *item)
46094639
{
46104640
example= item;
4611-
max_length= item->max_length;
4612-
decimals= item->decimals;
4613-
collation.set(item->collation);
4614-
unsigned_flag= item->unsigned_flag;
4641+
Type_std_attributes::set(item);
46154642
if (item->type() == FIELD_ITEM)
46164643
cached_field= ((Item_field *)item)->field;
46174644
return 0;

sql/item_cmpfunc.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,13 +2654,10 @@ void Item_func_if::fix_after_pullout(st_select_lex *new_parent, Item **ref)
26542654

26552655
void Item_func_if::cache_type_info(Item *source)
26562656
{
2657-
collation.set(source->collation);
2657+
Type_std_attributes::set(source);
26582658
cached_field_type= source->field_type();
26592659
cached_result_type= source->result_type();
2660-
decimals= source->decimals;
2661-
max_length= source->max_length;
26622660
maybe_null= source->maybe_null;
2663-
unsigned_flag= source->unsigned_flag;
26642661
}
26652662

26662663

sql/item_func.cc

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6645,11 +6645,8 @@ void Item_func_sp::fix_length_and_dec()
66456645
DBUG_ENTER("Item_func_sp::fix_length_and_dec");
66466646

66476647
DBUG_ASSERT(sp_result_field);
6648-
decimals= sp_result_field->decimals();
6649-
max_length= sp_result_field->field_length;
6650-
collation.set(sp_result_field->charset());
6648+
Type_std_attributes::set(sp_result_field);
66516649
maybe_null= 1;
6652-
unsigned_flag= MY_TEST(sp_result_field->flags & UNSIGNED_FLAG);
66536650

66546651
DBUG_VOID_RETURN;
66556652
}
@@ -6999,9 +6996,6 @@ my_decimal *Item_func_last_value::val_decimal(my_decimal *decimal_value)
69996996
void Item_func_last_value::fix_length_and_dec()
70006997
{
70016998
last_value= args[arg_count -1];
7002-
decimals= last_value->decimals;
7003-
max_length= last_value->max_length;
7004-
collation.set(last_value->collation.collation);
6999+
Type_std_attributes::set(last_value);
70057000
maybe_null= last_value->maybe_null;
7006-
unsigned_flag= last_value->unsigned_flag;
70077001
}

0 commit comments

Comments
 (0)