You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3108 lines
79 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
22 years ago
22 years ago
22 years ago
23 years ago
26 years ago
26 years ago
22 years ago
22 years ago
26 years ago
26 years ago
22 years ago
26 years ago
26 years ago
26 years ago
22 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
26 years ago
26 years ago
23 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
26 years ago
26 years ago
22 years ago
26 years ago
26 years ago
22 years ago
26 years ago
22 years ago
26 years ago
23 years ago
23 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
26 years ago
26 years ago
22 years ago
26 years ago
23 years ago
23 years ago
22 years ago
23 years ago
26 years ago
26 years ago
22 years ago
26 years ago
26 years ago
22 years ago
26 years ago
23 years ago
23 years ago
22 years ago
23 years ago
26 years ago
26 years ago
22 years ago
26 years ago
26 years ago
22 years ago
26 years ago
23 years ago
23 years ago
22 years ago
23 years ago
26 years ago
26 years ago
22 years ago
26 years ago
23 years ago
23 years ago
22 years ago
23 years ago
26 years ago
26 years ago
23 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
20 years ago
20 years ago
26 years ago
26 years ago
26 years ago
26 years ago
23 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
24 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
23 years ago
23 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
26 years ago
26 years ago
26 years ago
23 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
26 years ago
23 years ago
26 years ago
22 years ago
23 years ago
Fixed bug#16377: result of DATE/TIME functions were compared as strings which can lead to a wrong result. All date/time functions has the STRING result type thus their results are compared as strings. The string date representation allows a user to skip some of leading zeros. This can lead to wrong comparison result if a date/time function result is compared to such a string constant. The idea behind this bug fix is to compare results of date/time functions and data/time constants as ints, because that date/time representation is more exact. To achieve this the agg_cmp_type() is changed to take in the account that a date/time field or an date/time item should be compared as ints. This bug fix is partially back ported from 5.0. The agg_cmp_type() function now accepts THD as one of parameters. In addition, it now checks if a date/time field/function is present in the list. If so, it tries to coerce all constants to INT to make date/time comparison return correct result. The field for the constant coercion is taken from the Item_field or constructed from the Item_func. In latter case the constructed field will be freed after conversion of all constant items. Otherwise the result is same as before - aggregated with help of the item_cmp_type() function. From the Item_func_between::fix_length_and_dec() function removed the part which was converting date/time constants to int if possible. Now this is done by the agg_cmp_type() function. The new function result_as_longlong() is added to the Item class. It indicates that the item is a date/time item and result of it can be compared as int. Such items are date/time fields/functions. Correct val_int() methods are implemented for classes Item_date_typecast, Item_func_makedate, Item_time_typecast, Item_datetime_typecast. All these classes are derived from Item_str_func and Item_str_func::val_int() converts its string value to int without regard to the date/time type of these items. Arg_comparator::set_compare_func() and Arg_comparator::set_cmp_func() functions are changed to substitute result type of an item with the INT_RESULT if the item is a date/time item and another item is a constant. This is done to get a correct result of comparisons like date_time_function() = string_constant.
20 years ago
Fixed bug#16377: result of DATE/TIME functions were compared as strings which can lead to a wrong result. All date/time functions has the STRING result type thus their results are compared as strings. The string date representation allows a user to skip some of leading zeros. This can lead to wrong comparison result if a date/time function result is compared to such a string constant. The idea behind this bug fix is to compare results of date/time functions and data/time constants as ints, because that date/time representation is more exact. To achieve this the agg_cmp_type() is changed to take in the account that a date/time field or an date/time item should be compared as ints. This bug fix is partially back ported from 5.0. The agg_cmp_type() function now accepts THD as one of parameters. In addition, it now checks if a date/time field/function is present in the list. If so, it tries to coerce all constants to INT to make date/time comparison return correct result. The field for the constant coercion is taken from the Item_field or constructed from the Item_func. In latter case the constructed field will be freed after conversion of all constant items. Otherwise the result is same as before - aggregated with help of the item_cmp_type() function. From the Item_func_between::fix_length_and_dec() function removed the part which was converting date/time constants to int if possible. Now this is done by the agg_cmp_type() function. The new function result_as_longlong() is added to the Item class. It indicates that the item is a date/time item and result of it can be compared as int. Such items are date/time fields/functions. Correct val_int() methods are implemented for classes Item_date_typecast, Item_func_makedate, Item_time_typecast, Item_datetime_typecast. All these classes are derived from Item_str_func and Item_str_func::val_int() converts its string value to int without regard to the date/time type of these items. Arg_comparator::set_compare_func() and Arg_comparator::set_cmp_func() functions are changed to substitute result type of an item with the INT_RESULT if the item is a date/time item and another item is a constant. This is done to get a correct result of comparisons like date_time_function() = string_constant.
20 years ago
Fixed bug#16377: result of DATE/TIME functions were compared as strings which can lead to a wrong result. All date/time functions has the STRING result type thus their results are compared as strings. The string date representation allows a user to skip some of leading zeros. This can lead to wrong comparison result if a date/time function result is compared to such a string constant. The idea behind this bug fix is to compare results of date/time functions and data/time constants as ints, because that date/time representation is more exact. To achieve this the agg_cmp_type() is changed to take in the account that a date/time field or an date/time item should be compared as ints. This bug fix is partially back ported from 5.0. The agg_cmp_type() function now accepts THD as one of parameters. In addition, it now checks if a date/time field/function is present in the list. If so, it tries to coerce all constants to INT to make date/time comparison return correct result. The field for the constant coercion is taken from the Item_field or constructed from the Item_func. In latter case the constructed field will be freed after conversion of all constant items. Otherwise the result is same as before - aggregated with help of the item_cmp_type() function. From the Item_func_between::fix_length_and_dec() function removed the part which was converting date/time constants to int if possible. Now this is done by the agg_cmp_type() function. The new function result_as_longlong() is added to the Item class. It indicates that the item is a date/time item and result of it can be compared as int. Such items are date/time fields/functions. Correct val_int() methods are implemented for classes Item_date_typecast, Item_func_makedate, Item_time_typecast, Item_datetime_typecast. All these classes are derived from Item_str_func and Item_str_func::val_int() converts its string value to int without regard to the date/time type of these items. Arg_comparator::set_compare_func() and Arg_comparator::set_cmp_func() functions are changed to substitute result type of an item with the INT_RESULT if the item is a date/time item and another item is a constant. This is done to get a correct result of comparisons like date_time_function() = string_constant.
20 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
Fixed bug#16377: result of DATE/TIME functions were compared as strings which can lead to a wrong result. All date/time functions has the STRING result type thus their results are compared as strings. The string date representation allows a user to skip some of leading zeros. This can lead to wrong comparison result if a date/time function result is compared to such a string constant. The idea behind this bug fix is to compare results of date/time functions and data/time constants as ints, because that date/time representation is more exact. To achieve this the agg_cmp_type() is changed to take in the account that a date/time field or an date/time item should be compared as ints. This bug fix is partially back ported from 5.0. The agg_cmp_type() function now accepts THD as one of parameters. In addition, it now checks if a date/time field/function is present in the list. If so, it tries to coerce all constants to INT to make date/time comparison return correct result. The field for the constant coercion is taken from the Item_field or constructed from the Item_func. In latter case the constructed field will be freed after conversion of all constant items. Otherwise the result is same as before - aggregated with help of the item_cmp_type() function. From the Item_func_between::fix_length_and_dec() function removed the part which was converting date/time constants to int if possible. Now this is done by the agg_cmp_type() function. The new function result_as_longlong() is added to the Item class. It indicates that the item is a date/time item and result of it can be compared as int. Such items are date/time fields/functions. Correct val_int() methods are implemented for classes Item_date_typecast, Item_func_makedate, Item_time_typecast, Item_datetime_typecast. All these classes are derived from Item_str_func and Item_str_func::val_int() converts its string value to int without regard to the date/time type of these items. Arg_comparator::set_compare_func() and Arg_comparator::set_cmp_func() functions are changed to substitute result type of an item with the INT_RESULT if the item is a date/time item and another item is a constant. This is done to get a correct result of comparisons like date_time_function() = string_constant.
20 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
22 years ago
21 years ago
22 years ago
22 years ago
22 years ago
  1. /* Copyright (C) 2000-2003 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  13. /* This file defines all time functions */
  14. #ifdef USE_PRAGMA_IMPLEMENTATION
  15. #pragma implementation // gcc: Class implementation
  16. #endif
  17. #include "mysql_priv.h"
  18. #include <m_ctype.h>
  19. #include <time.h>
  20. /* TODO: Move month and days to language files */
  21. /* Day number for Dec 31st, 9999 */
  22. #define MAX_DAY_NUMBER 3652424L
  23. /*
  24. OPTIMIZATION TODO:
  25. - Replace the switch with a function that should be called for each
  26. date type.
  27. - Remove sprintf and opencode the conversion, like we do in
  28. Field_datetime.
  29. The reason for this functions existence is that as we don't have a
  30. way to know if a datetime/time value has microseconds in them
  31. we are now only adding microseconds to the output if the
  32. value has microseconds.
  33. We can't use a standard make_date_time() for this as we don't know
  34. if someone will use %f in the format specifier in which case we would get
  35. the microseconds twice.
  36. */
  37. static bool make_datetime(date_time_format_types format, TIME *ltime,
  38. String *str)
  39. {
  40. char *buff;
  41. CHARSET_INFO *cs= &my_charset_bin;
  42. uint length= 30;
  43. if (str->alloc(length))
  44. return 1;
  45. buff= (char*) str->ptr();
  46. switch (format) {
  47. case TIME_ONLY:
  48. length= cs->cset->snprintf(cs, buff, length, "%s%02d:%02d:%02d",
  49. ltime->neg ? "-" : "",
  50. ltime->hour, ltime->minute, ltime->second);
  51. break;
  52. case TIME_MICROSECOND:
  53. length= cs->cset->snprintf(cs, buff, length, "%s%02d:%02d:%02d.%06d",
  54. ltime->neg ? "-" : "",
  55. ltime->hour, ltime->minute, ltime->second,
  56. ltime->second_part);
  57. break;
  58. case DATE_ONLY:
  59. length= cs->cset->snprintf(cs, buff, length, "%04d-%02d-%02d",
  60. ltime->year, ltime->month, ltime->day);
  61. break;
  62. case DATE_TIME:
  63. length= cs->cset->snprintf(cs, buff, length,
  64. "%04d-%02d-%02d %02d:%02d:%02d",
  65. ltime->year, ltime->month, ltime->day,
  66. ltime->hour, ltime->minute, ltime->second);
  67. break;
  68. case DATE_TIME_MICROSECOND:
  69. length= cs->cset->snprintf(cs, buff, length,
  70. "%04d-%02d-%02d %02d:%02d:%02d.%06d",
  71. ltime->year, ltime->month, ltime->day,
  72. ltime->hour, ltime->minute, ltime->second,
  73. ltime->second_part);
  74. break;
  75. }
  76. str->length(length);
  77. str->set_charset(cs);
  78. return 0;
  79. }
  80. /*
  81. Date formats corresponding to compound %r and %T conversion specifiers
  82. Note: We should init at least first element of "positions" array
  83. (first member) or hpux11 compiler will die horribly.
  84. */
  85. static DATE_TIME_FORMAT time_ampm_format= {{0}, '\0', 0,
  86. {(char *)"%I:%i:%S %p", 11}};
  87. static DATE_TIME_FORMAT time_24hrs_format= {{0}, '\0', 0,
  88. {(char *)"%H:%i:%S", 8}};
  89. /*
  90. Extract datetime value to TIME struct from string value
  91. according to format string.
  92. SYNOPSIS
  93. extract_date_time()
  94. format date/time format specification
  95. val String to decode
  96. length Length of string
  97. l_time Store result here
  98. cached_timestamp_type
  99. It uses to get an appropriate warning
  100. in the case when the value is truncated.
  101. sub_pattern_end if non-zero then we are parsing string which
  102. should correspond compound specifier (like %T or
  103. %r) and this parameter is pointer to place where
  104. pointer to end of string matching this specifier
  105. should be stored.
  106. NOTE
  107. Possibility to parse strings matching to patterns equivalent to compound
  108. specifiers is mainly intended for use from inside of this function in
  109. order to understand %T and %r conversion specifiers, so number of
  110. conversion specifiers that can be used in such sub-patterns is limited.
  111. Also most of checks are skipped in this case.
  112. If one adds new format specifiers to this function he should also
  113. consider adding them to get_date_time_result_type() function.
  114. RETURN
  115. 0 ok
  116. 1 error
  117. */
  118. static bool extract_date_time(DATE_TIME_FORMAT *format,
  119. const char *val, uint length, TIME *l_time,
  120. timestamp_type cached_timestamp_type,
  121. const char **sub_pattern_end,
  122. const char *date_time_type)
  123. {
  124. int weekday= 0, yearday= 0, daypart= 0;
  125. int week_number= -1;
  126. int error= 0;
  127. int strict_week_number_year= -1;
  128. int frac_part;
  129. bool usa_time= 0;
  130. bool sunday_first_n_first_week_non_iso;
  131. bool strict_week_number;
  132. bool strict_week_number_year_type;
  133. const char *val_begin= val;
  134. const char *val_end= val + length;
  135. const char *ptr= format->format.str;
  136. const char *end= ptr + format->format.length;
  137. CHARSET_INFO *cs= &my_charset_bin;
  138. DBUG_ENTER("extract_date_time");
  139. LINT_INIT(strict_week_number);
  140. /* Remove valgrind varnings when using gcc 3.3 and -O1 */
  141. PURIFY_OR_LINT_INIT(strict_week_number_year_type);
  142. PURIFY_OR_LINT_INIT(sunday_first_n_first_week_non_iso);
  143. if (!sub_pattern_end)
  144. bzero((char*) l_time, sizeof(*l_time));
  145. for (; ptr != end && val != val_end; ptr++)
  146. {
  147. if (*ptr == '%' && ptr+1 != end)
  148. {
  149. int val_len;
  150. char *tmp;
  151. error= 0;
  152. /* Skip pre-space between each argument */
  153. while (val != val_end && my_isspace(cs, *val))
  154. val++;
  155. val_len= (uint) (val_end - val);
  156. switch (*++ptr) {
  157. /* Year */
  158. case 'Y':
  159. tmp= (char*) val + min(4, val_len);
  160. l_time->year= (int) my_strtoll10(val, &tmp, &error);
  161. val= tmp;
  162. break;
  163. case 'y':
  164. tmp= (char*) val + min(2, val_len);
  165. l_time->year= (int) my_strtoll10(val, &tmp, &error);
  166. val= tmp;
  167. l_time->year+= (l_time->year < YY_PART_YEAR ? 2000 : 1900);
  168. break;
  169. /* Month */
  170. case 'm':
  171. case 'c':
  172. tmp= (char*) val + min(2, val_len);
  173. l_time->month= (int) my_strtoll10(val, &tmp, &error);
  174. val= tmp;
  175. break;
  176. case 'M':
  177. if ((l_time->month= check_word(my_locale_en_US.month_names,
  178. val, val_end, &val)) <= 0)
  179. goto err;
  180. break;
  181. case 'b':
  182. if ((l_time->month= check_word(my_locale_en_US.ab_month_names,
  183. val, val_end, &val)) <= 0)
  184. goto err;
  185. break;
  186. /* Day */
  187. case 'd':
  188. case 'e':
  189. tmp= (char*) val + min(2, val_len);
  190. l_time->day= (int) my_strtoll10(val, &tmp, &error);
  191. val= tmp;
  192. break;
  193. case 'D':
  194. tmp= (char*) val + min(2, val_len);
  195. l_time->day= (int) my_strtoll10(val, &tmp, &error);
  196. /* Skip 'st, 'nd, 'th .. */
  197. val= tmp + min((int) (end-tmp), 2);
  198. break;
  199. /* Hour */
  200. case 'h':
  201. case 'I':
  202. case 'l':
  203. usa_time= 1;
  204. /* fall through */
  205. case 'k':
  206. case 'H':
  207. tmp= (char*) val + min(2, val_len);
  208. l_time->hour= (int) my_strtoll10(val, &tmp, &error);
  209. val= tmp;
  210. break;
  211. /* Minute */
  212. case 'i':
  213. tmp= (char*) val + min(2, val_len);
  214. l_time->minute= (int) my_strtoll10(val, &tmp, &error);
  215. val= tmp;
  216. break;
  217. /* Second */
  218. case 's':
  219. case 'S':
  220. tmp= (char*) val + min(2, val_len);
  221. l_time->second= (int) my_strtoll10(val, &tmp, &error);
  222. val= tmp;
  223. break;
  224. /* Second part */
  225. case 'f':
  226. tmp= (char*) val_end;
  227. if (tmp - val > 6)
  228. tmp= (char*) val + 6;
  229. l_time->second_part= (int) my_strtoll10(val, &tmp, &error);
  230. frac_part= 6 - (tmp - val);
  231. if (frac_part > 0)
  232. l_time->second_part*= (ulong) log_10_int[frac_part];
  233. val= tmp;
  234. break;
  235. /* AM / PM */
  236. case 'p':
  237. if (val_len < 2 || ! usa_time)
  238. goto err;
  239. if (!my_strnncoll(&my_charset_latin1,
  240. (const uchar *) val, 2,
  241. (const uchar *) "PM", 2))
  242. daypart= 12;
  243. else if (my_strnncoll(&my_charset_latin1,
  244. (const uchar *) val, 2,
  245. (const uchar *) "AM", 2))
  246. goto err;
  247. val+= 2;
  248. break;
  249. /* Exotic things */
  250. case 'W':
  251. if ((weekday= check_word(my_locale_en_US.day_names, val, val_end, &val)) <= 0)
  252. goto err;
  253. break;
  254. case 'a':
  255. if ((weekday= check_word(my_locale_en_US.ab_day_names, val, val_end, &val)) <= 0)
  256. goto err;
  257. break;
  258. case 'w':
  259. tmp= (char*) val + 1;
  260. if ((weekday= (int) my_strtoll10(val, &tmp, &error)) < 0 ||
  261. weekday >= 7)
  262. goto err;
  263. /* We should use the same 1 - 7 scale for %w as for %W */
  264. if (!weekday)
  265. weekday= 7;
  266. val= tmp;
  267. break;
  268. case 'j':
  269. tmp= (char*) val + min(val_len, 3);
  270. yearday= (int) my_strtoll10(val, &tmp, &error);
  271. val= tmp;
  272. break;
  273. /* Week numbers */
  274. case 'V':
  275. case 'U':
  276. case 'v':
  277. case 'u':
  278. sunday_first_n_first_week_non_iso= (*ptr=='U' || *ptr== 'V');
  279. strict_week_number= (*ptr=='V' || *ptr=='v');
  280. tmp= (char*) val + min(val_len, 2);
  281. if ((week_number= (int) my_strtoll10(val, &tmp, &error)) < 0 ||
  282. strict_week_number && !week_number ||
  283. week_number > 53)
  284. goto err;
  285. val= tmp;
  286. break;
  287. /* Year used with 'strict' %V and %v week numbers */
  288. case 'X':
  289. case 'x':
  290. strict_week_number_year_type= (*ptr=='X');
  291. tmp= (char*) val + min(4, val_len);
  292. strict_week_number_year= (int) my_strtoll10(val, &tmp, &error);
  293. val= tmp;
  294. break;
  295. /* Time in AM/PM notation */
  296. case 'r':
  297. /*
  298. We can't just set error here, as we don't want to generate two
  299. warnings in case of errors
  300. */
  301. if (extract_date_time(&time_ampm_format, val,
  302. (uint)(val_end - val), l_time,
  303. cached_timestamp_type, &val, "time"))
  304. DBUG_RETURN(1);
  305. break;
  306. /* Time in 24-hour notation */
  307. case 'T':
  308. if (extract_date_time(&time_24hrs_format, val,
  309. (uint)(val_end - val), l_time,
  310. cached_timestamp_type, &val, "time"))
  311. DBUG_RETURN(1);
  312. break;
  313. /* Conversion specifiers that match classes of characters */
  314. case '.':
  315. while (my_ispunct(cs, *val) && val != val_end)
  316. val++;
  317. break;
  318. case '@':
  319. while (my_isalpha(cs, *val) && val != val_end)
  320. val++;
  321. break;
  322. case '#':
  323. while (my_isdigit(cs, *val) && val != val_end)
  324. val++;
  325. break;
  326. default:
  327. goto err;
  328. }
  329. if (error) // Error from my_strtoll10
  330. goto err;
  331. }
  332. else if (!my_isspace(cs, *ptr))
  333. {
  334. if (*val != *ptr)
  335. goto err;
  336. val++;
  337. }
  338. }
  339. if (usa_time)
  340. {
  341. if (l_time->hour > 12 || l_time->hour < 1)
  342. goto err;
  343. l_time->hour= l_time->hour%12+daypart;
  344. }
  345. /*
  346. If we are recursively called for parsing string matching compound
  347. specifiers we are already done.
  348. */
  349. if (sub_pattern_end)
  350. {
  351. *sub_pattern_end= val;
  352. DBUG_RETURN(0);
  353. }
  354. if (yearday > 0)
  355. {
  356. uint days= calc_daynr(l_time->year,1,1) + yearday - 1;
  357. if (days <= 0 || days > MAX_DAY_NUMBER)
  358. goto err;
  359. get_date_from_daynr(days,&l_time->year,&l_time->month,&l_time->day);
  360. }
  361. if (week_number >= 0 && weekday)
  362. {
  363. int days;
  364. uint weekday_b;
  365. /*
  366. %V,%v require %X,%x resprectively,
  367. %U,%u should be used with %Y and not %X or %x
  368. */
  369. if (strict_week_number &&
  370. (strict_week_number_year < 0 ||
  371. strict_week_number_year_type != sunday_first_n_first_week_non_iso) ||
  372. !strict_week_number && strict_week_number_year >= 0)
  373. goto err;
  374. /* Number of days since year 0 till 1st Jan of this year */
  375. days= calc_daynr((strict_week_number ? strict_week_number_year :
  376. l_time->year),
  377. 1, 1);
  378. /* Which day of week is 1st Jan of this year */
  379. weekday_b= calc_weekday(days, sunday_first_n_first_week_non_iso);
  380. /*
  381. Below we are going to sum:
  382. 1) number of days since year 0 till 1st day of 1st week of this year
  383. 2) number of days between 1st week and our week
  384. 3) and position of our day in the week
  385. */
  386. if (sunday_first_n_first_week_non_iso)
  387. {
  388. days+= ((weekday_b == 0) ? 0 : 7) - weekday_b +
  389. (week_number - 1) * 7 +
  390. weekday % 7;
  391. }
  392. else
  393. {
  394. days+= ((weekday_b <= 3) ? 0 : 7) - weekday_b +
  395. (week_number - 1) * 7 +
  396. (weekday - 1);
  397. }
  398. if (days <= 0 || days > MAX_DAY_NUMBER)
  399. goto err;
  400. get_date_from_daynr(days,&l_time->year,&l_time->month,&l_time->day);
  401. }
  402. if (l_time->month > 12 || l_time->day > 31 || l_time->hour > 23 ||
  403. l_time->minute > 59 || l_time->second > 59)
  404. goto err;
  405. if (val != val_end)
  406. {
  407. do
  408. {
  409. if (!my_isspace(&my_charset_latin1,*val))
  410. {
  411. make_truncated_value_warning(current_thd, val_begin, length,
  412. cached_timestamp_type, NullS);
  413. break;
  414. }
  415. } while (++val != val_end);
  416. }
  417. DBUG_RETURN(0);
  418. err:
  419. {
  420. char buff[128];
  421. strmake(buff, val_begin, min(length, sizeof(buff)-1));
  422. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
  423. ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
  424. date_time_type, buff, "str_to_time");
  425. }
  426. DBUG_RETURN(1);
  427. }
  428. /*
  429. Create a formated date/time value in a string
  430. */
  431. bool make_date_time(DATE_TIME_FORMAT *format, TIME *l_time,
  432. timestamp_type type, String *str)
  433. {
  434. char intbuff[15];
  435. uint hours_i;
  436. uint weekday;
  437. ulong length;
  438. const char *ptr, *end;
  439. MY_LOCALE *locale;
  440. THD *thd= current_thd;
  441. char buf[STRING_BUFFER_USUAL_SIZE];
  442. String tmp(buf, sizeof(buf), thd->variables.character_set_results);
  443. uint errors= 0;
  444. tmp.length(0);
  445. str->length(0);
  446. str->set_charset(&my_charset_bin);
  447. locale = thd->variables.lc_time_names;
  448. if (l_time->neg)
  449. str->append('-');
  450. end= (ptr= format->format.str) + format->format.length;
  451. for (; ptr != end ; ptr++)
  452. {
  453. if (*ptr != '%' || ptr+1 == end)
  454. str->append(*ptr);
  455. else
  456. {
  457. switch (*++ptr) {
  458. case 'M':
  459. if (!l_time->month)
  460. return 1;
  461. tmp.copy(locale->month_names->type_names[l_time->month-1],
  462. strlen(locale->month_names->type_names[l_time->month-1]),
  463. system_charset_info, tmp.charset(), &errors);
  464. str->append(tmp.ptr(), tmp.length());
  465. break;
  466. case 'b':
  467. if (!l_time->month)
  468. return 1;
  469. tmp.copy(locale->ab_month_names->type_names[l_time->month-1],
  470. strlen(locale->ab_month_names->type_names[l_time->month-1]),
  471. system_charset_info, tmp.charset(), &errors);
  472. str->append(tmp.ptr(), tmp.length());
  473. break;
  474. case 'W':
  475. if (type == MYSQL_TIMESTAMP_TIME)
  476. return 1;
  477. weekday= calc_weekday(calc_daynr(l_time->year,l_time->month,
  478. l_time->day),0);
  479. tmp.copy(locale->day_names->type_names[weekday],
  480. strlen(locale->day_names->type_names[weekday]),
  481. system_charset_info, tmp.charset(), &errors);
  482. str->append(tmp.ptr(), tmp.length());
  483. break;
  484. case 'a':
  485. if (type == MYSQL_TIMESTAMP_TIME)
  486. return 1;
  487. weekday=calc_weekday(calc_daynr(l_time->year,l_time->month,
  488. l_time->day),0);
  489. tmp.copy(locale->ab_day_names->type_names[weekday],
  490. strlen(locale->ab_day_names->type_names[weekday]),
  491. system_charset_info, tmp.charset(), &errors);
  492. str->append(tmp.ptr(), tmp.length());
  493. break;
  494. case 'D':
  495. if (type == MYSQL_TIMESTAMP_TIME)
  496. return 1;
  497. length= int10_to_str(l_time->day, intbuff, 10) - intbuff;
  498. str->append_with_prefill(intbuff, length, 1, '0');
  499. if (l_time->day >= 10 && l_time->day <= 19)
  500. str->append(STRING_WITH_LEN("th"));
  501. else
  502. {
  503. switch (l_time->day %10) {
  504. case 1:
  505. str->append(STRING_WITH_LEN("st"));
  506. break;
  507. case 2:
  508. str->append(STRING_WITH_LEN("nd"));
  509. break;
  510. case 3:
  511. str->append(STRING_WITH_LEN("rd"));
  512. break;
  513. default:
  514. str->append(STRING_WITH_LEN("th"));
  515. break;
  516. }
  517. }
  518. break;
  519. case 'Y':
  520. length= int10_to_str(l_time->year, intbuff, 10) - intbuff;
  521. str->append_with_prefill(intbuff, length, 4, '0');
  522. break;
  523. case 'y':
  524. length= int10_to_str(l_time->year%100, intbuff, 10) - intbuff;
  525. str->append_with_prefill(intbuff, length, 2, '0');
  526. break;
  527. case 'm':
  528. length= int10_to_str(l_time->month, intbuff, 10) - intbuff;
  529. str->append_with_prefill(intbuff, length, 2, '0');
  530. break;
  531. case 'c':
  532. length= int10_to_str(l_time->month, intbuff, 10) - intbuff;
  533. str->append_with_prefill(intbuff, length, 1, '0');
  534. break;
  535. case 'd':
  536. length= int10_to_str(l_time->day, intbuff, 10) - intbuff;
  537. str->append_with_prefill(intbuff, length, 2, '0');
  538. break;
  539. case 'e':
  540. length= int10_to_str(l_time->day, intbuff, 10) - intbuff;
  541. str->append_with_prefill(intbuff, length, 1, '0');
  542. break;
  543. case 'f':
  544. length= int10_to_str(l_time->second_part, intbuff, 10) - intbuff;
  545. str->append_with_prefill(intbuff, length, 6, '0');
  546. break;
  547. case 'H':
  548. length= int10_to_str(l_time->hour, intbuff, 10) - intbuff;
  549. str->append_with_prefill(intbuff, length, 2, '0');
  550. break;
  551. case 'h':
  552. case 'I':
  553. hours_i= (l_time->hour%24 + 11)%12+1;
  554. length= int10_to_str(hours_i, intbuff, 10) - intbuff;
  555. str->append_with_prefill(intbuff, length, 2, '0');
  556. break;
  557. case 'i': /* minutes */
  558. length= int10_to_str(l_time->minute, intbuff, 10) - intbuff;
  559. str->append_with_prefill(intbuff, length, 2, '0');
  560. break;
  561. case 'j':
  562. if (type == MYSQL_TIMESTAMP_TIME)
  563. return 1;
  564. length= int10_to_str(calc_daynr(l_time->year,l_time->month,
  565. l_time->day) -
  566. calc_daynr(l_time->year,1,1) + 1, intbuff, 10) - intbuff;
  567. str->append_with_prefill(intbuff, length, 3, '0');
  568. break;
  569. case 'k':
  570. length= int10_to_str(l_time->hour, intbuff, 10) - intbuff;
  571. str->append_with_prefill(intbuff, length, 1, '0');
  572. break;
  573. case 'l':
  574. hours_i= (l_time->hour%24 + 11)%12+1;
  575. length= int10_to_str(hours_i, intbuff, 10) - intbuff;
  576. str->append_with_prefill(intbuff, length, 1, '0');
  577. break;
  578. case 'p':
  579. hours_i= l_time->hour%24;
  580. str->append(hours_i < 12 ? "AM" : "PM",2);
  581. break;
  582. case 'r':
  583. length= my_sprintf(intbuff,
  584. (intbuff,
  585. ((l_time->hour % 24) < 12) ?
  586. "%02d:%02d:%02d AM" : "%02d:%02d:%02d PM",
  587. (l_time->hour+11)%12+1,
  588. l_time->minute,
  589. l_time->second));
  590. str->append(intbuff, length);
  591. break;
  592. case 'S':
  593. case 's':
  594. length= int10_to_str(l_time->second, intbuff, 10) - intbuff;
  595. str->append_with_prefill(intbuff, length, 2, '0');
  596. break;
  597. case 'T':
  598. length= my_sprintf(intbuff,
  599. (intbuff,
  600. "%02d:%02d:%02d",
  601. l_time->hour,
  602. l_time->minute,
  603. l_time->second));
  604. str->append(intbuff, length);
  605. break;
  606. case 'U':
  607. case 'u':
  608. {
  609. uint year;
  610. if (type == MYSQL_TIMESTAMP_TIME)
  611. return 1;
  612. length= int10_to_str(calc_week(l_time,
  613. (*ptr) == 'U' ?
  614. WEEK_FIRST_WEEKDAY : WEEK_MONDAY_FIRST,
  615. &year),
  616. intbuff, 10) - intbuff;
  617. str->append_with_prefill(intbuff, length, 2, '0');
  618. }
  619. break;
  620. case 'v':
  621. case 'V':
  622. {
  623. uint year;
  624. if (type == MYSQL_TIMESTAMP_TIME)
  625. return 1;
  626. length= int10_to_str(calc_week(l_time,
  627. ((*ptr) == 'V' ?
  628. (WEEK_YEAR | WEEK_FIRST_WEEKDAY) :
  629. (WEEK_YEAR | WEEK_MONDAY_FIRST)),
  630. &year),
  631. intbuff, 10) - intbuff;
  632. str->append_with_prefill(intbuff, length, 2, '0');
  633. }
  634. break;
  635. case 'x':
  636. case 'X':
  637. {
  638. uint year;
  639. if (type == MYSQL_TIMESTAMP_TIME)
  640. return 1;
  641. (void) calc_week(l_time,
  642. ((*ptr) == 'X' ?
  643. WEEK_YEAR | WEEK_FIRST_WEEKDAY :
  644. WEEK_YEAR | WEEK_MONDAY_FIRST),
  645. &year);
  646. length= int10_to_str(year, intbuff, 10) - intbuff;
  647. str->append_with_prefill(intbuff, length, 4, '0');
  648. }
  649. break;
  650. case 'w':
  651. if (type == MYSQL_TIMESTAMP_TIME)
  652. return 1;
  653. weekday=calc_weekday(calc_daynr(l_time->year,l_time->month,
  654. l_time->day),1);
  655. length= int10_to_str(weekday, intbuff, 10) - intbuff;
  656. str->append_with_prefill(intbuff, length, 1, '0');
  657. break;
  658. default:
  659. str->append(*ptr);
  660. break;
  661. }
  662. }
  663. }
  664. return 0;
  665. }
  666. /*
  667. Get a array of positive numbers from a string object.
  668. Each number is separated by 1 non digit character
  669. Return error if there is too many numbers.
  670. If there is too few numbers, assume that the numbers are left out
  671. from the high end. This allows one to give:
  672. DAY_TO_SECOND as "D MM:HH:SS", "MM:HH:SS" "HH:SS" or as seconds.
  673. SYNOPSIS
  674. str: string value
  675. length: length of str
  676. cs: charset of str
  677. values: array of results
  678. count: count of elements in result array
  679. transform_msec: if value is true we suppose
  680. that the last part of string value is microseconds
  681. and we should transform value to six digit value.
  682. For example, '1.1' -> '1.100000'
  683. */
  684. static bool get_interval_info(const char *str,uint length,CHARSET_INFO *cs,
  685. uint count, ulonglong *values,
  686. bool transform_msec)
  687. {
  688. const char *end=str+length;
  689. uint i;
  690. while (str != end && !my_isdigit(cs,*str))
  691. str++;
  692. for (i=0 ; i < count ; i++)
  693. {
  694. longlong value;
  695. const char *start= str;
  696. for (value=0; str != end && my_isdigit(cs,*str) ; str++)
  697. value= value*LL(10) + (longlong) (*str - '0');
  698. if (transform_msec && i == count - 1) // microseconds always last
  699. {
  700. long msec_length= 6 - (str - start);
  701. if (msec_length > 0)
  702. value*= (long) log_10_int[msec_length];
  703. }
  704. values[i]= value;
  705. while (str != end && !my_isdigit(cs,*str))
  706. str++;
  707. if (str == end && i != count-1)
  708. {
  709. i++;
  710. /* Change values[0...i-1] -> values[0...count-1] */
  711. bmove_upp((char*) (values+count), (char*) (values+i),
  712. sizeof(*values)*i);
  713. bzero((char*) values, sizeof(*values)*(count-i));
  714. break;
  715. }
  716. }
  717. return (str != end);
  718. }
  719. longlong Item_func_period_add::val_int()
  720. {
  721. DBUG_ASSERT(fixed == 1);
  722. ulong period=(ulong) args[0]->val_int();
  723. int months=(int) args[1]->val_int();
  724. if ((null_value=args[0]->null_value || args[1]->null_value) ||
  725. period == 0L)
  726. return 0; /* purecov: inspected */
  727. return (longlong)
  728. convert_month_to_period((uint) ((int) convert_period_to_month(period)+
  729. months));
  730. }
  731. longlong Item_func_period_diff::val_int()
  732. {
  733. DBUG_ASSERT(fixed == 1);
  734. ulong period1=(ulong) args[0]->val_int();
  735. ulong period2=(ulong) args[1]->val_int();
  736. if ((null_value=args[0]->null_value || args[1]->null_value))
  737. return 0; /* purecov: inspected */
  738. return (longlong) ((long) convert_period_to_month(period1)-
  739. (long) convert_period_to_month(period2));
  740. }
  741. longlong Item_func_to_days::val_int()
  742. {
  743. DBUG_ASSERT(fixed == 1);
  744. TIME ltime;
  745. if (get_arg0_date(&ltime, TIME_NO_ZERO_DATE))
  746. return 0;
  747. return (longlong) calc_daynr(ltime.year,ltime.month,ltime.day);
  748. }
  749. /*
  750. Get information about this Item tree monotonicity
  751. SYNOPSIS
  752. Item_func_to_days::get_monotonicity_info()
  753. DESCRIPTION
  754. Get information about monotonicity of the function represented by this item
  755. tree.
  756. RETURN
  757. See enum_monotonicity_info.
  758. */
  759. enum_monotonicity_info Item_func_to_days::get_monotonicity_info() const
  760. {
  761. if (args[0]->type() == Item::FIELD_ITEM)
  762. {
  763. if (args[0]->field_type() == MYSQL_TYPE_DATE)
  764. return MONOTONIC_STRICT_INCREASING;
  765. if (args[0]->field_type() == MYSQL_TYPE_DATETIME)
  766. return MONOTONIC_INCREASING;
  767. }
  768. return NON_MONOTONIC;
  769. }
  770. longlong Item_func_dayofyear::val_int()
  771. {
  772. DBUG_ASSERT(fixed == 1);
  773. TIME ltime;
  774. if (get_arg0_date(&ltime,TIME_NO_ZERO_DATE))
  775. return 0;
  776. return (longlong) calc_daynr(ltime.year,ltime.month,ltime.day) -
  777. calc_daynr(ltime.year,1,1) + 1;
  778. }
  779. longlong Item_func_dayofmonth::val_int()
  780. {
  781. DBUG_ASSERT(fixed == 1);
  782. TIME ltime;
  783. (void) get_arg0_date(&ltime, TIME_FUZZY_DATE);
  784. return (longlong) ltime.day;
  785. }
  786. longlong Item_func_month::val_int()
  787. {
  788. DBUG_ASSERT(fixed == 1);
  789. TIME ltime;
  790. (void) get_arg0_date(&ltime, TIME_FUZZY_DATE);
  791. return (longlong) ltime.month;
  792. }
  793. String* Item_func_monthname::val_str(String* str)
  794. {
  795. DBUG_ASSERT(fixed == 1);
  796. const char *month_name;
  797. uint month= (uint) val_int();
  798. THD *thd= current_thd;
  799. if (null_value || !month)
  800. {
  801. null_value=1;
  802. return (String*) 0;
  803. }
  804. null_value=0;
  805. month_name= thd->variables.lc_time_names->month_names->type_names[month-1];
  806. str->set(month_name, strlen(month_name), system_charset_info);
  807. return str;
  808. }
  809. // Returns the quarter of the year
  810. longlong Item_func_quarter::val_int()
  811. {
  812. DBUG_ASSERT(fixed == 1);
  813. TIME ltime;
  814. (void) get_arg0_date(&ltime, TIME_FUZZY_DATE);
  815. return (longlong) ((ltime.month+2)/3);
  816. }
  817. longlong Item_func_hour::val_int()
  818. {
  819. DBUG_ASSERT(fixed == 1);
  820. TIME ltime;
  821. (void) get_arg0_time(&ltime);
  822. return ltime.hour;
  823. }
  824. longlong Item_func_minute::val_int()
  825. {
  826. DBUG_ASSERT(fixed == 1);
  827. TIME ltime;
  828. (void) get_arg0_time(&ltime);
  829. return ltime.minute;
  830. }
  831. // Returns the second in time_exp in the range of 0 - 59
  832. longlong Item_func_second::val_int()
  833. {
  834. DBUG_ASSERT(fixed == 1);
  835. TIME ltime;
  836. (void) get_arg0_time(&ltime);
  837. return ltime.second;
  838. }
  839. uint week_mode(uint mode)
  840. {
  841. uint week_format= (mode & 7);
  842. if (!(week_format & WEEK_MONDAY_FIRST))
  843. week_format^= WEEK_FIRST_WEEKDAY;
  844. return week_format;
  845. }
  846. /*
  847. The bits in week_format(for calc_week() function) has the following meaning:
  848. WEEK_MONDAY_FIRST (0) If not set Sunday is first day of week
  849. If set Monday is first day of week
  850. WEEK_YEAR (1) If not set Week is in range 0-53
  851. Week 0 is returned for the the last week of the previous year (for
  852. a date at start of january) In this case one can get 53 for the
  853. first week of next year. This flag ensures that the week is
  854. relevant for the given year. Note that this flag is only
  855. releveant if WEEK_JANUARY is not set.
  856. If set Week is in range 1-53.
  857. In this case one may get week 53 for a date in January (when
  858. the week is that last week of previous year) and week 1 for a
  859. date in December.
  860. WEEK_FIRST_WEEKDAY (2) If not set Weeks are numbered according
  861. to ISO 8601:1988
  862. If set The week that contains the first
  863. 'first-day-of-week' is week 1.
  864. ISO 8601:1988 means that if the week containing January 1 has
  865. four or more days in the new year, then it is week 1;
  866. Otherwise it is the last week of the previous year, and the
  867. next week is week 1.
  868. */
  869. longlong Item_func_week::val_int()
  870. {
  871. DBUG_ASSERT(fixed == 1);
  872. uint year;
  873. TIME ltime;
  874. if (get_arg0_date(&ltime, TIME_NO_ZERO_DATE))
  875. return 0;
  876. return (longlong) calc_week(&ltime,
  877. week_mode((uint) args[1]->val_int()),
  878. &year);
  879. }
  880. longlong Item_func_yearweek::val_int()
  881. {
  882. DBUG_ASSERT(fixed == 1);
  883. uint year,week;
  884. TIME ltime;
  885. if (get_arg0_date(&ltime, TIME_NO_ZERO_DATE))
  886. return 0;
  887. week= calc_week(&ltime,
  888. (week_mode((uint) args[1]->val_int()) | WEEK_YEAR),
  889. &year);
  890. return week+year*100;
  891. }
  892. longlong Item_func_weekday::val_int()
  893. {
  894. DBUG_ASSERT(fixed == 1);
  895. TIME ltime;
  896. if (get_arg0_date(&ltime, TIME_NO_ZERO_DATE))
  897. return 0;
  898. return (longlong) calc_weekday(calc_daynr(ltime.year, ltime.month,
  899. ltime.day),
  900. odbc_type) + test(odbc_type);
  901. }
  902. String* Item_func_dayname::val_str(String* str)
  903. {
  904. DBUG_ASSERT(fixed == 1);
  905. uint weekday=(uint) val_int(); // Always Item_func_daynr()
  906. const char *name;
  907. THD *thd= current_thd;
  908. if (null_value)
  909. return (String*) 0;
  910. name= thd->variables.lc_time_names->day_names->type_names[weekday];
  911. str->set(name, strlen(name), system_charset_info);
  912. return str;
  913. }
  914. longlong Item_func_year::val_int()
  915. {
  916. DBUG_ASSERT(fixed == 1);
  917. TIME ltime;
  918. (void) get_arg0_date(&ltime, TIME_FUZZY_DATE);
  919. return (longlong) ltime.year;
  920. }
  921. /*
  922. Get information about this Item tree monotonicity
  923. SYNOPSIS
  924. Item_func_to_days::get_monotonicity_info()
  925. DESCRIPTION
  926. Get information about monotonicity of the function represented by this item
  927. tree.
  928. RETURN
  929. See enum_monotonicity_info.
  930. */
  931. enum_monotonicity_info Item_func_year::get_monotonicity_info() const
  932. {
  933. if (args[0]->type() == Item::FIELD_ITEM &&
  934. (args[0]->field_type() == MYSQL_TYPE_DATE ||
  935. args[0]->field_type() == MYSQL_TYPE_DATETIME))
  936. return MONOTONIC_INCREASING;
  937. return NON_MONOTONIC;
  938. }
  939. longlong Item_func_unix_timestamp::val_int()
  940. {
  941. TIME ltime;
  942. my_bool not_used;
  943. DBUG_ASSERT(fixed == 1);
  944. if (arg_count == 0)
  945. return (longlong) current_thd->query_start();
  946. if (args[0]->type() == FIELD_ITEM)
  947. { // Optimize timestamp field
  948. Field *field=((Item_field*) args[0])->field;
  949. if (field->type() == FIELD_TYPE_TIMESTAMP)
  950. return ((Field_timestamp*) field)->get_timestamp(&null_value);
  951. }
  952. if (get_arg0_date(&ltime, 0))
  953. {
  954. /*
  955. We have to set null_value again because get_arg0_date will also set it
  956. to true if we have wrong datetime parameter (and we should return 0 in
  957. this case).
  958. */
  959. null_value= args[0]->null_value;
  960. return 0;
  961. }
  962. return (longlong) TIME_to_timestamp(current_thd, &ltime, &not_used);
  963. }
  964. longlong Item_func_time_to_sec::val_int()
  965. {
  966. DBUG_ASSERT(fixed == 1);
  967. TIME ltime;
  968. longlong seconds;
  969. (void) get_arg0_time(&ltime);
  970. seconds=ltime.hour*3600L+ltime.minute*60+ltime.second;
  971. return ltime.neg ? -seconds : seconds;
  972. }
  973. /*
  974. Convert a string to a interval value
  975. To make code easy, allow interval objects without separators.
  976. */
  977. bool get_interval_value(Item *args,interval_type int_type,
  978. String *str_value, INTERVAL *interval)
  979. {
  980. ulonglong array[5];
  981. longlong value;
  982. const char *str;
  983. uint32 length;
  984. CHARSET_INFO *cs=str_value->charset();
  985. LINT_INIT(value);
  986. LINT_INIT(str);
  987. LINT_INIT(length);
  988. bzero((char*) interval,sizeof(*interval));
  989. if ((int) int_type <= INTERVAL_MICROSECOND)
  990. {
  991. value= args->val_int();
  992. if (args->null_value)
  993. return 1;
  994. if (value < 0)
  995. {
  996. interval->neg=1;
  997. value= -value;
  998. }
  999. }
  1000. else
  1001. {
  1002. String *res;
  1003. if (!(res=args->val_str(str_value)))
  1004. return (1);
  1005. /* record negative intervalls in interval->neg */
  1006. str=res->ptr();
  1007. const char *end=str+res->length();
  1008. while (str != end && my_isspace(cs,*str))
  1009. str++;
  1010. if (str != end && *str == '-')
  1011. {
  1012. interval->neg=1;
  1013. str++;
  1014. }
  1015. length=(uint32) (end-str); // Set up pointers to new str
  1016. }
  1017. switch (int_type) {
  1018. case INTERVAL_YEAR:
  1019. interval->year= (ulong) value;
  1020. break;
  1021. case INTERVAL_QUARTER:
  1022. interval->month= (ulong)(value*3);
  1023. break;
  1024. case INTERVAL_MONTH:
  1025. interval->month= (ulong) value;
  1026. break;
  1027. case INTERVAL_WEEK:
  1028. interval->day= (ulong)(value*7);
  1029. break;
  1030. case INTERVAL_DAY:
  1031. interval->day= (ulong) value;
  1032. break;
  1033. case INTERVAL_HOUR:
  1034. interval->hour= (ulong) value;
  1035. break;
  1036. case INTERVAL_MICROSECOND:
  1037. interval->second_part=value;
  1038. break;
  1039. case INTERVAL_MINUTE:
  1040. interval->minute=value;
  1041. break;
  1042. case INTERVAL_SECOND:
  1043. interval->second=value;
  1044. break;
  1045. case INTERVAL_YEAR_MONTH: // Allow YEAR-MONTH YYYYYMM
  1046. if (get_interval_info(str,length,cs,2,array,0))
  1047. return (1);
  1048. interval->year= (ulong) array[0];
  1049. interval->month= (ulong) array[1];
  1050. break;
  1051. case INTERVAL_DAY_HOUR:
  1052. if (get_interval_info(str,length,cs,2,array,0))
  1053. return (1);
  1054. interval->day= (ulong) array[0];
  1055. interval->hour= (ulong) array[1];
  1056. break;
  1057. case INTERVAL_DAY_MICROSECOND:
  1058. if (get_interval_info(str,length,cs,5,array,1))
  1059. return (1);
  1060. interval->day= (ulong) array[0];
  1061. interval->hour= (ulong) array[1];
  1062. interval->minute= array[2];
  1063. interval->second= array[3];
  1064. interval->second_part= array[4];
  1065. break;
  1066. case INTERVAL_DAY_MINUTE:
  1067. if (get_interval_info(str,length,cs,3,array,0))
  1068. return (1);
  1069. interval->day= (ulong) array[0];
  1070. interval->hour= (ulong) array[1];
  1071. interval->minute= array[2];
  1072. break;
  1073. case INTERVAL_DAY_SECOND:
  1074. if (get_interval_info(str,length,cs,4,array,0))
  1075. return (1);
  1076. interval->day= (ulong) array[0];
  1077. interval->hour= (ulong) array[1];
  1078. interval->minute= array[2];
  1079. interval->second= array[3];
  1080. break;
  1081. case INTERVAL_HOUR_MICROSECOND:
  1082. if (get_interval_info(str,length,cs,4,array,1))
  1083. return (1);
  1084. interval->hour= (ulong) array[0];
  1085. interval->minute= array[1];
  1086. interval->second= array[2];
  1087. interval->second_part= array[3];
  1088. break;
  1089. case INTERVAL_HOUR_MINUTE:
  1090. if (get_interval_info(str,length,cs,2,array,0))
  1091. return (1);
  1092. interval->hour= (ulong) array[0];
  1093. interval->minute= array[1];
  1094. break;
  1095. case INTERVAL_HOUR_SECOND:
  1096. if (get_interval_info(str,length,cs,3,array,0))
  1097. return (1);
  1098. interval->hour= (ulong) array[0];
  1099. interval->minute= array[1];
  1100. interval->second= array[2];
  1101. break;
  1102. case INTERVAL_MINUTE_MICROSECOND:
  1103. if (get_interval_info(str,length,cs,3,array,1))
  1104. return (1);
  1105. interval->minute= array[0];
  1106. interval->second= array[1];
  1107. interval->second_part= array[2];
  1108. break;
  1109. case INTERVAL_MINUTE_SECOND:
  1110. if (get_interval_info(str,length,cs,2,array,0))
  1111. return (1);
  1112. interval->minute= array[0];
  1113. interval->second= array[1];
  1114. break;
  1115. case INTERVAL_SECOND_MICROSECOND:
  1116. if (get_interval_info(str,length,cs,2,array,1))
  1117. return (1);
  1118. interval->second= array[0];
  1119. interval->second_part= array[1];
  1120. break;
  1121. }
  1122. return 0;
  1123. }
  1124. String *Item_date::val_str(String *str)
  1125. {
  1126. DBUG_ASSERT(fixed == 1);
  1127. TIME ltime;
  1128. if (get_date(&ltime, TIME_FUZZY_DATE))
  1129. return (String *) 0;
  1130. if (str->alloc(11))
  1131. {
  1132. null_value= 1;
  1133. return (String *) 0;
  1134. }
  1135. make_date((DATE_TIME_FORMAT *) 0, &ltime, str);
  1136. return str;
  1137. }
  1138. int Item_date::save_in_field(Field *field, bool no_conversions)
  1139. {
  1140. TIME ltime;
  1141. if (get_date(&ltime, TIME_FUZZY_DATE))
  1142. return set_field_to_null(field);
  1143. field->set_notnull();
  1144. field->store_time(&ltime, MYSQL_TIMESTAMP_DATE);
  1145. return 0;
  1146. }
  1147. longlong Item_date::val_int()
  1148. {
  1149. DBUG_ASSERT(fixed == 1);
  1150. TIME ltime;
  1151. if (get_date(&ltime, TIME_FUZZY_DATE))
  1152. return 0;
  1153. return (longlong) (ltime.year*10000L+ltime.month*100+ltime.day);
  1154. }
  1155. bool Item_func_from_days::get_date(TIME *ltime, uint fuzzy_date)
  1156. {
  1157. longlong value=args[0]->val_int();
  1158. if ((null_value=args[0]->null_value))
  1159. return 1;
  1160. bzero(ltime, sizeof(TIME));
  1161. get_date_from_daynr((long) value, &ltime->year, &ltime->month, &ltime->day);
  1162. ltime->time_type= MYSQL_TIMESTAMP_DATE;
  1163. return 0;
  1164. }
  1165. void Item_func_curdate::fix_length_and_dec()
  1166. {
  1167. collation.set(&my_charset_bin);
  1168. decimals=0;
  1169. max_length=MAX_DATE_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
  1170. store_now_in_TIME(&ltime);
  1171. /* We don't need to set second_part and neg because they already 0 */
  1172. ltime.hour= ltime.minute= ltime.second= 0;
  1173. ltime.time_type= MYSQL_TIMESTAMP_DATE;
  1174. value= (longlong) TIME_to_ulonglong_date(&ltime);
  1175. }
  1176. String *Item_func_curdate::val_str(String *str)
  1177. {
  1178. DBUG_ASSERT(fixed == 1);
  1179. if (str->alloc(11))
  1180. {
  1181. null_value= 1;
  1182. return (String *) 0;
  1183. }
  1184. make_date((DATE_TIME_FORMAT *) 0, &ltime, str);
  1185. return str;
  1186. }
  1187. /*
  1188. Converts current time in my_time_t to TIME represenatation for local
  1189. time zone. Defines time zone (local) used for whole CURDATE function.
  1190. */
  1191. void Item_func_curdate_local::store_now_in_TIME(TIME *now_time)
  1192. {
  1193. THD *thd= current_thd;
  1194. thd->variables.time_zone->gmt_sec_to_TIME(now_time,
  1195. (my_time_t)thd->query_start());
  1196. thd->time_zone_used= 1;
  1197. }
  1198. /*
  1199. Converts current time in my_time_t to TIME represenatation for UTC
  1200. time zone. Defines time zone (UTC) used for whole UTC_DATE function.
  1201. */
  1202. void Item_func_curdate_utc::store_now_in_TIME(TIME *now_time)
  1203. {
  1204. my_tz_UTC->gmt_sec_to_TIME(now_time,
  1205. (my_time_t)(current_thd->query_start()));
  1206. /*
  1207. We are not flagging this query as using time zone, since it uses fixed
  1208. UTC-SYSTEM time-zone.
  1209. */
  1210. }
  1211. bool Item_func_curdate::get_date(TIME *res,
  1212. uint fuzzy_date __attribute__((unused)))
  1213. {
  1214. *res=ltime;
  1215. return 0;
  1216. }
  1217. String *Item_func_curtime::val_str(String *str)
  1218. {
  1219. DBUG_ASSERT(fixed == 1);
  1220. str_value.set(buff, buff_length, &my_charset_bin);
  1221. return &str_value;
  1222. }
  1223. void Item_func_curtime::fix_length_and_dec()
  1224. {
  1225. TIME ltime;
  1226. decimals= DATETIME_DEC;
  1227. collation.set(&my_charset_bin);
  1228. store_now_in_TIME(&ltime);
  1229. value= TIME_to_ulonglong_time(&ltime);
  1230. buff_length= (uint) my_time_to_str(&ltime, buff);
  1231. max_length= buff_length;
  1232. }
  1233. /*
  1234. Converts current time in my_time_t to TIME represenatation for local
  1235. time zone. Defines time zone (local) used for whole CURTIME function.
  1236. */
  1237. void Item_func_curtime_local::store_now_in_TIME(TIME *now_time)
  1238. {
  1239. THD *thd= current_thd;
  1240. thd->variables.time_zone->gmt_sec_to_TIME(now_time,
  1241. (my_time_t)thd->query_start());
  1242. thd->time_zone_used= 1;
  1243. }
  1244. /*
  1245. Converts current time in my_time_t to TIME represenatation for UTC
  1246. time zone. Defines time zone (UTC) used for whole UTC_TIME function.
  1247. */
  1248. void Item_func_curtime_utc::store_now_in_TIME(TIME *now_time)
  1249. {
  1250. my_tz_UTC->gmt_sec_to_TIME(now_time,
  1251. (my_time_t)(current_thd->query_start()));
  1252. /*
  1253. We are not flagging this query as using time zone, since it uses fixed
  1254. UTC-SYSTEM time-zone.
  1255. */
  1256. }
  1257. String *Item_func_now::val_str(String *str)
  1258. {
  1259. DBUG_ASSERT(fixed == 1);
  1260. str_value.set(buff,buff_length, &my_charset_bin);
  1261. return &str_value;
  1262. }
  1263. void Item_func_now::fix_length_and_dec()
  1264. {
  1265. decimals= DATETIME_DEC;
  1266. collation.set(&my_charset_bin);
  1267. store_now_in_TIME(&ltime);
  1268. value= (longlong) TIME_to_ulonglong_datetime(&ltime);
  1269. buff_length= (uint) my_datetime_to_str(&ltime, buff);
  1270. max_length= buff_length;
  1271. }
  1272. /*
  1273. Converts current time in my_time_t to TIME represenatation for local
  1274. time zone. Defines time zone (local) used for whole NOW function.
  1275. */
  1276. void Item_func_now_local::store_now_in_TIME(TIME *now_time)
  1277. {
  1278. THD *thd= current_thd;
  1279. thd->variables.time_zone->gmt_sec_to_TIME(now_time,
  1280. (my_time_t)thd->query_start());
  1281. thd->time_zone_used= 1;
  1282. }
  1283. /*
  1284. Converts current time in my_time_t to TIME represenatation for UTC
  1285. time zone. Defines time zone (UTC) used for whole UTC_TIMESTAMP function.
  1286. */
  1287. void Item_func_now_utc::store_now_in_TIME(TIME *now_time)
  1288. {
  1289. my_tz_UTC->gmt_sec_to_TIME(now_time,
  1290. (my_time_t)(current_thd->query_start()));
  1291. /*
  1292. We are not flagging this query as using time zone, since it uses fixed
  1293. UTC-SYSTEM time-zone.
  1294. */
  1295. }
  1296. bool Item_func_now::get_date(TIME *res,
  1297. uint fuzzy_date __attribute__((unused)))
  1298. {
  1299. *res= ltime;
  1300. return 0;
  1301. }
  1302. int Item_func_now::save_in_field(Field *to, bool no_conversions)
  1303. {
  1304. to->set_notnull();
  1305. to->store_time(&ltime, MYSQL_TIMESTAMP_DATETIME);
  1306. return 0;
  1307. }
  1308. /*
  1309. Converts current time in my_time_t to TIME represenatation for local
  1310. time zone. Defines time zone (local) used for whole SYSDATE function.
  1311. */
  1312. void Item_func_sysdate_local::store_now_in_TIME(TIME *now_time)
  1313. {
  1314. THD *thd= current_thd;
  1315. thd->variables.time_zone->gmt_sec_to_TIME(now_time, time(NULL));
  1316. thd->time_zone_used= 1;
  1317. }
  1318. String *Item_func_sysdate_local::val_str(String *str)
  1319. {
  1320. DBUG_ASSERT(fixed == 1);
  1321. store_now_in_TIME(&ltime);
  1322. buff_length= (uint) my_datetime_to_str(&ltime, buff);
  1323. str_value.set(buff, buff_length, &my_charset_bin);
  1324. return &str_value;
  1325. }
  1326. longlong Item_func_sysdate_local::val_int()
  1327. {
  1328. DBUG_ASSERT(fixed == 1);
  1329. store_now_in_TIME(&ltime);
  1330. return (longlong) TIME_to_ulonglong_datetime(&ltime);
  1331. }
  1332. double Item_func_sysdate_local::val_real()
  1333. {
  1334. DBUG_ASSERT(fixed == 1);
  1335. store_now_in_TIME(&ltime);
  1336. return ulonglong2double(TIME_to_ulonglong_datetime(&ltime));
  1337. }
  1338. void Item_func_sysdate_local::fix_length_and_dec()
  1339. {
  1340. decimals= 0;
  1341. collation.set(&my_charset_bin);
  1342. max_length= MAX_DATETIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
  1343. }
  1344. bool Item_func_sysdate_local::get_date(TIME *res,
  1345. uint fuzzy_date __attribute__((unused)))
  1346. {
  1347. store_now_in_TIME(&ltime);
  1348. *res= ltime;
  1349. return 0;
  1350. }
  1351. int Item_func_sysdate_local::save_in_field(Field *to, bool no_conversions)
  1352. {
  1353. store_now_in_TIME(&ltime);
  1354. to->set_notnull();
  1355. to->store_time(&ltime, MYSQL_TIMESTAMP_DATETIME);
  1356. return 0;
  1357. }
  1358. String *Item_func_sec_to_time::val_str(String *str)
  1359. {
  1360. DBUG_ASSERT(fixed == 1);
  1361. longlong seconds=(longlong) args[0]->val_int();
  1362. uint sec;
  1363. TIME ltime;
  1364. if ((null_value=args[0]->null_value) || str->alloc(19))
  1365. {
  1366. null_value= 1;
  1367. return (String*) 0;
  1368. }
  1369. ltime.neg= 0;
  1370. if (seconds < 0)
  1371. {
  1372. seconds= -seconds;
  1373. ltime.neg= 1;
  1374. }
  1375. sec= (uint) ((ulonglong) seconds % 3600);
  1376. ltime.day= 0;
  1377. ltime.hour= (uint) (seconds/3600);
  1378. ltime.minute= sec/60;
  1379. ltime.second= sec % 60;
  1380. make_time((DATE_TIME_FORMAT *) 0, &ltime, str);
  1381. return str;
  1382. }
  1383. longlong Item_func_sec_to_time::val_int()
  1384. {
  1385. DBUG_ASSERT(fixed == 1);
  1386. longlong seconds=args[0]->val_int();
  1387. longlong sign=1;
  1388. if ((null_value=args[0]->null_value))
  1389. return 0;
  1390. if (seconds < 0)
  1391. {
  1392. seconds= -seconds;
  1393. sign= -1;
  1394. }
  1395. return sign*((seconds / 3600)*10000+((seconds/60) % 60)*100+ (seconds % 60));
  1396. }
  1397. void Item_func_date_format::fix_length_and_dec()
  1398. {
  1399. /*
  1400. Must use this_item() in case it's a local SP variable
  1401. (for ->max_length and ->str_value)
  1402. */
  1403. Item *arg1= args[1]->this_item();
  1404. decimals=0;
  1405. collation.set(&my_charset_bin);
  1406. if (arg1->type() == STRING_ITEM)
  1407. { // Optimize the normal case
  1408. fixed_length=1;
  1409. /*
  1410. The result is a binary string (no reason to use collation->mbmaxlen
  1411. This is becasue make_date_time() only returns binary strings
  1412. */
  1413. max_length= format_length(&arg1->str_value);
  1414. }
  1415. else
  1416. {
  1417. fixed_length=0;
  1418. /* The result is a binary string (no reason to use collation->mbmaxlen */
  1419. max_length=min(arg1->max_length, MAX_BLOB_WIDTH) * 10;
  1420. set_if_smaller(max_length,MAX_BLOB_WIDTH);
  1421. }
  1422. maybe_null=1; // If wrong date
  1423. }
  1424. bool Item_func_date_format::eq(const Item *item, bool binary_cmp) const
  1425. {
  1426. Item_func_date_format *item_func;
  1427. if (item->type() != FUNC_ITEM)
  1428. return 0;
  1429. if (func_name() != ((Item_func*) item)->func_name())
  1430. return 0;
  1431. if (this == item)
  1432. return 1;
  1433. item_func= (Item_func_date_format*) item;
  1434. if (!args[0]->eq(item_func->args[0], binary_cmp))
  1435. return 0;
  1436. /*
  1437. We must compare format string case sensitive.
  1438. This needed because format modifiers with different case,
  1439. for example %m and %M, have different meaning.
  1440. */
  1441. if (!args[1]->eq(item_func->args[1], 1))
  1442. return 0;
  1443. return 1;
  1444. }
  1445. uint Item_func_date_format::format_length(const String *format)
  1446. {
  1447. uint size=0;
  1448. const char *ptr=format->ptr();
  1449. const char *end=ptr+format->length();
  1450. for (; ptr != end ; ptr++)
  1451. {
  1452. if (*ptr != '%' || ptr == end-1)
  1453. size++;
  1454. else
  1455. {
  1456. switch(*++ptr) {
  1457. case 'M': /* month, textual */
  1458. case 'W': /* day (of the week), textual */
  1459. size += 64; /* large for UTF8 locale data */
  1460. break;
  1461. case 'D': /* day (of the month), numeric plus english suffix */
  1462. case 'Y': /* year, numeric, 4 digits */
  1463. case 'x': /* Year, used with 'v' */
  1464. case 'X': /* Year, used with 'v, where week starts with Monday' */
  1465. size += 4;
  1466. break;
  1467. case 'a': /* locale's abbreviated weekday name (Sun..Sat) */
  1468. case 'b': /* locale's abbreviated month name (Jan.Dec) */
  1469. size += 32; /* large for UTF8 locale data */
  1470. break;
  1471. case 'j': /* day of year (001..366) */
  1472. size += 3;
  1473. break;
  1474. case 'U': /* week (00..52) */
  1475. case 'u': /* week (00..52), where week starts with Monday */
  1476. case 'V': /* week 1..53 used with 'x' */
  1477. case 'v': /* week 1..53 used with 'x', where week starts with Monday */
  1478. case 'y': /* year, numeric, 2 digits */
  1479. case 'm': /* month, numeric */
  1480. case 'd': /* day (of the month), numeric */
  1481. case 'h': /* hour (01..12) */
  1482. case 'I': /* --||-- */
  1483. case 'i': /* minutes, numeric */
  1484. case 'l': /* hour ( 1..12) */
  1485. case 'p': /* locale's AM or PM */
  1486. case 'S': /* second (00..61) */
  1487. case 's': /* seconds, numeric */
  1488. case 'c': /* month (0..12) */
  1489. case 'e': /* day (0..31) */
  1490. size += 2;
  1491. break;
  1492. case 'k': /* hour ( 0..23) */
  1493. case 'H': /* hour (00..23; value > 23 OK, padding always 2-digit) */
  1494. size += 7; /* docs allow > 23, range depends on sizeof(unsigned int) */
  1495. break;
  1496. case 'r': /* time, 12-hour (hh:mm:ss [AP]M) */
  1497. size += 11;
  1498. break;
  1499. case 'T': /* time, 24-hour (hh:mm:ss) */
  1500. size += 8;
  1501. break;
  1502. case 'f': /* microseconds */
  1503. size += 6;
  1504. break;
  1505. case 'w': /* day (of the week), numeric */
  1506. case '%':
  1507. default:
  1508. size++;
  1509. break;
  1510. }
  1511. }
  1512. }
  1513. return size;
  1514. }
  1515. String *Item_func_date_format::val_str(String *str)
  1516. {
  1517. String *format;
  1518. TIME l_time;
  1519. uint size;
  1520. DBUG_ASSERT(fixed == 1);
  1521. if (!is_time_format)
  1522. {
  1523. if (get_arg0_date(&l_time, TIME_FUZZY_DATE))
  1524. return 0;
  1525. }
  1526. else
  1527. {
  1528. String *res;
  1529. if (!(res=args[0]->val_str(str)) ||
  1530. (str_to_time_with_warn(res->ptr(), res->length(), &l_time)))
  1531. goto null_date;
  1532. l_time.year=l_time.month=l_time.day=0;
  1533. null_value=0;
  1534. }
  1535. if (!(format = args[1]->val_str(str)) || !format->length())
  1536. goto null_date;
  1537. if (fixed_length)
  1538. size=max_length;
  1539. else
  1540. size=format_length(format);
  1541. if (format == str)
  1542. str= &value; // Save result here
  1543. if (str->alloc(size))
  1544. goto null_date;
  1545. DATE_TIME_FORMAT date_time_format;
  1546. date_time_format.format.str= (char*) format->ptr();
  1547. date_time_format.format.length= format->length();
  1548. /* Create the result string */
  1549. if (!make_date_time(&date_time_format, &l_time,
  1550. is_time_format ? MYSQL_TIMESTAMP_TIME :
  1551. MYSQL_TIMESTAMP_DATE,
  1552. str))
  1553. return str;
  1554. null_date:
  1555. null_value=1;
  1556. return 0;
  1557. }
  1558. void Item_func_from_unixtime::fix_length_and_dec()
  1559. {
  1560. thd= current_thd;
  1561. collation.set(&my_charset_bin);
  1562. decimals= DATETIME_DEC;
  1563. max_length=MAX_DATETIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
  1564. maybe_null= 1;
  1565. thd->time_zone_used= 1;
  1566. }
  1567. String *Item_func_from_unixtime::val_str(String *str)
  1568. {
  1569. TIME time_tmp;
  1570. DBUG_ASSERT(fixed == 1);
  1571. if (get_date(&time_tmp, 0))
  1572. return 0;
  1573. if (str->alloc(20*MY_CHARSET_BIN_MB_MAXLEN))
  1574. {
  1575. null_value= 1;
  1576. return 0;
  1577. }
  1578. make_datetime((DATE_TIME_FORMAT *) 0, &time_tmp, str);
  1579. return str;
  1580. }
  1581. longlong Item_func_from_unixtime::val_int()
  1582. {
  1583. TIME time_tmp;
  1584. DBUG_ASSERT(fixed == 1);
  1585. if (get_date(&time_tmp, 0))
  1586. return 0;
  1587. return (longlong) TIME_to_ulonglong_datetime(&time_tmp);
  1588. }
  1589. bool Item_func_from_unixtime::get_date(TIME *ltime,
  1590. uint fuzzy_date __attribute__((unused)))
  1591. {
  1592. ulonglong tmp= (ulonglong)(args[0]->val_int());
  1593. /*
  1594. "tmp > TIMESTAMP_MAX_VALUE" check also covers case of negative
  1595. from_unixtime() argument since tmp is unsigned.
  1596. */
  1597. if ((null_value= (args[0]->null_value || tmp > TIMESTAMP_MAX_VALUE)))
  1598. return 1;
  1599. thd->variables.time_zone->gmt_sec_to_TIME(ltime, (my_time_t)tmp);
  1600. return 0;
  1601. }
  1602. void Item_func_convert_tz::fix_length_and_dec()
  1603. {
  1604. collation.set(&my_charset_bin);
  1605. decimals= 0;
  1606. max_length= MAX_DATETIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
  1607. maybe_null= 1;
  1608. }
  1609. bool
  1610. Item_func_convert_tz::fix_fields(THD *thd_arg, Item **ref)
  1611. {
  1612. String str;
  1613. if (Item_date_func::fix_fields(thd_arg, ref))
  1614. return TRUE;
  1615. tz_tables= thd_arg->lex->time_zone_tables_used;
  1616. return FALSE;
  1617. }
  1618. String *Item_func_convert_tz::val_str(String *str)
  1619. {
  1620. TIME time_tmp;
  1621. if (get_date(&time_tmp, 0))
  1622. return 0;
  1623. if (str->alloc(20*MY_CHARSET_BIN_MB_MAXLEN))
  1624. {
  1625. null_value= 1;
  1626. return 0;
  1627. }
  1628. make_datetime((DATE_TIME_FORMAT *) 0, &time_tmp, str);
  1629. return str;
  1630. }
  1631. longlong Item_func_convert_tz::val_int()
  1632. {
  1633. TIME time_tmp;
  1634. if (get_date(&time_tmp, 0))
  1635. return 0;
  1636. return (longlong)TIME_to_ulonglong_datetime(&time_tmp);
  1637. }
  1638. bool Item_func_convert_tz::get_date(TIME *ltime,
  1639. uint fuzzy_date __attribute__((unused)))
  1640. {
  1641. my_time_t my_time_tmp;
  1642. String str;
  1643. if (!from_tz_cached)
  1644. {
  1645. from_tz= my_tz_find(args[1]->val_str(&str), tz_tables);
  1646. from_tz_cached= args[1]->const_item();
  1647. }
  1648. if (!to_tz_cached)
  1649. {
  1650. to_tz= my_tz_find(args[2]->val_str(&str), tz_tables);
  1651. to_tz_cached= args[2]->const_item();
  1652. }
  1653. if (from_tz==0 || to_tz==0 || get_arg0_date(ltime, TIME_NO_ZERO_DATE))
  1654. {
  1655. null_value= 1;
  1656. return 1;
  1657. }
  1658. /* Check if we in range where we treat datetime values as non-UTC */
  1659. if (ltime->year < TIMESTAMP_MAX_YEAR && ltime->year > TIMESTAMP_MIN_YEAR ||
  1660. ltime->year==TIMESTAMP_MAX_YEAR && ltime->month==1 && ltime->day==1 ||
  1661. ltime->year==TIMESTAMP_MIN_YEAR && ltime->month==12 && ltime->day==31)
  1662. {
  1663. my_bool not_used;
  1664. my_time_tmp= from_tz->TIME_to_gmt_sec(ltime, &not_used);
  1665. if (my_time_tmp >= TIMESTAMP_MIN_VALUE && my_time_tmp <= TIMESTAMP_MAX_VALUE)
  1666. to_tz->gmt_sec_to_TIME(ltime, my_time_tmp);
  1667. }
  1668. null_value= 0;
  1669. return 0;
  1670. }
  1671. void Item_func_convert_tz::cleanup()
  1672. {
  1673. from_tz_cached= to_tz_cached= 0;
  1674. Item_date_func::cleanup();
  1675. }
  1676. void Item_date_add_interval::fix_length_and_dec()
  1677. {
  1678. enum_field_types arg0_field_type;
  1679. collation.set(&my_charset_bin);
  1680. maybe_null=1;
  1681. max_length=MAX_DATETIME_FULL_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
  1682. value.alloc(max_length);
  1683. /*
  1684. The field type for the result of an Item_date function is defined as
  1685. follows:
  1686. - If first arg is a MYSQL_TYPE_DATETIME result is MYSQL_TYPE_DATETIME
  1687. - If first arg is a MYSQL_TYPE_DATE and the interval type uses hours,
  1688. minutes or seconds then type is MYSQL_TYPE_DATETIME.
  1689. - Otherwise the result is MYSQL_TYPE_STRING
  1690. (This is because you can't know if the string contains a DATE, TIME or
  1691. DATETIME argument)
  1692. */
  1693. cached_field_type= MYSQL_TYPE_STRING;
  1694. arg0_field_type= args[0]->field_type();
  1695. if (arg0_field_type == MYSQL_TYPE_DATETIME ||
  1696. arg0_field_type == MYSQL_TYPE_TIMESTAMP)
  1697. cached_field_type= MYSQL_TYPE_DATETIME;
  1698. else if (arg0_field_type == MYSQL_TYPE_DATE)
  1699. {
  1700. if (int_type <= INTERVAL_DAY || int_type == INTERVAL_YEAR_MONTH)
  1701. cached_field_type= arg0_field_type;
  1702. else
  1703. cached_field_type= MYSQL_TYPE_DATETIME;
  1704. }
  1705. }
  1706. /* Here arg[1] is a Item_interval object */
  1707. bool Item_date_add_interval::get_date(TIME *ltime, uint fuzzy_date)
  1708. {
  1709. INTERVAL interval;
  1710. if (args[0]->get_date(ltime, TIME_NO_ZERO_DATE) ||
  1711. get_interval_value(args[1], int_type, &value, &interval))
  1712. return (null_value=1);
  1713. if (date_sub_interval)
  1714. interval.neg = !interval.neg;
  1715. if (ltime->year < YY_MAGIC_BELOW)
  1716. return (null_value=1);
  1717. return (null_value= date_add_interval(ltime, int_type, interval));
  1718. }
  1719. String *Item_date_add_interval::val_str(String *str)
  1720. {
  1721. DBUG_ASSERT(fixed == 1);
  1722. TIME ltime;
  1723. enum date_time_format_types format;
  1724. if (Item_date_add_interval::get_date(&ltime, TIME_NO_ZERO_DATE))
  1725. return 0;
  1726. if (ltime.time_type == MYSQL_TIMESTAMP_DATE)
  1727. format= DATE_ONLY;
  1728. else if (ltime.second_part)
  1729. format= DATE_TIME_MICROSECOND;
  1730. else
  1731. format= DATE_TIME;
  1732. if (!make_datetime(format, &ltime, str))
  1733. return str;
  1734. null_value=1;
  1735. return 0;
  1736. }
  1737. longlong Item_date_add_interval::val_int()
  1738. {
  1739. DBUG_ASSERT(fixed == 1);
  1740. TIME ltime;
  1741. longlong date;
  1742. if (Item_date_add_interval::get_date(&ltime, TIME_NO_ZERO_DATE))
  1743. return (longlong) 0;
  1744. date = (ltime.year*100L + ltime.month)*100L + ltime.day;
  1745. return ltime.time_type == MYSQL_TIMESTAMP_DATE ? date :
  1746. ((date*100L + ltime.hour)*100L+ ltime.minute)*100L + ltime.second;
  1747. }
  1748. bool Item_date_add_interval::eq(const Item *item, bool binary_cmp) const
  1749. {
  1750. Item_date_add_interval *other= (Item_date_add_interval*) item;
  1751. if (!Item_func::eq(item, binary_cmp))
  1752. return 0;
  1753. return ((int_type == other->int_type) &&
  1754. (date_sub_interval == other->date_sub_interval));
  1755. }
  1756. static const char *interval_names[]=
  1757. {
  1758. "year", "quarter", "month", "day", "hour",
  1759. "minute", "week", "second", "microsecond",
  1760. "year_month", "day_hour", "day_minute",
  1761. "day_second", "hour_minute", "hour_second",
  1762. "minute_second", "day_microsecond",
  1763. "hour_microsecond", "minute_microsecond",
  1764. "second_microsecond"
  1765. };
  1766. void Item_date_add_interval::print(String *str)
  1767. {
  1768. str->append('(');
  1769. args[0]->print(str);
  1770. str->append(date_sub_interval?" - interval ":" + interval ");
  1771. args[1]->print(str);
  1772. str->append(' ');
  1773. str->append(interval_names[int_type]);
  1774. str->append(')');
  1775. }
  1776. void Item_extract::print(String *str)
  1777. {
  1778. str->append(STRING_WITH_LEN("extract("));
  1779. str->append(interval_names[int_type]);
  1780. str->append(STRING_WITH_LEN(" from "));
  1781. args[0]->print(str);
  1782. str->append(')');
  1783. }
  1784. void Item_extract::fix_length_and_dec()
  1785. {
  1786. value.alloc(32); // alloc buffer
  1787. maybe_null=1; // If wrong date
  1788. switch (int_type) {
  1789. case INTERVAL_YEAR: max_length=4; date_value=1; break;
  1790. case INTERVAL_YEAR_MONTH: max_length=6; date_value=1; break;
  1791. case INTERVAL_QUARTER: max_length=2; date_value=1; break;
  1792. case INTERVAL_MONTH: max_length=2; date_value=1; break;
  1793. case INTERVAL_WEEK: max_length=2; date_value=1; break;
  1794. case INTERVAL_DAY: max_length=2; date_value=1; break;
  1795. case INTERVAL_DAY_HOUR: max_length=9; date_value=0; break;
  1796. case INTERVAL_DAY_MINUTE: max_length=11; date_value=0; break;
  1797. case INTERVAL_DAY_SECOND: max_length=13; date_value=0; break;
  1798. case INTERVAL_HOUR: max_length=2; date_value=0; break;
  1799. case INTERVAL_HOUR_MINUTE: max_length=4; date_value=0; break;
  1800. case INTERVAL_HOUR_SECOND: max_length=6; date_value=0; break;
  1801. case INTERVAL_MINUTE: max_length=2; date_value=0; break;
  1802. case INTERVAL_MINUTE_SECOND: max_length=4; date_value=0; break;
  1803. case INTERVAL_SECOND: max_length=2; date_value=0; break;
  1804. case INTERVAL_MICROSECOND: max_length=2; date_value=0; break;
  1805. case INTERVAL_DAY_MICROSECOND: max_length=20; date_value=0; break;
  1806. case INTERVAL_HOUR_MICROSECOND: max_length=13; date_value=0; break;
  1807. case INTERVAL_MINUTE_MICROSECOND: max_length=11; date_value=0; break;
  1808. case INTERVAL_SECOND_MICROSECOND: max_length=9; date_value=0; break;
  1809. }
  1810. }
  1811. longlong Item_extract::val_int()
  1812. {
  1813. DBUG_ASSERT(fixed == 1);
  1814. TIME ltime;
  1815. uint year;
  1816. ulong week_format;
  1817. long neg;
  1818. if (date_value)
  1819. {
  1820. if (get_arg0_date(&ltime, TIME_FUZZY_DATE))
  1821. return 0;
  1822. neg=1;
  1823. }
  1824. else
  1825. {
  1826. String *res= args[0]->val_str(&value);
  1827. if (!res || str_to_time_with_warn(res->ptr(), res->length(), &ltime))
  1828. {
  1829. null_value=1;
  1830. return 0;
  1831. }
  1832. neg= ltime.neg ? -1 : 1;
  1833. null_value=0;
  1834. }
  1835. switch (int_type) {
  1836. case INTERVAL_YEAR: return ltime.year;
  1837. case INTERVAL_YEAR_MONTH: return ltime.year*100L+ltime.month;
  1838. case INTERVAL_QUARTER: return (ltime.month+2)/3;
  1839. case INTERVAL_MONTH: return ltime.month;
  1840. case INTERVAL_WEEK:
  1841. {
  1842. week_format= current_thd->variables.default_week_format;
  1843. return calc_week(&ltime, week_mode(week_format), &year);
  1844. }
  1845. case INTERVAL_DAY: return ltime.day;
  1846. case INTERVAL_DAY_HOUR: return (long) (ltime.day*100L+ltime.hour)*neg;
  1847. case INTERVAL_DAY_MINUTE: return (long) (ltime.day*10000L+
  1848. ltime.hour*100L+
  1849. ltime.minute)*neg;
  1850. case INTERVAL_DAY_SECOND: return ((longlong) ltime.day*1000000L+
  1851. (longlong) (ltime.hour*10000L+
  1852. ltime.minute*100+
  1853. ltime.second))*neg;
  1854. case INTERVAL_HOUR: return (long) ltime.hour*neg;
  1855. case INTERVAL_HOUR_MINUTE: return (long) (ltime.hour*100+ltime.minute)*neg;
  1856. case INTERVAL_HOUR_SECOND: return (long) (ltime.hour*10000+ltime.minute*100+
  1857. ltime.second)*neg;
  1858. case INTERVAL_MINUTE: return (long) ltime.minute*neg;
  1859. case INTERVAL_MINUTE_SECOND: return (long) (ltime.minute*100+ltime.second)*neg;
  1860. case INTERVAL_SECOND: return (long) ltime.second*neg;
  1861. case INTERVAL_MICROSECOND: return (long) ltime.second_part*neg;
  1862. case INTERVAL_DAY_MICROSECOND: return (((longlong)ltime.day*1000000L +
  1863. (longlong)ltime.hour*10000L +
  1864. ltime.minute*100 +
  1865. ltime.second)*1000000L +
  1866. ltime.second_part)*neg;
  1867. case INTERVAL_HOUR_MICROSECOND: return (((longlong)ltime.hour*10000L +
  1868. ltime.minute*100 +
  1869. ltime.second)*1000000L +
  1870. ltime.second_part)*neg;
  1871. case INTERVAL_MINUTE_MICROSECOND: return (((longlong)(ltime.minute*100+
  1872. ltime.second))*1000000L+
  1873. ltime.second_part)*neg;
  1874. case INTERVAL_SECOND_MICROSECOND: return ((longlong)ltime.second*1000000L+
  1875. ltime.second_part)*neg;
  1876. }
  1877. return 0; // Impossible
  1878. }
  1879. bool Item_extract::eq(const Item *item, bool binary_cmp) const
  1880. {
  1881. if (this == item)
  1882. return 1;
  1883. if (item->type() != FUNC_ITEM ||
  1884. functype() != ((Item_func*)item)->functype())
  1885. return 0;
  1886. Item_extract* ie= (Item_extract*)item;
  1887. if (ie->int_type != int_type)
  1888. return 0;
  1889. if (!args[0]->eq(ie->args[0], binary_cmp))
  1890. return 0;
  1891. return 1;
  1892. }
  1893. bool Item_char_typecast::eq(const Item *item, bool binary_cmp) const
  1894. {
  1895. if (this == item)
  1896. return 1;
  1897. if (item->type() != FUNC_ITEM ||
  1898. functype() != ((Item_func*)item)->functype())
  1899. return 0;
  1900. Item_char_typecast *cast= (Item_char_typecast*)item;
  1901. if (cast_length != cast->cast_length ||
  1902. cast_cs != cast->cast_cs)
  1903. return 0;
  1904. if (!args[0]->eq(cast->args[0], binary_cmp))
  1905. return 0;
  1906. return 1;
  1907. }
  1908. void Item_typecast::print(String *str)
  1909. {
  1910. str->append(STRING_WITH_LEN("cast("));
  1911. args[0]->print(str);
  1912. str->append(STRING_WITH_LEN(" as "));
  1913. str->append(cast_type());
  1914. str->append(')');
  1915. }
  1916. void Item_char_typecast::print(String *str)
  1917. {
  1918. str->append(STRING_WITH_LEN("cast("));
  1919. args[0]->print(str);
  1920. str->append(STRING_WITH_LEN(" as char"));
  1921. if (cast_length >= 0)
  1922. {
  1923. str->append('(');
  1924. char buffer[20];
  1925. // my_charset_bin is good enough for numbers
  1926. String st(buffer, sizeof(buffer), &my_charset_bin);
  1927. st.set((ulonglong)cast_length, &my_charset_bin);
  1928. str->append(st);
  1929. str->append(')');
  1930. }
  1931. if (cast_cs)
  1932. {
  1933. str->append(STRING_WITH_LEN(" charset "));
  1934. str->append(cast_cs->csname);
  1935. }
  1936. str->append(')');
  1937. }
  1938. String *Item_char_typecast::val_str(String *str)
  1939. {
  1940. DBUG_ASSERT(fixed == 1);
  1941. String *res;
  1942. uint32 length;
  1943. if (!charset_conversion)
  1944. {
  1945. if (!(res= args[0]->val_str(str)))
  1946. {
  1947. null_value= 1;
  1948. return 0;
  1949. }
  1950. }
  1951. else
  1952. {
  1953. // Convert character set if differ
  1954. uint dummy_errors;
  1955. if (!(res= args[0]->val_str(&tmp_value)) ||
  1956. str->copy(res->ptr(), res->length(), from_cs,
  1957. cast_cs, &dummy_errors))
  1958. {
  1959. null_value= 1;
  1960. return 0;
  1961. }
  1962. res= str;
  1963. }
  1964. res->set_charset(cast_cs);
  1965. /*
  1966. Cut the tail if cast with length
  1967. and the result is longer than cast length, e.g.
  1968. CAST('string' AS CHAR(1))
  1969. */
  1970. if (cast_length >= 0)
  1971. {
  1972. if (res->length() > (length= (uint32) res->charpos(cast_length)))
  1973. { // Safe even if const arg
  1974. char char_type[40];
  1975. my_snprintf(char_type, sizeof(char_type), "%s(%lu)",
  1976. cast_cs == &my_charset_bin ? "BINARY" : "CHAR", length);
  1977. if (!res->alloced_length())
  1978. { // Don't change const str
  1979. str_value= *res; // Not malloced string
  1980. res= &str_value;
  1981. }
  1982. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  1983. ER_TRUNCATED_WRONG_VALUE,
  1984. ER(ER_TRUNCATED_WRONG_VALUE), char_type,
  1985. res->c_ptr_safe());
  1986. res->length((uint) length);
  1987. }
  1988. else if (cast_cs == &my_charset_bin && res->length() < (uint) cast_length)
  1989. {
  1990. if (res->alloced_length() < (uint) cast_length)
  1991. {
  1992. str->alloc(cast_length);
  1993. str->copy(*res);
  1994. res= str;
  1995. }
  1996. bzero((char*) res->ptr() + res->length(),
  1997. (uint) cast_length - res->length());
  1998. res->length(cast_length);
  1999. }
  2000. }
  2001. null_value= 0;
  2002. return res;
  2003. }
  2004. void Item_char_typecast::fix_length_and_dec()
  2005. {
  2006. uint32 char_length;
  2007. /*
  2008. We always force character set conversion if cast_cs
  2009. is a multi-byte character set. It garantees that the
  2010. result of CAST is a well-formed string.
  2011. For single-byte character sets we allow just to copy
  2012. from the argument. A single-byte character sets string
  2013. is always well-formed.
  2014. There is a special trick to convert form a number to ucs2.
  2015. As numbers have my_charset_bin as their character set,
  2016. it wouldn't do conversion to ucs2 without an additional action.
  2017. To force conversion, we should pretend to be non-binary.
  2018. Let's choose from_cs this way:
  2019. - If the argument in a number and cast_cs is ucs2 (i.e. mbminlen > 1),
  2020. then from_cs is set to latin1, to perform latin1 -> ucs2 conversion.
  2021. - If the argument is a number and cast_cs is ASCII-compatible
  2022. (i.e. mbminlen == 1), then from_cs is set to cast_cs,
  2023. which allows just to take over the args[0]->val_str() result
  2024. and thus avoid unnecessary character set conversion.
  2025. - If the argument is not a number, then from_cs is set to
  2026. the argument's charset.
  2027. */
  2028. from_cs= (args[0]->result_type() == INT_RESULT ||
  2029. args[0]->result_type() == DECIMAL_RESULT ||
  2030. args[0]->result_type() == REAL_RESULT) ?
  2031. (cast_cs->mbminlen == 1 ? cast_cs : &my_charset_latin1) :
  2032. args[0]->collation.collation;
  2033. charset_conversion= (cast_cs->mbmaxlen > 1) ||
  2034. !my_charset_same(from_cs, cast_cs) &&
  2035. from_cs != &my_charset_bin &&
  2036. cast_cs != &my_charset_bin;
  2037. collation.set(cast_cs, DERIVATION_IMPLICIT);
  2038. char_length= (cast_length >= 0) ? cast_length :
  2039. args[0]->max_length/from_cs->mbmaxlen;
  2040. max_length= char_length * cast_cs->mbmaxlen;
  2041. }
  2042. String *Item_datetime_typecast::val_str(String *str)
  2043. {
  2044. DBUG_ASSERT(fixed == 1);
  2045. TIME ltime;
  2046. if (!get_arg0_date(&ltime, TIME_FUZZY_DATE) &&
  2047. !make_datetime(ltime.second_part ? DATE_TIME_MICROSECOND : DATE_TIME,
  2048. &ltime, str))
  2049. return str;
  2050. null_value=1;
  2051. return 0;
  2052. }
  2053. longlong Item_datetime_typecast::val_int()
  2054. {
  2055. DBUG_ASSERT(fixed == 1);
  2056. TIME ltime;
  2057. if (get_arg0_date(&ltime,1))
  2058. {
  2059. null_value= 1;
  2060. return 0;
  2061. }
  2062. return TIME_to_ulonglong_datetime(&ltime);
  2063. }
  2064. bool Item_time_typecast::get_time(TIME *ltime)
  2065. {
  2066. bool res= get_arg0_time(ltime);
  2067. /*
  2068. For MYSQL_TIMESTAMP_TIME value we can have non-zero day part,
  2069. which we should not lose.
  2070. */
  2071. if (ltime->time_type == MYSQL_TIMESTAMP_DATETIME)
  2072. ltime->year= ltime->month= ltime->day= 0;
  2073. ltime->time_type= MYSQL_TIMESTAMP_TIME;
  2074. return res;
  2075. }
  2076. longlong Item_time_typecast::val_int()
  2077. {
  2078. TIME ltime;
  2079. if (get_time(&ltime))
  2080. {
  2081. null_value= 1;
  2082. return 0;
  2083. }
  2084. return ltime.hour * 10000L + ltime.minute * 100 + ltime.second;
  2085. }
  2086. String *Item_time_typecast::val_str(String *str)
  2087. {
  2088. DBUG_ASSERT(fixed == 1);
  2089. TIME ltime;
  2090. if (!get_arg0_time(&ltime) &&
  2091. !make_datetime(ltime.second_part ? TIME_MICROSECOND : TIME_ONLY,
  2092. &ltime, str))
  2093. return str;
  2094. null_value=1;
  2095. return 0;
  2096. }
  2097. bool Item_date_typecast::get_date(TIME *ltime, uint fuzzy_date)
  2098. {
  2099. bool res= get_arg0_date(ltime, TIME_FUZZY_DATE);
  2100. ltime->hour= ltime->minute= ltime->second= ltime->second_part= 0;
  2101. ltime->time_type= MYSQL_TIMESTAMP_DATE;
  2102. return res;
  2103. }
  2104. String *Item_date_typecast::val_str(String *str)
  2105. {
  2106. DBUG_ASSERT(fixed == 1);
  2107. TIME ltime;
  2108. if (!get_arg0_date(&ltime, TIME_FUZZY_DATE) && !str->alloc(11))
  2109. {
  2110. make_date((DATE_TIME_FORMAT *) 0, &ltime, str);
  2111. return str;
  2112. }
  2113. null_value=1;
  2114. return 0;
  2115. }
  2116. longlong Item_date_typecast::val_int()
  2117. {
  2118. DBUG_ASSERT(fixed == 1);
  2119. TIME ltime;
  2120. if (args[0]->get_date(&ltime, TIME_FUZZY_DATE))
  2121. return 0;
  2122. return (longlong) (ltime.year * 10000L + ltime.month * 100 + ltime.day);
  2123. }
  2124. /*
  2125. MAKEDATE(a,b) is a date function that creates a date value
  2126. from a year and day value.
  2127. */
  2128. String *Item_func_makedate::val_str(String *str)
  2129. {
  2130. DBUG_ASSERT(fixed == 1);
  2131. TIME l_time;
  2132. long daynr= (long) args[1]->val_int();
  2133. long yearnr= (long) args[0]->val_int();
  2134. long days;
  2135. if (args[0]->null_value || args[1]->null_value ||
  2136. yearnr < 0 || daynr <= 0)
  2137. goto err;
  2138. days= calc_daynr(yearnr,1,1) + daynr - 1;
  2139. /* Day number from year 0 to 9999-12-31 */
  2140. if (days >= 0 && days <= MAX_DAY_NUMBER)
  2141. {
  2142. null_value=0;
  2143. get_date_from_daynr(days,&l_time.year,&l_time.month,&l_time.day);
  2144. if (str->alloc(11))
  2145. goto err;
  2146. make_date((DATE_TIME_FORMAT *) 0, &l_time, str);
  2147. return str;
  2148. }
  2149. err:
  2150. null_value=1;
  2151. return 0;
  2152. }
  2153. longlong Item_func_makedate::val_int()
  2154. {
  2155. DBUG_ASSERT(fixed == 1);
  2156. TIME l_time;
  2157. long daynr= (long) args[1]->val_int();
  2158. long yearnr= (long) args[0]->val_int();
  2159. long days;
  2160. if (args[0]->null_value || args[1]->null_value ||
  2161. yearnr < 0 || daynr <= 0)
  2162. goto err;
  2163. days= calc_daynr(yearnr,1,1) + daynr - 1;
  2164. /* Day number from year 0 to 9999-12-31 */
  2165. if (days >= 0 && days < MAX_DAY_NUMBER)
  2166. {
  2167. null_value=0;
  2168. get_date_from_daynr(days,&l_time.year,&l_time.month,&l_time.day);
  2169. return (longlong) (l_time.year * 10000L + l_time.month * 100 + l_time.day);
  2170. }
  2171. err:
  2172. null_value= 1;
  2173. return 0;
  2174. }
  2175. void Item_func_add_time::fix_length_and_dec()
  2176. {
  2177. enum_field_types arg0_field_type;
  2178. decimals=0;
  2179. max_length=MAX_DATETIME_FULL_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
  2180. maybe_null= 1;
  2181. /*
  2182. The field type for the result of an Item_func_add_time function is defined
  2183. as follows:
  2184. - If first arg is a MYSQL_TYPE_DATETIME or MYSQL_TYPE_TIMESTAMP
  2185. result is MYSQL_TYPE_DATETIME
  2186. - If first arg is a MYSQL_TYPE_TIME result is MYSQL_TYPE_TIME
  2187. - Otherwise the result is MYSQL_TYPE_STRING
  2188. */
  2189. cached_field_type= MYSQL_TYPE_STRING;
  2190. arg0_field_type= args[0]->field_type();
  2191. if (arg0_field_type == MYSQL_TYPE_DATE ||
  2192. arg0_field_type == MYSQL_TYPE_DATETIME ||
  2193. arg0_field_type == MYSQL_TYPE_TIMESTAMP)
  2194. cached_field_type= MYSQL_TYPE_DATETIME;
  2195. else if (arg0_field_type == MYSQL_TYPE_TIME)
  2196. cached_field_type= MYSQL_TYPE_TIME;
  2197. }
  2198. /*
  2199. ADDTIME(t,a) and SUBTIME(t,a) are time functions that calculate a
  2200. time/datetime value
  2201. t: time_or_datetime_expression
  2202. a: time_expression
  2203. Result: Time value or datetime value
  2204. */
  2205. String *Item_func_add_time::val_str(String *str)
  2206. {
  2207. DBUG_ASSERT(fixed == 1);
  2208. TIME l_time1, l_time2, l_time3;
  2209. bool is_time= 0;
  2210. long days, microseconds;
  2211. longlong seconds;
  2212. int l_sign= sign;
  2213. null_value=0;
  2214. if (is_date) // TIMESTAMP function
  2215. {
  2216. if (get_arg0_date(&l_time1, TIME_FUZZY_DATE) ||
  2217. args[1]->get_time(&l_time2) ||
  2218. l_time1.time_type == MYSQL_TIMESTAMP_TIME ||
  2219. l_time2.time_type != MYSQL_TIMESTAMP_TIME)
  2220. goto null_date;
  2221. }
  2222. else // ADDTIME function
  2223. {
  2224. if (args[0]->get_time(&l_time1) ||
  2225. args[1]->get_time(&l_time2) ||
  2226. l_time2.time_type == MYSQL_TIMESTAMP_DATETIME)
  2227. goto null_date;
  2228. is_time= (l_time1.time_type == MYSQL_TIMESTAMP_TIME);
  2229. }
  2230. if (l_time1.neg != l_time2.neg)
  2231. l_sign= -l_sign;
  2232. l_time3.neg= calc_time_diff(&l_time1, &l_time2, -l_sign,
  2233. &seconds, &microseconds);
  2234. /*
  2235. If first argument was negative and diff between arguments
  2236. is non-zero we need to swap sign to get proper result.
  2237. */
  2238. if (l_time1.neg && (seconds || microseconds))
  2239. l_time3.neg= 1-l_time3.neg; // Swap sign of result
  2240. if (!is_time && l_time3.neg)
  2241. goto null_date;
  2242. days= (long)(seconds/86400L);
  2243. calc_time_from_sec(&l_time3, (long)(seconds%86400L), microseconds);
  2244. if (!is_time)
  2245. {
  2246. get_date_from_daynr(days,&l_time3.year,&l_time3.month,&l_time3.day);
  2247. if (l_time3.day &&
  2248. !make_datetime(l_time1.second_part || l_time2.second_part ?
  2249. DATE_TIME_MICROSECOND : DATE_TIME,
  2250. &l_time3, str))
  2251. return str;
  2252. goto null_date;
  2253. }
  2254. l_time3.hour+= days*24;
  2255. if (!make_datetime(l_time1.second_part || l_time2.second_part ?
  2256. TIME_MICROSECOND : TIME_ONLY,
  2257. &l_time3, str))
  2258. return str;
  2259. null_date:
  2260. null_value=1;
  2261. return 0;
  2262. }
  2263. void Item_func_add_time::print(String *str)
  2264. {
  2265. if (is_date)
  2266. {
  2267. DBUG_ASSERT(sign > 0);
  2268. str->append(STRING_WITH_LEN("timestamp("));
  2269. }
  2270. else
  2271. {
  2272. if (sign > 0)
  2273. str->append(STRING_WITH_LEN("addtime("));
  2274. else
  2275. str->append(STRING_WITH_LEN("subtime("));
  2276. }
  2277. args[0]->print(str);
  2278. str->append(',');
  2279. args[1]->print(str);
  2280. str->append(')');
  2281. }
  2282. /*
  2283. TIMEDIFF(t,s) is a time function that calculates the
  2284. time value between a start and end time.
  2285. t and s: time_or_datetime_expression
  2286. Result: Time value
  2287. */
  2288. String *Item_func_timediff::val_str(String *str)
  2289. {
  2290. DBUG_ASSERT(fixed == 1);
  2291. longlong seconds;
  2292. long microseconds;
  2293. int l_sign= 1;
  2294. TIME l_time1 ,l_time2, l_time3;
  2295. null_value= 0;
  2296. if (args[0]->get_time(&l_time1) ||
  2297. args[1]->get_time(&l_time2) ||
  2298. l_time1.time_type != l_time2.time_type)
  2299. goto null_date;
  2300. if (l_time1.neg != l_time2.neg)
  2301. l_sign= -l_sign;
  2302. l_time3.neg= calc_time_diff(&l_time1, &l_time2, l_sign,
  2303. &seconds, &microseconds);
  2304. /*
  2305. For MYSQL_TIMESTAMP_TIME only:
  2306. If first argument was negative and diff between arguments
  2307. is non-zero we need to swap sign to get proper result.
  2308. */
  2309. if (l_time1.neg && (seconds || microseconds))
  2310. l_time3.neg= 1-l_time3.neg; // Swap sign of result
  2311. calc_time_from_sec(&l_time3, (long) seconds, microseconds);
  2312. if (!make_datetime(l_time1.second_part || l_time2.second_part ?
  2313. TIME_MICROSECOND : TIME_ONLY,
  2314. &l_time3, str))
  2315. return str;
  2316. null_date:
  2317. null_value=1;
  2318. return 0;
  2319. }
  2320. /*
  2321. MAKETIME(h,m,s) is a time function that calculates a time value
  2322. from the total number of hours, minutes, and seconds.
  2323. Result: Time value
  2324. */
  2325. String *Item_func_maketime::val_str(String *str)
  2326. {
  2327. DBUG_ASSERT(fixed == 1);
  2328. TIME ltime;
  2329. long hour= (long) args[0]->val_int();
  2330. long minute= (long) args[1]->val_int();
  2331. long second= (long) args[2]->val_int();
  2332. if ((null_value=(args[0]->null_value ||
  2333. args[1]->null_value ||
  2334. args[2]->null_value ||
  2335. minute > 59 || minute < 0 ||
  2336. second > 59 || second < 0 ||
  2337. str->alloc(19))))
  2338. return 0;
  2339. ltime.neg= 0;
  2340. if (hour < 0)
  2341. {
  2342. ltime.neg= 1;
  2343. hour= -hour;
  2344. }
  2345. ltime.hour= (ulong) hour;
  2346. ltime.minute= (ulong) minute;
  2347. ltime.second= (ulong) second;
  2348. make_time((DATE_TIME_FORMAT *) 0, &ltime, str);
  2349. return str;
  2350. }
  2351. /*
  2352. MICROSECOND(a) is a function ( extraction) that extracts the microseconds
  2353. from a.
  2354. a: Datetime or time value
  2355. Result: int value
  2356. */
  2357. longlong Item_func_microsecond::val_int()
  2358. {
  2359. DBUG_ASSERT(fixed == 1);
  2360. TIME ltime;
  2361. if (!get_arg0_time(&ltime))
  2362. return ltime.second_part;
  2363. return 0;
  2364. }
  2365. longlong Item_func_timestamp_diff::val_int()
  2366. {
  2367. TIME ltime1, ltime2;
  2368. longlong seconds;
  2369. long microseconds;
  2370. long months= 0;
  2371. int neg= 1;
  2372. null_value= 0;
  2373. if (args[0]->get_date(&ltime1, TIME_NO_ZERO_DATE) ||
  2374. args[1]->get_date(&ltime2, TIME_NO_ZERO_DATE))
  2375. goto null_date;
  2376. if (calc_time_diff(&ltime2,&ltime1, 1,
  2377. &seconds, &microseconds))
  2378. neg= -1;
  2379. if (int_type == INTERVAL_YEAR ||
  2380. int_type == INTERVAL_QUARTER ||
  2381. int_type == INTERVAL_MONTH)
  2382. {
  2383. uint year_beg, year_end, month_beg, month_end, day_beg, day_end;
  2384. uint years= 0;
  2385. uint second_beg, second_end, microsecond_beg, microsecond_end;
  2386. if (neg == -1)
  2387. {
  2388. year_beg= ltime2.year;
  2389. year_end= ltime1.year;
  2390. month_beg= ltime2.month;
  2391. month_end= ltime1.month;
  2392. day_beg= ltime2.day;
  2393. day_end= ltime1.day;
  2394. second_beg= ltime2.hour * 3600 + ltime2.minute * 60 + ltime2.second;
  2395. second_end= ltime1.hour * 3600 + ltime1.minute * 60 + ltime1.second;
  2396. microsecond_beg= ltime2.second_part;
  2397. microsecond_end= ltime1.second_part;
  2398. }
  2399. else
  2400. {
  2401. year_beg= ltime1.year;
  2402. year_end= ltime2.year;
  2403. month_beg= ltime1.month;
  2404. month_end= ltime2.month;
  2405. day_beg= ltime1.day;
  2406. day_end= ltime2.day;
  2407. second_beg= ltime1.hour * 3600 + ltime1.minute * 60 + ltime1.second;
  2408. second_end= ltime2.hour * 3600 + ltime2.minute * 60 + ltime2.second;
  2409. microsecond_beg= ltime1.second_part;
  2410. microsecond_end= ltime2.second_part;
  2411. }
  2412. /* calc years */
  2413. years= year_end - year_beg;
  2414. if (month_end < month_beg || (month_end == month_beg && day_end < day_beg))
  2415. years-= 1;
  2416. /* calc months */
  2417. months= 12*years;
  2418. if (month_end < month_beg || (month_end == month_beg && day_end < day_beg))
  2419. months+= 12 - (month_beg - month_end);
  2420. else
  2421. months+= (month_end - month_beg);
  2422. if (day_end < day_beg)
  2423. months-= 1;
  2424. else if ((day_end == day_beg) &&
  2425. ((second_end < second_beg) ||
  2426. (second_end == second_beg && microsecond_end < microsecond_beg)))
  2427. months-= 1;
  2428. }
  2429. switch (int_type) {
  2430. case INTERVAL_YEAR:
  2431. return months/12*neg;
  2432. case INTERVAL_QUARTER:
  2433. return months/3*neg;
  2434. case INTERVAL_MONTH:
  2435. return months*neg;
  2436. case INTERVAL_WEEK:
  2437. return seconds/86400L/7L*neg;
  2438. case INTERVAL_DAY:
  2439. return seconds/86400L*neg;
  2440. case INTERVAL_HOUR:
  2441. return seconds/3600L*neg;
  2442. case INTERVAL_MINUTE:
  2443. return seconds/60L*neg;
  2444. case INTERVAL_SECOND:
  2445. return seconds*neg;
  2446. case INTERVAL_MICROSECOND:
  2447. /*
  2448. In MySQL difference between any two valid datetime values
  2449. in microseconds fits into longlong.
  2450. */
  2451. return (seconds*1000000L+microseconds)*neg;
  2452. default:
  2453. break;
  2454. }
  2455. null_date:
  2456. null_value=1;
  2457. return 0;
  2458. }
  2459. void Item_func_timestamp_diff::print(String *str)
  2460. {
  2461. str->append(func_name());
  2462. str->append('(');
  2463. switch (int_type) {
  2464. case INTERVAL_YEAR:
  2465. str->append(STRING_WITH_LEN("YEAR"));
  2466. break;
  2467. case INTERVAL_QUARTER:
  2468. str->append(STRING_WITH_LEN("QUARTER"));
  2469. break;
  2470. case INTERVAL_MONTH:
  2471. str->append(STRING_WITH_LEN("MONTH"));
  2472. break;
  2473. case INTERVAL_WEEK:
  2474. str->append(STRING_WITH_LEN("WEEK"));
  2475. break;
  2476. case INTERVAL_DAY:
  2477. str->append(STRING_WITH_LEN("DAY"));
  2478. break;
  2479. case INTERVAL_HOUR:
  2480. str->append(STRING_WITH_LEN("HOUR"));
  2481. break;
  2482. case INTERVAL_MINUTE:
  2483. str->append(STRING_WITH_LEN("MINUTE"));
  2484. break;
  2485. case INTERVAL_SECOND:
  2486. str->append(STRING_WITH_LEN("SECOND"));
  2487. break;
  2488. case INTERVAL_MICROSECOND:
  2489. str->append(STRING_WITH_LEN("SECOND_FRAC"));
  2490. break;
  2491. default:
  2492. break;
  2493. }
  2494. for (uint i=0 ; i < 2 ; i++)
  2495. {
  2496. str->append(',');
  2497. args[i]->print(str);
  2498. }
  2499. str->append(')');
  2500. }
  2501. String *Item_func_get_format::val_str(String *str)
  2502. {
  2503. DBUG_ASSERT(fixed == 1);
  2504. const char *format_name;
  2505. KNOWN_DATE_TIME_FORMAT *format;
  2506. String *val= args[0]->val_str(str);
  2507. ulong val_len;
  2508. if ((null_value= args[0]->null_value))
  2509. return 0;
  2510. val_len= val->length();
  2511. for (format= &known_date_time_formats[0];
  2512. (format_name= format->format_name);
  2513. format++)
  2514. {
  2515. uint format_name_len;
  2516. format_name_len= strlen(format_name);
  2517. if (val_len == format_name_len &&
  2518. !my_strnncoll(&my_charset_latin1,
  2519. (const uchar *) val->ptr(), val_len,
  2520. (const uchar *) format_name, val_len))
  2521. {
  2522. const char *format_str= get_date_time_format_str(format, type);
  2523. str->set(format_str, strlen(format_str), &my_charset_bin);
  2524. return str;
  2525. }
  2526. }
  2527. null_value= 1;
  2528. return 0;
  2529. }
  2530. void Item_func_get_format::print(String *str)
  2531. {
  2532. str->append(func_name());
  2533. str->append('(');
  2534. switch (type) {
  2535. case MYSQL_TIMESTAMP_DATE:
  2536. str->append(STRING_WITH_LEN("DATE, "));
  2537. break;
  2538. case MYSQL_TIMESTAMP_DATETIME:
  2539. str->append(STRING_WITH_LEN("DATETIME, "));
  2540. break;
  2541. case MYSQL_TIMESTAMP_TIME:
  2542. str->append(STRING_WITH_LEN("TIME, "));
  2543. break;
  2544. default:
  2545. DBUG_ASSERT(0);
  2546. }
  2547. args[0]->print(str);
  2548. str->append(')');
  2549. }
  2550. /*
  2551. Get type of datetime value (DATE/TIME/...) which will be produced
  2552. according to format string.
  2553. SYNOPSIS
  2554. get_date_time_result_type()
  2555. format - format string
  2556. length - length of format string
  2557. NOTE
  2558. We don't process day format's characters('D', 'd', 'e') because day
  2559. may be a member of all date/time types.
  2560. Format specifiers supported by this function should be in sync with
  2561. specifiers supported by extract_date_time() function.
  2562. RETURN VALUE
  2563. One of date_time_format_types values:
  2564. DATE_TIME_MICROSECOND, DATE_TIME, DATE_ONLY, TIME_MICROSECOND, TIME_ONLY
  2565. */
  2566. static date_time_format_types
  2567. get_date_time_result_type(const char *format, uint length)
  2568. {
  2569. const char *time_part_frms= "HISThiklrs";
  2570. const char *date_part_frms= "MVUXYWabcjmvuxyw";
  2571. bool date_part_used= 0, time_part_used= 0, frac_second_used= 0;
  2572. const char *val= format;
  2573. const char *end= format + length;
  2574. for (; val != end && val != end; val++)
  2575. {
  2576. if (*val == '%' && val+1 != end)
  2577. {
  2578. val++;
  2579. if (*val == 'f')
  2580. frac_second_used= time_part_used= 1;
  2581. else if (!time_part_used && strchr(time_part_frms, *val))
  2582. time_part_used= 1;
  2583. else if (!date_part_used && strchr(date_part_frms, *val))
  2584. date_part_used= 1;
  2585. if (date_part_used && frac_second_used)
  2586. {
  2587. /*
  2588. frac_second_used implies time_part_used, and thus we already
  2589. have all types of date-time components and can end our search.
  2590. */
  2591. return DATE_TIME_MICROSECOND;
  2592. }
  2593. }
  2594. }
  2595. /* We don't have all three types of date-time components */
  2596. if (frac_second_used)
  2597. return TIME_MICROSECOND;
  2598. if (time_part_used)
  2599. {
  2600. if (date_part_used)
  2601. return DATE_TIME;
  2602. return TIME_ONLY;
  2603. }
  2604. return DATE_ONLY;
  2605. }
  2606. void Item_func_str_to_date::fix_length_and_dec()
  2607. {
  2608. char format_buff[64];
  2609. String format_str(format_buff, sizeof(format_buff), &my_charset_bin), *format;
  2610. maybe_null= 1;
  2611. decimals=0;
  2612. cached_field_type= MYSQL_TYPE_STRING;
  2613. max_length= MAX_DATETIME_FULL_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
  2614. cached_timestamp_type= MYSQL_TIMESTAMP_NONE;
  2615. format= args[1]->val_str(&format_str);
  2616. if (!args[1]->null_value && (const_item= args[1]->const_item()))
  2617. {
  2618. cached_format_type= get_date_time_result_type(format->ptr(),
  2619. format->length());
  2620. switch (cached_format_type) {
  2621. case DATE_ONLY:
  2622. cached_timestamp_type= MYSQL_TIMESTAMP_DATE;
  2623. cached_field_type= MYSQL_TYPE_DATE;
  2624. max_length= MAX_DATE_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
  2625. break;
  2626. case TIME_ONLY:
  2627. case TIME_MICROSECOND:
  2628. cached_timestamp_type= MYSQL_TIMESTAMP_TIME;
  2629. cached_field_type= MYSQL_TYPE_TIME;
  2630. max_length= MAX_TIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
  2631. break;
  2632. default:
  2633. cached_timestamp_type= MYSQL_TIMESTAMP_DATETIME;
  2634. cached_field_type= MYSQL_TYPE_DATETIME;
  2635. break;
  2636. }
  2637. }
  2638. }
  2639. bool Item_func_str_to_date::get_date(TIME *ltime, uint fuzzy_date)
  2640. {
  2641. DATE_TIME_FORMAT date_time_format;
  2642. char val_buff[64], format_buff[64];
  2643. String val_str(val_buff, sizeof(val_buff), &my_charset_bin), *val;
  2644. String format_str(format_buff, sizeof(format_buff), &my_charset_bin), *format;
  2645. val= args[0]->val_str(&val_str);
  2646. format= args[1]->val_str(&format_str);
  2647. if (args[0]->null_value || args[1]->null_value)
  2648. goto null_date;
  2649. null_value= 0;
  2650. bzero((char*) ltime, sizeof(ltime));
  2651. date_time_format.format.str= (char*) format->ptr();
  2652. date_time_format.format.length= format->length();
  2653. if (extract_date_time(&date_time_format, val->ptr(), val->length(),
  2654. ltime, cached_timestamp_type, 0, "datetime"))
  2655. goto null_date;
  2656. if (cached_timestamp_type == MYSQL_TIMESTAMP_TIME && ltime->day)
  2657. {
  2658. /*
  2659. Day part for time type can be nonzero value and so
  2660. we should add hours from day part to hour part to
  2661. keep valid time value.
  2662. */
  2663. ltime->hour+= ltime->day*24;
  2664. ltime->day= 0;
  2665. }
  2666. return 0;
  2667. null_date:
  2668. return (null_value=1);
  2669. }
  2670. String *Item_func_str_to_date::val_str(String *str)
  2671. {
  2672. DBUG_ASSERT(fixed == 1);
  2673. TIME ltime;
  2674. if (Item_func_str_to_date::get_date(&ltime, TIME_FUZZY_DATE))
  2675. return 0;
  2676. if (!make_datetime((const_item ? cached_format_type :
  2677. (ltime.second_part ? DATE_TIME_MICROSECOND : DATE_TIME)),
  2678. &ltime, str))
  2679. return str;
  2680. return 0;
  2681. }
  2682. bool Item_func_last_day::get_date(TIME *ltime, uint fuzzy_date)
  2683. {
  2684. if (get_arg0_date(ltime, fuzzy_date & ~TIME_FUZZY_DATE))
  2685. return 1;
  2686. uint month_idx= ltime->month-1;
  2687. ltime->day= days_in_month[month_idx];
  2688. if ( month_idx == 1 && calc_days_in_year(ltime->year) == 366)
  2689. ltime->day= 29;
  2690. ltime->time_type= MYSQL_TIMESTAMP_DATE;
  2691. return 0;
  2692. }