~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/unireg.cc

  • Committer: Monty Taylor
  • Date: 2008-07-05 18:10:38 UTC
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: monty@inaugust.com-20080705181038-0ih0nnamu5qrut0y
Fixed prototypes. Cleaned define a little bit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
    str is a (long) to record position where 0 is the first position.
24
24
*/
25
25
 
26
 
#include <drizzled/server_includes.h>
27
 
#include <drizzled/drizzled_error_messages.h>
 
26
#include "mysql_priv.h"
 
27
#include <m_ctype.h>
 
28
#include <assert.h>
28
29
 
29
30
#define FCOMP                   17              /* Bytes for a packed field */
30
31
 
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
                           uint table_options,
45
46
                           List<Create_field> &create_fields,
46
 
                           uint32_t reclength, ulong data_offset,
 
47
                           uint reclength, ulong data_offset,
47
48
                           handler *handler);
48
49
 
49
50
/**
55
56
 
56
57
struct Pack_header_error_handler: public Internal_error_handler
57
58
{
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,
61
62
                            THD *thd);
62
63
  bool is_handled;
63
 
  Pack_header_error_handler() :is_handled(false) {}
 
64
  Pack_header_error_handler() :is_handled(FALSE) {}
64
65
};
65
66
 
66
67
 
67
68
bool
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 */,
72
73
             THD * /* thd */)
73
74
{
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)
104
105
{
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;
108
109
  File file;
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;
 
113
  uchar *screen_buff;
113
114
  char buff[128];
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;
117
119
  int error;
 
120
  DBUG_ENTER("mysql_create_frm");
118
121
 
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)))
122
 
    return(1);
123
 
  assert(db_file != NULL);
 
125
    DBUG_RETURN(1);
 
126
  DBUG_ASSERT(db_file != NULL);
124
127
 
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))
129
132
 
130
133
  thd->push_internal_handler(&pack_header_error_handler);
131
134
 
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);
138
141
 
139
142
  if (error)
140
143
  {
141
 
    free(screen_buff);
 
144
    my_free(screen_buff, MYF(0));
142
145
    if (! pack_header_error_handler.is_handled)
143
 
      return(1);
 
146
      DBUG_RETURN(1);
144
147
 
145
148
    // Try again without UNIREG screens (to get more columns)
146
149
    if (!(screen_buff=pack_screens(create_fields,&info_length,&screens,1)))
147
 
      return(1);
148
 
    if (pack_header(forminfo,
 
150
      DBUG_RETURN(1);
 
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))
151
154
    {
152
 
      free(screen_buff);
153
 
      return(1);
 
155
      my_free(screen_buff, MYF(0));
 
156
      DBUG_RETURN(1);
154
157
    }
155
158
  }
156
159
  reclength=uint2korr(forminfo+266);
171
174
  create_info->extra_size+= 6;
172
175
 
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 +
 
181
    tablespace_len + 1 +
 
182
    create_fields.elements;
176
183
  create_info->extra_size+= format_section_len;
177
184
 
178
185
  tmp_len= system_charset_info->cset->charpos(system_charset_info,
184
191
  if (tmp_len < create_info->comment.length)
185
192
  {
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);
189
 
    free(screen_buff);
190
 
    return(1);
 
196
    my_free(screen_buff,MYF(0));
 
197
    DBUG_RETURN(1);
191
198
  }
192
199
 
193
200
  //if table comment is larger than 180 bytes, store into extra segment.
199
206
  else{
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
204
211
    /*
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.
208
215
    */
209
 
    memset(forminfo+47 + forminfo[46], 0, 61 - forminfo[46]);
 
216
    memset((char*) forminfo+47 + forminfo[46], 0, 61 - forminfo[46]);
210
217
#endif
211
218
  }
212
219
 
213
220
  if ((file=create_frm(thd, file_name, db, table, reclength, fileinfo,
214
221
                       create_info, keys, key_info)) < 0)
