18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include <boost/lexical_cast.hpp>
22
#include <drizzled/server_includes.h>
23
23
#include <drizzled/field/datetime.h>
24
24
#include <drizzled/error.h>
25
25
#include <drizzled/table.h>
26
#include <drizzled/temporal.h>
27
26
#include <drizzled/session.h>
29
#if defined(CMATH_NAMESPACE)
30
using namespace CMATH_NAMESPACE;
38
33
/****************************************************************************
40
35
** In string context: YYYY-MM-DD HH:MM:DD
41
36
** In number context: YYYYMMDDHHMMDD
37
** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
42
38
****************************************************************************/
44
40
int Field_datetime::store(const char *from,
46
42
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 (getTable() && getTable()->isDatabaseLowByteFirst())
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 boost::lexical_cast */
80
std::string tmp(boost::lexical_cast<std::string>(from));
82
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
85
return Field_datetime::store((int64_t) rint(from), false);
88
int Field_datetime::store(int64_t from, bool)
90
ASSERT_COLUMN_MARKED_FOR_WRITE;
92
* Try to create a DateTime from the supplied integer. Throw an error
93
* if unable to create a valid DateTime.
96
if (! temporal.from_int64_t(from))
98
/* Convert the integer to a string using boost::lexical_cast */
99
std::string tmp(boost::lexical_cast<std::string>(from));
101
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
106
* Because "from" may be a silly MySQL-like "datetime number" (like, oh, 101)
107
* we must here get the value of the DateTime as its *real* int64_t, after
108
* the conversion above has been done...yuck. God, save us.
111
temporal.to_int64_t(&int_value);
113
#ifdef WORDS_BIGENDIAN
114
if (getTable() && getTable()->isDatabaseLowByteFirst())
116
int8store(ptr, int_value);
120
int64_tstore(ptr, int_value);
124
int Field_datetime::store_time(type::Time <ime, type::timestamp_t)
128
temporal.set_years(ltime.year);
129
temporal.set_months(ltime.month);
130
temporal.set_days(ltime.day);
131
temporal.set_hours(ltime.hour);
132
temporal.set_minutes(ltime.minute);
133
temporal.set_seconds(ltime.second);
135
if (! temporal.is_valid())
137
char tmp_string[type::Time::MAX_STRING_LENGTH];
138
size_t tmp_string_len;
140
tmp_string_len= temporal.to_string(tmp_string, type::Time::MAX_STRING_LENGTH);
141
assert(tmp_string_len < type::Time::MAX_STRING_LENGTH);
142
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp_string);
147
temporal.to_int64_t(&int_value);
149
#ifdef WORDS_BIGENDIAN
150
if (getTable() && getTable()->isDatabaseLowByteFirst())
152
int8store(ptr, int_value);
156
int64_tstore(ptr, int_value);
161
double Field_datetime::val_real(void) const
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,
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);
181
double Field_datetime::val_real(void)
163
183
return (double) Field_datetime::val_int();
166
int64_t Field_datetime::val_int(void) const
186
int64_t Field_datetime::val_int(void)
170
ASSERT_COLUMN_MARKED_FOR_READ;
172
189
#ifdef WORDS_BIGENDIAN
173
if (getTable() && getTable()->isDatabaseLowByteFirst())
190
if (table && table->s->db_low_byte_first)
174
191
j=sint8korr(ptr);
182
String *Field_datetime::val_str(String *val_buffer, String *) const
199
String *Field_datetime::val_str(String *val_buffer,
184
val_buffer->alloc(DateTime::MAX_STRING_LENGTH);
185
val_buffer->length(DateTime::MAX_STRING_LENGTH);
188
ASSERT_COLUMN_MARKED_FOR_READ;
202
val_buffer->alloc(field_length);
203
val_buffer->length(field_length);
190
209
#ifdef WORDS_BIGENDIAN
191
if (getTable() && getTable()->isDatabaseLowByteFirst())
210
if (table && table->s->db_low_byte_first)
192
211
tmp=sint8korr(ptr);
195
214
int64_tget(tmp,ptr);
199
/* TODO: add an assert that this succeeds
200
* currently fails due to bug in allowing
201
* ALTER TABLE to add a datetime column that's
202
* not null without a default value.
204
dt.from_int64_t(tmp, false); /* NOTE: this does *NOT* attempt convertion
205
from formats such as 20090101 as
206
the stored value has already been
211
rlen= dt.to_string((char*)val_buffer->ptr(), DateTime::MAX_STRING_LENGTH);
212
assert((rlen+1) < DateTime::MAX_STRING_LENGTH);
214
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);
216
244
return val_buffer;
219
bool Field_datetime::get_date(type::Time <ime, uint32_t fuzzydate) const
247
bool Field_datetime::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzydate)
221
249
int64_t tmp=Field_datetime::val_int();
222
250
uint32_t part1,part2;
223
251
part1=(uint32_t) (tmp/INT64_C(1000000));
224
252
part2=(uint32_t) (tmp - (uint64_t) part1*INT64_C(1000000));
226
ltime.time_type= type::DRIZZLE_TIMESTAMP_DATETIME;
228
ltime.second_part= 0;
229
ltime.second= (int) (part2%100);
230
ltime.minute= (int) (part2/100%100);
231
ltime.hour= (int) (part2/10000);
232
ltime.day= (int) (part1%100);
233
ltime.month= (int) (part1/100%100);
234
ltime.year= (int) (part1/10000);
236
return (!(fuzzydate & TIME_FUZZY_DATE) && (!ltime.month || !ltime.day)) ? 1 : 0;
254
ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME;
256
ltime->second_part= 0;
257
ltime->second= (int) (part2%100);
258
ltime->minute= (int) (part2/100%100);
259
ltime->hour= (int) (part2/10000);
260
ltime->day= (int) (part1%100);
261
ltime->month= (int) (part1/100%100);
262
ltime->year= (int) (part1/10000);
263
return (!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ? 1 : 0;
239
bool Field_datetime::get_time(type::Time <ime) const
266
bool Field_datetime::get_time(DRIZZLE_TIME *ltime)
241
268
return Field_datetime::get_date(ltime,0);