1
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 MySQL
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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>
30
#include <drizzled/current_session.h>
41
/****************************************************************************
42
** Drizzle date type stored in 3 bytes
43
** In number context: YYYYMMDD
44
****************************************************************************/
47
Store string into a date field
52
len Length of date field
53
cs Character set (not used)
57
1 Value was cut during conversion
59
3 Datetime value that was cut (warning level NOTE)
60
This is used by opt_range.cc:get_mm_leaf(). Note that there is a
61
nearly-identical class Field_date doesn't ever return 3 from its
64
int Field_date::store(const char *from,
66
const CHARSET_INFO * const )
69
* Try to create a DateTime from the supplied string. Throw an error
70
* if unable to create a valid DateTime. A DateTime is used so that
71
* automatic conversion from the higher-storage DateTime can be used
72
* and matches on datetime format strings can occur.
74
ASSERT_COLUMN_MARKED_FOR_WRITE;
76
if (! temporal.from_string(from, (size_t) len))
78
my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), from);
81
/* Create the stored integer format. @TODO This should go away. Should be up to engine... */
82
uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
83
int4store(ptr, int_value);
87
int Field_date::store(double from)
89
ASSERT_COLUMN_MARKED_FOR_WRITE;
90
if (from < 0.0 || from > 99991231235959.0)
92
/* Convert the double to a string using stringstream */
95
ss.precision(18); /* 18 places should be fine for error display of double input. */
96
ss << from; ss >> tmp;
98
my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
101
return Field_date::store((int64_t) rint(from), false);
104
int Field_date::store(int64_t from, bool)
107
* Try to create a DateTime from the supplied integer. Throw an error
108
* if unable to create a valid DateTime.
110
ASSERT_COLUMN_MARKED_FOR_WRITE;
112
if (! temporal.from_int64_t(from))
114
/* Convert the integer to a string using boost::lexical_cast */
115
std::string tmp(boost::lexical_cast<std::string>(from));
117
my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
121
/* Create the stored integer format. @TODO This should go away. Should be up to engine... */
122
uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
123
int4store(ptr, int_value);
128
int Field_date::store_time(type::Time <ime,
129
type::timestamp_t time_type)
133
if (time_type == type::DRIZZLE_TIMESTAMP_DATE || time_type == type::DRIZZLE_TIMESTAMP_DATETIME)
135
tmp= ltime.year*10000 + ltime.month*100 + ltime.day;
137
Session *session= getTable() ? getTable()->in_use : current_session;
138
type::cut_t cut_error= type::VALID;
139
if (ltime.check(tmp != 0,
141
(session->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))), cut_error))
143
char buff[type::Time::MAX_STRING_LENGTH];
144
String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
145
ltime.convert(str, type::DRIZZLE_TIMESTAMP_DATE);
146
set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
147
str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
150
error= static_cast<int>(cut_error);
152
if (not error && ltime.time_type != type::DRIZZLE_TIMESTAMP_DATE &&
153
(ltime.hour || ltime.minute || ltime.second || ltime.second_part))
155
char buff[type::Time::MAX_STRING_LENGTH];
156
String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
158
set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE,
159
ER_WARN_DATA_TRUNCATED,
160
str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
168
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
176
double Field_date::val_real(void) const
178
return (double) Field_date::val_int();
181
int64_t Field_date::val_int(void) const
185
ASSERT_COLUMN_MARKED_FOR_READ;
192
String *Field_date::val_str(String *val_buffer, String *) const
194
val_buffer->alloc(field_length);
195
val_buffer->length(field_length);
196
uint32_t tmp=(uint32_t) uint4korr(ptr);
198
char *pos=(char*) val_buffer->ptr()+10;
200
ASSERT_COLUMN_MARKED_FOR_READ;
202
/* Open coded to get more speed */
203
*pos--=0; // End NULL
204
part=(int32_t) (tmp % 100);
205
*pos--= (char) ('0'+part%10);
206
*pos--= (char) ('0'+part/10);
208
part=(int32_t) (tmp/100%100);
209
*pos--= (char) ('0'+part%10);
210
*pos--= (char) ('0'+part/10);
212
part=(int32_t) (tmp/10000);
213
*pos--= (char) ('0'+part%10); part/=10;
214
*pos--= (char) ('0'+part%10); part/=10;
215
*pos--= (char) ('0'+part%10); part/=10;
216
*pos= (char) ('0'+part);
220
bool Field_date::get_date(type::Time <ime, uint32_t fuzzydate) const
222
uint32_t tmp=(uint32_t) uint4korr(ptr);
223
ltime.day= (int) (tmp%100);
224
ltime.month= (int) (tmp/100%100);
225
ltime.year= (int) (tmp/10000);
226
ltime.time_type= type::DRIZZLE_TIMESTAMP_DATE;
227
ltime.hour= ltime.minute= ltime.second= ltime.second_part= ltime.neg= 0;
229
return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime.month || !ltime.day)) ?
233
bool Field_date::get_time(type::Time <ime) const
235
return Field_date::get_date(ltime ,0);
238
int Field_date::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
241
a=(uint32_t) uint4korr(a_ptr);
242
b=(uint32_t) uint4korr(b_ptr);
243
return (a < b) ? -1 : (a > b) ? 1 : 0;
246
void Field_date::sort_string(unsigned char *to,uint32_t )
254
void Field_date::sql_type(String &res) const
256
res.set_ascii(STRING_WITH_LEN("date"));
259
} /* namespace drizzled */