~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/unireg.cc

  • Committer: Monty Taylor
  • Date: 2008-09-15 17:24:04 UTC
  • Revision ID: monty@inaugust.com-20080915172404-ygh6hiyu0q7qpa9x
Removed strndup calls.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
*/
25
25
 
26
26
#include <drizzled/server_includes.h>
27
 
#include <drizzled/error.h>
 
27
#include <drizzled/drizzled_error_messages.h>
28
28
 
29
29
#define FCOMP                   17              /* Bytes for a packed field */
30
30
 
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,
 
31
static uchar * pack_screens(List<Create_field> &create_fields,
 
32
                            uint *info_length, uint *screens, bool small_file);
 
33
static uint pack_keys(uchar *keybuff,uint key_count, KEY *key_info,
34
34
                      ulong data_offset);
35
 
static bool pack_header(unsigned char *forminfo,
 
35
static bool pack_header(uchar *forminfo,enum legacy_db_type table_type,
36
36
                        List<Create_field> &create_fields,
37
 
                        uint32_t info_length, uint32_t screens, uint32_t table_options,
 
37
                        uint info_length, uint screens, uint table_options,
38
38
                        ulong data_offset, handler *file);
39
 
static uint32_t get_interval_id(uint32_t *int_count,List<Create_field> &create_fields,
 
39
static uint get_interval_id(uint *int_count,List<Create_field> &create_fields,
40
40
                            Create_field *last_field);
41
41
static bool pack_fields(File file, List<Create_field> &create_fields,
42
42
                        ulong data_offset);
43
 
static bool make_empty_rec(Session *session, int file, enum legacy_db_type table_type,
44
 
                           uint32_t table_options,
 
43
static bool make_empty_rec(THD *thd, int file, enum legacy_db_type table_type,
 
44
                           uint table_options,
45
45
                           List<Create_field> &create_fields,
46
 
                           uint32_t reclength, ulong data_offset,
 
46
                           uint reclength, ulong data_offset,
47
47
                           handler *handler);
48
48
 
49
49
/**
55
55
 
56
56
struct Pack_header_error_handler: public Internal_error_handler
57
57
{
58
 
  virtual bool handle_error(uint32_t sql_errno,
 
58
  virtual bool handle_error(uint sql_errno,
59
59
                            const char *message,
60
60
                            DRIZZLE_ERROR::enum_warning_level level,
61
 
                            Session *session);
 
61
                            THD *thd);
62
62
  bool is_handled;
63
63
  Pack_header_error_handler() :is_handled(false) {}
64
64
};
66
66
 
67
67
bool
68
68
Pack_header_error_handler::
69
 
handle_error(uint32_t sql_errno,
 
69
handle_error(uint sql_errno,
70
70
             const char * /* message */,
71
71
             DRIZZLE_ERROR::enum_warning_level /* level */,
72
 
             Session * /* session */)
 
72
             THD * /* thd */)
73
73
{
74
74
  is_handled= (sql_errno == ER_TOO_MANY_FIELDS);
75
75
  return is_handled;
80
80
 
81
81
  SYNOPSIS
82
82
    mysql_create_frm()
83
 
    session                     Thread handler
 
83
    thd                 Thread handler
84
84
    file_name           Path for file (including database and .frm)
85
85
    db                  Name of database
86
86
    table               Name of table
95
95
    1  error
96
96
*/
97
97
 
98
 
bool mysql_create_frm(Session *session, const char *file_name,
 
98
bool mysql_create_frm(THD *thd, const char *file_name,
99
99
                      const char *db, const char *table,
100
100
                      HA_CREATE_INFO *create_info,
101
101
                      List<Create_field> &create_fields,
102
 
                      uint32_t keys, KEY *key_info,
 
102
                      uint keys, KEY *key_info,
103
103
                      handler *db_file)
104
104
{
105
105
  LEX_STRING str_db_type;
106
 
  uint32_t reclength, info_length, screens, key_info_length, maxlength, tmp_len;
 
106
  uint reclength, info_length, screens, key_info_length, maxlength, tmp_len;
107
107
  ulong key_buff_length;
108
108
  File file;
109
109
  ulong filepos, data_offset;
110
 
  unsigned char fileinfo[64],forminfo[288],*keybuff;
 
110
  uchar fileinfo[64],forminfo[288],*keybuff;
111
111
  TYPELIB formnames;
112
 
  unsigned char *screen_buff;
 
112
  uchar *screen_buff;
113
113
  char buff[128];
114
 
  const uint32_t format_section_header_size= 8;
115
 
  uint32_t format_section_len;
 
114
  const uint format_section_header_size= 8;
 
115
  uint format_section_len;
116
116
  Pack_header_error_handler pack_header_error_handler;
117
117
  int error;
118
118
 
127
127
    create_info->null_bits++;
128
128
  data_offset= (create_info->null_bits + 7) / 8;
129
129
 
130
 
  session->push_internal_handler(&pack_header_error_handler);
 
130
  thd->push_internal_handler(&pack_header_error_handler);
131
131
 
132
 
  error= pack_header(forminfo,
 
132
  error= pack_header(forminfo, ha_legacy_type(create_info->db_type),
133
133
                     create_fields,info_length,
134
134
                     screens, create_info->table_options,
135
135
                     data_offset, db_file);
136
136
 
137
 
  session->pop_internal_handler();
 
137
  thd->pop_internal_handler();
138
138
 
139
139
  if (error)
140
140
  {
141
 
    free(screen_buff);
 
141
    my_free(screen_buff, MYF(0));
142
142
    if (! pack_header_error_handler.is_handled)
143
143
      return(1);
144
144
 
145
145
    // Try again without UNIREG screens (to get more columns)
146
146
    if (!(screen_buff=pack_screens(create_fields,&info_length,&screens,1)))
147
147
      return(1);
148
 
    if (pack_header(forminfo,
 
148
    if (pack_header(forminfo, ha_legacy_type(create_info->db_type),
149
149
                    create_fields,info_length,
150
150
                    screens, create_info->table_options, data_offset, db_file))
151
151
    {
152
 
      free(screen_buff);
 
152
      my_free(screen_buff, MYF(0));
153
153
      return(1);
154
154
    }
155
155
  }
186
186
    my_error(ER_WRONG_STRING_LENGTH, MYF(0),
187
187
             create_info->comment.str,"Table COMMENT",
188
188
             (uint) TABLE_COMMENT_MAXLEN);
189
 
    free(screen_buff);
 
189
    my_free(screen_buff,MYF(0));
190
190
    return(1);
191
191
  }
192
192
 
199
199
  else{
200
200
    strmake((char*) forminfo+47, create_info->comment.str ?
201
201
            create_info->comment.str : "", create_info->comment.length);
202
 
    forminfo[46]=(unsigned char) create_info->comment.length;
 
202
    forminfo[46]=(uchar) create_info->comment.length;
203
203
#ifdef EXTRA_DEBUG
204
204
    /*
205
205
      EXTRA_DEBUG causes strmake() to initialize its buffer behind the
210
210
#endif
211
211
  }
212
212
 
213
 
  if ((file=create_frm(session, file_name, db, table, reclength, fileinfo,
 
213
  if ((file=create_frm(thd, file_name, db, table, reclength, fileinfo,
214
214
                       create_info, keys, key_info)) < 0)
215
215
  {
216
 
    free(screen_buff);
 
216
    my_free(screen_buff, MYF(0));
217
217
    return(1);
218
218
  }
219
219
 
220
220
  key_buff_length= uint4korr(fileinfo+47);
221
 
  keybuff=(unsigned char*) my_malloc(key_buff_length, MYF(0));
 
221
  keybuff=(uchar*) my_malloc(key_buff_length, MYF(0));
222
222
  key_info_length= pack_keys(keybuff, keys, key_info, data_offset);
223
 
  get_form_pos(file,fileinfo,&formnames);
 
223
  VOID(get_form_pos(file,fileinfo,&formnames));
224
224
  if (!(filepos=make_new_entry(file,fileinfo,&formnames,"")))
225
225
    goto err;
226
226
  maxlength=(uint) next_io_size((ulong) (uint2korr(forminfo)+1000));
227
227
  int2store(forminfo+2,maxlength);
228
228
  int4store(fileinfo+10,(ulong) (filepos+maxlength));
229
 
  fileinfo[26]= (unsigned char) test((create_info->max_rows == 1) &&
 
229
  fileinfo[26]= (uchar) test((create_info->max_rows == 1) &&
230
230
                             (create_info->min_rows == 1) && (keys == 0));
231
231
  int2store(fileinfo+28,key_info_length);
232
232
 
236
236
  if (pwrite(file, fileinfo, 64, 0L) == 0 ||
237
237
      pwrite(file, keybuff, key_info_length, (ulong) uint2korr(fileinfo+6)) == 0)
238
238
    goto err;
239
 
  my_seek(file,
 
239
  VOID(my_seek(file,
240
240
               (ulong) uint2korr(fileinfo+6)+ (ulong) key_buff_length,
241
 
               MY_SEEK_SET,MYF(0));
242
 
  if (make_empty_rec(session,file,ha_legacy_type(create_info->db_type),
 
241
               MY_SEEK_SET,MYF(0)));
 
242
  if (make_empty_rec(thd,file,ha_legacy_type(create_info->db_type),
243
243
                     create_info->table_options,
244
244
                     create_fields,reclength, data_offset, db_file))
245
245
    goto err;
246
246
 
247
247
  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,
 
248
  if (my_write(file, (const uchar*)buff, 2, MYF(MY_NABP)) ||
 
249
      my_write(file, (const uchar*)create_info->connect_string.str,
250
250
               create_info->connect_string.length, MYF(MY_NABP)))
251
251
      goto err;
252
252
 
253
253
  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,
 
254
  if (my_write(file, (const uchar*)buff, 2, MYF(MY_NABP)) ||
 
255
      my_write(file, (const uchar*)str_db_type.str,
256
256
               str_db_type.length, MYF(MY_NABP)))
257
257
    goto err;
258
258
 
259
259
  {
260
260
    memset(buff, 0, 6);
261
 
    if (my_write(file, (unsigned char*) buff, 6, MYF_RW))
 
261
    if (my_write(file, (uchar*) buff, 6, MYF_RW))
262
262
      goto err;
263
263
  }
264
264
 
265
 
  if (forminfo[46] == (unsigned char)255)
 
265
  if (forminfo[46] == (uchar)255)
266
266
  {
267
 
    unsigned char comment_length_buff[2];
 
267
    uchar comment_length_buff[2];
268
268
    int2store(comment_length_buff,create_info->comment.length);
269
269
    if (my_write(file, comment_length_buff, 2, MYF(MY_NABP)) ||
270
 
        my_write(file, (unsigned char*)create_info->comment.str,
 
270
        my_write(file, (uchar*)create_info->comment.str,
271
271
                  create_info->comment.length, MYF(MY_NABP)))
272
272
      goto err;
273
273
  }
276
276
  {
277
277
    /* prepare header */
278
278
    {
279
 
      uint32_t flags= 0;
 
279
      uint flags= 0;
280
280
 
281
281
      memset(buff, 0, format_section_header_size);
282
282
      /* length of section 2 bytes*/
286
286
      /* 2 bytes left for future use */
287
287
    }
288
288
    /* write header */
289
 
    if (my_write(file, (const unsigned char*)buff, format_section_header_size, MYF_RW))
 
289
    if (my_write(file, (const uchar*)buff, format_section_header_size, MYF_RW))
290
290
      goto err;
291
291
    buff[0]= 0;
292
 
    if (my_write(file, (const unsigned char*)buff, 1, MYF_RW))
 
292
    if (my_write(file, (const uchar*)buff, 1, MYF_RW))
293
293
      goto err;
294
294
    /* write column info, 1 byte per column */
295
295
    {
296
296
      List_iterator<Create_field> it(create_fields);
297
297
      Create_field *field;
298
 
      unsigned char column_format, write_byte;
 
298
      uchar column_format, write_byte;
299
299
      while ((field=it++))
300
300
      {
301
 
        column_format= (unsigned char)field->column_format();
 
301
        column_format= (uchar)field->column_format();
302
302
        write_byte= (column_format << COLUMN_FORMAT_SHIFT);
303
303
        if (my_write(file, &write_byte, 1, MYF_RW))
304
304
          goto err;
305
305
      }
306
306
    }
307
307
  }
308
 
  my_seek(file,filepos,MY_SEEK_SET,MYF(0));
 
308
  VOID(my_seek(file,filepos,MY_SEEK_SET,MYF(0)));
309
309
  if (my_write(file, forminfo, 288, MYF_RW) ||
310
310
      my_write(file, screen_buff, info_length, MYF_RW) ||
311
311
      pack_fields(file, create_fields, data_offset))
312
312
    goto err;
313
313
 
314
 
  free(screen_buff);
315
 
  free(keybuff);
 
314
  my_free(screen_buff,MYF(0));
 
315
  my_free(keybuff, MYF(0));
316
316
 
317
317
  if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
318
318
      (my_sync(file, MYF(MY_WME)) ||
341
341
  return(0);
342
342
 
343
343
err:
344
 
  free(screen_buff);
345
 
  free(keybuff);
 
344
  my_free(screen_buff, MYF(0));
 
345
  my_free(keybuff, MYF(0));
346
346
err2:
347
 
  my_close(file,MYF(MY_WME));
 
347
  VOID(my_close(file,MYF(MY_WME)));
348
348
err3:
349
349
  my_delete(file_name,MYF(0));
350
350
  return(1);
356
356
 
357
357
  SYNOPSIS
358
358
    rea_create_table()
359
 
    session                     Thread handler
 
359
    thd                 Thread handler
360
360
    path                Name of file (including database, without .frm)
361
361
    db                  Data base name
362
362
    table_name          Table name
371
371
    1  error
372
372
*/
373
373
 
374
 
int rea_create_table(Session *session, const char *path,
 
374
int rea_create_table(THD *thd, const char *path,
375
375
                     const char *db, const char *table_name,
376
376
                     HA_CREATE_INFO *create_info,
377
377
                     List<Create_field> &create_fields,
378
 
                     uint32_t keys, KEY *key_info, handler *file)
 
378
                     uint keys, KEY *key_info, handler *file)
379
379
{
380
380
  
381
381
 
382
382
  char frm_name[FN_REFLEN];
383
 
  strxmov(frm_name, path, reg_ext, NULL);
384
 
  if (mysql_create_frm(session, frm_name, db, table_name, create_info,
 
383
  strxmov(frm_name, path, reg_ext, NullS);
 
384
  if (mysql_create_frm(thd, frm_name, db, table_name, create_info,
385
385
                       create_fields, keys, key_info, file))
386
386
 
387
387
    return(1);
388
388
 
389
389
  // Make sure mysql_create_frm din't remove extension
390
390
  assert(*fn_rext(frm_name));
391
 
  if (session->variables.keep_files_on_create)
 
391
  if (thd->variables.keep_files_on_create)
392
392
    create_info->options|= HA_CREATE_KEEP_FILES;
393
393
  if (file->ha_create_handler_files(path, NULL, CHF_CREATE_FLAG, create_info))
394
394
    goto err_handler;
395
 
  if (!create_info->frm_only && ha_create_table(session, path, db, table_name,
 
395
  if (!create_info->frm_only && ha_create_table(thd, path, db, table_name,
396
396
                                                create_info,0))
397
397
    goto err_handler;
398
398
  return(0);
399
399
 
400
400
err_handler:
401
 
  file->ha_create_handler_files(path, NULL, CHF_DELETE_FLAG, create_info);
 
401
  VOID(file->ha_create_handler_files(path, NULL, CHF_DELETE_FLAG, create_info));
402
402
  my_delete(frm_name, MYF(0));
403
403
  return(1);
404
404
} /* rea_create_table */
406
406
 
407
407
        /* Pack screens to a screen for save in a form-file */
408
408
 
409
 
static unsigned char *pack_screens(List<Create_field> &create_fields,
410
 
                           uint32_t *info_length, uint32_t *screens,
 
409
static uchar *pack_screens(List<Create_field> &create_fields,
 
410
                           uint *info_length, uint *screens,
411
411
                           bool small_file)
412
412
{
413
 
  register uint32_t i;
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;
 
413
  register uint i;
 
414
  uint row,start_row,end_row,fields_on_screen;
 
415
  uint length,cols;
 
416
  uchar *info,*pos,*start_screen;
 
417
  uint fields=create_fields.elements;
418
418
  List_iterator<Create_field> it(create_fields);
419
419
  
420
420
 
427
427
  while ((field=it++))
428
428
    length+=(uint) strlen(field->field_name)+1+TE_INFO_LENGTH+cols/2;
429
429
 
430
 
  if (!(info=(unsigned char*) my_malloc(length,MYF(MY_WME))))
 
430
  if (!(info=(uchar*) my_malloc(length,MYF(MY_WME))))
431
431
    return(0);
432
432
 
433
433
  start_screen=0;
443
443
      {
444
444
        length=(uint) (pos-start_screen);
445
445
        int2store(start_screen,length);
446
 
        start_screen[2]=(unsigned char) (fields_on_screen+1);
447
 
        start_screen[3]=(unsigned char) (fields_on_screen);
 
446
        start_screen[2]=(uchar) (fields_on_screen+1);
 
447
        start_screen[3]=(uchar) (fields_on_screen);
448
448
      }
449
449
      row=start_row;
450
450
      start_screen=pos;
451
451
      pos+=4;
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;
 
452
      pos[0]= (uchar) start_row-2;      /* Header string */
 
453
      pos[1]= (uchar) (cols >> 2);
 
454
      pos[2]= (uchar) (cols >> 1) +1;
455
455
      strfill((char *) pos+3,(uint) (cols >> 1),' ');
456
456
      pos+=(cols >> 1)+4;
457
457
    }
461
461
 
462
462
    if (!small_file)
463
463
    {
464
 
      pos[0]=(unsigned char) row;
 
464
      pos[0]=(uchar) row;
465
465
      pos[1]=0;
466
 
      pos[2]=(unsigned char) (length+1);
467
 
      pos=(unsigned char*) strmake((char*) pos+3,cfield->field_name,length)+1;
 
466
      pos[2]=(uchar) (length+1);
 
467
      pos=(uchar*) strmake((char*) pos+3,cfield->field_name,length)+1;
468
468
    }
469
469
    cfield->row=(uint8_t) row;
470
470
    cfield->col=(uint8_t) (length+1);
471
 
    cfield->sc_length=(uint8_t) cmin(cfield->length,(uint32_t)cols-(length+2));
 
471
    cfield->sc_length=(uint8_t) min(cfield->length,(uint32_t)cols-(length+2));
472
472
  }
473
473
  length=(uint) (pos-start_screen);
474
474
  int2store(start_screen,length);
475
 
  start_screen[2]=(unsigned char) (row-start_row+2);
476
 
  start_screen[3]=(unsigned char) (row-start_row+1);
 
475
  start_screen[2]=(uchar) (row-start_row+2);
 
476
  start_screen[3]=(uchar) (row-start_row+1);
477
477
 
478
478
  *info_length=(uint) (pos-info);
479
479
  return(info);
482
482
 
483
483
        /* Pack keyinfo and keynames to keybuff for save in form-file. */
484
484
 
485
 
static uint32_t pack_keys(unsigned char *keybuff, uint32_t key_count, KEY *keyinfo,
 
485
static uint pack_keys(uchar *keybuff, uint key_count, KEY *keyinfo,
486
486
                      ulong data_offset)
487
487
{
488
 
  uint32_t key_parts,length;
489
 
  unsigned char *pos, *keyname_pos;
 
488
  uint key_parts,length;
 
489
  uchar *pos, *keyname_pos;
490
490
  KEY *key,*end;
491
491
  KEY_PART_INFO *key_part,*key_part_end;
492
492
  
497
497
  {
498
498
    int2store(pos, (key->flags ^ HA_NOSAME));
499
499
    int2store(pos+2,key->key_length);
500
 
    pos[4]= (unsigned char) key->key_parts;
501
 
    pos[5]= (unsigned char) key->algorithm;
 
500
    pos[4]= (uchar) key->key_parts;
 
501
    pos[5]= (uchar) key->algorithm;
502
502
    int2store(pos+6, key->block_size);
503
503
    pos+=8;
504
504
    key_parts+=key->key_parts;
507
507
         key_part++)
508
508
 
509
509
    {
510
 
      uint32_t offset;
 
510
      uint offset;
511
511
      int2store(pos,key_part->fieldnr+1+FIELD_NAME_USED);
512
512
      offset= (uint) (key_part->offset+data_offset+1);
513
513
      int2store(pos+2, offset);
519
519
  }
520
520
        /* Save keynames */
521
521
  keyname_pos=pos;
522
 
  *pos++=(unsigned char) NAMES_SEP_CHAR;
 
522
  *pos++=(uchar) NAMES_SEP_CHAR;
523
523
  for (key=keyinfo ; key != end ; key++)
524
524
  {
525
 
    unsigned char *tmp=(unsigned char*) my_stpcpy((char*) pos,key->name);
526
 
    *tmp++= (unsigned char) NAMES_SEP_CHAR;
 
525
    uchar *tmp=(uchar*) stpcpy((char*) pos,key->name);
 
526
    *tmp++= (uchar) NAMES_SEP_CHAR;
527
527
    *tmp=0;
528
528
    pos=tmp;
529
529
  }
534
534
    if (key->flags & HA_USES_COMMENT)
535
535
    {
536
536
      int2store(pos, key->comment.length);
537
 
      unsigned char *tmp= (unsigned char*)my_stpncpy((char*) pos+2,key->comment.str,key->comment.length);
 
537
      uchar *tmp= (uchar*)stpncpy((char*) pos+2,key->comment.str,key->comment.length);
538
538
      pos= tmp;
539
539
    }
540
540
  }
547
547
  }
548
548
  else
549
549
  {
550
 
    keybuff[0]=(unsigned char) key_count;
551
 
    keybuff[1]=(unsigned char) key_parts;
 
550
    keybuff[0]=(uchar) key_count;
 
551
    keybuff[1]=(uchar) key_parts;
552
552
    keybuff[2]= keybuff[3]= 0;
553
553
  }
554
554
  length=(uint) (pos-keyname_pos);
559
559
 
560
560
/* Make formheader */
561
561
 
562
 
static bool pack_header(unsigned char *forminfo,
 
562
static bool pack_header(uchar *forminfo,
 
563
                        enum legacy_db_type table_type __attribute__((unused)),
563
564
                        List<Create_field> &create_fields,
564
 
                        uint32_t info_length, uint32_t screens, uint32_t table_options,
 
565
                        uint info_length, uint screens, uint table_options,
565
566
                        ulong data_offset, handler *file)
566
567
{
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;
 
568
  uint length,int_count,int_length,no_empty, int_parts;
 
569
  uint time_stamp_pos,null_fields;
 
570
  ulong reclength, totlength, n_length, com_length;
570
571
 
571
572
 
572
573
  if (create_fields.elements > MAX_FIELDS)
578
579
  totlength= 0L;
579
580
  reclength= data_offset;
580
581
  no_empty=int_count=int_parts=int_length=time_stamp_pos=null_fields=
581
 
    com_length=vcol_info_length=0;
 
582
    com_length=0;
582
583
  n_length=2L;
583
584
 
584
585
        /* Check fields */
587
588
  Create_field *field;
588
589
  while ((field=it++))
589
590
  {
590
 
    uint32_t tmp_len= system_charset_info->cset->charpos(system_charset_info,
 
591
    uint tmp_len= system_charset_info->cset->charpos(system_charset_info,
591
592
                                                     field->comment.str,
592
593
                                                     field->comment.str +
593
594
                                                     field->comment.length,
600
601
               (uint) COLUMN_COMMENT_MAXLEN);
601
602
      return(1);
602
603
    }
603
 
    if (field->vcol_info)
604
 
    {
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);
610
 
 
611
 
      if (tmp_len < field->vcol_info->expr_str.length)
612
 
      {
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);
616
 
        return(1);
617
 
      }
618
 
      /*
619
 
        Sum up the length of the expression string and mandatory header bytes
620
 
        to the total length.
621
 
      */
622
 
      vcol_info_length+= field->vcol_info->expr_str.length+(uint)FRM_VCOL_HEADER_SIZE;
623
 
    }
624
604
 
625
605
    totlength+= field->length;
626
606
    com_length+= field->comment.length;
640
620
        !time_stamp_pos)
641
621
      time_stamp_pos= (uint) field->offset+ (uint) data_offset + 1;
642
622
    length=field->pack_length;
 
623
    /* Ensure we don't have any bugs when generating offsets */
 
624
    assert(reclength == field->offset + data_offset);
643
625
    if ((uint) field->offset+ (uint) data_offset+ length > reclength)
644
626
      reclength=(uint) (field->offset+ data_offset + length);
645
627
    n_length+= (ulong) strlen(field->field_name)+1;
647
629
    field->save_interval= 0;
648
630
    if (field->interval)
649
631
    {
650
 
      uint32_t old_int_count=int_count;
 
632
      uint old_int_count=int_count;
651
633
 
652
634
      if (field->charset->mbminlen > 1)
653
635
      {
667
649
                                    (field->interval->count+1));
668
650
        field->interval->type_names[field->interval->count]= 0;
669
651
        field->interval->type_lengths=
670
 
          (uint32_t *) sql_alloc(sizeof(uint) * field->interval->count);
 
652
          (uint *) sql_alloc(sizeof(uint) * field->interval->count);
671
653
 
672
 
        for (uint32_t pos= 0; pos < field->interval->count; pos++)
 
654
        for (uint pos= 0; pos < field->interval->count; pos++)
673
655
        {
674
656
          char *dst;
675
657
          const char *src= field->save_interval->type_names[pos];
676
 
          uint32_t hex_length;
 
658
          uint hex_length;
677
659
          length= field->save_interval->type_lengths[pos];
678
660
          hex_length= length * 2;
679
661
          field->interval->type_lengths[pos]= hex_length;
704
686
    return(1);
705
687
  }
706
688
  /* Hack to avoid bugs with small static rows in MySQL */
707
 
  reclength=cmax((ulong)file->min_record_length(table_options),reclength);
 
689
  reclength=max((ulong)file->min_record_length(table_options),reclength);
708
690
  if (info_length+(ulong) create_fields.elements*FCOMP+288+
709
 
      n_length+int_length+com_length+vcol_info_length > 65535L || 
710
 
      int_count > 255)
 
691
      n_length+int_length+com_length > 65535L || int_count > 255)
711
692
  {
712
693
    my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0));
713
694
    return(1);
715
696
 
716
697
  memset(forminfo, 0, 288);
717
698
  length=(info_length+create_fields.elements*FCOMP+288+n_length+int_length+
718
 
          com_length+vcol_info_length);
 
699
          com_length);
719
700
  int2store(forminfo,length);
720
701
  forminfo[256] = (uint8_t) screens;
721
702
  int2store(forminfo+258,create_fields.elements);
732
713
  int2store(forminfo+280,22);                   /* Rows needed */
733
714
  int2store(forminfo+282,null_fields);
734
715
  int2store(forminfo+284,com_length);
735
 
  int2store(forminfo+286,vcol_info_length);
736
 
  /* forminfo+288 is free to use for additional information */
 
716
  /* Up to forminfo+288 is free to use for additional information */
737
717
  return(0);
738
718
} /* pack_header */
739
719
 
740
720
 
741
721
        /* get each unique interval each own id */
742
722
 
743
 
static uint32_t get_interval_id(uint32_t *int_count,List<Create_field> &create_fields,
 
723
static uint get_interval_id(uint *int_count,List<Create_field> &create_fields,
744
724
                            Create_field *last_field)
745
725
{
746
726
  List_iterator<Create_field> it(create_fields);
771
751
static bool pack_fields(File file, List<Create_field> &create_fields,
772
752
                        ulong data_offset)
773
753
{
774
 
  register uint32_t i;
775
 
  uint32_t int_count, comment_length=0, vcol_info_length=0;
776
 
  unsigned char buff[MAX_FIELD_WIDTH];
 
754
  register uint i;
 
755
  uint int_count, comment_length=0;
 
756
  uchar buff[MAX_FIELD_WIDTH];
777
757
  Create_field *field;
778
758
  
779
759
 
784
764
  int_count=0;
785
765
  while ((field=it++))
786
766
  {
787
 
    uint32_t recpos;
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;
 
767
    uint recpos;
 
768
    buff[0]= (uchar) field->row;
 
769
    buff[1]= (uchar) field->col;
 
770
    buff[2]= (uchar) field->sc_length;
792
771
    int2store(buff+3, field->length);
793
772
    /* The +1 is here becasue the col offset in .frm file have offset 1 */
794
773
    recpos= field->offset+1 + (uint) data_offset;
795
774
    int3store(buff+5,recpos);
796
775
    int2store(buff+8,field->pack_flag);
797
776
    int2store(buff+10,field->unireg_check);
798
 
    buff[12]= (unsigned char) field->interval_id;
799
 
    buff[13]= (unsigned char) field->sql_type; 
 
777
    buff[12]= (uchar) field->interval_id;
 
778
    buff[13]= (uchar) field->sql_type; 
800
779
    if (field->charset) 
801
 
      buff[14]= (unsigned char) field->charset->number;
 
780
      buff[14]= (uchar) field->charset->number;
802
781
    else
803
782
      buff[14]= 0;                              // Numerical
804
 
    if (field->vcol_info)
805
 
    {
806
 
      /* 
807
 
        Use the interval_id place in the .frm file to store the length of
808
 
        virtual field's data.
809
 
      */
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;
814
 
    }
815
783
    int2store(buff+15, field->comment.length);
816
784
    comment_length+= field->comment.length;
817
785
    set_if_bigger(int_count,field->interval_id);
820
788
  }
821
789
 
822
790
        /* Write fieldnames */
823
 
  buff[0]=(unsigned char) NAMES_SEP_CHAR;
 
791
  buff[0]=(uchar) NAMES_SEP_CHAR;
824
792
  if (my_write(file, buff, 1, MYF_RW))
825
793
    return(1);
826
794
  i=0;
827
795
  it.rewind();
828
796
  while ((field=it++))
829
797
  {
830
 
    char *pos= my_stpcpy((char*) buff,field->field_name);
 
798
    char *pos= stpcpy((char*) buff,field->field_name);
831
799
    *pos++=NAMES_SEP_CHAR;
832
800
    if (i == create_fields.elements-1)
833
801
      *pos++=0;
849
817
      {
850
818
        unsigned char  sep= 0;
851
819
        unsigned char  occ[256];
852
 
        uint32_t           i;
 
820
        uint           i;
853
821
        unsigned char *val= NULL;
854
822
 
855
823
        memset(occ, 0, sizeof(occ));
856
824
 
857
825
        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++)
 
826
          for (uint j = 0; j < field->interval->type_lengths[i]; j++)
859
827
            occ[(unsigned int) (val[j])]= 1;
860
828
 
861
829
        if (!occ[(unsigned char)NAMES_SEP_CHAR])
864
832
          sep= ',';
865
833
        else
866
834
        {
867
 
          for (uint32_t i=1; i<256; i++)
 
835
          for (uint i=1; i<256; i++)
868
836
          {
869
837
            if(!occ[i])
870
838
            {
891
859
        tmp.append('\0');                      // End of intervall
892
860
      }
893
861
    }
894
 
    if (my_write(file,(unsigned char*) tmp.ptr(),tmp.length(),MYF_RW))
 
862
    if (my_write(file,(uchar*) tmp.ptr(),tmp.length(),MYF_RW))
895
863
      return(1);
896
864
  }
897
865
  if (comment_length)
901
869
    while ((field=it++))
902
870
    {
903
871
      if (field->comment.length)
904
 
        if (my_write(file, (unsigned char*) field->comment.str, field->comment.length,
 
872
        if (my_write(file, (uchar*) field->comment.str, field->comment.length,
905
873
                     MYF_RW))
906
874
          return(1);
907
875
    }
908
876
  }
909
 
  if (vcol_info_length)
910
 
  {
911
 
    it.rewind();
912
 
    int_count=0;
913
 
    while ((field=it++))
914
 
    {
915
 
      /*
916
 
        Pack each virtual field as follows:
917
 
        byte 1      = 1 (always 1 to allow for future extensions)
918
 
        byte 2      = sql_type
919
 
        byte 3      = flags (as of now, 0 - no flags, 1 - field is physically stored)
920
 
        byte 4-...  = virtual column expression (text data)
921
 
      */
922
 
      if (field->vcol_info && field->vcol_info->expr_str.length)
923
 
      {
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))
928
 
          return(1);
929
 
        if (my_write(file, 
930
 
                     (unsigned char*) field->vcol_info->expr_str.str, 
931
 
                     field->vcol_info->expr_str.length,
932
 
                     MYF_RW))
933
 
          return(1);
934
 
      }
935
 
    }
936
 
  }
937
877
  return(0);
938
878
}
939
879
 
940
880
 
941
881
/* save an empty record on start of formfile */
942
882
 
943
 
static bool make_empty_rec(Session *session, File file,
 
883
static bool make_empty_rec(THD *thd, File file,
944
884
                           enum legacy_db_type table_type __attribute__((unused)),
945
 
                           uint32_t table_options,
 
885
                           uint table_options,
946
886
                           List<Create_field> &create_fields,
947
 
                           uint32_t reclength,
 
887
                           uint reclength,
948
888
                           ulong data_offset,
949
889
                           handler *handler)
950
890
{
951
891
  int error= 0;
952
892
  Field::utype type;
953
 
  uint32_t null_count;
954
 
  unsigned char *buff,*null_pos;
 
893
  uint null_count;
 
894
  uchar *buff,*null_pos;
955
895
  Table table;
956
896
  TABLE_SHARE share;
957
897
  Create_field *field;
958
 
  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
 
898
  enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
959
899
  
960
900
 
961
901
  /* We need a table to generate columns for default values */
963
903
  memset(&share, 0, sizeof(share));
964
904
  table.s= &share;
965
905
 
966
 
  if (!(buff=(unsigned char*) my_malloc((size_t) reclength,MYF(MY_WME | MY_ZEROFILL))))
 
906
  if (!(buff=(uchar*) my_malloc((size_t) reclength,MYF(MY_WME | MY_ZEROFILL))))
967
907
  {
968
908
    return(1);
969
909
  }
970
910
 
971
 
  table.in_use= session;
 
911
  table.in_use= thd;
972
912
  table.s->db_low_byte_first= handler->low_byte_first();
973
913
  table.s->blob_ptr_size= portable_sizeof_char_ptr;
974
914
 
981
921
  null_pos= buff;
982
922
 
983
923
  List_iterator<Create_field> it(create_fields);
984
 
  session->count_cuted_fields= CHECK_FIELD_WARN;    // To find wrong default values
 
924
  thd->count_cuted_fields= CHECK_FIELD_WARN;    // To find wrong default values
985
925
  while ((field=it++))
986
926
  {
987
927
    /*
1048
988
    of 8 there are no unused bits.
1049
989
  */
1050
990
  if (null_count & 7)
1051
 
    *(null_pos + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
 
991
    *(null_pos + null_count / 8)|= ~(((uchar) 1 << (null_count & 7)) - 1);
1052
992
 
1053
993
  error= my_write(file, buff, (size_t) reclength,MYF_RW) != 0;
1054
994
 
1055
995
err:
1056
 
  free(buff);
1057
 
  session->count_cuted_fields= old_count_cuted_fields;
 
996
  my_free(buff, MYF(MY_FAE));
 
997
  thd->count_cuted_fields= old_count_cuted_fields;
1058
998
  return(error);
1059
999
} /* make_empty_rec */