~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/date.cc

  • Committer: Jay Pipes
  • Date: 2008-07-18 20:20:47 UTC
  • mto: This revision was merged to the branch mainline in revision 182.
  • Revision ID: jay@mysql.com-20080718202047-1tnd4i9z3k3cvg9v
DBUG entirely removed from server and client

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
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
 
 
21
 
#include <config.h>
22
 
#include <boost/lexical_cast.hpp>
23
 
 
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>
30
 
#include <drizzled/current_session.h>
31
 
 
32
 
#include <math.h>
33
 
 
34
 
#include <sstream>
35
 
#include <string>
36
 
 
37
 
namespace drizzled
38
 
{
39
 
 
40
 
 
41
 
/****************************************************************************
42
 
** Drizzle date type stored in 3 bytes
43
 
** In number context: YYYYMMDD
44
 
****************************************************************************/
45
 
 
46
 
/*
47
 
  Store string into a date field
48
 
 
49
 
  SYNOPSIS
50
 
    Field_date::store()
51
 
    from                Date string
52
 
    len                 Length of date field
53
 
    cs                  Character set (not used)
54
 
 
55
 
  RETURN
56
 
    0  ok
57
 
    1  Value was cut during conversion
58
 
    2  Wrong date string
59
 
    3  Datetime value that was cut (warning level NOTE)
60
 
       This is used by opt_range.cc:get_mm_leaf(). Note that there is a
61
 
       nearly-identical class Field_date doesn't ever return 3 from its
62
 
       store function.
63
 
*/
64
 
int Field_date::store(const char *from,
65
 
                         uint32_t len,
66
 
                         const CHARSET_INFO * const )
67
 
{
68
 
  /* 
69
 
   * Try to create a DateTime from the supplied string.  Throw an error
70
 
   * if unable to create a valid DateTime.  A DateTime is used so that
71
 
   * automatic conversion from the higher-storage DateTime can be used
72
 
   * and matches on datetime format strings can occur.
73
 
   */
74
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
75
 
  DateTime temporal;
76
 
  if (! temporal.from_string(from, (size_t) len))
77
 
  {
78
 
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), from);
79
 
    return 2;
80
 
  }
81
 
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
82
 
  uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
83
 
  int4store(ptr, int_value);
84
 
  return 0;
85
 
}
86
 
 
87
 
int Field_date::store(double from)
88
 
{
89
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
90
 
  if (from < 0.0 || from > 99991231235959.0)
91
 
  {
92
 
    /* Convert the double to a string using stringstream */
93
 
    std::stringstream ss;
94
 
    std::string tmp;
95
 
    ss.precision(18); /* 18 places should be fine for error display of double input. */
96
 
    ss << from; ss >> tmp;
97
 
 
98
 
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
99
 
    return 2;
100
 
  }
101
 
  return Field_date::store((int64_t) rint(from), false);
102
 
}
103
 
 
104
 
int Field_date::store(int64_t from, bool)
105
 
{
106
 
  /* 
107
 
   * Try to create a DateTime from the supplied integer.  Throw an error
108
 
   * if unable to create a valid DateTime.  
109
 
   */
110
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
111
 
  DateTime temporal;
112
 
  if (! temporal.from_int64_t(from))
113
 
  {
114
 
    /* Convert the integer to a string using boost::lexical_cast */
115
 
    std::string tmp(boost::lexical_cast<std::string>(from)); 
116
 
 
117
 
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
118
 
    return 2;
119
 
  }
120
 
 
121
 
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
122
 
  uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
123
 
  int4store(ptr, int_value);
124
 
 
125
 
  return 0;
126
 
}
127
 
 
128
 
int Field_date::store_time(type::Time &ltime,
129
 
                           type::timestamp_t time_type)
130
 
{
131
 
  long tmp;
132
 
  int error= 0;
133
 
  if (time_type == type::DRIZZLE_TIMESTAMP_DATE || time_type == type::DRIZZLE_TIMESTAMP_DATETIME)
134
 
  {
135
 
    tmp= ltime.year*10000 + ltime.month*100 + ltime.day;
136
 
 
137
 
    Session *session= getTable() ? getTable()->in_use : current_session;
138
 
    type::cut_t cut_error= type::VALID;
139
 
    if (ltime.check(tmp != 0,
140
 
                     (TIME_FUZZY_DATE |
141
 
                      (session->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))), cut_error))
142
 
    {
143
 
      char buff[type::Time::MAX_STRING_LENGTH];
144
 
      String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
145
 
      ltime.convert(str, type::DRIZZLE_TIMESTAMP_DATE);
146
 
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
147
 
                           str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
148
 
    }
149
 
 
150
 
    error= static_cast<int>(cut_error);
151
 
 
152
 
    if (not error && ltime.time_type != type::DRIZZLE_TIMESTAMP_DATE &&
153
 
        (ltime.hour || ltime.minute || ltime.second || ltime.second_part))
154
 
    {
155
 
      char buff[type::Time::MAX_STRING_LENGTH];
156
 
      String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
157
 
      ltime.convert(str);
158
 
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE,
159
 
                           ER_WARN_DATA_TRUNCATED,
160
 
                           str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
161
 
      error= 3;
162
 
    }
163
 
  }
