~drizzle-trunk/drizzle/development

466 by Monty Taylor
Fixed modelines... these files are c++.
1
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 MySQL
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
21
#include "config.h"
873.1.1 by Jay Pipes
Fixes the Field_date class to not allow any invalid input at
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"
1237.9.8 by Monty Taylor
Fixed solaris build.
27
#include "drizzled/time_functions.h"
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
28
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
29
#include <math.h>
30
873.1.8 by Jay Pipes
Fixes Arg_comparator::can_compare_as_dates to never, ever allow bad
31
#include <sstream>
32
#include <string>
33
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
34
namespace drizzled
35
{
36
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
37
38
/****************************************************************************
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
39
** Drizzle date type stored in 3 bytes
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
40
** In number context: YYYYMMDD
41
****************************************************************************/
42
43
/*
44
  Store string into a date field
45
46
  SYNOPSIS
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
47
    Field_date::store()
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
48
    from                Date string
49
    len                 Length of date field
50
    cs                  Character set (not used)
51
52
  RETURN
53
    0  ok
54
    1  Value was cut during conversion
55
    2  Wrong date string
56
    3  Datetime value that was cut (warning level NOTE)
57
       This is used by opt_range.cc:get_mm_leaf(). Note that there is a
58
       nearly-identical class Field_date doesn't ever return 3 from its
59
       store function.
60
*/
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
61
int Field_date::store(const char *from,
482 by Brian Aker
Remove uint.
62
                         uint32_t len,
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
63
                         const CHARSET_INFO * const )
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
64
{
873.1.1 by Jay Pipes
Fixes the Field_date class to not allow any invalid input at
65
#ifdef NOTDEFINED
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
66
  long tmp;
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
67
  DRIZZLE_TIME l_time;
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
68
  int error;
520.1.22 by Brian Aker
Second pass of thd cleanup
69
  Session *session= table ? table->in_use : current_session;
236.1.25 by Monty Taylor
Fixed a few naming references.
70
  enum enum_drizzle_timestamp_type ret;
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
71
  if ((ret= str_to_datetime(from, len, &l_time,
72
                            (TIME_FUZZY_DATE |
520.1.22 by Brian Aker
Second pass of thd cleanup
73
                             (session->variables.sql_mode &
361 by Brian Aker
One more mode down, two more left to go!
74
                              (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))),
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
75
                            &error)) <= DRIZZLE_TIMESTAMP_ERROR)
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
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;
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
83
    if (!error && (ret != DRIZZLE_TIMESTAMP_DATE) &&
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_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)
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
89
    set_datetime_warning(error == 3 ? DRIZZLE_ERROR::WARN_LEVEL_NOTE :
90
                         DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
91
                         ER_WARN_DATA_TRUNCATED,
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
92
                         from, len, DRIZZLE_TIMESTAMP_DATE, 1);
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
93
94
  int3store(ptr, tmp);
873.1.1 by Jay Pipes
Fixes the Field_date class to not allow any invalid input at
95
#endif /* NOTDEFINED */
96
  /* 
97
   * Try to create a DateTime from the supplied string.  Throw an error
98
   * if unable to create a valid DateTime.  A DateTime is used so that
99
   * automatic conversion from the higher-storage DateTime can be used
100
   * and matches on datetime format strings can occur.
101
   */
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
102
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
103
  DateTime temporal;
873.1.1 by Jay Pipes
Fixes the Field_date class to not allow any invalid input at
104
  if (! temporal.from_string(from, (size_t) len))
105
  {
106
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), from);
107
    return 2;
108
  }
109
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
110
  uint32_t int_value= (temporal.years() * 16 * 32) + (temporal.months() * 32) + temporal.days();
111
  int3store(ptr, int_value);
