~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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
1775.5.1 by earney
modified files containing stringstream to use boost:lexical_cast instead as
22
#include <boost/lexical_cast.hpp>
23
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
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>
2154.2.24 by Brian Aker
Merge in all changes for current_session, etc.
30
#include <drizzled/current_session.h>
2241.3.2 by Olaf van der Spek
Refactor Session::variables
31
#include <drizzled/system_variables.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.
32
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
33
#include <math.h>
34
873.1.8 by Jay Pipes
Fixes Arg_comparator::can_compare_as_dates to never, ever allow bad
35
#include <sstream>
36
#include <string>
37
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
38
namespace drizzled
39
{
40
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
41
42
/****************************************************************************
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
43
** Drizzle date type stored in 3 bytes
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
44
** In number context: YYYYMMDD
45
****************************************************************************/
46
47
/*
48
  Store string into a date field
49
50
  SYNOPSIS
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
51
    Field_date::store()
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
52
    from                Date string
53
    len                 Length of date field
54
    cs                  Character set (not used)
55
56
  RETURN
57
    0  ok
58
    1  Value was cut during conversion
59
    2  Wrong date string
60
    3  Datetime value that was cut (warning level NOTE)
61
       This is used by opt_range.cc:get_mm_leaf(). Note that there is a
62
       nearly-identical class Field_date doesn't ever return 3 from its
63
       store function.
64
*/
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
65
int Field_date::store(const char *from,
482 by Brian Aker
Remove uint.
66
                         uint32_t len,
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
67
                         const charset_info_st * const )
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
68
{
873.1.1 by Jay Pipes
Fixes the Field_date class to not allow any invalid input at
69
  /* 
70
   * Try to create a DateTime from the supplied string.  Throw an error
71
   * if unable to create a valid DateTime.  A DateTime is used so that
72
   * automatic conversion from the higher-storage DateTime can be used
73
   * and matches on datetime format strings can occur.
74
   */
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
75
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
76
  DateTime temporal;
873.1.1 by Jay Pipes
Fixes the Field_date class to not allow any invalid input at
77
  if (! temporal.from_string(from, (size_t) len))
78
  {
2114.5.4 by Brian Aker
Fix error message on date.
79
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), from);
873.1.1 by Jay Pipes
Fixes the Field_date class to not allow any invalid input at
80
    return 2;
81
  }
82
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
1782.4.2 by Brian Aker
Convert date from 3 byte to 4 byte.
83
  uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
84
  int4store(ptr, int_value);
