23
23
str is a (long) to record position where 0 is the first position.
26
#include <drizzled/server_includes.h>
27
#include <drizzled/drizzled_error_messages.h>
26
#include "mysql_priv.h"
29
30
#define FCOMP 17 /* Bytes for a packed field */
31
static unsigned char * pack_screens(List<Create_field> &create_fields,
32
uint32_t *info_length, uint32_t *screens, bool small_file);
33
static uint32_t pack_keys(unsigned char *keybuff,uint32_t key_count, KEY *key_info,
32
static uchar * pack_screens(List<Create_field> &create_fields,
33
uint *info_length, uint *screens, bool small_file);
34
static uint pack_keys(uchar *keybuff,uint key_count, KEY *key_info,
34
35
ulong data_offset);
35
static bool pack_header(unsigned char *forminfo,
36
static bool pack_header(uchar *forminfo,enum legacy_db_type table_type,
36
37
List<Create_field> &create_fields,
37
uint32_t info_length, uint32_t screens, uint32_t table_options,
38
uint info_length, uint screens, uint table_options,
38
39
ulong data_offset, handler *file);
39
static uint32_t get_interval_id(uint32_t *int_count,List<Create_field> &create_fields,
40
static uint get_interval_id(uint *int_count,List<Create_field> &create_fields,
40
41
Create_field *last_field);
41
42
static bool pack_fields(File file, List<Create_field> &create_fields,
42
43
ulong data_offset);
43
44
static bool make_empty_rec(THD *thd, int file, enum legacy_db_type table_type,
44
uint32_t table_options,
45
46
List<Create_field> &create_fields,
46
uint32_t reclength, ulong data_offset,
47
uint reclength, ulong data_offset,
56
57
struct Pack_header_error_handler: public Internal_error_handler
58
virtual bool handle_error(uint32_t sql_errno,
59
virtual bool handle_error(uint sql_errno,
59
60
const char *message,
60
DRIZZLE_ERROR::enum_warning_level level,
61
MYSQL_ERROR::enum_warning_level level,
63
Pack_header_error_handler() :is_handled(false) {}
64
Pack_header_error_handler() :is_handled(FALSE) {}
68
69
Pack_header_error_handler::
69
handle_error(uint32_t sql_errno,
70
handle_error(uint sql_errno,
70
71
const char * /* message */,
71
DRIZZLE_ERROR::enum_warning_level /* level */,
72
MYSQL_ERROR::enum_warning_level /* level */,
74
75
is_handled= (sql_errno == ER_TOO_MANY_FIELDS);
99
100
const char *db, const char *table,
100
101
HA_CREATE_INFO *create_info,
101
102
List<Create_field> &create_fields,
102
uint32_t keys, KEY *key_info,
103
uint keys, KEY *key_info,
103
104
handler *db_file)
105
106
LEX_STRING str_db_type;
106
uint32_t reclength, info_length, screens, key_info_length, maxlength, tmp_len;
107
uint reclength, info_length, screens, key_info_length, maxlength, tmp_len;
107
108
ulong key_buff_length;
109
110
ulong filepos, data_offset;
110
unsigned char fileinfo[64],forminfo[288],*keybuff;
111
uchar fileinfo[64],forminfo[288],*keybuff;
111
112
TYPELIB formnames;
112
unsigned char *screen_buff;
114
const uint32_t format_section_header_size= 8;
115
uint32_t format_section_len;
115
const uint format_section_header_size= 8;
116
uint format_section_len;
117
uint tablespace_len= 0;
116
118
Pack_header_error_handler pack_header_error_handler;
120
DBUG_ENTER("mysql_create_frm");
119
assert(*fn_rext((char*)file_name)); // Check .frm extension
122
DBUG_ASSERT(*fn_rext((char*)file_name)); // Check .frm extension
120
123
formnames.type_names=0;
121
124
if (!(screen_buff=pack_screens(create_fields,&info_length,&screens,0)))
123
assert(db_file != NULL);
126
DBUG_ASSERT(db_file != NULL);
125
128
/* If fixed row records, we need one bit to check for deleted rows */
126
129
if (!(create_info->table_options & HA_OPTION_PACK_RECORD))
130
133
thd->push_internal_handler(&pack_header_error_handler);
132
error= pack_header(forminfo,
135
error= pack_header(forminfo, ha_legacy_type(create_info->db_type),
133
136
create_fields,info_length,
134
137
screens, create_info->table_options,
135
138
data_offset, db_file);
144
my_free(screen_buff, MYF(0));
142
145
if (! pack_header_error_handler.is_handled)
145
148
// Try again without UNIREG screens (to get more columns)
146
149
if (!(screen_buff=pack_screens(create_fields,&info_length,&screens,1)))
148
if (pack_header(forminfo,
151
if (pack_header(forminfo, ha_legacy_type(create_info->db_type),
149
152
create_fields,info_length,
150
153
screens, create_info->table_options, data_offset, db_file))
155
my_free(screen_buff, MYF(0));
156
159
reclength=uint2korr(forminfo+266);
171
174
create_info->extra_size+= 6;
173
176
/* Add space for storage type and field format array of fields */
177
if (create_info->tablespace)
178
tablespace_len= strlen(create_info->tablespace);
174
179
format_section_len=
175
format_section_header_size + 1 + create_fields.elements;
180
format_section_header_size +
182
create_fields.elements;
176
183
create_info->extra_size+= format_section_len;
178
185
tmp_len= system_charset_info->cset->charpos(system_charset_info,
184
191
if (tmp_len < create_info->comment.length)
186
193
my_error(ER_WRONG_STRING_LENGTH, MYF(0),
187
create_info->comment.str,"Table COMMENT",
194
create_info->comment.str,"TABLE COMMENT",
188
195
(uint) TABLE_COMMENT_MAXLEN);
196
my_free(screen_buff,MYF(0));
193
200
//if table comment is larger than 180 bytes, store into extra segment.
200
207
strmake((char*) forminfo+47, create_info->comment.str ?
201
208
create_info->comment.str : "", create_info->comment.length);
202
forminfo[46]=(unsigned char) create_info->comment.length;
209
forminfo[46]=(uchar) create_info->comment.length;
203
210
#ifdef EXTRA_DEBUG
205
212
EXTRA_DEBUG causes strmake() to initialize its buffer behind the
206
213
payload with a magic value to detect wrong buffer-sizes. We
207
214
explicitly zero that segment again.
209
memset(forminfo+47 + forminfo[46], 0, 61 - forminfo[46]);
216
memset((char*) forminfo+47 + forminfo[46], 0, 61 - forminfo[46]);
213
220
if ((file=create_frm(thd, file_name, db, table, reclength, fileinfo,
214
221
create_info, keys, key_info)) < 0)
223
my_free(screen_buff, MYF(0));
220
227
key_buff_length= uint4korr(fileinfo+47);
221
keybuff=(unsigned char*) my_malloc(key_buff_length, MYF(0));
228
keybuff=(uchar*) my_malloc(key_buff_length, MYF(0));
222
229
key_info_length= pack_keys(keybuff, keys, key_info, data_offset);
223
get_form_pos(file,fileinfo,&formnames);
230
VOID(get_form_pos(file,fileinfo,&formnames));
224
231
if (!(filepos=make_new_entry(file,fileinfo,&formnames,"")))
226
233
maxlength=(uint) next_io_size((ulong) (uint2korr(forminfo)+1000));
227
234
int2store(forminfo+2,maxlength);
228
235
int4store(fileinfo+10,(ulong) (filepos+maxlength));
229
fileinfo[26]= (unsigned char) test((create_info->max_rows == 1) &&
236
fileinfo[26]= (uchar) test((create_info->max_rows == 1) &&
230
237
(create_info->min_rows == 1) && (keys == 0));
231
238
int2store(fileinfo+28,key_info_length);
236
243
if (pwrite(file, fileinfo, 64, 0L) == 0 ||
237
244
pwrite(file, keybuff, key_info_length, (ulong) uint2korr(fileinfo+6)) == 0)
240
247
(ulong) uint2korr(fileinfo+6)+ (ulong) key_buff_length,
248
MY_SEEK_SET,MYF(0)));
242
249
if (make_empty_rec(thd,file,ha_legacy_type(create_info->db_type),
243
250
create_info->table_options,
244
251
create_fields,reclength, data_offset, db_file))
247
254
int2store(buff, create_info->connect_string.length);
248
if (my_write(file, (const unsigned char*)buff, 2, MYF(MY_NABP)) ||
249
my_write(file, (const unsigned char*)create_info->connect_string.str,
255
if (my_write(file, (const uchar*)buff, 2, MYF(MY_NABP)) ||
256
my_write(file, (const uchar*)create_info->connect_string.str,
250
257
create_info->connect_string.length, MYF(MY_NABP)))
253
260
int2store(buff, str_db_type.length);
254
if (my_write(file, (const unsigned char*)buff, 2, MYF(MY_NABP)) ||
255
my_write(file, (const unsigned char*)str_db_type.str,
261
if (my_write(file, (const uchar*)buff, 2, MYF(MY_NABP)) ||
262
my_write(file, (const uchar*)str_db_type.str,
256
263
str_db_type.length, MYF(MY_NABP)))
261
if (my_write(file, (unsigned char*) buff, 6, MYF_RW))
267
bzero((uchar*) buff, 6);
268
if (my_write(file, (uchar*) buff, 6, MYF_RW))
265
if (forminfo[46] == (unsigned char)255)
272
if (forminfo[46] == (uchar)255)
267
unsigned char comment_length_buff[2];
274
uchar comment_length_buff[2];
268
275
int2store(comment_length_buff,create_info->comment.length);
269
276
if (my_write(file, comment_length_buff, 2, MYF(MY_NABP)) ||
270
my_write(file, (unsigned char*)create_info->comment.str,
277
my_write(file, (uchar*)create_info->comment.str,
271
278
create_info->comment.length, MYF(MY_NABP)))
277
284
/* prepare header */
287
flags|= create_info->default_storage_media; //3 bits
281
memset(buff, 0, format_section_header_size);
289
bzero(buff, format_section_header_size);
282
290
/* length of section 2 bytes*/
283
291
int2store(buff+0, format_section_len);
284
292
/* flags of section 4 bytes*/
286
294
/* 2 bytes left for future use */
288
296
/* write header */
289
if (my_write(file, (const unsigned char*)buff, format_section_header_size, MYF_RW))
297
if (my_write(file, (const uchar*)buff, format_section_header_size, MYF_RW))
299
/* write tablespace name */
300
if (tablespace_len > 0)
301
if (my_write(file, (const uchar*)create_info->tablespace, tablespace_len, MYF_RW))
292
if (my_write(file, (const unsigned char*)buff, 1, MYF_RW))
304
if (my_write(file, (const uchar*)buff, 1, MYF_RW))
294
306
/* write column info, 1 byte per column */
296
308
List_iterator<Create_field> it(create_fields);
297
309
Create_field *field;
298
unsigned char column_format, write_byte;
310
uchar storage_type, column_format, write_byte;
299
311
while ((field=it++))
301
column_format= (unsigned char)field->column_format();
302
write_byte= (column_format << COLUMN_FORMAT_SHIFT);
313
storage_type= (uchar)field->field_storage_type();
314
column_format= (uchar)field->column_format();
315
write_byte= storage_type + (column_format << COLUMN_FORMAT_SHIFT);
303
316
if (my_write(file, &write_byte, 1, MYF_RW))
308
my_seek(file,filepos,MY_SEEK_SET,MYF(0));
321
VOID(my_seek(file,filepos,MY_SEEK_SET,MYF(0)));
309
322
if (my_write(file, forminfo, 288, MYF_RW) ||
310
323
my_write(file, screen_buff, info_length, MYF_RW) ||
311
324
pack_fields(file, create_fields, data_offset))
327
my_free(screen_buff,MYF(0));
328
my_free(keybuff, MYF(0));
317
if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
330
if (opt_sync_frm && !(create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
318
331
(my_sync(file, MYF(MY_WME)) ||
319
332
my_sync_dir_by_file(file_name, MYF(MY_WME))))
375
388
const char *db, const char *table_name,
376
389
HA_CREATE_INFO *create_info,
377
390
List<Create_field> &create_fields,
378
uint32_t keys, KEY *key_info, handler *file)
391
uint keys, KEY *key_info, handler *file)
393
DBUG_ENTER("rea_create_table");
382
395
char frm_name[FN_REFLEN];
383
strxmov(frm_name, path, reg_ext, NULL);
396
strxmov(frm_name, path, reg_ext, NullS);
384
397
if (mysql_create_frm(thd, frm_name, db, table_name, create_info,
385
398
create_fields, keys, key_info, file))
389
402
// Make sure mysql_create_frm din't remove extension
390
assert(*fn_rext(frm_name));
403
DBUG_ASSERT(*fn_rext(frm_name));
391
404
if (thd->variables.keep_files_on_create)
392
405
create_info->options|= HA_CREATE_KEEP_FILES;
393
406
if (file->ha_create_handler_files(path, NULL, CHF_CREATE_FLAG, create_info))
395
408
if (!create_info->frm_only && ha_create_table(thd, path, db, table_name,
397
410
goto err_handler;
401
file->ha_create_handler_files(path, NULL, CHF_DELETE_FLAG, create_info);
414
VOID(file->ha_create_handler_files(path, NULL, CHF_DELETE_FLAG, create_info));
402
415
my_delete(frm_name, MYF(0));
404
417
} /* rea_create_table */
407
420
/* Pack screens to a screen for save in a form-file */
409
static unsigned char *pack_screens(List<Create_field> &create_fields,
410
uint32_t *info_length, uint32_t *screens,
422
static uchar *pack_screens(List<Create_field> &create_fields,
423
uint *info_length, uint *screens,
414
uint32_t row,start_row,end_row,fields_on_screen;
415
uint32_t length,cols;
416
unsigned char *info,*pos,*start_screen;
417
uint32_t fields=create_fields.elements;
427
uint row,start_row,end_row,fields_on_screen;
429
uchar *info,*pos,*start_screen;
430
uint fields=create_fields.elements;
418
431
List_iterator<Create_field> it(create_fields);
432
DBUG_ENTER("pack_screens");
421
434
start_row=4; end_row=22; cols=80; fields_on_screen=end_row+1-start_row;
444
457
length=(uint) (pos-start_screen);
445
458
int2store(start_screen,length);
446
start_screen[2]=(unsigned char) (fields_on_screen+1);
447
start_screen[3]=(unsigned char) (fields_on_screen);
459
start_screen[2]=(uchar) (fields_on_screen+1);
460
start_screen[3]=(uchar) (fields_on_screen);
450
463
start_screen=pos;
452
pos[0]= (unsigned char) start_row-2; /* Header string */
453
pos[1]= (unsigned char) (cols >> 2);
454
pos[2]= (unsigned char) (cols >> 1) +1;
465
pos[0]= (uchar) start_row-2; /* Header string */
466
pos[1]= (uchar) (cols >> 2);
467
pos[2]= (uchar) (cols >> 1) +1;
455
468
strfill((char *) pos+3,(uint) (cols >> 1),' ');
456
469
pos+=(cols >> 1)+4;
464
pos[0]=(unsigned char) row;
466
pos[2]=(unsigned char) (length+1);
467
pos=(unsigned char*) strmake((char*) pos+3,cfield->field_name,length)+1;
479
pos[2]=(uchar) (length+1);
480
pos=(uchar*) strmake((char*) pos+3,cfield->field_name,length)+1;
469
cfield->row=(uint8_t) row;
470
cfield->col=(uint8_t) (length+1);
471
cfield->sc_length=(uint8_t) cmin(cfield->length,(uint32_t)cols-(length+2));
482
cfield->row=(uint8) row;
483
cfield->col=(uint8) (length+1);
484
cfield->sc_length=(uint8) min(cfield->length,cols-(length+2));
473
486
length=(uint) (pos-start_screen);
474
487
int2store(start_screen,length);
475
start_screen[2]=(unsigned char) (row-start_row+2);
476
start_screen[3]=(unsigned char) (row-start_row+1);
488
start_screen[2]=(uchar) (row-start_row+2);
489
start_screen[3]=(uchar) (row-start_row+1);
478
491
*info_length=(uint) (pos-info);
480
493
} /* pack_screens */
483
496
/* Pack keyinfo and keynames to keybuff for save in form-file. */
485
static uint32_t pack_keys(unsigned char *keybuff, uint32_t key_count, KEY *keyinfo,
498
static uint pack_keys(uchar *keybuff, uint key_count, KEY *keyinfo,
486
499
ulong data_offset)
488
uint32_t key_parts,length;
489
unsigned char *pos, *keyname_pos;
501
uint key_parts,length;
502
uchar *pos, *keyname_pos;
491
504
KEY_PART_INFO *key_part,*key_part_end;
505
DBUG_ENTER("pack_keys");
498
511
int2store(pos, (key->flags ^ HA_NOSAME));
499
512
int2store(pos+2,key->key_length);
500
pos[4]= (unsigned char) key->key_parts;
501
pos[5]= (unsigned char) key->algorithm;
513
pos[4]= (uchar) key->key_parts;
514
pos[5]= (uchar) key->algorithm;
502
515
int2store(pos+6, key->block_size);
504
517
key_parts+=key->key_parts;
518
DBUG_PRINT("loop", ("flags: %lu key_parts: %d at 0x%lx",
519
key->flags, key->key_parts,
520
(long) key->key_part));
505
521
for (key_part=key->key_part,key_part_end=key_part+key->key_parts ;
506
522
key_part != key_part_end ;
527
DBUG_PRINT("loop",("field: %d startpos: %lu length: %d",
528
key_part->fieldnr, key_part->offset + data_offset,
511
530
int2store(pos,key_part->fieldnr+1+FIELD_NAME_USED);
512
531
offset= (uint) (key_part->offset+data_offset+1);
513
532
int2store(pos+2, offset);
520
539
/* Save keynames */
522
*pos++=(unsigned char) NAMES_SEP_CHAR;
541
*pos++=(uchar) NAMES_SEP_CHAR;
523
542
for (key=keyinfo ; key != end ; key++)
525
unsigned char *tmp=(unsigned char*) my_stpcpy((char*) pos,key->name);
526
*tmp++= (unsigned char) NAMES_SEP_CHAR;
544
uchar *tmp=(uchar*) strmov((char*) pos,key->name);
545
*tmp++= (uchar) NAMES_SEP_CHAR;
550
keybuff[0]=(unsigned char) key_count;
551
keybuff[1]=(unsigned char) key_parts;
569
keybuff[0]=(uchar) key_count;
570
keybuff[1]=(uchar) key_parts;
552
571
keybuff[2]= keybuff[3]= 0;
554
573
length=(uint) (pos-keyname_pos);
555
574
int2store(keybuff+4,length);
556
return((uint) (pos-keybuff));
575
DBUG_RETURN((uint) (pos-keybuff));
557
576
} /* pack_keys */
560
/* Make formheader */
579
/* Make formheader */
562
static bool pack_header(unsigned char *forminfo,
563
List<Create_field> &create_fields,
564
uint32_t info_length, uint32_t screens, uint32_t table_options,
581
static bool pack_header(uchar *forminfo, enum legacy_db_type table_type,
582
List<Create_field> &create_fields,
583
uint info_length, uint screens, uint table_options,
565
584
ulong data_offset, handler *file)
567
uint32_t length,int_count,int_length,no_empty, int_parts;
568
uint32_t time_stamp_pos,null_fields;
569
ulong reclength, totlength, n_length, com_length, vcol_info_length;
586
uint length,int_count,int_length,no_empty, int_parts;
587
uint time_stamp_pos,null_fields;
588
ulong reclength, totlength, n_length, com_length;
589
DBUG_ENTER("pack_header");
572
591
if (create_fields.elements > MAX_FIELDS)
574
593
my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0));
579
598
reclength= data_offset;
580
599
no_empty=int_count=int_parts=int_length=time_stamp_pos=null_fields=
581
com_length=vcol_info_length=0;
584
603
/* Check fields */
598
617
my_error(ER_WRONG_STRING_LENGTH, MYF(0),
599
618
field->comment.str,"COLUMN COMMENT",
600
619
(uint) COLUMN_COMMENT_MAXLEN);
603
if (field->vcol_info)
605
tmp_len= system_charset_info->cset->charpos(system_charset_info,
606
field->vcol_info->expr_str.str,
607
field->vcol_info->expr_str.str +
608
field->vcol_info->expr_str.length,
609
VIRTUAL_COLUMN_EXPRESSION_MAXLEN);
611
if (tmp_len < field->vcol_info->expr_str.length)
613
my_error(ER_WRONG_STRING_LENGTH, MYF(0),
614
field->vcol_info->expr_str.str,"VIRTUAL COLUMN EXPRESSION",
615
(uint) VIRTUAL_COLUMN_EXPRESSION_MAXLEN);
619
Sum up the length of the expression string and mandatory header bytes
622
vcol_info_length+= field->vcol_info->expr_str.length+(uint)FRM_VCOL_HEADER_SIZE;
625
623
totlength+= field->length;
635
633
We mark first TIMESTAMP field with NOW() in DEFAULT or ON UPDATE
636
634
as auto-update field.
638
if (field->sql_type == DRIZZLE_TYPE_TIMESTAMP &&
636
if (field->sql_type == MYSQL_TYPE_TIMESTAMP &&
639
637
MTYP_TYPENR(field->unireg_check) != Field::NONE &&
641
639
time_stamp_pos= (uint) field->offset+ (uint) data_offset + 1;
642
640
length=field->pack_length;
641
/* Ensure we don't have any bugs when generating offsets */
642
DBUG_ASSERT(reclength == field->offset + data_offset);
643
643
if ((uint) field->offset+ (uint) data_offset+ length > reclength)
644
644
reclength=(uint) (field->offset+ data_offset + length);
645
645
n_length+= (ulong) strlen(field->field_name)+1;
667
667
(field->interval->count+1));
668
668
field->interval->type_names[field->interval->count]= 0;
669
669
field->interval->type_lengths=
670
(uint32_t *) sql_alloc(sizeof(uint) * field->interval->count);
670
(uint *) sql_alloc(sizeof(uint) * field->interval->count);
672
for (uint32_t pos= 0; pos < field->interval->count; pos++)
672
for (uint pos= 0; pos < field->interval->count; pos++)
675
675
const char *src= field->save_interval->type_names[pos];
677
677
length= field->save_interval->type_lengths[pos];
678
678
hex_length= length * 2;
679
679
field->interval->type_lengths[pos]= hex_length;
701
701
if (reclength > (ulong) file->max_record_length())
703
703
my_error(ER_TOO_BIG_ROWSIZE, MYF(0), (uint) file->max_record_length());
706
706
/* Hack to avoid bugs with small static rows in MySQL */
707
reclength=cmax((ulong)file->min_record_length(table_options),reclength);
707
reclength=max(file->min_record_length(table_options),reclength);
708
708
if (info_length+(ulong) create_fields.elements*FCOMP+288+
709
n_length+int_length+com_length+vcol_info_length > 65535L ||
709
n_length+int_length+com_length > 65535L || int_count > 255)
712
711
my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0));
716
memset(forminfo, 0, 288);
715
bzero((char*)forminfo,288);
717
716
length=(info_length+create_fields.elements*FCOMP+288+n_length+int_length+
718
com_length+vcol_info_length);
719
718
int2store(forminfo,length);
720
forminfo[256] = (uint8_t) screens;
719
forminfo[256] = (uint8) screens;
721
720
int2store(forminfo+258,create_fields.elements);
722
721
int2store(forminfo+260,info_length);
723
722
int2store(forminfo+262,totlength);
732
731
int2store(forminfo+280,22); /* Rows needed */
733
732
int2store(forminfo+282,null_fields);
734
733
int2store(forminfo+284,com_length);
735
int2store(forminfo+286,vcol_info_length);
736
/* forminfo+288 is free to use for additional information */
734
/* Up to forminfo+288 is free to use for additional information */
738
736
} /* pack_header */
741
739
/* get each unique interval each own id */
743
static uint32_t get_interval_id(uint32_t *int_count,List<Create_field> &create_fields,
741
static uint get_interval_id(uint *int_count,List<Create_field> &create_fields,
744
742
Create_field *last_field)
746
744
List_iterator<Create_field> it(create_fields);
771
769
static bool pack_fields(File file, List<Create_field> &create_fields,
772
770
ulong data_offset)
775
uint32_t int_count, comment_length=0, vcol_info_length=0;
776
unsigned char buff[MAX_FIELD_WIDTH];
773
uint int_count, comment_length=0;
774
uchar buff[MAX_FIELD_WIDTH];
777
775
Create_field *field;
776
DBUG_ENTER("pack_fields");
780
778
/* Write field info */
785
783
while ((field=it++))
788
uint32_t cur_vcol_expr_len= 0;
789
buff[0]= (unsigned char) field->row;
790
buff[1]= (unsigned char) field->col;
791
buff[2]= (unsigned char) field->sc_length;
786
buff[0]= (uchar) field->row;
787
buff[1]= (uchar) field->col;
788
buff[2]= (uchar) field->sc_length;
792
789
int2store(buff+3, field->length);
793
790
/* The +1 is here becasue the col offset in .frm file have offset 1 */
794
791
recpos= field->offset+1 + (uint) data_offset;
795
792
int3store(buff+5,recpos);
796
793
int2store(buff+8,field->pack_flag);
797
794
int2store(buff+10,field->unireg_check);
798
buff[12]= (unsigned char) field->interval_id;
799
buff[13]= (unsigned char) field->sql_type;
795
buff[12]= (uchar) field->interval_id;
796
buff[13]= (uchar) field->sql_type;
800
797
if (field->charset)
801
buff[14]= (unsigned char) field->charset->number;
798
buff[14]= (uchar) field->charset->number;
803
800
buff[14]= 0; // Numerical
804
if (field->vcol_info)
807
Use the interval_id place in the .frm file to store the length of
808
virtual field's data.
810
buff[12]= cur_vcol_expr_len= field->vcol_info->expr_str.length +
811
(uint)FRM_VCOL_HEADER_SIZE;
812
vcol_info_length+= cur_vcol_expr_len+(uint)FRM_VCOL_HEADER_SIZE;
813
buff[13]= (unsigned char) DRIZZLE_TYPE_VIRTUAL;
815
801
int2store(buff+15, field->comment.length);
816
802
comment_length+= field->comment.length;
817
803
set_if_bigger(int_count,field->interval_id);
818
804
if (my_write(file, buff, FCOMP, MYF_RW))
822
808
/* Write fieldnames */
823
buff[0]=(unsigned char) NAMES_SEP_CHAR;
809
buff[0]=(uchar) NAMES_SEP_CHAR;
824
810
if (my_write(file, buff, 1, MYF_RW))
828
814
while ((field=it++))
830
char *pos= my_stpcpy((char*) buff,field->field_name);
816
char *pos= strmov((char*) buff,field->field_name);
831
817
*pos++=NAMES_SEP_CHAR;
832
818
if (i == create_fields.elements-1)
834
820
if (my_write(file, buff, (size_t) (pos-(char*) buff),MYF_RW))
850
836
unsigned char sep= 0;
851
837
unsigned char occ[256];
853
839
unsigned char *val= NULL;
855
memset(occ, 0, sizeof(occ));
841
bzero(occ, sizeof(occ));
857
843
for (i=0; (val= (unsigned char*) field->interval->type_names[i]); i++)
858
for (uint32_t j = 0; j < field->interval->type_lengths[i]; j++)
844
for (uint j = 0; j < field->interval->type_lengths[i]; j++)
859
845
occ[(unsigned int) (val[j])]= 1;
861
847
if (!occ[(unsigned char)NAMES_SEP_CHAR])
901
887
while ((field=it++))
903
889
if (field->comment.length)
904
if (my_write(file, (unsigned char*) field->comment.str, field->comment.length,
890
if (my_write(file, (uchar*) field->comment.str, field->comment.length,
909
if (vcol_info_length)
916
Pack each virtual field as follows:
917
byte 1 = 1 (always 1 to allow for future extensions)
919
byte 3 = flags (as of now, 0 - no flags, 1 - field is physically stored)
920
byte 4-... = virtual column expression (text data)
922
if (field->vcol_info && field->vcol_info->expr_str.length)
924
buff[0]= (unsigned char)1;
925
buff[1]= (unsigned char) field->sql_type;
926
buff[2]= (unsigned char) field->is_stored;
927
if (my_write(file, buff, 3, MYF_RW))
930
(unsigned char*) field->vcol_info->expr_str.str,
931
field->vcol_info->expr_str.length,
941
/* save an empty record on start of formfile */
899
/* save an empty record on start of formfile */
943
static bool make_empty_rec(THD *thd, File file,
944
enum legacy_db_type table_type __attribute__((unused)),
945
uint32_t table_options,
946
List<Create_field> &create_fields,
901
static bool make_empty_rec(THD *thd, File file,enum legacy_db_type table_type,
903
List<Create_field> &create_fields,
948
905
ulong data_offset,
949
906
handler *handler)
952
909
Field::utype type;
954
unsigned char *buff,*null_pos;
911
uchar *buff,*null_pos;
956
913
TABLE_SHARE share;
957
914
Create_field *field;
958
915
enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
916
DBUG_ENTER("make_empty_rec");
961
918
/* We need a table to generate columns for default values */
962
memset(&table, 0, sizeof(table));
963
memset(&share, 0, sizeof(share));
919
bzero((char*) &table, sizeof(table));
920
bzero((char*) &share, sizeof(share));
966
if (!(buff=(unsigned char*) my_malloc((size_t) reclength,MYF(MY_WME | MY_ZEROFILL))))
923
if (!(buff=(uchar*) my_malloc((size_t) reclength,MYF(MY_WME | MY_ZEROFILL))))
971
928
table.in_use= thd;
1031
else if (regfield->real_type() == DRIZZLE_TYPE_ENUM &&
991
else if (regfield->real_type() == MYSQL_TYPE_ENUM &&
1032
992
(field->flags & NOT_NULL_FLAG))
1034
994
regfield->set_notnull();
1035
regfield->store((int64_t) 1, true);
995
regfield->store((longlong) 1, TRUE);
1037
997
else if (type == Field::YES) // Old unireg type
1038
998
regfield->store(ER(ER_YES),(uint) strlen(ER(ER_YES)),system_charset_info);
1042
1002
regfield->reset();
1044
assert(data_offset == ((null_count + 7) / 8));
1004
DBUG_ASSERT(data_offset == ((null_count + 7) / 8));
1047
1007
We need to set the unused bits to 1. If the number of bits is a multiple
1048
1008
of 8 there are no unused bits.
1050
1010
if (null_count & 7)
1051
*(null_pos + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1011
*(null_pos + null_count / 8)|= ~(((uchar) 1 << (null_count & 7)) - 1);
1053
1013
error= my_write(file, buff, (size_t) reclength,MYF_RW) != 0;
1016
my_free(buff, MYF(MY_FAE));
1057
1017
thd->count_cuted_fields= old_count_cuted_fields;
1059
1019
} /* make_empty_rec */