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