18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
#ifdef USE_PRAGMA_IMPLEMENTATION
22
#pragma implementation // gcc: Class implementation
25
#include <drizzled/server_includes.h>
26
#include <drizzled/field/date.h>
21
#include "drizzled/server_includes.h"
22
#include "drizzled/field/date.h"
23
#include "drizzled/error.h"
24
#include "drizzled/table.h"
25
#include "drizzled/temporal.h"
26
#include "drizzled/session.h"
28
32
/****************************************************************************
30
** This is identical to the old date type, but stored on 3 bytes instead of 4
33
** Drizzle date type stored in 3 bytes
31
34
** In number context: YYYYMMDD
32
35
****************************************************************************/
49
52
nearly-identical class Field_date doesn't ever return 3 from its
53
int Field_newdate::store(const char *from,
55
int Field_date::store(const char *from,
55
const CHARSET_INFO * const cs __attribute__((unused)))
57
const CHARSET_INFO * const )
58
61
DRIZZLE_TIME l_time;
60
THD *thd= table ? table->in_use : current_thd;
63
Session *session= table ? table->in_use : current_session;
61
64
enum enum_drizzle_timestamp_type ret;
62
65
if ((ret= str_to_datetime(from, len, &l_time,
64
(thd->variables.sql_mode &
67
(session->variables.sql_mode &
65
68
(MODE_NO_ZERO_DATE | MODE_INVALID_DATES))),
66
69
&error)) <= DRIZZLE_TIMESTAMP_ERROR)
83
86
from, len, DRIZZLE_TIMESTAMP_DATE, 1);
85
88
int3store(ptr, tmp);
90
int Field_newdate::store(double nr)
92
if (nr < 0.0 || nr > 99991231235959.0)
94
int3store(ptr,(int32_t) 0);
95
set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
96
ER_WARN_DATA_TRUNCATED, nr, DRIZZLE_TIMESTAMP_DATE);
99
return Field_newdate::store((int64_t) rint(nr), false);
103
int Field_newdate::store(int64_t nr,
104
bool unsigned_val __attribute__((unused)))
109
THD *thd= table ? table->in_use : current_thd;
110
if (number_to_datetime(nr, &l_time,
112
(thd->variables.sql_mode &
113
(MODE_NO_ZERO_DATE | MODE_INVALID_DATES))),
114
&error) == INT64_C(-1))
120
tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
122
if (!error && l_time.time_type != DRIZZLE_TIMESTAMP_DATE &&
123
(l_time.hour || l_time.minute || l_time.second || l_time.second_part))
127
set_datetime_warning(error == 3 ? DRIZZLE_ERROR::WARN_LEVEL_NOTE :
128
DRIZZLE_ERROR::WARN_LEVEL_WARN,
130
ER_WARN_DATA_OUT_OF_RANGE : ER_WARN_DATA_TRUNCATED,
131
nr,DRIZZLE_TIMESTAMP_DATE, 1);
138
int Field_newdate::store_time(DRIZZLE_TIME *ltime,
89
#endif /* NOTDEFINED */
91
* Try to create a DateTime from the supplied string. Throw an error
92
* if unable to create a valid DateTime. A DateTime is used so that
93
* automatic conversion from the higher-storage DateTime can be used
94
* and matches on datetime format strings can occur.
96
drizzled::DateTime temporal;
97
if (! temporal.from_string(from, (size_t) len))
99
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), from);
102
/* Create the stored integer format. @TODO This should go away. Should be up to engine... */
103
uint32_t int_value= (temporal.years() * 16 * 32) + (temporal.months() * 32) + temporal.days();
104
int3store(ptr, int_value);
108
int Field_date::store(double from)
110
if (from < 0.0 || from > 99991231235959.0)
112
/* Convert the double to a string using stringstream */
113
std::stringstream ss;
115
ss.precision(18); /* 18 places should be fine for error display of double input. */
116
ss << from; ss >> tmp;
118
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
121
return Field_date::store((int64_t) rint(from), false);
124
int Field_date::store(int64_t from, bool)
127
* Try to create a DateTime from the supplied integer. Throw an error
128
* if unable to create a valid DateTime.
130
drizzled::DateTime temporal;
131
if (! temporal.from_int64_t(from))
133
/* Convert the integer to a string using stringstream */
134
std::stringstream ss;
136
ss << from; ss >> tmp;
138
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
142
/* Create the stored integer format. @TODO This should go away. Should be up to engine... */
143
uint32_t int_value= (temporal.years() * 16 * 32) + (temporal.months() * 32) + temporal.days();
144
int3store(ptr, int_value);
148
int Field_date::store_time(DRIZZLE_TIME *ltime,
139
149
enum enum_drizzle_timestamp_type time_type)
146
156
tmp=ltime->year*16*32+ltime->month*32+ltime->day;
147
157
if (check_date(ltime, tmp != 0,
148
158
(TIME_FUZZY_DATE |
149
(current_thd->variables.sql_mode &
159
(current_session->variables.sql_mode &
150
160
(MODE_NO_ZERO_DATE | MODE_INVALID_DATES))), &error))
152
162
char buff[MAX_DATE_STRING_REP_LENGTH];
153
163
String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
154
make_date((DATE_TIME_FORMAT *) 0, ltime, &str);
164
make_date(ltime, &str);
155
165
set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
156
166
str.ptr(), str.length(), DRIZZLE_TIMESTAMP_DATE, 1);
161
171
char buff[MAX_DATE_STRING_REP_LENGTH];
162
172
String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
163
make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
173
make_datetime(ltime, &str);
164
174
set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE,
165
175
ER_WARN_DATA_TRUNCATED,
166
176
str.ptr(), str.length(), DRIZZLE_TIMESTAMP_DATE, 1);
181
bool Field_newdate::send_binary(Protocol *protocol)
184
Field_newdate::get_date(&tm,0);
185
return protocol->store_date(&tm);
189
double Field_newdate::val_real(void)
191
return (double) Field_newdate::val_int();
195
int64_t Field_newdate::val_int(void)
190
double Field_date::val_real(void)
192
return (double) Field_date::val_int();
195
int64_t Field_date::val_int(void)
197
197
uint32_t j= uint3korr(ptr);
198
198
j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
199
199
return (int64_t) j;
203
String *Field_newdate::val_str(String *val_buffer,
204
String *val_ptr __attribute__((unused)))
202
String *Field_date::val_str(String *val_buffer,
206
205
val_buffer->alloc(field_length);
207
206
val_buffer->length(field_length);