diff options
Diffstat (limited to 'sql-common')
-rw-r--r-- | sql-common/client.c | 149 | ||||
-rw-r--r-- | sql-common/client_plugin.c | 20 | ||||
-rw-r--r-- | sql-common/my_time.c | 248 |
3 files changed, 291 insertions, 126 deletions
diff --git a/sql-common/client.c b/sql-common/client.c index 57653203149..6db20153eaa 100644 --- a/sql-common/client.c +++ b/sql-common/client.c @@ -128,6 +128,7 @@ const char *def_shared_memory_base_name= default_shared_memory_base_name; static void mysql_close_free_options(MYSQL *mysql); static void mysql_close_free(MYSQL *mysql); static void mysql_prune_stmt_list(MYSQL *mysql); +static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length); #if !defined(__WIN__) static int wait_for_data(my_socket fd, uint timeout); @@ -731,6 +732,7 @@ cli_safe_read(MYSQL *mysql) NET *net= &mysql->net; ulong len=0; +restart: if (net->vio != 0) len=my_net_read(net); @@ -751,13 +753,27 @@ cli_safe_read(MYSQL *mysql) { if (len > 3) { - char *pos=(char*) net->read_pos+1; - net->last_errno=uint2korr(pos); + uchar *pos= net->read_pos+1; + uint last_errno=uint2korr(pos); + + if (last_errno == 65535 && + (mysql->server_capabilities & CLIENT_PROGRESS)) + { + if (cli_report_progress(mysql, pos+2, (uint) (len-3))) + { + /* Wrong packet */ + set_mysql_error(mysql, CR_MALFORMED_PACKET, unknown_sqlstate); + return (packet_error); + } + goto restart; + } + net->last_errno= last_errno; + pos+=2; len-=2; - if (protocol_41(mysql) && pos[0] == '#') + if (protocol_41(mysql) && (char) pos[0] == '#') { - strmake(net->sqlstate, pos+1, SQLSTATE_LENGTH); + strmake(net->sqlstate, (char*) pos+1, SQLSTATE_LENGTH); pos+= SQLSTATE_LENGTH+1; } else @@ -1010,6 +1026,40 @@ static void cli_flush_use_result(MYSQL *mysql, my_bool flush_all_results) } +/* + Report progress to the client + + RETURN VALUES + 0 ok + 1 error +*/ + +static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length) +{ + uint stage, max_stage, proc_length; + double progress; + uchar *start= packet; + + if (length < 5) + return 1; /* Wrong packet */ + + if (!(mysql->options.extension && mysql->options.extension->report_progress)) + return 0; /* No callback, ignore packet */ + + packet++; /* Ignore number of strings */ + stage= (uint) *packet++; + max_stage= (uint) *packet++; + progress= uint3korr(packet)/1000.0; + packet+= 3; + proc_length= net_field_length(&packet); + if (packet + proc_length > start + length) + return 1; /* Wrong packet */ + (*mysql->options.extension->report_progress)(mysql, stage, max_stage, + progress, (char*) packet, + proc_length); + return 0; +} + #ifdef __WIN__ static my_bool is_NT(void) { @@ -1018,57 +1068,6 @@ static my_bool is_NT(void) } #endif - -#ifdef CHECK_LICENSE -/** - Check server side variable 'license'. - - If the variable does not exist or does not contain 'Commercial', - we're talking to non-commercial server from commercial client. - - @retval 0 success - @retval !0 network error or the server is not commercial. - Error code is saved in mysql->net.last_errno. -*/ - -static int check_license(MYSQL *mysql) -{ - MYSQL_ROW row; - MYSQL_RES *res; - NET *net= &mysql->net; - static const char query[]= "SELECT @@license"; - static const char required_license[]= STRINGIFY_ARG(LICENSE); - - if (mysql_real_query(mysql, query, sizeof(query)-1)) - { - if (net->last_errno == ER_UNKNOWN_SYSTEM_VARIABLE) - { - set_mysql_extended_error(mysql, CR_WRONG_LICENSE, unknown_sqlstate, - ER(CR_WRONG_LICENSE), required_license); - } - return 1; - } - if (!(res= mysql_use_result(mysql))) - return 1; - row= mysql_fetch_row(res); - /* - If no rows in result set, or column value is NULL (none of these - two is ever true for server variables now), or column value - mismatch, set wrong license error. - */ - if (!net->last_errno && - (!row || !row[0] || - strncmp(row[0], required_license, sizeof(required_license)))) - { - set_mysql_extended_error(mysql, CR_WRONG_LICENSE, unknown_sqlstate, - ER(CR_WRONG_LICENSE), required_license); - } - mysql_free_result(res); - return net->last_errno; -} -#endif /* CHECK_LICENSE */ - - /************************************************************************** Shut down connection **************************************************************************/ @@ -1180,21 +1179,24 @@ static int add_init_command(struct st_mysql_options *options, const char *cmd) return 0; } -#define EXTENSION_SET_STRING(OPTS, X, STR) \ - if ((OPTS)->extension) \ - my_free((OPTS)->extension->X); \ - else \ +#define EXTENSION_SET(OPTS, X, VAL) \ + if (!(OPTS)->extension) \ (OPTS)->extension= (struct st_mysql_options_extention *) \ my_malloc(sizeof(struct st_mysql_options_extention), \ MYF(MY_WME | MY_ZEROFILL)); \ - (OPTS)->extension->X= my_strdup((STR), MYF(MY_WME)); + (OPTS)->extension->X= VAL; + +#define EXTENSION_SET_STRING(OPTS, X, STR) \ + if ((OPTS)->extension) \ + my_free((OPTS)->extension->X); \ + EXTENSION_SET(OPTS, X, my_strdup((STR), MYF(MY_WME))); void mysql_read_default_options(struct st_mysql_options *options, const char *filename,const char *group) { int argc; char *argv_buff[1],**argv; - const char *groups[3]; + const char *groups[5]; DBUG_ENTER("mysql_read_default_options"); DBUG_PRINT("enter",("file: %s group: %s",filename,group ? group :"NULL")); @@ -1202,7 +1204,11 @@ void mysql_read_default_options(struct st_mysql_options *options, array_elements(default_options)); argc=1; argv=argv_buff; argv_buff[0]= (char*) "client"; - groups[0]= (char*) "client"; groups[1]= (char*) group; groups[2]=0; + groups[0]= (char*) "client"; + groups[1]= (char*) "client-server"; + groups[2]= (char*) "client-mariadb"; + groups[3]= (char*) group; + groups[4]=0; my_load_defaults(filename, groups, &argc, &argv, NULL); if (argc != 1) /* If some default option */ @@ -1746,6 +1752,7 @@ mysql_init(MYSQL *mysql) */ mysql->reconnect= 0; + DBUG_PRINT("mysql",("mysql: 0x%lx", (long) mysql)); return mysql; } @@ -2356,7 +2363,7 @@ static int send_change_user_packet(MCPVIO_EXT *mpvio, char *buff, *end; int res= 1; - buff= my_alloca(USERNAME_LENGTH + data_len + 1 + NAME_LEN + 2 + NAME_LEN); + buff= my_alloca(USERNAME_LENGTH+1 + data_len+1 + NAME_LEN+1 + 2 + NAME_LEN+1); end= strmake(buff, mysql->user, USERNAME_LENGTH) + 1; @@ -3425,11 +3432,6 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user, if (mysql->client_flag & CLIENT_COMPRESS) /* We will use compression */ net->compress=1; -#ifdef CHECK_LICENSE - if (check_license(mysql)) - goto error; -#endif - if (db && !mysql->db && mysql_select_db(mysql, db)) { if (mysql->net.last_errno == CR_SERVER_LOST) @@ -3724,6 +3726,8 @@ void mysql_detach_stmt_list(LIST **stmt_list __attribute__((unused)), void STDCALL mysql_close(MYSQL *mysql) { DBUG_ENTER("mysql_close"); + DBUG_PRINT("enter", ("mysql: 0x%lx", (long) mysql)); + if (mysql) /* Some simple safety */ { /* If connection is still up, send a QUIT message */ @@ -4106,6 +4110,15 @@ mysql_options(MYSQL *mysql,enum mysql_option option, const void *arg) case MYSQL_DEFAULT_AUTH: EXTENSION_SET_STRING(&mysql->options, default_auth, arg); break; + case MYSQL_PROGRESS_CALLBACK: + if (!mysql->options.extension) + mysql->options.extension= (struct st_mysql_options_extention *) + my_malloc(sizeof(struct st_mysql_options_extention), + MYF(MY_WME | MY_ZEROFILL)); + if (mysql->options.extension) + mysql->options.extension->report_progress= + (void (*)(const MYSQL *, uint, uint, double, const char *, uint)) arg; + break; default: DBUG_RETURN(1); } diff --git a/sql-common/client_plugin.c b/sql-common/client_plugin.c index 9bae7169256..0acadaa10fc 100644 --- a/sql-common/client_plugin.c +++ b/sql-common/client_plugin.c @@ -28,6 +28,11 @@ There is no reference counting and no unloading either. */ +#if _MSC_VER +/* Silence warnings about variable 'unused' being used. */ +#define FORCE_INIT_OF_VARS 1 +#endif + #include <my_global.h> #include "mysql.h" #include <my_sys.h> @@ -47,7 +52,8 @@ struct st_client_plugin_int { static my_bool initialized= 0; static MEM_ROOT mem_root; -static const char *plugin_declarations_sym= "_mysql_client_plugin_declaration_"; +#define plugin_declarations_sym "_mysql_client_plugin_declaration_" + static uint plugin_version[MYSQL_CLIENT_MAX_PLUGINS]= { 0, /* these two are taken by Connector/C */ @@ -180,7 +186,7 @@ err1: ER(CR_AUTH_PLUGIN_CANNOT_LOAD), plugin->name, errmsg); if (dlhandle) - dlclose(dlhandle); + (void)dlclose(dlhandle); DBUG_RETURN(NULL); } @@ -235,6 +241,7 @@ int mysql_client_plugin_init() { MYSQL mysql; struct st_mysql_client_plugin **builtin; + va_list unused; DBUG_ENTER("mysql_client_plugin_init"); if (initialized) @@ -252,7 +259,7 @@ int mysql_client_plugin_init() pthread_mutex_lock(&LOCK_load_client_plugin); for (builtin= mysql_client_builtins; *builtin; builtin++) - add_plugin(&mysql, *builtin, 0, 0, 0); + add_plugin(&mysql, *builtin, 0, 0, unused); pthread_mutex_unlock(&LOCK_load_client_plugin); @@ -281,7 +288,7 @@ void mysql_client_plugin_deinit() if (p->plugin->deinit) p->plugin->deinit(); if (p->dlhandle) - dlclose(p->dlhandle); + (void)dlclose(p->dlhandle); } bzero(&plugin_list, sizeof(plugin_list)); @@ -298,6 +305,7 @@ struct st_mysql_client_plugin * mysql_client_register_plugin(MYSQL *mysql, struct st_mysql_client_plugin *plugin) { + va_list unused; DBUG_ENTER("mysql_client_register_plugin"); if (is_not_initialized(mysql, plugin->name)) @@ -314,7 +322,7 @@ mysql_client_register_plugin(MYSQL *mysql, plugin= NULL; } else - plugin= add_plugin(mysql, plugin, 0, 0, 0); + plugin= add_plugin(mysql, plugin, 0, 0, unused); pthread_mutex_unlock(&LOCK_load_client_plugin); DBUG_RETURN(plugin); @@ -365,7 +373,7 @@ mysql_load_plugin_v(MYSQL *mysql, const char *name, int type, if (!(sym= dlsym(dlhandle, plugin_declarations_sym))) { errmsg= "not a plugin"; - dlclose(dlhandle); + (void)dlclose(dlhandle); goto err; } diff --git a/sql-common/my_time.c b/sql-common/my_time.c index d76ec58d623..a0fc2aa98de 100644 --- a/sql-common/my_time.c +++ b/sql-common/my_time.c @@ -18,6 +18,7 @@ #include <m_ctype.h> /* Windows version of localtime_r() is declared in my_ptrhead.h */ #include <my_pthread.h> +#include <mysqld_error.h> ulonglong log_10_int[20]= { @@ -81,7 +82,7 @@ my_bool check_date(const MYSQL_TIME *ltime, my_bool not_zero_date, if (not_zero_date) { if ((((flags & TIME_NO_ZERO_IN_DATE) || !(flags & TIME_FUZZY_DATE)) && - (ltime->month == 0 || ltime->day == 0)) || + (ltime->month == 0 || ltime->day == 0)) || ltime->neg || (!(flags & TIME_INVALID_DATES) && ltime->month && ltime->day > days_in_month[ltime->month-1] && (ltime->month != 2 || calc_days_in_year(ltime->year) != 366 || @@ -160,7 +161,7 @@ enum enum_mysql_timestamp_type str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time, ulonglong flags, int *was_cut) { - uint field_length, UNINIT_VAR(year_length), digits, i, number_of_fields; + uint UNINIT_VAR(field_length), UNINIT_VAR(year_length), digits, i, number_of_fields; uint date[MAX_DATE_PARTS], date_len[MAX_DATE_PARTS]; uint add_hours= 0, start_loop; ulong not_zero_date, allow_space; @@ -171,9 +172,14 @@ str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time, my_bool found_delimitier= 0, found_space= 0; uint frac_pos, frac_len; DBUG_ENTER("str_to_datetime"); - DBUG_PRINT("ENTER",("str: %.*s",length,str)); + DBUG_PRINT("enter",("str: %.*s",length,str)); - LINT_INIT(field_length); + if (flags & TIME_TIME_ONLY) + { + enum enum_mysql_timestamp_type ret; + ret= str_to_time(str, length, l_time, flags, was_cut); + DBUG_RETURN(ret); + } *was_cut= 0; @@ -477,12 +483,13 @@ err: work with times where the time arguments are in the above order. RETURN - 0 ok - 1 error + MYSQL_TIMESTAMP_TIME + MYSQL_TIMESTAMP_ERROR */ -my_bool str_to_time(const char *str, uint length, MYSQL_TIME *l_time, - int *warning) +enum enum_mysql_timestamp_type +str_to_time(const char *str, uint length, MYSQL_TIME *l_time, + ulong fuzzydate, int *warning) { ulong date[5]; ulonglong value; @@ -501,7 +508,7 @@ my_bool str_to_time(const char *str, uint length, MYSQL_TIME *l_time, length--; } if (str == end) - return 1; + return MYSQL_TIMESTAMP_ERROR; /* Check first if this is a full TIMESTAMP */ if (length >= 12) @@ -509,12 +516,13 @@ my_bool str_to_time(const char *str, uint length, MYSQL_TIME *l_time, int was_cut; enum enum_mysql_timestamp_type res= str_to_datetime(str, length, l_time, - (TIME_FUZZY_DATE | TIME_DATETIME_ONLY), &was_cut); + (fuzzydate & ~TIME_TIME_ONLY) | TIME_DATETIME_ONLY, + &was_cut); if ((int) res >= (int) MYSQL_TIMESTAMP_ERROR) { if (was_cut) *warning|= MYSQL_TIME_WARN_TRUNCATED; - return res == MYSQL_TIMESTAMP_ERROR; + return res; } } @@ -609,7 +617,7 @@ fractional: ((str[1] == '-' || str[1] == '+') && (end - str) > 2 && my_isdigit(&my_charset_latin1, str[2])))) - return 1; + return MYSQL_TIMESTAMP_ERROR; if (internal_format_positions[7] != 255) { @@ -632,7 +640,7 @@ fractional: if (date[0] > UINT_MAX || date[1] > UINT_MAX || date[2] > UINT_MAX || date[3] > UINT_MAX || date[4] > UINT_MAX) - return 1; + return MYSQL_TIMESTAMP_ERROR; l_time->year= 0; /* For protocol::store_time */ l_time->month= 0; @@ -644,8 +652,8 @@ fractional: l_time->time_type= MYSQL_TIMESTAMP_TIME; /* Check if the value is valid and fits into MYSQL_TIME range */ - if (check_time_range(l_time, warning)) - return 1; + if (check_time_range(l_time, 6, warning)) + return MYSQL_TIMESTAMP_ERROR; /* Check if there is garbage at end of the MYSQL_TIME specification */ if (str != end) @@ -659,7 +667,7 @@ fractional: } } while (++str != end); } - return 0; + return MYSQL_TIMESTAMP_TIME; } @@ -669,6 +677,7 @@ fractional: SYNOPSIS: check_time_range() time pointer to MYSQL_TIME value + uint dec warning set MYSQL_TIME_WARN_OUT_OF_RANGE flag if the value is out of range DESCRIPTION @@ -681,24 +690,31 @@ fractional: 1 time value is invalid */ -int check_time_range(struct st_mysql_time *my_time, int *warning) +int check_time_range(struct st_mysql_time *my_time, uint dec, int *warning) { longlong hour; + static ulong max_sec_part[TIME_SECOND_PART_DIGITS+1]= {000000, 900000, 990000, + 999000, 999900, 999990, 999999}; if (my_time->minute >= 60 || my_time->second >= 60) return 1; hour= my_time->hour + (24*my_time->day); + + if (dec == AUTO_SEC_PART_DIGITS) + dec= TIME_SECOND_PART_DIGITS; + if (hour <= TIME_MAX_HOUR && (hour != TIME_MAX_HOUR || my_time->minute != TIME_MAX_MINUTE || - my_time->second != TIME_MAX_SECOND || !my_time->second_part)) + my_time->second != TIME_MAX_SECOND || + my_time->second_part <= max_sec_part[dec])) return 0; my_time->day= 0; my_time->hour= TIME_MAX_HOUR; my_time->minute= TIME_MAX_MINUTE; my_time->second= TIME_MAX_SECOND; - my_time->second_part= 0; + my_time->second_part= max_sec_part[dec]; *warning|= MYSQL_TIME_WARN_OUT_OF_RANGE; return 0; } @@ -715,7 +731,7 @@ void my_init_time(void) time_t seconds; struct tm *l_time,tm_tmp; MYSQL_TIME my_time; - my_bool not_used; + uint not_used; seconds= (time_t) time((time_t*) 0); localtime_r(&seconds,&tm_tmp); @@ -727,6 +743,10 @@ void my_init_time(void) my_time.hour= (uint) l_time->tm_hour; my_time.minute= (uint) l_time->tm_min; my_time.second= (uint) l_time->tm_sec; + my_time.neg= 0; + my_time.second_part= 0; + my_time.time_type= MYSQL_TIMESTAMP_DATETIME; + my_system_gmt_sec(&my_time, &my_time_zone, ¬_used); /* Init my_time_zone */ } @@ -797,7 +817,11 @@ long calc_daynr(uint year,uint month,uint day) t - time value to be converted my_timezone - pointer to long where offset of system time zone from UTC will be stored for caching - in_dst_time_gap - set to true if time falls into spring time-gap + error_code - 0, if the conversion was successful; + ER_WARN_DATA_OUT_OF_RANGE, if t contains datetime value + which is out of TIMESTAMP range; + ER_WARN_INVALID_TIMESTAMP, if t represents value which + doesn't exists (falls into the spring time-gap). NOTES The idea is to cache the time zone offset from UTC (including daylight @@ -811,8 +835,7 @@ long calc_daynr(uint year,uint month,uint day) Time in UTC seconds since Unix Epoch representation. */ my_time_t -my_system_gmt_sec(const MYSQL_TIME *t_src, long *my_timezone, - my_bool *in_dst_time_gap) +my_system_gmt_sec(const MYSQL_TIME *t_src, long *my_timezone, uint *error_code) { uint loop; time_t tmp= 0; @@ -829,7 +852,11 @@ my_system_gmt_sec(const MYSQL_TIME *t_src, long *my_timezone, memcpy(&tmp_time, t_src, sizeof(MYSQL_TIME)); if (!validate_timestamp_range(t)) + { + *error_code= ER_WARN_DATA_OUT_OF_RANGE; return 0; + } + *error_code= 0; /* Calculate the gmt time based on current time and timezone @@ -975,7 +1002,7 @@ my_system_gmt_sec(const MYSQL_TIME *t_src, long *my_timezone, else if (diff == -3600) tmp-=t->minute*60 + t->second; /* Move to previous hour */ - *in_dst_time_gap= 1; + *error_code= ER_WARN_INVALID_TIMESTAMP; } *my_timezone= current_timezone; @@ -994,7 +1021,10 @@ my_system_gmt_sec(const MYSQL_TIME *t_src, long *my_timezone, larger then TIMESTAMP_MAX_VALUE, so another check will work. */ if (!IS_TIME_T_VALID_FOR_TIMESTAMP(tmp)) + { tmp= 0; + *error_code= ER_WARN_DATA_OUT_OF_RANGE; + } return (my_time_t) tmp; } /* my_system_gmt_sec */ @@ -1014,22 +1044,27 @@ void set_zero_time(MYSQL_TIME *tm, enum enum_mysql_timestamp_type time_type) using default format. This functions don't check that given MYSQL_TIME structure members are in valid range. If they are not, return value won't reflect any - valid date either. Additionally, make_time doesn't take into - account time->day member: it's assumed that days have been converted - to hours already. + valid date either. RETURN number of characters written to 'to' */ -int my_time_to_str(const MYSQL_TIME *l_time, char *to) +int my_time_to_str(const MYSQL_TIME *l_time, char *to, uint digits) { - uint extra_hours= 0; - return sprintf(to, "%s%02u:%02u:%02u", + ulong day= (l_time->year || l_time->month) ? 0 : l_time->day; + + if (digits == AUTO_SEC_PART_DIGITS) + digits= l_time->second_part ? TIME_SECOND_PART_DIGITS : 0; + + DBUG_ASSERT(digits <= TIME_SECOND_PART_DIGITS); + + return sprintf(to, + digits ? "%s%02lu:%02u:%02u.%0*lu" + : "%s%02lu:%02u:%02u", (l_time->neg ? "-" : ""), - extra_hours+ l_time->hour, - l_time->minute, - l_time->second); + day * 24L + l_time->hour, l_time->minute, l_time->second, + digits, (ulong)sec_part_shift(l_time->second_part, digits)); } int my_date_to_str(const MYSQL_TIME *l_time, char *to) @@ -1040,15 +1075,19 @@ int my_date_to_str(const MYSQL_TIME *l_time, char *to) l_time->day); } -int my_datetime_to_str(const MYSQL_TIME *l_time, char *to) +int my_datetime_to_str(const MYSQL_TIME *l_time, char *to, uint digits) { - return sprintf(to, "%04u-%02u-%02u %02u:%02u:%02u", - l_time->year, - l_time->month, - l_time->day, - l_time->hour, - l_time->minute, - l_time->second); + if (digits == AUTO_SEC_PART_DIGITS) + digits= l_time->second_part ? TIME_SECOND_PART_DIGITS : 0; + + DBUG_ASSERT(digits <= TIME_SECOND_PART_DIGITS); + + return sprintf(to, + digits ? "%04u-%02u-%02u %02u:%02u:%02u.%0*lu" + : "%04u-%02u-%02u %02u:%02u:%02u", + l_time->year, l_time->month, l_time->day, + l_time->hour, l_time->minute, l_time->second, + digits, (ulong)sec_part_shift(l_time->second_part, digits)); } @@ -1059,19 +1098,22 @@ int my_datetime_to_str(const MYSQL_TIME *l_time, char *to) SYNOPSIS my_TIME_to_string() + RETURN + length of string + NOTE The string must have at least MAX_DATE_STRING_REP_LENGTH bytes reserved. */ -int my_TIME_to_str(const MYSQL_TIME *l_time, char *to) +int my_TIME_to_str(const MYSQL_TIME *l_time, char *to, uint digits) { switch (l_time->time_type) { case MYSQL_TIMESTAMP_DATETIME: - return my_datetime_to_str(l_time, to); + return my_datetime_to_str(l_time, to, digits); case MYSQL_TIMESTAMP_DATE: return my_date_to_str(l_time, to); case MYSQL_TIMESTAMP_TIME: - return my_time_to_str(l_time, to); + return my_time_to_str(l_time, to, digits); case MYSQL_TIMESTAMP_NONE: case MYSQL_TIMESTAMP_ERROR: to[0]='\0'; @@ -1109,16 +1151,15 @@ int my_TIME_to_str(const MYSQL_TIME *l_time, char *to) Datetime value in YYYYMMDDHHMMSS format. */ -longlong number_to_datetime(longlong nr, MYSQL_TIME *time_res, +longlong number_to_datetime(longlong nr, ulong sec_part, MYSQL_TIME *time_res, ulonglong flags, int *was_cut) { long part1,part2; *was_cut= 0; - bzero((char*) time_res, sizeof(*time_res)); time_res->time_type=MYSQL_TIMESTAMP_DATE; - if (nr == LL(0) || nr >= LL(10000101000000)) + if (nr == 0 || nr >= 10000101000000LL || sec_part) { time_res->time_type=MYSQL_TIMESTAMP_DATETIME; goto ok; @@ -1173,22 +1214,87 @@ longlong number_to_datetime(longlong nr, MYSQL_TIME *time_res, time_res->hour= (int) (part2/10000L); part2%=10000L; time_res->minute=(int) part2 / 100; time_res->second=(int) part2 % 100; + time_res->second_part= sec_part; + time_res->neg= 0; if (time_res->year <= 9999 && time_res->month <= 12 && time_res->day <= 31 && time_res->hour <= 23 && time_res->minute <= 59 && time_res->second <= 59 && - !check_date(time_res, (nr != 0), flags, was_cut)) + sec_part <= TIME_MAX_SECOND_PART && + !check_date(time_res, nr || sec_part, flags, was_cut)) return nr; /* Don't want to have was_cut get set if NO_ZERO_DATE was violated. */ - if (!nr && (flags & TIME_NO_ZERO_DATE)) - return LL(-1); + if (nr || !(flags & TIME_NO_ZERO_DATE)) + *was_cut= 1; + return LL(-1); err: - *was_cut= 1; + { + /* reset everything except time_type */ + enum enum_mysql_timestamp_type save= time_res->time_type; + bzero((char*) time_res, sizeof(*time_res)); + time_res->time_type= save; /* Restore range */ + *was_cut= 1; /* Found invalid date */ + } return LL(-1); } +/* + Convert a pair of integers to a MYSQL_TIME struct. + + @param[in] nr a number to convert + @param[out] ltime Date to check. + @param[out] was_cut MYSQL_TIME_WARN_OUT_OF_RANGE if the value was + modified to fit in the valid range. Otherwise 0. + + @details + Takes a number in the [-]HHHMMSS.uuuuuu, + YYMMDDHHMMSS.uuuuuu, or in the YYYYMMDDHHMMSS.uuuuuu formats. + + @return + 0 time value is valid, but was possibly truncated + -1 time value is invalid +*/ +int number_to_time(my_bool neg, longlong nr, ulong sec_part, + MYSQL_TIME *ltime, int *was_cut) +{ + if (nr > 9999999 && neg == 0) + { + if (number_to_datetime(nr, sec_part, ltime, + TIME_FUZZY_DATE | TIME_INVALID_DATES, was_cut) < 0) + return -1; + + ltime->year= ltime->month= ltime->day= 0; + ltime->time_type= MYSQL_TIMESTAMP_TIME; + *was_cut= MYSQL_TIME_WARN_TRUNCATED; + return 0; + } + + *was_cut= 0; + ltime->year= ltime->month= ltime->day= 0; + ltime->time_type= MYSQL_TIMESTAMP_TIME; + + ltime->neg= neg; + + if (nr > TIME_MAX_VALUE) + { + nr= TIME_MAX_VALUE; + sec_part= TIME_MAX_SECOND_PART; + *was_cut= MYSQL_TIME_WARN_OUT_OF_RANGE; + } + ltime->hour = (uint)(nr/100/100); + ltime->minute= nr/100%100; + ltime->second= nr%100; + ltime->second_part= sec_part; + + if (ltime->minute < 60 && ltime->second < 60 && sec_part <= TIME_MAX_SECOND_PART) + return 0; + + *was_cut= MYSQL_TIME_WARN_TRUNCATED; + return -1; +} + /* Convert time value to integer in YYYYMMDDHHMMSS format */ @@ -1237,7 +1343,7 @@ ulonglong TIME_to_ulonglong_time(const MYSQL_TIME *my_time) DESCRIPTION The function is used when we need to convert value of time item to a number if it's used in numeric context, i. e.: - SELECT NOW()+1, CURDATE()+0, CURTIMIE()+0; + SELECT NOW()+1, CURDATE()+0, CURTIME()+0; SELECT ?+1; NOTE @@ -1264,3 +1370,41 @@ ulonglong TIME_to_ulonglong(const MYSQL_TIME *my_time) return 0; } +double TIME_to_double(const MYSQL_TIME *my_time) +{ + double d= (double)TIME_to_ulonglong(my_time); + + if (my_time->time_type == MYSQL_TIMESTAMP_DATE) + return d; + + d+= my_time->second_part/(double)TIME_SECOND_PART_FACTOR; + return my_time->neg ? -d : d; +} + +longlong pack_time(MYSQL_TIME *my_time) +{ + return ((((((my_time->year * 13ULL + + my_time->month) * 32ULL + + my_time->day) * 24ULL + + my_time->hour) * 60ULL + + my_time->minute) * 60ULL + + my_time->second) * 1000000ULL + + my_time->second_part) * (my_time->neg ? -1 : 1); +} + +#define get_one(WHERE, FACTOR) WHERE= (ulong)(packed % FACTOR); packed/= FACTOR + +MYSQL_TIME *unpack_time(longlong packed, MYSQL_TIME *my_time) +{ + if ((my_time->neg= packed < 0)) + packed= -packed; + get_one(my_time->second_part, 1000000ULL); + get_one(my_time->second, 60ULL); + get_one(my_time->minute, 60ULL); + get_one(my_time->hour, 24ULL); + get_one(my_time->day, 32ULL); + get_one(my_time->month, 13ULL); + my_time->year= (uint)packed; + my_time->time_type= MYSQL_TIMESTAMP_DATETIME; + return my_time; +} |