~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.3 by Toru Maesaka
ripped out datetime and moved to field/
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"
1775.5.1 by earney
modified files containing stringstream to use boost:lexical_cast instead as
22
#include <boost/lexical_cast.hpp>
873.1.2 by Jay Pipes
Fixed Field_datetime to never accept any bad datetimes as a string. This broke
23
#include "drizzled/field/datetime.h"
24
#include "drizzled/error.h"
25
#include "drizzled/table.h"
26
#include "drizzled/temporal.h"
27
#include "drizzled/session.h"
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
28
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
29
#include <math.h>
30
873.1.7 by Jay Pipes
Fixes Field_datetime::store(NUMBER) to throw an error when invalid
31
#include <sstream>
32
#include <string>
33
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
34
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
35
namespace drizzled
36
{
37
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
38
/****************************************************************************
39
** datetime type
40
** In string context: YYYY-MM-DD HH:MM:DD
41
** In number context: YYYYMMDDHHMMDD
42
****************************************************************************/
43
44
int Field_datetime::store(const char *from,
482 by Brian Aker
Remove uint.
45
                          uint32_t len,
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
46
                          const CHARSET_INFO * const )
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
47
{
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
48
  ASSERT_COLUMN_MARKED_FOR_WRITE;
873.1.2 by Jay Pipes
Fixed Field_datetime to never accept any bad datetimes as a string. This broke
49
  /* 
50
   * Try to create a DateTime from the supplied string.  Throw an error
51
   * if unable to create a valid DateTime.  
52
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
53
  DateTime temporal;
873.1.2 by Jay Pipes
Fixed Field_datetime to never accept any bad datetimes as a string. This broke
54
  if (! temporal.from_string(from, (size_t) len))
55
  {
56
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), from);
57
    return 2;
58
  }
59
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
60
  int64_t int_value;
61
  temporal.to_int64_t(&int_value);
873.1.7 by Jay Pipes
Fixes Field_datetime::store(NUMBER) to throw an error when invalid
62
63
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
64
  if (getTable() && getTable()->getShare()->db_low_byte_first)
873.1.7 by Jay Pipes
Fixes Field_datetime::store(NUMBER) to throw an error when invalid
65
  {
66
    int8store(ptr, int_value);
67
  }
68
  else
69
#endif
70
    int64_tstore(ptr, int_value);
71
  return 0;
72
}
73
74
int Field_datetime::store(double from)
75
{
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
76
  ASSERT_COLUMN_MARKED_FOR_WRITE;
873.1.7 by Jay Pipes
Fixes Field_datetime::store(NUMBER) to throw an error when invalid
77
  if (from < 0.0 || from > 99991231235959.0)
78
  {
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.
79
    /* Convert the double to a string using boost::lexical_cast */
1775.5.1 by earney
modified files containing stringstream to use boost:lexical_cast instead as
80
    std::string tmp(boost::lexical_cast<std::string>(from));
873.1.7 by Jay Pipes
Fixes Field_datetime::store(NUMBER) to throw an error when invalid
81
82
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
83
    return 2;
84
  }
85
  return Field_datetime::store((int64_t) rint(from), false);
86
}
87
88
int Field_datetime::store(int64_t from, bool)
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.7 by Jay Pipes
Fixes Field_datetime::store(NUMBER) to throw an error when invalid
91
  /* 
873.1.8 by Jay Pipes
Fixes Arg_comparator::can_compare_as_dates to never, ever allow bad
92
   * Try to create a DateTime from the supplied integer.  Throw an error
873.1.7 by Jay Pipes
Fixes Field_datetime::store(NUMBER) to throw an error when invalid
93
   * if unable to create a valid DateTime.  
94
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
95
  DateTime temporal;
873.1.7 by Jay Pipes
Fixes Field_datetime::store(NUMBER) to throw an error when invalid
96
  if (! temporal.from_int64_t(from))
97
  {
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.
98
    /* 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
99
    std::string tmp(boost::lexical_cast<std::string>(from));
873.1.7 by Jay Pipes
Fixes Field_datetime::store(NUMBER) to throw an error when invalid
100
101
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
102
    return 2;
103
  }
104
105
  /* 
106
   * Because "from" may be a silly MySQL-like "datetime number" (like, oh, 101)
107
   * we must here get the value of the DateTime as its *real* int64_t, after
108
   * the conversion above has been done...yuck. God, save us.
109
   */
