24 #include <drizzled/current_session.h>
25 #include <drizzled/error.h>
26 #include <drizzled/function/time/typecast.h>
27 #include <drizzled/time_functions.h>
28 #include <drizzled/charset.h>
36 if (item->type() != FUNC_ITEM ||
37 functype() != ((
Item_func*)item)->functype())
41 if (cast_length != cast->cast_length ||
42 cast_cs != cast->cast_cs)
45 if (!args[0]->
eq(cast->args[0], binary_cmp))
52 str->append(STRING_WITH_LEN(
"cast("));
54 str->append(STRING_WITH_LEN(
" as "));
55 str->append(cast_type(), strlen(cast_type()));
62 str->append(STRING_WITH_LEN(
"cast("));
64 str->append(STRING_WITH_LEN(
" as char"));
70 String st(buffer,
sizeof(buffer), &my_charset_bin);
71 st.set((uint64_t)cast_length, &my_charset_bin);
78 str->append(STRING_WITH_LEN(
" charset "));
79 str->append(cast_cs->csname, strlen(cast_cs->csname));
90 if (!charset_conversion)
92 if (!(res= args[0]->
val_str(str)))
101 if (!(res= args[0]->
val_str(&tmp_value)))
106 str->copy(res->ptr(), res->length(), cast_cs);
110 res->set_charset(cast_cs);
117 if (cast_length >= 0)
119 if (res->length() > (length= (uint32_t) res->charpos(cast_length)))
122 snprintf(char_type,
sizeof(char_type),
"%s(%lu)",
123 cast_cs == &my_charset_bin ?
"BINARY" :
"CHAR",
124 (ulong) cast_length);
126 if (!res->alloced_length())
131 push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
132 ER_TRUNCATED_WRONG_VALUE,
133 ER(ER_TRUNCATED_WRONG_VALUE), char_type,
135 res->length((uint) length);
137 else if (cast_cs == &my_charset_bin && res->length() < (uint) cast_length)
139 if (res->alloced_length() < (uint) cast_length)
141 str->alloc(cast_length);
145 memset(res->ptr() + res->length(), 0,
146 (uint) cast_length - res->length());
147 res->length(cast_length);
155 void Item_char_typecast::fix_length_and_dec()
157 uint32_t char_length;
180 from_cs= (args[0]->result_type() == INT_RESULT ||
181 args[0]->result_type() == DECIMAL_RESULT ||
182 args[0]->result_type() == REAL_RESULT) ?
183 (cast_cs->mbminlen == 1 ? cast_cs : &my_charset_utf8_general_ci) :
184 args[0]->collation.collation;
185 charset_conversion= (cast_cs->mbmaxlen > 1) ||
186 (!my_charset_same(from_cs, cast_cs) && from_cs != &my_charset_bin && cast_cs != &my_charset_bin);
187 collation.set(cast_cs, DERIVATION_IMPLICIT);
188 char_length= (cast_length >= 0) ? (uint32_t)cast_length :
189 (uint32_t)args[0]->max_length/from_cs->mbmaxlen;
190 max_length= char_length * cast_cs->mbmaxlen;
199 if (not get_arg0_date(ltime, TIME_FUZZY_DATE))
201 if (ltime.second_part)
222 if (get_arg0_date(ltime, 1))
237 bool res= get_arg0_date(ltime, TIME_FUZZY_DATE);
239 ltime.hour= ltime.minute= ltime.second= ltime.second_part= 0;
240 ltime.time_type= type::DRIZZLE_TIMESTAMP_DATE;
259 if (!get_arg0_date(ltime, TIME_FUZZY_DATE))
261 str->alloc(type::Time::MAX_STRING_LENGTH);
262 ltime.convert(*str, type::DRIZZLE_TIMESTAMP_DATE);
279 return (int64_t) (ltime.year * 10000L + ltime.month * 100 + ltime.day);
virtual void print(String *str)
String * val_str(String *str)
bool eq(const Item *item, bool binary_cmp) const
bool get_time(type::Time <ime)
String * val_str(String *str)
virtual void print(String *str)
bool get_date(type::Time <ime, uint32_t fuzzy_date)
virtual void print(String *str)
String * val_str(String *a)