44
40
int Field_datetime::store(const char *from,
46
const CHARSET_INFO * const )
48
ASSERT_COLUMN_MARKED_FOR_WRITE;
50
* Try to create a DateTime from the supplied string. Throw an error
51
* if unable to create a valid DateTime.
54
if (! temporal.from_string(from, (size_t) len))
56
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), from);
59
/* Create the stored integer format. @TODO This should go away. Should be up to engine... */
61
temporal.to_int64_t(&int_value);
63
#ifdef WORDS_BIGENDIAN
64
if (table && table->s->db_low_byte_first)
66
int8store(ptr, int_value);
70
int64_tstore(ptr, int_value);
74
int Field_datetime::store(double from)
76
ASSERT_COLUMN_MARKED_FOR_WRITE;
77
if (from < 0.0 || from > 99991231235959.0)
79
/* Convert the double to a string using stringstream */
82
ss.precision(18); /* 18 places should be fine for error display of double input. */
83
ss << from; ss >> tmp;
85
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
88
return Field_datetime::store((int64_t) rint(from), false);
91
int Field_datetime::store(int64_t from, bool)
93
ASSERT_COLUMN_MARKED_FOR_WRITE;
95
* Try to create a DateTime from the supplied integer. Throw an error
96
* if unable to create a valid DateTime.
99
if (! temporal.from_int64_t(from))
101
/* Convert the integer to a string using stringstream */
102
std::stringstream ss;
104
ss << from; ss >> tmp;
106
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
111
* Because "from" may be a silly MySQL-like "datetime number" (like, oh, 101)
112
* we must here get the value of the DateTime as its *real* int64_t, after
113
* the conversion above has been done...yuck. God, save us.
116
temporal.to_int64_t(&int_value);
118
#ifdef WORDS_BIGENDIAN
119
if (table && table->s->db_low_byte_first)
121
int8store(ptr, int_value);
125
int64_tstore(ptr, int_value);
129
int Field_datetime::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
133
temporal.set_years(ltime->year);
134
temporal.set_months(ltime->month);
135
temporal.set_days(ltime->day);
136
temporal.set_hours(ltime->hour);
137
temporal.set_minutes(ltime->minute);
138
temporal.set_seconds(ltime->second);
140
if (! temporal.is_valid())
142
char tmp_string[MAX_DATE_STRING_REP_LENGTH];
143
size_t tmp_string_len;
145
tmp_string_len= temporal.to_string(tmp_string, MAX_DATE_STRING_REP_LENGTH);
146
assert(tmp_string_len < MAX_DATE_STRING_REP_LENGTH);
147
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp_string);
152
temporal.to_int64_t(&int_value);
154
#ifdef WORDS_BIGENDIAN
155
if (table && table->s->db_low_byte_first)
157
int8store(ptr, int_value);
161
int64_tstore(ptr, int_value);
42
const CHARSET_INFO * const cs __attribute__((unused)))
44
DRIZZLE_TIME time_tmp;
47
enum enum_drizzle_timestamp_type func_res;
48
Session *session= table ? table->in_use : current_session;
50
func_res= str_to_datetime(from, len, &time_tmp,
52
(session->variables.sql_mode &
53
(MODE_NO_ZERO_DATE | MODE_INVALID_DATES))),
55
if ((int) func_res > (int) DRIZZLE_TIMESTAMP_ERROR)
56
tmp= TIME_to_uint64_t_datetime(&time_tmp);
58
error= 1; // Fix if invalid zero date
61
set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
62
ER_WARN_DATA_OUT_OF_RANGE,
63
from, len, DRIZZLE_TIMESTAMP_DATETIME, 1);
65
#ifdef WORDS_BIGENDIAN
66
if (table && table->s->db_low_byte_first)
72
int64_tstore(ptr,tmp);
77
int Field_datetime::store(double nr)
80
if (nr < 0.0 || nr > 99991231235959.0)
82
set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
83
ER_WARN_DATA_OUT_OF_RANGE,
84
nr, DRIZZLE_TIMESTAMP_DATETIME);
88
error|= Field_datetime::store((int64_t) rint(nr), false);
93
int Field_datetime::store(int64_t nr,
94
bool unsigned_val __attribute__((unused)))
96
DRIZZLE_TIME not_used;
98
int64_t initial_nr= nr;
99
Session *session= table ? table->in_use : current_session;
101
nr= number_to_datetime(nr, ¬_used, (TIME_FUZZY_DATE |
102
(session->variables.sql_mode &
104
MODE_INVALID_DATES))), &error);
106
if (nr == INT64_C(-1))
113
set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
114
error == 2 ? ER_WARN_DATA_OUT_OF_RANGE :
115
ER_WARN_DATA_TRUNCATED, initial_nr,
116
DRIZZLE_TIMESTAMP_DATETIME, 1);
118
#ifdef WORDS_BIGENDIAN
119
if (table && table->s->db_low_byte_first)
125
int64_tstore(ptr,nr);
130
int Field_datetime::store_time(DRIZZLE_TIME *ltime,
131
enum enum_drizzle_timestamp_type time_type)
136
We don't perform range checking here since values stored in TIME
137
structure always fit into DATETIME range.
139
if (time_type == DRIZZLE_TIMESTAMP_DATE ||
140
time_type == DRIZZLE_TIMESTAMP_DATETIME)
142
tmp=((ltime->year*10000L+ltime->month*100+ltime->day)*INT64_C(1000000)+
143
(ltime->hour*10000L+ltime->minute*100+ltime->second));
144
if (check_date(ltime, tmp != 0,
146
(current_session->variables.sql_mode &
147
(MODE_NO_ZERO_DATE | MODE_INVALID_DATES))), &error))
149
char buff[MAX_DATE_STRING_REP_LENGTH];
150
String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
151
make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
152
set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
153
str.ptr(), str.length(), DRIZZLE_TIMESTAMP_DATETIME,1);
160
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
162
#ifdef WORDS_BIGENDIAN
163
if (table && table->s->db_low_byte_first)
169
int64_tstore(ptr,tmp);
173
bool Field_datetime::send_binary(Protocol *protocol)
176
Field_datetime::get_date(&tm, TIME_FUZZY_DATE);
177
return protocol->store(&tm);
165
181
double Field_datetime::val_real(void)
200
214
int64_tget(tmp,ptr);
204
/* TODO: add an assert that this succeeds
205
* currently fails due to bug in allowing
206
* ALTER TABLE to add a datetime column that's
207
* not null without a default value.
209
dt.from_int64_t(tmp, false); /* NOTE: this does *NOT* attempt convertion
210
from formats such as 20090101 as
211
the stored value has already been
216
rlen= dt.to_string((char*)val_buffer->ptr(), DateTime::MAX_STRING_LENGTH);
217
assert((rlen+1) < DateTime::MAX_STRING_LENGTH);
219
val_buffer->length(rlen);
217
Avoid problem with slow int64_t arithmetic and sprintf
220
part1=(long) (tmp/INT64_C(1000000));
221
part2=(long) (tmp - (uint64_t) part1*INT64_C(1000000));
223
pos=(char*) val_buffer->ptr() + MAX_DATETIME_WIDTH;
225
*pos--= (char) ('0'+(char) (part2%10)); part2/=10;
226
*pos--= (char) ('0'+(char) (part2%10)); part3= (int) (part2 / 10);
228
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
229
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
231
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
232
*pos--= (char) ('0'+(char) part3);
234
*pos--= (char) ('0'+(char) (part1%10)); part1/=10;
235
*pos--= (char) ('0'+(char) (part1%10)); part1/=10;
237
*pos--= (char) ('0'+(char) (part1%10)); part1/=10;
238
*pos--= (char) ('0'+(char) (part1%10)); part3= (int) (part1/10);
240
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
241
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
242
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
243
*pos=(char) ('0'+(char) part3);
221
244
return val_buffer;