215
222
  {
216
 
    free(screen_buff);
217
 
    return(1);
 
223
    my_free(screen_buff, MYF(0));
 
224
    DBUG_RETURN(1);
218
225
  }
219
226
 
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,"")))
225
232
    goto err;
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);
232
239
 
236
243
  if (pwrite(file, fileinfo, 64, 0L) == 0 ||
237
244
      pwrite(file, keybuff, key_info_length, (ulong) uint2korr(fileinfo+6)) == 0)
238
245
    goto err;
239
 
  my_seek(file,
 
246
  VOID(my_seek(file,
240
247
               (ulong) uint2korr(fileinfo+6)+ (ulong) key_buff_length,
241
 
               MY_SEEK_SET,MYF(0));
 
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))
245
252
    goto err;
246
253
 
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)))
251
258
      goto err;
252
259
 
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)))
257
264
    goto err;
258
265
 
259
266
  {
260
 
    memset(buff, 0, 6);
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))
262
269
      goto err;
263
270
  }
264
271
 
265
 
  if (forminfo[46] == (unsigned char)255)
 
272
  if (forminfo[46] == (uchar)255)
266
273
  {
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)))
272
279
      goto err;
273
280
  }
276
283
  {
277
284
    /* prepare header */
278
285
    {
279
 
      uint32_t flags= 0;
 
286
      uint flags= 0;
 
287
      flags|= create_info->default_storage_media; //3 bits
280
288
 
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 */
287
295
    }
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))
290
298
      goto err;
 
299
    /* write tablespace name */
 
300
    if (tablespace_len > 0)
 
301
      if (my_write(file, (const uchar*)create_info->tablespace, tablespace_len, MYF_RW))
 
302
        goto err;
291
303
    buff[0]= 0;
292
 
    if (my_write(file, (const unsigned char*)buff, 1, MYF_RW))
 
304
    if (my_write(file, (const uchar*)buff, 1, MYF_RW))
293
305
      goto err;
294
306
    /* write column info, 1 byte per column */
295
307
    {
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++))
300
312
      {
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))
304
317
          goto err;
305
318
      }
306
319
    }
307
320
  }
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))
312
325
    goto err;
313
326
 
314
 
  free(screen_buff);
315
 
  free(keybuff);
 
327
  my_free(screen_buff,MYF(0));
 
328
  my_free(keybuff, MYF(0));
316
329
 
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))))
320
333
      goto err2;
338
351
      }
339
352
    }
340
353
  }
341
 
  return(0);
 
354
  DBUG_RETURN(0);
342
355
 
343
356
err:
344
 
  free(screen_buff);
345
 
  free(keybuff);
 
357
  my_free(screen_buff, MYF(0));
 
358
  my_free(keybuff, MYF(0));
346
359
err2:
347
 
  my_close(file,MYF(MY_WME));
 
360
  VOID(my_close(file,MYF(MY_WME)));
348
361
err3:
349
362
  my_delete(file_name,MYF(0));
350
 
  return(1);
 
363
  DBUG_RETURN(1);
351
364
} /* mysql_create_frm */
352
365
 
353
366
 
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)
379
392
{
380
 
  
 
393
  DBUG_ENTER("rea_create_table");
381
394
 
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))
386
399
 
387
 
    return(1);
 
400
    DBUG_RETURN(1);
388
401
 
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,
396
409
                                                create_info,0))
397
410
    goto err_handler;
398
 
  return(0);
 
411
  DBUG_RETURN(0);
399
412
 
400
413
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));
403
 
  return(1);
 
416
  DBUG_RETURN(1);
404
417
} /* rea_create_table */
405
418
 
406
419
 
407
420
        /* Pack screens to a screen for save in a form-file */
408
421
 
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,
411
424
                           bool small_file)
