~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/date.cc

  • Committer: Olaf van der Spek
  • Date: 2011-03-23 10:31:37 UTC
  • mto: (2247.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2248.
  • Revision ID: olafvdspek@gmail.com-20110323103137-lwevis2tfchgu18u
Propogate return void

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include "config.h"
 
21
#include <config.h>
22
22
#include <boost/lexical_cast.hpp>
23
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"
 
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
#include <drizzled/system_variables.h>
30
32
 
31
33
#include <math.h>
32
34
 
74
76
  DateTime temporal;
75
77
  if (! temporal.from_string(from, (size_t) len))
76
78
  {
77
 
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), from);
 
79
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), from);
78
80
    return 2;
79
81
  }
80
82
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
94
96
    ss.precision(18); /* 18 places should be fine for error display of double input. */
95
97
    ss << from; ss >> tmp;
96
98
 
97
 
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
99
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
98
100
    return 2;
99
101
  }
100
102
  return Field_date::store((int64_t) rint(from), false);
113
115
    /* Convert the integer to a string using boost::lexical_cast */
114
116
    std::string tmp(boost::lexical_cast<std::string>(from)); 
115
117
 
116
 
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
118
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
117
119
    return 2;
118
120
  }
119
121
 
120
122
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
121
123
  uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
122
124
  int4store(ptr, int_value);
 
125
 
123
126
  return 0;
124
127
}
125
128
 
126
 
int Field_date::store_time(DRIZZLE_TIME *ltime,
127
 
                              enum enum_drizzle_timestamp_type time_type)
 
129
int Field_date::store_time(type::Time &ltime,
 
130
                           type::timestamp_t time_type)
128
131
{
129
132
  long tmp;
130
133
  int error= 0;
131
 
  if (time_type == DRIZZLE_TIMESTAMP_DATE ||
132
 
      time_type == DRIZZLE_TIMESTAMP_DATETIME)
 
134
  if (time_type == type::DRIZZLE_TIMESTAMP_DATE || time_type == type::DRIZZLE_TIMESTAMP_DATETIME)
133
135
  {
134
 
    tmp= ltime->year*10000 + ltime->month*100 + ltime->day;
135
 
    if (check_date(ltime, tmp != 0,
136
 
                   (TIME_FUZZY_DATE |
137
 
                    (current_session->variables.sql_mode &
138
 
                     (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))), &error))
 
136
    tmp= ltime.year*10000 + ltime.month*100 + ltime.day;
 
137
 
 
138
    Session *session= getTable() ? getTable()->in_use : current_session;
 
139
    type::cut_t cut_error= type::VALID;
 
140
    if (ltime.check(tmp != 0,
 
141
                     (TIME_FUZZY_DATE |
 
142
                      (session->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))), cut_error))
139
143
    {
140
 
      char buff[MAX_DATE_STRING_REP_LENGTH];
 
144
      char buff[type::Time::MAX_STRING_LENGTH];
141
145
      String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
142
 
      make_date(ltime, &str);
 
146
      ltime.convert(str, type::DRIZZLE_TIMESTAMP_DATE);
143
147
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
144
 
                           str.ptr(), str.length(), DRIZZLE_TIMESTAMP_DATE, 1);
 
148
                           str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
145
149
    }
146
 
    if (!error && ltime->time_type != DRIZZLE_TIMESTAMP_DATE &&
147
 
        (ltime->hour || ltime->minute || ltime->second || ltime->second_part))
 
150
 
 
151
    error= static_cast<int>(cut_error);
 
152
 
 
153
    if (not error && ltime.time_type != type::DRIZZLE_TIMESTAMP_DATE &&
 
154
        (ltime.hour || ltime.minute || ltime.second || ltime.second_part))
148
155
    {
149
 
      char buff[MAX_DATE_STRING_REP_LENGTH];
 
156
      char buff[type::Time::MAX_STRING_LENGTH];
150
157
      String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
151
 
      make_datetime(ltime, &str);
 
158
      ltime.convert(str);
152
159
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE,
153
160
                           ER_WARN_DATA_TRUNCATED,
154
 
                           str.ptr(), str.length(), DRIZZLE_TIMESTAMP_DATE, 1);
 
161
                           str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
155
162
      error= 3;
156
163
    }
157
164
  }
161
168
    error= 1;
162
169
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
163
170
  }
 
171
 
164
172
  int4store(ptr,tmp);
 
173
 
165
174
  return error;
166
175
}
167
176
 
168
 
double Field_date::val_real(void)
 
177
double Field_date::val_real(void) const
169
178
{
170
179
  return (double) Field_date::val_int();
171
180
}
172
181
 
173
 
int64_t Field_date::val_int(void)
 
182
int64_t Field_date::val_int(void) const
174
183
{
175
184
  uint32_t j;
176
185
 
181
190
  return (int64_t) j;
182
191
}
183
192
 
184
 
String *Field_date::val_str(String *val_buffer, String *)
 
193
String *Field_date::val_str(String *val_buffer, String *) const
185
194
{
186
195
  val_buffer->alloc(field_length);
187
196
  val_buffer->length(field_length);
209
218
  return val_buffer;
210
219
}
211
220
 
212
 
bool Field_date::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
221
bool Field_date::get_date(type::Time &ltime, uint32_t fuzzydate) const
213
222
{
214
223
  uint32_t tmp=(uint32_t) uint4korr(ptr);
215
 
  ltime->day=           (int) (tmp%100);
216
 
  ltime->month=         (int) (tmp/100%100);
217
 
  ltime->year=          (int) (tmp/10000);
218
 
  ltime->time_type= DRIZZLE_TIMESTAMP_DATE;
219
 
  ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
220
 
  return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ?
 
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)) ?
221
231
          1 : 0);
222
232
}
223
233
 
224
 
bool Field_date::get_time(DRIZZLE_TIME *ltime)
 
234
bool Field_date::get_time(type::Time &ltime) const
225
235
{
226
 
  return Field_date::get_date(ltime,0);
 
236
  return Field_date::get_date(ltime ,0);
227
237
}
228
238
 
229
239
int Field_date::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)