24
24
Move month and days to language files
26
#include <drizzled/server_includes.h>
27
#ifdef USE_PRAGMA_IMPLEMENTATION
28
#pragma implementation // gcc: Class implementation
31
#include "mysql_priv.h"
28
#include <drizzled/drizzled_error_messages.h>
30
35
/** Day number for Dec 31st, 9999. */
31
36
#define MAX_DAY_NUMBER 3652424L
48
53
the microseconds twice.
51
static bool make_datetime(date_time_format_types format, DRIZZLE_TIME *ltime,
56
static bool make_datetime(date_time_format_types format, MYSQL_TIME *ltime,
55
const CHARSET_INFO * const cs= &my_charset_bin;
56
uint32_t length= MAX_DATE_STRING_REP_LENGTH;
60
CHARSET_INFO *cs= &my_charset_bin;
61
uint length= MAX_DATE_STRING_REP_LENGTH;
58
63
if (str->alloc(length))
122
make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
127
make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
123
128
str->ptr(), str->length(),
124
DRIZZLE_TIMESTAMP_TIME, NULL);
129
MYSQL_TIMESTAMP_TIME, NullS);
125
130
return make_datetime(format, ltime, str);
130
Wrapper over make_time() with validation of the input DRIZZLE_TIME value
135
Wrapper over make_time() with validation of the input MYSQL_TIME value
133
138
see make_time() for more info
140
145
static bool make_time_with_warn(const DATE_TIME_FORMAT *format,
141
DRIZZLE_TIME *l_time, String *str)
146
MYSQL_TIME *l_time, String *str)
144
149
make_time(format, l_time, str);
149
make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
154
make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
150
155
str->ptr(), str->length(),
151
DRIZZLE_TIMESTAMP_TIME, NULL);
156
MYSQL_TIMESTAMP_TIME, NullS);
152
157
make_time(format, l_time, str);
160
Convert seconds to DRIZZLE_TIME value with overflow checking
165
Convert seconds to MYSQL_TIME value with overflow checking
164
169
seconds number of seconds
165
170
unsigned_flag 1, if 'seconds' is unsigned, 0, otherwise
166
ltime output DRIZZLE_TIME value
171
ltime output MYSQL_TIME value
169
If the 'seconds' argument is inside DRIZZLE_TIME data range, convert it to a
174
If the 'seconds' argument is inside MYSQL_TIME data range, convert it to a
170
175
corresponding value.
171
176
Otherwise, truncate the resulting value to the nearest endpoint, and
172
177
produce a warning message.
179
static bool sec_to_time(int64_t seconds, bool unsigned_flag, DRIZZLE_TIME *ltime)
184
static bool sec_to_time(longlong seconds, bool unsigned_flag, MYSQL_TIME *ltime)
183
memset(ltime, 0, sizeof(*ltime));
188
bzero((char *)ltime, sizeof(*ltime));
188
195
if (seconds < -3020399)
205
212
ltime->second= TIME_MAX_SECOND;
208
int len= (int)(int64_t10_to_str(seconds, buf, unsigned_flag ? 10 : -10)
215
int len= (int)(longlong10_to_str(seconds, buf, unsigned_flag ? 10 : -10)
210
make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
211
buf, len, DRIZZLE_TIMESTAMP_TIME,
217
make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
218
buf, len, MYSQL_TIMESTAMP_TIME,
227
234
{(char *)"%H:%i:%S", 8}};
230
Extract datetime value to DRIZZLE_TIME struct from string value
237
Extract datetime value to MYSQL_TIME struct from string value
231
238
according to format string.
233
240
@param format date/time format specification
262
269
static bool extract_date_time(DATE_TIME_FORMAT *format,
263
const char *val, uint32_t length, DRIZZLE_TIME *l_time,
264
enum enum_drizzle_timestamp_type cached_timestamp_type,
270
const char *val, uint length, MYSQL_TIME *l_time,
271
timestamp_type cached_timestamp_type,
265
272
const char **sub_pattern_end,
266
273
const char *date_time_type)
278
285
const char *val_end= val + length;
279
286
const char *ptr= format->format.str;
280
287
const char *end= ptr + format->format.length;
281
const CHARSET_INFO * const cs= &my_charset_bin;
288
CHARSET_INFO *cs= &my_charset_bin;
289
DBUG_ENTER("extract_date_time");
283
291
if (!sub_pattern_end)
284
memset(l_time, 0, sizeof(*l_time));
292
bzero((char*) l_time, sizeof(*l_time));
286
294
for (; ptr != end && val != val_end; ptr++)
300
308
switch (*++ptr) {
303
tmp= (char*) val + cmin(4, val_len);
311
tmp= (char*) val + min(4, val_len);
304
312
l_time->year= (int) my_strtoll10(val, &tmp, &error);
305
313
if ((int) (tmp-val) <= 2)
306
314
l_time->year= year_2000_handling(l_time->year);
310
tmp= (char*) val + cmin(2, val_len);
318
tmp= (char*) val + min(2, val_len);
311
319
l_time->year= (int) my_strtoll10(val, &tmp, &error);
313
321
l_time->year= year_2000_handling(l_time->year);
319
tmp= (char*) val + cmin(2, val_len);
327
tmp= (char*) val + min(2, val_len);
320
328
l_time->month= (int) my_strtoll10(val, &tmp, &error);
336
tmp= (char*) val + cmin(2, val_len);
344
tmp= (char*) val + min(2, val_len);
337
345
l_time->day= (int) my_strtoll10(val, &tmp, &error);
341
tmp= (char*) val + cmin(2, val_len);
349
tmp= (char*) val + min(2, val_len);
342
350
l_time->day= (int) my_strtoll10(val, &tmp, &error);
343
351
/* Skip 'st, 'nd, 'th .. */
344
val= tmp + cmin((int) (val_end-tmp), 2);
352
val= tmp + min((int) (val_end-tmp), 2);
352
360
/* fall through */
355
tmp= (char*) val + cmin(2, val_len);
363
tmp= (char*) val + min(2, val_len);
356
364
l_time->hour= (int) my_strtoll10(val, &tmp, &error);
362
tmp= (char*) val + cmin(2, val_len);
370
tmp= (char*) val + min(2, val_len);
363
371
l_time->minute= (int) my_strtoll10(val, &tmp, &error);
370
tmp= (char*) val + cmin(2, val_len);
378
tmp= (char*) val + min(2, val_len);
371
379
l_time->second= (int) my_strtoll10(val, &tmp, &error);
389
397
if (val_len < 2 || ! usa_time)
391
if (!my_strnncoll(&my_charset_utf8_general_ci,
392
(const unsigned char *) val, 2,
393
(const unsigned char *) "PM", 2))
399
if (!my_strnncoll(&my_charset_latin1,
400
(const uchar *) val, 2,
401
(const uchar *) "PM", 2))
395
else if (my_strnncoll(&my_charset_utf8_general_ci,
396
(const unsigned char *) val, 2,
397
(const unsigned char *) "AM", 2))
403
else if (my_strnncoll(&my_charset_latin1,
404
(const uchar *) val, 2,
405
(const uchar *) "AM", 2))
422
tmp= (char*) val + cmin(val_len, 3);
430
tmp= (char*) val + min(val_len, 3);
423
431
yearday= (int) my_strtoll10(val, &tmp, &error);
432
440
sunday_first_n_first_week_non_iso= (*ptr=='U' || *ptr== 'V');
433
441
strict_week_number= (*ptr=='V' || *ptr=='v');
434
tmp= (char*) val + cmin(val_len, 2);
442
tmp= (char*) val + min(val_len, 2);
435
443
if ((week_number= (int) my_strtoll10(val, &tmp, &error)) < 0 ||
436
444
(strict_week_number && !week_number) ||
437
445
week_number > 53)
445
453
strict_week_number_year_type= (*ptr=='X');
446
tmp= (char*) val + cmin(4, val_len);
454
tmp= (char*) val + min(4, val_len);
447
455
strict_week_number_year= (int) my_strtoll10(val, &tmp, &error);
457
465
if (extract_date_time(&time_ampm_format, val,
458
466
(uint)(val_end - val), l_time,
459
467
cached_timestamp_type, &val, "time"))
463
471
/* Time in 24-hour notation */
465
473
if (extract_date_time(&time_24hrs_format, val,
466
474
(uint)(val_end - val), l_time,
467
475
cached_timestamp_type, &val, "time"))
471
479
/* Conversion specifiers that match classes of characters */
576
if (!my_isspace(&my_charset_utf8_general_ci,*val))
584
if (!my_isspace(&my_charset_latin1,*val))
578
make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
586
make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
579
587
val_begin, length,
580
cached_timestamp_type, NULL);
588
cached_timestamp_type, NullS);
583
591
} while (++val != val_end);
590
strmake(buff, val_begin, cmin(length, (uint)sizeof(buff)-1));
591
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
598
strmake(buff, val_begin, min(length, sizeof(buff)-1));
599
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
592
600
ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
593
601
date_time_type, buff, "str_to_date");
600
608
Create a formated date/time value in a string.
603
bool make_date_time(DATE_TIME_FORMAT *format, DRIZZLE_TIME *l_time,
604
enum enum_drizzle_timestamp_type type, String *str)
611
bool make_date_time(DATE_TIME_FORMAT *format, MYSQL_TIME *l_time,
612
timestamp_type type, String *str)
606
614
char intbuff[15];
610
618
const char *ptr, *end;
611
619
THD *thd= current_thd;
657
665
system_charset_info);
660
if (type == DRIZZLE_TIMESTAMP_TIME)
668
if (type == MYSQL_TIMESTAMP_TIME)
662
670
length= int10_to_str(l_time->day, intbuff, 10) - intbuff;
663
671
str->append_with_prefill(intbuff, length, 1, '0');
724
732
str->append_with_prefill(intbuff, length, 2, '0');
727
if (type == DRIZZLE_TIMESTAMP_TIME)
735
if (type == MYSQL_TIMESTAMP_TIME)
729
737
length= int10_to_str(calc_daynr(l_time->year,l_time->month,
745
753
str->append(hours_i < 12 ? "AM" : "PM",2);
748
length= sprintf(intbuff,
756
length= my_sprintf(intbuff,
749
758
((l_time->hour % 24) < 12) ?
750
759
"%02d:%02d:%02d AM" : "%02d:%02d:%02d PM",
751
760
(l_time->hour+11)%12+1,
754
763
str->append(intbuff, length);
759
768
str->append_with_prefill(intbuff, length, 2, '0');
762
length= sprintf(intbuff,
771
length= my_sprintf(intbuff,
763
773
"%02d:%02d:%02d",
767
777
str->append(intbuff, length);
773
if (type == DRIZZLE_TIMESTAMP_TIME)
783
if (type == MYSQL_TIMESTAMP_TIME)
775
785
length= int10_to_str(calc_week(l_time,
848
858
For example, '1.1' -> '1.100000'
851
static bool get_interval_info(const char *str,uint32_t length, const CHARSET_INFO * const cs,
852
uint32_t count, uint64_t *values,
861
static bool get_interval_info(const char *str,uint length,CHARSET_INFO *cs,
862
uint count, ulonglong *values,
853
863
bool transform_msec)
855
865
const char *end=str+length;
857
867
while (str != end && !my_isdigit(cs,*str))
860
870
for (i=0 ; i < count ; i++)
863
873
const char *start= str;
864
874
for (value=0; str != end && my_isdigit(cs,*str) ; str++)
865
value= value * 10L + (int64_t) (*str - '0');
875
value= value*LL(10) + (longlong) (*str - '0');
866
876
if (transform_msec && i == count - 1) // microseconds always last
868
878
long msec_length= 6 - (str - start);
878
888
/* Change values[0...i-1] -> values[0...count-1] */
879
bmove_upp((unsigned char*) (values+count), (unsigned char*) (values+i),
889
bmove_upp((uchar*) (values+count), (uchar*) (values+i),
880
890
sizeof(*values)*i);
881
memset(values, 0, sizeof(*values)*(count-i));
891
bzero((uchar*) values, sizeof(*values)*(count-i));
889
int64_t Item_func_period_add::val_int()
899
longlong Item_func_period_add::val_int()
901
DBUG_ASSERT(fixed == 1);
892
902
ulong period=(ulong) args[0]->val_int();
893
903
int months=(int) args[1]->val_int();
895
905
if ((null_value=args[0]->null_value || args[1]->null_value) ||
897
907
return 0; /* purecov: inspected */
899
909
convert_month_to_period((uint) ((int) convert_period_to_month(period)+
904
int64_t Item_func_period_diff::val_int()
914
longlong Item_func_period_diff::val_int()
916
DBUG_ASSERT(fixed == 1);
907
917
ulong period1=(ulong) args[0]->val_int();
908
918
ulong period2=(ulong) args[1]->val_int();
910
920
if ((null_value=args[0]->null_value || args[1]->null_value))
911
921
return 0; /* purecov: inspected */
912
return (int64_t) ((long) convert_period_to_month(period1)-
922
return (longlong) ((long) convert_period_to_month(period1)-
913
923
(long) convert_period_to_month(period2));
918
int64_t Item_func_to_days::val_int()
928
longlong Item_func_to_days::val_int()
930
DBUG_ASSERT(fixed == 1);
922
932
if (get_arg0_date(<ime, TIME_NO_ZERO_DATE))
924
return (int64_t) calc_daynr(ltime.year,ltime.month,ltime.day);
934
return (longlong) calc_daynr(ltime.year,ltime.month,ltime.day);
944
954
if (args[0]->type() == Item::FIELD_ITEM)
946
if (args[0]->field_type() == DRIZZLE_TYPE_NEWDATE)
956
if (args[0]->field_type() == MYSQL_TYPE_DATE)
947
957
return MONOTONIC_STRICT_INCREASING;
948
if (args[0]->field_type() == DRIZZLE_TYPE_DATETIME)
958
if (args[0]->field_type() == MYSQL_TYPE_DATETIME)
949
959
return MONOTONIC_INCREASING;
951
961
return NON_MONOTONIC;
955
int64_t Item_func_to_days::val_int_endpoint(bool left_endp, bool *incl_endp)
965
longlong Item_func_to_days::val_int_endpoint(bool left_endp, bool *incl_endp)
967
DBUG_ASSERT(fixed == 1);
960
970
if (get_arg0_date(<ime, TIME_NO_ZERO_DATE))
962
972
/* got NULL, leave the incl_endp intact */
965
res=(int64_t) calc_daynr(ltime.year,ltime.month,ltime.day);
975
res=(longlong) calc_daynr(ltime.year,ltime.month,ltime.day);
967
if (args[0]->field_type() == DRIZZLE_TYPE_NEWDATE)
977
if (args[0]->field_type() == MYSQL_TYPE_DATE)
969
979
// TO_DAYS() is strictly monotonic for dates, leave incl_endp intact
985
995
ltime.second_part))
986
996
; /* do nothing */
993
int64_t Item_func_dayofyear::val_int()
1003
longlong Item_func_dayofyear::val_int()
1005
DBUG_ASSERT(fixed == 1);
997
1007
if (get_arg0_date(<ime,TIME_NO_ZERO_DATE))
999
return (int64_t) calc_daynr(ltime.year,ltime.month,ltime.day) -
1009
return (longlong) calc_daynr(ltime.year,ltime.month,ltime.day) -
1000
1010
calc_daynr(ltime.year,1,1) + 1;
1003
int64_t Item_func_dayofmonth::val_int()
1013
longlong Item_func_dayofmonth::val_int()
1015
DBUG_ASSERT(fixed == 1);
1007
1017
(void) get_arg0_date(<ime, TIME_FUZZY_DATE);
1008
return (int64_t) ltime.day;
1018
return (longlong) ltime.day;
1011
int64_t Item_func_month::val_int()
1021
longlong Item_func_month::val_int()
1023
DBUG_ASSERT(fixed == 1);
1015
1025
(void) get_arg0_date(<ime, TIME_FUZZY_DATE);
1016
return (int64_t) ltime.month;
1026
return (longlong) ltime.month;
1020
1030
String* Item_func_monthname::val_str(String* str)
1032
DBUG_ASSERT(fixed == 1);
1023
1033
const char *month_name;
1024
uint32_t month= (uint) val_int();
1034
uint month= (uint) val_int();
1025
1035
THD *thd= current_thd;
1027
1037
if (null_value || !month)
1040
1050
Returns the quarter of the year.
1043
int64_t Item_func_quarter::val_int()
1053
longlong Item_func_quarter::val_int()
1055
DBUG_ASSERT(fixed == 1);
1047
1057
if (get_arg0_date(<ime, TIME_FUZZY_DATE))
1049
return (int64_t) ((ltime.month+2)/3);
1059
return (longlong) ((ltime.month+2)/3);
1052
int64_t Item_func_hour::val_int()
1062
longlong Item_func_hour::val_int()
1064
DBUG_ASSERT(fixed == 1);
1056
1066
(void) get_arg0_time(<ime);
1057
1067
return ltime.hour;
1060
int64_t Item_func_minute::val_int()
1070
longlong Item_func_minute::val_int()
1072
DBUG_ASSERT(fixed == 1);
1064
1074
(void) get_arg0_time(<ime);
1065
1075
return ltime.minute;
1069
1079
Returns the second in time_exp in the range of 0 - 59.
1071
int64_t Item_func_second::val_int()
1081
longlong Item_func_second::val_int()
1083
DBUG_ASSERT(fixed == 1);
1075
1085
(void) get_arg0_time(<ime);
1076
1086
return ltime.second;
1080
uint32_t week_mode(uint32_t mode)
1090
uint week_mode(uint mode)
1082
uint32_t week_format= (mode & 7);
1092
uint week_format= (mode & 7);
1083
1093
if (!(week_format & WEEK_MONDAY_FIRST))
1084
1094
week_format^= WEEK_FIRST_WEEKDAY;
1085
1095
return week_format;
1119
int64_t Item_func_week::val_int()
1129
longlong Item_func_week::val_int()
1131
DBUG_ASSERT(fixed == 1);
1124
1134
if (get_arg0_date(<ime, TIME_NO_ZERO_DATE))
1126
return (int64_t) calc_week(<ime,
1136
return (longlong) calc_week(<ime,
1127
1137
week_mode((uint) args[1]->val_int()),
1132
int64_t Item_func_yearweek::val_int()
1142
longlong Item_func_yearweek::val_int()
1144
DBUG_ASSERT(fixed == 1);
1137
1147
if (get_arg0_date(<ime, TIME_NO_ZERO_DATE))
1139
1149
week= calc_week(<ime,
1146
int64_t Item_func_weekday::val_int()
1156
longlong Item_func_weekday::val_int()
1158
DBUG_ASSERT(fixed == 1);
1151
1161
if (get_arg0_date(<ime, TIME_NO_ZERO_DATE))
1154
return (int64_t) calc_weekday(calc_daynr(ltime.year, ltime.month,
1164
return (longlong) calc_weekday(calc_daynr(ltime.year, ltime.month,
1156
1166
odbc_type) + test(odbc_type);
1176
int64_t Item_func_year::val_int()
1186
longlong Item_func_year::val_int()
1188
DBUG_ASSERT(fixed == 1);
1180
1190
(void) get_arg0_date(<ime, TIME_FUZZY_DATE);
1181
return (int64_t) ltime.year;
1191
return (longlong) ltime.year;
1199
1209
enum_monotonicity_info Item_func_year::get_monotonicity_info() const
1201
1211
if (args[0]->type() == Item::FIELD_ITEM &&
1202
(args[0]->field_type() == DRIZZLE_TYPE_NEWDATE ||
1203
args[0]->field_type() == DRIZZLE_TYPE_DATETIME))
1212
(args[0]->field_type() == MYSQL_TYPE_DATE ||
1213
args[0]->field_type() == MYSQL_TYPE_DATETIME))
1204
1214
return MONOTONIC_INCREASING;
1205
1215
return NON_MONOTONIC;
1209
int64_t Item_func_year::val_int_endpoint(bool left_endp, bool *incl_endp)
1219
longlong Item_func_year::val_int_endpoint(bool left_endp, bool *incl_endp)
1221
DBUG_ASSERT(fixed == 1);
1213
1223
if (get_arg0_date(<ime, TIME_FUZZY_DATE))
1215
1225
/* got NULL, leave the incl_endp intact */
1226
return LONGLONG_MIN;
1231
1241
!(ltime.hour || ltime.minute || ltime.second || ltime.second_part))
1232
1242
; /* do nothing */
1235
1245
return ltime.year;
1239
int64_t Item_func_unix_timestamp::val_int()
1249
longlong Item_func_unix_timestamp::val_int()
1254
DBUG_ASSERT(fixed == 1);
1245
1255
if (arg_count == 0)
1246
return (int64_t) current_thd->query_start();
1256
return (longlong) current_thd->query_start();
1247
1257
if (args[0]->type() == FIELD_ITEM)
1248
1258
{ // Optimize timestamp field
1249
1259
Field *field=((Item_field*) args[0])->field;
1250
if (field->type() == DRIZZLE_TYPE_TIMESTAMP)
1260
if (field->type() == MYSQL_TYPE_TIMESTAMP)
1251
1261
return ((Field_timestamp*) field)->get_timestamp(&null_value);
1265
return (int64_t) TIME_to_timestamp(current_thd, <ime, ¬_used);
1275
return (longlong) TIME_to_timestamp(current_thd, <ime, ¬_used);
1269
int64_t Item_func_time_to_sec::val_int()
1279
longlong Item_func_time_to_sec::val_int()
1281
DBUG_ASSERT(fixed == 1);
1274
1284
(void) get_arg0_time(<ime);
1275
1285
seconds=ltime.hour*3600L+ltime.minute*60+ltime.second;
1276
1286
return ltime.neg ? -seconds : seconds;
1286
1296
bool get_interval_value(Item *args,interval_type int_type,
1287
1297
String *str_value, INTERVAL *interval)
1291
1301
const char *str= NULL;
1292
1302
size_t length= 0;
1293
const CHARSET_INFO * const cs= str_value->charset();
1303
CHARSET_INFO *cs=str_value->charset();
1295
memset(interval, 0, sizeof(*interval));
1305
bzero((char*) interval,sizeof(*interval));
1296
1306
if ((int) int_type <= INTERVAL_MICROSECOND)
1298
1308
value= args->val_int();
1454
int64_t Item_date::val_int()
1464
longlong Item_date::val_int()
1466
DBUG_ASSERT(fixed == 1);
1458
1468
if (get_date(<ime, TIME_FUZZY_DATE))
1460
return (int64_t) (ltime.year*10000L+ltime.month*100+ltime.day);
1470
return (longlong) (ltime.year*10000L+ltime.month*100+ltime.day);
1464
bool Item_func_from_days::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date __attribute__((unused)))
1474
bool Item_func_from_days::get_date(MYSQL_TIME *ltime, uint fuzzy_date)
1466
int64_t value=args[0]->val_int();
1476
longlong value=args[0]->val_int();
1467
1477
if ((null_value=args[0]->null_value))
1469
memset(ltime, 0, sizeof(DRIZZLE_TIME));
1479
bzero(ltime, sizeof(MYSQL_TIME));
1470
1480
get_date_from_daynr((long) value, <ime->year, <ime->month, <ime->day);
1471
ltime->time_type= DRIZZLE_TIMESTAMP_DATE;
1481
ltime->time_type= MYSQL_TIMESTAMP_DATE;
1484
1494
/* We don't need to set second_part and neg because they already 0 */
1485
1495
ltime.hour= ltime.minute= ltime.second= 0;
1486
ltime.time_type= DRIZZLE_TIMESTAMP_DATE;
1487
value= (int64_t) TIME_to_uint64_t_date(<ime);
1496
ltime.time_type= MYSQL_TIMESTAMP_DATE;
1497
value= (longlong) TIME_to_ulonglong_date(<ime);
1490
1500
String *Item_func_curdate::val_str(String *str)
1502
DBUG_ASSERT(fixed == 1);
1493
1503
if (str->alloc(MAX_DATE_STRING_REP_LENGTH))
1503
Converts current time in my_time_t to DRIZZLE_TIME represenatation for local
1513
Converts current time in my_time_t to MYSQL_TIME represenatation for local
1504
1514
time zone. Defines time zone (local) used for whole CURDATE function.
1506
void Item_func_curdate_local::store_now_in_TIME(DRIZZLE_TIME *now_time)
1516
void Item_func_curdate_local::store_now_in_TIME(MYSQL_TIME *now_time)
1508
1518
THD *thd= current_thd;
1509
1519
thd->variables.time_zone->gmt_sec_to_TIME(now_time,
1516
Converts current time in my_time_t to DRIZZLE_TIME represenatation for UTC
1526
Converts current time in my_time_t to MYSQL_TIME represenatation for UTC
1517
1527
time zone. Defines time zone (UTC) used for whole UTC_DATE function.
1519
void Item_func_curdate_utc::store_now_in_TIME(DRIZZLE_TIME *now_time)
1529
void Item_func_curdate_utc::store_now_in_TIME(MYSQL_TIME *now_time)
1521
1531
my_tz_UTC->gmt_sec_to_TIME(now_time,
1522
1532
(my_time_t)(current_thd->query_start()));
1530
bool Item_func_curdate::get_date(DRIZZLE_TIME *res,
1531
uint32_t fuzzy_date __attribute__((unused)))
1540
bool Item_func_curdate::get_date(MYSQL_TIME *res,
1541
uint fuzzy_date __attribute__((unused)))
1538
String *Item_func_curtime::val_str(String *str __attribute__((unused)))
1548
String *Item_func_curtime::val_str(String *str)
1550
DBUG_ASSERT(fixed == 1);
1541
1551
str_value.set(buff, buff_length, &my_charset_bin);
1542
1552
return &str_value;
1546
1556
void Item_func_curtime::fix_length_and_dec()
1550
1560
decimals= DATETIME_DEC;
1551
1561
collation.set(&my_charset_bin);
1552
1562
store_now_in_TIME(<ime);
1553
value= TIME_to_uint64_t_time(<ime);
1563
value= TIME_to_ulonglong_time(<ime);
1554
1564
buff_length= (uint) my_time_to_str(<ime, buff);
1555
1565
max_length= buff_length;
1560
Converts current time in my_time_t to DRIZZLE_TIME represenatation for local
1570
Converts current time in my_time_t to MYSQL_TIME represenatation for local
1561
1571
time zone. Defines time zone (local) used for whole CURTIME function.
1563
void Item_func_curtime_local::store_now_in_TIME(DRIZZLE_TIME *now_time)
1573
void Item_func_curtime_local::store_now_in_TIME(MYSQL_TIME *now_time)
1565
1575
THD *thd= current_thd;
1566
1576
thd->variables.time_zone->gmt_sec_to_TIME(now_time,
1573
Converts current time in my_time_t to DRIZZLE_TIME represenatation for UTC
1583
Converts current time in my_time_t to MYSQL_TIME represenatation for UTC
1574
1584
time zone. Defines time zone (UTC) used for whole UTC_TIME function.
1576
void Item_func_curtime_utc::store_now_in_TIME(DRIZZLE_TIME *now_time)
1586
void Item_func_curtime_utc::store_now_in_TIME(MYSQL_TIME *now_time)
1578
1588
my_tz_UTC->gmt_sec_to_TIME(now_time,
1579
1589
(my_time_t)(current_thd->query_start()));
1598
1608
collation.set(&my_charset_bin);
1600
1610
store_now_in_TIME(<ime);
1601
value= (int64_t) TIME_to_uint64_t_datetime(<ime);
1611
value= (longlong) TIME_to_ulonglong_datetime(<ime);
1603
1613
buff_length= (uint) my_datetime_to_str(<ime, buff);
1604
1614
max_length= buff_length;
1609
Converts current time in my_time_t to DRIZZLE_TIME represenatation for local
1619
Converts current time in my_time_t to MYSQL_TIME represenatation for local
1610
1620
time zone. Defines time zone (local) used for whole NOW function.
1612
void Item_func_now_local::store_now_in_TIME(DRIZZLE_TIME *now_time)
1622
void Item_func_now_local::store_now_in_TIME(MYSQL_TIME *now_time)
1614
1624
THD *thd= current_thd;
1615
1625
thd->variables.time_zone->gmt_sec_to_TIME(now_time,
1622
Converts current time in my_time_t to DRIZZLE_TIME represenatation for UTC
1632
Converts current time in my_time_t to MYSQL_TIME represenatation for UTC
1623
1633
time zone. Defines time zone (UTC) used for whole UTC_TIMESTAMP function.
1625
void Item_func_now_utc::store_now_in_TIME(DRIZZLE_TIME *now_time)
1635
void Item_func_now_utc::store_now_in_TIME(MYSQL_TIME *now_time)
1627
1637
my_tz_UTC->gmt_sec_to_TIME(now_time,
1628
1638
(my_time_t)(current_thd->query_start()));
1636
bool Item_func_now::get_date(DRIZZLE_TIME *res,
1637
uint32_t fuzzy_date __attribute__((unused)))
1646
bool Item_func_now::get_date(MYSQL_TIME *res,
1647
uint fuzzy_date __attribute__((unused)))
1644
int Item_func_now::save_in_field(Field *to, bool no_conversions __attribute__((unused)))
1654
int Item_func_now::save_in_field(Field *to, bool no_conversions)
1646
1656
to->set_notnull();
1647
return to->store_time(<ime, DRIZZLE_TIMESTAMP_DATETIME);
1657
return to->store_time(<ime, MYSQL_TIMESTAMP_DATETIME);
1652
Converts current time in my_time_t to DRIZZLE_TIME represenatation for local
1662
Converts current time in my_time_t to MYSQL_TIME represenatation for local
1653
1663
time zone. Defines time zone (local) used for whole SYSDATE function.
1655
void Item_func_sysdate_local::store_now_in_TIME(DRIZZLE_TIME *now_time)
1665
void Item_func_sysdate_local::store_now_in_TIME(MYSQL_TIME *now_time)
1657
1667
THD *thd= current_thd;
1658
1668
thd->variables.time_zone->gmt_sec_to_TIME(now_time, (my_time_t) my_time(0));
1663
String *Item_func_sysdate_local::val_str(String *str __attribute__((unused)))
1673
String *Item_func_sysdate_local::val_str(String *str)
1675
DBUG_ASSERT(fixed == 1);
1666
1676
store_now_in_TIME(<ime);
1667
1677
buff_length= (uint) my_datetime_to_str(<ime, buff);
1668
1678
str_value.set(buff, buff_length, &my_charset_bin);
1673
int64_t Item_func_sysdate_local::val_int()
1683
longlong Item_func_sysdate_local::val_int()
1685
DBUG_ASSERT(fixed == 1);
1676
1686
store_now_in_TIME(<ime);
1677
return (int64_t) TIME_to_uint64_t_datetime(<ime);
1687
return (longlong) TIME_to_ulonglong_datetime(<ime);
1681
1691
double Item_func_sysdate_local::val_real()
1693
DBUG_ASSERT(fixed == 1);
1684
1694
store_now_in_TIME(<ime);
1685
return uint64_t2double(TIME_to_uint64_t_datetime(<ime));
1695
return ulonglong2double(TIME_to_ulonglong_datetime(<ime));
1697
bool Item_func_sysdate_local::get_date(DRIZZLE_TIME *res,
1698
uint32_t fuzzy_date __attribute__((unused)))
1707
bool Item_func_sysdate_local::get_date(MYSQL_TIME *res,
1708
uint fuzzy_date __attribute__((unused)))
1700
1710
store_now_in_TIME(<ime);
1706
int Item_func_sysdate_local::save_in_field(Field *to, bool no_conversions __attribute__((unused)))
1716
int Item_func_sysdate_local::save_in_field(Field *to, bool no_conversions)
1708
1718
store_now_in_TIME(<ime);
1709
1719
to->set_notnull();
1710
to->store_time(<ime, DRIZZLE_TIMESTAMP_DATETIME);
1720
to->store_time(<ime, MYSQL_TIMESTAMP_DATETIME);
1715
1725
String *Item_func_sec_to_time::val_str(String *str)
1719
int64_t arg_val= args[0]->val_int();
1727
DBUG_ASSERT(fixed == 1);
1729
longlong arg_val= args[0]->val_int();
1721
1731
if ((null_value=args[0]->null_value) ||
1722
1732
str->alloc(MAX_DATE_STRING_REP_LENGTH))
1735
int64_t Item_func_sec_to_time::val_int()
1745
longlong Item_func_sec_to_time::val_int()
1739
int64_t arg_val= args[0]->val_int();
1747
DBUG_ASSERT(fixed == 1);
1749
longlong arg_val= args[0]->val_int();
1741
1751
if ((null_value=args[0]->null_value))
1758
1768
Item *arg1= args[1]->this_item();
1761
const CHARSET_INFO * const cs= thd->variables.collation_connection;
1762
uint32_t repertoire= arg1->collation.repertoire;
1771
CHARSET_INFO *cs= thd->variables.collation_connection;
1772
uint32 repertoire= arg1->collation.repertoire;
1763
1773
if (!thd->variables.lc_time_names->is_ascii)
1764
1774
repertoire|= MY_REPERTOIRE_EXTENDED;
1765
1775
collation.set(cs, arg1->collation.derivation, repertoire);
1774
1784
fixed_length=0;
1775
max_length=cmin(arg1->max_length,(uint32_t) MAX_BLOB_WIDTH) * 10 *
1785
max_length=min(arg1->max_length, MAX_BLOB_WIDTH) * 10 *
1776
1786
collation.collation->mbmaxlen;
1777
1787
set_if_smaller(max_length,MAX_BLOB_WIDTH);
1924
1934
/* Create the result string */
1925
1935
str->set_charset(collation.collation);
1926
1936
if (!make_date_time(&date_time_format, &l_time,
1927
is_time_format ? DRIZZLE_TIMESTAMP_TIME :
1928
DRIZZLE_TIMESTAMP_DATE,
1937
is_time_format ? MYSQL_TIMESTAMP_TIME :
1938
MYSQL_TIMESTAMP_DATE,
1970
int64_t Item_func_from_unixtime::val_int()
1980
longlong Item_func_from_unixtime::val_int()
1972
DRIZZLE_TIME time_tmp;
1982
MYSQL_TIME time_tmp;
1984
DBUG_ASSERT(fixed == 1);
1976
1986
if (get_date(&time_tmp, 0))
1979
return (int64_t) TIME_to_uint64_t_datetime(&time_tmp);
1989
return (longlong) TIME_to_ulonglong_datetime(&time_tmp);
1982
bool Item_func_from_unixtime::get_date(DRIZZLE_TIME *ltime,
1983
uint32_t fuzzy_date __attribute__((unused)))
1992
bool Item_func_from_unixtime::get_date(MYSQL_TIME *ltime,
1993
uint fuzzy_date __attribute__((unused)))
1985
uint64_t tmp= (uint64_t)(args[0]->val_int());
1995
ulonglong tmp= (ulonglong)(args[0]->val_int());
1987
1997
"tmp > TIMESTAMP_MAX_VALUE" check also covers case of negative
1988
1998
from_unixtime() argument since tmp is unsigned.
2009
2019
The field type for the result of an Item_date function is defined as
2012
- If first arg is a DRIZZLE_TYPE_DATETIME result is DRIZZLE_TYPE_DATETIME
2013
- If first arg is a DRIZZLE_TYPE_NEWDATE and the interval type uses hours,
2014
minutes or seconds then type is DRIZZLE_TYPE_DATETIME.
2015
- Otherwise the result is DRIZZLE_TYPE_VARCHAR
2016
(This is because you can't know if the string contains a DATE, DRIZZLE_TIME or
2022
- If first arg is a MYSQL_TYPE_DATETIME result is MYSQL_TYPE_DATETIME
2023
- If first arg is a MYSQL_TYPE_DATE and the interval type uses hours,
2024
minutes or seconds then type is MYSQL_TYPE_DATETIME.
2025
- Otherwise the result is MYSQL_TYPE_STRING
2026
(This is because you can't know if the string contains a DATE, MYSQL_TIME or
2017
2027
DATETIME argument)
2019
cached_field_type= DRIZZLE_TYPE_VARCHAR;
2029
cached_field_type= MYSQL_TYPE_STRING;
2020
2030
arg0_field_type= args[0]->field_type();
2021
if (arg0_field_type == DRIZZLE_TYPE_DATETIME ||
2022
arg0_field_type == DRIZZLE_TYPE_TIMESTAMP)
2023
cached_field_type= DRIZZLE_TYPE_DATETIME;
2024
else if (arg0_field_type == DRIZZLE_TYPE_NEWDATE)
2031
if (arg0_field_type == MYSQL_TYPE_DATETIME ||
2032
arg0_field_type == MYSQL_TYPE_TIMESTAMP)
2033
cached_field_type= MYSQL_TYPE_DATETIME;
2034
else if (arg0_field_type == MYSQL_TYPE_DATE)
2026
2036
if (int_type <= INTERVAL_DAY || int_type == INTERVAL_YEAR_MONTH)
2027
2037
cached_field_type= arg0_field_type;
2029
cached_field_type= DRIZZLE_TYPE_DATETIME;
2039
cached_field_type= MYSQL_TYPE_DATETIME;
2034
2044
/* Here arg[1] is a Item_interval object */
2036
bool Item_date_add_interval::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date __attribute__((unused)))
2046
bool Item_date_add_interval::get_date(MYSQL_TIME *ltime, uint fuzzy_date)
2038
2048
INTERVAL interval;
2053
2063
String *Item_date_add_interval::val_str(String *str)
2065
DBUG_ASSERT(fixed == 1);
2057
2067
enum date_time_format_types format;
2059
2069
if (Item_date_add_interval::get_date(<ime, TIME_NO_ZERO_DATE))
2062
if (ltime.time_type == DRIZZLE_TIMESTAMP_DATE)
2072
if (ltime.time_type == MYSQL_TIMESTAMP_DATE)
2063
2073
format= DATE_ONLY;
2064
2074
else if (ltime.second_part)
2065
2075
format= DATE_TIME_MICROSECOND;
2077
int64_t Item_date_add_interval::val_int()
2087
longlong Item_date_add_interval::val_int()
2089
DBUG_ASSERT(fixed == 1);
2082
2092
if (Item_date_add_interval::get_date(<ime, TIME_NO_ZERO_DATE))
2093
return (longlong) 0;
2084
2094
date = (ltime.year*100L + ltime.month)*100L + ltime.day;
2085
return ltime.time_type == DRIZZLE_TIMESTAMP_DATE ? date :
2095
return ltime.time_type == MYSQL_TIMESTAMP_DATE ? date :
2086
2096
((date*100L + ltime.hour)*100L+ ltime.minute)*100L + ltime.second;
2159
2169
case INTERVAL_HOUR_MICROSECOND: max_length=13; date_value=0; break;
2160
2170
case INTERVAL_MINUTE_MICROSECOND: max_length=11; date_value=0; break;
2161
2171
case INTERVAL_SECOND_MICROSECOND: max_length=9; date_value=0; break;
2162
case INTERVAL_LAST: assert(0); break; /* purecov: deadcode */
2172
case INTERVAL_LAST: DBUG_ASSERT(0); break; /* purecov: deadcode */
2167
int64_t Item_extract::val_int()
2177
longlong Item_extract::val_int()
2179
DBUG_ASSERT(fixed == 1);
2172
2182
ulong week_format;
2174
2184
if (date_value)
2203
2213
case INTERVAL_DAY_MINUTE: return (long) (ltime.day*10000L+
2204
2214
ltime.hour*100L+
2205
2215
ltime.minute)*neg;
2206
case INTERVAL_DAY_SECOND: return ((int64_t) ltime.day*1000000L+
2207
(int64_t) (ltime.hour*10000L+
2216
case INTERVAL_DAY_SECOND: return ((longlong) ltime.day*1000000L+
2217
(longlong) (ltime.hour*10000L+
2208
2218
ltime.minute*100+
2209
2219
ltime.second))*neg;
2210
2220
case INTERVAL_HOUR: return (long) ltime.hour*neg;
2215
2225
case INTERVAL_MINUTE_SECOND: return (long) (ltime.minute*100+ltime.second)*neg;
2216
2226
case INTERVAL_SECOND: return (long) ltime.second*neg;
2217
2227
case INTERVAL_MICROSECOND: return (long) ltime.second_part*neg;
2218
case INTERVAL_DAY_MICROSECOND: return (((int64_t)ltime.day*1000000L +
2219
(int64_t)ltime.hour*10000L +
2228
case INTERVAL_DAY_MICROSECOND: return (((longlong)ltime.day*1000000L +
2229
(longlong)ltime.hour*10000L +
2220
2230
ltime.minute*100 +
2221
2231
ltime.second)*1000000L +
2222
2232
ltime.second_part)*neg;
2223
case INTERVAL_HOUR_MICROSECOND: return (((int64_t)ltime.hour*10000L +
2233
case INTERVAL_HOUR_MICROSECOND: return (((longlong)ltime.hour*10000L +
2224
2234
ltime.minute*100 +
2225
2235
ltime.second)*1000000L +
2226
2236
ltime.second_part)*neg;
2227
case INTERVAL_MINUTE_MICROSECOND: return (((int64_t)(ltime.minute*100+
2237
case INTERVAL_MINUTE_MICROSECOND: return (((longlong)(ltime.minute*100+
2228
2238
ltime.second))*1000000L+
2229
2239
ltime.second_part)*neg;
2230
case INTERVAL_SECOND_MICROSECOND: return ((int64_t)ltime.second*1000000L+
2240
case INTERVAL_SECOND_MICROSECOND: return ((longlong)ltime.second*1000000L+
2231
2241
ltime.second_part)*neg;
2232
case INTERVAL_LAST: assert(0); break; /* purecov: deadcode */
2242
case INTERVAL_LAST: DBUG_ASSERT(0); break; /* purecov: deadcode */
2234
2244
return 0; // Impossible
2291
2301
char buffer[20];
2292
2302
// my_charset_bin is good enough for numbers
2293
2303
String st(buffer, sizeof(buffer), &my_charset_bin);
2294
st.set((uint64_t)cast_length, &my_charset_bin);
2304
st.set((ulonglong)cast_length, &my_charset_bin);
2295
2305
str->append(st);
2296
2306
str->append(')');
2322
2332
// Convert character set if differ
2323
uint32_t dummy_errors;
2324
2334
if (!(res= args[0]->val_str(&tmp_value)) ||
2325
2335
str->copy(res->ptr(), res->length(), from_cs,
2326
2336
cast_cs, &dummy_errors))
2341
2351
if (cast_length >= 0)
2343
if (res->length() > (length= (uint32_t) res->charpos(cast_length)))
2353
if (res->length() > (length= (uint32) res->charpos(cast_length)))
2344
2354
{ // Safe even if const arg
2345
2355
char char_type[40];
2346
snprintf(char_type, sizeof(char_type), "%s(%lu)",
2347
cast_cs == &my_charset_bin ? "BINARY" : "CHAR",
2356
my_snprintf(char_type, sizeof(char_type), "%s(%lu)",
2357
cast_cs == &my_charset_bin ? "BINARY" : "CHAR",
2350
2360
if (!res->alloced_length())
2351
2361
{ // Don't change const str
2352
2362
str_value= *res; // Not malloced string
2353
2363
res= &str_value;
2355
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2365
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2356
2366
ER_TRUNCATED_WRONG_VALUE,
2357
2367
ER(ER_TRUNCATED_WRONG_VALUE), char_type,
2358
2368
res->c_ptr_safe());
2366
2376
str->copy(*res);
2369
memset(res->ptr() + res->length(), 0,
2370
(uint) cast_length - res->length());
2379
bzero((char*) res->ptr() + res->length(),
2380
(uint) cast_length - res->length());
2371
2381
res->length(cast_length);
2404
2414
from_cs= (args[0]->result_type() == INT_RESULT ||
2405
2415
args[0]->result_type() == DECIMAL_RESULT ||
2406
2416
args[0]->result_type() == REAL_RESULT) ?
2407
(cast_cs->mbminlen == 1 ? cast_cs : &my_charset_utf8_general_ci) :
2417
(cast_cs->mbminlen == 1 ? cast_cs : &my_charset_latin1) :
2408
2418
args[0]->collation.collation;
2409
2419
charset_conversion= (cast_cs->mbmaxlen > 1) ||
2410
2420
(!my_charset_same(from_cs, cast_cs) && from_cs != &my_charset_bin && cast_cs != &my_charset_bin);
2433
int64_t Item_datetime_typecast::val_int()
2443
longlong Item_datetime_typecast::val_int()
2445
DBUG_ASSERT(fixed == 1);
2437
2447
if (get_arg0_date(<ime,1))
2443
return TIME_to_uint64_t_datetime(<ime);
2453
return TIME_to_ulonglong_datetime(<ime);
2447
bool Item_time_typecast::get_time(DRIZZLE_TIME *ltime)
2457
bool Item_time_typecast::get_time(MYSQL_TIME *ltime)
2449
2459
bool res= get_arg0_time(ltime);
2451
For DRIZZLE_TIMESTAMP_TIME value we can have non-zero day part,
2461
For MYSQL_TIMESTAMP_TIME value we can have non-zero day part,
2452
2462
which we should not lose.
2454
if (ltime->time_type == DRIZZLE_TIMESTAMP_DATETIME)
2464
if (ltime->time_type == MYSQL_TIMESTAMP_DATETIME)
2455
2465
ltime->year= ltime->month= ltime->day= 0;
2456
ltime->time_type= DRIZZLE_TIMESTAMP_TIME;
2466
ltime->time_type= MYSQL_TIMESTAMP_TIME;
2461
int64_t Item_time_typecast::val_int()
2471
longlong Item_time_typecast::val_int()
2464
2474
if (get_time(<ime))
2487
bool Item_date_typecast::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date __attribute__((unused)))
2497
bool Item_date_typecast::get_date(MYSQL_TIME *ltime, uint fuzzy_date)
2489
2499
bool res= get_arg0_date(ltime, TIME_FUZZY_DATE);
2490
2500
ltime->hour= ltime->minute= ltime->second= ltime->second_part= 0;
2491
ltime->time_type= DRIZZLE_TIMESTAMP_DATE;
2501
ltime->time_type= MYSQL_TIMESTAMP_DATE;
2496
bool Item_date_typecast::get_time(DRIZZLE_TIME *ltime)
2506
bool Item_date_typecast::get_time(MYSQL_TIME *ltime)
2498
memset(ltime, 0, sizeof(DRIZZLE_TIME));
2508
bzero((char *)ltime, sizeof(MYSQL_TIME));
2499
2509
return args[0]->null_value;
2503
2513
String *Item_date_typecast::val_str(String *str)
2515
DBUG_ASSERT(fixed == 1);
2508
2518
if (!get_arg0_date(<ime, TIME_FUZZY_DATE) &&
2509
2519
!str->alloc(MAX_DATE_STRING_REP_LENGTH))
2519
int64_t Item_date_typecast::val_int()
2529
longlong Item_date_typecast::val_int()
2531
DBUG_ASSERT(fixed == 1);
2523
2533
if ((null_value= args[0]->get_date(<ime, TIME_FUZZY_DATE)))
2525
return (int64_t) (ltime.year * 10000L + ltime.month * 100 + ltime.day);
2535
return (longlong) (ltime.year * 10000L + ltime.month * 100 + ltime.day);
2578
2588
for dates between 0000-01-01 and 0099-12-31
2581
int64_t Item_func_makedate::val_int()
2591
longlong Item_func_makedate::val_int()
2584
DRIZZLE_TIME l_time;
2593
DBUG_ASSERT(fixed == 1);
2585
2595
long daynr= (long) args[1]->val_int();
2586
2596
long year= (long) args[0]->val_int();
2601
2611
get_date_from_daynr(days,&l_time.year,&l_time.month,&l_time.day);
2602
return (int64_t) (l_time.year * 10000L + l_time.month * 100 + l_time.day);
2612
return (longlong) (l_time.year * 10000L + l_time.month * 100 + l_time.day);
2619
2629
The field type for the result of an Item_func_add_time function is defined
2622
- If first arg is a DRIZZLE_TYPE_DATETIME or DRIZZLE_TYPE_TIMESTAMP
2623
result is DRIZZLE_TYPE_DATETIME
2624
- If first arg is a DRIZZLE_TYPE_TIME result is DRIZZLE_TYPE_TIME
2625
- Otherwise the result is DRIZZLE_TYPE_VARCHAR
2632
- If first arg is a MYSQL_TYPE_DATETIME or MYSQL_TYPE_TIMESTAMP
2633
result is MYSQL_TYPE_DATETIME
2634
- If first arg is a MYSQL_TYPE_TIME result is MYSQL_TYPE_TIME
2635
- Otherwise the result is MYSQL_TYPE_STRING
2628
cached_field_type= DRIZZLE_TYPE_VARCHAR;
2638
cached_field_type= MYSQL_TYPE_STRING;
2629
2639
arg0_field_type= args[0]->field_type();
2630
if (arg0_field_type == DRIZZLE_TYPE_NEWDATE ||
2631
arg0_field_type == DRIZZLE_TYPE_DATETIME ||
2632
arg0_field_type == DRIZZLE_TYPE_TIMESTAMP)
2633
cached_field_type= DRIZZLE_TYPE_DATETIME;
2634
else if (arg0_field_type == DRIZZLE_TYPE_TIME)
2635
cached_field_type= DRIZZLE_TYPE_TIME;
2640
if (arg0_field_type == MYSQL_TYPE_DATE ||
2641
arg0_field_type == MYSQL_TYPE_DATETIME ||
2642
arg0_field_type == MYSQL_TYPE_TIMESTAMP)
2643
cached_field_type= MYSQL_TYPE_DATETIME;
2644
else if (arg0_field_type == MYSQL_TYPE_TIME)
2645
cached_field_type= MYSQL_TYPE_TIME;
2648
2658
String *Item_func_add_time::val_str(String *str)
2651
DRIZZLE_TIME l_time1, l_time2, l_time3;
2660
DBUG_ASSERT(fixed == 1);
2661
MYSQL_TIME l_time1, l_time2, l_time3;
2652
2662
bool is_time= 0;
2653
2663
long days, microseconds;
2655
2665
int l_sign= sign;
2660
2670
if (get_arg0_date(&l_time1, TIME_FUZZY_DATE) ||
2661
2671
args[1]->get_time(&l_time2) ||
2662
l_time1.time_type == DRIZZLE_TIMESTAMP_TIME ||
2663
l_time2.time_type != DRIZZLE_TIMESTAMP_TIME)
2672
l_time1.time_type == MYSQL_TIMESTAMP_TIME ||
2673
l_time2.time_type != MYSQL_TIMESTAMP_TIME)
2664
2674
goto null_date;
2666
2676
else // ADDTIME function
2668
2678
if (args[0]->get_time(&l_time1) ||
2669
2679
args[1]->get_time(&l_time2) ||
2670
l_time2.time_type == DRIZZLE_TIMESTAMP_DATETIME)
2680
l_time2.time_type == MYSQL_TIMESTAMP_DATETIME)
2671
2681
goto null_date;
2672
is_time= (l_time1.time_type == DRIZZLE_TIMESTAMP_TIME);
2682
is_time= (l_time1.time_type == MYSQL_TIMESTAMP_TIME);
2674
2684
if (l_time1.neg != l_time2.neg)
2675
2685
l_sign= -l_sign;
2677
memset(&l_time3, 0, sizeof(l_time3));
2687
bzero((char *)&l_time3, sizeof(l_time3));
2679
2689
l_time3.neg= calc_time_diff(&l_time1, &l_time2, -l_sign,
2680
2690
&seconds, µseconds);
2748
2758
String *Item_func_timediff::val_str(String *str)
2760
DBUG_ASSERT(fixed == 1);
2752
2762
long microseconds;
2754
DRIZZLE_TIME l_time1 ,l_time2, l_time3;
2764
MYSQL_TIME l_time1 ,l_time2, l_time3;
2757
2767
if (args[0]->get_time(&l_time1) ||
2762
2772
if (l_time1.neg != l_time2.neg)
2763
2773
l_sign= -l_sign;
2765
memset(&l_time3, 0, sizeof(l_time3));
2775
bzero((char *)&l_time3, sizeof(l_time3));
2767
2777
l_time3.neg= calc_time_diff(&l_time1, &l_time2, l_sign,
2768
2778
&seconds, µseconds);
2771
For DRIZZLE_TIMESTAMP_TIME only:
2781
For MYSQL_TIMESTAMP_TIME only:
2772
2782
If first argument was negative and diff between arguments
2773
2783
is non-zero we need to swap sign to get proper result.
2796
2806
String *Item_func_maketime::val_str(String *str)
2808
DBUG_ASSERT(fixed == 1);
2800
2810
bool overflow= 0;
2802
int64_t hour= args[0]->val_int();
2803
int64_t minute= args[1]->val_int();
2804
int64_t second= args[2]->val_int();
2812
longlong hour= args[0]->val_int();
2813
longlong minute= args[1]->val_int();
2814
longlong second= args[2]->val_int();
2806
2816
if ((null_value=(args[0]->null_value ||
2807
2817
args[1]->null_value ||
2811
2821
str->alloc(MAX_DATE_STRING_REP_LENGTH))))
2814
memset(<ime, 0, sizeof(ltime));
2824
bzero((char *)<ime, sizeof(ltime));
2817
2827
/* Check for integer overflows */
2830
if (args[0]->unsigned_flag)
2821
2835
if (-hour > UINT_MAX || hour > UINT_MAX)
2833
2847
ltime.minute= TIME_MAX_MINUTE;
2834
2848
ltime.second= TIME_MAX_SECOND;
2836
char *ptr= int64_t10_to_str(hour, buf, args[0]->unsigned_flag ? 10 : -10);
2850
char *ptr= longlong10_to_str(hour, buf, args[0]->unsigned_flag ? 10 : -10);
2837
2851
int len = (int)(ptr - buf) +
2838
sprintf(ptr, ":%02u:%02u", (uint)minute, (uint)second);
2839
make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2840
buf, len, DRIZZLE_TIMESTAMP_TIME,
2852
my_sprintf(ptr, (ptr, ":%02u:%02u", (uint)minute, (uint)second));
2853
make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2854
buf, len, MYSQL_TIMESTAMP_TIME,
2844
2858
if (make_time_with_warn((DATE_TIME_FORMAT *) 0, <ime, str))
2858
2872
Result: int value
2861
int64_t Item_func_microsecond::val_int()
2875
longlong Item_func_microsecond::val_int()
2877
DBUG_ASSERT(fixed == 1);
2865
2879
if (!get_arg0_time(<ime))
2866
2880
return ltime.second_part;
2871
int64_t Item_func_timestamp_diff::val_int()
2885
longlong Item_func_timestamp_diff::val_int()
2873
DRIZZLE_TIME ltime1, ltime2;
2887
MYSQL_TIME ltime1, ltime2;
2875
2889
long microseconds;
2876
2890
long months= 0;
2889
2903
int_type == INTERVAL_QUARTER ||
2890
2904
int_type == INTERVAL_MONTH)
2892
uint32_t year_beg, year_end, month_beg, month_end, day_beg, day_end;
2894
uint32_t second_beg, second_end, microsecond_beg, microsecond_end;
2906
uint year_beg, year_end, month_beg, month_end, day_beg, day_end;
2908
uint second_beg, second_end, microsecond_beg, microsecond_end;
2960
2974
case INTERVAL_MICROSECOND:
2962
2976
In MySQL difference between any two valid datetime values
2963
in microseconds fits into int64_t.
2977
in microseconds fits into longlong.
2965
2979
return (seconds*1000000L+microseconds)*neg;
3035
3049
(format_name= format->format_name);
3038
uint32_t format_name_len;
3052
uint format_name_len;
3039
3053
format_name_len= strlen(format_name);
3040
3054
if (val_len == format_name_len &&
3041
!my_strnncoll(&my_charset_utf8_general_ci,
3042
(const unsigned char *) val->ptr(), val_len,
3043
(const unsigned char *) format_name, val_len))
3055
!my_strnncoll(&my_charset_latin1,
3056
(const uchar *) val->ptr(), val_len,
3057
(const uchar *) format_name, val_len))
3045
3059
const char *format_str= get_date_time_format_str(format, type);
3046
3060
str->set(format_str, strlen(format_str), &my_charset_bin);
3059
3073
str->append('(');
3061
3075
switch (type) {
3062
case DRIZZLE_TIMESTAMP_DATE:
3076
case MYSQL_TIMESTAMP_DATE:
3063
3077
str->append(STRING_WITH_LEN("DATE, "));
3065
case DRIZZLE_TIMESTAMP_DATETIME:
3079
case MYSQL_TIMESTAMP_DATETIME:
3066
3080
str->append(STRING_WITH_LEN("DATETIME, "));
3068
case DRIZZLE_TIMESTAMP_TIME:
3082
case MYSQL_TIMESTAMP_TIME:
3069
3083
str->append(STRING_WITH_LEN("TIME, "));
3074
3088
args[0]->print(str, query_type);
3075
3089
str->append(')');
3103
3117
static date_time_format_types
3104
get_date_time_result_type(const char *format, uint32_t length)
3118
get_date_time_result_type(const char *format, uint length)
3106
3120
const char *time_part_frms= "HISThiklrs";
3107
3121
const char *date_part_frms= "MVUXYWabcjmvuxyw";
3152
cached_field_type= DRIZZLE_TYPE_DATETIME;
3166
cached_field_type= MYSQL_TYPE_DATETIME;
3153
3167
max_length= MAX_DATETIME_FULL_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
3154
cached_timestamp_type= DRIZZLE_TIMESTAMP_NONE;
3168
cached_timestamp_type= MYSQL_TIMESTAMP_NONE;
3155
3169
if ((const_item= args[1]->const_item()))
3157
3171
char format_buff[64];
3163
3177
format->length());
3164
3178
switch (cached_format_type) {
3165
3179
case DATE_ONLY:
3166
cached_timestamp_type= DRIZZLE_TIMESTAMP_DATE;
3167
cached_field_type= DRIZZLE_TYPE_NEWDATE;
3180
cached_timestamp_type= MYSQL_TIMESTAMP_DATE;
3181
cached_field_type= MYSQL_TYPE_DATE;
3168
3182
max_length= MAX_DATE_WIDTH * MY_CHARSET_BIN_MB_MAXLEN;
3170
3184
case TIME_ONLY:
3171
3185
case TIME_MICROSECOND:
3172
cached_timestamp_type= DRIZZLE_TIMESTAMP_TIME;
3173
cached_field_type= DRIZZLE_TYPE_TIME;
3186
cached_timestamp_type= MYSQL_TIMESTAMP_TIME;
3187
cached_field_type= MYSQL_TYPE_TIME;
3174
3188
max_length= MAX_TIME_WIDTH * MY_CHARSET_BIN_MB_MAXLEN;
3177
cached_timestamp_type= DRIZZLE_TIMESTAMP_DATETIME;
3178
cached_field_type= DRIZZLE_TYPE_DATETIME;
3191
cached_timestamp_type= MYSQL_TIMESTAMP_DATETIME;
3192
cached_field_type= MYSQL_TYPE_DATETIME;
3186
bool Item_func_str_to_date::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date)
3200
bool Item_func_str_to_date::get_date(MYSQL_TIME *ltime, uint fuzzy_date)
3188
3202
DATE_TIME_FORMAT date_time_format;
3189
3203
char val_buff[64], format_buff[64];
3196
3210
goto null_date;
3199
memset(ltime, 0, sizeof(*ltime));
3213
bzero((char*) ltime, sizeof(*ltime));
3200
3214
date_time_format.format.str= (char*) format->ptr();
3201
3215
date_time_format.format.length= format->length();
3202
3216
if (extract_date_time(&date_time_format, val->ptr(), val->length(),
3204
3218
((fuzzy_date & TIME_NO_ZERO_DATE) &&
3205
3219
(ltime->year == 0 || ltime->month == 0 || ltime->day == 0)))
3206
3220
goto null_date;
3207
if (cached_timestamp_type == DRIZZLE_TIMESTAMP_TIME && ltime->day)
3221
if (cached_timestamp_type == MYSQL_TIMESTAMP_TIME && ltime->day)
3210
3224
Day part for time type can be nonzero value and so
3240
bool Item_func_last_day::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date)
3254
bool Item_func_last_day::get_date(MYSQL_TIME *ltime, uint fuzzy_date)
3242
3256
if (get_arg0_date(ltime, fuzzy_date & ~TIME_FUZZY_DATE) ||
3243
3257
(ltime->month == 0))
3249
uint32_t month_idx= ltime->month-1;
3263
uint month_idx= ltime->month-1;
3250
3264
ltime->day= days_in_month[month_idx];
3251
3265
if ( month_idx == 1 && calc_days_in_year(ltime->year) == 366)
3252
3266
ltime->day= 29;
3253
3267
ltime->hour= ltime->minute= ltime->second= 0;
3254
3268
ltime->second_part= 0;
3255
ltime->time_type= DRIZZLE_TIMESTAMP_DATE;
3269
ltime->time_type= MYSQL_TIMESTAMP_DATE;