412
425
{
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;
 
426
  register uint i;
 
427
  uint row,start_row,end_row,fields_on_screen;
 
428
  uint length,cols;
 
429
  uchar *info,*pos,*start_screen;
 
430
  uint fields=create_fields.elements;
418
431
  List_iterator<Create_field> it(create_fields);
419
 
  
 
432
  DBUG_ENTER("pack_screens");
420
433
 
421
434
  start_row=4; end_row=22; cols=80; fields_on_screen=end_row+1-start_row;
422
435
 
427
440
  while ((field=it++))
428
441
    length+=(uint) strlen(field->field_name)+1+TE_INFO_LENGTH+cols/2;
429
442
 
430
 
  if (!(info=(unsigned char*) my_malloc(length,MYF(MY_WME))))
431
 
    return(0);
 
443
  if (!(info=(uchar*) my_malloc(length,MYF(MY_WME))))
 
444
    DBUG_RETURN(0);
432
445
 
433
446
  start_screen=0;
434
447
  row=end_row;
443
456
      {
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);
448
461
      }
449
462
      row=start_row;
450
463
      start_screen=pos;
451
464
      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;
 
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;
457
470
    }
461
474
 
462
475
    if (!small_file)
463
476
    {
464
 
      pos[0]=(unsigned char) row;
 
477
      pos[0]=(uchar) row;
465
478
      pos[1]=0;
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;
468
481
    }
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));
472
485
  }
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);
477
490
 
478
491
  *info_length=(uint) (pos-info);
479
 
  return(info);
 
492
  DBUG_RETURN(info);
480
493
} /* pack_screens */
481
494
 
482
495
 
483
496
        /* Pack keyinfo and keynames to keybuff for save in form-file. */
484
497
 
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)
487
500
{
488
 
  uint32_t key_parts,length;
489
 
  unsigned char *pos, *keyname_pos;
 
501
  uint key_parts,length;
 
502
  uchar *pos, *keyname_pos;
490
503
  KEY *key,*end;
491
504
  KEY_PART_INFO *key_part,*key_part_end;
492
 
  
 
505
  DBUG_ENTER("pack_keys");
493
506
 
494
507
  pos=keybuff+6;
495
508
  key_parts=0;
497
510
  {
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);
503
516
    pos+=8;
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 ;
507
523
         key_part++)
508
524
 
509
525
    {
510
 
      uint32_t offset;
 
526
      uint offset;
 
527
      DBUG_PRINT("loop",("field: %d  startpos: %lu  length: %d",
 
528
                         key_part->fieldnr, key_part->offset + data_offset,
 
529
                         key_part->length));
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);
519
538
  }
520
539
        /* Save keynames */
521
540
  keyname_pos=pos;
522
 
  *pos++=(unsigned char) NAMES_SEP_CHAR;
 
541
  *pos++=(uchar) NAMES_SEP_CHAR;
523
542
  for (key=keyinfo ; key != end ; key++)
524
543
  {
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;
527
546
    *tmp=0;
528
547
    pos=tmp;
529
548
  }
534
553
    if (key->flags & HA_USES_COMMENT)
535
554
    {
536
555
      int2store(pos, key->comment.length);
537
 
      unsigned char *tmp= (unsigned char*)my_stpncpy((char*) pos+2,key->comment.str,key->comment.length);
 
556
      uchar *tmp= (uchar*)strnmov((char*) pos+2,key->comment.str,key->comment.length);
538
557
      pos= tmp;
539
558
    }
540
559
  }
547
566
  }
548
567
  else
549
568
  {
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;
553
572
  }
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 */
558
577
 
559
578
 
560
 
/* Make formheader */
 
579
        /* Make formheader */
561
580
 
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)
566
585
{
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;
570
 
 
 
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");
571
590
 
572
591
  if (create_fields.elements > MAX_FIELDS)
573
592
  {
574
593
    my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0));
575
 
    return(1);
 
594
    DBUG_RETURN(1);
576
595
  }
577
596
 
578
597
  totlength= 0L;
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;
 
600
    com_length=0;
582
601
  n_length=2L;
583
602
 
584
603
        /* Check fields */
587
606
  Create_field *field;