873.1.1 by Jay Pipes
Fixes the Field_date class to not allow any invalid input at
85
  return 0;
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
86
}
87
873.1.8 by Jay Pipes
Fixes Arg_comparator::can_compare_as_dates to never, ever allow bad
88
int Field_date::store(double from)
89
{
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
90
  ASSERT_COLUMN_MARKED_FOR_WRITE;
873.1.8 by Jay Pipes
Fixes Arg_comparator::can_compare_as_dates to never, ever allow bad
91
  if (from < 0.0 || from > 99991231235959.0)
92
  {
93
    /* Convert the double to a string using stringstream */
1775.5.2 by earney
removed old commented out statements and uncommmeted out first occurance of stringstream in date.cc since it sets the precision.
94
    std::stringstream ss;
95
    std::string tmp;
96
    ss.precision(18); /* 18 places should be fine for error display of double input. */
97
    ss << from; ss >> tmp;
873.1.8 by Jay Pipes
Fixes Arg_comparator::can_compare_as_dates to never, ever allow bad
98
2114.5.4 by Brian Aker
Fix error message on date.
99
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
873.1.8 by Jay Pipes
Fixes Arg_comparator::can_compare_as_dates to never, ever allow bad
100
    return 2;
101
  }
102
  return Field_date::store((int64_t) rint(from), false);
103
}
104
105
int Field_date::store(int64_t from, bool)
106
{
107
  /* 
108
   * Try to create a DateTime from the supplied integer.  Throw an error
109
   * if unable to create a valid DateTime.  
110
   */
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
111
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
112
  DateTime temporal;
873.1.8 by Jay Pipes
Fixes Arg_comparator::can_compare_as_dates to never, ever allow bad
113
  if (! temporal.from_int64_t(from))
114
  {
1775.5.2 by earney
removed old commented out statements and uncommmeted out first occurance of stringstream in date.cc since it sets the precision.
115
    /* Convert the integer to a string using boost::lexical_cast */
1775.5.1 by earney
modified files containing stringstream to use boost:lexical_cast instead as
116
    std::string tmp(boost::lexical_cast<std::string>(from)); 
873.1.8 by Jay Pipes
Fixes Arg_comparator::can_compare_as_dates to never, ever allow bad
117
2114.5.4 by Brian Aker
Fix error message on date.
118
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
873.1.8 by Jay Pipes
Fixes Arg_comparator::can_compare_as_dates to never, ever allow bad
119
    return 2;
120
  }
121
122
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
1782.4.2 by Brian Aker
Convert date from 3 byte to 4 byte.
123
  uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
124
  int4store(ptr, int_value);
2114.5.4 by Brian Aker
Fix error message on date.
125
873.1.8 by Jay Pipes
Fixes Arg_comparator::can_compare_as_dates to never, ever allow bad
126
  return 0;
127
}
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
128
2104.2.7 by Brian Aker
Fix store_time to take reference.
129
int Field_date::store_time(type::Time &ltime,
2088.8.9 by Brian Aker
Merge in namespace of enum.
130
                           type::timestamp_t time_type)
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
131
{
132
  long tmp;
133
  int error= 0;
2088.8.9 by Brian Aker
Merge in namespace of enum.
134
  if (time_type == type::DRIZZLE_TIMESTAMP_DATE || time_type == type::DRIZZLE_TIMESTAMP_DATETIME)
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
135
  {
2104.2.7 by Brian Aker
Fix store_time to take reference.
136
    tmp= ltime.year*10000 + ltime.month*100 + ltime.day;
2069.2.3 by Brian Aker
Encapsulate a bit more of the logic around making strings from type::Time
137
2148.5.2 by Brian Aker
Additional remove of current_session.
138
    Session *session= getTable() ? getTable()->in_use : current_session;
2104.2.6 by Brian Aker
Enum was_cut
139
    type::cut_t cut_error= type::VALID;
2104.2.7 by Brian Aker
Fix store_time to take reference.
140
    if (ltime.check(tmp != 0,
2098.3.4 by Brian Aker
Additional encapsulation of time.
141
                     (TIME_FUZZY_DATE |
2148.5.2 by Brian Aker
Additional remove of current_session.
142
                      (session->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))), cut_error))
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
143
    {
2088.8.11 by Brian Aker
Fix additional output, swaps for the valus.
144
      char buff[type::Time::MAX_STRING_LENGTH];
383.1.12 by Brian Aker
Much closer toward UTF8 being around all the time...
145
      String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
2104.2.7 by Brian Aker
Fix store_time to take reference.
146
      ltime.convert(str, type::DRIZZLE_TIMESTAMP_DATE);
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
147
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
2088.8.9 by Brian Aker
Merge in namespace of enum.
148
                           str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
149
    }
2069.2.3 by Brian Aker
Encapsulate a bit more of the logic around making strings from type::Time
150
2104.2.6 by Brian Aker
Enum was_cut
151
    error= static_cast<int>(cut_error);
152
2104.2.7 by Brian Aker
Fix store_time to take reference.
153
    if (not error && ltime.time_type != type::DRIZZLE_TIMESTAMP_DATE &&
154
        (ltime.hour || ltime.minute || ltime.second || ltime.second_part))
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
155
    {
2088.8.11 by Brian Aker
Fix additional output, swaps for the valus.
156
      char buff[type::Time::MAX_STRING_LENGTH];
383.1.12 by Brian Aker
Much closer toward UTF8 being around all the time...
157
      String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
2104.2.7 by Brian Aker
Fix store_time to take reference.
158
      ltime.convert(str);
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
159
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE,
212.5.42 by Monty Taylor
Ding dong include is dead.
160
                           ER_WARN_DATA_TRUNCATED,
2088.8.9 by Brian Aker
Merge in namespace of enum.
161
                           str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
162
      error= 3;
163
    }
164
  }
165
  else
166
  {
167
    tmp=0;
168
    error= 1;
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
169
    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
170
  }
2088.8.9 by Brian Aker
Merge in namespace of enum.
171
1782.4.2 by Brian Aker
Convert date from 3 byte to 4 byte.
172
  int4store(ptr,tmp);
