00001 /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 00018 /* 00019 Functions to create a unireg form-file from a FIELD and a fieldname-fieldinfo 00020 struct. 00021 In the following functions FIELD * is an ordinary field-structure with 00022 the following exeptions: 00023 sc_length,typepos,row,kol,dtype,regnr and field need not to be set. 00024 str is a (long) to record position where 0 is the first position. 00025 */ 00026 00027 #include "mysql_priv.h" 00028 #include <m_ctype.h> 00029 #include <assert.h> 00030 00031 #define FCOMP 17 /* Bytes for a packed field */ 00032 00033 static uchar * pack_screens(List<create_field> &create_fields, 00034 uint *info_length, uint *screens, bool small_file); 00035 static uint pack_keys(uchar *keybuff,uint key_count, KEY *key_info, 00036 ulong data_offset); 00037 static bool pack_header(uchar *forminfo,enum legacy_db_type table_type, 00038 List<create_field> &create_fields, 00039 uint info_length, uint screens, uint table_options, 00040 ulong data_offset, handler *file); 00041 static uint get_interval_id(uint *int_count,List<create_field> &create_fields, 00042 create_field *last_field); 00043 static bool pack_fields(File file, List<create_field> &create_fields, 00044 ulong data_offset); 00045 static bool make_empty_rec(THD *thd, int file, enum legacy_db_type table_type, 00046 uint table_options, 00047 List<create_field> &create_fields, 00048 uint reclength, ulong data_offset, 00049 handler *handler); 00050 00051 /* 00052 Create a frm (table definition) file 00053 00054 SYNOPSIS 00055 mysql_create_frm() 00056 thd Thread handler 00057 file_name Path for file (including database and .frm) 00058 db Name of database 00059 table Name of table 00060 create_info create info parameters 00061 create_fields Fields to create 00062 keys number of keys to create 00063 key_info Keys to create 00064 db_file Handler to use. May be zero, in which case we use 00065 create_info->db_type 00066 RETURN 00067 0 ok 00068 1 error 00069 */ 00070 00071 bool mysql_create_frm(THD *thd, const char *file_name, 00072 const char *db, const char *table, 00073 HA_CREATE_INFO *create_info, 00074 List<create_field> &create_fields, 00075 uint keys, KEY *key_info, 00076 handler *db_file) 00077 { 00078 LEX_STRING str_db_type; 00079 uint reclength, info_length, screens, key_info_length, maxlength, tmp_len, i; 00080 ulong key_buff_length; 00081 File file; 00082 ulong filepos, data_offset; 00083 uchar fileinfo[64],forminfo[288],*keybuff; 00084 TYPELIB formnames; 00085 uchar *screen_buff; 00086 char buff[32]; 00087 #ifdef WITH_PARTITION_STORAGE_ENGINE 00088 partition_info *part_info= thd->work_part_info; 00089 #endif 00090 DBUG_ENTER("mysql_create_frm"); 00091 00092 DBUG_ASSERT(*fn_rext((char*)file_name)); // Check .frm extension 00093 formnames.type_names=0; 00094 if (!(screen_buff=pack_screens(create_fields,&info_length,&screens,0))) 00095 DBUG_RETURN(1); 00096 DBUG_ASSERT(db_file != NULL); 00097 00098 /* If fixed row records, we need one bit to check for deleted rows */ 00099 if (!(create_info->table_options & HA_OPTION_PACK_RECORD)) 00100 create_info->null_bits++; 00101 data_offset= (create_info->null_bits + 7) / 8; 00102 00103 if (pack_header(forminfo, ha_legacy_type(create_info->db_type), 00104 create_fields,info_length, 00105 screens, create_info->table_options, 00106 data_offset, db_file)) 00107 { 00108 my_free((gptr) screen_buff,MYF(0)); 00109 if (thd->net.last_errno != ER_TOO_MANY_FIELDS) 00110 DBUG_RETURN(1); 00111 00112 // Try again without UNIREG screens (to get more columns) 00113 thd->net.last_error[0]=0; 00114 if (!(screen_buff=pack_screens(create_fields,&info_length,&screens,1))) 00115 DBUG_RETURN(1); 00116 if (pack_header(forminfo, ha_legacy_type(create_info->db_type), 00117 create_fields,info_length, 00118 screens, create_info->table_options, data_offset, db_file)) 00119 { 00120 my_free((gptr) screen_buff,MYF(0)); 00121 DBUG_RETURN(1); 00122 } 00123 } 00124 reclength=uint2korr(forminfo+266); 00125 00126 /* Calculate extra data segment length */ 00127 str_db_type.str= (char *) ha_resolve_storage_engine_name(create_info->db_type); 00128 str_db_type.length= strlen(str_db_type.str); 00129 /* str_db_type */ 00130 create_info->extra_size= (2 + str_db_type.length + 00131 2 + create_info->connect_string.length); 00132 /* 00133 Partition: 00134 Length of partition info = 4 byte 00135 Potential NULL byte at end of partition info string = 1 byte 00136 Indicator if auto-partitioned table = 1 byte 00137 => Total 6 byte 00138 */ 00139 create_info->extra_size+= 6; 00140 #ifdef WITH_PARTITION_STORAGE_ENGINE 00141 if (part_info) 00142 { 00143 create_info->extra_size+= part_info->part_info_len; 00144 } 00145 #endif 00146 00147 for (i= 0; i < keys; i++) 00148 { 00149 if (key_info[i].parser_name) 00150 create_info->extra_size+= key_info[i].parser_name->length + 1; 00151 } 00152 00153 if ((file=create_frm(thd, file_name, db, table, reclength, fileinfo, 00154 create_info, keys)) < 0) 00155 { 00156 my_free((gptr) screen_buff,MYF(0)); 00157 DBUG_RETURN(1); 00158 } 00159 00160 key_buff_length= uint4korr(fileinfo+47); 00161 keybuff=(uchar*) my_malloc(key_buff_length, MYF(0)); 00162 key_info_length= pack_keys(keybuff, keys, key_info, data_offset); 00163 VOID(get_form_pos(file,fileinfo,&formnames)); 00164 if (!(filepos=make_new_entry(file,fileinfo,&formnames,""))) 00165 goto err; 00166 maxlength=(uint) next_io_size((ulong) (uint2korr(forminfo)+1000)); 00167 int2store(forminfo+2,maxlength); 00168 int4store(fileinfo+10,(ulong) (filepos+maxlength)); 00169 fileinfo[26]= (uchar) test((create_info->max_rows == 1) && 00170 (create_info->min_rows == 1) && (keys == 0)); 00171 int2store(fileinfo+28,key_info_length); 00172 00173 tmp_len= system_charset_info->cset->charpos(system_charset_info, 00174 create_info->comment.str, 00175 create_info->comment.str + 00176 create_info->comment.length, 60); 00177 if (tmp_len < create_info->comment.length) 00178 { 00179 char buff[128]; 00180 (void) my_snprintf(buff, sizeof(buff), "Too long comment for table '%s'", 00181 table); 00182 if ((thd->variables.sql_mode & 00183 (MODE_STRICT_TRANS_TABLES | MODE_STRICT_ALL_TABLES))) 00184 { 00185 my_message(ER_UNKNOWN_ERROR, buff, MYF(0)); 00186 goto err; 00187 } 00188 push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, 00189 ER_UNKNOWN_ERROR, ER(ER_UNKNOWN_ERROR), buff); 00190 create_info->comment.length= tmp_len; 00191 } 00192 00193 strmake((char*) forminfo+47, create_info->comment.str ? 00194 create_info->comment.str : "", create_info->comment.length); 00195 forminfo[46]=(uchar) create_info->comment.length; 00196 #ifdef WITH_PARTITION_STORAGE_ENGINE 00197 if (part_info) 00198 { 00199 fileinfo[61]= (uchar) ha_legacy_type(part_info->default_engine_type); 00200 DBUG_PRINT("info", ("part_db_type = %d", fileinfo[61])); 00201 } 00202 #endif 00203 int2store(fileinfo+59,db_file->extra_rec_buf_length()); 00204 00205 if (my_pwrite(file,(byte*) fileinfo,64,0L,MYF_RW) || 00206 my_pwrite(file,(byte*) keybuff,key_info_length, 00207 (ulong) uint2korr(fileinfo+6),MYF_RW)) 00208 goto err; 00209 VOID(my_seek(file, 00210 (ulong) uint2korr(fileinfo+6)+ (ulong) key_buff_length, 00211 MY_SEEK_SET,MYF(0))); 00212 if (make_empty_rec(thd,file,ha_legacy_type(create_info->db_type), 00213 create_info->table_options, 00214 create_fields,reclength, data_offset, db_file)) 00215 goto err; 00216 00217 int2store(buff, create_info->connect_string.length); 00218 if (my_write(file, (const byte*)buff, 2, MYF(MY_NABP)) || 00219 my_write(file, (const byte*)create_info->connect_string.str, 00220 create_info->connect_string.length, MYF(MY_NABP))) 00221 goto err; 00222 00223 int2store(buff, str_db_type.length); 00224 if (my_write(file, (const byte*)buff, 2, MYF(MY_NABP)) || 00225 my_write(file, (const byte*)str_db_type.str, 00226 str_db_type.length, MYF(MY_NABP))) 00227 goto err; 00228 00229 #ifdef WITH_PARTITION_STORAGE_ENGINE 00230 if (part_info) 00231 { 00232 char auto_partitioned= part_info->is_auto_partitioned ? 1 : 0; 00233 int4store(buff, part_info->part_info_len); 00234 if (my_write(file, (const byte*)buff, 4, MYF_RW) || 00235 my_write(file, (const byte*)part_info->part_info_string, 00236 part_info->part_info_len + 1, MYF_RW) || 00237 my_write(file, (const byte*)&auto_partitioned, 1, MYF_RW)) 00238 goto err; 00239 } 00240 else 00241 #endif 00242 { 00243 bzero(buff, 6); 00244 if (my_write(file, (byte*) buff, 6, MYF_RW)) 00245 goto err; 00246 } 00247 for (i= 0; i < keys; i++) 00248 { 00249 if (key_info[i].parser_name) 00250 { 00251 if (my_write(file, (const byte*)key_info[i].parser_name->str, 00252 key_info[i].parser_name->length + 1, MYF(MY_NABP))) 00253 goto err; 00254 } 00255 } 00256 00257 VOID(my_seek(file,filepos,MY_SEEK_SET,MYF(0))); 00258 if (my_write(file,(byte*) forminfo,288,MYF_RW) || 00259 my_write(file,(byte*) screen_buff,info_length,MYF_RW) || 00260 pack_fields(file, create_fields, data_offset)) 00261 goto err; 00262 00263 #ifdef HAVE_CRYPTED_FRM 00264 if (create_info->password) 00265 { 00266 char tmp=2,*disk_buff=0; 00267 SQL_CRYPT *crypted=new SQL_CRYPT(create_info->password); 00268 if (!crypted || my_pwrite(file,&tmp,1,26,MYF_RW)) // Mark crypted 00269 goto err; 00270 uint read_length=uint2korr(forminfo)-256; 00271 VOID(my_seek(file,filepos+256,MY_SEEK_SET,MYF(0))); 00272 if (read_string(file,(gptr*) &disk_buff,read_length)) 00273 goto err; 00274 crypted->encode(disk_buff,read_length); 00275 delete crypted; 00276 if (my_pwrite(file,disk_buff,read_length,filepos+256,MYF_RW)) 00277 { 00278 my_free(disk_buff,MYF(0)); 00279 goto err; 00280 } 00281 my_free(disk_buff,MYF(0)); 00282 } 00283 #endif 00284 00285 my_free((gptr) screen_buff,MYF(0)); 00286 my_free((gptr) keybuff, MYF(0)); 00287 00288 if (opt_sync_frm && !(create_info->options & HA_LEX_CREATE_TMP_TABLE) && 00289 my_sync(file, MYF(MY_WME))) 00290 goto err2; 00291 if (my_close(file,MYF(MY_WME))) 00292 goto err3; 00293 00294 { 00295 /* Unescape all UCS2 intervals: were escaped in pack_headers */ 00296 List_iterator<create_field> it(create_fields); 00297 create_field *field; 00298 while ((field=it++)) 00299 { 00300 if (field->interval && field->charset->mbminlen > 1) 00301 unhex_type2(field->interval); 00302 } 00303 } 00304 DBUG_RETURN(0); 00305 00306 err: 00307 my_free((gptr) screen_buff,MYF(0)); 00308 my_free((gptr) keybuff, MYF(0)); 00309 err2: 00310 VOID(my_close(file,MYF(MY_WME))); 00311 err3: 00312 my_delete(file_name,MYF(0)); 00313 DBUG_RETURN(1); 00314 } /* mysql_create_frm */ 00315 00316 00317 /* 00318 Create a frm (table definition) file and the tables 00319 00320 SYNOPSIS 00321 rea_create_table() 00322 thd Thread handler 00323 path Name of file (including database, without .frm) 00324 db Data base name 00325 table_name Table name 00326 create_info create info parameters 00327 create_fields Fields to create 00328 keys number of keys to create 00329 key_info Keys to create 00330 file Handler to use 00331 00332 RETURN 00333 0 ok 00334 1 error 00335 */ 00336 00337 int rea_create_table(THD *thd, const char *path, 00338 const char *db, const char *table_name, 00339 HA_CREATE_INFO *create_info, 00340 List<create_field> &create_fields, 00341 uint keys, KEY *key_info, handler *file) 00342 { 00343 DBUG_ENTER("rea_create_table"); 00344 00345 char frm_name[FN_REFLEN]; 00346 strxmov(frm_name, path, reg_ext, NullS); 00347 if (mysql_create_frm(thd, frm_name, db, table_name, create_info, 00348 create_fields, keys, key_info, file)) 00349 00350 DBUG_RETURN(1); 00351 00352 // Make sure mysql_create_frm din't remove extension 00353 DBUG_ASSERT(*fn_rext(frm_name)); 00354 if (file->create_handler_files(path, NULL, CHF_CREATE_FLAG, create_info)) 00355 goto err_handler; 00356 if (!create_info->frm_only && ha_create_table(thd, path, db, table_name, 00357 create_info,0)) 00358 goto err_handler; 00359 DBUG_RETURN(0); 00360 00361 err_handler: 00362 VOID(file->create_handler_files(path, NULL, CHF_DELETE_FLAG, create_info)); 00363 my_delete(frm_name, MYF(0)); 00364 DBUG_RETURN(1); 00365 } /* rea_create_table */ 00366 00367 00368 /* Pack screens to a screen for save in a form-file */ 00369 00370 static uchar *pack_screens(List<create_field> &create_fields, 00371 uint *info_length, uint *screens, 00372 bool small_file) 00373 { 00374 reg1 uint i; 00375 uint row,start_row,end_row,fields_on_screen; 00376 uint length,cols; 00377 uchar *info,*pos,*start_screen; 00378 uint fields=create_fields.elements; 00379 List_iterator<create_field> it(create_fields); 00380 DBUG_ENTER("pack_screens"); 00381 00382 start_row=4; end_row=22; cols=80; fields_on_screen=end_row+1-start_row; 00383 00384 *screens=(fields-1)/fields_on_screen+1; 00385 length= (*screens) * (SC_INFO_LENGTH+ (cols>> 1)+4); 00386 00387 create_field *field; 00388 while ((field=it++)) 00389 length+=(uint) strlen(field->field_name)+1+TE_INFO_LENGTH+cols/2; 00390 00391 if (!(info=(uchar*) my_malloc(length,MYF(MY_WME)))) 00392 DBUG_RETURN(0); 00393 00394 start_screen=0; 00395 row=end_row; 00396 pos=info; 00397 it.rewind(); 00398 for (i=0 ; i < fields ; i++) 00399 { 00400 create_field *cfield=it++; 00401 if (row++ == end_row) 00402 { 00403 if (i) 00404 { 00405 length=(uint) (pos-start_screen); 00406 int2store(start_screen,length); 00407 start_screen[2]=(uchar) (fields_on_screen+1); 00408 start_screen[3]=(uchar) (fields_on_screen); 00409 } 00410 row=start_row; 00411 start_screen=pos; 00412 pos+=4; 00413 pos[0]= (uchar) start_row-2; /* Header string */ 00414 pos[1]= (uchar) (cols >> 2); 00415 pos[2]= (uchar) (cols >> 1) +1; 00416 strfill((my_string) pos+3,(uint) (cols >> 1),' '); 00417 pos+=(cols >> 1)+4; 00418 } 00419 length=(uint) strlen(cfield->field_name); 00420 if (length > cols-3) 00421 length=cols-3; 00422 00423 if (!small_file) 00424 { 00425 pos[0]=(uchar) row; 00426 pos[1]=0; 00427 pos[2]=(uchar) (length+1); 00428 pos=(uchar*) strmake((char*) pos+3,cfield->field_name,length)+1; 00429 } 00430 cfield->row=(uint8) row; 00431 cfield->col=(uint8) (length+1); 00432 cfield->sc_length=(uint8) min(cfield->length,cols-(length+2)); 00433 } 00434 length=(uint) (pos-start_screen); 00435 int2store(start_screen,length); 00436 start_screen[2]=(uchar) (row-start_row+2); 00437 start_screen[3]=(uchar) (row-start_row+1); 00438 00439 *info_length=(uint) (pos-info); 00440 DBUG_RETURN(info); 00441 } /* pack_screens */ 00442 00443 00444 /* Pack keyinfo and keynames to keybuff for save in form-file. */ 00445 00446 static uint pack_keys(uchar *keybuff, uint key_count, KEY *keyinfo, 00447 ulong data_offset) 00448 { 00449 uint key_parts,length; 00450 uchar *pos, *keyname_pos; 00451 KEY *key,*end; 00452 KEY_PART_INFO *key_part,*key_part_end; 00453 DBUG_ENTER("pack_keys"); 00454 00455 pos=keybuff+6; 00456 key_parts=0; 00457 for (key=keyinfo,end=keyinfo+key_count ; key != end ; key++) 00458 { 00459 int2store(pos, (key->flags ^ HA_NOSAME)); 00460 int2store(pos+2,key->key_length); 00461 pos[4]= (uchar) key->key_parts; 00462 pos[5]= (uchar) key->algorithm; 00463 int2store(pos+6, key->block_size); 00464 pos+=8; 00465 key_parts+=key->key_parts; 00466 DBUG_PRINT("loop",("flags: %d key_parts: %d at 0x%lx", 00467 key->flags,key->key_parts, 00468 key->key_part)); 00469 for (key_part=key->key_part,key_part_end=key_part+key->key_parts ; 00470 key_part != key_part_end ; 00471 key_part++) 00472 00473 { 00474 uint offset; 00475 DBUG_PRINT("loop",("field: %d startpos: %lu length: %ld", 00476 key_part->fieldnr, key_part->offset + data_offset, 00477 key_part->length)); 00478 int2store(pos,key_part->fieldnr+1+FIELD_NAME_USED); 00479 offset= (uint) (key_part->offset+data_offset+1); 00480 int2store(pos+2, offset); 00481 pos[4]=0; // Sort order 00482 int2store(pos+5,key_part->key_type); 00483 int2store(pos+7,key_part->length); 00484 pos+=9; 00485 } 00486 } 00487 /* Save keynames */ 00488 keyname_pos=pos; 00489 *pos++=(uchar) NAMES_SEP_CHAR; 00490 for (key=keyinfo ; key != end ; key++) 00491 { 00492 uchar *tmp=(uchar*) strmov((char*) pos,key->name); 00493 *tmp++= (uchar) NAMES_SEP_CHAR; 00494 *tmp=0; 00495 pos=tmp; 00496 } 00497 *(pos++)=0; 00498 00499 if (key_count > 127 || key_parts > 127) 00500 { 00501 keybuff[0]= (key_count & 0x7f) | 0x80; 00502 keybuff[1]= key_count >> 7; 00503 int2store(keybuff+2,key_parts); 00504 } 00505 else 00506 { 00507 keybuff[0]=(uchar) key_count; 00508 keybuff[1]=(uchar) key_parts; 00509 keybuff[2]= keybuff[3]= 0; 00510 } 00511 length=(uint) (pos-keyname_pos); 00512 int2store(keybuff+4,length); 00513 DBUG_RETURN((uint) (pos-keybuff)); 00514 } /* pack_keys */ 00515 00516 00517 /* Make formheader */ 00518 00519 static bool pack_header(uchar *forminfo, enum legacy_db_type table_type, 00520 List<create_field> &create_fields, 00521 uint info_length, uint screens, uint table_options, 00522 ulong data_offset, handler *file) 00523 { 00524 uint length,int_count,int_length,no_empty, int_parts; 00525 uint time_stamp_pos,null_fields; 00526 ulong reclength, totlength, n_length, com_length; 00527 DBUG_ENTER("pack_header"); 00528 00529 if (create_fields.elements > MAX_FIELDS) 00530 { 00531 my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0)); 00532 DBUG_RETURN(1); 00533 } 00534 00535 totlength= 0L; 00536 reclength= data_offset; 00537 no_empty=int_count=int_parts=int_length=time_stamp_pos=null_fields= 00538 com_length=0; 00539 n_length=2L; 00540 00541 /* Check fields */ 00542 00543 List_iterator<create_field> it(create_fields); 00544 create_field *field; 00545 while ((field=it++)) 00546 { 00547 00548 uint tmp_len= system_charset_info->cset->charpos(system_charset_info, 00549 field->comment.str, 00550 field->comment.str + 00551 field->comment.length, 255); 00552 if (tmp_len < field->comment.length) 00553 { 00554 char buff[128]; 00555 (void) my_snprintf(buff,sizeof(buff), "Too long comment for field '%s'", 00556 field->field_name); 00557 if ((current_thd->variables.sql_mode & 00558 (MODE_STRICT_TRANS_TABLES | MODE_STRICT_ALL_TABLES))) 00559 { 00560 my_message(ER_UNKNOWN_ERROR, buff, MYF(0)); 00561 DBUG_RETURN(1); 00562 } 00563 push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, 00564 ER_UNKNOWN_ERROR, ER(ER_UNKNOWN_ERROR), buff); 00565 field->comment.length= tmp_len; 00566 } 00567 00568 totlength+= field->length; 00569 com_length+= field->comment.length; 00570 if (MTYP_TYPENR(field->unireg_check) == Field::NOEMPTY || 00571 field->unireg_check & MTYP_NOEMPTY_BIT) 00572 { 00573 field->unireg_check= (Field::utype) ((uint) field->unireg_check | 00574 MTYP_NOEMPTY_BIT); 00575 no_empty++; 00576 } 00577 /* 00578 We mark first TIMESTAMP field with NOW() in DEFAULT or ON UPDATE 00579 as auto-update field. 00580 */ 00581 if (field->sql_type == FIELD_TYPE_TIMESTAMP && 00582 MTYP_TYPENR(field->unireg_check) != Field::NONE && 00583 !time_stamp_pos) 00584 time_stamp_pos= (uint) field->offset+ (uint) data_offset + 1; 00585 length=field->pack_length; 00586 /* Ensure we don't have any bugs when generating offsets */ 00587 DBUG_ASSERT(reclength == field->offset + data_offset); 00588 if ((uint) field->offset+ (uint) data_offset+ length > reclength) 00589 reclength=(uint) (field->offset+ data_offset + length); 00590 n_length+= (ulong) strlen(field->field_name)+1; 00591 field->interval_id=0; 00592 if (field->interval) 00593 { 00594 uint old_int_count=int_count; 00595 00596 if (field->charset->mbminlen > 1) 00597 { 00598 /* Escape UCS2 intervals using HEX notation */ 00599 for (uint pos= 0; pos < field->interval->count; pos++) 00600 { 00601 char *dst; 00602 uint length= field->interval->type_lengths[pos], hex_length; 00603 const char *src= field->interval->type_names[pos]; 00604 hex_length= length * 2; 00605 field->interval->type_lengths[pos]= hex_length; 00606 field->interval->type_names[pos]= dst= sql_alloc(hex_length + 1); 00607 octet2hex(dst, src, length); 00608 } 00609 } 00610 00611 field->interval_id=get_interval_id(&int_count,create_fields,field); 00612 if (old_int_count != int_count) 00613 { 00614 for (const char **pos=field->interval->type_names ; *pos ; pos++) 00615 int_length+=(uint) strlen(*pos)+1; // field + suffix prefix 00616 int_parts+=field->interval->count+1; 00617 } 00618 } 00619 if (f_maybe_null(field->pack_flag)) 00620 null_fields++; 00621 } 00622 int_length+=int_count*2; // 255 prefix + 0 suffix 00623 00624 /* Save values in forminfo */ 00625 00626 if (reclength > (ulong) file->max_record_length()) 00627 { 00628 my_error(ER_TOO_BIG_ROWSIZE, MYF(0), (uint) file->max_record_length()); 00629 DBUG_RETURN(1); 00630 } 00631 /* Hack to avoid bugs with small static rows in MySQL */ 00632 reclength=max(file->min_record_length(table_options),reclength); 00633 if (info_length+(ulong) create_fields.elements*FCOMP+288+ 00634 n_length+int_length+com_length > 65535L || int_count > 255) 00635 { 00636 my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0)); 00637 DBUG_RETURN(1); 00638 } 00639 00640 bzero((char*)forminfo,288); 00641 length=(info_length+create_fields.elements*FCOMP+288+n_length+int_length+ 00642 com_length); 00643 int2store(forminfo,length); 00644 forminfo[256] = (uint8) screens; 00645 int2store(forminfo+258,create_fields.elements); 00646 int2store(forminfo+260,info_length); 00647 int2store(forminfo+262,totlength); 00648 int2store(forminfo+264,no_empty); 00649 int2store(forminfo+266,reclength); 00650 int2store(forminfo+268,n_length); 00651 int2store(forminfo+270,int_count); 00652 int2store(forminfo+272,int_parts); 00653 int2store(forminfo+274,int_length); 00654 int2store(forminfo+276,time_stamp_pos); 00655 int2store(forminfo+278,80); /* Columns needed */ 00656 int2store(forminfo+280,22); /* Rows needed */ 00657 int2store(forminfo+282,null_fields); 00658 int2store(forminfo+284,com_length); 00659 /* Up to forminfo+288 is free to use for additional information */ 00660 DBUG_RETURN(0); 00661 } /* pack_header */ 00662 00663 00664 /* get each unique interval each own id */ 00665 00666 static uint get_interval_id(uint *int_count,List<create_field> &create_fields, 00667 create_field *last_field) 00668 { 00669 List_iterator<create_field> it(create_fields); 00670 create_field *field; 00671 TYPELIB *interval=last_field->interval; 00672 00673 while ((field=it++) != last_field) 00674 { 00675 if (field->interval_id && field->interval->count == interval->count) 00676 { 00677 const char **a,**b; 00678 for (a=field->interval->type_names, b=interval->type_names ; 00679 *a && !strcmp(*a,*b); 00680 a++,b++) ; 00681 00682 if (! *a) 00683 { 00684 return field->interval_id; // Re-use last interval 00685 } 00686 } 00687 } 00688 return ++*int_count; // New unique interval 00689 } 00690 00691 00692 /* Save fields, fieldnames and intervals */ 00693 00694 static bool pack_fields(File file, List<create_field> &create_fields, 00695 ulong data_offset) 00696 { 00697 reg2 uint i; 00698 uint int_count, comment_length=0; 00699 uchar buff[MAX_FIELD_WIDTH]; 00700 create_field *field; 00701 DBUG_ENTER("pack_fields"); 00702 00703 /* Write field info */ 00704 00705 List_iterator<create_field> it(create_fields); 00706 00707 int_count=0; 00708 while ((field=it++)) 00709 { 00710 uint recpos; 00711 buff[0]= (uchar) field->row; 00712 buff[1]= (uchar) field->col; 00713 buff[2]= (uchar) field->sc_length; 00714 int2store(buff+3, field->length); 00715 /* The +1 is here becasue the col offset in .frm file have offset 1 */ 00716 recpos= field->offset+1 + (uint) data_offset; 00717 int3store(buff+5,recpos); 00718 int2store(buff+8,field->pack_flag); 00719 int2store(buff+10,field->unireg_check); 00720 buff[12]= (uchar) field->interval_id; 00721 buff[13]= (uchar) field->sql_type; 00722 if (field->sql_type == FIELD_TYPE_GEOMETRY) 00723 { 00724 buff[14]= (uchar) field->geom_type; 00725 #ifndef HAVE_SPATIAL 00726 DBUG_ASSERT(0); // Should newer happen 00727 #endif 00728 } 00729 else if (field->charset) 00730 buff[14]= (uchar) field->charset->number; 00731 else 00732 buff[14]= 0; // Numerical 00733 int2store(buff+15, field->comment.length); 00734 comment_length+= field->comment.length; 00735 set_if_bigger(int_count,field->interval_id); 00736 if (my_write(file,(byte*) buff,FCOMP,MYF_RW)) 00737 DBUG_RETURN(1); 00738 } 00739 00740 /* Write fieldnames */ 00741 buff[0]=(uchar) NAMES_SEP_CHAR; 00742 if (my_write(file,(byte*) buff,1,MYF_RW)) 00743 DBUG_RETURN(1); 00744 i=0; 00745 it.rewind(); 00746 while ((field=it++)) 00747 { 00748 char *pos= strmov((char*) buff,field->field_name); 00749 *pos++=NAMES_SEP_CHAR; 00750 if (i == create_fields.elements-1) 00751 *pos++=0; 00752 if (my_write(file,(byte*) buff,(uint) (pos-(char*) buff),MYF_RW)) 00753 DBUG_RETURN(1); 00754 i++; 00755 } 00756 00757 /* Write intervals */ 00758 if (int_count) 00759 { 00760 String tmp((char*) buff,sizeof(buff), &my_charset_bin); 00761 tmp.length(0); 00762 it.rewind(); 00763 int_count=0; 00764 while ((field=it++)) 00765 { 00766 if (field->interval_id > int_count) 00767 { 00768 int_count=field->interval_id; 00769 tmp.append(NAMES_SEP_CHAR); 00770 for (const char **pos=field->interval->type_names ; *pos ; pos++) 00771 { 00772 tmp.append(*pos); 00773 tmp.append(NAMES_SEP_CHAR); 00774 } 00775 tmp.append('\0'); // End of intervall 00776 } 00777 } 00778 if (my_write(file,(byte*) tmp.ptr(),tmp.length(),MYF_RW)) 00779 DBUG_RETURN(1); 00780 } 00781 if (comment_length) 00782 { 00783 it.rewind(); 00784 int_count=0; 00785 while ((field=it++)) 00786 { 00787 if (field->comment.length) 00788 if (my_write(file, (byte*) field->comment.str, field->comment.length, 00789 MYF_RW)) 00790 DBUG_RETURN(1); 00791 } 00792 } 00793 DBUG_RETURN(0); 00794 } 00795 00796 00797 /* save an empty record on start of formfile */ 00798 00799 static bool make_empty_rec(THD *thd, File file,enum legacy_db_type table_type, 00800 uint table_options, 00801 List<create_field> &create_fields, 00802 uint reclength, 00803 ulong data_offset, 00804 handler *handler) 00805 { 00806 int error= 0; 00807 Field::utype type; 00808 uint null_count; 00809 uchar *buff,*null_pos; 00810 TABLE table; 00811 TABLE_SHARE share; 00812 create_field *field; 00813 enum_check_fields old_count_cuted_fields= thd->count_cuted_fields; 00814 DBUG_ENTER("make_empty_rec"); 00815 00816 /* We need a table to generate columns for default values */ 00817 bzero((char*) &table, sizeof(table)); 00818 bzero((char*) &share, sizeof(share)); 00819 table.s= &share; 00820 00821 if (!(buff=(uchar*) my_malloc((uint) reclength,MYF(MY_WME | MY_ZEROFILL)))) 00822 { 00823 DBUG_RETURN(1); 00824 } 00825 00826 table.in_use= thd; 00827 table.s->db_low_byte_first= handler->low_byte_first(); 00828 table.s->blob_ptr_size= portable_sizeof_char_ptr; 00829 00830 null_count=0; 00831 if (!(table_options & HA_OPTION_PACK_RECORD)) 00832 { 00833 null_count++; // Need one bit for delete mark 00834 *buff|= 1; 00835 } 00836 null_pos= buff; 00837 00838 List_iterator<create_field> it(create_fields); 00839 thd->count_cuted_fields= CHECK_FIELD_WARN; // To find wrong default values 00840 while ((field=it++)) 00841 { 00842 /* 00843 regfield don't have to be deleted as it's allocated with sql_alloc() 00844 */ 00845 Field *regfield= make_field(&share, 00846 (char*) buff+field->offset + data_offset, 00847 field->length, 00848 null_pos + null_count / 8, 00849 null_count & 7, 00850 field->pack_flag, 00851 field->sql_type, 00852 field->charset, 00853 field->geom_type, 00854 field->unireg_check, 00855 field->interval, 00856 field->field_name); 00857 if (!regfield) 00858 goto err; // End of memory 00859 00860 /* save_in_field() will access regfield->table->in_use */ 00861 regfield->init(&table); 00862 00863 if (!(field->flags & NOT_NULL_FLAG)) 00864 { 00865 *regfield->null_ptr|= regfield->null_bit; 00866 null_count++; 00867 } 00868 00869 if (field->sql_type == FIELD_TYPE_BIT && !f_bit_as_char(field->pack_flag)) 00870 null_count+= field->length & 7; 00871 00872 type= (Field::utype) MTYP_TYPENR(field->unireg_check); 00873 00874 if (field->def && 00875 (regfield->real_type() != FIELD_TYPE_YEAR || 00876 field->def->val_int() != 0)) 00877 { 00878 if (field->def->save_in_field(regfield, 1)) 00879 { 00880 my_error(ER_INVALID_DEFAULT, MYF(0), regfield->field_name); 00881 error= 1; 00882 delete regfield; //To avoid memory leak 00883 goto err; 00884 } 00885 } 00886 else if (regfield->real_type() == FIELD_TYPE_ENUM && 00887 (field->flags & NOT_NULL_FLAG)) 00888 { 00889 regfield->set_notnull(); 00890 regfield->store((longlong) 1, TRUE); 00891 } 00892 else if (type == Field::YES) // Old unireg type 00893 regfield->store(ER(ER_YES),(uint) strlen(ER(ER_YES)),system_charset_info); 00894 else if (type == Field::NO) // Old unireg type 00895 regfield->store(ER(ER_NO), (uint) strlen(ER(ER_NO)),system_charset_info); 00896 else 00897 regfield->reset(); 00898 } 00899 DBUG_ASSERT(data_offset == ((null_count + 7) / 8)); 00900 00901 /* 00902 We need to set the unused bits to 1. If the number of bits is a multiple 00903 of 8 there are no unused bits. 00904 */ 00905 if (null_count & 7) 00906 *(null_pos + null_count / 8)|= ~(((uchar) 1 << (null_count & 7)) - 1); 00907 00908 error=(int) my_write(file,(byte*) buff, (uint) reclength,MYF_RW); 00909 00910 err: 00911 my_free((gptr) buff,MYF(MY_FAE)); 00912 thd->count_cuted_fields= old_count_cuted_fields; 00913 DBUG_RETURN(error); 00914 } /* make_empty_rec */
1.4.7

