~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/datetime.cc

  • Committer: Joe Daly
  • Date: 2010-01-06 02:20:42 UTC
  • mto: This revision was merged to the branch mainline in revision 1267.
  • Revision ID: skinny.moey@gmail.com-20100106022042-8ov23wc4aq8f9k7d
rename hash_algorithm to algorithm

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>
23
22
#include "drizzled/field/datetime.h"
24
23
#include "drizzled/error.h"
25
24
#include "drizzled/table.h"
32
31
#include <string>
33
32
 
34
33
 
35
 
namespace drizzled
36
 
{
37
 
 
38
34
/****************************************************************************
39
35
** datetime type
40
36
** In string context: YYYY-MM-DD HH:MM:DD
41
37
** In number context: YYYYMMDDHHMMDD
 
38
** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
42
39
****************************************************************************/
43
40
 
44
41
int Field_datetime::store(const char *from,
50
47
   * Try to create a DateTime from the supplied string.  Throw an error
51
48
   * if unable to create a valid DateTime.  
52
49
   */
53
 
  DateTime temporal;
 
50
  drizzled::DateTime temporal;
54
51
  if (! temporal.from_string(from, (size_t) len))
55
52
  {
56
53
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), from);
61
58
  temporal.to_int64_t(&int_value);
62
59
 
63
60
#ifdef WORDS_BIGENDIAN
64
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
61
  if (table && table->s->db_low_byte_first)
65
62
  {
66
63
    int8store(ptr, int_value);
67
64
  }
76
73
  ASSERT_COLUMN_MARKED_FOR_WRITE;
77
74
  if (from < 0.0 || from > 99991231235959.0)
78
75
  {
79
 
    /* Convert the double to a string using boost::lexical_cast */
80
 
    std::string tmp(boost::lexical_cast<std::string>(from));
 
76
    /* Convert the double to a string using stringstream */
 
77
    std::stringstream ss;
 
78
    std::string tmp;
 
79
    ss.precision(18); /* 18 places should be fine for error display of double input. */
 
80
    ss << from; ss >> tmp;
81
81
 
82
82
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
83
83
    return 2;
92
92
   * Try to create a DateTime from the supplied integer.  Throw an error
93
93
   * if unable to create a valid DateTime.  
94
94
   */
95
 
  DateTime temporal;
 
95
  drizzled::DateTime temporal;
96
96
  if (! temporal.from_int64_t(from))
97
97
  {
98
 
    /* Convert the integer to a string using boost::lexical_cast */
99
 
    std::string tmp(boost::lexical_cast<std::string>(from));
 
98
    /* Convert the integer to a string using stringstream */
 
99
    std::stringstream ss;
 
100
    std::string tmp;
 
101
    ss << from; ss >> tmp;
100
102
 
101
103
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
102
104
    return 2;
111
113
  temporal.to_int64_t(&int_value);
112
114
 
113
115
#ifdef WORDS_BIGENDIAN
114
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
116
  if (table && table->s->db_low_byte_first)
115
117
  {
116
118
    int8store(ptr, int_value);
117
119
  }
123
125
 
124
126
int Field_datetime::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
125
127
{
126
 
  DateTime temporal;
 
128
  drizzled::DateTime temporal;
127
129
 
128
130
  temporal.set_years(ltime->year);
129
131
  temporal.set_months(ltime->month);
147
149
  temporal.to_int64_t(&int_value);
148
150
 
149
151
#ifdef WORDS_BIGENDIAN
150
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
152
  if (table && table->s->db_low_byte_first)
151
153
  {
152
154
    int8store(ptr, int_value);
153
155
  }
169
171
  ASSERT_COLUMN_MARKED_FOR_READ;
170
172
 
171
173
#ifdef WORDS_BIGENDIAN
172
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
174
  if (table && table->s->db_low_byte_first)
173
175
    j=sint8korr(ptr);
174
176
  else
175
177
#endif
181
183
String *Field_datetime::val_str(String *val_buffer,
182
184
                                String *)
183
185
{
184
 
  val_buffer->alloc(DateTime::MAX_STRING_LENGTH);
185
 
  val_buffer->length(DateTime::MAX_STRING_LENGTH);
 
186
  val_buffer->alloc(drizzled::DateTime::MAX_STRING_LENGTH);
 
187
  val_buffer->length(drizzled::DateTime::MAX_STRING_LENGTH);
186
188
  int64_t tmp;
187
189
 
188
190
  ASSERT_COLUMN_MARKED_FOR_READ;
189
191
 
190
192
#ifdef WORDS_BIGENDIAN
191
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
193
  if (table && table->s->db_low_byte_first)
192
194
    tmp=sint8korr(ptr);
193
195
  else
194
196
#endif
195
197
    int64_tget(tmp,ptr);
196
198
 
197
 
  DateTime dt;
 
199
  drizzled::DateTime dt;
198
200
 
199
201
  /* TODO: add an assert that this succeeds
200
202
   * currently fails due to bug in allowing
202
204
   * not null without a default value.
203
205
   */
204
206
  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
 
                               */
 
207
                                        from formats such as 20090101 as
 
208
                                        the stored value has already been
 
209
                                        converted.
 
210
                               */
209
211
 
210
212
  int rlen;
211
 
  rlen= dt.to_string((char*)val_buffer->ptr(), DateTime::MAX_STRING_LENGTH);
212
 
  assert((rlen+1) <  DateTime::MAX_STRING_LENGTH);
 
213
  rlen= dt.to_string((char*)val_buffer->ptr(), drizzled::DateTime::MAX_STRING_LENGTH);
 
214
  assert((rlen+1) <  drizzled::DateTime::MAX_STRING_LENGTH);
213
215
 
214
216
  val_buffer->length(rlen);
215
217
 
244
246
{
245
247
  int64_t a,b;
246
248
#ifdef WORDS_BIGENDIAN
247
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
249
  if (table && table->s->db_low_byte_first)
248
250
  {
249
251
    a=sint8korr(a_ptr);
250
252
    b=sint8korr(b_ptr);
262
264
void Field_datetime::sort_string(unsigned char *to,uint32_t )
263
265
{
264
266
#ifdef WORDS_BIGENDIAN
265
 
  if (!getTable() || !getTable()->getShare()->db_low_byte_first)
 
267
  if (!table || !table->s->db_low_byte_first)
266
268
  {
267
269
    to[0] = ptr[0];
268
270
    to[1] = ptr[1];
293
295
  res.set_ascii(STRING_WITH_LEN("datetime"));
294
296
}
295
297
 
296
 
} /* namespace drizzled */