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));
207
212
ltime->second= TIME_MAX_SECOND;
210
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)
212
make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
213
buf, len, DRIZZLE_TIMESTAMP_TIME,
217
make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
218
buf, len, MYSQL_TIMESTAMP_TIME,
229
234
{(char *)"%H:%i:%S", 8}};
232
Extract datetime value to DRIZZLE_TIME struct from string value
237
Extract datetime value to MYSQL_TIME struct from string value
233
238
according to format string.
235
240
@param format date/time format specification
264
269
static bool extract_date_time(DATE_TIME_FORMAT *format,
265
const char *val, uint32_t length, DRIZZLE_TIME *l_time,
266
enum enum_drizzle_timestamp_type cached_timestamp_type,
270
const char *val, uint length, MYSQL_TIME *l_time,
271
timestamp_type cached_timestamp_type,
267
272
const char **sub_pattern_end,
268
273
const char *date_time_type)
280
285
const char *val_end= val + length;
281
286
const char *ptr= format->format.str;
282
287
const char *end= ptr + format->format.length;
283
const CHARSET_INFO * const cs= &my_charset_bin;
288
CHARSET_INFO *cs= &my_charset_bin;
289
DBUG_ENTER("extract_date_time");
285
291
if (!sub_pattern_end)
286
memset(l_time, 0, sizeof(*l_time));
292
bzero((char*) l_time, sizeof(*l_time));
288
294
for (; ptr != end && val != val_end; ptr++)
302
308
switch (*++ptr) {
305
tmp= (char*) val + cmin(4, val_len);
311
tmp= (char*) val + min(4, val_len);
306
312
l_time->year= (int) my_strtoll10(val, &tmp, &error);
307
313
if ((int) (tmp-val) <= 2)
308
314
l_time->year= year_2000_handling(l_time->year);
312
tmp= (char*) val + cmin(2, val_len);
318
tmp= (char*) val + min(2, val_len);
313
319
l_time->year= (int) my_strtoll10(val, &tmp, &error);
315
321
l_time->year= year_2000_handling(l_time->year);
321
tmp= (char*) val + cmin(2, val_len);
327
tmp= (char*) val + min(2, val_len);
322
328
l_time->month= (int) my_strtoll10(val, &tmp, &error);
338
tmp= (char*) val + cmin(2, val_len);
344
tmp= (char*) val + min(2, val_len);
339
345
l_time->day= (int) my_strtoll10(val, &tmp, &error);
343
tmp= (char*) val + cmin(2, val_len);
349
tmp= (char*) val + min(2, val_len);
344
350
l_time->day= (int) my_strtoll10(val, &tmp, &error);
345
351
/* Skip 'st, 'nd, 'th .. */
346
val= tmp + cmin((int) (val_end-tmp), 2);
352
val= tmp + min((int) (val_end-tmp), 2);
354
360
/* fall through */
357
tmp= (char*) val + cmin(2, val_len);
363
tmp= (char*) val + min(2, val_len);
358
364
l_time->hour= (int) my_strtoll10(val, &tmp, &error);
364
tmp= (char*) val + cmin(2, val_len);
370
tmp= (char*) val + min(2, val_len);
365
371
l_time->minute= (int) my_strtoll10(val, &tmp, &error);
372
tmp= (char*) val + cmin(2, val_len);
378
tmp= (char*) val + min(2, val_len);
373
379
l_time->second= (int) my_strtoll10(val, &tmp, &error);
391
397
if (val_len < 2 || ! usa_time)
393
if (!my_strnncoll(&my_charset_utf8_general_ci,
394
(const unsigned char *) val, 2,
395
(const unsigned char *) "PM", 2))
399
if (!my_strnncoll(&my_charset_latin1,
400
(const uchar *) val, 2,
401
(const uchar *) "PM", 2))
397
else if (my_strnncoll(&my_charset_utf8_general_ci,
398
(const unsigned char *) val, 2,
399
(const unsigned char *) "AM", 2))
403
else if (my_strnncoll(&my_charset_latin1,
404
(const uchar *) val, 2,
405
(const uchar *) "AM", 2))
424
tmp= (char*) val + cmin(val_len, 3);
430
tmp= (char*) val + min(val_len, 3);
425
431
yearday= (int) my_strtoll10(val, &tmp, &error);
434
440
sunday_first_n_first_week_non_iso= (*ptr=='U' || *ptr== 'V');
435
441
strict_week_number= (*ptr=='V' || *ptr=='v');
436
tmp= (char*) val + cmin(val_len, 2);
442
tmp= (char*) val + min(val_len, 2);
437
443
if ((week_number= (int) my_strtoll10(val, &tmp, &error)) < 0 ||
438
444
(strict_week_number && !week_number) ||
439
445
week_number > 53)
447
453
strict_week_number_year_type= (*ptr=='X');
448
tmp= (char*) val + cmin(4, val_len);
454
tmp= (char*) val + min(4, val_len);
449
455
strict_week_number_year= (int) my_strtoll10(val, &tmp, &error);
459
465
if (extract_date_time(&time_ampm_format, val,
460
466
(uint)(val_end - val), l_time,
461
467
cached_timestamp_type, &val, "time"))
465
471
/* Time in 24-hour notation */
467
473
if (extract_date_time(&time_24hrs_format, val,
468
474
(uint)(val_end - val), l_time,
469
475
cached_timestamp_type, &val, "time"))
473
479
/* Conversion specifiers that match classes of characters */
578
if (!my_isspace(&my_charset_utf8_general_ci,*val))
584
if (!my_isspace(&my_charset_latin1,*val))
580
make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
586
make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
581
587
val_begin, length,
582
cached_timestamp_type, NULL);
588
cached_timestamp_type, NullS);
585
591
} while (++val != val_end);
592
strmake(buff, val_begin, cmin(length, (uint)sizeof(buff)-1));
593
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,
594
600
ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
595
601
date_time_type, buff, "str_to_date");
602
608
Create a formated date/time value in a string.
605
bool make_date_time(DATE_TIME_FORMAT *format, DRIZZLE_TIME *l_time,
606
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)
608
614
char intbuff[15];
612
618
const char *ptr, *end;
613
619
THD *thd= current_thd;
659
665
system_charset_info);
662
if (type == DRIZZLE_TIMESTAMP_TIME)
668
if (type == MYSQL_TIMESTAMP_TIME)
664
670
length= int10_to_str(l_time->day, intbuff, 10) - intbuff;
665
671
str->append_with_prefill(intbuff, length, 1, '0');
726
732
str->append_with_prefill(intbuff, length, 2, '0');
729
if (type == DRIZZLE_TIMESTAMP_TIME)
735
if (type == MYSQL_TIMESTAMP_TIME)
731
737
length= int10_to_str(calc_daynr(l_time->year,l_time->month,
747
753
str->append(hours_i < 12 ? "AM" : "PM",2);
750
length= sprintf(intbuff,
756
length= my_sprintf(intbuff,
751
758
((l_time->hour % 24) < 12) ?
752
759
"%02d:%02d:%02d AM" : "%02d:%02d:%02d PM",
753
760
(l_time->hour+11)%12+1,
756
763
str->append(intbuff, length);
761
768
str->append_with_prefill(intbuff, length, 2, '0');
764
length= sprintf(intbuff,
771
length= my_sprintf(intbuff,
765
773
"%02d:%02d:%02d",
769
777
str->append(intbuff, length);
775
if (type == DRIZZLE_TIMESTAMP_TIME)
783
if (type == MYSQL_TIMESTAMP_TIME)
777
785
length= int10_to_str(calc_week(l_time,
850
858
For example, '1.1' -> '1.100000'
853
static bool get_interval_info(const char *str,uint32_t length, const CHARSET_INFO * const cs,
854
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,
855
863
bool transform_msec)
857
865
const char *end=str+length;
859
867
while (str != end && !my_isdigit(cs,*str))
862
870
for (i=0 ; i < count ; i++)
865
873
const char *start= str;
866
874
for (value=0; str != end && my_isdigit(cs,*str) ; str++)
867
value= value * 10L + (int64_t) (*str - '0');
875
value= value*LL(10) + (longlong) (*str - '0');
868
876
if (transform_msec && i == count - 1) // microseconds always last
870
878
long msec_length= 6 - (str - start);
880
888
/* Change values[0...i-1] -> values[0...count-1] */
881
bmove_upp((unsigned char*) (values+count), (unsigned char*) (values+i),
889
bmove_upp((uchar*) (values+count), (uchar*) (values+i),
882
890
sizeof(*values)*i);
883
memset(values, 0, sizeof(*values)*(count-i));
891
bzero((uchar*) values, sizeof(*values)*(count-i));
891
int64_t Item_func_period_add::val_int()
899
longlong Item_func_period_add::val_int()
901
DBUG_ASSERT(fixed == 1);
894
902
ulong period=(ulong) args[0]->val_int();
895
903
int months=(int) args[1]->val_int();
897
905
if ((null_value=args[0]->null_value || args[1]->null_value) ||
899
907
return 0; /* purecov: inspected */
901
909
convert_month_to_period((uint) ((int) convert_period_to_month(period)+
906
int64_t Item_func_period_diff::val_int()
914
longlong Item_func_period_diff::val_int()
916
DBUG_ASSERT(fixed == 1);
909
917
ulong period1=(ulong) args[0]->val_int();
910
918
ulong period2=(ulong) args[1]->val_int();
912
920
if ((null_value=args[0]->null_value || args[1]->null_value))
913
921
return 0; /* purecov: inspected */
914
return (int64_t) ((long) convert_period_to_month(period1)-
922
return (longlong) ((long) convert_period_to_month(period1)-
915
923
(long) convert_period_to_month(period2));
920
int64_t Item_func_to_days::val_int()
928
longlong Item_func_to_days::val_int()
930
DBUG_ASSERT(fixed == 1);
924
932
if (get_arg0_date(<ime, TIME_NO_ZERO_DATE))
926
return (int64_t) calc_daynr(ltime.year,ltime.month,ltime.day);
934
return (longlong) calc_daynr(ltime.year,ltime.month,ltime.day);
946
954
if (args[0]->type() == Item::FIELD_ITEM)
948
if (args[0]->field_type() == DRIZZLE_TYPE_NEWDATE)
956
if (args[0]->field_type() == MYSQL_TYPE_DATE)
949
957
return MONOTONIC_STRICT_INCREASING;
950
if (args[0]->field_type() == DRIZZLE_TYPE_DATETIME)
958
if (args[0]->field_type() == MYSQL_TYPE_DATETIME)
951
959
return MONOTONIC_INCREASING;
953
961
return NON_MONOTONIC;
957
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);
962
970
if (get_arg0_date(<ime, TIME_NO_ZERO_DATE))
964
972
/* got NULL, leave the incl_endp intact */
967
res=(int64_t) calc_daynr(ltime.year,ltime.month,ltime.day);
975
res=(longlong) calc_daynr(ltime.year,ltime.month,ltime.day);
969
if (args[0]->field_type() == DRIZZLE_TYPE_NEWDATE)
977
if (args[0]->field_type() == MYSQL_TYPE_DATE)
971
979
// TO_DAYS() is strictly monotonic for dates, leave incl_endp intact
987
995
ltime.second_part))
988
996
; /* do nothing */
995
int64_t Item_func_dayofyear::val_int()
1003
longlong Item_func_dayofyear::val_int()
1005
DBUG_ASSERT(fixed == 1);
999
1007
if (get_arg0_date(<ime,TIME_NO_ZERO_DATE))
1001
return (int64_t) calc_daynr(ltime.year,ltime.month,ltime.day) -
1009
return (longlong) calc_daynr(ltime.year,ltime.month,ltime.day) -
1002
1010
calc_daynr(ltime.year,1,1) + 1;
1005
int64_t Item_func_dayofmonth::val_int()
1013
longlong Item_func_dayofmonth::val_int()
1015
DBUG_ASSERT(fixed == 1);
1009
1017
(void) get_arg0_date(<ime, TIME_FUZZY_DATE);
1010
return (int64_t) ltime.day;
1018
return (longlong) ltime.day;
1013
int64_t Item_func_month::val_int()
1021
longlong Item_func_month::val_int()
1023
DBUG_ASSERT(fixed == 1);
1017
1025
(void) get_arg0_date(<ime, TIME_FUZZY_DATE);
1018
return (int64_t) ltime.month;
1026
return (longlong) ltime.month;
1022
1030
String* Item_func_monthname::val_str(String* str)
1032
DBUG_ASSERT(fixed == 1);
1025
1033
const char *month_name;
1026
uint32_t month= (uint) val_int();
1034
uint month= (uint) val_int();
1027
1035
THD *thd= current_thd;
1029
1037
if (null_value || !month)
1042
1050
Returns the quarter of the year.
1045
int64_t Item_func_quarter::val_int()
1053
longlong Item_func_quarter::val_int()
1055
DBUG_ASSERT(fixed == 1);
1049
1057
if (get_arg0_date(<ime, TIME_FUZZY_DATE))
1051
return (int64_t) ((ltime.month+2)/3);
1059
return (longlong) ((ltime.month+2)/3);
1054
int64_t Item_func_hour::val_int()
1062
longlong Item_func_hour::val_int()
1064
DBUG_ASSERT(fixed == 1);
1058
1066
(void) get_arg0_time(<ime);
1059
1067
return ltime.hour;
1062
int64_t Item_func_minute::val_int()
1070
longlong Item_func_minute::val_int()
1072
DBUG_ASSERT(fixed == 1);
1066
1074
(void) get_arg0_time(<ime);
1067
1075
return ltime.minute;
1071
1079
Returns the second in time_exp in the range of 0 - 59.
1073
int64_t Item_func_second::val_int()
1081
longlong Item_func_second::val_int()
1083
DBUG_ASSERT(fixed == 1);
1077
1085
(void) get_arg0_time(<ime);
1078
1086
return ltime.second;
1082
uint32_t week_mode(uint32_t mode)
1090
uint week_mode(uint mode)
1084
uint32_t week_format= (mode & 7);
1092
uint week_format= (mode & 7);
1085
1093
if (!(week_format & WEEK_MONDAY_FIRST))
1086
1094
week_format^= WEEK_FIRST_WEEKDAY;
1087
1095
return week_format;
1121
int64_t Item_func_week::val_int()
1129
longlong Item_func_week::val_int()
1131
DBUG_ASSERT(fixed == 1);
1126
1134
if (get_arg0_date(<ime, TIME_NO_ZERO_DATE))
1128
return (int64_t) calc_week(<ime,
1136
return (longlong) calc_week(<ime,
1129
1137
week_mode((uint) args[1]->val_int()),
1134
int64_t Item_func_yearweek::val_int()
1142
longlong Item_func_yearweek::val_int()
1144
DBUG_ASSERT(fixed == 1);
1139
1147
if (get_arg0_date(<ime, TIME_NO_ZERO_DATE))
1141
1149
week= calc_week(<ime,
1148
int64_t Item_func_weekday::val_int()
1156
longlong Item_func_weekday::val_int()
1158
DBUG_ASSERT(fixed == 1);
1153
1161
if (get_arg0_date(<ime, TIME_NO_ZERO_DATE))
1156
return (int64_t) calc_weekday(calc_daynr(ltime.year, ltime.month,
1164
return (longlong) calc_weekday(calc_daynr(ltime.year, ltime.month,
1158
1166
odbc_type) + test(odbc_type);
1178
int64_t Item_func_year::val_int()
1186
longlong Item_func_year::val_int()
1188
DBUG_ASSERT(fixed == 1);
1182
1190
(void) get_arg0_date(<ime, TIME_FUZZY_DATE);
1183
return (int64_t) ltime.year;
1191
return (longlong) ltime.year;
1201
1209
enum_monotonicity_info Item_func_year::get_monotonicity_info() const
1203
1211
if (args[0]->type() == Item::FIELD_ITEM &&
1204
(args[0]->field_type() == DRIZZLE_TYPE_NEWDATE ||
1205
args[0]->field_type() == DRIZZLE_TYPE_DATETIME))
1212
(args[0]->field_type() == MYSQL_TYPE_DATE ||
1213
args[0]->field_type() == MYSQL_TYPE_DATETIME))
1206
1214
return MONOTONIC_INCREASING;
1207
1215
return NON_MONOTONIC;
1211
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);
1215
1223
if (get_arg0_date(<ime, TIME_FUZZY_DATE))
1217
1225
/* got NULL, leave the incl_endp intact */
1226
return LONGLONG_MIN;
1233
1241
!(ltime.hour || ltime.minute || ltime.second || ltime.second_part))
1234
1242
; /* do nothing */
1237
1245
return ltime.year;
1241
int64_t Item_func_unix_timestamp::val_int()
1249
longlong Item_func_unix_timestamp::val_int()
1254
DBUG_ASSERT(fixed == 1);
1247
1255
if (arg_count == 0)
1248
return (int64_t) current_thd->query_start();
1256
return (longlong) current_thd->query_start();
1249
1257
if (args[0]->type() == FIELD_ITEM)
1250
1258
{ // Optimize timestamp field
1251
1259
Field *field=((Item_field*) args[0])->field;
1252
if (field->type() == DRIZZLE_TYPE_TIMESTAMP)
1260
if (field->type() == MYSQL_TYPE_TIMESTAMP)
1253
1261
return ((Field_timestamp*) field)->get_timestamp(&null_value);
1267
return (int64_t) TIME_to_timestamp(current_thd, <ime, ¬_used);
1275
return (longlong) TIME_to_timestamp(current_thd, <ime, ¬_used);
1271
int64_t Item_func_time_to_sec::val_int()
1279
longlong Item_func_time_to_sec::val_int()
1281
DBUG_ASSERT(fixed == 1);
1276
1284
(void) get_arg0_time(<ime);
1277
1285
seconds=ltime.hour*3600L+ltime.minute*60+ltime.second;
1278
1286
return ltime.neg ? -seconds : seconds;
1288
1296
bool get_interval_value(Item *args,interval_type int_type,
1289
1297
String *str_value, INTERVAL *interval)
1293
1301
const char *str= NULL;
1294
1302
size_t length= 0;
1295
const CHARSET_INFO * const cs= str_value->charset();
1303
CHARSET_INFO *cs=str_value->charset();
1297
memset(interval, 0, sizeof(*interval));
1305
bzero((char*) interval,sizeof(*interval));
1298
1306
if ((int) int_type <= INTERVAL_MICROSECOND)
1300
1308
value= args->val_int();
1456
int64_t Item_date::val_int()
1464
longlong Item_date::val_int()
1466
DBUG_ASSERT(fixed == 1);
1460
1468
if (get_date(<ime, TIME_FUZZY_DATE))
1462
return (int64_t) (ltime.year*10000L+ltime.month*100+ltime.day);
1470
return (longlong) (ltime.year*10000L+ltime.month*100+ltime.day);
1466
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)
1468
int64_t value=args[0]->val_int();
1476
longlong value=args[0]->val_int();
1469
1477
if ((null_value=args[0]->null_value))
1471
memset(ltime, 0, sizeof(DRIZZLE_TIME));
1479
bzero(ltime, sizeof(MYSQL_TIME));
1472
1480
get_date_from_daynr((long) value, <ime->year, <ime->month, <ime->day);
1473
ltime->time_type= DRIZZLE_TIMESTAMP_DATE;
1481
ltime->time_type= MYSQL_TIMESTAMP_DATE;
1486
1494
/* We don't need to set second_part and neg because they already 0 */
1487
1495
ltime.hour= ltime.minute= ltime.second= 0;
1488
ltime.time_type= DRIZZLE_TIMESTAMP_DATE;
1489
value= (int64_t) TIME_to_uint64_t_date(<ime);
1496
ltime.time_type= MYSQL_TIMESTAMP_DATE;
1497
value= (longlong) TIME_to_ulonglong_date(<ime);
1492
1500
String *Item_func_curdate::val_str(String *str)
1502
DBUG_ASSERT(fixed == 1);
1495
1503
if (str->alloc(MAX_DATE_STRING_REP_LENGTH))
1505
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
1506
1514
time zone. Defines time zone (local) used for whole CURDATE function.
1508
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)
1510
1518
THD *thd= current_thd;
1511
1519
thd->variables.time_zone->gmt_sec_to_TIME(now_time,
1518
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
1519
1527
time zone. Defines time zone (UTC) used for whole UTC_DATE function.
1521
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)
1523
1531
my_tz_UTC->gmt_sec_to_TIME(now_time,
1524
1532
(my_time_t)(current_thd->query_start()));
1532
bool Item_func_curdate::get_date(DRIZZLE_TIME *res,
1533
uint32_t fuzzy_date __attribute__((unused)))
1540
bool Item_func_curdate::get_date(MYSQL_TIME *res,
1541
uint fuzzy_date __attribute__((unused)))
1540
String *Item_func_curtime::val_str(String *str __attribute__((unused)))
1548
String *Item_func_curtime::val_str(String *str)
1550
DBUG_ASSERT(fixed == 1);
1543
1551
str_value.set(buff, buff_length, &my_charset_bin);
1544
1552
return &str_value;
1548
1556
void Item_func_curtime::fix_length_and_dec()
1552
1560
decimals= DATETIME_DEC;
1553
1561
collation.set(&my_charset_bin);
1554
1562
store_now_in_TIME(<ime);
1555
value= TIME_to_uint64_t_time(<ime);
1563
value= TIME_to_ulonglong_time(<ime);
1556
1564
buff_length= (uint) my_time_to_str(<ime, buff);
1557
1565
max_length= buff_length;
1562
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
1563
1571
time zone. Defines time zone (local) used for whole CURTIME function.
1565
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)
1567
1575
THD *thd= current_thd;
1568
1576
thd->variables.time_zone->gmt_sec_to_TIME(now_time,
1575
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
1576
1584
time zone. Defines time zone (UTC) used for whole UTC_TIME function.
1578
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)
1580
1588
my_tz_UTC->gmt_sec_to_TIME(now_time,
1581
1589
(my_time_t)(current_thd->query_start()));
1600
1608
collation.set(&my_charset_bin);
1602
1610
store_now_in_TIME(<ime);
1603
value= (int64_t) TIME_to_uint64_t_datetime(<ime);
1611
value= (longlong) TIME_to_ulonglong_datetime(<ime);
1605
1613
buff_length= (uint) my_datetime_to_str(<ime, buff);
1606
1614
max_length= buff_length;
1611
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
1612
1620
time zone. Defines time zone (local) used for whole NOW function.
1614
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)
1616
1624
THD *thd= current_thd;
1617
1625
thd->variables.time_zone->gmt_sec_to_TIME(now_time,
1624
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
1625
1633
time zone. Defines time zone (UTC) used for whole UTC_TIMESTAMP function.
1627
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)
1629
1637
my_tz_UTC->gmt_sec_to_TIME(now_time,
1630
1638
(my_time_t)(current_thd->query_start()));
1638
bool Item_func_now::get_date(DRIZZLE_TIME *res,
1639
uint32_t fuzzy_date __attribute__((unused)))
1646
bool Item_func_now::get_date(MYSQL_TIME *res,
1647
uint fuzzy_date __attribute__((unused)))
1646
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)
1648
1656
to->set_notnull();
1649
return to->store_time(<ime, DRIZZLE_TIMESTAMP_DATETIME);
1657
return to->store_time(<ime, MYSQL_TIMESTAMP_DATETIME);
1654
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
1655
1663
time zone. Defines time zone (local) used for whole SYSDATE function.
1657
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)
1659
1667
THD *thd= current_thd;
1660
1668
thd->variables.time_zone->gmt_sec_to_TIME(now_time, (my_time_t) my_time(0));
1665
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);
1668
1676
store_now_in_TIME(<ime);
1669
1677
buff_length= (uint) my_datetime_to_str(<ime, buff);
1670
1678
str_value.set(buff, buff_length, &my_charset_bin);
1675
int64_t Item_func_sysdate_local::val_int()
1683
longlong Item_func_sysdate_local::val_int()
1685
DBUG_ASSERT(fixed == 1);
1678
1686
store_now_in_TIME(<ime);
1679
return (int64_t) TIME_to_uint64_t_datetime(<ime);
1687
return (longlong) TIME_to_ulonglong_datetime(<ime);
1683
1691
double Item_func_sysdate_local::val_real()
1693
DBUG_ASSERT(fixed == 1);
1686
1694
store_now_in_TIME(<ime);
1687
return uint64_t2double(TIME_to_uint64_t_datetime(<ime));
1695
return ulonglong2double(TIME_to_ulonglong_datetime(<ime));
1699
bool Item_func_sysdate_local::get_date(DRIZZLE_TIME *res,
1700
uint32_t fuzzy_date __attribute__((unused)))
1707
bool Item_func_sysdate_local::get_date(MYSQL_TIME *res,
1708
uint fuzzy_date __attribute__((unused)))
1702
1710
store_now_in_TIME(<ime);
1708
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)
1710
1718
store_now_in_TIME(<ime);
1711
1719
to->set_notnull();
1712
to->store_time(<ime, DRIZZLE_TIMESTAMP_DATETIME);
1720
to->store_time(<ime, MYSQL_TIMESTAMP_DATETIME);
1717
1725
String *Item_func_sec_to_time::val_str(String *str)
1721
int64_t arg_val= args[0]->val_int();
1727
DBUG_ASSERT(fixed == 1);
1729
longlong arg_val= args[0]->val_int();
1723
1731
if ((null_value=args[0]->null_value) ||
1724
1732
str->alloc(MAX_DATE_STRING_REP_LENGTH))
1737
int64_t Item_func_sec_to_time::val_int()
1745
longlong Item_func_sec_to_time::val_int()
1741
int64_t arg_val= args[0]->val_int();
1747
DBUG_ASSERT(fixed == 1);
1749
longlong arg_val= args[0]->val_int();
1743
1751
if ((null_value=args[0]->null_value))
1760
1768
Item *arg1= args[1]->this_item();
1763
const CHARSET_INFO * const cs= thd->variables.collation_connection;
1764
uint32_t repertoire= arg1->collation.repertoire;
1771
CHARSET_INFO *cs= thd->variables.collation_connection;
1772
uint32 repertoire= arg1->collation.repertoire;
1765
1773
if (!thd->variables.lc_time_names->is_ascii)
1766
1774
repertoire|= MY_REPERTOIRE_EXTENDED;
1767
1775
collation.set(cs, arg1->collation.derivation, repertoire);
1776
1784
fixed_length=0;
1777
max_length=cmin(arg1->max_length,(uint32_t) MAX_BLOB_WIDTH) * 10 *
1785
max_length=min(arg1->max_length, MAX_BLOB_WIDTH) * 10 *
1778
1786
collation.collation->mbmaxlen;
1779
1787
set_if_smaller(max_length,MAX_BLOB_WIDTH);
1926
1934
/* Create the result string */
1927
1935
str->set_charset(collation.collation);
1928
1936
if (!make_date_time(&date_time_format, &l_time,
1929
is_time_format ? DRIZZLE_TIMESTAMP_TIME :
1930
DRIZZLE_TIMESTAMP_DATE,
1937
is_time_format ? MYSQL_TIMESTAMP_TIME :
1938
MYSQL_TIMESTAMP_DATE,
1972
int64_t Item_func_from_unixtime::val_int()
1980
longlong Item_func_from_unixtime::val_int()
1974
DRIZZLE_TIME time_tmp;
1982
MYSQL_TIME time_tmp;
1984
DBUG_ASSERT(fixed == 1);
1978
1986
if (get_date(&time_tmp, 0))
1981
return (int64_t) TIME_to_uint64_t_datetime(&time_tmp);
1989
return (longlong) TIME_to_ulonglong_datetime(&time_tmp);
1984
bool Item_func_from_unixtime::get_date(DRIZZLE_TIME *ltime,
1985
uint32_t fuzzy_date __attribute__((unused)))
1992
bool Item_func_from_unixtime::get_date(MYSQL_TIME *ltime,
1993
uint fuzzy_date __attribute__((unused)))
1987
uint64_t tmp= (uint64_t)(args[0]->val_int());
1995
ulonglong tmp= (ulonglong)(args[0]->val_int());
1989
1997
"tmp > TIMESTAMP_MAX_VALUE" check also covers case of negative
1990
1998
from_unixtime() argument since tmp is unsigned.
2011
2019
The field type for the result of an Item_date function is defined as
2014
- If first arg is a DRIZZLE_TYPE_DATETIME result is DRIZZLE_TYPE_DATETIME
2015
- If first arg is a DRIZZLE_TYPE_NEWDATE and the interval type uses hours,
2016
minutes or seconds then type is DRIZZLE_TYPE_DATETIME.
2017
- Otherwise the result is DRIZZLE_TYPE_VARCHAR
2018
(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
2019
2027
DATETIME argument)
2021
cached_field_type= DRIZZLE_TYPE_VARCHAR;
2029
cached_field_type= MYSQL_TYPE_STRING;
2022
2030
arg0_field_type= args[0]->field_type();
2023
if (arg0_field_type == DRIZZLE_TYPE_DATETIME ||
2024
arg0_field_type == DRIZZLE_TYPE_TIMESTAMP)
2025
cached_field_type= DRIZZLE_TYPE_DATETIME;
2026
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)
2028
2036
if (int_type <= INTERVAL_DAY || int_type == INTERVAL_YEAR_MONTH)
2029
2037
cached_field_type= arg0_field_type;
2031
cached_field_type= DRIZZLE_TYPE_DATETIME;
2039
cached_field_type= MYSQL_TYPE_DATETIME;
2036
2044
/* Here arg[1] is a Item_interval object */
2038
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)
2040
2048
INTERVAL interval;
2055
2063
String *Item_date_add_interval::val_str(String *str)
2065
DBUG_ASSERT(fixed == 1);
2059
2067
enum date_time_format_types format;
2061
2069
if (Item_date_add_interval::get_date(<ime, TIME_NO_ZERO_DATE))
2064
if (ltime.time_type == DRIZZLE_TIMESTAMP_DATE)
2072
if (ltime.time_type == MYSQL_TIMESTAMP_DATE)
2065
2073
format= DATE_ONLY;
2066
2074
else if (ltime.second_part)
2067
2075
format= DATE_TIME_MICROSECOND;
2079
int64_t Item_date_add_interval::val_int()
2087
longlong Item_date_add_interval::val_int()
2089
DBUG_ASSERT(fixed == 1);
2084
2092
if (Item_date_add_interval::get_date(<ime, TIME_NO_ZERO_DATE))
2093
return (longlong) 0;
2086
2094
date = (ltime.year*100L + ltime.month)*100L + ltime.day;
2087
return ltime.time_type == DRIZZLE_TIMESTAMP_DATE ? date :
2095
return ltime.time_type == MYSQL_TIMESTAMP_DATE ? date :
2088
2096
((date*100L + ltime.hour)*100L+ ltime.minute)*100L + ltime.second;
2161
2169
case INTERVAL_HOUR_MICROSECOND: max_length=13; date_value=0; break;
2162
2170
case INTERVAL_MINUTE_MICROSECOND: max_length=11; date_value=0; break;
2163
2171
case INTERVAL_SECOND_MICROSECOND: max_length=9; date_value=0; break;
2164
case INTERVAL_LAST: assert(0); break; /* purecov: deadcode */
2172
case INTERVAL_LAST: DBUG_ASSERT(0); break; /* purecov: deadcode */
2169
int64_t Item_extract::val_int()
2177
longlong Item_extract::val_int()
2179
DBUG_ASSERT(fixed == 1);
2174
2182
ulong week_format;
2176
2184
if (date_value)
2205
2213
case INTERVAL_DAY_MINUTE: return (long) (ltime.day*10000L+
2206
2214
ltime.hour*100L+
2207
2215
ltime.minute)*neg;
2208
case INTERVAL_DAY_SECOND: return ((int64_t) ltime.day*1000000L+
2209
(int64_t) (ltime.hour*10000L+
2216
case INTERVAL_DAY_SECOND: return ((longlong) ltime.day*1000000L+
2217
(longlong) (ltime.hour*10000L+
2210
2218
ltime.minute*100+
2211
2219
ltime.second))*neg;
2212
2220
case INTERVAL_HOUR: return (long) ltime.hour*neg;
2217
2225
case INTERVAL_MINUTE_SECOND: return (long) (ltime.minute*100+ltime.second)*neg;
2218
2226
case INTERVAL_SECOND: return (long) ltime.second*neg;
2219
2227
case INTERVAL_MICROSECOND: return (long) ltime.second_part*neg;
2220
case INTERVAL_DAY_MICROSECOND: return (((int64_t)ltime.day*1000000L +
2221
(int64_t)ltime.hour*10000L +
2228
case INTERVAL_DAY_MICROSECOND: return (((longlong)ltime.day*1000000L +
2229
(longlong)ltime.hour*10000L +
2222
2230
ltime.minute*100 +
2223
2231
ltime.second)*1000000L +
2224
2232
ltime.second_part)*neg;
2225
case INTERVAL_HOUR_MICROSECOND: return (((int64_t)ltime.hour*10000L +
2233
case INTERVAL_HOUR_MICROSECOND: return (((longlong)ltime.hour*10000L +
2226
2234
ltime.minute*100 +
2227
2235
ltime.second)*1000000L +
2228
2236
ltime.second_part)*neg;
2229
case INTERVAL_MINUTE_MICROSECOND: return (((int64_t)(ltime.minute*100+
2237
case INTERVAL_MINUTE_MICROSECOND: return (((longlong)(ltime.minute*100+
2230
2238
ltime.second))*1000000L+
2231
2239
ltime.second_part)*neg;
2232
case INTERVAL_SECOND_MICROSECOND: return ((int64_t)ltime.second*1000000L+
2240
case INTERVAL_SECOND_MICROSECOND: return ((longlong)ltime.second*1000000L+
2233
2241
ltime.second_part)*neg;
2234
case INTERVAL_LAST: assert(0); break; /* purecov: deadcode */
2242
case INTERVAL_LAST: DBUG_ASSERT(0); break; /* purecov: deadcode */
2236
2244
return 0; // Impossible
2293
2301
char buffer[20];
2294
2302
// my_charset_bin is good enough for numbers
2295
2303
String st(buffer, sizeof(buffer), &my_charset_bin);
2296
st.set((uint64_t)cast_length, &my_charset_bin);
2304
st.set((ulonglong)cast_length, &my_charset_bin);
2297
2305
str->append(st);
2298
2306
str->append(')');
2324
2332
// Convert character set if differ
2325
uint32_t dummy_errors;
2326
2334
if (!(res= args[0]->val_str(&tmp_value)) ||
2327
2335
str->copy(res->ptr(), res->length(), from_cs,
2328
2336
cast_cs, &dummy_errors))
2343
2351
if (cast_length >= 0)
2345
if (res->length() > (length= (uint32_t) res->charpos(cast_length)))
2353
if (res->length() > (length= (uint32) res->charpos(cast_length)))
2346
2354
{ // Safe even if const arg
2347
2355
char char_type[40];
2348
snprintf(char_type, sizeof(char_type), "%s(%lu)",
2349
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",
2352
2360
if (!res->alloced_length())
2353
2361
{ // Don't change const str
2354
2362
str_value= *res; // Not malloced string
2355
2363
res= &str_value;
2357
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2365
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2358
2366
ER_TRUNCATED_WRONG_VALUE,
2359
2367
ER(ER_TRUNCATED_WRONG_VALUE), char_type,
2360
2368
res->c_ptr_safe());
2368
2376
str->copy(*res);
2371
memset(res->ptr() + res->length(), 0,
2372
(uint) cast_length - res->length());
2379
bzero((char*) res->ptr() + res->length(),
2380
(uint) cast_length - res->length());
2373
2381
res->length(cast_length);
2406
2414
from_cs= (args[0]->result_type() == INT_RESULT ||
2407
2415
args[0]->result_type() == DECIMAL_RESULT ||
2408
2416
args[0]->result_type() == REAL_RESULT) ?
2409
(cast_cs->mbminlen == 1 ? cast_cs : &my_charset_utf8_general_ci) :
2417
(cast_cs->mbminlen == 1 ? cast_cs : &my_charset_latin1) :
2410
2418
args[0]->collation.collation;
2411
2419
charset_conversion= (cast_cs->mbmaxlen > 1) ||
2412
2420
(!my_charset_same(from_cs, cast_cs) && from_cs != &my_charset_bin && cast_cs != &my_charset_bin);
2435
int64_t Item_datetime_typecast::val_int()
2443
longlong Item_datetime_typecast::val_int()
2445
DBUG_ASSERT(fixed == 1);
2439
2447
if (get_arg0_date(<ime,1))
2445
return TIME_to_uint64_t_datetime(<ime);
2453
return TIME_to_ulonglong_datetime(<ime);
2449
bool Item_time_typecast::get_time(DRIZZLE_TIME *ltime)
2457
bool Item_time_typecast::get_time(MYSQL_TIME *ltime)
2451
2459
bool res= get_arg0_time(ltime);
2453
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,
2454
2462
which we should not lose.
2456
if (ltime->time_type == DRIZZLE_TIMESTAMP_DATETIME)
2464
if (ltime->time_type == MYSQL_TIMESTAMP_DATETIME)
2457
2465
ltime->year= ltime->month= ltime->day= 0;
2458
ltime->time_type= DRIZZLE_TIMESTAMP_TIME;
2466
ltime->time_type= MYSQL_TIMESTAMP_TIME;
2463
int64_t Item_time_typecast::val_int()
2471
longlong Item_time_typecast::val_int()
2466
2474
if (get_time(<ime))
2489
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)
2491
2499
bool res= get_arg0_date(ltime, TIME_FUZZY_DATE);
2492
2500
ltime->hour= ltime->minute= ltime->second= ltime->second_part= 0;
2493
ltime->time_type= DRIZZLE_TIMESTAMP_DATE;
2501
ltime->time_type= MYSQL_TIMESTAMP_DATE;
2498
bool Item_date_typecast::get_time(DRIZZLE_TIME *ltime)
2506
bool Item_date_typecast::get_time(MYSQL_TIME *ltime)
2500
memset(ltime, 0, sizeof(DRIZZLE_TIME));
2508
bzero((char *)ltime, sizeof(MYSQL_TIME));
2501
2509
return args[0]->null_value;
2505
2513
String *Item_date_typecast::val_str(String *str)
2515
DBUG_ASSERT(fixed == 1);
2510
2518
if (!get_arg0_date(<ime, TIME_FUZZY_DATE) &&
2511
2519
!str->alloc(MAX_DATE_STRING_REP_LENGTH))
2521
int64_t Item_date_typecast::val_int()
2529
longlong Item_date_typecast::val_int()
2531
DBUG_ASSERT(fixed == 1);
2525
2533
if ((null_value= args[0]->get_date(<ime, TIME_FUZZY_DATE)))
2527
return (int64_t) (ltime.year * 10000L + ltime.month * 100 + ltime.day);
2535
return (longlong) (ltime.year * 10000L + ltime.month * 100 + ltime.day);
2580
2588
for dates between 0000-01-01 and 0099-12-31
2583
int64_t Item_func_makedate::val_int()
2591
longlong Item_func_makedate::val_int()
2586
DRIZZLE_TIME l_time;
2593
DBUG_ASSERT(fixed == 1);
2587
2595
long daynr= (long) args[1]->val_int();
2588
2596
long year= (long) args[0]->val_int();
2603
2611
get_date_from_daynr(days,&l_time.year,&l_time.month,&l_time.day);
2604
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);
2621
2629
The field type for the result of an Item_func_add_time function is defined
2624
- If first arg is a DRIZZLE_TYPE_DATETIME or DRIZZLE_TYPE_TIMESTAMP
2625
result is DRIZZLE_TYPE_DATETIME
2626
- If first arg is a DRIZZLE_TYPE_TIME result is DRIZZLE_TYPE_TIME
2627
- 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
2630
cached_field_type= DRIZZLE_TYPE_VARCHAR;
2638
cached_field_type= MYSQL_TYPE_STRING;
2631
2639
arg0_field_type= args[0]->field_type();
2632
if (arg0_field_type == DRIZZLE_TYPE_NEWDATE ||
2633
arg0_field_type == DRIZZLE_TYPE_DATETIME ||
2634
arg0_field_type == DRIZZLE_TYPE_TIMESTAMP)
2635
cached_field_type= DRIZZLE_TYPE_DATETIME;
2636
else if (arg0_field_type == DRIZZLE_TYPE_TIME)
2637
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;
2650
2658
String *Item_func_add_time::val_str(String *str)
2653
DRIZZLE_TIME l_time1, l_time2, l_time3;
2660
DBUG_ASSERT(fixed == 1);
2661
MYSQL_TIME l_time1, l_time2, l_time3;
2654
2662
bool is_time= 0;
2655
2663
long days, microseconds;
2657
2665
int l_sign= sign;
2662
2670
if (get_arg0_date(&l_time1, TIME_FUZZY_DATE) ||
2663
2671
args[1]->get_time(&l_time2) ||
2664
l_time1.time_type == DRIZZLE_TIMESTAMP_TIME ||
2665
l_time2.time_type != DRIZZLE_TIMESTAMP_TIME)
2672
l_time1.time_type == MYSQL_TIMESTAMP_TIME ||
2673
l_time2.time_type != MYSQL_TIMESTAMP_TIME)
2666
2674
goto null_date;
2668
2676
else // ADDTIME function
2670
2678
if (args[0]->get_time(&l_time1) ||
2671
2679
args[1]->get_time(&l_time2) ||
2672
l_time2.time_type == DRIZZLE_TIMESTAMP_DATETIME)
2680
l_time2.time_type == MYSQL_TIMESTAMP_DATETIME)
2673
2681
goto null_date;
2674
is_time= (l_time1.time_type == DRIZZLE_TIMESTAMP_TIME);
2682
is_time= (l_time1.time_type == MYSQL_TIMESTAMP_TIME);
2676
2684
if (l_time1.neg != l_time2.neg)
2677
2685
l_sign= -l_sign;
2679
memset(&l_time3, 0, sizeof(l_time3));
2687
bzero((char *)&l_time3, sizeof(l_time3));
2681
2689
l_time3.neg= calc_time_diff(&l_time1, &l_time2, -l_sign,
2682
2690
&seconds, µseconds);
2750
2758
String *Item_func_timediff::val_str(String *str)
2760
DBUG_ASSERT(fixed == 1);
2754
2762
long microseconds;
2756
DRIZZLE_TIME l_time1 ,l_time2, l_time3;
2764
MYSQL_TIME l_time1 ,l_time2, l_time3;
2759
2767
if (args[0]->get_time(&l_time1) ||
2764
2772
if (l_time1.neg != l_time2.neg)
2765
2773
l_sign= -l_sign;
2767
memset(&l_time3, 0, sizeof(l_time3));
2775
bzero((char *)&l_time3, sizeof(l_time3));
2769
2777
l_time3.neg= calc_time_diff(&l_time1, &l_time2, l_sign,
2770
2778
&seconds, µseconds);
2773
For DRIZZLE_TIMESTAMP_TIME only:
2781
For MYSQL_TIMESTAMP_TIME only:
2774
2782
If first argument was negative and diff between arguments
2775
2783
is non-zero we need to swap sign to get proper result.
2798
2806
String *Item_func_maketime::val_str(String *str)
2808
DBUG_ASSERT(fixed == 1);
2802
2810
bool overflow= 0;
2804
int64_t hour= args[0]->val_int();
2805
int64_t minute= args[1]->val_int();
2806
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();
2808
2816
if ((null_value=(args[0]->null_value ||
2809
2817
args[1]->null_value ||
2813
2821
str->alloc(MAX_DATE_STRING_REP_LENGTH))))
2816
memset(<ime, 0, sizeof(ltime));
2824
bzero((char *)<ime, sizeof(ltime));
2819
2827
/* Check for integer overflows */
2839
2847
ltime.minute= TIME_MAX_MINUTE;
2840
2848
ltime.second= TIME_MAX_SECOND;
2842
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);
2843
2851
int len = (int)(ptr - buf) +
2844
sprintf(ptr, ":%02u:%02u", (uint)minute, (uint)second);
2845
make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2846
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,
2850
2858
if (make_time_with_warn((DATE_TIME_FORMAT *) 0, <ime, str))
2864
2872
Result: int value
2867
int64_t Item_func_microsecond::val_int()
2875
longlong Item_func_microsecond::val_int()
2877
DBUG_ASSERT(fixed == 1);
2871
2879
if (!get_arg0_time(<ime))
2872
2880
return ltime.second_part;
2877
int64_t Item_func_timestamp_diff::val_int()
2885
longlong Item_func_timestamp_diff::val_int()
2879
DRIZZLE_TIME ltime1, ltime2;
2887
MYSQL_TIME ltime1, ltime2;
2881
2889
long microseconds;
2882
2890
long months= 0;
2895
2903
int_type == INTERVAL_QUARTER ||
2896
2904
int_type == INTERVAL_MONTH)
2898
uint32_t year_beg, year_end, month_beg, month_end, day_beg, day_end;
2900
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;
2966
2974
case INTERVAL_MICROSECOND:
2968
2976
In MySQL difference between any two valid datetime values
2969
in microseconds fits into int64_t.
2977
in microseconds fits into longlong.
2971
2979
return (seconds*1000000L+microseconds)*neg;
3041
3049
(format_name= format->format_name);
3044
uint32_t format_name_len;
3052
uint format_name_len;
3045
3053
format_name_len= strlen(format_name);
3046
3054
if (val_len == format_name_len &&
3047
!my_strnncoll(&my_charset_utf8_general_ci,
3048
(const unsigned char *) val->ptr(), val_len,
3049
(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))
3051
3059
const char *format_str= get_date_time_format_str(format, type);
3052
3060
str->set(format_str, strlen(format_str), &my_charset_bin);
3065
3073
str->append('(');
3067
3075
switch (type) {
3068
case DRIZZLE_TIMESTAMP_DATE:
3076
case MYSQL_TIMESTAMP_DATE:
3069
3077
str->append(STRING_WITH_LEN("DATE, "));
3071
case DRIZZLE_TIMESTAMP_DATETIME:
3079
case MYSQL_TIMESTAMP_DATETIME:
3072
3080
str->append(STRING_WITH_LEN("DATETIME, "));
3074
case DRIZZLE_TIMESTAMP_TIME:
3082
case MYSQL_TIMESTAMP_TIME:
3075
3083
str->append(STRING_WITH_LEN("TIME, "));
3080
3088
args[0]->print(str, query_type);
3081
3089
str->append(')');
3109
3117
static date_time_format_types
3110
get_date_time_result_type(const char *format, uint32_t length)
3118
get_date_time_result_type(const char *format, uint length)
3112
3120
const char *time_part_frms= "HISThiklrs";
3113
3121
const char *date_part_frms= "MVUXYWabcjmvuxyw";
3158
cached_field_type= DRIZZLE_TYPE_DATETIME;
3166
cached_field_type= MYSQL_TYPE_DATETIME;
3159
3167
max_length= MAX_DATETIME_FULL_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
3160
cached_timestamp_type= DRIZZLE_TIMESTAMP_NONE;
3168
cached_timestamp_type= MYSQL_TIMESTAMP_NONE;
3161
3169
if ((const_item= args[1]->const_item()))
3163
3171
char format_buff[64];
3169
3177
format->length());
3170
3178
switch (cached_format_type) {
3171
3179
case DATE_ONLY:
3172
cached_timestamp_type= DRIZZLE_TIMESTAMP_DATE;
3173
cached_field_type= DRIZZLE_TYPE_NEWDATE;
3180
cached_timestamp_type= MYSQL_TIMESTAMP_DATE;
3181
cached_field_type= MYSQL_TYPE_DATE;
3174
3182
max_length= MAX_DATE_WIDTH * MY_CHARSET_BIN_MB_MAXLEN;
3176
3184
case TIME_ONLY:
3177
3185
case TIME_MICROSECOND:
3178
cached_timestamp_type= DRIZZLE_TIMESTAMP_TIME;
3179
cached_field_type= DRIZZLE_TYPE_TIME;
3186
cached_timestamp_type= MYSQL_TIMESTAMP_TIME;
3187
cached_field_type= MYSQL_TYPE_TIME;
3180
3188
max_length= MAX_TIME_WIDTH * MY_CHARSET_BIN_MB_MAXLEN;
3183
cached_timestamp_type= DRIZZLE_TIMESTAMP_DATETIME;
3184
cached_field_type= DRIZZLE_TYPE_DATETIME;
3191
cached_timestamp_type= MYSQL_TIMESTAMP_DATETIME;
3192
cached_field_type= MYSQL_TYPE_DATETIME;
3192
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)
3194
3202
DATE_TIME_FORMAT date_time_format;
3195
3203
char val_buff[64], format_buff[64];
3202
3210
goto null_date;
3205
memset(ltime, 0, sizeof(*ltime));
3213
bzero((char*) ltime, sizeof(*ltime));
3206
3214
date_time_format.format.str= (char*) format->ptr();
3207
3215
date_time_format.format.length= format->length();
3208
3216
if (extract_date_time(&date_time_format, val->ptr(), val->length(),
3210
3218
((fuzzy_date & TIME_NO_ZERO_DATE) &&
3211
3219
(ltime->year == 0 || ltime->month == 0 || ltime->day == 0)))
3212
3220
goto null_date;
3213
if (cached_timestamp_type == DRIZZLE_TIMESTAMP_TIME && ltime->day)
3221
if (cached_timestamp_type == MYSQL_TIMESTAMP_TIME && ltime->day)
3216
3224
Day part for time type can be nonzero value and so
3246
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)
3248
3256
if (get_arg0_date(ltime, fuzzy_date & ~TIME_FUZZY_DATE) ||
3249
3257
(ltime->month == 0))
3255
uint32_t month_idx= ltime->month-1;
3263
uint month_idx= ltime->month-1;
3256
3264
ltime->day= days_in_month[month_idx];
3257
3265
if ( month_idx == 1 && calc_days_in_year(ltime->year) == 366)
3258
3266
ltime->day= 29;
3259
3267
ltime->hour= ltime->minute= ltime->second= 0;
3260
3268
ltime->second_part= 0;
3261
ltime->time_type= DRIZZLE_TIMESTAMP_DATE;
3269
ltime->time_type= MYSQL_TIMESTAMP_DATE;