~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/date.cc

  • Committer: Padraig O'Sullivan
  • Date: 2009-03-21 20:26:28 UTC
  • mto: (960.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 961.
  • Revision ID: osullivan.padraig@gmail.com-20090321202628-nh6qsi825m4d4av6
Removing the queues.[h,cc] files from the mysys directory. The only place
where they are needed now is in the MyISAM storage engine. Thus, I moved the
files there and updated the files in the MyISAM storage engine
appropriately.

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
 
#ifdef USE_PRAGMA_IMPLEMENTATION
22
 
#pragma implementation                          // gcc: Class implementation
23
 
#endif
24
 
 
25
 
#include <drizzled/server_includes.h>
26
 
#include <drizzled/field/date.h>
 
21
#include "drizzled/server_includes.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
 
 
28
#include <sstream>
 
29
#include <string>
 
30
 
27
31
 
28
32
/****************************************************************************
29
 
** The new date type
30
 
** This is identical to the old date type, but stored on 3 bytes instead of 4
 
33
** Drizzle date type stored in 3 bytes
31
34
** In number context: YYYYMMDD
32
35
****************************************************************************/
33
36
 
35
38
  Store string into a date field
36
39
 
37
40
  SYNOPSIS
38
 
    Field_newdate::store()
 
41
    Field_date::store()
39
42
    from                Date string
40
43
    len                 Length of date field
41
44
    cs                  Character set (not used)
49
52
       nearly-identical class Field_date doesn't ever return 3 from its
50
53
       store function.
51
54
*/
52
 
 
53
 
int Field_newdate::store(const char *from,
 
55
int Field_date::store(const char *from,
54
56
                         uint32_t len,
55
 
                         const CHARSET_INFO * const cs __attribute__((unused)))
 
57
                         const CHARSET_INFO * const )
56
58
{
 
59
#ifdef NOTDEFINED
57
60
  long tmp;
58
61
  DRIZZLE_TIME l_time;
59
62
  int error;
60
 
  THD *thd= table ? table->in_use : current_thd;
 
63
  Session *session= table ? table->in_use : current_session;
61
64
  enum enum_drizzle_timestamp_type ret;
62
65
  if ((ret= str_to_datetime(from, len, &l_time,
63
66
                            (TIME_FUZZY_DATE |
64
 
                             (thd->variables.sql_mode &
 
67
                             (session->variables.sql_mode &
65
68
                              (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))),
66
69
                            &error)) <= DRIZZLE_TIMESTAMP_ERROR)
67
70
  {
83
86
                         from, len, DRIZZLE_TIMESTAMP_DATE, 1);
84
87
 
85
88
  int3store(ptr, tmp);
86
 
  return error;
87
 
}
88
 
 
89
 
 
90
 
int Field_newdate::store(double nr)
91
 
{
92
 
  if (nr < 0.0 || nr > 99991231235959.0)
93
 
  {
94
 
    int3store(ptr,(int32_t) 0);
95
 
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
96
 
                         ER_WARN_DATA_TRUNCATED, nr, DRIZZLE_TIMESTAMP_DATE);
97
 
    return 1;
98
 
  }
99
 
  return Field_newdate::store((int64_t) rint(nr), false);
100
 
}
101
 
 
102
 
 
103
 
int Field_newdate::store(int64_t nr,
104
 
                         bool unsigned_val __attribute__((unused)))
105
 
{
106
 
  DRIZZLE_TIME l_time;
107
 
  int64_t tmp;
108
 
  int error;
109
 
  THD *thd= table ? table->in_use : current_thd;
110
 
  if (number_to_datetime(nr, &l_time,
111
 
                         (TIME_FUZZY_DATE |
112
 
                          (thd->variables.sql_mode &
113
 
                           (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))),
114
 
                         &error) == INT64_C(-1))
115
 
  {
116
 
    tmp= 0L;
117
 
    error= 2;
118
 
  }
119
 
  else
120
 
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
121
 
 
122
 
  if (!error && l_time.time_type != DRIZZLE_TIMESTAMP_DATE &&
123
 
      (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
124
 
    error= 3;
125
 
 
126
 
  if (error)
127
 
    set_datetime_warning(error == 3 ? DRIZZLE_ERROR::WARN_LEVEL_NOTE :
128
 
                         DRIZZLE_ERROR::WARN_LEVEL_WARN,
129
 
                         error == 2 ? 
130
 
                         ER_WARN_DATA_OUT_OF_RANGE : ER_WARN_DATA_TRUNCATED,
131
 
                         nr,DRIZZLE_TIMESTAMP_DATE, 1);
132
 
 
133
 
  int3store(ptr,tmp);
134
 
  return error;
135
 
}
136
 
 
137
 
 
138
 
int Field_newdate::store_time(DRIZZLE_TIME *ltime,
 
89
#endif /* NOTDEFINED */
 
90
  /* 
 
91
   * Try to create a DateTime from the supplied string.  Throw an error
 
92
   * if unable to create a valid DateTime.  A DateTime is used so that
 
93
   * automatic conversion from the higher-storage DateTime can be used
 
94
   * and matches on datetime format strings can occur.
 
95
   */
 
96
  drizzled::DateTime temporal;
 
97
  if (! temporal.from_string(from, (size_t) len))
 
98
  {
 
99
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), from);
 
100
    return 2;
 
101
  }
 
102
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
 
103
  uint32_t int_value= (temporal.years() * 16 * 32) + (temporal.months() * 32) + temporal.days();
 
104
  int3store(ptr, int_value);
 
105
  return 0;
 
106
}
 
107
 
 
108
int Field_date::store(double from)
 
109
{
 
110
  if (from < 0.0 || from > 99991231235959.0)
 
111
  {
 
112
    /* Convert the double to a string using stringstream */
 
113
    std::stringstream ss;
 
114
    std::string tmp;
 
115
    ss.precision(18); /* 18 places should be fine for error display of double input. */
 
116
    ss << from; ss >> tmp;
 
117
 
 
118
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
119
    return 2;
 
120
  }
 
121
  return Field_date::store((int64_t) rint(from), false);
 
122
}
 
123
 
 
124
int Field_date::store(int64_t from, bool)
 
125
{
 
126
  /* 
 
127
   * Try to create a DateTime from the supplied integer.  Throw an error
 
128
   * if unable to create a valid DateTime.  
 
129
   */
 
130
  drizzled::DateTime temporal;
 
131
  if (! temporal.from_int64_t(from))
 
132
  {
 
133
    /* Convert the integer to a string using stringstream */
 
134
    std::stringstream ss;
 
135
    std::string tmp;
 
136
    ss << from; ss >> tmp;
 
137
 
 
138
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
139
    return 2;
 
140
  }
 
141
 
 
142
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
 
143
  uint32_t int_value= (temporal.years() * 16 * 32) + (temporal.months() * 32) + temporal.days();
 
144
  int3store(ptr, int_value);
 
145
  return 0;
 
146
}
 
147
 
 
148
int Field_date::store_time(DRIZZLE_TIME *ltime,
139
149
                              enum enum_drizzle_timestamp_type time_type)
140
150
{
141
151
  long tmp;
146
156
    tmp=ltime->year*16*32+ltime->month*32+ltime->day;
147
157
    if (check_date(ltime, tmp != 0,
148
158
                   (TIME_FUZZY_DATE |
149
 
                    (current_thd->variables.sql_mode &
 
159
                    (current_session->variables.sql_mode &
150
160
                     (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))), &error))
151
161
    {
152
162
      char buff[MAX_DATE_STRING_REP_LENGTH];
153
163
      String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
154
 
      make_date((DATE_TIME_FORMAT *) 0, ltime, &str);
 
164
      make_date(ltime, &str);
155
165
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
156
166
                           str.ptr(), str.length(), DRIZZLE_TIMESTAMP_DATE, 1);
157
167
    }
160
170
    {
161
171
      char buff[MAX_DATE_STRING_REP_LENGTH];
162
172
      String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
163
 
      make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
 
173
      make_datetime(ltime, &str);
164
174
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE,
165
175
                           ER_WARN_DATA_TRUNCATED,
166
176
                           str.ptr(), str.length(), DRIZZLE_TIMESTAMP_DATE, 1);
177
187
  return error;
178
188
}
179
189
 
180
 
 
181
 
bool Field_newdate::send_binary(Protocol *protocol)
182
 
{
183
 
  DRIZZLE_TIME tm;
184
 
  Field_newdate::get_date(&tm,0);
185
 
  return protocol->store_date(&tm);
186
 
}
187
 
 
188
 
 
189
 
double Field_newdate::val_real(void)
190
 
{
191
 
  return (double) Field_newdate::val_int();
192
 
}
193
 
 
194
 
 
195
 
int64_t Field_newdate::val_int(void)
 
190
double Field_date::val_real(void)
 
191
{
 
192
  return (double) Field_date::val_int();
 
193
}
 
194
 
 
195
int64_t Field_date::val_int(void)
196
196
{
197
197
  uint32_t j= uint3korr(ptr);
198
198
  j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
199
199
  return (int64_t) j;
200
200
}
201
201
 
202
 
 
203
 
String *Field_newdate::val_str(String *val_buffer,
204
 
                               String *val_ptr __attribute__((unused)))
 
202
String *Field_date::val_str(String *val_buffer,
 
203
                               String *)
205
204
{
206
205
  val_buffer->alloc(field_length);
207
206
  val_buffer->length(field_length);
227
226
  return val_buffer;
228
227
}
229
228
 
230
 
 
231
 
bool Field_newdate::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
229
bool Field_date::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
232
230
{
233
231
  uint32_t tmp=(uint32_t) uint3korr(ptr);
234
232
  ltime->day=   tmp & 31;
240
238
          1 : 0);
241
239
}
242
240
 
243
 
 
244
 
bool Field_newdate::get_time(DRIZZLE_TIME *ltime)
 
241
bool Field_date::get_time(DRIZZLE_TIME *ltime)
245
242
{
246
 
  return Field_newdate::get_date(ltime,0);
 
243
  return Field_date::get_date(ltime,0);
247
244
}
248
245
 
249
 
 
250
 
int Field_newdate::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
246
int Field_date::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
251
247
{
252
248
  uint32_t a,b;
253
249
  a=(uint32_t) uint3korr(a_ptr);
255
251
  return (a < b) ? -1 : (a > b) ? 1 : 0;
256
252
}
257
253
 
258
 
 
259
 
void Field_newdate::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
 
254
void Field_date::sort_string(unsigned char *to,uint32_t )
260
255
{
261
256
  to[0] = ptr[2];
262
257
  to[1] = ptr[1];
263
258
  to[2] = ptr[0];
264
259
}
265
260
 
266
 
 
267
 
void Field_newdate::sql_type(String &res) const
 
261
void Field_date::sql_type(String &res) const
268
262
{
269
263
  res.set_ascii(STRING_WITH_LEN("date"));
270
264
}
271