~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/datetime.cc

  • Committer: Brian Aker
  • Date: 2009-03-25 18:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 963.
  • Revision ID: brian@tangent.org-20090325182415-opf2720c1hidtfgk
Cut down on shutdown loop of plugins (cutting stuff out in order to simplify
old lock 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
#include "drizzled/server_includes.h"
23
22
#include "drizzled/field/datetime.h"
24
23
#include "drizzled/error.h"
25
24
#include "drizzled/table.h"
26
25
#include "drizzled/temporal.h"
27
26
#include "drizzled/session.h"
28
27
 
29
 
#include <math.h>
30
 
 
31
28
#include <sstream>
32
29
#include <string>
33
30
 
34
31
 
35
 
namespace drizzled
36
 
{
37
 
 
38
32
/****************************************************************************
39
33
** datetime type
40
34
** In string context: YYYY-MM-DD HH:MM:DD
41
35
** In number context: YYYYMMDDHHMMDD
 
36
** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
42
37
****************************************************************************/
43
38
 
44
39
int Field_datetime::store(const char *from,
45
40
                          uint32_t len,
46
41
                          const CHARSET_INFO * const )
47
42
{
48
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
49
43
  /* 
50
44
   * Try to create a DateTime from the supplied string.  Throw an error
51
45
   * if unable to create a valid DateTime.  
52
46
   */
53
 
  DateTime temporal;
 
47
  drizzled::DateTime temporal;
54
48
  if (! temporal.from_string(from, (size_t) len))
55
49
  {
56
50
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), from);
61
55
  temporal.to_int64_t(&int_value);
62
56
 
63
57
#ifdef WORDS_BIGENDIAN
64
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
58
  if (table && table->s->db_low_byte_first)
65
59
  {
66
60
    int8store(ptr, int_value);
67
61
  }
73
67
 
74
68
int Field_datetime::store(double from)
75
69
{
76
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
77
70
  if (from < 0.0 || from > 99991231235959.0)
78
71
  {
79
 
    /* Convert the double to a string using boost::lexical_cast */
80
 
    std::string tmp(boost::lexical_cast<std::string>(from));
 
72
    /* Convert the double to a string using stringstream */
 
73
    std::stringstream ss;
 
74
    std::string tmp;
 
75
    ss.precision(18); /* 18 places should be fine for error display of double input. */
 
76
    ss << from; ss >> tmp;
81
77
 
82
78
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
83
79
    return 2;
87
83
 
88
84
int Field_datetime::store(int64_t from, bool)
89
85
{
90
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
91
86
  /* 
92
87
   * Try to create a DateTime from the supplied integer.  Throw an error
93
88
   * if unable to create a valid DateTime.  
94
89
   */
95
 
  DateTime temporal;
 
90
  drizzled::DateTime temporal;
96
91
  if (! temporal.from_int64_t(from))
97
92
  {
98
 
    /* Convert the integer to a string using boost::lexical_cast */
99
 
    std::string tmp(boost::lexical_cast<std::string>(from));
 
93
    /* Convert the integer to a string using stringstream */
 
94
    std::stringstream ss;
 
95
    std::string tmp;
 
96
    ss << from; ss >> tmp;
100
97
 
101
98
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
102
99
    return 2;
111
108
  temporal.to_int64_t(&int_value);
112
109
 
113
110
#ifdef WORDS_BIGENDIAN
114
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
111
  if (table && table->s->db_low_byte_first)
115
112
  {
116
113
    int8store(ptr, int_value);
117
114
  }
123
120
 
124
121
int Field_datetime::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
125
122
{
126
 
  DateTime temporal;
 
123
  drizzled::DateTime temporal;
127
124
 
128
125
  temporal.set_years(ltime->year);
129
126
  temporal.set_months(ltime->month);
137
134
    char tmp_string[MAX_DATE_STRING_REP_LENGTH];
138
135
    size_t tmp_string_len;
139
136
 
140
 
    tmp_string_len= temporal.to_string(tmp_string, MAX_DATE_STRING_REP_LENGTH);
141
 
    assert(tmp_string_len < MAX_DATE_STRING_REP_LENGTH);
 
137
    temporal.to_string(tmp_string, &tmp_string_len);
142
138
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp_string);
143
139
    return 1;
144
140
  }
147
143
  temporal.to_int64_t(&int_value);
148
144
 
149
145
#ifdef WORDS_BIGENDIAN
150
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
146
  if (table && table->s->db_low_byte_first)
151
147
  {
152
148
    int8store(ptr, int_value);
153
149
  }
165
161
int64_t Field_datetime::val_int(void)
166
162
{
167
163
  int64_t j;
168
 
 
169
 
  ASSERT_COLUMN_MARKED_FOR_READ;
170
 
 
171
164
#ifdef WORDS_BIGENDIAN
172
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
165
  if (table && table->s->db_low_byte_first)
173
166
    j=sint8korr(ptr);
174
167
  else
175
168
#endif
181
174
String *Field_datetime::val_str(String *val_buffer,
182
175
                                String *)
183
176
{
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;
 
177
  val_buffer->alloc(field_length);
 
178
  val_buffer->length(field_length);
 
179
  uint64_t tmp;
 
180
  long part1,part2;
 
181
  char *pos;
 
182
  int part3;
189
183
 
190
184
#ifdef WORDS_BIGENDIAN
191
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
185
  if (table && table->s->db_low_byte_first)
192
186
    tmp=sint8korr(ptr);
193
187
  else
194
188
#endif
195
189
    int64_tget(tmp,ptr);
196
190
 
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
 
 
 
191
  /*
 
192
    Avoid problem with slow int64_t arithmetic and sprintf
 
193
  */
 
194
 
 
195
  part1=(long) (tmp/INT64_C(1000000));
 
196
  part2=(long) (tmp - (uint64_t) part1*INT64_C(1000000));
 
197
 
 
198
  pos=(char*) val_buffer->ptr() + MAX_DATETIME_WIDTH;
 
199
  *pos--=0;
 
200
  *pos--= (char) ('0'+(char) (part2%10)); part2/=10;
 
201
  *pos--= (char) ('0'+(char) (part2%10)); part3= (int) (part2 / 10);
 
202
  *pos--= ':';
 
203
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
204
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
205
  *pos--= ':';
 
206
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
207
  *pos--= (char) ('0'+(char) part3);
 
208
  *pos--= ' ';
 
209
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
210
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
211
  *pos--= '-';
 
212
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
213
  *pos--= (char) ('0'+(char) (part1%10)); part3= (int) (part1/10);
 
214
  *pos--= '-';
 
215
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
216
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
217
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
218
  *pos=(char) ('0'+(char) part3);
216
219
  return val_buffer;
217
220
}
218
221
 
244
247
{
245
248
  int64_t a,b;
246
249
#ifdef WORDS_BIGENDIAN
247
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
250
  if (table && table->s->db_low_byte_first)
248
251
  {
249
252
    a=sint8korr(a_ptr);
250
253
    b=sint8korr(b_ptr);
262
265
void Field_datetime::sort_string(unsigned char *to,uint32_t )
263
266
{
264
267
#ifdef WORDS_BIGENDIAN
265
 
  if (!getTable() || !getTable()->getShare()->db_low_byte_first)
 
268
  if (!table || !table->s->db_low_byte_first)
266
269
  {
267
270
    to[0] = ptr[0];
268
271
    to[1] = ptr[1];
293
296
  res.set_ascii(STRING_WITH_LEN("datetime"));
294
297
}
295
298
 
296
 
} /* namespace drizzled */