~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/datetime.cc

  • Committer: Brian Aker
  • Date: 2009-02-10 00:14:40 UTC
  • Revision ID: brian@tangent.org-20090210001440-qjg8eofh3h93064b
Adding Multi-threaded Scheduler into the system.

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>
22
 
#include <boost/lexical_cast.hpp>
 
21
 
 
22
#include <drizzled/server_includes.h>
23
23
#include <drizzled/field/datetime.h>
24
24
#include <drizzled/error.h>
25
25
#include <drizzled/table.h>
26
 
#include <drizzled/temporal.h>
27
26
#include <drizzled/session.h>
28
 
 
29
 
#include <math.h>
30
 
 
31
 
#include <sstream>
32
 
#include <string>
33
 
 
34
 
 
35
 
namespace drizzled
36
 
{
 
27
#include CMATH_H
 
28
 
 
29
#if defined(CMATH_NAMESPACE)
 
30
using namespace CMATH_NAMESPACE;
 
31
#endif
37
32
 
38
33
/****************************************************************************
39
34
** datetime type
40
35
** In string context: YYYY-MM-DD HH:MM:DD
41
36
** In number context: YYYYMMDDHHMMDD
 
37
** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
42
38
****************************************************************************/
43
39
 
44
40
int Field_datetime::store(const char *from,
45
41
                          uint32_t len,
46
42
                          const CHARSET_INFO * const )
47
43
{
48
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
49
 
  /* 
50
 
   * Try to create a DateTime from the supplied string.  Throw an error
51
 
   * if unable to create a valid DateTime.  
52
 
   */
53
 
  DateTime temporal;
54
 
  if (! temporal.from_string(from, (size_t) len))
55
 
  {
56
 
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), from);
57
 
    return 2;
58
 
  }
59
 
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
60
 
  int64_t int_value;
61
 
  temporal.to_int64_t(&int_value);
62
 
 
63
 
#ifdef WORDS_BIGENDIAN
64
 
  if (getTable() && getTable()->isDatabaseLowByteFirst())
65
 
  {
66
 
    int8store(ptr, int_value);
67
 
  }
68
 
  else
69
 
#endif
70
 
    int64_tstore(ptr, int_value);
71
 
  return 0;
72
 
}
73
 
 
74
 
int Field_datetime::store(double from)
75
 
{
76
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
77
 
  if (from < 0.0 || from > 99991231235959.0)
78
 
  {
79
 
    /* Convert the double to a string using boost::lexical_cast */
80
 
    std::string tmp(boost::lexical_cast<std::string>(from));
81
 
 
82
 
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
83
 
    return 2;
84
 
  }
85
 
  return Field_datetime::store((int64_t) rint(from), false);
86
 
}
87
 
 
88
 
int Field_datetime::store(int64_t from, bool)
89
 
{
90
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
91
 
  /* 
92
 
   * Try to create a DateTime from the supplied integer.  Throw an error
93
 
   * if unable to create a valid DateTime.  
94
 
   */
95
 
  DateTime temporal;
96
 
  if (! temporal.from_int64_t(from))
97
 
  {
98
 
    /* Convert the integer to a string using boost::lexical_cast */
99
 
    std::string tmp(boost::lexical_cast<std::string>(from));
100
 
 
101
 
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
102
 
    return 2;
103
 
  }
104
 
 
105
 
  /* 
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.
109
 
   */
110
 
  int64_t int_value;
111
 
  temporal.to_int64_t(&int_value);
112
 
 
113
 
#ifdef WORDS_BIGENDIAN
114
 
  if (getTable() && getTable()->isDatabaseLowByteFirst())
115
 
  {
116
 
    int8store(ptr, int_value);
117
 
  }
118
 
  else
119
 
#endif
120
 
    int64_tstore(ptr, int_value);
121
 
  return 0;
122
 
}
123
 
 
124
 
int Field_datetime::store_time(type::Time &ltime, type::timestamp_t)
125
 
{
126
 
  DateTime temporal;
127
 
 
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);
134
 
 
135
 
  if (! temporal.is_valid())
