diff options
Diffstat (limited to 'sql-common')
-rw-r--r-- | sql-common/client.c | 55 | ||||
-rw-r--r-- | sql-common/my_time.c | 122 |
2 files changed, 120 insertions, 57 deletions
diff --git a/sql-common/client.c b/sql-common/client.c index 927da747392..cc70bbc7523 100644 --- a/sql-common/client.c +++ b/sql-common/client.c @@ -713,8 +713,9 @@ void free_old_query(MYSQL *mysql) if (mysql->fields) free_root(&mysql->field_alloc,MYF(0)); init_alloc_root(&mysql->field_alloc,8192,0); /* Assume rowlength < 8192 */ - mysql->fields=0; - mysql->field_count=0; /* For API */ + mysql->fields= 0; + mysql->field_count= 0; /* For API */ + mysql->info= 0; DBUG_VOID_RETURN; } @@ -2213,6 +2214,15 @@ my_bool mysql_reconnect(MYSQL *mysql) strmov(mysql->net.sqlstate, tmp_mysql.net.sqlstate); DBUG_RETURN(1); } + if (mysql_set_character_set(&tmp_mysql, mysql->charset->csname)) + { + mysql_close(&tmp_mysql); + mysql->net.last_errno= tmp_mysql.net.last_errno; + strmov(mysql->net.last_error, tmp_mysql.net.last_error); + strmov(mysql->net.sqlstate, tmp_mysql.net.sqlstate); + DBUG_RETURN(1); + } + tmp_mysql.reconnect= 1; tmp_mysql.free_me= mysql->free_me; @@ -2781,8 +2791,49 @@ uint STDCALL mysql_errno(MYSQL *mysql) return mysql->net.last_errno; } + const char * STDCALL mysql_error(MYSQL *mysql) { return mysql->net.last_error; } +/* + mysql_set_character_set function sends SET NAMES cs_name to + the server (which changes character_set_client, character_set_result + and character_set_connection) and updates mysql->charset so other + functions like mysql_real_escape will work correctly. +*/ +int STDCALL mysql_set_character_set(MYSQL *mysql, const char *cs_name) +{ + struct charset_info_st *cs; + const char *save_csdir= charsets_dir; + + if (mysql->options.charset_dir) + charsets_dir= mysql->options.charset_dir; + + if (strlen(cs_name) < MY_CS_NAME_SIZE && + (cs= get_charset_by_csname(cs_name, MY_CS_PRIMARY, MYF(0)))) + { + char buff[MY_CS_NAME_SIZE + 10]; + charsets_dir= save_csdir; + sprintf(buff, "SET NAMES %s", cs_name); + if (!mysql_real_query(mysql, buff, strlen(buff))) + { + mysql->charset= cs; + } + } + else + { + char cs_dir_name[FN_REFLEN]; + get_charsets_dir(cs_dir_name); + mysql->net.last_errno= CR_CANT_READ_CHARSET; + strmov(mysql->net.sqlstate, unknown_sqlstate); + my_snprintf(mysql->net.last_error, sizeof(mysql->net.last_error) - 1, + ER(mysql->net.last_errno), cs_name, cs_dir_name); + + } + charsets_dir= save_csdir; + return mysql->net.last_errno; +} + + diff --git a/sql-common/my_time.c b/sql-common/my_time.c index 1078259f15d..72809ee9b4b 100644 --- a/sql-common/my_time.c +++ b/sql-common/my_time.c @@ -56,12 +56,15 @@ uint calc_days_in_year(uint year) } /* - check date. - - SYNOPOSIS - bool check_date() - time Date to check. + Check datetime value for validity according to flags. + SYNOPSIS + check_date() + ltime Date to check. + not_zero_date ltime is not the zero date + flags flags to check + was_cut set to 2 if value was truncated. + NOTE: This is not touched if value was not truncated NOTES Here we assume that year and month is ok ! If month is 0 we allow any date. (This only happens if we allow zero @@ -69,18 +72,34 @@ uint calc_days_in_year(uint year) RETURN 0 ok - 1 errro + 1 error */ -bool check_date(MYSQL_TIME *ltime) +static my_bool check_date(const MYSQL_TIME *ltime, my_bool not_zero_date, + ulong flags, int *was_cut) { - if (ltime->month && ltime->day > days_in_month[ltime->month-1]) + if (not_zero_date) { - if (ltime->month != 2 || calc_days_in_year(ltime->year) != 366 || - ltime->day != 29) - return 1; + if ((((flags & TIME_NO_ZERO_IN_DATE) || !(flags & TIME_FUZZY_DATE)) && + (ltime->month == 0 || ltime->day == 0)) || + (!(flags & TIME_INVALID_DATES) && + ltime->month && ltime->day > days_in_month[ltime->month-1] && + (ltime->month != 2 || calc_days_in_year(ltime->year) != 366 || + ltime->day != 29))) + { + *was_cut= 2; + return TRUE; + } } - return 0; + else if (flags & TIME_NO_ZERO_DATE) + { + /* + We don't set *was_cut here to signal that the problem was a zero date + and not an invalid date + */ + return TRUE; + } + return FALSE; } @@ -100,7 +119,7 @@ bool check_date(MYSQL_TIME *ltime) TIME_INVALID_DATES Allow 2000-02-31 was_cut 0 Value ok 1 If value was cut during conversion - 2 Date part was withing ranges but date was wrong + 2 Date part was within ranges but date was wrong DESCRIPTION At least the following formats are recogniced (based on number of digits) @@ -145,11 +164,11 @@ str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time, uint date[MAX_DATE_PARTS], date_len[MAX_DATE_PARTS]; uint add_hours= 0, start_loop; ulong not_zero_date, allow_space; - bool is_internal_format; + my_bool is_internal_format; const char *pos, *last_field_pos; const char *end=str+length; const uchar *format_position; - bool found_delimitier= 0, found_space= 0; + 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)); @@ -168,8 +187,6 @@ str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time, *was_cut= 1; DBUG_RETURN(MYSQL_TIMESTAMP_NONE); } - if (flags & TIME_NO_ZERO_IN_DATE) - flags&= ~TIME_FUZZY_DATE; is_internal_format= 0; /* This has to be changed if want to activate different timestamp formats */ @@ -180,7 +197,9 @@ str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time, If length= 8 or >= 14 then year is of format YYYY. (YYYY-MM-DD, YYYYMMDD, YYYYYMMDDHHMMSS) */ - for (pos=str; pos != end && my_isdigit(&my_charset_latin1,*pos) ; pos++) + for (pos=str; + pos != end && (my_isdigit(&my_charset_latin1,*pos) || *pos == 'T'); + pos++) ; digits= (uint) (pos-str); @@ -190,7 +209,7 @@ str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time, { /* Found date in internal format (only numbers like YYYYMMDD) */ year_length= (digits == 4 || digits == 8 || digits >= 14) ? 4 : 2; - field_length=year_length-1; + field_length= year_length; is_internal_format= 1; format_position= internal_format_positions; } @@ -220,6 +239,8 @@ str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time, start_loop= 5; /* Start with first date part */ } } + + field_length= format_position[0] == 0 ? 4 : 2; } /* @@ -244,7 +265,7 @@ str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time, const char *start= str; ulong tmp_value= (uint) (uchar) (*str++ - '0'); while (str != end && my_isdigit(&my_charset_latin1,str[0]) && - (!is_internal_format || field_length--)) + (!is_internal_format || --field_length)) { tmp_value=tmp_value*10 + (ulong) (uchar) (*str - '0'); str++; @@ -258,8 +279,8 @@ str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time, date[i]=tmp_value; not_zero_date|= tmp_value; - /* Length-1 of next field */ - field_length= format_position[i+1] == 0 ? 3 : 1; + /* Length of next field */ + field_length= format_position[i+1] == 0 ? 4 : 2; if ((last_field_pos= str) == end) { @@ -277,7 +298,7 @@ str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time, if (*str == '.') /* Followed by part seconds */ { str++; - field_length= 5; /* 5 digits after first (=6) */ + field_length= 6; /* 6 digits */ } continue; @@ -385,22 +406,10 @@ str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time, if (year_length == 2 && not_zero_date) l_time->year+= (l_time->year < YY_PART_YEAR ? 2000 : 1900); - if (!not_zero_date && (flags & TIME_NO_ZERO_DATE)) - { - /* - We don't set *was_cut here to signal that the problem was a zero date - and not an invalid date - */ - goto err; - } - if (number_of_fields < 3 || l_time->year > 9999 || l_time->month > 12 || l_time->day > 31 || l_time->hour > 23 || - l_time->minute > 59 || l_time->second > 59 || - (!(flags & TIME_FUZZY_DATE) && (l_time->month == 0 || - l_time->day == 0) && - not_zero_date)) + l_time->minute > 59 || l_time->second > 59) { /* Only give warning for a zero date if there is some garbage after */ if (!not_zero_date) /* If zero date */ @@ -418,15 +427,12 @@ str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time, goto err; } + if (check_date(l_time, not_zero_date, flags, was_cut)) + goto err; + l_time->time_type= (number_of_fields <= 3 ? MYSQL_TIMESTAMP_DATE : MYSQL_TIMESTAMP_DATETIME); - if (not_zero_date && !(flags & TIME_INVALID_DATES) && check_date(l_time)) - { - *was_cut= 2; /* Not correct date */ - goto err; - } - for (; str != end ; str++) { if (!my_isspace(&my_charset_latin1,*str)) @@ -469,12 +475,12 @@ err: 1 error */ -bool str_to_time(const char *str, uint length, MYSQL_TIME *l_time, - int *was_cut) +my_bool str_to_time(const char *str, uint length, MYSQL_TIME *l_time, + int *was_cut) { long date[5],value; const char *end=str+length, *end_of_days; - bool found_days,found_hours; + my_bool found_days,found_hours; uint state; l_time->neg=0; @@ -641,7 +647,7 @@ void init_time(void) time_t seconds; struct tm *l_time,tm_tmp; MYSQL_TIME my_time; - bool not_used; + my_bool not_used; seconds= (time_t) time((time_t*) 0); localtime_r(&seconds,&tm_tmp); @@ -707,7 +713,8 @@ 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, long *my_timezone, bool *in_dst_time_gap) +my_system_gmt_sec(const MYSQL_TIME *t, long *my_timezone, + my_bool *in_dst_time_gap) { uint loop; time_t tmp; @@ -881,9 +888,10 @@ int my_TIME_to_str(const MYSQL_TIME *l_time, char *to) number_to_datetime() nr - datetime value as number time_res - pointer for structure for broken-down representation - fuzzy_date - indicates whenever we allow fuzzy dates - was_cut - set ot 1 if there was some kind of error during - conversion or to 0 if everything was OK. + flags - flags to use in validating date, as in str_to_datetime() + was_cut 0 Value ok + 1 If value was cut during conversion + 2 Date part was within ranges but date was wrong DESCRIPTION Convert a datetime value of formats YYMMDD, YYYYMMDD, YYMMDDHHMSS, @@ -893,12 +901,13 @@ int my_TIME_to_str(const MYSQL_TIME *l_time, char *to) This function also checks if datetime value fits in DATETIME range. RETURN VALUE + -1 Timestamp with wrong values + anything else DATETIME as integer in YYYYMMDDHHMMSS format Datetime value in YYYYMMDDHHMMSS format. - If input value is not valid datetime value then 0 is returned. */ longlong number_to_datetime(longlong nr, MYSQL_TIME *time_res, - my_bool fuzzy_date, int *was_cut) + uint flags, int *was_cut) { long part1,part2; @@ -952,13 +961,16 @@ longlong number_to_datetime(longlong nr, MYSQL_TIME *time_res, 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 && - (fuzzy_date || (time_res->month != 0 && time_res->day != 0) || nr==0)) + !check_date(time_res, (nr != 0), flags, was_cut)) return nr; - err: + /* 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); + err: *was_cut= 1; - return LL(0); + return LL(-1); } |