Skip to content

Commit

Permalink
Moving Item::str_value from public to protected.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Barkov committed Sep 2, 2014
1 parent 658a1e9 commit fbaaf36
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 43 deletions.
9 changes: 3 additions & 6 deletions sql/item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1280,16 +1280,13 @@ Item *Item_param::safe_charset_converter(CHARSET_INFO *tocs)
String *ostr= val_str(&cnvstr);
if (!needs_charset_converter(tocs))
return this;
cnvitem->str_value.copy(ostr->ptr(), ostr->length(),
ostr->charset(), tocs, &cnv_errors);
cnvitem->copy_value(ostr->ptr(), ostr->length(),
ostr->charset(), tocs, &cnv_errors);
if (cnv_errors)
return NULL;
if (ostr->charset() == &my_charset_bin && tocs != &my_charset_bin &&
!cnvitem->check_well_formed_result(&cnvitem->str_value, true))
!cnvitem->check_well_formed_result(true))
return NULL;
cnvitem->str_value.mark_as_const();
cnvitem->collation.set(tocs);
cnvitem->max_length= cnvitem->str_value.numchars() * tocs->mbmaxlen;
return cnvitem;
}
return this;
Expand Down
60 changes: 58 additions & 2 deletions sql/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,20 @@ class Item {
/* Reuse size, only used by SP local variable assignment, otherwize 0 */
uint rsize;

protected:
/*
str_values's main purpose is to be used to cache the value in
save_in_field
*/
String str_value;

public:
/*
Cache val_str() into the own buffer, e.g. to evaluate constant
expressions with subqueries in the ORDER/GROUP clauses.
*/
String *val_str() { return val_str(&str_value); }

char * name; /* Name from select */
/* Original item name (if it was renamed)*/
char * orig_name;
Expand Down Expand Up @@ -1662,6 +1671,7 @@ class Field_enumerator
};

class sp_head;
class Item_string;


/**
Expand Down Expand Up @@ -2366,7 +2376,7 @@ class Item_param :public Item_basic_value,
{
char cnvbuf[MAX_FIELD_WIDTH];
String cnvstr;
Item *cnvitem;
Item_string *cnvitem;

public:
enum enum_item_param_state
Expand Down Expand Up @@ -2758,6 +2768,24 @@ class Item_string :public Item_basic_constant
// it is constant => can be used without fix_fields (and frequently used)
fixed= 1;
}
void set_value(const String *str)
{
str_value= *str;
collation.set(str->charset());
}
void copy_value(const char *str, uint32 length, CHARSET_INFO *fromcs,
CHARSET_INFO *tocs, uint *cnv_errors)
{
str_value.copy(str, length, fromcs, tocs, cnv_errors);
str_value.mark_as_const();
collation.set(tocs);
fix_char_length(str_value.numchars());
}

void print_value(String *to) const
{
str_value.print(to);
}
/*
This is used in stored procedures to avoid memory leaks and
does a deep copy of its argument.
Expand Down Expand Up @@ -2850,6 +2878,34 @@ class Item_string :public Item_basic_constant
String *check_well_formed_result(bool send_error)
{ return Item::check_well_formed_result(&str_value, send_error); }

enum_field_types odbc_temporal_literal_type(const LEX_STRING *type_str) const
{
/*
If string is a reasonably short pure ASCII string literal,
try to parse known ODBC style date, time or timestamp literals,
e.g:
SELECT {d'2001-01-01'};
SELECT {t'10:20:30'};
SELECT {ts'2001-01-01 10:20:30'};
*/
if (collation.repertoire == MY_REPERTOIRE_ASCII &&
str_value.length() < MAX_DATE_STRING_REP_LENGTH * 4)
{
if (type_str->length == 1)
{
if (type_str->str[0] == 'd') /* {d'2001-01-01'} */
return MYSQL_TYPE_DATE;
else if (type_str->str[0] == 't') /* {t'10:20:30'} */
return MYSQL_TYPE_TIME;
}
else if (type_str->length == 2) /* {ts'2001-01-01 10:20:30'} */
{
if (type_str->str[0] == 't' && type_str->str[1] == 's')
return MYSQL_TYPE_DATETIME;
}
}
return MYSQL_TYPE_STRING; // Not a temporal literal
}
private:
bool m_cs_specified;
};
Expand Down Expand Up @@ -2995,7 +3051,7 @@ class Item_hex_constant: public Item_basic_constant
{
return item->basic_const_item() && item->type() == type() &&
item->cast_to_int_type() == cast_to_int_type() &&
str_value.bin_eq(&item->str_value);
str_value.bin_eq(&((Item_hex_constant*)item)->str_value);
}
String *val_str(String*) { DBUG_ASSERT(fixed == 1); return &str_value; }
};
Expand Down
2 changes: 1 addition & 1 deletion sql/item_cmpfunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ class in_string :public in_vector
{
String *str=((String*) base)+pos;
Item_string *to= (Item_string*)item;
to->str_value= *str;
to->set_value(str);
}
Item_result result_type() { return STRING_RESULT; }
};
Expand Down
4 changes: 2 additions & 2 deletions sql/item_create.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5241,13 +5241,13 @@ Create_func_space::create_1_arg(THD *thd, Item *arg1)
<code>thd->variables.collation_connection</code>.
*/
CHARSET_INFO *cs= thd->variables.collation_connection;
Item *sp;
Item_string *sp;

