~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/microtime.cc

Renamed namespace slot to namespace service.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; either version 2 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 
 */
20
 
 
21
 
#include <config.h>
22
 
#include <boost/lexical_cast.hpp>
23
 
#include <drizzled/field/microtime.h>
24
 
#include <drizzled/error.h>
25
 
#include <drizzled/tztime.h>
26
 
#include <drizzled/table.h>
27
 
#include <drizzled/session.h>
28
 
#include <drizzled/current_session.h>
29
 
 
30
 
#include <math.h>
31
 
 
32
 
#include <sstream>
33
 
 
34
 
#include <boost/date_time/posix_time/posix_time.hpp>
35
 
 
36
 
#include <drizzled/temporal.h>
37
 
 
38
 
namespace drizzled
39
 
{
40
 
 
41
 
namespace field
42
 
{
43
 
 
44
 
static boost::posix_time::ptime _epoch(boost::gregorian::date(1970, 1, 1));
45
 
 
46
 
Microtime::Microtime(unsigned char *ptr_arg,
47
 
                     unsigned char *null_ptr_arg,
48
 
                     unsigned char null_bit_arg,
49
 
                     enum utype unireg_check_arg,
50
 
                     const char *field_name_arg,
51
 
                     drizzled::TableShare *share) :
52
 
  Epoch(ptr_arg,
53
 
        null_ptr_arg,
54
 
        null_bit_arg,
55
 
        unireg_check_arg,
56
 
        field_name_arg,
57
 
        share)
58
 
{
59
 
}
60
 
 
61
 
Microtime::Microtime(bool maybe_null_arg,
62
 
                     const char *field_name_arg) :
63
 
  Epoch(maybe_null_arg,
64
 
        field_name_arg)
65
 
{
66
 
}
67
 
 
68
 
int Microtime::store(const char *from,
69
 
                     uint32_t len,
70
 
                     const CHARSET_INFO * const )
71
 
{
72
 
  MicroTimestamp temporal;
73
 
 
74
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
75
 
 
76
 
  if (not temporal.from_string(from, (size_t) len))
77
 
  {
78
 
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
79
 
    return 1;
80
 
  }
81
 
 
82
 
  struct timeval tmp;
83
 
  temporal.to_timeval(tmp);
84
 
 
85
 
  uint64_t tmp_seconds= tmp.tv_sec;
86
 
  uint32_t tmp_micro= tmp.tv_usec;
87
 
 
88
 
  pack_num(tmp_seconds);
89
 
  pack_num(tmp_micro, ptr +8);
90
 
 
91
 
  return 0;
92
 
}
93
 
 
94
 
int Microtime::store_time(type::Time &ltime, type::timestamp_t )
95
 
{
96
 
  long my_timezone;
97
 
  bool in_dst_time_gap;
98
 
 
99
 
  type::Time::epoch_t time_tmp;
100
 
  ltime.convert(time_tmp, &my_timezone, &in_dst_time_gap, true);
101
 
  uint64_t tmp_seconds= time_tmp;
102
 
  uint32_t tmp_micro= ltime.second_part;
103
 
 
104
 
  pack_num(tmp_seconds);
105
 
  pack_num(tmp_micro, ptr +8);
106
 
 
107
 
  return 0;
108
 
}
109
 
 
110
 
int Microtime::store(double from)
111
 
{
112
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
113
 
 
114
 
  uint64_t from_tmp= (uint64_t)from;
115
 
  type::Time::usec_t fractional_seconds= (type::Time::usec_t)((from - from_tmp) * type::Time::FRACTIONAL_DIGITS) % type::Time::FRACTIONAL_DIGITS;
116
 
 
117
 
  MicroTimestamp temporal;
118
 
  if (not temporal.from_int64_t(from_tmp))
119
 
  {
120
 
    /* Convert the integer to a string using boost::lexical_cast */
121
 
    std::string tmp(boost::lexical_cast<std::string>(from));
122
 
 
123
 
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
124
 
    return 2;
125
 
  }
126
 
 
127
 
  time_t tmp;
128
 
  temporal.to_time_t(tmp);
129
 
 
130
 
  uint64_t tmp_micro= tmp;
131
 
  pack_num(tmp_micro);
132
 
  pack_num(fractional_seconds, ptr +8);
133
 
 
134
 
  return 0;
135
 
}
136
 
 
137
 
int Microtime::store(int64_t from, bool)
138
 
{
139
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
140
 
 
141
 
  MicroTimestamp temporal;
142
 
  if (not temporal.from_int64_t(from))
143
 
  {
144
 
    /* Convert the integer to a string using boost::lexical_cast */
145
 
    std::string tmp(boost::lexical_cast<std::string>(from));
146
 
 
147
 
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
148
 
    return 2;
149
 
  }
150
 
 
151
 
  time_t tmp;
152
 
  temporal.to_time_t(tmp);
153
 
 
154
 
  uint64_t tmp_micro= tmp;
155
 
  pack_num(tmp_micro);
156
 
  pack_num(static_cast<uint32_t>(0), ptr +8);
157
 
 
158
 
  return 0;
159
 
}
160
 
 
161
 
double Microtime::val_real(void) const
162
 
{
163
 
  uint64_t temp;
164
 
  type::Time::usec_t micro_temp;
165
 
 
166
 
  ASSERT_COLUMN_MARKED_FOR_READ;
167
 
 
168
 
  unpack_num(temp);
169
 
  unpack_num(micro_temp, ptr +8);
170
 
 
171
 
  Timestamp temporal;
172
 
  (void) temporal.from_time_t((time_t) temp);
173
 
 
174
 
  /* We must convert into a "timestamp-formatted integer" ... */
175
 
  int64_t result;
176
 
  temporal.to_int64_t(&result);
177
 
 
178
 
  result+= micro_temp % type::Time::FRACTIONAL_DIGITS;
179
 
 
180
 
  return result;
181
 
}
182
 
 
183
 
type::Decimal *Microtime::val_decimal(type::Decimal *decimal_value) const
184
 
{
185
 
  type::Time ltime;
186
 
 
187
 
  get_date(ltime, 0);
188
 
 
189
 
  return date2_class_decimal(&ltime, decimal_value);
190
 
}
191
 
 
192
 
int64_t Microtime::val_int(void) const
193
 
{
194
 
  uint64_t temp;
195
 
 
196
 
  ASSERT_COLUMN_MARKED_FOR_READ;
197
 
 
198
 
  unpack_num(temp);
199
 
 
200
 
  Timestamp temporal;
201
 
  (void) temporal.from_time_t((time_t) temp);
202
 
 
203
 
  /* We must convert into a "timestamp-formatted integer" ... */
204
 
  int64_t result;
205
 
  temporal.to_int64_t(&result);
206
 
 
207
 
  return result;
208
 
}
209
 
 
210
 
String *Microtime::val_str(String *val_buffer, String *) const
211
 
{
212
 
  uint64_t temp= 0;
213
 
  type::Time::usec_t micro_temp= 0;
214
 
 
215
 
  unpack_num(temp);
216
 
  unpack_num(micro_temp, ptr +8);
217
 
 
218
 
  type::Time tmp_time;
219
 
  tmp_time.store(temp, micro_temp);
220
 
 
221
 
  tmp_time.convert(*val_buffer);
222
 
 
223
 
 
224
 
  return val_buffer;
225
 
}
226
 
 
227
 
bool Microtime::get_date(type::Time &ltime, uint32_t) const
228
 
{
229
 
  uint64_t temp;
230
 
  uint32_t micro_temp= 0;
231
 
 
232
 
  unpack_num(temp);
233
 
  unpack_num(micro_temp, ptr +8);
234
 
  
235
 
  ltime.reset();
236
 
 
237
 
  ltime.store(temp, micro_temp);
238
 
 
239
 
  return false;
240
 
}
241
 
 
242
 
bool Microtime::get_time(type::Time &ltime) const
243
 
{
244
 
  return Microtime::get_date(ltime, 0);
245
 
}
246
 
 
247
 
int Microtime::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
248
 
{
249
 
  uint64_t a,b;
250
 
  uint32_t a_micro, b_micro;
251
 
 
252
 
  unpack_num(a, a_ptr);
253
 
  unpack_num(a_micro, a_ptr +8);
254
 
 
255
 
  unpack_num(b, b_ptr);
256
 
  unpack_num(b_micro, b_ptr +8);
257
 
 
258
 
  if (a == b)
259
 
    return (a_micro < b_micro) ? -1 : (a_micro > b_micro) ? 1 : 0;
260
 
 
261
 
  return (a < b) ? -1 : (a > b) ? 1 : 0;
262
 
}
263
 
 
264
 
 
265
 
void Microtime::sort_string(unsigned char *to,uint32_t )
266
 
{
267
 
#ifdef WORDS_BIGENDIAN
268
 
  if ((not getTable()) or (not getTable()->getShare()->db_low_byte_first))
269
 
  {
270
 
    std::reverse_copy(to, to+pack_length(), ptr);
271
 
    std::reverse_copy(to +8, to+pack_length(), ptr +8);
272
 
  }
273
 
  else
274
 
#endif
275
 
  {
276
 
    memcpy(to, ptr, pack_length());
277
 
  }
278
 
}
279
 
 
280
 
void Microtime::sql_type(String &res) const
281
 
{
282
 
  res.set_ascii(STRING_WITH_LEN("microsecond timestamp"));
283
 
}
284
 
 
285
 
void Microtime::set_time()
286
 
{
287
 
  Session *session= getTable() ? getTable()->in_use : current_session;
288
 
 
289
 
  type::Time::usec_t fractional_seconds= 0;
290
 
  uint64_t epoch_seconds= session->getCurrentTimestampEpoch(fractional_seconds);
291
 
 
292
 
  set_notnull();
293
 
  pack_num(epoch_seconds);
294
 
  pack_num(fractional_seconds, ptr +8);
295
 
}
296
 
 
297
 
long Microtime::get_timestamp(bool *null_value) const
298
 
{
299
 
  if ((*null_value= is_null()))
300
 
    return 0;
301
 
 
302
 
  uint64_t tmp;
303
 
  return unpack_num(tmp);
304
 
}
305
 
 
306
 
} /* namespace field */
307
 
} /* namespace drizzled */