110
  int64_t int_value;
111
  temporal.to_int64_t(&int_value);
112
113
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
114
  if (getTable() && getTable()->getShare()->db_low_byte_first)
873.1.7 by Jay Pipes
Fixes Field_datetime::store(NUMBER) to throw an error when invalid
115
  {
116
    int8store(ptr, int_value);
117
  }
118
  else
119
#endif
120
    int64_tstore(ptr, int_value);
121
  return 0;
122
}
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
123
907.1.7 by Jay Pipes
Merged in remove-timezone work
124
int Field_datetime::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
125
{
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
126
  DateTime temporal;
907.1.7 by Jay Pipes
Merged in remove-timezone work
127
128
  temporal.set_years(ltime->year);
129
  temporal.set_months(ltime->month);
130
  temporal.set_days(ltime->day);
131
  temporal.set_hours(ltime->hour);
132
  temporal.set_minutes(ltime->minute);
133
  temporal.set_seconds(ltime->second);
134
135
  if (! temporal.is_valid())
136
  {
137
    char tmp_string[MAX_DATE_STRING_REP_LENGTH];
138
    size_t tmp_string_len;
139
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
140
    tmp_string_len= temporal.to_string(tmp_string, MAX_DATE_STRING_REP_LENGTH);
141
    assert(tmp_string_len < MAX_DATE_STRING_REP_LENGTH);
907.1.7 by Jay Pipes
Merged in remove-timezone work
142
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp_string);
143
    return 1;
144
  }
145
146
  int64_t int_value;
147
  temporal.to_int64_t(&int_value);
148
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
149
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
150
  if (getTable() && getTable()->getShare()->db_low_byte_first)
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
151
  {
907.1.7 by Jay Pipes
Merged in remove-timezone work
152
    int8store(ptr, int_value);
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
153
  }
154
  else
155
#endif
907.1.7 by Jay Pipes
Merged in remove-timezone work
156
    int64_tstore(ptr, int_value);