136
 
  {
137
 
    char tmp_string[type::Time::MAX_STRING_LENGTH];
138
 
    size_t tmp_string_len;
139
 
 
140
 
    tmp_string_len= temporal.to_string(tmp_string, type::Time::MAX_STRING_LENGTH);
141
 
    assert(tmp_string_len < type::Time::MAX_STRING_LENGTH);
142
 
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp_string);
143
 
    return 1;
144
 
  }
145
 
 
146
 
  int64_t int_value;
147
 
  temporal.to_int64_t(&int_value);
148
 
 
149
 
#ifdef WORDS_BIGENDIAN
150
 
  if (getTable() && getTable()->isDatabaseLowByteFirst())
151
 
  {
152
 
    int8store(ptr, int_value);
153
 
  }
154
 
  else
155
 
#endif
156
 
    int64_tstore(ptr, int_value);
157
 
 
158
 
  return 0;
159
 
}
160
 
 
161
 
double Field_datetime::val_real(void) const
 
44
  DRIZZLE_TIME time_tmp;
 
45
  int error;
 
46
  uint64_t tmp= 0;
 
47
  enum enum_drizzle_timestamp_type func_res;
 
48
  Session *session= table ? table->in_use : current_session;
 
49
 
 
50
  func_res= str_to_datetime(from, len, &time_tmp,
 
51
                            (TIME_FUZZY_DATE |
 
52
                             (session->variables.sql_mode &
 
53
                              (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))),
 
54
                            &error);
 
55
  if ((int) func_res > (int) DRIZZLE_TIMESTAMP_ERROR)
 
56
    tmp= TIME_to_uint64_t_datetime(&time_tmp);
 
57
  else
 
58
    error= 1;                                 // Fix if invalid zero date
 
59
 
 
60
  if (error)
 
61
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
62
                         ER_WARN_DATA_OUT_OF_RANGE,
 
63
                         from, len, DRIZZLE_TIMESTAMP_DATETIME, 1);
 
64
 
 
65
#ifdef WORDS_BIGENDIAN
 
66
  if (table && table->s->db_low_byte_first)
 
67
  {
 
68
    int8store(ptr,tmp);
 
69
  }
 
70
  else
 
71
#endif
 
72
    int64_tstore(ptr,tmp);
 
73
  return error;
 
74
}
 
75
 
 
76
 
 
77
int Field_datetime::store(double nr)
 
78
{
 
79
  int error= 0;
 
80
  if (nr < 0.0 || nr > 99991231235959.0)
 
81
  {
 
82
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
83
                         ER_WARN_DATA_OUT_OF_RANGE,
 
84
                         nr, DRIZZLE_TIMESTAMP_DATETIME);
 
85
    nr= 0.0;
 
86
    error= 1;
 
87
  }
 
88
  error|= Field_datetime::store((int64_t) rint(nr), false);
 
89
  return error;
 
90
}
 
91
 
 
92
 
 
93
int Field_datetime::store(int64_t nr,
 
94
                          bool )
 
95
{
 
96
  DRIZZLE_TIME not_used;
 
97
  int error;
 
98
  int64_t initial_nr= nr;
 
99
  Session *session= table ? table->in_use : current_session;
 
100
 
 
101
  nr= number_to_datetime(nr, &not_used, (TIME_FUZZY_DATE |
 
102
                                         (session->variables.sql_mode &
 
103
                                          (MODE_NO_ZERO_DATE |
 
104
                                           MODE_INVALID_DATES))), &error);
 
105
 
 
106
  if (nr == INT64_C(-1))
 
107
  {
 
108
    nr= 0;
 
109
    error= 2;
 
110
  }
 
111
 
 
112
  if (error)
 
113
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
114
                         error == 2 ? ER_WARN_DATA_OUT_OF_RANGE :
 
115
                         ER_WARN_DATA_TRUNCATED, initial_nr,
 
116
                         DRIZZLE_TIMESTAMP_DATETIME, 1);
 
117
 
 
118
#ifdef WORDS_BIGENDIAN
 
119
  if (table && table->s->db_low_byte_first)
 
120
  {
 
121
    int8store(ptr,nr);
 
122
  }
 
123
  else
 
124
#endif
 
125
    int64_tstore(ptr,nr);
 
126
  return error;
 
127
}
 