164
 
  else
165
 
  {
166
 
    tmp=0;
167
 
    error= 1;
168
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
169
 
  }
170
 
 
171
 
  int4store(ptr,tmp);
172
 
 
173
 
  return error;
174
 
}
175
 
 
176
 
double Field_date::val_real(void) const
177
 
{
178
 
  return (double) Field_date::val_int();
179
 
}
180
 
 
181
 
int64_t Field_date::val_int(void) const
182
 
{
183
 
  uint32_t j;
184
 
 
185
 
  ASSERT_COLUMN_MARKED_FOR_READ;
186
 
 
187
 
  j= uint4korr(ptr);
188
 
 
189
 
  return (int64_t) j;
190
 
}
191
 
 
192
 
String *Field_date::val_str(String *val_buffer, String *) const
193
 
{
194
 
  val_buffer->alloc(field_length);
195
 
  val_buffer->length(field_length);
196
 
  uint32_t tmp=(uint32_t) uint4korr(ptr);
197
 
  int32_t part;
198
 
  char *pos=(char*) val_buffer->ptr()+10;
199
 
 
200
 
  ASSERT_COLUMN_MARKED_FOR_READ;
201
 
 
202
 
  /* Open coded to get more speed */
203
 
  *pos--=0;                                     // End NULL
204
 
  part=(int32_t) (tmp % 100);
205
 
  *pos--= (char) ('0'+part%10);
206
 
  *pos--= (char) ('0'+part/10);
207
 
  *pos--= '-';
208
 
  part=(int32_t) (tmp/100%100);
209
 
  *pos--= (char) ('0'+part%10);
210
 
  *pos--= (char) ('0'+part/10);
211
 
  *pos--= '-';
212
 
  part=(int32_t) (tmp/10000);
213
 
  *pos--= (char) ('0'+part%10); part/=10;
214
 
  *pos--= (char) ('0'+part%10); part/=10;
215
 
  *pos--= (char) ('0'+part%10); part/=10;
216
 
  *pos=   (char) ('0'+part);
217
 
  return val_buffer;
218
 
}
219
 
 
220
 
bool Field_date::get_date(type::Time &ltime, uint32_t fuzzydate) const
221
 
{
222
 
  uint32_t tmp=(uint32_t) uint4korr(ptr);
223
 
  ltime.day=            (int) (tmp%100);
224
 
  ltime.month=  (int) (tmp/100%100);
225
 
  ltime.year=           (int) (tmp/10000);
226
 
  ltime.time_type= type::DRIZZLE_TIMESTAMP_DATE;
227
 
  ltime.hour= ltime.minute= ltime.second= ltime.second_part= ltime.neg= 0;
228
 
 
229
 
  return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime.month || !ltime.day)) ?
230
 
          1 : 0);
231
 
}
232
 
 
233
 
bool Field_date::get_time(type::Time &ltime) const
234
 
{
235
 
  return Field_date::get_date(ltime ,0);
236
 
}
237
 
 
238
 
int Field_date::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
239
 
{
240
 
  uint32_t a,b;
241
 
  a=(uint32_t) uint4korr(a_ptr);
242
 
  b=(uint32_t) uint4korr(b_ptr);
243
 
  return (a < b) ? -1 : (a > b) ? 1 : 0;
244
 
}
245
 
 
246
 
void Field_date::sort_string(unsigned char *to,uint32_t )
247
 
{
248
 
  to[0] = ptr[3];
249
 
  to[1] = ptr[2];
250
 
  to[2] = ptr[1];
251
 
  to[3] = ptr[0];
252
 
}
253
 
 
254
 
void Field_date::sql_type(String &res) const
255
 
{
256
 
  res.set_ascii(STRING_WITH_LEN("date"));
257
 
}
258
 
 
259
 
} /* namespace drizzled */