~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/epoch.cc

  • Committer: Brian Aker
  • Date: 2010-12-27 20:04:50 UTC
  • mto: (2060.2.1 clean)
  • mto: This revision was merged to the branch mainline in revision 2063.
  • Revision ID: brian@tangent.org-20101227200450-dmxpemwyfmlinlnm
Merge in first pass.

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>
22
 
 
 
21
#include "config.h"
23
22
#include <boost/lexical_cast.hpp>
24
23
#include <drizzled/field/epoch.h>
25
24
#include <drizzled/error.h>
26
25
#include <drizzled/tztime.h>
27
26
#include <drizzled/table.h>
28
27
#include <drizzled/session.h>
29
 
#include <drizzled/current_session.h>
30
28
 
31
29
#include <math.h>
32
30
 
33
31
#include <sstream>
34
32
 
35
 
#include <drizzled/temporal.h>
 
33
#include "drizzled/temporal.h"
36
34
 
37
35
namespace drizzled
38
36
{
84
82
  exception is different behavior of old/new timestamps during ALTER TABLE.
85
83
 */
86
84
  Epoch::Epoch(unsigned char *ptr_arg,
 
85
               uint32_t,
87
86
               unsigned char *null_ptr_arg,
88
87
               unsigned char null_bit_arg,
89
88
               enum utype unireg_check_arg,
90
89
               const char *field_name_arg,
91
 
               drizzled::TableShare *share) :
 
90
               drizzled::TableShare *share,
 
91
               const drizzled::CHARSET_INFO * const cs) :
92
92
  Field_str(ptr_arg,
93
 
            MicroTimestamp::MAX_STRING_LENGTH - 1, /* no \0 */
 
93
            DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
94
94
            null_ptr_arg,
95
95
            null_bit_arg,
96
96
            field_name_arg,
97
 
            &my_charset_bin)
 
97
            cs)
98
98
{
99
99
  unireg_check= unireg_check_arg;
100
100
  if (! share->getTimestampField() && unireg_check != NONE)
108
108
}
109
109
 
110
110
Epoch::Epoch(bool maybe_null_arg,
111
 
             const char *field_name_arg) :
 
111
             const char *field_name_arg,
 
112
             const CHARSET_INFO * const cs) :
112
113
  Field_str((unsigned char*) NULL,
113
 
            MicroTimestamp::MAX_STRING_LENGTH - 1, /* no \0 */
 
114
            DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
114
115
            maybe_null_arg ? (unsigned char*) "": 0,
115
116
            0,
116
117
            field_name_arg,
117
 
            &my_charset_bin)
 
118
            cs)
118
119
{
119
120
  if (unireg_check != TIMESTAMP_DN_FIELD)
120
121
    flags|= ON_UPDATE_NOW_FLAG;
164
165
 
165
166
  if (not temporal.from_string(from, (size_t) len))
166
167
  {
167
 
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
 
168
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
168
169
    return 1;
169
170
  }
170
171
 
171
172
  time_t tmp;
172
173
  temporal.to_time_t(tmp);
173
174
 
174
 
  uint64_t time_tmp= tmp;
175
 
  pack_num(time_tmp);
 
175
  pack_num(tmp);
176
176
  return 0;
177
177
}
178
178
 
180
180
{
181
181
  ASSERT_COLUMN_MARKED_FOR_WRITE;
182
182
 
183
 
  uint64_t from_tmp= (uint64_t)from;
184
 
 
185
 
  Timestamp temporal;
186
 
  if (not temporal.from_int64_t(from_tmp))
 
183
  if (from < 0 || from > 99991231235959.0)
187
184
  {
188
 
    /* Convert the integer to a string using boost::lexical_cast */
189
 
    std::string tmp(boost::lexical_cast<std::string>(from));
 
185
    /* Convert the double to a string using stringstream */
 
186
    std::stringstream ss;
 
187
    std::string tmp;
 
188
    ss.precision(18); /* 18 places should be fine for error display of double input. */
 
189
    ss << from; 
 
190
    ss >> tmp;
190
191
 
191
 
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
192
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
192
193
    return 2;
193
194
  }
194
 
 
195
 
  time_t tmp;
196
 
  temporal.to_time_t(tmp);
197
 
 
198
 
  uint64_t tmp_micro= tmp;
199
 
  pack_num(tmp_micro);
200
 
 
201
 
  return 0;
202
 
}
203
 
 
204
 