if (cs->mbminlen > 1)
{
uint dummy_errors;
sp= new (thd->mem_root) Item_string("", 0, cs, DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII);
sp->str_value.copy(" ", 1, &my_charset_latin1, cs, &dummy_errors);
sp->copy_value(" ", 1, &my_charset_latin1, cs, &dummy_errors);
}
else
{
Expand Down
9 changes: 9 additions & 0 deletions sql/item_create.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ Item *create_temporal_literal(THD *thd,
CHARSET_INFO *cs,
enum_field_types type,
bool send_error);
inline
Item *create_temporal_literal(THD *thd, const String *str,
enum_field_types type,
bool send_error)
{
return create_temporal_literal(thd,
str->ptr(), str->length(), str->charset(),
type, send_error);
}

int item_create_init();
void item_create_cleanup();
Expand Down
2 changes: 1 addition & 1 deletion sql/item_strfunc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3397,7 +3397,7 @@ void Item_func_set_collation::print(String *str, enum_query_type query_type)
str->append(STRING_WITH_LEN(" collate "));
DBUG_ASSERT(args[1]->basic_const_item() &&
args[1]->type() == Item::STRING_ITEM);
args[1]->str_value.print(str);
((Item_string *)args[1])->print_value(str);
str->append(')');
}

Expand Down
2 changes: 1 addition & 1 deletion sql/item_timefunc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1816,7 +1816,7 @@ void Item_func_date_format::fix_length_and_dec()
if (arg1->type() == STRING_ITEM)
{ // Optimize the normal case
fixed_length=1;
max_length= format_length(&arg1->str_value) *
max_length= format_length(arg1->val_str(NULL)) *
collation.collation->mbmaxlen;
}
else
Expand Down
2 changes: 1 addition & 1 deletion sql/sql_select.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2521,7 +2521,7 @@ void JOIN::exec_inner()
Item *cur_const_item;
while ((cur_const_item= const_item_it++))
{
cur_const_item->val_str(&cur_const_item->str_value);
cur_const_item->val_str(); // This caches val_str() to Item::str_value
if (thd->is_error())
{
error= thd->is_error();
Expand Down
2 changes: 1 addition & 1 deletion sql/sql_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ bool String::append_for_single_quote(const char *st, uint len)
return 0;
}

void String::print(String *str)
void String::print(String *str) const
{
str->append_for_single_quote(Ptr, str_length);
}
Expand Down
2 changes: 1 addition & 1 deletion sql/sql_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ class String
str_length+= arg_length;
return FALSE;
}
void print(String *print);
void print(String *print) const;

bool append_for_single_quote(const char *st, uint len);
bool append_for_single_quote(const String *s)
Expand Down
29 changes: 5 additions & 24 deletions sql/sql_yacc.yy
Original file line number Diff line number Diff line change
Expand Up @@ -9195,7 +9195,6 @@ simple_expr:
}
| '{' ident expr '}'
{
Item_string *item;
$$= NULL;
/*
If "expr" is reasonably short pure ASCII string literal,
Expand All @@ -9205,31 +9204,13 @@ simple_expr:
SELECT {t'10:20:30'};
SELECT {ts'2001-01-01 10:20:30'};
*/
if ($3->type() == Item::STRING_ITEM &&
(item= (Item_string *) $3) &&
item->collation.repertoire == MY_REPERTOIRE_ASCII &&
item->str_value.length() < MAX_DATE_STRING_REP_LENGTH * 4)
{
enum_field_types type= MYSQL_TYPE_STRING;
LEX_STRING *ls= &$2;
if (ls->length == 1)
{
if (ls->str[0] == 'd') /* {d'2001-01-01'} */
type= MYSQL_TYPE_DATE;
else if (ls->str[0] == 't') /* {t'10:20:30'} */
type= MYSQL_TYPE_TIME;
}
else if (ls->length == 2) /* {ts'2001-01-01 10:20:30'} */
{
if (ls->str[0] == 't' && ls->str[1] == 's')
type= MYSQL_TYPE_DATETIME;
}
if ($3->type() == Item::STRING_ITEM)
{
Item_string *item= (Item_string *) $3;
enum_field_types type= item->odbc_temporal_literal_type(&$2);
if (type != MYSQL_TYPE_STRING)
{
$$= create_temporal_literal(thd,
item->str_value.ptr(),
item->str_value.length(),
item->str_value.charset(),
$$= create_temporal_literal(thd, item->val_str(NULL),
type, false);
}
}
Expand Down
6 changes: 3 additions & 3 deletions storage/sphinx/ha_sphinx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2719,11 +2719,11 @@ const Item * ha_sphinx::cond_push ( const Item *cond )
break;

// copy the query, and let know that we intercepted this condition
Item_string * pString = (Item_string *) args[1];
String *pString= args[1]->val_str(NULL);
pTls->m_bQuery = true;
strncpy ( pTls->m_sQuery, pString->str_value.c_ptr(), sizeof(pTls->m_sQuery) );
strncpy ( pTls->m_sQuery, pString->c_ptr(), sizeof(pTls->m_sQuery) );
pTls->m_sQuery[sizeof(pTls->m_sQuery)-1] = '\0';
pTls->m_pQueryCharset = pString->str_value.charset();
pTls->m_pQueryCharset = pString->charset();

} else
{
Expand Down

0 comments on commit fbaaf36

Please sign in to comment.