~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/date.cc

  • Committer: Brian Aker
  • Date: 2010-05-28 01:36:09 UTC
  • Revision ID: brian@gaz-20100528013609-lvsd6znyufrpmddk
Rollup patch for hiding tableshare.

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>
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>
 
21
#include "config.h"
 
22
#include "drizzled/field/date.h"
 
23
#include "drizzled/error.h"
 
24
#include "drizzled/table.h"
 
25
#include "drizzled/temporal.h"
 
26
#include "drizzled/session.h"
 
27
#include "drizzled/time_functions.h"
31
28
 
32
29
#include <math.h>
33
30
 
65
62
                         uint32_t len,
66
63
                         const CHARSET_INFO * const )
67
64
{
 
65
#ifdef NOTDEFINED
 
66
  long tmp;
 
67
  DRIZZLE_TIME l_time;
 
68
  int error;
 
69
  Session *session= table ? table->in_use : current_session;
 
70
  enum enum_drizzle_timestamp_type ret;
 
71
  if ((ret= str_to_datetime(from, len, &l_time,
 
72
                            (TIME_FUZZY_DATE |
 
73
                             (session->variables.sql_mode &
 
74
                              (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))),
 
75
                            &error)) <= DRIZZLE_TIMESTAMP_ERROR)
 
76
  {
 
77
    tmp= 0;
 
78
    error= 2;
 
79
  }
 
80
  else
 
81
  {
 
82
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
 
83
    if (!error && (ret != DRIZZLE_TIMESTAMP_DATE) &&
 
84
        (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
 
85
      error= 3;                                 // Datetime was cut (note)
 
86
  }
 
87
 
 
88
  if (error)
 
89
    set_datetime_warning(error == 3 ? DRIZZLE_ERROR::WARN_LEVEL_NOTE :
 
90
                         DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
91
                         ER_WARN_DATA_TRUNCATED,
 
92
                         from, len, DRIZZLE_TIMESTAMP_DATE, 1);
 
93
 
 
94
  int3store(ptr, tmp);
 
95
#endif /* NOTDEFINED */
68
96
  /* 
69
97
   * Try to create a DateTime from the supplied string.  Throw an error
70
98
   * if unable to create a valid DateTime.  A DateTime is used so that
75
103
  DateTime temporal;
76
104
  if (! temporal.from_string(from, (size_t) len))
77
105
  {
78
 
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), from);
 
106
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), from);
79
107
    return 2;
80
108
  }
81
109
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
82
 
  uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
83
 
  int4store(ptr, int_value);
 
110
  uint32_t int_value= (temporal.years() * 16 * 32) + (temporal.months() * 32) + temporal.days();
 
111
  int3store(ptr, int_value);
84
112
  return 0;
85
113
}
86
114
 
95
123
    ss.precision(18); /* 18 places should be fine for error display of double input. */
96
124
    ss << from; ss >> tmp;
97
125
 
98
 
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
126
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
99
127
    return 2;
100
128
  }
101
129
  return Field_date::store((int64_t) rint(from), false);
111
139
  DateTime temporal;
112
140
  if (! temporal.from_int64_t(from))
113
141
  {
114
 
    /* Convert the integer to a string using boost::lexical_cast */
115
 
    std::string tmp(boost::lexical_cast<std::string>(from)); 
 
142
    /* Convert the integer to a string using stringstream */
 
143
    std::stringstream ss;
 
144
    std::string tmp;
 
145
    ss << from; ss >> tmp;
116
146
 
117
 
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
147
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
118
148
    return 2;
119
149
  }
120
150
 
121
151
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
122
 
  uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
123
 
  int4store(ptr, int_value);
124
 
 
 
152
  uint32_t int_value= (temporal.years() * 16 * 32) + (temporal.months() * 32) + temporal.days();
 
153
  int3store(ptr, int_value);
125
154
  return 0;
126
155
}
127
156
 
128
 
int Field_date::store_time(type::Time &ltime,
129
 
                           type::timestamp_t time_type)
 
157
int Field_date::store_time(DRIZZLE_TIME *ltime,
 
158
                              enum enum_drizzle_timestamp_type time_type)
130
159
{
131
160
  long tmp;
132
161
  int error= 0;
133
 
  if (time_type == type::DRIZZLE_TIMESTAMP_DATE || time_type == type::DRIZZLE_TIMESTAMP_DATETIME)
 
162
  if (time_type == DRIZZLE_TIMESTAMP_DATE ||
 
163
      time_type == DRIZZLE_TIMESTAMP_DATETIME)
134
164
  {
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))
 
165
    tmp=ltime->year*16*32+ltime->month*32+ltime->day;
 
166
    if (check_date(ltime, tmp != 0,
 
167
                   (TIME_FUZZY_DATE |
 
168
                    (current_session->variables.sql_mode &
 
169
                     (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))), &error))
142
170
    {
143
 
      char buff[type::Time::MAX_STRING_LENGTH];
 
171
      char buff[MAX_DATE_STRING_REP_LENGTH];
144
172
      String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
145
 
      ltime.convert(str, type::DRIZZLE_TIMESTAMP_DATE);
 
173
      make_date(ltime, &str);
146
174
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
147
 
                           str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
 
175
                           str.ptr(), str.length(), DRIZZLE_TIMESTAMP_DATE, 1);
148
176
    }
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))
 
