mirror of
https://github.com/MariaDB/server.git
synced 2025-01-24 15:54:37 +01:00
6aaccbcbf7
Added basic per-thread time zone functionality (based on public domain elsie-code). Now user can select current time zone (from the list of time zones described in system tables). All NOW-like functions honor this time zone, values of TIMESTAMP type are interpreted as values in this time zone, so now our TIMESTAMP type behaves similar to Oracle's TIMESTAMP WITH LOCAL TIME ZONE (or proper PostgresSQL type). WL#1266 "CONVERT_TZ() - basic time with time zone conversion function". Fixed problems described in Bug #2336 (Different number of warnings when inserting bad datetime as string or as number). This required reworking of datetime realted warning hadling (they now generated at Field object level not in conversion functions). Optimization: Now Field class descendants use table->in_use member instead of current_thd macro. include/my_global.h: Added macro for reading of 32-bit ints stored in network order from unaligned memory location. include/mysqld_error.h: Added error-code for invalid timestamp warning and error-code for wrong or unknown time zone specification. libmysqld/Makefile.am: Added main per-thread time zone support file to libmysqld libmysqld/lib_sql.cc: Added initialization of time zones infrastructure to embedded server. mysql-test/r/connect.result: Updated test result since now mysql database contains more system tables. mysql-test/r/date_formats.result: Now when truncation occurs during conversion to datetime value we are producing Warnings instead of Notes. Also we are giving more clear warnings about this in some cases. mysql-test/r/func_sapdb.result: New warnings about truncation occured during conversion to datetime value added due their better handling. mysql-test/r/func_time.result: New warnings about truncation occured during conversion to datetime value added due their better handling. mysql-test/r/select.result: New warnings about truncation occured during conversion to datetime value added due their better handling. Also tweaked test a bit to made it less ambigious for reader. mysql-test/r/system_mysql_db.result: Updated test result because new system tables holding time zone descriptions were added. mysql-test/r/timezone.result: Updated timezone.test to use new system variable which shows system time zone. Added test of warning which is produced if someone tries to store non-existing (due falling into spring time-gap) datetime value into TIMESTAMP field. mysql-test/r/type_datetime.result: Separated and extended test of values and warnings produced for bad values stored in DATETTIME fields. mysql-test/r/type_time.result: Now we are producing more consistent warning when we are truncating datetime value while storing it in TIME field. mysql-test/r/type_timestamp.result: Separated and extended test of values and warnings produced for bad values stored in TIMESTAMP fields. mysql-test/t/select.test: Updated test to make it less ambigous for reader. mysql-test/t/timezone.test: Updated timezone.test to use new system variable which shows system time zone. Added test of warning which is produced if someone tries to store non-existing (due falling into spring time-gap) datetime value into TIMESTAMP field. mysql-test/t/type_datetime.test: Separated and extended test of values and warnings produced for bad values stored in DATETTIME fields. mysql-test/t/type_timestamp.test: Separated and extended test of values and warnings produced for bad values stored in TIMESTAMP fields. scripts/mysql_create_system_tables.sh: Added creation of tables with time zone descriptions. Also added descriptions of time zones used in tests. scripts/mysql_fix_privilege_tables.sql: Added mysql.time_zone* tables family. sql/Makefile.am: Added files implementing time zone support to server, also added rules for building of mysql_tzinfo_to_sql converter and test_time test. sql/field.cc: Now we are using per-thread time zone for TIMESTAMP <-> whatever conversion. Fixed generation of warnings for datetime types (DATETIME/TIMESTAMP/DATE/...) and any other Field to datetime conversion (now we are generating warnings no in lower level functions like in str_to_TIME() but in Field methods. This allows generate better and more consistent warnings and to reuse code of str_to_TIME() outside of server). Added 3rd parameter to set_warning() method to be able to not increment cut fields but still produce a warning. Also added set_datetime_warning() family of auxiliary methods which allow easier generate datetime related warnings. Also replaced occurences of current_thd with table->in_use member, added asserts for catching all places there we need to set table->in_use accordingly. Renamed fix_datetime() function to number_to_TIME() and moved it to sql/time.cc there it fits better. sql/field.h: Added comment about places where we can use table->in_use member instead of current_thd. Added 3rd parameter to Field::set_warning() method and set_datetime_warning() family of methods. sql/field_conv.cc: Field::set_warning() method with 2 arguments was replaced with more generic set_warning() method with 3 arguments. sql/ha_berkeley.cc: Now we set table->in_use for temporary tables so we have to use table->tmp_table for checking if table is temporary. sql/item.cc: Replaced calls to str_to_time() and str_to_TIME() funcs with their warning generating analogs. sql/item_create.cc: Added creation of CONVERT_TZ function as FUNC_ARG3. sql/item_create.h: Added creation of CONVERT_TZ function as FUNC_ARG3. sql/item_timefunc.cc: Added support of per-thread time zone to NOW-like and FROM_UNIXTIME, UNIX_TIMESTAMP functions. Added support for CONVERT_TZ function. Removed call to str_to_timestamp function which caused non-optimal behavior in certain cases. Replaced calls to str_to_time() function with its warning generating analog. sql/item_timefunc.h: Added support of per-thread time zone to NOW-like and FROM_UNIXTIME, UNIX_TIMESTAMP functions. Added support of CONVERT_TZ function. sql/lex.h: Added support of CONVERT_TZ function. sql/log.cc: Added support for replication of statements depending on time zone. sql/mysql_priv.h: Now including headers with per-thread time zone support functions and classes. Added portable replacement of time_t - my_time_t type. Added time zone as one of query distinguishing parameters for query cache. Fixed declarations of str_to_TIME, str_to_time and my_system_gmt_sec (former my_gmt_sec) since now they have one more out parameter which informs about wrong datetime value or data truncation during conversion. Added warning generating version of str_to_TIME() and str_to_time() functions. Thrown away str_to_datetime/timestamp functions since they are not needed any longer. Added number_to_TIME function. sql/mysqld.cc: Added per-thread time zone support initialization. Added new startup parameter --default-time-zone. sql/set_var.cc: Added support for per-thread time_zone variable. Renamed old timezone variable to system_time_zone. sql/set_var.h: Added support for per-thread time_zone variable. sql/share/czech/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/danish/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/dutch/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/english/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/estonian/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/french/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/german/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/greek/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/hungarian/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/italian/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/japanese/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/korean/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/norwegian-ny/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/norwegian/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/polish/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/portuguese/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/romanian/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/russian/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/serbian/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/slovak/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/spanish/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/swedish/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/share/ukrainian/errmsg.txt: Added error message for barking when incorrect time zone name or specifiaction is provided and for warning about invalid TIMESTAMP values (e.g. falling into the spring time-gap). sql/slave.cc: In order to support replication of statements using time zones in 4.1 we should ensure that both master and slave have same default time zone. sql/sql_base.cc: Now we are setting TABLE::in_use member for all tables (which assume calls to Field::store or val_ methods). sql/sql_cache.cc: Added time zone as one more query distinguishing parameter for query cache. sql/sql_class.cc: Added THD::time_zone_used variable indicating that this query uses per thread time zone. sql/sql_class.h: Added per-thread time zone variable. Added THD::time_zone_used variable indicating that this query uses per thread time zone so if this is updating query the time zone should be logged to binlog. sql/sql_insert.cc: We should set TABLE::in_use member pointing to thread which is called INSERT DELAYED and not to worker thread. sql/sql_load.cc: Field::set_warning() now has one more argument now. sql/sql_parse.cc: Resetting THD::time_zone_used variable in the end of query processing. sql/sql_select.cc: Now we are setting TABLE::in_use member for all tables (which assume calls to Field::store or val_ methods). sql/sql_show.cc: Now using per thread time zone for extended show tables. sql/time.cc: Added support for per-thread time zones for TIMESTAMP type and reworked generation of warnings for TIMESTAMP and DATETIME types. (Introduced new TIME_to_timestamp() function. Removed hours normalisation from former my_gmt_sec() since it was not working and not used anywhere now, but breaks parameter constness, added to this function generation of warning if we are falling in spring time-gap. Removed str_to_timestamp and str_to_datetime functions which are no longer used. Moved fix_datetime function from sql/field.cc to this file as number_to_TIME() function. Added out parameter for str_to_TIME and str_to_time functions which indicates if value was truncated during conversion, removed direct generation of warnings from this functions.) sql/unireg.cc: Now we are setting TABLE::in_use member for all tables (which assume calls to Field::store or val_ methods). BitKeeper/etc/ignore: Added sql/test_time sql/mysql_tzinfo_to_sql libmysqld/tztime.cc to the ignore list
700 lines
20 KiB
C++
700 lines
20 KiB
C++
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
|
|
|
|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
/*
|
|
Functions to create a unireg form-file from a FIELD and a fieldname-fieldinfo
|
|
struct.
|
|
In the following functions FIELD * is an ordinary field-structure with
|
|
the following exeptions:
|
|
sc_length,typepos,row,kol,dtype,regnr and field need not to be set.
|
|
str is a (long) to record position where 0 is the first position.
|
|
*/
|
|
|
|
#define USES_TYPES
|
|
#include "mysql_priv.h"
|
|
#include <m_ctype.h>
|
|
|
|
#define FCOMP 17 /* Bytes for a packed field */
|
|
#define FCOMP 17 /* Bytes for a packed field */
|
|
|
|
static uchar * pack_screens(List<create_field> &create_fields,
|
|
uint *info_length, uint *screens, bool small_file);
|
|
static uint pack_keys(uchar *keybuff,uint key_count, KEY *key_info);
|
|
static bool pack_header(uchar *forminfo,enum db_type table_type,
|
|
List<create_field> &create_fields,
|
|
uint info_length, uint screens, uint table_options,
|
|
handler *file);
|
|
static uint get_interval_id(uint *int_count,List<create_field> &create_fields,
|
|
create_field *last_field);
|
|
static bool pack_fields(File file, List<create_field> &create_fields);
|
|
static bool make_empty_rec(int file, enum db_type table_type,
|
|
uint table_options,
|
|
List<create_field> &create_fields,
|
|
uint reclength,uint null_fields);
|
|
|
|
/*
|
|
Create a frm (table definition) file
|
|
|
|
SYNOPSIS
|
|
mysql_create_frm()
|
|
thd Thread handler
|
|
file_name Name of file (including database and .frm)
|
|
create_info create info parameters
|
|
create_fields Fields to create
|
|
keys number of keys to create
|
|
key_info Keys to create
|
|
db_file Handler to use. May be zero, in which case we use
|
|
create_info->db_type
|
|
RETURN
|
|
0 ok
|
|
1 error
|
|
*/
|
|
|
|
bool mysql_create_frm(THD *thd, my_string file_name,
|
|
HA_CREATE_INFO *create_info,
|
|
List<create_field> &create_fields,
|
|
uint keys, KEY *key_info,
|
|
handler *db_file)
|
|
{
|
|
uint reclength,info_length,screens,key_info_length,maxlength,null_fields;
|
|
File file;
|
|
ulong filepos;
|
|
uchar fileinfo[64],forminfo[288],*keybuff;
|
|
TYPELIB formnames;
|
|
uchar *screen_buff;
|
|
DBUG_ENTER("rea_create_table");
|
|
|
|
formnames.type_names=0;
|
|
if (!(screen_buff=pack_screens(create_fields,&info_length,&screens,0)))
|
|
DBUG_RETURN(1);
|
|
if (db_file == NULL)
|
|
db_file=get_new_handler((TABLE*) 0, create_info->db_type);
|
|
if (pack_header(forminfo, create_info->db_type,create_fields,info_length,
|
|
screens, create_info->table_options, db_file))
|
|
{
|
|
my_free((gptr) screen_buff,MYF(0));
|
|
if (thd->net.last_errno != ER_TOO_MANY_FIELDS)
|
|
DBUG_RETURN(1);
|
|
|
|
// Try again without UNIREG screens (to get more columns)
|
|
thd->net.last_error[0]=0;
|
|
if (!(screen_buff=pack_screens(create_fields,&info_length,&screens,1)))
|
|
DBUG_RETURN(1);
|
|
if (pack_header(forminfo, create_info->db_type, create_fields,info_length,
|
|
screens, create_info->table_options, db_file))
|
|
{
|
|
my_free((gptr) screen_buff,MYF(0));
|
|
DBUG_RETURN(1);
|
|
}
|
|
}
|
|
reclength=uint2korr(forminfo+266);
|
|
null_fields=uint2korr(forminfo+282);
|
|
|
|
if ((file=create_frm(file_name, reclength, fileinfo,
|
|
create_info, keys)) < 0)
|
|
{
|
|
my_free((gptr) screen_buff,MYF(0));
|
|
DBUG_RETURN(1);
|
|
}
|
|
|
|
uint key_buff_length=keys*(7+NAME_LEN+MAX_REF_PARTS*9)+16;
|
|
keybuff=(uchar*) my_malloc(key_buff_length, MYF(0));
|
|
key_info_length=pack_keys(keybuff,keys,key_info);
|
|
VOID(get_form_pos(file,fileinfo,&formnames));
|
|
if (!(filepos=make_new_entry(file,fileinfo,&formnames,"")))
|
|
goto err;
|
|
maxlength=(uint) next_io_size((ulong) (uint2korr(forminfo)+1000));
|
|
int2store(forminfo+2,maxlength);
|
|
int4store(fileinfo+10,(ulong) (filepos+maxlength));
|
|
int4store(fileinfo+47,key_buff_length);
|
|
fileinfo[26]= (uchar) test((create_info->max_rows == 1) &&
|
|
(create_info->min_rows == 1) && (keys == 0));
|
|
int2store(fileinfo+28,key_info_length);
|
|
strmake((char*) forminfo+47,create_info->comment ? create_info->comment : "",
|
|
60);
|
|
forminfo[46]=(uchar) strlen((char*)forminfo+47); // Length of comment
|
|
|
|
if (my_pwrite(file,(byte*) fileinfo,64,0L,MYF_RW) ||
|
|
my_pwrite(file,(byte*) keybuff,key_info_length,
|
|
(ulong) uint2korr(fileinfo+6),MYF_RW))
|
|
goto err;
|
|
VOID(my_seek(file,
|
|
(ulong) uint2korr(fileinfo+6)+ (ulong) key_buff_length,
|
|
MY_SEEK_SET,MYF(0)));
|
|
if (make_empty_rec(file,create_info->db_type,create_info->table_options,
|
|
create_fields,reclength,null_fields))
|
|
goto err;
|
|
|
|
VOID(my_seek(file,filepos,MY_SEEK_SET,MYF(0)));
|
|
if (my_write(file,(byte*) forminfo,288,MYF_RW) ||
|
|
my_write(file,(byte*) screen_buff,info_length,MYF_RW) ||
|
|
pack_fields(file,create_fields))
|
|
goto err;
|
|
|
|
#ifdef HAVE_CRYPTED_FRM
|
|
if (create_info->password)
|
|
{
|
|
char tmp=2,*disk_buff=0;
|
|
SQL_CRYPT *crypted=new SQL_CRYPT(create_info->password);
|
|
if (!crypted || my_pwrite(file,&tmp,1,26,MYF_RW)) // Mark crypted
|
|
goto err;
|
|
uint read_length=uint2korr(forminfo)-256;
|
|
VOID(my_seek(file,filepos+256,MY_SEEK_SET,MYF(0)));
|
|
if (read_string(file,(gptr*) &disk_buff,read_length))
|
|
goto err;
|
|
crypted->encode(disk_buff,read_length);
|
|
delete crypted;
|
|
if (my_pwrite(file,disk_buff,read_length,filepos+256,MYF_RW))
|
|
{
|
|
my_free(disk_buff,MYF(0));
|
|
goto err;
|
|
}
|
|
my_free(disk_buff,MYF(0));
|
|
}
|
|
#endif
|
|
|
|
my_free((gptr) screen_buff,MYF(0));
|
|
my_free((gptr) keybuff, MYF(0));
|
|
|
|
if (opt_sync_frm && !(create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
|
|
my_sync(file, MYF(MY_WME)))
|
|
goto err2;
|
|
if (my_close(file,MYF(MY_WME)))
|
|
goto err3;
|
|
DBUG_RETURN(0);
|
|
|
|
err:
|
|
my_free((gptr) screen_buff,MYF(0));
|
|
my_free((gptr) keybuff, MYF(0));
|
|
err2:
|
|
VOID(my_close(file,MYF(MY_WME)));
|
|
err3:
|
|
my_delete(file_name,MYF(0));
|
|
DBUG_RETURN(1);
|
|
} /* mysql_create_frm */
|
|
|
|
|
|
/*
|
|
Create a frm (table definition) file and the tables
|
|
|
|
SYNOPSIS
|
|
mysql_create_frm()
|
|
thd Thread handler
|
|
file_name Name of file (including database and .frm)
|
|
create_info create info parameters
|
|
create_fields Fields to create
|
|
keys number of keys to create
|
|
key_info Keys to create
|
|
db_file Handler to use. May be zero, in which case we use
|
|
create_info->db_type
|
|
RETURN
|
|
0 ok
|
|
1 error
|
|
*/
|
|
|
|
int rea_create_table(THD *thd, my_string file_name,
|
|
HA_CREATE_INFO *create_info,
|
|
List<create_field> &create_fields,
|
|
uint keys, KEY *key_info)
|
|
{
|
|
DBUG_ENTER("rea_create_table");
|
|
|
|
if (mysql_create_frm(thd, file_name, create_info,
|
|
create_fields, keys, key_info, NULL))
|
|
DBUG_RETURN(1);
|
|
if (ha_create_table(file_name,create_info,0))
|
|
{
|
|
my_delete(file_name,MYF(0));
|
|
DBUG_RETURN(1);
|
|
}
|
|
DBUG_RETURN(0);
|
|
} /* rea_create_table */
|
|
|
|
|
|
/* Pack screens to a screen for save in a form-file */
|
|
|
|
static uchar * pack_screens(List<create_field> &create_fields,
|
|
uint *info_length, uint *screens,
|
|
bool small_file)
|
|
{
|
|
reg1 uint i;
|
|
uint row,start_row,end_row,fields_on_screen;
|
|
uint length,cols;
|
|
uchar *info,*pos,*start_screen;
|
|
uint fields=create_fields.elements;
|
|
List_iterator<create_field> it(create_fields);
|
|
DBUG_ENTER("pack_screens");
|
|
|
|
start_row=4; end_row=22; cols=80; fields_on_screen=end_row+1-start_row;
|
|
|
|
*screens=(fields-1)/fields_on_screen+1;
|
|
length= (*screens) * (SC_INFO_LENGTH+ (cols>> 1)+4);
|
|
|
|
create_field *field;
|
|
while ((field=it++))
|
|
length+=(uint) strlen(field->field_name)+1+TE_INFO_LENGTH+cols/2;
|
|
|
|
if (!(info=(uchar*) my_malloc(length,MYF(MY_WME))))
|
|
DBUG_RETURN(0);
|
|
|
|
start_screen=0;
|
|
row=end_row;
|
|
pos=info;
|
|
it.rewind();
|
|
for (i=0 ; i < fields ; i++)
|
|
{
|
|
create_field *cfield=it++;
|
|
if (row++ == end_row)
|
|
{
|
|
if (i)
|
|
{
|
|
length=(uint) (pos-start_screen);
|
|
int2store(start_screen,length);
|
|
start_screen[2]=(uchar) (fields_on_screen+1);
|
|
start_screen[3]=(uchar) (fields_on_screen);
|
|
}
|
|
row=start_row;
|
|
start_screen=pos;
|
|
pos+=4;
|
|
pos[0]= (uchar) start_row-2; /* Header string */
|
|
pos[1]= (uchar) (cols >> 2);
|
|
pos[2]= (uchar) (cols >> 1) +1;
|
|
strfill((my_string) pos+3,(uint) (cols >> 1),' ');
|
|
pos+=(cols >> 1)+4;
|
|
}
|
|
length=(uint) strlen(cfield->field_name);
|
|
if (length > cols-3)
|
|
length=cols-3;
|
|
|
|
if (!small_file)
|
|
{
|
|
pos[0]=(uchar) row;
|
|
pos[1]=0;
|
|
pos[2]=(uchar) (length+1);
|
|
pos=(uchar*) strmake((char*) pos+3,cfield->field_name,length)+1;
|
|
}
|
|
cfield->row=(uint8) row;
|
|
cfield->col=(uint8) (length+1);
|
|
cfield->sc_length=(uint8) min(cfield->length,cols-(length+2));
|
|
}
|
|
length=(uint) (pos-start_screen);
|
|
int2store(start_screen,length);
|
|
start_screen[2]=(uchar) (row-start_row+2);
|
|
start_screen[3]=(uchar) (row-start_row+1);
|
|
|
|
*info_length=(uint) (pos-info);
|
|
DBUG_RETURN(info);
|
|
} /* pack_screens */
|
|
|
|
|
|
/* Pack keyinfo and keynames to keybuff for save in form-file. */
|
|
|
|
static uint pack_keys(uchar *keybuff,uint key_count,KEY *keyinfo)
|
|
{
|
|
uint key_parts,length;
|
|
uchar *pos, *keyname_pos;
|
|
KEY *key,*end;
|
|
KEY_PART_INFO *key_part,*key_part_end;
|
|
DBUG_ENTER("pack_keys");
|
|
|
|
pos=keybuff+6;
|
|
key_parts=0;
|
|
for (key=keyinfo,end=keyinfo+key_count ; key != end ; key++)
|
|
{
|
|
int2store(pos, (key->flags ^ HA_NOSAME));
|
|
int2store(pos+2,key->key_length);
|
|
pos[4]= (uchar) key->key_parts;
|
|
pos[5]= (uchar) key->algorithm;
|
|
pos[6]=pos[7]=0; // For the future
|
|
pos+=8;
|
|
key_parts+=key->key_parts;
|
|
DBUG_PRINT("loop",("flags: %d key_parts: %d at %lx",
|
|
key->flags,key->key_parts,
|
|
key->key_part));
|
|
for (key_part=key->key_part,key_part_end=key_part+key->key_parts ;
|
|
key_part != key_part_end ;
|
|
key_part++)
|
|
|
|
{
|
|
DBUG_PRINT("loop",("field: %d startpos: %ld length: %ld",
|
|
key_part->fieldnr,key_part->offset,key_part->length));
|
|
int2store(pos,key_part->fieldnr+1+FIELD_NAME_USED);
|
|
int2store(pos+2,key_part->offset+1);
|
|
pos[4]=0; // Sort order
|
|
int2store(pos+5,key_part->key_type);
|
|
int2store(pos+7,key_part->length);
|
|
pos+=9;
|
|
}
|
|
}
|
|
/* Save keynames */
|
|
keyname_pos=pos;
|
|
*pos++=(uchar) NAMES_SEP_CHAR;
|
|
for (key=keyinfo ; key != end ; key++)
|
|
{
|
|
uchar *tmp=(uchar*) strmov((char*) pos,key->name);
|
|
*tmp++= (uchar) NAMES_SEP_CHAR;
|
|
*tmp=0;
|
|
pos=tmp;
|
|
}
|
|
*(pos++)=0;
|
|
|
|
if (key_count > 127 || key_parts > 127)
|
|
{
|
|
keybuff[0]= (key_count & 0x7f) | 0x80;
|
|
keybuff[1]= key_count >> 7;
|
|
int2store(keybuff+2,key_parts);
|
|
}
|
|
else
|
|
{
|
|
keybuff[0]=(uchar) key_count;
|
|
keybuff[1]=(uchar) key_parts;
|
|
keybuff[2]= keybuff[3]= 0;
|
|
}
|
|
length=(uint) (pos-keyname_pos);
|
|
int2store(keybuff+4,length);
|
|
DBUG_RETURN((uint) (pos-keybuff));
|
|
} /* pack_keys */
|
|
|
|
|
|
/* Make formheader */
|
|
|
|
static bool pack_header(uchar *forminfo, enum db_type table_type,
|
|
List<create_field> &create_fields,
|
|
uint info_length, uint screens,uint table_options,
|
|
handler *file)
|
|
{
|
|
uint length,int_count,int_length,no_empty, int_parts;
|
|
uint time_stamp_pos,null_fields;
|
|
ulong reclength, totlength, n_length, com_length;
|
|
DBUG_ENTER("pack_header");
|
|
|
|
if (create_fields.elements > MAX_FIELDS)
|
|
{
|
|
my_error(ER_TOO_MANY_FIELDS,MYF(0));
|
|
DBUG_RETURN(1);
|
|
}
|
|
|
|
totlength=reclength=0L;
|
|
no_empty=int_count=int_parts=int_length=time_stamp_pos=null_fields=
|
|
com_length=0;
|
|
n_length=2L;
|
|
|
|
/* Check fields */
|
|
|
|
List_iterator<create_field> it(create_fields);
|
|
create_field *field;
|
|
while ((field=it++))
|
|
{
|
|
totlength+= field->length;
|
|
com_length+= field->comment.length;
|
|
if (MTYP_TYPENR(field->unireg_check) == Field::NOEMPTY ||
|
|
field->unireg_check & MTYP_NOEMPTY_BIT)
|
|
{
|
|
field->unireg_check= (Field::utype) ((uint) field->unireg_check |
|
|
MTYP_NOEMPTY_BIT);
|
|
no_empty++;
|
|
}
|
|
/*
|
|
We mark first TIMESTAMP field with NOW() in DEFAULT or ON UPDATE
|
|
as auto-update field.
|
|
*/
|
|
if (field->sql_type == FIELD_TYPE_TIMESTAMP &&
|
|
MTYP_TYPENR(field->unireg_check) != Field::NONE &&
|
|
!time_stamp_pos)
|
|
time_stamp_pos=(int) field->offset+1;
|
|
length=field->pack_length;
|
|
if ((int) field->offset+length > reclength)
|
|
reclength=(int) field->offset+length;
|
|
n_length+= (ulong) strlen(field->field_name)+1;
|
|
field->interval_id=0;
|
|
if (field->interval)
|
|
{
|
|
uint old_int_count=int_count;
|
|
field->interval_id=get_interval_id(&int_count,create_fields,field);
|
|
if (old_int_count != int_count)
|
|
{
|
|
for (const char **pos=field->interval->type_names ; *pos ; pos++)
|
|
int_length+=(uint) strlen(*pos)+1; // field + suffix prefix
|
|
int_parts+=field->interval->count+1;
|
|
}
|
|
}
|
|
if (f_maybe_null(field->pack_flag))
|
|
null_fields++;
|
|
}
|
|
int_length+=int_count*2; // 255 prefix + 0 suffix
|
|
|
|
/* Save values in forminfo */
|
|
|
|
if (reclength > (ulong) file->max_record_length())
|
|
{
|
|
my_error(ER_TOO_BIG_ROWSIZE, MYF(0), (uint) file->max_record_length());
|
|
DBUG_RETURN(1);
|
|
}
|
|
/* Hack to avoid bugs with small static rows in MySQL */
|
|
reclength=max(file->min_record_length(table_options),reclength);
|
|
if (info_length+(ulong) create_fields.elements*FCOMP+288+
|
|
n_length+int_length+com_length > 65535L || int_count > 255)
|
|
{
|
|
my_error(ER_TOO_MANY_FIELDS,MYF(0));
|
|
DBUG_RETURN(1);
|
|
}
|
|
|
|
bzero((char*)forminfo,288);
|
|
length=(info_length+create_fields.elements*FCOMP+288+n_length+int_length+
|
|
com_length);
|
|
int2store(forminfo,length);
|
|
forminfo[256] = (uint8) screens;
|
|
int2store(forminfo+258,create_fields.elements);
|
|
int2store(forminfo+260,info_length);
|
|
int2store(forminfo+262,totlength);
|
|
int2store(forminfo+264,no_empty);
|
|
int2store(forminfo+266,reclength);
|
|
int2store(forminfo+268,n_length);
|
|
int2store(forminfo+270,int_count);
|
|
int2store(forminfo+272,int_parts);
|
|
int2store(forminfo+274,int_length);
|
|
int2store(forminfo+276,time_stamp_pos);
|
|
int2store(forminfo+278,80); /* Columns needed */
|
|
int2store(forminfo+280,22); /* Rows needed */
|
|
int2store(forminfo+282,null_fields);
|
|
int2store(forminfo+284,com_length);
|
|
DBUG_RETURN(0);
|
|
} /* pack_header */
|
|
|
|
|
|
/* get each unique interval each own id */
|
|
|
|
static uint get_interval_id(uint *int_count,List<create_field> &create_fields,
|
|
create_field *last_field)
|
|
{
|
|
List_iterator<create_field> it(create_fields);
|
|
create_field *field;
|
|
TYPELIB *interval=last_field->interval;
|
|
|
|
while ((field=it++) != last_field)
|
|
{
|
|
if (field->interval_id && field->interval->count == interval->count)
|
|
{
|
|
const char **a,**b;
|
|
for (a=field->interval->type_names, b=interval->type_names ;
|
|
*a && !strcmp(*a,*b);
|
|
a++,b++) ;
|
|
|
|
if (! *a)
|
|
{
|
|
return field->interval_id; // Re-use last interval
|
|
}
|
|
}
|
|
}
|
|
return ++*int_count; // New unique interval
|
|
}
|
|
|
|
|
|
/* Save fields, fieldnames and intervals */
|
|
|
|
static bool pack_fields(File file,List<create_field> &create_fields)
|
|
{
|
|
reg2 uint i;
|
|
uint int_count, comment_length=0;
|
|
uchar buff[MAX_FIELD_WIDTH];
|
|
create_field *field;
|
|
DBUG_ENTER("pack_fields");
|
|
|
|
/* Write field info */
|
|
|
|
List_iterator<create_field> it(create_fields);
|
|
|
|
int_count=0;
|
|
while ((field=it++))
|
|
{
|
|
buff[0]= (uchar) field->row;
|
|
buff[1]= (uchar) field->col;
|
|
buff[2]= (uchar) field->sc_length;
|
|
int2store(buff+3, field->length);
|
|
uint recpos=(uint) field->offset+1;
|
|
int3store(buff+5,recpos);
|
|
int2store(buff+8,field->pack_flag);
|
|
int2store(buff+10,field->unireg_check);
|
|
buff[12]= (uchar) field->interval_id;
|
|
buff[13]= (uchar) field->sql_type;
|
|
if (field->sql_type == FIELD_TYPE_GEOMETRY)
|
|
{
|
|
buff[14]= (uchar) field->geom_type;
|
|
#ifndef HAVE_SPATIAL
|
|
DBUG_ASSERT(0); // Should newer happen
|
|
#endif
|
|
}
|
|
else if (field->charset)
|
|
buff[14]= (uchar) field->charset->number;
|
|
else
|
|
buff[14]= 0; // Numerical
|
|
int2store(buff+15, field->comment.length);
|
|
comment_length+= field->comment.length;
|
|
set_if_bigger(int_count,field->interval_id);
|
|
if (my_write(file,(byte*) buff,FCOMP,MYF_RW))
|
|
DBUG_RETURN(1);
|
|
}
|
|
|
|
/* Write fieldnames */
|
|
buff[0]=(uchar) NAMES_SEP_CHAR;
|
|
if (my_write(file,(byte*) buff,1,MYF_RW))
|
|
DBUG_RETURN(1);
|
|
i=0;
|
|
it.rewind();
|
|
while ((field=it++))
|
|
{
|
|
char *pos= strmov((char*) buff,field->field_name);
|
|
*pos++=NAMES_SEP_CHAR;
|
|
if (i == create_fields.elements-1)
|
|
*pos++=0;
|
|
if (my_write(file,(byte*) buff,(uint) (pos-(char*) buff),MYF_RW))
|
|
DBUG_RETURN(1);
|
|
i++;
|
|
}
|
|
|
|
/* Write intervals */
|
|
if (int_count)
|
|
{
|
|
String tmp((char*) buff,sizeof(buff), &my_charset_bin);
|
|
tmp.length(0);
|
|
it.rewind();
|
|
int_count=0;
|
|
while ((field=it++))
|
|
{
|
|
if (field->interval_id > int_count)
|
|
{
|
|
int_count=field->interval_id;
|
|
tmp.append(NAMES_SEP_CHAR);
|
|
for (const char **pos=field->interval->type_names ; *pos ; pos++)
|
|
{
|
|
tmp.append(*pos);
|
|
tmp.append(NAMES_SEP_CHAR);
|
|
}
|
|
tmp.append('\0'); // End of intervall
|
|
}
|
|
}
|
|
if (my_write(file,(byte*) tmp.ptr(),tmp.length(),MYF_RW))
|
|
DBUG_RETURN(1);
|
|
}
|
|
if (comment_length)
|
|
{
|
|
it.rewind();
|
|
int_count=0;
|
|
while ((field=it++))
|
|
{
|
|
if (field->comment.length)
|
|
if (my_write(file, (byte*) field->comment.str, field->comment.length,
|
|
MYF_RW))
|
|
DBUG_RETURN(1);
|
|
}
|
|
}
|
|
DBUG_RETURN(0);
|
|
}
|
|
|
|
|
|
/* save an empty record on start of formfile */
|
|
|
|
static bool make_empty_rec(File file,enum db_type table_type,
|
|
uint table_options,
|
|
List<create_field> &create_fields,
|
|
uint reclength, uint null_fields)
|
|
{
|
|
int error;
|
|
Field::utype type;
|
|
uint firstpos,null_count,null_length;
|
|
uchar *buff,*null_pos;
|
|
TABLE table;
|
|
create_field *field;
|
|
handler *handler;
|
|
DBUG_ENTER("make_empty_rec");
|
|
|
|
/* We need a table to generate columns for default values */
|
|
bzero((char*) &table,sizeof(table));
|
|
handler= get_new_handler((TABLE*) 0, table_type);
|
|
|
|
if (!handler ||
|
|
!(buff=(uchar*) my_malloc((uint) reclength,MYF(MY_WME | MY_ZEROFILL))))
|
|
{
|
|
delete handler;
|
|
DBUG_RETURN(1);
|
|
}
|
|
|
|
table.in_use= current_thd;
|
|
table.db_low_byte_first= handler->low_byte_first();
|
|
table.blob_ptr_size=portable_sizeof_char_ptr;
|
|
|
|
firstpos=reclength;
|
|
null_count=0;
|
|
if (!(table_options & HA_OPTION_PACK_RECORD))
|
|
{
|
|
null_fields++; // Need one bit for delete mark
|
|
null_count++;
|
|
}
|
|
bfill(buff,(null_length=(null_fields+7)/8),255);
|
|
null_pos=buff;
|
|
|
|
List_iterator<create_field> it(create_fields);
|
|
while ((field=it++))
|
|
{
|
|
Field *regfield=make_field((char*) buff+field->offset,field->length,
|
|
field->flags & NOT_NULL_FLAG ? 0:
|
|
null_pos+null_count/8,
|
|
1 << (null_count & 7),
|
|
field->pack_flag,
|
|
field->sql_type,
|
|
field->charset,
|
|
field->geom_type,
|
|
field->unireg_check,
|
|
field->interval,
|
|
field->field_name,
|
|
&table);
|
|
|
|
if (!(field->flags & NOT_NULL_FLAG))
|
|
null_count++;
|
|
|
|
if ((uint) field->offset < firstpos &&
|
|
regfield->type() != FIELD_TYPE_NULL)
|
|
firstpos= field->offset;
|
|
|
|
type= (Field::utype) MTYP_TYPENR(field->unireg_check);
|
|
|
|
if (field->def &&
|
|
(regfield->real_type() != FIELD_TYPE_YEAR ||
|
|
field->def->val_int() != 0))
|
|
(void) field->def->save_in_field(regfield, 1);
|
|
else if (regfield->real_type() == FIELD_TYPE_ENUM &&
|
|
(field->flags & NOT_NULL_FLAG))
|
|
{
|
|
regfield->set_notnull();
|
|
regfield->store((longlong) 1);
|
|
}
|
|
else if (type == Field::YES) // Old unireg type
|
|
regfield->store(ER(ER_YES),(uint) strlen(ER(ER_YES)),system_charset_info);
|
|
else if (type == Field::NO) // Old unireg type
|
|
regfield->store(ER(ER_NO), (uint) strlen(ER(ER_NO)),system_charset_info);
|
|
else
|
|
regfield->reset();
|
|
delete regfield;
|
|
}
|
|
|
|
/* Fill not used startpos */
|
|
bfill((byte*) buff+null_length,firstpos-null_length,255);
|
|
error=(int) my_write(file,(byte*) buff,(uint) reclength,MYF_RW);
|
|
my_free((gptr) buff,MYF(MY_FAE));
|
|
delete handler;
|
|
DBUG_RETURN(error);
|
|
} /* make_empty_rec */
|