~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* -*- mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"
#include <boost/lexical_cast.hpp>
#include <drizzled/field/microtime.h>
#include <drizzled/error.h>
#include <drizzled/tztime.h>
#include <drizzled/table.h>
#include <drizzled/session.h>

#include <math.h>

#include <sstream>

#include <boost/date_time/posix_time/posix_time.hpp>

#include "drizzled/temporal.h"

namespace drizzled
{

namespace field
{

static boost::posix_time::ptime _epoch(boost::gregorian::date(1970, 1, 1));

Microtime::Microtime(unsigned char *ptr_arg,
                     unsigned char *null_ptr_arg,
                     unsigned char null_bit_arg,
                     enum utype unireg_check_arg,
                     const char *field_name_arg,
                     drizzled::TableShare *share) :
  Epoch(ptr_arg,
        null_ptr_arg,
        null_bit_arg,
        unireg_check_arg,
        field_name_arg,
        share)
{
}

Microtime::Microtime(bool maybe_null_arg,
                     const char *field_name_arg) :
  Epoch(maybe_null_arg,
        field_name_arg)
{
}

int Microtime::store(const char *from,
                     uint32_t len,
                     const CHARSET_INFO * const )
{
  MicroTimestamp temporal;

  ASSERT_COLUMN_MARKED_FOR_WRITE;

  if (not temporal.from_string(from, (size_t) len))
  {
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
    return 1;
  }

  struct timeval tmp;
  temporal.to_timeval(tmp);

  uint64_t tmp_seconds= tmp.tv_sec;
  uint32_t tmp_micro= tmp.tv_usec;

  pack_num(tmp_seconds);
  pack_num(tmp_micro, ptr +8);

  return 0;
}

int Microtime::store_time(type::Time *ltime, type::timestamp_t )
{
  long my_timezone;
  bool in_dst_time_gap;

  type::Time::epoch_t time_tmp= my_system_gmt_sec(ltime, &my_timezone, &in_dst_time_gap, true);
  uint64_t tmp_seconds= time_tmp;
  uint32_t tmp_micro= ltime->second_part;

  pack_num(tmp_seconds);
  pack_num(tmp_micro, ptr +8);

  return 0;
}

int Microtime::store(double from)
{
  ASSERT_COLUMN_MARKED_FOR_WRITE;

  uint64_t from_tmp= (uint64_t)from;
  type::Time::usec_t fractional_seconds= (type::Time::usec_t)((from - from_tmp) * type::Time::FRACTIONAL_DIGITS) % type::Time::FRACTIONAL_DIGITS;

  MicroTimestamp temporal;
  if (not temporal.from_int64_t(from_tmp))
  {
    /* Convert the integer to a string using boost::lexical_cast */
    std::string tmp(boost::lexical_cast<std::string>(from));

    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
    return 2;
  }

  time_t tmp;
  temporal.to_time_t(tmp);

  uint64_t tmp_micro= tmp;
  pack_num(tmp_micro);
  pack_num(fractional_seconds, ptr +8);

  return 0;
}

int Microtime::store(int64_t from, bool)
{
  ASSERT_COLUMN_MARKED_FOR_WRITE;

  MicroTimestamp temporal;
  if (not temporal.from_int64_t(from))
  {
    /* Convert the integer to a string using boost::lexical_cast */
    std::string tmp(boost::lexical_cast<std::string>(from));

    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
    return 2;
  }

  time_t tmp;
  temporal.to_time_t(tmp);

  uint64_t tmp_micro= tmp;
  pack_num(tmp_micro);
  pack_num(static_cast<uint32_t>(0), ptr +8);

  return 0;
}

double Microtime::val_real(void)
{
  uint64_t temp;
  type::Time::usec_t micro_temp;

  ASSERT_COLUMN_MARKED_FOR_READ;

  unpack_num(temp);
  unpack_num(micro_temp, ptr +8);

  Timestamp temporal;
  (void) temporal.from_time_t((time_t) temp);

  /* We must convert into a "timestamp-formatted integer" ... */
  int64_t result;
  temporal.to_int64_t(&result);

  result+= micro_temp % type::Time::FRACTIONAL_DIGITS;

  return result;
}

type::Decimal *Microtime::val_decimal(type::Decimal *decimal_value)
{
  type::Time ltime;

  get_date(&ltime, 0);

  return date2_class_decimal(&ltime, decimal_value);
}

int64_t Microtime::val_int(void)
{
  uint64_t temp;

  ASSERT_COLUMN_MARKED_FOR_READ;

  unpack_num(temp);

  Timestamp temporal;
  (void) temporal.from_time_t((time_t) temp);

  /* We must convert into a "timestamp-formatted integer" ... */
  int64_t result;
  temporal.to_int64_t(&result);

  return result;
}

String *Microtime::val_str(String *val_buffer, String *)
{
  uint64_t temp= 0;
  type::Time::usec_t micro_temp= 0;

  unpack_num(temp);
  unpack_num(micro_temp, ptr +8);

  type::Time tmp_time;
  tmp_time.store(temp, micro_temp);

  tmp_time.convert(*val_buffer);


  return val_buffer;
}

bool Microtime::get_date(type::Time *ltime, uint32_t)
{
  uint64_t temp;
  uint32_t micro_temp= 0;

  unpack_num(temp);
  unpack_num(micro_temp, ptr +8);
  
  ltime->reset();

  ltime->store(temp, micro_temp);

  return false;
}

bool Microtime::get_time(type::Time *ltime)
{
  return Microtime::get_date(ltime, 0);
}

int Microtime::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
{
  uint64_t a,b;
  uint32_t a_micro, b_micro;

  unpack_num(a, a_ptr);
  unpack_num(a_micro, a_ptr +8);

  unpack_num(b, b_ptr);
  unpack_num(b_micro, b_ptr +8);

  if (a == b)
    return (a_micro < b_micro) ? -1 : (a_micro > b_micro) ? 1 : 0;

  return (a < b) ? -1 : (a > b) ? 1 : 0;
}


void Microtime::sort_string(unsigned char *to,uint32_t )
{
#ifdef WORDS_BIGENDIAN
  if ((not getTable()) or (not getTable()->getShare()->db_low_byte_first))
  {
    std::reverse_copy(to, to+pack_length(), ptr);
    std::reverse_copy(to +8, to+pack_length(), ptr +8);
  }
  else
#endif
  {
    memcpy(to, ptr, pack_length());
  }
}

void Microtime::sql_type(String &res) const
{
  res.set_ascii(STRING_WITH_LEN("microsecond timestamp"));
}

void Microtime::set_time()
{
  Session *session= getTable() ? getTable()->in_use : current_session;

  type::Time::usec_t fractional_seconds= 0;
  uint64_t epoch_seconds= session->getCurrentTimestampEpoch(fractional_seconds);

  set_notnull();
  pack_num(epoch_seconds);
  pack_num(fractional_seconds, ptr +8);
}

long Microtime::get_timestamp(bool *null_value)
{
  if ((*null_value= is_null()))
    return 0;

  uint64_t tmp;
  return unpack_num(tmp);
}

} /* namespace field */
} /* namespace drizzled */