177
    if (!error && ltime->time_type != DRIZZLE_TIMESTAMP_DATE &&
 
178
        (ltime->hour || ltime->minute || ltime->second || ltime->second_part))
154
179
    {
155
 
      char buff[type::Time::MAX_STRING_LENGTH];
 
180
      char buff[MAX_DATE_STRING_REP_LENGTH];
156
181
      String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
157
 
      ltime.convert(str);
 
182
      make_datetime(ltime, &str);
158
183
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE,
159
184
                           ER_WARN_DATA_TRUNCATED,
160
 
                           str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
 
185
                           str.ptr(), str.length(), DRIZZLE_TIMESTAMP_DATE, 1);
161
186
      error= 3;
162
187
    }
163
188
  }
167
192
    error= 1;
168
193
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
169
194
  }
170
 
 
171
 
  int4store(ptr,tmp);
172
 
 
 
195
  int3store(ptr,tmp);
173
196
  return error;
174
197
}
175
198
 
176
 
double Field_date::val_real(void) const
 
199
double Field_date::val_real(void)
177
200
{
178
201
  return (double) Field_date::val_int();
179
202
}
180
203
 
181
 
int64_t Field_date::val_int(void) const
 
204
int64_t Field_date::val_int(void)
182
205
{
183
206
  uint32_t j;
184
207
 
185
208
  ASSERT_COLUMN_MARKED_FOR_READ;
186
209
 
187
 
  j= uint4korr(ptr);
 
210
  j= uint3korr(ptr);
 
211
  j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
188
212
 
189
213
  return (int64_t) j;
190
214
}
191
215
 
192
 
String *Field_date::val_str(String *val_buffer, String *) const
 
216
String *Field_date::val_str(String *val_buffer,
 
217
                               String *)
193
218
{
194
219
  val_buffer->alloc(field_length);
195
220
  val_buffer->length(field_length);
196
 
  uint32_t tmp=(uint32_t) uint4korr(ptr);
197
 
  int32_t part;
 
221
  uint32_t tmp=(uint32_t) uint3korr(ptr);
 
222
  int part;
198
223
  char *pos=(char*) val_buffer->ptr()+10;
199
224
 
200
225
  ASSERT_COLUMN_MARKED_FOR_READ;
201
226
 
202
227
  /* Open coded to get more speed */
203
228
  *pos--=0;                                     // End NULL
204
 
  part=(int32_t) (tmp % 100);
205
 
  *pos--= (char) ('0'+part%10);
206
 
  *pos--= (char) ('0'+part/10);
207
 
  *pos--= '-';
208
 
  part=(int32_t) (tmp/100%100);
209
 
  *pos--= (char) ('0'+part%10);
210
 
  *pos--= (char) ('0'+part/10);
211
 
  *pos--= '-';
212
 
  part=(int32_t) (tmp/10000);
 
229
  part=(int) (tmp & 31);
 
230
  *pos--= (char) ('0'+part%10);
 
231
  *pos--= (char) ('0'+part/10);
 
232
  *pos--= '-';
 
233
  part=(int) (tmp >> 5 & 15);
 
234
  *pos--= (char) ('0'+part%10);
 
235
  *pos--= (char) ('0'+part/10);
 
236
  *pos--= '-';
 
237
  part=(int) (tmp >> 9);
213
238
  *pos--= (char) ('0'+part%10); part/=10;
214
239
  *pos--= (char) ('0'+part%10); part/=10;
215
240
  *pos--= (char) ('0'+part%10); part/=10;
217
242
  return val_buffer;
218
243
}
219
244
 
220
 
bool Field_date::get_date(type::Time &ltime, uint32_t fuzzydate) const
 
245
bool Field_date::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
221
246
{
222
 
  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)) ?
 
247
  uint32_t tmp=(uint32_t) uint3korr(ptr);
 
248
  ltime->day=   tmp & 31;
 
249
  ltime->month= (tmp >> 5) & 15;
 
250
  ltime->year=  (tmp >> 9);
 
251
  ltime->time_type= DRIZZLE_TIMESTAMP_DATE;
 
252
  ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
 
253
  return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ?
230
254
          1 : 0);
231
255
}
232
256
 
233
 
bool Field_date::get_time(type::Time &ltime) const
 
257
bool Field_date::get_time(DRIZZLE_TIME *ltime)
234
258
{
235
 
  return Field_date::get_date(ltime ,0);
 
259
  return Field_date::get_date(ltime,0);
236
260
}
237
261
 
238
262
int Field_date::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
239
263
{
240
264
  uint32_t a,b;
241
 
  a=(uint32_t) uint4korr(a_ptr);
242
 
  b=(uint32_t) uint4korr(b_ptr);
 
265
  a=(uint32_t) uint3korr(a_ptr);
 
266
  b=(uint32_t) uint3korr(b_ptr);
243
267
  return (a < b) ? -1 : (a > b) ? 1 : 0;
244
268
}
245
269
 
246
270
void Field_date::sort_string(unsigned char *to,uint32_t )
247
271
{
248
 
  to[0] = ptr[3];
249
 
  to[1] = ptr[2];
250
 
  to[2] = ptr[1];
251
 
  to[3] = ptr[0];
 
272
  to[0] = ptr[2];
 
273
  to[1] = ptr[1];
 
274
  to[2] = ptr[0];
252
275
}
253
276
 
254
277
void Field_date::sql_type(String &res) const