~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/datetime.cc

  • Committer: Stewart Smith
  • Date: 2009-02-16 04:19:00 UTC
  • mfrom: (869.2.6 dollhouse)
  • mto: This revision was merged to the branch mainline in revision 887.
  • Revision ID: stewart@flamingspork.com-20090216041900-idx8llj31btz5as3
merge unireg_type removal, mainline

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
 
 
22
 
#include <drizzled/server_includes.h>
23
 
#include <drizzled/field/datetime.h>
24
 
#include <drizzled/error.h>
25
 
#include <drizzled/table.h>
26
 
#include <drizzled/session.h>
 
21
#include "drizzled/server_includes.h"
 
22
#include "drizzled/field/datetime.h"
 
23
#include "drizzled/error.h"
 
24
#include "drizzled/table.h"
 
25
#include "drizzled/temporal.h"
 
26
#include "drizzled/session.h"
 
27
 
 
28
#include <sstream>
 
29
#include <string>
 
30
 
27
31
#include CMATH_H
28
32
 
29
33
#if defined(CMATH_NAMESPACE)
41
45
                          uint32_t len,
42
46
                          const CHARSET_INFO * const )
43
47
{
 
48
#ifdef NOTDEFINED
44
49
  DRIZZLE_TIME time_tmp;
45
50
  int error;
46
51
  uint64_t tmp= 0;
70
75
  else
71
76
#endif
72
77
    int64_tstore(ptr,tmp);
73
 
  return error;
74
 
}
75
 
 
76
 
 
77
 
int Field_datetime::store(double nr)
78
 
{
79
 
  int error= 0;
80
 
  if (nr < 0.0 || nr > 99991231235959.0)
81
 
  {
82
 
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
83
 
                         ER_WARN_DATA_OUT_OF_RANGE,
84
 
                         nr, DRIZZLE_TIMESTAMP_DATETIME);
85
 
    nr= 0.0;
86
 
    error= 1;
87
 
  }
88
 
  error|= Field_datetime::store((int64_t) rint(nr), false);
89
 
  return error;
90
 
}
91
 
 
92
 
 
93
 
int Field_datetime::store(int64_t nr,
94
 
                          bool )
95
 
{
96
 
  DRIZZLE_TIME not_used;
97
 
  int error;
98
 
  int64_t initial_nr= nr;
99
 
  Session *session= table ? table->in_use : current_session;
100
 
 
101
 
  nr= number_to_datetime(nr, &not_used, (TIME_FUZZY_DATE |
102
 
                                         (session->variables.sql_mode &
103
 
                                          (MODE_NO_ZERO_DATE |
104
 
                                           MODE_INVALID_DATES))), &error);
105
 
 
106
 
  if (nr == INT64_C(-1))
107
 
  {
108
 
    nr= 0;
109
 
    error= 2;
110
 
  }
111
 
 
112
 
  if (error)
113
 
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
114
 
                         error == 2 ? ER_WARN_DATA_OUT_OF_RANGE :
115
 
                         ER_WARN_DATA_TRUNCATED, initial_nr,
116
 
                         DRIZZLE_TIMESTAMP_DATETIME, 1);
117
 
 
118
 
#ifdef WORDS_BIGENDIAN
119
 
  if (table && table->s->db_low_byte_first)
120
 
  {
121
 
    int8store(ptr,nr);
122
 
  }
123
 
  else
124
 
#endif
125
 
    int64_tstore(ptr,nr);
126
 
  return error;
127
 
}
128
 
 
 
78
#endif /* NOTDEFINED */
 
79
  /* 
 
80
   * Try to create a DateTime from the supplied string.  Throw an error
 
81
   * if unable to create a valid DateTime.  
 
82
   */
 
83
  drizzled::DateTime temporal;
 
84
  if (! temporal.from_string(from, (size_t) len))
 
85
  {
 
86
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), from);
 
87
    return 2;
 
88
  }
 
89
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
 
90
  int64_t int_value;
 
91
  temporal.to_int64_t(&int_value);
 
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 integer.  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
}
129
156
 
130
157
int Field_datetime::store_time(DRIZZLE_TIME *ltime,
131
158
                               enum enum_drizzle_timestamp_type time_type)