588
607
  while ((field=it++))
589
608
  {
590
 
    uint32_t tmp_len= system_charset_info->cset->charpos(system_charset_info,
 
609
    uint tmp_len= system_charset_info->cset->charpos(system_charset_info,
591
610
                                                     field->comment.str,
592
611
                                                     field->comment.str +
593
612
                                                     field->comment.length,
598
617
      my_error(ER_WRONG_STRING_LENGTH, MYF(0),
599
618
               field->comment.str,"COLUMN COMMENT",
600
619
               (uint) COLUMN_COMMENT_MAXLEN);
601
 
      return(1);
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;
 
620
      DBUG_RETURN(1);
623
621
    }
624
622
 
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.
637
635
    */
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 &&
640
638
        !time_stamp_pos)
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;
647
647
    field->save_interval= 0;
648
648
    if (field->interval)
649
649
    {
650
 
      uint32_t old_int_count=int_count;
 
650
      uint old_int_count=int_count;
651
651
 
652
652
      if (field->charset->mbminlen > 1)
653
653
      {
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);
671
671
 
672
 
        for (uint32_t pos= 0; pos < field->interval->count; pos++)
 
672
        for (uint pos= 0; pos < field->interval->count; pos++)
673
673
        {
674
674
          char *dst;
675
675
          const char *src= field->save_interval->type_names[pos];
676
 
          uint32_t hex_length;
 
676
          uint hex_length;
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())
702
702
  {
703
703
    my_error(ER_TOO_BIG_ROWSIZE, MYF(0), (uint) file->max_record_length());
704
 
    return(1);
 
704
    DBUG_RETURN(1);
705
705
  }
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 || 
710
 
      int_count > 255)
 
709
      n_length+int_length+com_length > 65535L || int_count > 255)
711
710
  {
712
711
    my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0));
713
 
    return(1);
 
712
    DBUG_RETURN(1);
714
713
  }
715
714
 
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);
 
717
          com_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 */
737
 
  return(0);
 
734
  /* Up to forminfo+288 is free to use for additional information */
 
735
  DBUG_RETURN(0);
738
736
} /* pack_header */
739
737
 
740
738
 
741
739
        /* get each unique interval each own id */
742
740
 
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)
745
743
{
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)
773
771
{
774
 
  register uint32_t i;
775
 
  uint32_t int_count, comment_length=0, vcol_info_length=0;
776
 
  unsigned char buff[MAX_FIELD_WIDTH];
 
772
  register uint i;
 
773
  uint int_count, comment_length=0;
 
774
  uchar buff[MAX_FIELD_WIDTH];
777
775
  Create_field *field;
778
 
  
 
776
  DBUG_ENTER("pack_fields");
779
777
 
780
778
        /* Write field info */
781
779
 
784
782
  int_count=0;
785
783
  while ((field=it++))
786
784
  {
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;
 
785
    uint recpos;
 
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;
802
799
    else
803
800
      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
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))
819
 
      return(1);
 
805
      DBUG_RETURN(1);
820
806
  }
821
807
 
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))
825
 
    return(1);
 
811
    DBUG_RETURN(1);
826
812
  i=0;
827
813
  it.rewind();
828
814
  while ((field=it++))
829
815
  {
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)
833
819
      *pos++=0;
834
820
    if (my_write(file, buff, (size_t) (pos-(char*) buff),MYF_RW))
835
 
      return(1);
 
821
      DBUG_RETURN(1);
836
822
    i++;
837
823
  }
838
824
 