2088.8.9 by Brian Aker
Merge in namespace of enum.
173
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
174
  return error;
175
}
176
2181.2.1 by Brian Aker
Protect all of the val_* methods from modification.
177
double Field_date::val_real(void) const
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
178
{
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
179
  return (double) Field_date::val_int();
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
180
}
181
2181.2.1 by Brian Aker
Protect all of the val_* methods from modification.
182
int64_t Field_date::val_int(void) const
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
183
{
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
184
  uint32_t j;
185
186
  ASSERT_COLUMN_MARKED_FOR_READ;
187
1782.4.2 by Brian Aker
Convert date from 3 byte to 4 byte.
188
  j= uint4korr(ptr);
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
189
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
190
  return (int64_t) j;
191
}
192
2181.2.1 by Brian Aker
Protect all of the val_* methods from modification.
193
String *Field_date::val_str(String *val_buffer, String *) const
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
194
{
195
  val_buffer->alloc(field_length);
196
  val_buffer->length(field_length);
1782.4.2 by Brian Aker
Convert date from 3 byte to 4 byte.
197
  uint32_t tmp=(uint32_t) uint4korr(ptr);
198
  int32_t part;
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
199
  char *pos=(char*) val_buffer->ptr()+10;
200
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
201
  ASSERT_COLUMN_MARKED_FOR_READ;
202
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
203
  /* Open coded to get more speed */
204
  *pos--=0;					// End NULL
1782.4.2 by Brian Aker
Convert date from 3 byte to 4 byte.
205
  part=(int32_t) (tmp % 100);
206
  *pos--= (char) ('0'+part%10);
207
  *pos--= (char) ('0'+part/10);
208
  *pos--= '-';
209
  part=(int32_t) (tmp/100%100);
210
  *pos--= (char) ('0'+part%10);
211
  *pos--= (char) ('0'+part/10);
212
  *pos--= '-';
213
  part=(int32_t) (tmp/10000);
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
214
  *pos--= (char) ('0'+part%10); part/=10;
215
  *pos--= (char) ('0'+part%10); part/=10;
216
  *pos--= (char) ('0'+part%10); part/=10;
217
  *pos=   (char) ('0'+part);
218
  return val_buffer;
219
}
220
2181.2.1 by Brian Aker
Protect all of the val_* methods from modification.
221
bool Field_date::get_date(type::Time &ltime, uint32_t fuzzydate) const
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
222
{
1782.4.2 by Brian Aker
Convert date from 3 byte to 4 byte.
223
  uint32_t tmp=(uint32_t) uint4korr(ptr);
2104.2.8 by Brian Aker
Merge in reference from pointer.
224
  ltime.day=		(int) (tmp%100);
225
  ltime.month= 	(int) (tmp/100%100);
226
  ltime.year= 		(int) (tmp/10000);
227
  ltime.time_type= type::DRIZZLE_TIMESTAMP_DATE;
228
  ltime.hour= ltime.minute= ltime.second= ltime.second_part= ltime.neg= 0;
229
230
  return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime.month || !ltime.day)) ?
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
231
          1 : 0);
232
}
233
2181.2.3 by Brian Aker
Further commit const on field.
234
bool Field_date::get_time(type::Time &ltime) const
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
235
{
2104.2.8 by Brian Aker
Merge in reference from pointer.
236
  return Field_date::get_date(ltime ,0);
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
237
}
238
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
239
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
240
{
205 by Brian Aker
uint32 -> uin32_t
241
  uint32_t a,b;
1782.4.2 by Brian Aker
Convert date from 3 byte to 4 byte.
242
  a=(uint32_t) uint4korr(a_ptr);
243
  b=(uint32_t) uint4korr(b_ptr);
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
244
  return (a < b) ? -1 : (a > b) ? 1 : 0;
245
}
246
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
247
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
248
{
1782.4.2 by Brian Aker
Convert date from 3 byte to 4 byte.
249
  to[0] = ptr[3];
250
  to[1] = ptr[2];
251
  to[2] = ptr[1];
252
  to[3] = ptr[0];
173.1.8 by Toru Maesaka
ripped out STRING and renamed new_decimal and new_date
253
}
254
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
255
} /* namespace drizzled */