~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
2109.1.7 by Brian Aker
Merge in fix for Solaris and make use of isDatabaseLowByteFirst()
64
  if (getTable() && getTable()->isDatabaseLowByteFirst())
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
2109.1.7 by Brian Aker
Merge in fix for Solaris and make use of isDatabaseLowByteFirst()
114
  if (getTable() && getTable()->isDatabaseLowByteFirst())
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
2104.2.7 by Brian Aker
Fix store_time to take reference.
124
int Field_datetime::store_time(type::Time &ltime, type::timestamp_t)
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
2104.2.7 by Brian Aker
Fix store_time to take reference.
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);
907.1.7 by Jay Pipes
Merged in remove-timezone work
134
135
  if (! temporal.is_valid())
136
  {
2088.8.11 by Brian Aker
Fix additional output, swaps for the valus.
137
    char tmp_string[type::Time::MAX_STRING_LENGTH];
907.1.7 by Jay Pipes
Merged in remove-timezone work
138
    size_t tmp_string_len;
139
2088.8.11 by Brian Aker
Fix additional output, swaps for the valus.
140
    tmp_string_len= temporal.to_string(tmp_string, type::Time::MAX_STRING_LENGTH);
141
    assert(tmp_string_len < type::Time::MAX_STRING_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
2109.1.7 by Brian Aker
Merge in fix for Solaris and make use of isDatabaseLowByteFirst()
150
  if (getTable() && getTable()->isDatabaseLowByteFirst())
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);
2109.1.7 by Brian Aker
Merge in fix for Solaris and make use of isDatabaseLowByteFirst()
157
907.1.7 by Jay Pipes
Merged in remove-timezone work
158
  return 0;
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
159
}
160
161
double Field_datetime::val_real(void)
162
{
163
  return (double) Field_datetime::val_int();
164
}
165
166
int64_t Field_datetime::val_int(void)
167
{
168
  int64_t j;
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
169
170
  ASSERT_COLUMN_MARKED_FOR_READ;
171
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
172
#ifdef WORDS_BIGENDIAN
2109.1.7 by Brian Aker
Merge in fix for Solaris and make use of isDatabaseLowByteFirst()
173
  if (getTable() && getTable()->isDatabaseLowByteFirst())
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
174
    j=sint8korr(ptr);
175
  else
176
#endif
177
    int64_tget(j,ptr);
178
  return j;
179
}
180
181
182
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.
183
				String *)
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
184
{
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
185
  val_buffer->alloc(DateTime::MAX_STRING_LENGTH);
186
  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.
187
  int64_t tmp;
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
188
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
189
  ASSERT_COLUMN_MARKED_FOR_READ;
190
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
191
#ifdef WORDS_BIGENDIAN
2109.1.7 by Brian Aker
Merge in fix for Solaris and make use of isDatabaseLowByteFirst()
192
  if (getTable() && getTable()->isDatabaseLowByteFirst())
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
193
    tmp=sint8korr(ptr);
194
  else
195
#endif
196
    int64_tget(tmp,ptr);
197
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
198
  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.
199
200
  /* TODO: add an assert that this succeeds
201
   * currently fails due to bug in allowing
202
   * ALTER TABLE to add a datetime column that's
203
   * not null without a default value.
204
   */
205
  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.
206
                                 from formats such as 20090101 as
207
                                 the stored value has already been
208
                                 converted.
209
                               */
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.
210
211
  int rlen;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
212
  rlen= dt.to_string((char*)val_buffer->ptr(), DateTime::MAX_STRING_LENGTH);
213
  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.
214
215
  val_buffer->length(rlen);
216
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
217
  return val_buffer;
218
}
219
2104.2.8 by Brian Aker
Merge in reference from pointer.
220
bool Field_datetime::get_date(type::Time &ltime, uint32_t fuzzydate)
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
221
{
222
  int64_t tmp=Field_datetime::val_int();
205 by Brian Aker
uint32 -> uin32_t
223
  uint32_t part1,part2;
422 by Monty
Various int64 constant fixes.
224
  part1=(uint32_t) (tmp/INT64_C(1000000));
225
  part2=(uint32_t) (tmp - (uint64_t) part1*INT64_C(1000000));
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
226
2104.2.8 by Brian Aker
Merge in reference from pointer.
227
  ltime.time_type=	type::DRIZZLE_TIMESTAMP_DATETIME;
228
  ltime.neg=		0;
229
  ltime.second_part=	0;
230
  ltime.second=	(int) (part2%100);
231
  ltime.minute=	(int) (part2/100%100);
232
  ltime.hour=		(int) (part2/10000);
233
  ltime.day=		(int) (part1%100);
234
  ltime.month= 	(int) (part1/100%100);
235
  ltime.year= 		(int) (part1/10000);
236
237
  return (!(fuzzydate & TIME_FUZZY_DATE) && (!ltime.month || !ltime.day)) ? 1 : 0;
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
238
}
239
2104.2.8 by Brian Aker
Merge in reference from pointer.
240
bool Field_datetime::get_time(type::Time &ltime)
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
241
{
242
  return Field_datetime::get_date(ltime,0);
243
}
244
481 by Brian Aker
Remove all of uchar.
245
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/
246
{
247
  int64_t a,b;
248
#ifdef WORDS_BIGENDIAN
2109.1.7 by Brian Aker
Merge in fix for Solaris and make use of isDatabaseLowByteFirst()
249
  if (getTable() && getTable()->isDatabaseLowByteFirst())
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
250
  {
251
    a=sint8korr(a_ptr);
252
    b=sint8korr(b_ptr);
253
  }
254
  else
255
#endif
256
  {
257
    int64_tget(a,a_ptr);
258
    int64_tget(b,b_ptr);
259
  }
260
  return ((uint64_t) a < (uint64_t) b) ? -1 :
261
    ((uint64_t) a > (uint64_t) b) ? 1 : 0;
262
}
263
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
264
void Field_datetime::sort_string(unsigned char *to,uint32_t )
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
265
{
266
#ifdef WORDS_BIGENDIAN
2109.1.7 by Brian Aker
Merge in fix for Solaris and make use of isDatabaseLowByteFirst()
267
  if (not getTable() || not getTable()->isDatabaseLowByteFirst())
173.1.3 by Toru Maesaka
ripped out datetime and moved to field/
268
  {
269
    to[0] = ptr[0];
270
    to[1] = ptr[1];
271
    to[2] = ptr[2];
272
    to[3] = ptr[3];
273
    to[4] = ptr[4];
274
    to[5] = ptr[5];
275
    to[6] = ptr[6];
276
    to[7] = ptr[7];
277
  }
278
  else
279
#endif
280
  {
281
    to[0] = ptr[7];
282
    to[1] = ptr[6];
283
    to[2] = ptr[5];
284
    to[3] = ptr[4];
285
    to[4] = ptr[3];
286
    to[5] = ptr[2];
287
    to[6] = ptr[1];
288
    to[7] = ptr[0];
289
  }
290
}
291
292
293
void Field_datetime::sql_type(String &res) const
294
{
295
  res.set_ascii(STRING_WITH_LEN("datetime"));
296
}
297
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
298
} /* namespace drizzled */