~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/date.cc

edit

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include <config.h>
 
21
#include "config.h"
22
22
#include <boost/lexical_cast.hpp>
23
23
 
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>
 
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"
31
30
 
32
31
#include <math.h>
33
32
 
75
74
  DateTime temporal;
76
75
  if (! temporal.from_string(from, (size_t) len))
77
76
  {
78
 
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), from);
 
77
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), from);
79
78
    return 2;
80
79
  }
81
80
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
95
94
    ss.precision(18); /* 18 places should be fine for error display of double input. */
96
95
    ss << from; ss >> tmp;
97
96
 
98
 
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
97
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
99
98
    return 2;
100
99
  }
101
100
  return Field_date::store((int64_t) rint(from), false);
114
113
    /* Convert the integer to a string using boost::lexical_cast */
115
114
    std::string tmp(boost::lexical_cast<std::string>(from)); 
116
115
 
117
 
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
116
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
118
117
    return 2;
119
118
  }
120
119
 
121
120
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
122
121
  uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
123
122
  int4store(ptr, int_value);
124
 
 
125
123
  return 0;
126
124
}
127
125
 
128
 
int Field_date::store_time(type::Time &ltime,
129
 
                           type::timestamp_t time_type)
 
126
int Field_date::store_time(type::Time *ltime,
 
127
                              enum enum_drizzle_timestamp_type time_type)
130
128
{
131
129
  long tmp;
132
130
  int error= 0;
133
 
  if (time_type == type::DRIZZLE_TIMESTAMP_DATE || time_type == type::DRIZZLE_TIMESTAMP_DATETIME)
 
131
  if (time_type == DRIZZLE_TIMESTAMP_DATE ||
 
132
      time_type == DRIZZLE_TIMESTAMP_DATETIME)
134
133
  {
135
 
    tmp= ltime.year*10000 + ltime.month*100 + ltime.day;
136
 
 
137
 
    Session *session= getTable() ? getTable()->in_use : current_session;
138
 
    type::cut_t cut_error= type::VALID;
139
 
    if (ltime.check(tmp != 0,
140
 
                     (TIME_FUZZY_DATE |
141
 
                      (session->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))), cut_error))
 
134
    tmp= ltime->year*10000 + ltime->month*100 + ltime->day;
 
135
    if (check_date(ltime, tmp != 0,
 
136
                   (TIME_FUZZY_DATE |
 
137
                    (current_session->variables.sql_mode &
 
138
                     (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))), &error))
142
139
    {
143
 
      char buff[type::Time::MAX_STRING_LENGTH];
 
140
      char buff[MAX_DATE_STRING_REP_LENGTH];
144
141
      String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
145
 
      ltime.convert(str, type::DRIZZLE_TIMESTAMP_DATE);
 
142
      make_date(ltime, &str);
146
143
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
147
 
                           str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
 
144
                           str.ptr(), str.length(), DRIZZLE_TIMESTAMP_DATE, 1);
148
145
    }
149
 
 
150
 
    error= static_cast<int>(cut_error);
151
 
 
152
 
    if (not error && ltime.time_type != type::DRIZZLE_TIMESTAMP_DATE &&
153
 
        (ltime.hour || ltime.minute || ltime.second || ltime.second_part))
 
146
    if (!error && ltime->time_type != DRIZZLE_TIMESTAMP_DATE &&
 
147
        (ltime->hour || ltime->minute || ltime->second || ltime->second_part))
154
148
    {
155
 
      char buff[type::Time::MAX_STRING_LENGTH];
 
149
      char buff[MAX_DATE_STRING_REP_LENGTH];
156
150
      String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
157
 
      ltime.convert(str);
 
151
      make_datetime(ltime, &str);
158
152
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE,
159
153
                           ER_WARN_DATA_TRUNCATED,
160
 
                           str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
 
154
                           str.ptr(), str.length(), DRIZZLE_TIMESTAMP_DATE, 1);
161
155
      error= 3;
162
156
    }
163
157
  }
167
161
    error= 1;
168
162
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
169
163
  }
170
 
 
171
164
  int4store(ptr,tmp);
172
 
 
173
165
  return error;
174
166
}
175
167
 
176
 
double Field_date::val_real(void) const
 
168
double Field_date::val_real(void)
177
169
{
178
170
  return (double) Field_date::val_int();
179
171
}
180
172
 
181
 
int64_t Field_date::val_int(void) const
 
173
int64_t Field_date::val_int(void)
182
174
{
183
175
  uint32_t j;
184
176
 
189
181
  return (int64_t) j;
190
182
}
191
183
 
192
 
String *Field_date::val_str(String *val_buffer, String *) const
 
184
String *Field_date::val_str(String *val_buffer, String *)
193
185
{
194
186
  val_buffer->alloc(field_length);
195
187
  val_buffer->length(field_length);
217
209
  return val_buffer;
218
210
}
219
211
 
220
 
bool Field_date::get_date(type::Time &ltime, uint32_t fuzzydate) const
 
212
bool Field_date::get_date(type::Time *ltime,uint32_t fuzzydate)
221
213
{
222
214
  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;
228
 
 
229
 
  return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime.month || !ltime.day)) ?
 
215
  ltime->day=           (int) (tmp%100);
 
216
  ltime->month=         (int) (tmp/100%100);
 
217
  ltime->year=          (int) (tmp/10000);
 
218
  ltime->time_type= DRIZZLE_TIMESTAMP_DATE;
 
219
  ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
 
220
  return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ?
230
221
          1 : 0);
231
222
}
232
223
 
233
 
bool Field_date::get_time(type::Time &ltime) const
 
224
bool Field_date::get_time(type::Time *ltime)
234
225
{
235
 
  return Field_date::get_date(ltime ,0);
 
226
  return Field_date::get_date(ltime,0);
236
227
}
237
228
 
238
229
int Field_date::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)