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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/* -*- 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/epoch.h>
#include <drizzled/error.h>
#include <drizzled/table.h>
#include <drizzled/session.h>
#include <drizzled/session/times.h>
#include <drizzled/current_session.h>
#include <drizzled/temporal.h>
#include <cmath>
#include <sstream>
namespace drizzled {
namespace field {
/**
TIMESTAMP type holds datetime values in range from 1970-01-01 00:00:01 UTC to
2038-01-01 00:00:00 UTC stored as number of seconds since Unix
Epoch in UTC.
Up to one of timestamps columns in the table can be automatically
set on row update and/or have NOW() as default value.
TABLE::timestamp_field points to Field object for such timestamp with
auto-set-on-update. TABLE::time_stamp holds offset in record + 1 for this
field, and is used by handler code which performs updates required.
Actually SQL-99 says that we should allow niladic functions (like NOW())
as defaults for any field. Current limitations (only NOW() and only
for one TIMESTAMP field) are because of restricted binary .frm format
and should go away in the future.
Also because of this limitation of binary .frm format we use 5 different
unireg_check values with TIMESTAMP field to distinguish various cases of
DEFAULT or ON UPDATE values. These values are:
TIMESTAMP_OLD_FIELD - old timestamp, if there was not any fields with
auto-set-on-update (or now() as default) in this table before, then this
field has NOW() as default and is updated when row changes, else it is
field which has 0 as default value and is not automatically updated.
TIMESTAMP_DN_FIELD - field with NOW() as default but not set on update
automatically (TIMESTAMP DEFAULT NOW())
TIMESTAMP_UN_FIELD - field which is set on update automatically but has not
NOW() as default (but it may has 0 or some other const timestamp as
default) (TIMESTAMP ON UPDATE NOW()).
TIMESTAMP_DNUN_FIELD - field which has now() as default and is auto-set on
update. (TIMESTAMP DEFAULT NOW() ON UPDATE NOW())
NONE - field which is not auto-set on update with some other than NOW()
default value (TIMESTAMP DEFAULT 0).
Note that TIMESTAMP_OLD_FIELDs are never created explicitly now, they are
left only for preserving ability to read old tables. Such fields replaced
with their newer analogs in CREATE TABLE and in SHOW CREATE TABLE. This is
because we want to prefer NONE unireg_check before TIMESTAMP_OLD_FIELD for
"TIMESTAMP DEFAULT 'Const'" field. (Old timestamps allowed such
specification too but ignored default value for first timestamp, which of
course is non-standard.) In most cases user won't notice any change, only
exception is different behavior of old/new timestamps during ALTER TABLE.
*/
Epoch::Epoch(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) :
Field_str(ptr_arg,
MicroTimestamp::MAX_STRING_LENGTH - 1, /* no \0 */
null_ptr_arg,
null_bit_arg,
field_name_arg,
&my_charset_bin)
{
unireg_check= unireg_check_arg;
if (! share->getTimestampField() && unireg_check != NONE)
{
/* This timestamp has auto-update */
share->setTimestampField(this);
flags|= FUNCTION_DEFAULT_FLAG;
if (unireg_check != TIMESTAMP_DN_FIELD)
flags|= ON_UPDATE_NOW_FLAG;
}
}
Epoch::Epoch(bool maybe_null_arg,
const char *field_name_arg) :
Field_str((unsigned char*) NULL,
MicroTimestamp::MAX_STRING_LENGTH - 1, /* no \0 */
maybe_null_arg ? (unsigned char*) "": 0,
0,
field_name_arg,
&my_charset_bin)
{
if (unireg_check != TIMESTAMP_DN_FIELD)
flags|= ON_UPDATE_NOW_FLAG;
}
/**
Get auto-set type for TIMESTAMP field.
Returns value indicating during which operations this TIMESTAMP field
should be auto-set to current timestamp.
*/
timestamp_auto_set_type Epoch::get_auto_set_type() const
{
switch (unireg_check)
{
case TIMESTAMP_DN_FIELD:
return TIMESTAMP_AUTO_SET_ON_INSERT;
case TIMESTAMP_UN_FIELD:
return TIMESTAMP_AUTO_SET_ON_UPDATE;
case TIMESTAMP_OLD_FIELD:
/*
Although we can have several such columns in legacy tables this
function should be called only for first of them (i.e. the one
having auto-set property).
*/
assert(getTable()->timestamp_field == this);
/* Fall-through */
case TIMESTAMP_DNUN_FIELD:
return TIMESTAMP_AUTO_SET_ON_BOTH;
default:
/*
Normally this function should not be called for TIMESTAMPs without
auto-set property.
*/
assert(0);
return TIMESTAMP_NO_AUTO_SET;
}
}
int Epoch::store(const char *from,
uint32_t len,
const charset_info_st * const )
{
Timestamp 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;
}
time_t tmp;
temporal.to_time_t(tmp);
uint64_t time_tmp= tmp;
pack_num(time_tmp);
return 0;
}
int Epoch::store(double from)
{
ASSERT_COLUMN_MARKED_FOR_WRITE;
uint64_t from_tmp= (uint64_t)from;
Timestamp 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);
return 0;
}
int Epoch::store_decimal(const type::Decimal *value)
{
double tmp;
value->convert(tmp);
return store(tmp);
}
int Epoch::store(int64_t from, bool)
{
ASSERT_COLUMN_MARKED_FOR_WRITE;
/*
* Try to create a DateTime from the supplied integer. Throw an error
* if unable to create a valid DateTime.
*/
Timestamp 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 tmp64= tmp;
pack_num(tmp64);
return 0;
}
double Epoch::val_real(void) const
{
return (double) Epoch::val_int();
}
int64_t Epoch::val_int(void) const
{
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 *Epoch::val_str(String *val_buffer, String *) const
{
uint64_t temp= 0;
char *to;
int to_len= field_length + 1;
val_buffer->alloc(to_len);
to= (char *) val_buffer->ptr();
unpack_num(temp);
val_buffer->set_charset(&my_charset_bin); /* Safety */
Timestamp temporal;
(void) temporal.from_time_t((time_t) temp);
int rlen;
rlen= temporal.to_string(to, to_len);
assert(rlen < to_len);
val_buffer->length(rlen);
return val_buffer;
}
bool Epoch::get_date(type::Time <ime, uint32_t) const
{
uint64_t temp;
type::epoch_t time_temp;
unpack_num(temp);
time_temp= temp;
ltime.reset();
ltime.store(time_temp);
return 0;
}
bool Epoch::get_time(type::Time <ime) const
{
return Epoch::get_date(ltime, 0);
}
int Epoch::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
{
uint64_t a,b;
unpack_num(a, a_ptr);
unpack_num(b, b_ptr);
return (a < b) ? -1 : (a > b) ? 1 : 0;
}
void Epoch::sort_string(unsigned char *to,uint32_t )
{
#ifdef WORDS_BIGENDIAN
if (!getTable() || !getTable()->getShare()->db_low_byte_first)
{
to[0] = ptr[0];
to[1] = ptr[1];
to[2] = ptr[2];
to[3] = ptr[3];
to[4] = ptr[4];
to[5] = ptr[5];
to[6] = ptr[6];
to[7] = ptr[7];
}
else
#endif
{
to[0] = ptr[7];
to[1] = ptr[6];
to[2] = ptr[5];
to[3] = ptr[4];
to[4] = ptr[3];
to[5] = ptr[2];
to[6] = ptr[1];
to[7] = ptr[0];
}
}
void Epoch::set_time()
{
Session *session= getTable() ? getTable()->in_use : current_session;
time_t tmp= session->times.getCurrentTimestampEpoch();
set_notnull();
pack_num(static_cast<uint32_t>(tmp));
}
void Epoch::set_default()
{
if (getTable()->timestamp_field == this &&
unireg_check != TIMESTAMP_UN_FIELD)
{
set_time();
}
else
{
Field::set_default();
}
}
long Epoch::get_timestamp(bool *null_value) const
{
if ((*null_value= is_null()))
return 0;
uint64_t tmp;
return unpack_num(tmp);
}
size_t Epoch::max_string_length()
{
return sizeof(uint64_t);
}
} /* namespace field */
} /* namespace drizzled */
|