128
 
 
129
 
 
130
int Field_datetime::store_time(DRIZZLE_TIME *ltime,
 
131
                               enum enum_drizzle_timestamp_type time_type)
 
132
{
 
133
  int64_t tmp;
 
134
  int error= 0;
 
135
  /*
 
136
    We don't perform range checking here since values stored in TIME
 
137
    structure always fit into DATETIME range.
 
138
  */
 
139
  if (time_type == DRIZZLE_TIMESTAMP_DATE ||
 
140
      time_type == DRIZZLE_TIMESTAMP_DATETIME)
 
141
  {
 
142
    tmp=((ltime->year*10000L+ltime->month*100+ltime->day)*INT64_C(1000000)+
 
143
         (ltime->hour*10000L+ltime->minute*100+ltime->second));
 
144
    if (check_date(ltime, tmp != 0,
 
145
                   (TIME_FUZZY_DATE |
 
146
                    (current_session->variables.sql_mode &
 
147
                     (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))), &error))
 
148
    {
 
149
      char buff[MAX_DATE_STRING_REP_LENGTH];
 
150
      String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
 
151
      make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
 
152
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
 
153
                           str.ptr(), str.length(), DRIZZLE_TIMESTAMP_DATETIME,1);
 
154
    }
 
155
  }
 
156
  else
 
157
  {
 
158
    tmp=0;
 
159
    error= 1;
 
160
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
 
161
  }
 
162
#ifdef WORDS_BIGENDIAN
 
163
  if (table && table->s->db_low_byte_first)
 
164
  {
 
165
    int8store(ptr,tmp);
 
166
  }
 
167
  else
 
168
#endif
 
169
    int64_tstore(ptr,tmp);
 
170
  return error;
 
171
}
 
172
 
 
173
bool Field_datetime::send_binary(Protocol *protocol)
 
174
{
 
175
  DRIZZLE_TIME tm;
 
176
  Field_datetime::get_date(&tm, TIME_FUZZY_DATE);
 
177
  return protocol->store(&tm);
 
178
}
 
179
 
 
180
 
 
181
double Field_datetime::val_real(void)
162
182
{
163
183
  return (double) Field_datetime::val_int();
164
184
}
165
185
 
166
 
int64_t Field_datetime::val_int(void) const
 
186
int64_t Field_datetime::val_int(void)
167
187
{
168
188
  int64_t j;
169
 
 
170
 
  ASSERT_COLUMN_MARKED_FOR_READ;
171
 
 
172
189
#ifdef WORDS_BIGENDIAN
173
 
  if (getTable() && getTable()->isDatabaseLowByteFirst())
 
190
  if (table && table->s->db_low_byte_first)
174
191
    j=sint8korr(ptr);
175
192
  else
176
193
#endif
179
196
}
180
197
 
181
198
 
182
 
String *Field_datetime::val_str(String *val_buffer, String *) const
 
199
String *Field_datetime::val_str(String *val_buffer,
 
200
                                String *)
183
201
{
184
 
  val_buffer->alloc(DateTime::MAX_STRING_LENGTH);
185
 
  val_buffer->length(DateTime::MAX_STRING_LENGTH);
186
 
  int64_t tmp;
187
 
 
188
 
  ASSERT_COLUMN_MARKED_FOR_READ;
 
202
  val_buffer->alloc(field_length);
 
203
  val_buffer->length(field_length);
 
204
  uint64_t tmp;
 
205
  long part1,part2;
 
206
  char *pos;
 
207
  int part3;
189
208
 
190
209
#ifdef WORDS_BIGENDIAN
191
 
  if (getTable() && getTable()->isDatabaseLowByteFirst())
 
210
  if (table && table->s->db_low_byte_first)
192
211
    tmp=sint8korr(ptr);
193
212
  else
194
213
#endif
195
214
    int64_tget(tmp,ptr);
196
215
 
197
 
  DateTime dt;
198
 
 
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.
203
 
   */
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
207
 
                                 converted.
208
 
                               */
209
 
 
210
 
  int rlen;
211
 
  rlen= dt.to_string((char*)val_buffer->ptr(), DateTime::MAX_STRING_LENGTH);
212
 
  assert((rlen+1) <  DateTime::MAX_STRING_LENGTH);
213
 
 
214
 
  val_buffer->length(rlen);
215
 
 
 
216
  /*
 
217
    Avoid problem with slow int64_t arithmetic and sprintf
 
218
  */
 
219
 
 
220
  part1=(long) (tmp/INT64_C(1000000));
 
221
  part2=(long) (tmp - (uint64_t) part1*INT64_C(1000000));
 
222
 
 
223
  pos=(char*) val_buffer->ptr() + MAX_DATETIME_WIDTH;
 
224
  *pos--=0;
 
225
  *pos--= (char) ('0'+(char) (part2%10)); part2/=10;
 
226
  *pos--= (char) ('0'+(char) (part2%10)); part3= (int) (part2 / 10);
 
227
  *pos--= ':';
 
228
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
229
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
230
  *pos--= ':';
 
231
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
232
  *pos--= (char) ('0'+(char) part3);
 
233
  *pos--= ' ';
 
234
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
235
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
236
  *pos--= '-';
 
237
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
238
  *pos--= (char) ('0'+(char) (part1%10)); part3= (int) (part1/10);
 
239
  *pos--= '-';
 
240
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
241
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
242
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
243
  *pos=(char) ('0'+(char) part3);
216
244
  return val_buffer;
217
245
}
218
246
 