849
835
      {
850
836
        unsigned char  sep= 0;
851
837
        unsigned char  occ[256];
852
 
        uint32_t           i;
 
838
        uint           i;
853
839
        unsigned char *val= NULL;
854
840
 
855
 
        memset(occ, 0, sizeof(occ));
 
841
        bzero(occ, sizeof(occ));
856
842
 
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;
860
846
 
861
847
        if (!occ[(unsigned char)NAMES_SEP_CHAR])
864
850
          sep= ',';
865
851
        else
866
852
        {
867
 
          for (uint32_t i=1; i<256; i++)
 
853
          for (uint i=1; i<256; i++)
868
854
          {
869
855
            if(!occ[i])
870
856
            {
877
863
          {
878
864
            my_message(ER_WRONG_FIELD_TERMINATORS,ER(ER_WRONG_FIELD_TERMINATORS),
879
865
                       MYF(0));
880
 
            return(1);
 
866
            DBUG_RETURN(1);
881
867
          }
882
868
        }
883
869
 
891
877
        tmp.append('\0');                      // End of intervall
892
878
      }
893
879
    }
894
 
    if (my_write(file,(unsigned char*) tmp.ptr(),tmp.length(),MYF_RW))
895
 
      return(1);
 
880
    if (my_write(file,(uchar*) tmp.ptr(),tmp.length(),MYF_RW))
 
881
      DBUG_RETURN(1);
896
882
  }
897
883
  if (comment_length)
898
884
  {
901
887
    while ((field=it++))
902
888
    {
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,
905
891
                     MYF_RW))
906
 
          return(1);
907
 
    }
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
 
  }
937
 
  return(0);
 
892
          DBUG_RETURN(1);
 
893
    }
 
894
  }
 
895
  DBUG_RETURN(0);
938
896
}
939
897
 
940
898
 
941
 
/* save an empty record on start of formfile */
 
899
        /* save an empty record on start of formfile */
942
900
 
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,
947
 
                           uint32_t reclength,
 
901
static bool make_empty_rec(THD *thd, File file,enum legacy_db_type table_type,
 
902
                           uint table_options,
 
903
                           List<Create_field> &create_fields,
 
904
                           uint reclength,
948
905
                           ulong data_offset,
949
906
                           handler *handler)
950
907
{
951
908
  int error= 0;
952
909
  Field::utype type;
953
 
  uint32_t null_count;
954
 
  unsigned char *buff,*null_pos;
955
 
  Table table;
 
910
  uint null_count;
 
911
  uchar *buff,*null_pos;
 
912
  TABLE table;
956
913
  TABLE_SHARE share;
957
914
  Create_field *field;
958
915
  enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
959
 
  
 
916
  DBUG_ENTER("make_empty_rec");
960
917
 
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));
964
921
  table.s= &share;
965
922
 
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))))
967
924
  {
968
 
    return(1);
 
925
    DBUG_RETURN(1);
969
926
  }
970
927
 
971
928
  table.in_use= thd;
1014
971
      null_count++;
1015
972
    }
1016
973
 
 
974
    if (field->sql_type == MYSQL_TYPE_BIT && !f_bit_as_char(field->pack_flag))
 
975
      null_count+= field->length & 7;
 
976
 
1017
977
    type= (Field::utype) MTYP_TYPENR(field->unireg_check);
1018
978
 
1019
979
    if (field->def)
1028
988
        goto err;
1029
989
      }
1030
990
    }
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))
1033
993
    {
1034
994
      regfield->set_notnull();
1035
 
      regfield->store((int64_t) 1, true);
 
995
      regfield->store((longlong) 1, TRUE);
1036
996
    }
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);
1041
1001
    else
1042
1002
      regfield->reset();
1043
1003
  }
1044
 
  assert(data_offset == ((null_count + 7) / 8));
 
1004
  DBUG_ASSERT(data_offset == ((null_count + 7) / 8));
1045
1005
 
1046
1006
  /*
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.
1049
1009
  */
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);
1052
1012
 
1053
1013
  error= my_write(file, buff, (size_t) reclength,MYF_RW) != 0;
1054
1014
 
1055
1015
err:
1056
 
  free(buff);
 
1016
  my_free(buff, MYF(MY_FAE));
1057
1017
  thd->count_cuted_fields= old_count_cuted_fields;
1058
 
  return(error);
 
1018
  DBUG_RETURN(error);
1059
1019
} /* make_empty_rec */