18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include <drizzled/server_includes.h>
23
#include <drizzled/field/date.h>
24
#include <drizzled/error.h>
25
#include <drizzled/table.h>
26
#include <drizzled/session.h>
30
#if defined(CMATH_NAMESPACE)
31
using namespace CMATH_NAMESPACE;
22
#include <boost/lexical_cast.hpp>
24
#include "drizzled/field/date.h"
25
#include "drizzled/error.h"
26
#include "drizzled/table.h"
27
#include "drizzled/temporal.h"
28
#include "drizzled/session.h"
29
#include "drizzled/time_functions.h"
34
40
/****************************************************************************
35
41
** Drizzle date type stored in 3 bytes
54
60
nearly-identical class Field_date doesn't ever return 3 from its
58
63
int Field_date::store(const char *from,
60
const CHARSET_INFO * const cs __attribute__((unused)))
65
Session *session= table ? table->in_use : current_session;
66
enum enum_drizzle_timestamp_type ret;
67
if ((ret= str_to_datetime(from, len, &l_time,
69
(session->variables.sql_mode &
70
(MODE_NO_ZERO_DATE | MODE_INVALID_DATES))),
71
&error)) <= DRIZZLE_TIMESTAMP_ERROR)
78
tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
79
if (!error && (ret != DRIZZLE_TIMESTAMP_DATE) &&
80
(l_time.hour || l_time.minute || l_time.second || l_time.second_part))
81
error= 3; // Datetime was cut (note)
85
set_datetime_warning(error == 3 ? DRIZZLE_ERROR::WARN_LEVEL_NOTE :
86
DRIZZLE_ERROR::WARN_LEVEL_WARN,
87
ER_WARN_DATA_TRUNCATED,
88
from, len, DRIZZLE_TIMESTAMP_DATE, 1);
95
int Field_date::store(double nr)
97
if (nr < 0.0 || nr > 99991231235959.0)
99
int3store(ptr,(int32_t) 0);
100
set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
101
ER_WARN_DATA_TRUNCATED, nr, DRIZZLE_TIMESTAMP_DATE);
104
return Field_date::store((int64_t) rint(nr), false);
108
int Field_date::store(int64_t nr,
109
bool unsigned_val __attribute__((unused)))
114
Session *session= table ? table->in_use : current_session;
115
if (number_to_datetime(nr, &l_time,
117
(session->variables.sql_mode &
118
(MODE_NO_ZERO_DATE | MODE_INVALID_DATES))),
119
&error) == INT64_C(-1))
125
tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
127
if (!error && l_time.time_type != DRIZZLE_TIMESTAMP_DATE &&
128
(l_time.hour || l_time.minute || l_time.second || l_time.second_part))
132
set_datetime_warning(error == 3 ? DRIZZLE_ERROR::WARN_LEVEL_NOTE :
133
DRIZZLE_ERROR::WARN_LEVEL_WARN,
135
ER_WARN_DATA_OUT_OF_RANGE : ER_WARN_DATA_TRUNCATED,
136
nr,DRIZZLE_TIMESTAMP_DATE, 1);
65
const CHARSET_INFO * const )
68
* Try to create a DateTime from the supplied string. Throw an error
69
* if unable to create a valid DateTime. A DateTime is used so that
70
* automatic conversion from the higher-storage DateTime can be used
71
* and matches on datetime format strings can occur.
73
ASSERT_COLUMN_MARKED_FOR_WRITE;
75
if (! temporal.from_string(from, (size_t) len))
77
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), from);
80
/* Create the stored integer format. @TODO This should go away. Should be up to engine... */
81
uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
82
int4store(ptr, int_value);
86
int Field_date::store(double from)
88
ASSERT_COLUMN_MARKED_FOR_WRITE;
89
if (from < 0.0 || from > 99991231235959.0)
91
/* Convert the double to a string using stringstream */
94
ss.precision(18); /* 18 places should be fine for error display of double input. */
95
ss << from; ss >> tmp;
97
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
100
return Field_date::store((int64_t) rint(from), false);
103
int Field_date::store(int64_t from, bool)
106
* Try to create a DateTime from the supplied integer. Throw an error
107
* if unable to create a valid DateTime.
109
ASSERT_COLUMN_MARKED_FOR_WRITE;
111
if (! temporal.from_int64_t(from))
113
/* Convert the integer to a string using boost::lexical_cast */
114
std::string tmp(boost::lexical_cast<std::string>(from));
116
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
120
/* Create the stored integer format. @TODO This should go away. Should be up to engine... */
121
uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
122
int4store(ptr, int_value);
143
126
int Field_date::store_time(DRIZZLE_TIME *ltime,
144
127
enum enum_drizzle_timestamp_type time_type)
179
162
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
186
bool Field_date::send_binary(Protocol *protocol)
189
Field_date::get_date(&tm,0);
190
return protocol->store_date(&tm);
194
168
double Field_date::val_real(void)
196
170
return (double) Field_date::val_int();
200
173
int64_t Field_date::val_int(void)
202
uint32_t j= uint3korr(ptr);
203
j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
177
ASSERT_COLUMN_MARKED_FOR_READ;
204
181
return (int64_t) j;
208
String *Field_date::val_str(String *val_buffer,
209
String *val_ptr __attribute__((unused)))
184
String *Field_date::val_str(String *val_buffer, String *)
211
186
val_buffer->alloc(field_length);
212
187
val_buffer->length(field_length);
213
uint32_t tmp=(uint32_t) uint3korr(ptr);
188
uint32_t tmp=(uint32_t) uint4korr(ptr);
215
190
char *pos=(char*) val_buffer->ptr()+10;
192
ASSERT_COLUMN_MARKED_FOR_READ;
217
194
/* Open coded to get more speed */
218
195
*pos--=0; // End NULL
219
part=(int) (tmp & 31);
220
*pos--= (char) ('0'+part%10);
221
*pos--= (char) ('0'+part/10);
223
part=(int) (tmp >> 5 & 15);
224
*pos--= (char) ('0'+part%10);
225
*pos--= (char) ('0'+part/10);
227
part=(int) (tmp >> 9);
196
part=(int32_t) (tmp % 100);
197
*pos--= (char) ('0'+part%10);
198
*pos--= (char) ('0'+part/10);
200
part=(int32_t) (tmp/100%100);
201
*pos--= (char) ('0'+part%10);
202
*pos--= (char) ('0'+part/10);
204
part=(int32_t) (tmp/10000);
228
205
*pos--= (char) ('0'+part%10); part/=10;
229
206
*pos--= (char) ('0'+part%10); part/=10;
230
207
*pos--= (char) ('0'+part%10); part/=10;
232
209
return val_buffer;
236
212
bool Field_date::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
238
uint32_t tmp=(uint32_t) uint3korr(ptr);
239
ltime->day= tmp & 31;
240
ltime->month= (tmp >> 5) & 15;
241
ltime->year= (tmp >> 9);
214
uint32_t tmp=(uint32_t) uint4korr(ptr);
215
ltime->day= (int) (tmp%100);
216
ltime->month= (int) (tmp/100%100);
217
ltime->year= (int) (tmp/10000);
242
218
ltime->time_type= DRIZZLE_TIMESTAMP_DATE;
243
219
ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
244
220
return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ?
249
224
bool Field_date::get_time(DRIZZLE_TIME *ltime)
251
226
return Field_date::get_date(ltime,0);
255
229
int Field_date::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
258
a=(uint32_t) uint3korr(a_ptr);
259
b=(uint32_t) uint3korr(b_ptr);
232
a=(uint32_t) uint4korr(a_ptr);
233
b=(uint32_t) uint4korr(b_ptr);
260
234
return (a < b) ? -1 : (a > b) ? 1 : 0;
264
void Field_date::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
237
void Field_date::sort_string(unsigned char *to,uint32_t )
272
245
void Field_date::sql_type(String &res) const
274
247
res.set_ascii(STRING_WITH_LEN("date"));
250
} /* namespace drizzled */