~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/unireg.cc

  • Committer: Brian Aker
  • Date: 2008-11-04 15:39:09 UTC
  • mfrom: (575.1.2 devel)
  • Revision ID: brian@tangent.org-20081104153909-c72hn65udxs1ccal
Merge of Monty's work

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/drizzled_error_messages.h>
 
27
#include <drizzled/error.h>
28
28
 
29
29
#define FCOMP                   17              /* Bytes for a packed field */
30
30
 
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,
 
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,
34
34
                      ulong data_offset);
35
 
static bool pack_header(uchar *forminfo,enum legacy_db_type table_type,
 
35
static bool pack_header(unsigned char *forminfo,
36
36
                        List<Create_field> &create_fields,
37
 
                        uint info_length, uint screens, uint table_options,
 
37
                        uint32_t info_length, uint32_t screens, uint32_t table_options,
38
38
                        ulong data_offset, handler *file);
39
 
static uint get_interval_id(uint *int_count,List<Create_field> &create_fields,
 
39
static uint32_t get_interval_id(uint32_t *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(THD *thd, int file, enum legacy_db_type table_type,
44
 
                           uint table_options,
 
43
static bool make_empty_rec(Session *session, int file, enum legacy_db_type table_type,
 
44
                           uint32_t table_options,
45
45
                           List<Create_field> &create_fields,
46
 
                           uint reclength, ulong data_offset,
 
46
                           uint32_t 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(uint sql_errno,
 
58
  virtual bool handle_error(uint32_t sql_errno,
59
59
                            const char *message,
60
60
                            DRIZZLE_ERROR::enum_warning_level level,
61
 
                            THD *thd);
 
61
                            Session *session);
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(uint sql_errno,
 
69
handle_error(uint32_t sql_errno,
70
70
             const char * /* message */,
71
71
             DRIZZLE_ERROR::enum_warning_level /* level */,
72
 
             THD * /* thd */)
 
72
             Session * /* session */)
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
 
    thd                 Thread handler
 
83
    session                     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(THD *thd, const char *file_name,
 
98
bool mysql_create_frm(Session *session, 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
 
                      uint keys, KEY *key_info,
 
102
                      uint32_t keys, KEY *key_info,
103
103
                      handler *db_file)
104
104
{
105
105
  LEX_STRING str_db_type;
106
 
  uint reclength, info_length, screens, key_info_length, maxlength, tmp_len;
 
106
  uint32_t 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
 
  uchar fileinfo[64],forminfo[288],*keybuff;
 
110
  unsigned char fileinfo[64],forminfo[288],*keybuff;
111
111
  TYPELIB formnames;
112
 
  uchar *screen_buff;
 
112
  unsigned char *screen_buff;
113
113
  char buff[128];
114
 
  const uint format_section_header_size= 8;
115
 
  uint format_section_len;
 
114
  const uint32_t format_section_header_size= 8;
 
115
  uint32_t 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
 
  thd->push_internal_handler(&pack_header_error_handler);
 
130
  session->push_internal_handler(&pack_header_error_handler);
131
131
 
132
 
  error= pack_header(forminfo, ha_legacy_type(create_info->db_type),
 
132
  error= pack_header(forminfo,
133
133
                     create_fields,info_length,
134
134
                     screens, create_info->table_options,
135
135
                     data_offset, db_file);
136
136
 
137
 
  thd->pop_internal_handler();
 
137
  session->pop_internal_handler();
138
138
 
139
139
  if (error)
140
140
  {
141
 
    my_free(screen_buff, MYF(0));
 
141
    free(screen_buff);
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, ha_legacy_type(create_info->db_type),
 
148
    if (pack_header(forminfo,
149
149
                    create_fields,info_length,
150
150
                    screens, create_info->table_options, data_offset, db_file))
151
151
    {
152
 
      my_free(screen_buff, MYF(0));
 
152
      free(screen_buff);
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
 
    my_free(screen_buff,MYF(0));
 
189
    free(screen_buff);
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]=(uchar) create_info->comment.length;
 
202
    forminfo[46]=(unsigned char) 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(thd, file_name, db, table, reclength, fileinfo,
 
213
  if ((file=create_frm(session, file_name, db, table, reclength, fileinfo,
214
214
                       create_info, keys, key_info)) < 0)
215
215
  {
216
 
    my_free(screen_buff, MYF(0));
 
216
    free(screen_buff);
217
217
    return(1);
218
218
  }
219
219
 
220
220
  key_buff_length= uint4korr(fileinfo+47);
221
 
  keybuff=(uchar*) my_malloc(key_buff_length, MYF(0));
 
221
  keybuff=(unsigned char*) my_malloc(key_buff_length, MYF(0));
222
222
  key_info_length= pack_keys(keybuff, keys, key_info, data_offset);
223
 
  VOID(get_form_pos(file,fileinfo,&formnames));
 
223
  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]= (uchar) test((create_info->max_rows == 1) &&
 
229
  fileinfo[26]= (unsigned char) 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
 
  VOID(my_seek(file,
 
239
  my_seek(file,
240
240
               (ulong) uint2korr(fileinfo+6)+ (ulong) key_buff_length,
241
 
               MY_SEEK_SET,MYF(0)));
242
 
  if (make_empty_rec(thd,file,ha_legacy_type(create_info->db_type),
 
241
               MY_SEEK_SET,MYF(0));
 
242
  if (make_empty_rec(session,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 uchar*)buff, 2, MYF(MY_NABP)) ||
249
 
      my_write(file, (const uchar*)create_info->connect_string.str,
 
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,
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 uchar*)buff, 2, MYF(MY_NABP)) ||
255
 
      my_write(file, (const uchar*)str_db_type.str,
 
254
  if (my_write(file, (const unsigned char*)buff, 2, MYF(MY_NABP)) ||
 
255
      my_write(file, (const unsigned char*)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, (uchar*) buff, 6, MYF_RW))
 
261
    if (my_write(file, (unsigned char*) buff, 6, MYF_RW))
262
262
      goto err;
263
263
  }
264
264
 
265
 
  if (forminfo[46] == (uchar)255)
 
265
  if (forminfo[46] == (unsigned char)255)
266
266
  {
267
 
    uchar comment_length_buff[2];
 
267
    unsigned char 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, (uchar*)create_info->comment.str,
 
270
        my_write(file, (unsigned char*)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
 
      uint flags= 0;
 
279
      uint32_t 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 uchar*)buff, format_section_header_size, MYF_RW))
 
289
    if (my_write(file, (const unsigned char*)buff, format_section_header_size, MYF_RW))
290
290
      goto err;
291
291
    buff[0]= 0;
292
 
    if (my_write(file, (const uchar*)buff, 1, MYF_RW))
 
292
    if (my_write(file, (const unsigned char*)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
 
      uchar column_format, write_byte;
 
298
      unsigned char column_format, write_byte;
299
299
      while ((field=it++))
300
300
      {
301
 
        column_format= (uchar)field->column_format();
 
301
        column_format= (unsigned char)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
 
  VOID(my_seek(file,filepos,MY_SEEK_SET,MYF(0)));
 
308
  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
 
  my_free(screen_buff,MYF(0));
315
 
  my_free(keybuff, MYF(0));
 
314
  free(screen_buff);
 
315
  free(keybuff);
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
 
  my_free(screen_buff, MYF(0));
345
 
  my_free(keybuff, MYF(0));
 
344
  free(screen_buff);
 
345
  free(keybuff);
346
346
err2:
347
 
  VOID(my_close(file,MYF(MY_WME)));
 
347
  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
 
    thd                 Thread handler
 
359
    session                     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(THD *thd, const char *path,
 
374
int rea_create_table(Session *session, 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
 
                     uint keys, KEY *key_info, handler *file)
 
378
                     uint32_t 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, NullS);
384
 
  if (mysql_create_frm(thd, frm_name, db, table_name, create_info,
 
383
  strxmov(frm_name, path, reg_ext, NULL);
 
384
  if (mysql_create_frm(session, 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 (thd->variables.keep_files_on_create)
 
391
  if (session->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(thd, path, db, table_name,
 
395
  if (!create_info->frm_only && ha_create_table(session, 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
 
  VOID(file->ha_create_handler_files(path, NULL, CHF_DELETE_FLAG, create_info));
 
401
  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 uchar *pack_screens(List<Create_field> &create_fields,
410
 
                           uint *info_length, uint *screens,
 
409
static unsigned char *pack_screens(List<Create_field> &create_fields,
 
410
                           uint32_t *info_length, uint32_t *screens,
411
411
                           bool small_file)
412
412
{
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;
 
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;
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=(uchar*) my_malloc(length,MYF(MY_WME))))
 
430
  if (!(info=(unsigned char*) 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]=(uchar) (fields_on_screen+1);
447
 
        start_screen[3]=(uchar) (fields_on_screen);
 
446
        start_screen[2]=(unsigned char) (fields_on_screen+1);
 
447
        start_screen[3]=(unsigned char) (fields_on_screen);
448
448
      }
449
449
      row=start_row;
450
450
      start_screen=pos;
451
451
      pos+=4;
452
 
      pos[0]= (uchar) start_row-2;      /* Header string */
453
 
      pos[1]= (uchar) (cols >> 2);
454
 
      pos[2]= (uchar) (cols >> 1) +1;
 
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;
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]=(uchar) row;
 
464
      pos[0]=(unsigned char) row;
465
465
      pos[1]=0;
466
 
      pos[2]=(uchar) (length+1);
467
 
      pos=(uchar*) strmake((char*) pos+3,cfield->field_name,length)+1;
 
466
      pos[2]=(unsigned char) (length+1);
 
467
      pos=(unsigned char*) 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) min(cfield->length,(uint32_t)cols-(length+2));
 
471
    cfield->sc_length=(uint8_t) cmin(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]=(uchar) (row-start_row+2);
476
 
  start_screen[3]=(uchar) (row-start_row+1);
 
475
  start_screen[2]=(unsigned char) (row-start_row+2);
 
476
  start_screen[3]=(unsigned char) (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 uint pack_keys(uchar *keybuff, uint key_count, KEY *keyinfo,
 
485
static uint32_t pack_keys(unsigned char *keybuff, uint32_t key_count, KEY *keyinfo,
486
486
                      ulong data_offset)
487
487
{
488
 
  uint key_parts,length;
489
 
  uchar *pos, *keyname_pos;
 
488
  uint32_t key_parts,length;
 
489
  unsigned char *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]= (uchar) key->key_parts;
501
 
    pos[5]= (uchar) key->algorithm;
 
500
    pos[4]= (unsigned char) key->key_parts;
 
501
    pos[5]= (unsigned char) 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
 
      uint offset;
 
510
      uint32_t 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++=(uchar) NAMES_SEP_CHAR;
 
522
  *pos++=(unsigned char) NAMES_SEP_CHAR;
523
523
  for (key=keyinfo ; key != end ; key++)
524
524
  {
525
 
    uchar *tmp=(uchar*) stpcpy((char*) pos,key->name);
526
 
    *tmp++= (uchar) NAMES_SEP_CHAR;
 
525
    unsigned char *tmp=(unsigned char*) my_stpcpy((char*) pos,key->name);
 
526
    *tmp++= (unsigned char) 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
 
      uchar *tmp= (uchar*)stpncpy((char*) pos+2,key->comment.str,key->comment.length);
 
537
      unsigned char *tmp= (unsigned char*)my_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]=(uchar) key_count;
551
 
    keybuff[1]=(uchar) key_parts;
 
550
    keybuff[0]=(unsigned char) key_count;
 
551
    keybuff[1]=(unsigned char) 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(uchar *forminfo,
563
 
                        enum legacy_db_type table_type __attribute__((unused)),
 
562
static bool pack_header(unsigned char *forminfo,
564
563
                        List<Create_field> &create_fields,
565
 
                        uint info_length, uint screens, uint table_options,
 
564
                        uint32_t info_length, uint32_t screens, uint32_t table_options,
566
565
                        ulong data_offset, handler *file)
567
566
{
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;
 
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;
571
570
 
572
571
 
573
572
  if (create_fields.elements > MAX_FIELDS)
579
578
  totlength= 0L;
580
579
  reclength= data_offset;
581
580
  no_empty=int_count=int_parts=int_length=time_stamp_pos=null_fields=
582
 
    com_length=0;
 
581
    com_length=vcol_info_length=0;
583
582
  n_length=2L;
584
583
 
585
584
        /* Check fields */
588
587
  Create_field *field;
589
588
  while ((field=it++))
590
589
  {
591
 
    uint tmp_len= system_charset_info->cset->charpos(system_charset_info,
 
590
    uint32_t tmp_len= system_charset_info->cset->charpos(system_charset_info,
592
591
                                                     field->comment.str,
593
592
                                                     field->comment.str +
594
593
                                                     field->comment.length,
601
600
               (uint) COLUMN_COMMENT_MAXLEN);
602
601
      return(1);
603
602
    }
 
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
    }
604
624
 
605
625
    totlength+= field->length;
606
626
    com_length+= field->comment.length;
620
640
        !time_stamp_pos)
621
641
      time_stamp_pos= (uint) field->offset+ (uint) data_offset + 1;
622
642
    length=field->pack_length;
623
 
    /* Ensure we don't have any bugs when generating offsets */
624
 
    assert(reclength == field->offset + data_offset);
625
643
    if ((uint) field->offset+ (uint) data_offset+ length > reclength)
626
644
      reclength=(uint) (field->offset+ data_offset + length);
627
645
    n_length+= (ulong) strlen(field->field_name)+1;
629
647
    field->save_interval= 0;
630
648
    if (field->interval)
631
649
    {
632
 
      uint old_int_count=int_count;
 
650
      uint32_t old_int_count=int_count;
633
651
 
634
652
      if (field->charset->mbminlen > 1)
635
653
      {
649
667
                                    (field->interval->count+1));
650
668
        field->interval->type_names[field->interval->count]= 0;
651
669
        field->interval->type_lengths=
652
 
          (uint *) sql_alloc(sizeof(uint) * field->interval->count);
 
670
          (uint32_t *) sql_alloc(sizeof(uint) * field->interval->count);
653
671
 
654
 
        for (uint pos= 0; pos < field->interval->count; pos++)
 
672
        for (uint32_t pos= 0; pos < field->interval->count; pos++)
655
673
        {
656
674
          char *dst;
657
675
          const char *src= field->save_interval->type_names[pos];
658
 
          uint hex_length;
 
676
          uint32_t hex_length;
659
677
          length= field->save_interval->type_lengths[pos];
660
678
          hex_length= length * 2;
661
679
          field->interval->type_lengths[pos]= hex_length;
686
704
    return(1);
687
705
  }
688
706
  /* Hack to avoid bugs with small static rows in MySQL */
689
 
  reclength=max((ulong)file->min_record_length(table_options),reclength);
 
707
  reclength=cmax((ulong)file->min_record_length(table_options),reclength);
690
708
  if (info_length+(ulong) create_fields.elements*FCOMP+288+
691
 
      n_length+int_length+com_length > 65535L || int_count > 255)
 
709
      n_length+int_length+com_length+vcol_info_length > 65535L || 
 
710
      int_count > 255)
692
711
  {
693
712
    my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0));
694
713
    return(1);
696
715
 
697
716
  memset(forminfo, 0, 288);
698
717
  length=(info_length+create_fields.elements*FCOMP+288+n_length+int_length+
699
 
          com_length);
 
718
          com_length+vcol_info_length);
700
719
  int2store(forminfo,length);
701
720
  forminfo[256] = (uint8_t) screens;
702
721
  int2store(forminfo+258,create_fields.elements);
713
732
  int2store(forminfo+280,22);                   /* Rows needed */
714
733
  int2store(forminfo+282,null_fields);
715
734
  int2store(forminfo+284,com_length);
716
 
  /* Up to forminfo+288 is free to use for additional information */
 
735
  int2store(forminfo+286,vcol_info_length);
 
736
  /* forminfo+288 is free to use for additional information */
717
737
  return(0);
718
738
} /* pack_header */
719
739
 
720
740
 
721
741
        /* get each unique interval each own id */
722
742
 
723
 
static uint get_interval_id(uint *int_count,List<Create_field> &create_fields,
 
743
static uint32_t get_interval_id(uint32_t *int_count,List<Create_field> &create_fields,
724
744
                            Create_field *last_field)
725
745
{
726
746
  List_iterator<Create_field> it(create_fields);
751
771
static bool pack_fields(File file, List<Create_field> &create_fields,
752
772
                        ulong data_offset)
753
773
{
754
 
  register uint i;
755
 
  uint int_count, comment_length=0;
756
 
  uchar buff[MAX_FIELD_WIDTH];
 
774
  register uint32_t i;
 
775
  uint32_t int_count, comment_length=0, vcol_info_length=0;
 
776
  unsigned char buff[MAX_FIELD_WIDTH];
757
777
  Create_field *field;
758
778
  
759
779
 
764
784
  int_count=0;
765
785
  while ((field=it++))
766
786
  {
767
 
    uint recpos;
768
 
    buff[0]= (uchar) field->row;
769
 
    buff[1]= (uchar) field->col;
770
 
    buff[2]= (uchar) field->sc_length;
 
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;
771
792
    int2store(buff+3, field->length);
772
793
    /* The +1 is here becasue the col offset in .frm file have offset 1 */
773
794
    recpos= field->offset+1 + (uint) data_offset;
774
795
    int3store(buff+5,recpos);
775
796
    int2store(buff+8,field->pack_flag);
776
797
    int2store(buff+10,field->unireg_check);
777
 
    buff[12]= (uchar) field->interval_id;
778
 
    buff[13]= (uchar) field->sql_type; 
 
798
    buff[12]= (unsigned char) field->interval_id;
 
799
    buff[13]= (unsigned char) field->sql_type; 
779
800
    if (field->charset) 
780
 
      buff[14]= (uchar) field->charset->number;
 
801
      buff[14]= (unsigned char) field->charset->number;
781
802
    else
782
803
      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
    }
783
815
    int2store(buff+15, field->comment.length);
784
816
    comment_length+= field->comment.length;
785
817
    set_if_bigger(int_count,field->interval_id);
788
820
  }
789
821
 
790
822
        /* Write fieldnames */
791
 
  buff[0]=(uchar) NAMES_SEP_CHAR;
 
823
  buff[0]=(unsigned char) NAMES_SEP_CHAR;
792
824
  if (my_write(file, buff, 1, MYF_RW))
793
825
    return(1);
794
826
  i=0;
795
827
  it.rewind();
796
828
  while ((field=it++))
797
829
  {
798
 
    char *pos= stpcpy((char*) buff,field->field_name);
 
830
    char *pos= my_stpcpy((char*) buff,field->field_name);
799
831
    *pos++=NAMES_SEP_CHAR;
800
832
    if (i == create_fields.elements-1)
801
833
      *pos++=0;
817
849
      {
818
850
        unsigned char  sep= 0;
819
851
        unsigned char  occ[256];
820
 
        uint           i;
 
852
        uint32_t           i;
821
853
        unsigned char *val= NULL;
822
854
 
823
855
        memset(occ, 0, sizeof(occ));
824
856
 
825
857
        for (i=0; (val= (unsigned char*) field->interval->type_names[i]); i++)
826
 
          for (uint j = 0; j < field->interval->type_lengths[i]; j++)
 
858
          for (uint32_t j = 0; j < field->interval->type_lengths[i]; j++)
827
859
            occ[(unsigned int) (val[j])]= 1;
828
860
 
829
861
        if (!occ[(unsigned char)NAMES_SEP_CHAR])
832
864
          sep= ',';
833
865
        else
834
866
        {
835
 
          for (uint i=1; i<256; i++)
 
867
          for (uint32_t i=1; i<256; i++)
836
868
          {
837
869
            if(!occ[i])
838
870
            {
859
891
        tmp.append('\0');                      // End of intervall
860
892
      }
861
893
    }
862
 
    if (my_write(file,(uchar*) tmp.ptr(),tmp.length(),MYF_RW))
 
894
    if (my_write(file,(unsigned char*) tmp.ptr(),tmp.length(),MYF_RW))
863
895
      return(1);
864
896
  }
865
897
  if (comment_length)
869
901
    while ((field=it++))
870
902
    {
871
903
      if (field->comment.length)
872
 
        if (my_write(file, (uchar*) field->comment.str, field->comment.length,
 
904
        if (my_write(file, (unsigned char*) field->comment.str, field->comment.length,
873
905
                     MYF_RW))
874
906
          return(1);
875
907
    }
876
908
  }
 
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
  }
877
937
  return(0);
878
938
}
879
939
 
880
940
 
881
941
/* save an empty record on start of formfile */
882
942
 
883
 
static bool make_empty_rec(THD *thd, File file,
 
943
static bool make_empty_rec(Session *session, File file,
884
944
                           enum legacy_db_type table_type __attribute__((unused)),
885
 
                           uint table_options,
 
945
                           uint32_t table_options,
886
946
                           List<Create_field> &create_fields,
887
 
                           uint reclength,
 
947
                           uint32_t reclength,
888
948
                           ulong data_offset,
889
949
                           handler *handler)
890
950
{
891
951
  int error= 0;
892
952
  Field::utype type;
893
 
  uint null_count;
894
 
  uchar *buff,*null_pos;
 
953
  uint32_t null_count;
 
954
  unsigned char *buff,*null_pos;
895
955
  Table table;
896
956
  TABLE_SHARE share;
897
957
  Create_field *field;
898
 
  enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
 
958
  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
899
959
  
900
960
 
901
961
  /* We need a table to generate columns for default values */
903
963
  memset(&share, 0, sizeof(share));
904
964
  table.s= &share;
905
965
 
906
 
  if (!(buff=(uchar*) my_malloc((size_t) reclength,MYF(MY_WME | MY_ZEROFILL))))
 
966
  if (!(buff=(unsigned char*) my_malloc((size_t) reclength,MYF(MY_WME | MY_ZEROFILL))))
907
967
  {
908
968
    return(1);
909
969
  }
910
970
 
911
 
  table.in_use= thd;
 
971
  table.in_use= session;
912
972
  table.s->db_low_byte_first= handler->low_byte_first();
913
973
  table.s->blob_ptr_size= portable_sizeof_char_ptr;
914
974
 
921
981
  null_pos= buff;
922
982
 
923
983
  List_iterator<Create_field> it(create_fields);
924
 
  thd->count_cuted_fields= CHECK_FIELD_WARN;    // To find wrong default values
 
984
  session->count_cuted_fields= CHECK_FIELD_WARN;    // To find wrong default values
925
985
  while ((field=it++))
926
986
  {
927
987
    /*
988
1048
    of 8 there are no unused bits.
989
1049
  */
990
1050
  if (null_count & 7)
991
 
    *(null_pos + null_count / 8)|= ~(((uchar) 1 << (null_count & 7)) - 1);
 
1051
    *(null_pos + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
992
1052
 
993
1053
  error= my_write(file, buff, (size_t) reclength,MYF_RW) != 0;
994
1054
 
995
1055
err:
996
 
  my_free(buff, MYF(MY_FAE));
997
 
  thd->count_cuted_fields= old_count_cuted_fields;
 
1056
  free(buff);
 
1057
  session->count_cuted_fields= old_count_cuted_fields;
998
1058
  return(error);
999
1059
} /* make_empty_rec */