18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include <boost/lexical_cast.hpp>
23
#include "drizzled/field/datetime.h"
24
#include "drizzled/error.h"
25
#include "drizzled/table.h"
26
#include "drizzled/temporal.h"
27
#include "drizzled/session.h"
22
#include <drizzled/server_includes.h>
23
#include <drizzled/field/datetime.h>
38
25
/****************************************************************************
40
27
** In string context: YYYY-MM-DD HH:MM:DD
41
28
** In number context: YYYYMMDDHHMMDD
29
** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
42
30
****************************************************************************/
44
32
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 (getTable() && getTable()->getShare()->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 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()->getShare()->db_low_byte_first)
116
int8store(ptr, int_value);
120
int64_tstore(ptr, int_value);
124
int Field_datetime::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
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[MAX_DATE_STRING_REP_LENGTH];
138
size_t tmp_string_len;
140
tmp_string_len= temporal.to_string(tmp_string, MAX_DATE_STRING_REP_LENGTH);
141
assert(tmp_string_len < MAX_DATE_STRING_REP_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()->getShare()->db_low_byte_first)
152
int8store(ptr, int_value);
156
int64_tstore(ptr, int_value);
34
const CHARSET_INFO * const cs __attribute__((unused)))
36
DRIZZLE_TIME time_tmp;
39
enum enum_drizzle_timestamp_type func_res;
40
Session *session= table ? table->in_use : current_session;
42
func_res= str_to_datetime(from, len, &time_tmp,
44
(session->variables.sql_mode &
45
(MODE_NO_ZERO_DATE | MODE_INVALID_DATES))),
47
if ((int) func_res > (int) DRIZZLE_TIMESTAMP_ERROR)
48
tmp= TIME_to_uint64_t_datetime(&time_tmp);
50
error= 1; // Fix if invalid zero date
53
set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
54
ER_WARN_DATA_OUT_OF_RANGE,
55
from, len, DRIZZLE_TIMESTAMP_DATETIME, 1);
57
#ifdef WORDS_BIGENDIAN
58
if (table && table->s->db_low_byte_first)
64
int64_tstore(ptr,tmp);
69
int Field_datetime::store(double nr)
72
if (nr < 0.0 || nr > 99991231235959.0)
74
set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
75
ER_WARN_DATA_OUT_OF_RANGE,
76
nr, DRIZZLE_TIMESTAMP_DATETIME);
80
error|= Field_datetime::store((int64_t) rint(nr), false);
85
int Field_datetime::store(int64_t nr,
86
bool unsigned_val __attribute__((unused)))
88
DRIZZLE_TIME not_used;
90
int64_t initial_nr= nr;
91
Session *session= table ? table->in_use : current_session;
93
nr= number_to_datetime(nr, ¬_used, (TIME_FUZZY_DATE |
94
(session->variables.sql_mode &
96
MODE_INVALID_DATES))), &error);
98
if (nr == INT64_C(-1))
105
set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
106
error == 2 ? ER_WARN_DATA_OUT_OF_RANGE :
107
ER_WARN_DATA_TRUNCATED, initial_nr,
108
DRIZZLE_TIMESTAMP_DATETIME, 1);
110
#ifdef WORDS_BIGENDIAN
111
if (table && table->s->db_low_byte_first)
117
int64_tstore(ptr,nr);
122
int Field_datetime::store_time(DRIZZLE_TIME *ltime,
123
enum enum_drizzle_timestamp_type time_type)
128
We don't perform range checking here since values stored in TIME
129
structure always fit into DATETIME range.
131
if (time_type == DRIZZLE_TIMESTAMP_DATE ||
132
time_type == DRIZZLE_TIMESTAMP_DATETIME)
134
tmp=((ltime->year*10000L+ltime->month*100+ltime->day)*INT64_C(1000000)+
135
(ltime->hour*10000L+ltime->minute*100+ltime->second));
136
if (check_date(ltime, tmp != 0,
138
(current_session->variables.sql_mode &
139
(MODE_NO_ZERO_DATE | MODE_INVALID_DATES))), &error))
141
char buff[MAX_DATE_STRING_REP_LENGTH];
142
String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
143
make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
144
set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
145
str.ptr(), str.length(), DRIZZLE_TIMESTAMP_DATETIME,1);
152
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
154
#ifdef WORDS_BIGENDIAN
155
if (table && table->s->db_low_byte_first)
161
int64_tstore(ptr,tmp);
165
bool Field_datetime::send_binary(Protocol *protocol)
168
Field_datetime::get_date(&tm, TIME_FUZZY_DATE);
169
return protocol->store(&tm);
160
173
double Field_datetime::val_real(void)
181
191
String *Field_datetime::val_str(String *val_buffer,
192
String *val_ptr __attribute__((unused)))
184
val_buffer->alloc(DateTime::MAX_STRING_LENGTH);
185
val_buffer->length(DateTime::MAX_STRING_LENGTH);
188
ASSERT_COLUMN_MARKED_FOR_READ;
194
val_buffer->alloc(field_length);
195
val_buffer->length(field_length);
190
201
#ifdef WORDS_BIGENDIAN
191
if (getTable() && getTable()->getShare()->db_low_byte_first)
202
if (table && table->s->db_low_byte_first)
192
203
tmp=sint8korr(ptr);
195
206
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);
209
Avoid problem with slow int64_t arithmetic and sprintf
212
part1=(long) (tmp/INT64_C(1000000));
213
part2=(long) (tmp - (uint64_t) part1*INT64_C(1000000));
215
pos=(char*) val_buffer->ptr() + MAX_DATETIME_WIDTH;
217
*pos--= (char) ('0'+(char) (part2%10)); part2/=10;
218
*pos--= (char) ('0'+(char) (part2%10)); part3= (int) (part2 / 10);
220
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
221
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
223
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
224
*pos--= (char) ('0'+(char) part3);
226
*pos--= (char) ('0'+(char) (part1%10)); part1/=10;
227
*pos--= (char) ('0'+(char) (part1%10)); part1/=10;
229
*pos--= (char) ('0'+(char) (part1%10)); part1/=10;
230
*pos--= (char) ('0'+(char) (part1%10)); part3= (int) (part1/10);
232
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
233
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
234
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
235
*pos=(char) ('0'+(char) part3);
216
236
return val_buffer;