112
  return 0;
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
113
}
114
873.1.8 by Jay Pipes
Fixes Arg_comparator::can_compare_as_dates to never, ever allow bad
115
int Field_date::store(double from)
116
{
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
117
  ASSERT_COLUMN_MARKED_FOR_WRITE;
873.1.8 by Jay Pipes
Fixes Arg_comparator::can_compare_as_dates to never, ever allow bad
118
  if (from < 0.0 || from > 99991231235959.0)
119
  {
120
    /* Convert the double to a string using stringstream */
121
    std::stringstream ss;
122
    std::string tmp;
123
    ss.precision(18); /* 18 places should be fine for error display of double input. */
124
    ss << from; ss >> tmp;
125
126
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
127
    return 2;
128
  }
129
  return Field_date::store((int64_t) rint(from), false);
130
}
131
132
int Field_date::store(int64_t from, bool)
133
{
134
  /* 
135
   * Try to create a DateTime from the supplied integer.  Throw an error
136
   * if unable to create a valid DateTime.  
137
   */
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
138
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
139
  DateTime temporal;
873.1.8 by Jay Pipes
Fixes Arg_comparator::can_compare_as_dates to never, ever allow bad
140
  if (! temporal.from_int64_t(from))
141
  {
142
    /* Convert the integer to a string using stringstream */
143
    std::stringstream ss;
144
    std::string tmp;
145
    ss << from; ss >> tmp;
146
147
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
148
    return 2;
149
  }
150
151
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
152
  uint32_t int_value= (temporal.years() * 16 * 32) + (temporal.months() * 32) + temporal.days();
153
  int3store(ptr, int_value);
154
  return 0;
155
}
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
156
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
157
int Field_date::store_time(DRIZZLE_TIME *ltime,
398.1.1 by Monty Taylor
Remove typedef enum enum_drizzle_timestamp_type timestamp_type;
158
                              enum enum_drizzle_timestamp_type time_type)
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
159
{
160
  long tmp;
161
  int error= 0;
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
162
  if (time_type == DRIZZLE_TIMESTAMP_DATE ||
163
      time_type == DRIZZLE_TIMESTAMP_DATETIME)
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
164
  {
165
    tmp=ltime->year*16*32+ltime->month*32+ltime->day;
166
    if (check_date(ltime, tmp != 0,
167
                   (TIME_FUZZY_DATE |
520.1.22 by Brian Aker
Second pass of thd cleanup
168
                    (current_session->variables.sql_mode &
361 by Brian Aker
One more mode down, two more left to go!
169
                     (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))), &error))
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
170
    {
171
      char buff[MAX_DATE_STRING_REP_LENGTH];
383.1.12 by Brian Aker
Much closer toward UTF8 being around all the time...
172
      String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
907.1.2 by Jay Pipes
Merging in old r902 temporal changes
173
      make_date(ltime, &str);
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
174
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
175
                           str.ptr(), str.length(), DRIZZLE_TIMESTAMP_DATE, 1);
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
176
    }
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
177
    if (!error && ltime->time_type != DRIZZLE_TIMESTAMP_DATE &&
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
178
        (ltime->hour || ltime->minute || ltime->second || ltime->second_part))
179
    {
180
      char buff[MAX_DATE_STRING_REP_LENGTH];
383.1.12 by Brian Aker
Much closer toward UTF8 being around all the time...
181
      String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
907.1.2 by Jay Pipes
Merging in old r902 temporal changes
182
      make_datetime(ltime, &str);
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
183
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE,
212.5.42 by Monty Taylor
Ding dong include is dead.
184
                           ER_WARN_DATA_TRUNCATED,
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
185
                           str.ptr(), str.length(), DRIZZLE_TIMESTAMP_DATE, 1);
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
186
      error= 3;
187
    }
188
  }
189
  else
190
  {
191
    tmp=0;
192
    error= 1;
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
193
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
194
  }
195
  int3store(ptr,tmp);
196
  return error;
197
}
198
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
199
double Field_date::val_real(void)
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
200
{
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
201
  return (double) Field_date::val_int();
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
202
}
203
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
204
int64_t Field_date::val_int(void)
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
205
{
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
206
  uint32_t j;
207
208
  ASSERT_COLUMN_MARKED_FOR_READ;
209
210
  j= uint3korr(ptr);
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
211
  j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
212
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
213
  return (int64_t) j;
214
}
215
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
216
String *Field_date::val_str(String *val_buffer,
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
217
			       String *)
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
218
{
219
  val_buffer->alloc(field_length);
220
  val_buffer->length(field_length);
205 by Brian Aker
uint32 -> uin32_t
221
  uint32_t tmp=(uint32_t) uint3korr(ptr);
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
222
  int part;
223
  char *pos=(char*) val_buffer->ptr()+10;
224
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
225
  ASSERT_COLUMN_MARKED_FOR_READ;
226
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
227
  /* Open coded to get more speed */
228
  *pos--=0;					// End NULL
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);
238
  *pos--= (char) ('0'+part%10); part/=10;
239
  *pos--= (char) ('0'+part%10); part/=10;
240
  *pos--= (char) ('0'+part%10); part/=10;
241
  *pos=   (char) ('0'+part);
242
  return val_buffer;
243
}
244
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
245
bool Field_date::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
246
{
205 by Brian Aker
uint32 -> uin32_t
247
  uint32_t tmp=(uint32_t) uint3korr(ptr);
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
248
  ltime->day=   tmp & 31;
249
  ltime->month= (tmp >> 5) & 15;
250
  ltime->year=  (tmp >> 9);
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
251
  ltime->time_type= DRIZZLE_TIMESTAMP_DATE;
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
252
  ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
253
  return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ?
254
          1 : 0);
255
}
256
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
257
bool Field_date::get_time(DRIZZLE_TIME *ltime)
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
258
{
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
259
  return Field_date::get_date(ltime,0);
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
260
}
261
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
262
int Field_date::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
263
{
205 by Brian Aker
uint32 -> uin32_t
264
  uint32_t a,b;
265
  a=(uint32_t) uint3korr(a_ptr);
266
  b=(uint32_t) uint3korr(b_ptr);
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
267
  return (a < b) ? -1 : (a > b) ? 1 : 0;
268
}
269
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
270
void Field_date::sort_string(unsigned char *to,uint32_t )
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
271
{
272
  to[0] = ptr[2];
273
  to[1] = ptr[1];
274
  to[2] = ptr[0];
275
}
276
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
277
void Field_date::sql_type(String &res) const
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
278
{
279
  res.set_ascii(STRING_WITH_LEN("date"));
280
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
281
282
} /* namespace drizzled */