int Epoch::store_decimal(const type::Decimal *value)
205
 
{
206
 
  double tmp;
207
 
  value->convert(tmp);
208
 
 
209
 
  return store(tmp);
 
195
  return Epoch::store((int64_t) rint(from), false);
210
196
}
211
197
 
212
198
int Epoch::store(int64_t from, bool)
218
204
   * if unable to create a valid DateTime.  
219
205
   */
220
206
  Timestamp temporal;
221
 
  if (not temporal.from_int64_t(from))
 
207
  if (! temporal.from_int64_t(from))
222
208
  {
223
209
    /* Convert the integer to a string using boost::lexical_cast */
224
210
    std::string tmp(boost::lexical_cast<std::string>(from));
225
211
 
226
 
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
212
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
227
213
    return 2;
228
214
  }
229
215
 
230
216
  time_t tmp;
231
217
  temporal.to_time_t(tmp);
232
218
 
233
 
  uint64_t tmp64= tmp;
234
 
  pack_num(tmp64);
 
219
  pack_num(tmp);
235
220
 
236
221
  return 0;
237
222
}
238
223
 
239
 
double Epoch::val_real(void) const
 
224
double Epoch::val_real(void)
240
225
{
241
226
  return (double) Epoch::val_int();
242
227
}
243
228
 
244
 
int64_t Epoch::val_int(void) const
 
229
int64_t Epoch::val_int(void)
245
230
{
246
231
  uint64_t temp;
247
232
 
258
243
  return result;
259
244
}
260
245
 
261
 
String *Epoch::val_str(String *val_buffer, String *) const
 
246
String *Epoch::val_str(String *val_buffer, String *)
262
247
{
263
248
  uint64_t temp= 0;
264
249
  char *to;
282
267
  return val_buffer;
283
268
}
284
269
 
285
 
bool Epoch::get_date(type::Time &ltime, uint32_t) const
 
270
bool Epoch::get_date(DRIZZLE_TIME *ltime, uint32_t)
286
271
{
287
272
  uint64_t temp;
288
 
  type::Time::epoch_t time_temp;
289
273
 
290
274
  unpack_num(temp);
291
 
  time_temp= temp;
292
275
  
293
 
  ltime.reset();
294
 
 
295
 
  ltime.store(time_temp);
 
276
  memset(ltime, 0, sizeof(*ltime));
 
277
 
 
278
  Timestamp temporal;
 
279
  (void) temporal.from_time_t((time_t) temp);
 
280
 
 
281
  /* @TODO Goodbye the below code when DRIZZLE_TIME is finally gone.. */
 
282
 
 
283
  ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME;
 
284
  ltime->year= temporal.years();
 
285
  ltime->month= temporal.months();
 
286
  ltime->day= temporal.days();
 
287
  ltime->hour= temporal.hours();
 
288
  ltime->minute= temporal.minutes();
 
289
  ltime->second= temporal.seconds();
296
290
 
297
291
  return 0;
298
292
}
299
293
 
300
 
bool Epoch::get_time(type::Time &ltime) const
 
294
bool Epoch::get_time(DRIZZLE_TIME *ltime)
301
295
{
302
 
  return Epoch::get_date(ltime, 0);
 
296
  return Epoch::get_date(ltime,0);
303
297
}
304
298
 
305
299
int Epoch::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
349
343
void Epoch::set_time()
350
344
{
351
345
  Session *session= getTable() ? getTable()->in_use : current_session;
352
 
  time_t tmp= session->getCurrentTimestampEpoch();
353
 
 
 
346
  time_t tmp= session->query_start();
354
347
  set_notnull();
355
 
  pack_num(static_cast<uint32_t>(tmp));
 
348
  pack_num(tmp);
356
349
}
357
350
 
358
351
void Epoch::set_default()
368
361
  }
369
362
}
370
363
 
371
 
long Epoch::get_timestamp(bool *null_value) const
 
364
long Epoch::get_timestamp(bool *null_value)
372
365
{
373
366
  if ((*null_value= is_null()))
374
367
    return 0;