summaryrefslogtreecommitdiff
path: root/sql/field.cc
diff options
context:
space:
mode:
authorunknown <dlenev@jabberwock.localdomain>2004-04-02 10:12:53 +0400
committerunknown <dlenev@jabberwock.localdomain>2004-04-02 10:12:53 +0400
commit32b28f92980ca2c057e61cb662f5c244e998d121 (patch)
tree9a60588328f7225c26e34d7cf0ce3ae4875c3523 /sql/field.cc
parenta62a5fc9c0cef8d9f1ae685b43b6dc539d6fcfc8 (diff)
downloadmariadb-git-32b28f92980ca2c057e61cb662f5c244e998d121.tar.gz
WL#1266 "Separate auto-set logic from TIMESTAMP type."
Final version of patch. Adds support for specifying of DEFAULT NOW() and/or ON UPDATE NOW() clauses for TIMESTAMP field definition. Current implementation allows only one such field per table and uses several unireg types for storing info about this properties of field. It should be replaced with better implementation when new .frm format is introduced. include/mysqld_error.h: Added error codes for case when we have more than one column with NOW() in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. mysql-test/r/create.result: Added tests for using of DEFAULT NOW() and ON UPDATE NOW() with non-TIMESTAMP fields. mysql-test/r/show_check.result: Updated test results to reflect new default look of TIMESTAMP fields in SHOW CREATE TABLE. mysql-test/r/system_mysql_db.result: Updated test results to reflect new default look of TIMESTAMP fields in SHOW CREATE TABLE. mysql-test/r/type_ranges.result: Updated test results to reflect new default look of TIMESTAMP fields in SHOW COLUMNS. mysql-test/r/type_timestamp.result: Added tests for various DEFAULT and ON UPDATE clauses for TIMESTAMP fields definitions. mysql-test/t/create.test: Added tests for using of DEFAULT NOW() and ON UPDATE NOW() with non-TIMESTAMP fields. mysql-test/t/type_timestamp.test: Added tests for various DEFAULT and ON UPDATE clauses for TIMESTAMP fields definitions. sql/field.cc: Added support for various combinations of DEFAULT and ON UPDATE clauses for TIMESTAMP field. Setting TABLE::timestamp* members for TIMESTAMP fields with auto-set option taking into account their unireg type (which corresponds to various DEFAULT/ON UPDATE values combinations). Replaced TABLE::time_stamp with TABLE::timestamp_default_now/on_update_now couple moved their setup to separate method set_timestamp_offsets(), which now is called from open_table instead of Field_timestamp cons. sql/field.h: Added more unireg types for handling of DEFAULT NOW() and ON UPDATE NOW() for TIMESTAMP fields. Fixed value corresponding to DEFAULT item for TIMESTAMP field. sql/ha_berkeley.cc: Now TIMESTAMP column with auto-set property could be updated during INSERT or/and UPDATE independently. sql/ha_heap.cc: Now TIMESTAMP column with auto-set property could be updated during INSERT or/and UPDATE independently. sql/ha_innodb.cc: Now TIMESTAMP column with auto-set property could be updated during INSERT or/and UPDATE independently. sql/ha_isam.cc: Now TIMESTAMP column with auto-set property could be updated during INSERT or/and UPDATE independently. sql/ha_isammrg.cc: Now TIMESTAMP column with auto-set property could be updated during INSERT or/and UPDATE independently. sql/ha_myisam.cc: Now TIMESTAMP column with auto-set property could be updated during INSERT or/and UPDATE independently. sql/ha_myisammrg.cc: Now TIMESTAMP column with auto-set property could be updated during INSERT or/and UPDATE independently. sql/item_func.h: We need to distinguish NOW() from other function for using in DEFAULT and in ON UPDATE clauses. sql/item_timefunc.h: We need to distinguish NOW() from other function for using in DEFAULT and in ON UPDATE clauses. sql/mysql_priv.h: Added parameter for ON UPDATE value to add_field_to_list() function. sql/share/czech/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/danish/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/dutch/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/english/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/estonian/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/french/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/german/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/greek/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/hungarian/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/italian/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/japanese/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/korean/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/norwegian-ny/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/norwegian/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/polish/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/portuguese/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/romanian/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/russian/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/serbian/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/slovak/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/spanish/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/swedish/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/share/ukrainian/errmsg.txt: Added error messages for case when we have more than one column with NOW() (CURRENT_TIMESTAMP is just more standard alias) in DEFAULT or ON UPDATE clauses and for case when we are using ON UPDATE clause with wrong type. sql/sql_base.cc: Added setup of TABLE::timestamp_default_now/on_update_now pair for each statement to open_table(). sql/sql_insert.cc: Using TABLE::timestamp_default_now/on_update_now pair instead of old TABLE::time_stamp. Added check for case then REPLACE could not be converted to UPDATE because of different DEFAULT/ON UPDATE values for TIMESTAMP field. sql/sql_lex.h: Added member for value used in ON UPDATE clause to st_lex. sql/sql_load.cc: Using TABLE::timestamp_default_now/on_update_now pair instead of old TABLE::time_stamp. We don't need to restore these members since they are set up for each statement in open_table(). sql/sql_parse.cc: Added handling of DEFAULT NOW() and ON UPDATE NOW() clauses for TIMESTAMP fields to add_field_to_list() function. sql/sql_show.cc: Added support for DEFAULT CURRENT_TIMESTAMP (aka NOW() ) and ON UPDATE CURRENT_TIMESTAMP to SHOW CREATE TABLE and SHOW COLUMNS. sql/sql_table.cc: mysql_create_table() function - added check for number of TIMESTAMP fields with auto-set values and replacing of old style TIMESTAMPs with their newer analogs. mysql_alter_table(): Using TABLE::timestamp_default_now/on_update_now pair instead of old TABLE::time_stamp. We don't need to restore these members since they are set up for each statement in open_table(). sql/sql_update.cc: Left only setting of TABLE::timestamp_default_now/on_update_now to 0 since they should be already set up in open_table(). sql/sql_yacc.yy: Added support for DEFAULT NOW() and ON UPDATE NOW() in field definitions. sql/table.h: Replaced TABLE::time_stamp withTABLE::timestamp_default_now/timestamp_on_update_now pair which allows to distinguish TIMESTAMP's with various DEFAULT/ON UPDATE clauses and optimize checks if TIMESTAMP field should be set to NOW() in handlers. sql/unireg.cc: Now we are marking only TIMESTAMP fields with NOW() as default or as on update value as special field for unireg.
Diffstat (limited to 'sql/field.cc')
-rw-r--r--sql/field.cc77
1 files changed, 69 insertions, 8 deletions
diff --git a/sql/field.cc b/sql/field.cc
index 89c6464c5f0..238d5e36147 100644
--- a/sql/field.cc
+++ b/sql/field.cc
@@ -2842,11 +2842,50 @@ void Field_double::sql_type(String &res) const
}
-/****************************************************************************
-** timestamp
-** The first timestamp in the table is automaticly updated
-** by handler.cc. The form->timestamp points at the automatic timestamp.
-****************************************************************************/
+/*
+ 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 automaitcally 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_FIELD's 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.
+ */
Field_timestamp::Field_timestamp(char *ptr_arg, uint32 len_arg,
enum utype unireg_check_arg,
@@ -2857,15 +2896,37 @@ Field_timestamp::Field_timestamp(char *ptr_arg, uint32 len_arg,
unireg_check_arg, field_name_arg, table_arg, cs)
{
flags|=ZEROFILL_FLAG; /* 4.0 MYD compatibility */
- if (table && !table->timestamp_field)
+ if (table && !table->timestamp_field &&
+ unireg_check != NONE)
{
- table->timestamp_field= this; // Automatic timestamp
- table->time_stamp=(ulong) (ptr_arg - (char*) table->record[0])+1;
+ /* This timestamp has auto-update */
+ table->timestamp_field= this;
flags|=TIMESTAMP_FLAG;
}
}
+/*
+ Sets TABLE::timestamp_default_now and TABLE::timestamp_on_update_now
+ members according to unireg type of this TIMESTAMP field.
+
+ SYNOPSIS
+ Field_timestamp::set_timestamp_offsets()
+
+*/
+void Field_timestamp::set_timestamp_offsets()
+{
+ ulong timestamp= (ulong) (ptr - (char*) table->record[0]) + 1;
+
+ DBUG_ASSERT(table->timestamp_field == this && unireg_check != NONE);
+
+ table->timestamp_default_now=
+ (unireg_check == TIMESTAMP_UN_FIELD)? 0 : timestamp;
+ table->timestamp_on_update_now=
+ (unireg_check == TIMESTAMP_DN_FIELD)? 0 : timestamp;
+}
+
+
int Field_timestamp::store(const char *from,uint len,CHARSET_INFO *cs)
{
long tmp=(long) str_to_timestamp(from,len);