~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/datetime.cc

  • Committer: Brian Aker
  • Date: 2009-02-11 21:23:31 UTC
  • mfrom: (873.1.7 temporal-new)
  • Revision ID: brian@tangent.org-20090211212331-ebvmf4nwqb3nv902
Merge of Jay's work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include "drizzled/temporal.h"
26
26
#include "drizzled/session.h"
27
27
 
 
28
#include <sstream>
 
29
#include <string>
 
30
 
28
31
#include CMATH_H
29
32
 
30
33
#if defined(CMATH_NAMESPACE)
86
89
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
87
90
  int64_t int_value;
88
91
  temporal.to_int64_t(&int_value);
89
 
#ifdef WORDS_BIGENDIAN
90
 
  if (table && table->s->db_low_byte_first)
91
 
  {
92
 
    int8store(ptr, int_value);
93
 
  }
94
 
  else
95
 
#endif
96
 
    int64_tstore(ptr, int_value);
97
 
  return 0;
98
 
}
99
 
 
100
 
int Field_datetime::store(double nr)
101
 
{
102
 
  int error= 0;
103
 
  if (nr < 0.0 || nr > 99991231235959.0)
104
 
  {
105
 
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
106
 
                         ER_WARN_DATA_OUT_OF_RANGE,
107
 
                         nr, DRIZZLE_TIMESTAMP_DATETIME);
108
 
    nr= 0.0;
109
 
    error= 1;
110
 
  }
111
 
  error|= Field_datetime::store((int64_t) rint(nr), false);
112
 
  return error;
113
 
}
114
 
 
115
 
 
116
 
int Field_datetime::store(int64_t nr,
117
 
                          bool )
118
 
{
119
 
  DRIZZLE_TIME not_used;
120
 
  int error;
121
 
  int64_t initial_nr= nr;
122
 
  Session *session= table ? table->in_use : current_session;
123
 
 
124
 
  nr= number_to_datetime(nr, &not_used, (TIME_FUZZY_DATE |
125
 
                                         (session->variables.sql_mode &
126
 
                                          (MODE_NO_ZERO_DATE |
127
 
                                           MODE_INVALID_DATES))), &error);
128
 
 
129
 
  if (nr == INT64_C(-1))
130
 
  {
131
 
    nr= 0;
132
 
    error= 2;
133
 
  }
134
 
 
135
 
  if (error)
136
 
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
137
 
                         error == 2 ? ER_WARN_DATA_OUT_OF_RANGE :
138
 
                         ER_WARN_DATA_TRUNCATED, initial_nr,
139
 
                         DRIZZLE_TIMESTAMP_DATETIME, 1);
140
 
 
141
 
#ifdef WORDS_BIGENDIAN
142
 
  if (table && table->s->db_low_byte_first)
143
 
  {
144
 
    int8store(ptr,nr);
145
 
  }
146
 
  else
147
 
#endif
148
 
    int64_tstore(ptr,nr);
149
 
  return error;
150
 
}
151
 
 
 
92
 
 
93
#ifdef WORDS_BIGENDIAN
 
94
  if (table && table->s->db_low_byte_first)
 
95
  {
 
96
    int8store(ptr, int_value);
 
97
  }
 
98
  else
 
99
#endif
 
100
    int64_tstore(ptr, int_value);
 
101
  return 0;
 
102
}
 
103
 
 
104
int Field_datetime::store(double from)
 
105
{
 
106
  if (from < 0.0 || from > 99991231235959.0)
 
107
  {
 
108
    /* Convert the double to a string using stringstream */
 
109
    std::stringstream ss;
 
110
    std::string tmp;
 
111
    ss.precision(18); /* 18 places should be fine for error display of double input. */
 
112
    ss << from; ss >> tmp;
 
113
 
 
114
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
115
    return 2;
 
116
  }
 
117
  return Field_datetime::store((int64_t) rint(from), false);
 
118
}
 
119
 
 
120
int Field_datetime::store(int64_t from, bool)
 
121
{
 
122
  /* 
 
123
   * Try to create a DateTime from the supplied string.  Throw an error
 
124
   * if unable to create a valid DateTime.  
 
125
   */
 
126
  drizzled::DateTime temporal;
 
127
  if (! temporal.from_int64_t(from))
 
128
  {
 
129
    /* Convert the integer to a string using stringstream */
 
130
    std::stringstream ss;
 
131
    std::string tmp;
 
132
    ss << from; ss >> tmp;
 
133
 
 
134
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
135
    return 2;
 
136
  }
 
137
 
 
138
  /* 
 
139
   * Because "from" may be a silly MySQL-like "datetime number" (like, oh, 101)
 
140
   * we must here get the value of the DateTime as its *real* int64_t, after
 
141
   * the conversion above has been done...yuck. God, save us.
 
142
   */
 
143
  int64_t int_value;
 
144
  temporal.to_int64_t(&int_value);
 
145
 
 
146
#ifdef WORDS_BIGENDIAN
 
147
  if (table && table->s->db_low_byte_first)
 
148
  {
 
149
    int8store(ptr, int_value);
 
150
  }
 
151
  else
 
152
#endif
 
153
    int64_tstore(ptr, int_value);
 
154
  return 0;
 
155
}
152
156
 
153
157
int Field_datetime::store_time(DRIZZLE_TIME *ltime,
154
158
                               enum enum_drizzle_timestamp_type time_type)