18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
#ifdef USE_PRAGMA_IMPLEMENTATION
22
#pragma implementation // gcc: Class implementation
25
22
#include <drizzled/server_includes.h>
26
23
#include <drizzled/field/timestamp.h>
24
#include <drizzled/error.h>
25
#include <drizzled/tztime.h>
26
#include <drizzled/table.h>
27
#include <drizzled/session.h>
30
#if defined(CMATH_NAMESPACE)
31
using namespace CMATH_NAMESPACE;
29
TIMESTAMP type holds datetime values in range from 1970-01-01 00:00:01 UTC to
30
2038-01-01 00:00:00 UTC stored as number of seconds since Unix
35
TIMESTAMP type holds datetime values in range from 1970-01-01 00:00:01 UTC to
36
2038-01-01 00:00:00 UTC stored as number of seconds since Unix
33
Up to one of timestamps columns in the table can be automatically
39
Up to one of timestamps columns in the table can be automatically
34
40
set on row update and/or have NOW() as default value.
35
TABLE::timestamp_field points to Field object for such timestamp with
41
TABLE::timestamp_field points to Field object for such timestamp with
36
42
auto-set-on-update. TABLE::time_stamp holds offset in record + 1 for this
37
43
field, and is used by handler code which performs updates required.
39
45
Actually SQL-99 says that we should allow niladic functions (like NOW())
40
as defaults for any field. Current limitations (only NOW() and only
41
for one TIMESTAMP field) are because of restricted binary .frm format
46
as defaults for any field. Current limitations (only NOW() and only
47
for one TIMESTAMP field) are because of restricted binary .frm format
42
48
and should go away in the future.
44
50
Also because of this limitation of binary .frm format we use 5 different
45
51
unireg_check values with TIMESTAMP field to distinguish various cases of
46
52
DEFAULT or ON UPDATE values. These values are:
48
54
TIMESTAMP_OLD_FIELD - old timestamp, if there was not any fields with
49
auto-set-on-update (or now() as default) in this table before, then this
50
field has NOW() as default and is updated when row changes, else it is
55
auto-set-on-update (or now() as default) in this table before, then this
56
field has NOW() as default and is updated when row changes, else it is
51
57
field which has 0 as default value and is not automatically updated.
52
58
TIMESTAMP_DN_FIELD - field with NOW() as default but not set on update
53
59
automatically (TIMESTAMP DEFAULT NOW())
54
TIMESTAMP_UN_FIELD - field which is set on update automatically but has not
55
NOW() as default (but it may has 0 or some other const timestamp as
60
TIMESTAMP_UN_FIELD - field which is set on update automatically but has not
61
NOW() as default (but it may has 0 or some other const timestamp as
56
62
default) (TIMESTAMP ON UPDATE NOW()).
57
TIMESTAMP_DNUN_FIELD - field which has now() as default and is auto-set on
63
TIMESTAMP_DNUN_FIELD - field which has now() as default and is auto-set on
58
64
update. (TIMESTAMP DEFAULT NOW() ON UPDATE NOW())
59
NONE - field which is not auto-set on update with some other than NOW()
65
NONE - field which is not auto-set on update with some other than NOW()
60
66
default value (TIMESTAMP DEFAULT 0).
62
Note that TIMESTAMP_OLD_FIELDs are never created explicitly now, they are
63
left only for preserving ability to read old tables. Such fields replaced
64
with their newer analogs in CREATE TABLE and in SHOW CREATE TABLE. This is
65
because we want to prefer NONE unireg_check before TIMESTAMP_OLD_FIELD for
66
"TIMESTAMP DEFAULT 'Const'" field. (Old timestamps allowed such
67
specification too but ignored default value for first timestamp, which of
68
Note that TIMESTAMP_OLD_FIELDs are never created explicitly now, they are
69
left only for preserving ability to read old tables. Such fields replaced
70
with their newer analogs in CREATE TABLE and in SHOW CREATE TABLE. This is
71
because we want to prefer NONE unireg_check before TIMESTAMP_OLD_FIELD for
72
"TIMESTAMP DEFAULT 'Const'" field. (Old timestamps allowed such
73
specification too but ignored default value for first timestamp, which of
68
74
course is non-standard.) In most cases user won't notice any change, only
69
75
exception is different behavior of old/new timestamps during ALTER TABLE.
72
78
Field_timestamp::Field_timestamp(unsigned char *ptr_arg,
73
uint32_t len_arg __attribute__((unused)),
74
80
unsigned char *null_ptr_arg, unsigned char null_bit_arg,
75
81
enum utype unireg_check_arg,
76
82
const char *field_name_arg,
434
437
void Field_timestamp::set_time()
436
THD *thd= table ? table->in_use : current_thd;
437
long tmp= (long) thd->query_start();
439
Session *session= table ? table->in_use : current_session;
440
long tmp= (long) session->query_start();
439
442
store_timestamp(tmp);
446
void Field_timestamp::set_default()
448
if (table->timestamp_field == this &&
449
unireg_check != TIMESTAMP_UN_FIELD)
452
Field::set_default();
455
long Field_timestamp::get_timestamp(bool *null_value)
457
if ((*null_value= is_null()))
459
#ifdef WORDS_BIGENDIAN
460
if (table && table->s->db_low_byte_first)
461
return sint4korr(ptr);
469
void Field_timestamp::store_timestamp(time_t timestamp)
471
#ifdef WORDS_BIGENDIAN
472
if (table && table->s->db_low_byte_first)
474
int4store(ptr,timestamp);
478
longstore(ptr,(uint32_t) timestamp);