~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/datetime.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
#include "config.h"
 
22
#include <boost/lexical_cast.hpp>
22
23
#include "drizzled/field/datetime.h"
23
24
#include "drizzled/error.h"
24
25
#include "drizzled/table.h"
38
39
** datetime type
39
40
** In string context: YYYY-MM-DD HH:MM:DD
40
41
** In number context: YYYYMMDDHHMMDD
41
 
** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
42
42
****************************************************************************/
43
43
 
44
44
int Field_datetime::store(const char *from,
61
61
  temporal.to_int64_t(&int_value);
62
62
 
63
63
#ifdef WORDS_BIGENDIAN
64
 
  if (getTable() && getTable()->s->db_low_byte_first)
 
64
  if (getTable() && getTable()->isDatabaseLowByteFirst())
65
65
  {
66
66
    int8store(ptr, int_value);
67
67
  }
76
76
  ASSERT_COLUMN_MARKED_FOR_WRITE;
77
77
  if (from < 0.0 || from > 99991231235959.0)
78
78
  {
79
 
    /* Convert the double to a string using stringstream */
80
 
    std::stringstream ss;
81
 
    std::string tmp;
82
 
    ss.precision(18); /* 18 places should be fine for error display of double input. */
83
 
    ss << from; ss >> tmp;
 
79
    /* Convert the double to a string using boost::lexical_cast */
 
80
    std::string tmp(boost::lexical_cast<std::string>(from));
84
81
 
85
82
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
86
83
    return 2;
98
95
  DateTime temporal;
99
96
  if (! temporal.from_int64_t(from))
100
97
  {
101
 
    /* Convert the integer to a string using stringstream */
102
 
    std::stringstream ss;
103
 
    std::string tmp;
104
 
    ss << from; ss >> tmp;
 
98
    /* Convert the integer to a string using boost::lexical_cast */
 
99
    std::string tmp(boost::lexical_cast<std::string>(from));
105
100
 
106
101
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
107
102
    return 2;
116
111
  temporal.to_int64_t(&int_value);
117
112
 
118
113
#ifdef WORDS_BIGENDIAN
119
 
  if (getTable() && getTable()->s->db_low_byte_first)
 
114
  if (getTable() && getTable()->isDatabaseLowByteFirst())
120
115
  {
121
116
    int8store(ptr, int_value);
122
117
  }
126
121
  return 0;
127
122
}
128
123
 
129
 
int Field_datetime::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
 
124
int Field_datetime::store_time(type::Time &ltime, type::timestamp_t)
130
125
{
131
126
  DateTime temporal;
132
127
 
133
 
  temporal.set_years(ltime->year);
134
 
  temporal.set_months(ltime->month);
135
 
  temporal.set_days(ltime->day);
136
 
  temporal.set_hours(ltime->hour);
137
 
  temporal.set_minutes(ltime->minute);
138
 
  temporal.set_seconds(ltime->second);
 
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);
139
134
 
140
135
  if (! temporal.is_valid())
141
136
  {
142
 
    char tmp_string[MAX_DATE_STRING_REP_LENGTH];
 
137
    char tmp_string[type::Time::MAX_STRING_LENGTH];
143
138
    size_t tmp_string_len;
144
139
 
145
 
    tmp_string_len= temporal.to_string(tmp_string, MAX_DATE_STRING_REP_LENGTH);
146
 
    assert(tmp_string_len < MAX_DATE_STRING_REP_LENGTH);
 
140
    tmp_string_len= temporal.to_string(tmp_string, type::Time::MAX_STRING_LENGTH);
 
141
    assert(tmp_string_len < type::Time::MAX_STRING_LENGTH);
147
142
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp_string);
148
143
    return 1;
149
144
  }
152
147
  temporal.to_int64_t(&int_value);
153
148
 
154
149
#ifdef WORDS_BIGENDIAN
155
 
  if (getTable() && getTable()->s->db_low_byte_first)
 
150
  if (getTable() && getTable()->isDatabaseLowByteFirst())
156
151
  {
157
152
    int8store(ptr, int_value);
158
153
  }
159
154
  else
160
155
#endif
161
156
    int64_tstore(ptr, int_value);
 
157
 
162
158
  return 0;
163
159
}
164
160
 
