Skip to content

Commit

Permalink
MDEV-13526 Add Type_handler::Item_val_bool()
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Barkov committed Aug 15, 2017
1 parent 9822fb1 commit 6179a8e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 31 deletions.
30 changes: 0 additions & 30 deletions sql/item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,36 +109,6 @@ void Item::push_note_converted_to_positive_complement(THD *thd)
}


/**
@todo
Make this functions class dependent
*/

bool Item::val_bool()
{
switch(result_type()) {
case INT_RESULT:
return val_int() != 0;
case DECIMAL_RESULT:
{
my_decimal decimal_value;
my_decimal *val= val_decimal(&decimal_value);
if (val)
return !my_decimal_is_zero(val);
return 0;
}
case REAL_RESULT:
case STRING_RESULT:
return val_real() != 0.0;
case ROW_RESULT:
case TIME_RESULT:
DBUG_ASSERT(0);
return 0; // Wrong (but safe)
}
return 0; // Wrong (but safe)
}


/**
Get date/time/datetime.
Optionally extend TIME result to DATETIME.
Expand Down
5 changes: 4 additions & 1 deletion sql/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,10 @@ class Item: public Value_source,
FALSE value is false or NULL
TRUE value is true (not equal to 0)
*/
virtual bool val_bool();
virtual bool val_bool()
{
return type_handler()->Item_val_bool(this);
}
virtual String *val_nodeset(String*) { return 0; }

/*
Expand Down
32 changes: 32 additions & 0 deletions sql/sql_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3077,6 +3077,38 @@ bool Type_handler_geometry::
#endif


/*************************************************************************/

bool Type_handler_real_result::Item_val_bool(Item *item) const
{
return item->val_real() != 0.0;
}

bool Type_handler_int_result::Item_val_bool(Item *item) const
{
return item->val_int() != 0;
}

bool Type_handler_decimal_result::Item_val_bool(Item *item) const
{
my_decimal decimal_value;
my_decimal *val= item->val_decimal(&decimal_value);
if (val)
return !my_decimal_is_zero(val);
return false;
}

bool Type_handler_temporal_result::Item_val_bool(Item *item) const
{
return item->val_real() != 0.0;
}

bool Type_handler_string_result::Item_val_bool(Item *item) const
{
return item->val_real() != 0.0;
}


/*************************************************************************/

longlong Type_handler_real_result::
Expand Down
11 changes: 11 additions & 0 deletions sql/sql_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,7 @@ class Type_handler
virtual
bool Item_sum_variance_fix_length_and_dec(Item_sum_variance *) const= 0;

virtual bool Item_val_bool(Item *item) const= 0;
virtual longlong Item_val_int_signed_typecast(Item *item) const= 0;
virtual longlong Item_val_int_unsigned_typecast(Item *item) const= 0;

Expand Down Expand Up @@ -1147,6 +1148,11 @@ class Type_handler_row: public Type_handler
DBUG_ASSERT(0);
return true;
}
bool Item_val_bool(Item *item) const
{
DBUG_ASSERT(0);
return false;
}
longlong Item_val_int_signed_typecast(Item *item) const
{
DBUG_ASSERT(0);
Expand Down Expand Up @@ -1352,6 +1358,7 @@ class Type_handler_real_result: public Type_handler_numeric
bool Item_sum_variance_fix_length_and_dec(Item_sum_variance *) const;
bool Item_func_signed_fix_length_and_dec(Item_func_signed *item) const;
bool Item_func_unsigned_fix_length_and_dec(Item_func_unsigned *item) const;
bool Item_val_bool(Item *item) const;
longlong Item_val_int_signed_typecast(Item *item) const;
longlong Item_val_int_unsigned_typecast(Item *item) const;
String *Item_func_hex_val_str_ascii(Item_func_hex *item, String *str) const;
Expand Down Expand Up @@ -1427,6 +1434,7 @@ class Type_handler_decimal_result: public Type_handler_numeric
bool Item_sum_sum_fix_length_and_dec(Item_sum_sum *) const;
bool Item_sum_avg_fix_length_and_dec(Item_sum_avg *) const;
bool Item_sum_variance_fix_length_and_dec(Item_sum_variance *) const;
bool Item_val_bool(Item *item) const;
longlong Item_val_int_signed_typecast(Item *item) const;
longlong Item_val_int_unsigned_typecast(Item *item) const;
String *Item_func_hex_val_str_ascii(Item_func_hex *item, String *str) const;
Expand Down Expand Up @@ -1494,6 +1502,7 @@ class Type_handler_int_result: public Type_handler_numeric
bool Item_sum_sum_fix_length_and_dec(Item_sum_sum *) const;
bool Item_sum_avg_fix_length_and_dec(Item_sum_avg *) const;
bool Item_sum_variance_fix_length_and_dec(Item_sum_variance *) const;
bool Item_val_bool(Item *item) const;
longlong Item_val_int_signed_typecast(Item *item) const;
longlong Item_val_int_unsigned_typecast(Item *item) const;
String *Item_func_hex_val_str_ascii(Item_func_hex *item, String *str) const;
Expand Down Expand Up @@ -1564,6 +1573,7 @@ class Type_handler_temporal_result: public Type_handler
bool Item_sum_sum_fix_length_and_dec(Item_sum_sum *) const;
bool Item_sum_avg_fix_length_and_dec(Item_sum_avg *) const;
bool Item_sum_variance_fix_length_and_dec(Item_sum_variance *) const;
bool Item_val_bool(Item *item) const;
longlong Item_val_int_signed_typecast(Item *item) const;
longlong Item_val_int_unsigned_typecast(Item *item) const;
String *Item_func_hex_val_str_ascii(Item_func_hex *item, String *str) const;
Expand Down Expand Up @@ -1673,6 +1683,7 @@ class Type_handler_string_result: public Type_handler
bool Item_sum_variance_fix_length_and_dec(Item_sum_variance *) const;
bool Item_func_signed_fix_length_and_dec(Item_func_signed *item) const;
bool Item_func_unsigned_fix_length_and_dec(Item_func_unsigned *item) const;
bool Item_val_bool(Item *item) const;
longlong Item_val_int_signed_typecast(Item *item) const;
longlong Item_val_int_unsigned_typecast(Item *item) const;
String *Item_func_hex_val_str_ascii(Item_func_hex *item, String *str) const;
Expand Down

0 comments on commit 6179a8e

Please sign in to comment.