219
 
bool Field_datetime::get_date(type::Time &ltime, uint32_t fuzzydate) const
 
247
bool Field_datetime::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzydate)
220
248
{
221
249
  int64_t tmp=Field_datetime::val_int();
222
250
  uint32_t part1,part2;
223
251
  part1=(uint32_t) (tmp/INT64_C(1000000));
224
252
  part2=(uint32_t) (tmp - (uint64_t) part1*INT64_C(1000000));
225
253
 
226
 
  ltime.time_type=      type::DRIZZLE_TIMESTAMP_DATETIME;
227
 
  ltime.neg=            0;
228
 
  ltime.second_part=    0;
229
 
  ltime.second= (int) (part2%100);
230
 
  ltime.minute= (int) (part2/100%100);
231
 
  ltime.hour=           (int) (part2/10000);
232
 
  ltime.day=            (int) (part1%100);
233
 
  ltime.month=  (int) (part1/100%100);
234
 
  ltime.year=           (int) (part1/10000);
235
 
 
236
 
  return (!(fuzzydate & TIME_FUZZY_DATE) && (!ltime.month || !ltime.day)) ? 1 : 0;
 
254
  ltime->time_type=     DRIZZLE_TIMESTAMP_DATETIME;
 
255
  ltime->neg=           0;
 
256
  ltime->second_part=   0;
 
257
  ltime->second=        (int) (part2%100);
 
258
  ltime->minute=        (int) (part2/100%100);
 
259
  ltime->hour=          (int) (part2/10000);
 
260
  ltime->day=           (int) (part1%100);
 
261
  ltime->month=         (int) (part1/100%100);
 
262
  ltime->year=          (int) (part1/10000);
 
263
  return (!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ? 1 : 0;
237
264
}
238
265
 
239
 
bool Field_datetime::get_time(type::Time &ltime) const
 
266
bool Field_datetime::get_time(DRIZZLE_TIME *ltime)
240
267
{
241
268
  return Field_datetime::get_date(ltime,0);
242
269
}
245
272
{
246
273
  int64_t a,b;
247
274
#ifdef WORDS_BIGENDIAN
248
 
  if (getTable() && getTable()->isDatabaseLowByteFirst())
 
275
  if (table && table->s->db_low_byte_first)
249
276
  {
250
277
    a=sint8korr(a_ptr);
251
278
    b=sint8korr(b_ptr);
263
290
void Field_datetime::sort_string(unsigned char *to,uint32_t )
264
291
{
265
292
#ifdef WORDS_BIGENDIAN
266
 
  if (not getTable() || not getTable()->isDatabaseLowByteFirst())
 
293
  if (!table || !table->s->db_low_byte_first)
267
294
  {
268
295
    to[0] = ptr[0];
269
296
    to[1] = ptr[1];
294
321
  res.set_ascii(STRING_WITH_LEN("datetime"));
295
322
}
296
323
 
297
 
} /* namespace drizzled */