174
170
  ASSERT_COLUMN_MARKED_FOR_READ;
175
171
 
176
172
#ifdef WORDS_BIGENDIAN
177
 
  if (getTable() && getTable()->s->db_low_byte_first)
 
173
  if (getTable() && getTable()->isDatabaseLowByteFirst())
178
174
    j=sint8korr(ptr);
179
175
  else
180
176
#endif
193
189
  ASSERT_COLUMN_MARKED_FOR_READ;
194
190
 
195
191
#ifdef WORDS_BIGENDIAN
196
 
  if (getTable() && getTable()->s->db_low_byte_first)
 
192
  if (getTable() && getTable()->isDatabaseLowByteFirst())
197
193
    tmp=sint8korr(ptr);
198
194
  else
199
195
#endif
207
203
   * not null without a default value.
208
204
   */
209
205
  dt.from_int64_t(tmp, false); /* NOTE: this does *NOT* attempt convertion
210
 
                                        from formats such as 20090101 as
211
 
                                        the stored value has already been
212
 
                                        converted.
213
 
                               */
 
206
                                 from formats such as 20090101 as
 
207
                                 the stored value has already been
 
208
                                 converted.
 
209
                               */
214
210
 
215
211
  int rlen;
216
212
  rlen= dt.to_string((char*)val_buffer->ptr(), DateTime::MAX_STRING_LENGTH);
221
217
  return val_buffer;
222
218
}
223
219
 
224
 
bool Field_datetime::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzydate)
 
220
bool Field_datetime::get_date(type::Time &ltime, uint32_t fuzzydate)
225
221
{
226
222
  int64_t tmp=Field_datetime::val_int();
227
223
  uint32_t part1,part2;
228
224
  part1=(uint32_t) (tmp/INT64_C(1000000));
229
225
  part2=(uint32_t) (tmp - (uint64_t) part1*INT64_C(1000000));
230
226
 
231
 
  ltime->time_type=     DRIZZLE_TIMESTAMP_DATETIME;
232
 
  ltime->neg=           0;
233
 
  ltime->second_part=   0;
234
 
  ltime->second=        (int) (part2%100);
235
 
  ltime->minute=        (int) (part2/100%100);
236
 
  ltime->hour=          (int) (part2/10000);
237
 
  ltime->day=           (int) (part1%100);
238
 
  ltime->month=         (int) (part1/100%100);
239
 
  ltime->year=          (int) (part1/10000);
240
 
  return (!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ? 1 : 0;
 
227
  ltime.time_type=      type::DRIZZLE_TIMESTAMP_DATETIME;
 
228
  ltime.neg=            0;
 
229
  ltime.second_part=    0;
 
230
  ltime.second= (int) (part2%100);
 
231
  ltime.minute= (int) (part2/100%100);
 
232
  ltime.hour=           (int) (part2/10000);
 
233
  ltime.day=            (int) (part1%100);
 
234
  ltime.month=  (int) (part1/100%100);
 
235
  ltime.year=           (int) (part1/10000);
 
236
 
 
237
  return (!(fuzzydate & TIME_FUZZY_DATE) && (!ltime.month || !ltime.day)) ? 1 : 0;
241
238
}
242
239
 
243
 
bool Field_datetime::get_time(DRIZZLE_TIME *ltime)
 
240
bool Field_datetime::get_time(type::Time &ltime)
244
241
{
245
242
  return Field_datetime::get_date(ltime,0);
246
243
}
249
246
{
250
247
  int64_t a,b;
251
248
#ifdef WORDS_BIGENDIAN
252
 
  if (getTable() && getTable()->s->db_low_byte_first)
 
249
  if (getTable() && getTable()->isDatabaseLowByteFirst())
253
250
  {
254
251
    a=sint8korr(a_ptr);
255
252
    b=sint8korr(b_ptr);
267
264
void Field_datetime::sort_string(unsigned char *to,uint32_t )
268
265
{
269
266
#ifdef WORDS_BIGENDIAN
270
 
  if (!getTable() || !getTable()->s->db_low_byte_first)
 
267
  if (not getTable() || not getTable()->isDatabaseLowByteFirst())
271
268
  {
272
269
    to[0] = ptr[0];
273
270
    to[1] = ptr[1];