157
  return 0;
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
158
}
159
160
double Field_datetime::val_real(void)
161
{
162
  return (double) Field_datetime::val_int();
163
}
164
165
int64_t Field_datetime::val_int(void)
166
{
167
  int64_t j;
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
168
169
  ASSERT_COLUMN_MARKED_FOR_READ;
170
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
171
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
172
  if (getTable() && getTable()->getShare()->db_low_byte_first)
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
173
    j=sint8korr(ptr);
174
  else
175
#endif
176
    int64_tget(j,ptr);
177
  return j;
178
}
179
180
181
String *Field_datetime::val_str(String *val_buffer,
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
182
				String *)
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
183
{
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
184
  val_buffer->alloc(DateTime::MAX_STRING_LENGTH);
185
  val_buffer->length(DateTime::MAX_STRING_LENGTH);
1079.3.2 by Stewart Smith
Replace MAX_(DATE|TIME).*_WIDTH defines in definitions.h with real (and correct) static const members to Temporal types.
186
  int64_t tmp;
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
187
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
188
  ASSERT_COLUMN_MARKED_FOR_READ;
189
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
190
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
191
  if (getTable() && getTable()->getShare()->db_low_byte_first)
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
192
    tmp=sint8korr(ptr);
193
  else
194
#endif
195
    int64_tget(tmp,ptr);
196
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
197
  DateTime dt;
1079.3.2 by Stewart Smith
Replace MAX_(DATE|TIME).*_WIDTH defines in definitions.h with real (and correct) static const members to Temporal types.
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
1782.4.2 by Brian Aker
Convert date from 3 byte to 4 byte.
205
                                 from formats such as 20090101 as
206
                                 the stored value has already been
207
                                 converted.
208
                               */
1079.3.2 by Stewart Smith
Replace MAX_(DATE|TIME).*_WIDTH defines in definitions.h with real (and correct) static const members to Temporal types.
209
210
  int rlen;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
211
  rlen= dt.to_string((char*)val_buffer->ptr(), DateTime::MAX_STRING_LENGTH);
212
  assert((rlen+1) <  DateTime::MAX_STRING_LENGTH);
1079.3.2 by Stewart Smith
Replace MAX_(DATE|TIME).*_WIDTH defines in definitions.h with real (and correct) static const members to Temporal types.
213
214
  val_buffer->length(rlen);
215
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
216
  return val_buffer;
217
}
218
482 by Brian Aker
Remove uint.
219
bool Field_datetime::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzydate)
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
220
{
221
  int64_t tmp=Field_datetime::val_int();
205 by Brian Aker
uint32 -> uin32_t
222
  uint32_t part1,part2;
422 by Monty
Various int64 constant fixes.
223
  part1=(uint32_t) (tmp/INT64_C(1000000));
224
  part2=(uint32_t) (tmp - (uint64_t) part1*INT64_C(1000000));
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
225
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
226
  ltime->time_type=	DRIZZLE_TIMESTAMP_DATETIME;
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
227
  ltime->neg=		0;
228
  ltime->second_part=	0;
229
  ltime->second=	(int) (part2%100);
230
  ltime->minute=	(int) (part2/100%100);
231
  ltime->hour=		(int) (part2/10000);
232
  ltime->day=		(int) (part1%100);
233
  ltime->month= 	(int) (part1/100%100);
234
  ltime->year= 		(int) (part1/10000);
235
  return (!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ? 1 : 0;
236
}
237
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
238
bool Field_datetime::get_time(DRIZZLE_TIME *ltime)
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
239
{
240
  return Field_datetime::get_date(ltime,0);
241
}
242
481 by Brian Aker
Remove all of uchar.
243
int Field_datetime::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
244
{
245
  int64_t a,b;
246
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
247
  if (getTable() && getTable()->getShare()->db_low_byte_first)
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
248
  {
249
    a=sint8korr(a_ptr);
250
    b=sint8korr(b_ptr);
251
  }
252
  else
253
#endif
254
  {
255
    int64_tget(a,a_ptr);
256
    int64_tget(b,b_ptr);
257
  }
258
  return ((uint64_t) a < (uint64_t) b) ? -1 :
259
    ((uint64_t) a > (uint64_t) b) ? 1 : 0;
260
}
261
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
262
void Field_datetime::sort_string(unsigned char *to,uint32_t )
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
263
{
264
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
265
  if (!getTable() || !getTable()->getShare()->db_low_byte_first)
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
266
  {
267
    to[0] = ptr[0];
268
    to[1] = ptr[1];
269
    to[2] = ptr[2];
270
    to[3] = ptr[3];
271
    to[4] = ptr[4];
272
    to[5] = ptr[5];
273
    to[6] = ptr[6];
274
    to[7] = ptr[7];
275
  }
276
  else
277
#endif
278
  {
279
    to[0] = ptr[7];
280
    to[1] = ptr[6];
281
    to[2] = ptr[5];
282
    to[3] = ptr[4];
283
    to[4] = ptr[3];
284
    to[5] = ptr[2];
285
    to[6] = ptr[1];
286
    to[7] = ptr[0];
287
  }
288
}
289
290
291
void Field_datetime::sql_type(String &res) const
292
{
293
  res.set_ascii(STRING_WITH_LEN("datetime"));
294
}
295
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
296
} /* namespace drizzled */