~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/unireg.cc

  • Committer: Stewart Smith
  • Date: 2008-06-30 06:46:40 UTC
  • mto: This revision was merged to the branch mainline in revision 19.
  • Revision ID: stewart@flamingspork.com-20080630064640-1tbyi1e8j4duba45
no embedded server, stop testing for it in tests.

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
 
233
240
 
234
241
  int2store(fileinfo+59,db_file->extra_rec_buf_length());
235
242
 
236
 
  if (pwrite(file, fileinfo, 64, 0L) == 0 ||
237
 
      pwrite(file, keybuff, key_info_length, (ulong) uint2korr(fileinfo+6)) == 0)
 
243
  if (my_pwrite(file, fileinfo, 64, 0L, MYF_RW) ||
 
244
      my_pwrite(file, keybuff, key_info_length,
 
245
                (ulong) uint2korr(fileinfo+6),MYF_RW))
238
246
    goto err;
239
 
  my_seek(file,
 
247
  VOID(my_seek(file,
240
248
               (ulong) uint2korr(fileinfo+6)+ (ulong) key_buff_length,
241
 
               MY_SEEK_SET,MYF(0));
 
249
               MY_SEEK_SET,MYF(0)));
242
250
  if (make_empty_rec(thd,file,ha_legacy_type(create_info->db_type),
243
251
                     create_info->table_options,
244
252
                     create_fields,reclength, data_offset, db_file))
245
253
    goto err;
246
254
 
247
255
  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,
 
256
  if (my_write(file, (const uchar*)buff, 2, MYF(MY_NABP)) ||
 
257
      my_write(file, (const uchar*)create_info->connect_string.str,
250
258
               create_info->connect_string.length, MYF(MY_NABP)))
251
259
      goto err;
252
260
 
253
261
  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,
 
262
  if (my_write(file, (const uchar*)buff, 2, MYF(MY_NABP)) ||
 
263
      my_write(file, (const uchar*)str_db_type.str,
256
264
               str_db_type.length, MYF(MY_NABP)))
257
265
    goto err;
258
266
 
259
267
  {
260
 
    memset(buff, 0, 6);
261
 
    if (my_write(file, (unsigned char*) buff, 6, MYF_RW))
 
268
    bzero((uchar*) buff, 6);
 
269
    if (my_write(file, (uchar*) buff, 6, MYF_RW))
262
270
      goto err;
263
271
  }
264
272
 
265
 
  if (forminfo[46] == (unsigned char)255)
 
273
  if (forminfo[46] == (uchar)255)
266
274
  {
267
 
    unsigned char comment_length_buff[2];
 
275
    uchar comment_length_buff[2];
268
276
    int2store(comment_length_buff,create_info->comment.length);
269
277
    if (my_write(file, comment_length_buff, 2, MYF(MY_NABP)) ||
270
 
        my_write(file, (unsigned char*)create_info->comment.str,
 
278
        my_write(file, (uchar*)create_info->comment.str,
271
279
                  create_info->comment.length, MYF(MY_NABP)))
272
280
      goto err;
273
281
  }
276
284
  {
277
285
    /* prepare header */
278
286
    {
279
 
      uint32_t flags= 0;
 
287
      uint flags= 0;
 
288
      flags|= create_info->default_storage_media; //3 bits
280
289
 
281
 
      memset(buff, 0, format_section_header_size);
 
290
      bzero(buff, format_section_header_size);
282
291
      /* length of section 2 bytes*/
283
292
      int2store(buff+0, format_section_len);
284
293
      /* flags of section 4 bytes*/
286
295
      /* 2 bytes left for future use */
287
296
    }
288
297
    /* write header */
289
 
    if (my_write(file, (const unsigned char*)buff, format_section_header_size, MYF_RW))
 
298
    if (my_write(file, (const uchar*)buff, format_section_header_size, MYF_RW))
290
299
      goto err;
 
300
    /* write tablespace name */
 
301
    if (tablespace_len > 0)
 
302
      if (my_write(file, (const uchar*)create_info->tablespace, tablespace_len, MYF_RW))
 
303
        goto err;
291
304
    buff[0]= 0;
292
 
    if (my_write(file, (const unsigned char*)buff, 1, MYF_RW))
 
305
    if (my_write(file, (const uchar*)buff, 1, MYF_RW))
293
306
      goto err;
294
307
    /* write column info, 1 byte per column */
295
308
    {
296
309
      List_iterator<Create_field> it(create_fields);
297
310
      Create_field *field;
298
 
      unsigned char column_format, write_byte;
 
311
      uchar storage_type, column_format, write_byte;
299
312
      while ((field=it++))
300
313
      {
301
 
        column_format= (unsigned char)field->column_format();
302
 
        write_byte= (column_format << COLUMN_FORMAT_SHIFT);
 
314
        storage_type= (uchar)field->field_storage_type();
 
315
        column_format= (uchar)field->column_format();
 
316
        write_byte= storage_type + (column_format << COLUMN_FORMAT_SHIFT);
303
317
        if (my_write(file, &write_byte, 1, MYF_RW))
304
318
          goto err;
305
319
      }
306
320
    }
307
321
  }
308
 
  my_seek(file,filepos,MY_SEEK_SET,MYF(0));
 
322
  VOID(my_seek(file,filepos,MY_SEEK_SET,MYF(0)));
309
323
  if (my_write(file, forminfo, 288, MYF_RW) ||
310
324
      my_write(file, screen_buff, info_length, MYF_RW) ||
311
325
      pack_fields(file, create_fields, data_offset))
312
326
    goto err;
313
327
 
314
 
  free(screen_buff);
315
 
  free(keybuff);
 
328
  my_free(screen_buff,MYF(0));
 
329
  my_free(keybuff, MYF(0));
316
330
 
317
 
  if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
 
331
  if (opt_sync_frm && !(create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
318
332
      (my_sync(file, MYF(MY_WME)) ||
319
333
       my_sync_dir_by_file(file_name, MYF(MY_WME))))
320
334
      goto err2;
338
352
      }
339
353
    }
340
354
  }
341
 
  return(0);
 
355
  DBUG_RETURN(0);
342
356
 
343
357
err:
344
 
  free(screen_buff);
345
 
  free(keybuff);
 
358
  my_free(screen_buff, MYF(0));
 
359
  my_free(keybuff, MYF(0));
346
360
err2:
347
 
  my_close(file,MYF(MY_WME));
 
361
  VOID(my_close(file,MYF(MY_WME)));
348
362
err3:
349
363
  my_delete(file_name,MYF(0));
350
 
  return(1);
 
364
  DBUG_RETURN(1);
351
365
} /* mysql_create_frm */
352
366
 
353
367
 
375
389
                     const char *db, const char *table_name,
376
390
                     HA_CREATE_INFO *create_info,
377
391
                     List<Create_field> &create_fields,
378
 
                     uint32_t keys, KEY *key_info, handler *file)
 
392
                     uint keys, KEY *key_info, handler *file)
379
393
{
380
 
  
 
394
  DBUG_ENTER("rea_create_table");
381
395
 
382
396
  char frm_name[FN_REFLEN];
383
 
  strxmov(frm_name, path, reg_ext, NULL);
 
397
  strxmov(frm_name, path, reg_ext, NullS);
384
398
  if (mysql_create_frm(thd, frm_name, db, table_name, create_info,
385
399
                       create_fields, keys, key_info, file))
386
400
 
387
 
    return(1);
 
401
    DBUG_RETURN(1);
388
402
 
389
403
  // Make sure mysql_create_frm din't remove extension
390
 
  assert(*fn_rext(frm_name));
 
404
  DBUG_ASSERT(*fn_rext(frm_name));
391
405
  if (thd->variables.keep_files_on_create)
392
406
    create_info->options|= HA_CREATE_KEEP_FILES;
393
407
  if (file->ha_create_handler_files(path, NULL, CHF_CREATE_FLAG, create_info))
395
409
  if (!create_info->frm_only && ha_create_table(thd, path, db, table_name,
396
410
                                                create_info,0))
397
411
    goto err_handler;
398
 
  return(0);
 
412
  DBUG_RETURN(0);
399
413
 
400
414
err_handler:
401
 
  file->ha_create_handler_files(path, NULL, CHF_DELETE_FLAG, create_info);
 
415
  VOID(file->ha_create_handler_files(path, NULL, CHF_DELETE_FLAG, create_info));
402
416
  my_delete(frm_name, MYF(0));
403
 
  return(1);
 
417
  DBUG_RETURN(1);
404
418
} /* rea_create_table */
405
419
 
406
420
 
407
421
        /* Pack screens to a screen for save in a form-file */
408
422
 
409
 
static unsigned char *pack_screens(List<Create_field> &create_fields,
410
 
                           uint32_t *info_length, uint32_t *screens,
 
423
static uchar *pack_screens(List<Create_field> &create_fields,
 
424
                           uint *info_length, uint *screens,
411
425
                           bool small_file)
412
426
{
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;
 
427
  register uint i;
 
428
  uint row,start_row,end_row,fields_on_screen;
 
429
  uint length,cols;
 
430
  uchar *info,*pos,*start_screen;
 
431
  uint fields=create_fields.elements;
418
432
  List_iterator<Create_field> it(create_fields);
419
 
  
 
433
  DBUG_ENTER("pack_screens");
420
434
 
421
435
  start_row=4; end_row=22; cols=80; fields_on_screen=end_row+1-start_row;
422
436
 
427
441
  while ((field=it++))
428
442
    length+=(uint) strlen(field->field_name)+1+TE_INFO_LENGTH+cols/2;
429
443
 
430
 
  if (!(info=(unsigned char*) my_malloc(length,MYF(MY_WME))))
431
 
    return(0);
 
444
  if (!(info=(uchar*) my_malloc(length,MYF(MY_WME))))
 
445
    DBUG_RETURN(0);
432
446
 
433
447
  start_screen=0;
434
448
  row=end_row;
443
457
      {
444
458
        length=(uint) (pos-start_screen);
445
459
        int2store(start_screen,length);
446
 
        start_screen[2]=(unsigned char) (fields_on_screen+1);
447
 
        start_screen[3]=(unsigned char) (fields_on_screen);
 
460
        start_screen[2]=(uchar) (fields_on_screen+1);
 
461
        start_screen[3]=(uchar) (fields_on_screen);
448
462
      }
449
463
      row=start_row;
450
464
      start_screen=pos;
451
465
      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;
 
466
      pos[0]= (uchar) start_row-2;      /* Header string */
 
467
      pos[1]= (uchar) (cols >> 2);
 
468
      pos[2]= (uchar) (cols >> 1) +1;
455
469
      strfill((char *) pos+3,(uint) (cols >> 1),' ');
456
470
      pos+=(cols >> 1)+4;
457
471
    }
461
475
 
462
476
    if (!small_file)
463
477
    {
464
 
      pos[0]=(unsigned char) row;
 
478
      pos[0]=(uchar) row;
465
479
      pos[1]=0;
466
 
      pos[2]=(unsigned char) (length+1);
467
 
      pos=(unsigned char*) strmake((char*) pos+3,cfield->field_name,length)+1;
 
480
      pos[2]=(uchar) (length+1);
 
481
      pos=(uchar*) strmake((char*) pos+3,cfield->field_name,length)+1;
468
482
    }
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));
 
483
    cfield->row=(uint8) row;
 
484
    cfield->col=(uint8) (length+1);
 
485
    cfield->sc_length=(uint8) min(cfield->length,cols-(length+2));
472
486
  }
473
487
  length=(uint) (pos-start_screen);
474
488
  int2store(start_screen,length);
475
 
  start_screen[2]=(unsigned char) (row-start_row+2);
476
 
  start_screen[3]=(unsigned char) (row-start_row+1);
 
489
  start_screen[2]=(uchar) (row-start_row+2);
 
490
  start_screen[3]=(uchar) (row-start_row+1);
477
491
 
478
492
  *info_length=(uint) (pos-info);
479
 
  return(info);
 
493
  DBUG_RETURN(info);
480
494
} /* pack_screens */
481
495
 
482
496
 
483
497
        /* Pack keyinfo and keynames to keybuff for save in form-file. */
484
498
 
485
 
static uint32_t pack_keys(unsigned char *keybuff, uint32_t key_count, KEY *keyinfo,
 
499
static uint pack_keys(uchar *keybuff, uint key_count, KEY *keyinfo,
486
500
                      ulong data_offset)
487
501
{
488
 
  uint32_t key_parts,length;
489
 
  unsigned char *pos, *keyname_pos;
 
502
  uint key_parts,length;
 
503
  uchar *pos, *keyname_pos;
490
504
  KEY *key,*end;
491
505
  KEY_PART_INFO *key_part,*key_part_end;
492
 
  
 
506
  DBUG_ENTER("pack_keys");
493
507
 
494
508
  pos=keybuff+6;
495
509
  key_parts=0;
497
511
  {
498
512
    int2store(pos, (key->flags ^ HA_NOSAME));
499
513
    int2store(pos+2,key->key_length);
500
 
    pos[4]= (unsigned char) key->key_parts;
501
 
    pos[5]= (unsigned char) key->algorithm;
 
514
    pos[4]= (uchar) key->key_parts;
 
515
    pos[5]= (uchar) key->algorithm;
502
516
    int2store(pos+6, key->block_size);
503
517
    pos+=8;
504
518
    key_parts+=key->key_parts;
 
519
    DBUG_PRINT("loop", ("flags: %lu  key_parts: %d at 0x%lx",
 
520
                        key->flags, key->key_parts,
 
521
                        (long) key->key_part));
505
522
    for (key_part=key->key_part,key_part_end=key_part+key->key_parts ;
506
523
         key_part != key_part_end ;
507
524
         key_part++)
508
525
 
509
526
    {
510
 
      uint32_t offset;
 
527
      uint offset;
 
528
      DBUG_PRINT("loop",("field: %d  startpos: %lu  length: %d",
 
529
                         key_part->fieldnr, key_part->offset + data_offset,
 
530
                         key_part->length));
511
531
      int2store(pos,key_part->fieldnr+1+FIELD_NAME_USED);
512
532
      offset= (uint) (key_part->offset+data_offset+1);
513
533
      int2store(pos+2, offset);
519
539
  }
520
540
        /* Save keynames */
521
541
  keyname_pos=pos;
522
 
  *pos++=(unsigned char) NAMES_SEP_CHAR;
 
542
  *pos++=(uchar) NAMES_SEP_CHAR;
523
543
  for (key=keyinfo ; key != end ; key++)
524
544
  {
525
 
    unsigned char *tmp=(unsigned char*) my_stpcpy((char*) pos,key->name);
526
 
    *tmp++= (unsigned char) NAMES_SEP_CHAR;
 
545
    uchar *tmp=(uchar*) strmov((char*) pos,key->name);
 
546
    *tmp++= (uchar) NAMES_SEP_CHAR;
527
547
    *tmp=0;
528
548
    pos=tmp;
529
549
  }
534
554
    if (key->flags & HA_USES_COMMENT)
535
555
    {
536
556
      int2store(pos, key->comment.length);
537
 
      unsigned char *tmp= (unsigned char*)my_stpncpy((char*) pos+2,key->comment.str,key->comment.length);
 
557
      uchar *tmp= (uchar*)strnmov((char*) pos+2,key->comment.str,key->comment.length);
538
558
      pos= tmp;
539
559
    }
540
560
  }
547
567
  }
548
568
  else
549
569
  {
550
 
    keybuff[0]=(unsigned char) key_count;
551
 
    keybuff[1]=(unsigned char) key_parts;
 
570
    keybuff[0]=(uchar) key_count;
 
571
    keybuff[1]=(uchar) key_parts;
552
572
    keybuff[2]= keybuff[3]= 0;
553
573
  }
554
574
  length=(uint) (pos-keyname_pos);
555
575
  int2store(keybuff+4,length);
556
 
  return((uint) (pos-keybuff));
 
576
  DBUG_RETURN((uint) (pos-keybuff));
557
577
} /* pack_keys */
558
578
 
559
579
 
560
 
/* Make formheader */
 
580
        /* Make formheader */
561
581
 
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,
 
582
static bool pack_header(uchar *forminfo, enum legacy_db_type table_type,
 
583
                        List<Create_field> &create_fields,
 
584
                        uint info_length, uint screens, uint table_options,
565
585
                        ulong data_offset, handler *file)
566
586
{
567
 
  uint32_t length,int_count,int_length,no_empty, int_parts;
568
 
  uint32_t time_stamp_pos,null_fields;
 
587
  uint length,int_count,int_length,no_empty, int_parts;
 
588
  uint time_stamp_pos,null_fields;
569
589
  ulong reclength, totlength, n_length, com_length;
570
 
 
 
590
  DBUG_ENTER("pack_header");
571
591
 
572
592
  if (create_fields.elements > MAX_FIELDS)
573
593
  {
574
594
    my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0));
575
 
    return(1);
 
595
    DBUG_RETURN(1);
576
596
  }
577
597
 
578
598
  totlength= 0L;
587
607
  Create_field *field;
588
608
  while ((field=it++))
589
609
  {
590
 
    uint32_t tmp_len= system_charset_info->cset->charpos(system_charset_info,
 
610
    uint tmp_len= system_charset_info->cset->charpos(system_charset_info,
591
611
                                                     field->comment.str,
592
612
                                                     field->comment.str +
593
613
                                                     field->comment.length,
598
618
      my_error(ER_WRONG_STRING_LENGTH, MYF(0),
599
619
               field->comment.str,"COLUMN COMMENT",
600
620
               (uint) COLUMN_COMMENT_MAXLEN);
601
 
      return(1);
 
621
      DBUG_RETURN(1);
602
622
    }
603
623
 
604
624
    totlength+= field->length;
614
634
      We mark first TIMESTAMP field with NOW() in DEFAULT or ON UPDATE 
615
635
      as auto-update field.
616
636
    */
617
 
    if (field->sql_type == DRIZZLE_TYPE_TIMESTAMP &&
 
637
    if (field->sql_type == MYSQL_TYPE_TIMESTAMP &&
618
638
        MTYP_TYPENR(field->unireg_check) != Field::NONE &&
619
639
        !time_stamp_pos)
620
640
      time_stamp_pos= (uint) field->offset+ (uint) data_offset + 1;
621
641
    length=field->pack_length;
622
642
    /* Ensure we don't have any bugs when generating offsets */
623
 
    assert(reclength == field->offset + data_offset);
 
643
    DBUG_ASSERT(reclength == field->offset + data_offset);
624
644
    if ((uint) field->offset+ (uint) data_offset+ length > reclength)
625
645
      reclength=(uint) (field->offset+ data_offset + length);
626
646
    n_length+= (ulong) strlen(field->field_name)+1;
628
648
    field->save_interval= 0;
629
649
    if (field->interval)
630
650
    {
631
 
      uint32_t old_int_count=int_count;
 
651
      uint old_int_count=int_count;
632
652
 
633
653
      if (field->charset->mbminlen > 1)
634
654
      {
648
668
                                    (field->interval->count+1));
649
669
        field->interval->type_names[field->interval->count]= 0;
650
670
        field->interval->type_lengths=
651
 
          (uint32_t *) sql_alloc(sizeof(uint) * field->interval->count);
 
671
          (uint *) sql_alloc(sizeof(uint) * field->interval->count);
652
672
 
653
 
        for (uint32_t pos= 0; pos < field->interval->count; pos++)
 
673
        for (uint pos= 0; pos < field->interval->count; pos++)
654
674
        {
655
675
          char *dst;
656
676
          const char *src= field->save_interval->type_names[pos];
657
 
          uint32_t hex_length;
 
677
          uint hex_length;
658
678
          length= field->save_interval->type_lengths[pos];
659
679
          hex_length= length * 2;
660
680
          field->interval->type_lengths[pos]= hex_length;
682
702
  if (reclength > (ulong) file->max_record_length())
683
703
  {
684
704
    my_error(ER_TOO_BIG_ROWSIZE, MYF(0), (uint) file->max_record_length());
685
 
    return(1);
 
705
    DBUG_RETURN(1);
686
706
  }
687
707
  /* Hack to avoid bugs with small static rows in MySQL */
688
 
  reclength=cmax((ulong)file->min_record_length(table_options),reclength);
 
708
  reclength=max(file->min_record_length(table_options),reclength);
689
709
  if (info_length+(ulong) create_fields.elements*FCOMP+288+
690
710
      n_length+int_length+com_length > 65535L || int_count > 255)
691
711
  {
692
712
    my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0));
693
 
    return(1);
 
713
    DBUG_RETURN(1);
694
714
  }
695
715
 
696
 
  memset(forminfo, 0, 288);
 
716
  bzero((char*)forminfo,288);
697
717
  length=(info_length+create_fields.elements*FCOMP+288+n_length+int_length+
698
718
          com_length);
699
719
  int2store(forminfo,length);
700
 
  forminfo[256] = (uint8_t) screens;
 
720
  forminfo[256] = (uint8) screens;
701
721
  int2store(forminfo+258,create_fields.elements);
702
722
  int2store(forminfo+260,info_length);
703
723
  int2store(forminfo+262,totlength);
713
733
  int2store(forminfo+282,null_fields);
714
734
  int2store(forminfo+284,com_length);
715
735
  /* Up to forminfo+288 is free to use for additional information */
716
 
  return(0);
 
736
  DBUG_RETURN(0);
717
737
} /* pack_header */
718
738
 
719
739
 
720
740
        /* get each unique interval each own id */
721
741
 
722
 
static uint32_t get_interval_id(uint32_t *int_count,List<Create_field> &create_fields,
 
742
static uint get_interval_id(uint *int_count,List<Create_field> &create_fields,
723
743
                            Create_field *last_field)
724
744
{
725
745
  List_iterator<Create_field> it(create_fields);
750
770
static bool pack_fields(File file, List<Create_field> &create_fields,
751
771
                        ulong data_offset)
752
772
{
753
 
  register uint32_t i;
754
 
  uint32_t int_count, comment_length=0;
755
 
  unsigned char buff[MAX_FIELD_WIDTH];
 
773
  register uint i;
 
774
  uint int_count, comment_length=0;
 
775
  uchar buff[MAX_FIELD_WIDTH];
756
776
  Create_field *field;
757
 
  
 
777
  DBUG_ENTER("pack_fields");
758
778
 
759
779
        /* Write field info */
760
780
 
763
783
  int_count=0;
764
784
  while ((field=it++))
765
785
  {
766
 
    uint32_t recpos;
767
 
    buff[0]= (unsigned char) field->row;
768
 
    buff[1]= (unsigned char) field->col;
769
 
    buff[2]= (unsigned char) field->sc_length;
 
786
    uint recpos;
 
787
    buff[0]= (uchar) field->row;
 
788
    buff[1]= (uchar) field->col;
 
789
    buff[2]= (uchar) field->sc_length;
770
790
    int2store(buff+3, field->length);
771
791
    /* The +1 is here becasue the col offset in .frm file have offset 1 */
772
792
    recpos= field->offset+1 + (uint) data_offset;
773
793
    int3store(buff+5,recpos);
774
794
    int2store(buff+8,field->pack_flag);
775
795
    int2store(buff+10,field->unireg_check);
776
 
    buff[12]= (unsigned char) field->interval_id;
777
 
    buff[13]= (unsigned char) field->sql_type; 
 
796
    buff[12]= (uchar) field->interval_id;
 
797
    buff[13]= (uchar) field->sql_type; 
778
798
    if (field->charset) 
779
 
      buff[14]= (unsigned char) field->charset->number;
 
799
      buff[14]= (uchar) field->charset->number;
780
800
    else
781
801
      buff[14]= 0;                              // Numerical
782
802
    int2store(buff+15, field->comment.length);
783
803
    comment_length+= field->comment.length;
784
804
    set_if_bigger(int_count,field->interval_id);
785
805
    if (my_write(file, buff, FCOMP, MYF_RW))
786
 
      return(1);
 
806
      DBUG_RETURN(1);
787
807
  }
788
808
 
789
809
        /* Write fieldnames */
790
 
  buff[0]=(unsigned char) NAMES_SEP_CHAR;
 
810
  buff[0]=(uchar) NAMES_SEP_CHAR;
791
811
  if (my_write(file, buff, 1, MYF_RW))
792
 
    return(1);
 
812
    DBUG_RETURN(1);
793
813
  i=0;
794
814
  it.rewind();
795
815
  while ((field=it++))
796
816
  {
797
 
    char *pos= my_stpcpy((char*) buff,field->field_name);
 
817
    char *pos= strmov((char*) buff,field->field_name);
798
818
    *pos++=NAMES_SEP_CHAR;
799
819
    if (i == create_fields.elements-1)
800
820
      *pos++=0;
801
821
    if (my_write(file, buff, (size_t) (pos-(char*) buff),MYF_RW))
802
 
      return(1);
 
822
      DBUG_RETURN(1);
803
823
    i++;
804
824
  }
805
825
 
816
836
      {
817
837
        unsigned char  sep= 0;
818
838
        unsigned char  occ[256];
819
 
        uint32_t           i;
 
839
        uint           i;
820
840
        unsigned char *val= NULL;
821
841
 
822
 
        memset(occ, 0, sizeof(occ));
 
842
        bzero(occ, sizeof(occ));
823
843
 
824
844
        for (i=0; (val= (unsigned char*) field->interval->type_names[i]); i++)
825
 
          for (uint32_t j = 0; j < field->interval->type_lengths[i]; j++)
 
845
          for (uint j = 0; j < field->interval->type_lengths[i]; j++)
826
846
            occ[(unsigned int) (val[j])]= 1;
827
847
 
828
848
        if (!occ[(unsigned char)NAMES_SEP_CHAR])
831
851
          sep= ',';
832
852
        else
833
853
        {
834
 
          for (uint32_t i=1; i<256; i++)
 
854
          for (uint i=1; i<256; i++)
835
855
          {
836
856
            if(!occ[i])
837
857
            {
844
864
          {
845
865
            my_message(ER_WRONG_FIELD_TERMINATORS,ER(ER_WRONG_FIELD_TERMINATORS),
846
866
                       MYF(0));
847
 
            return(1);
 
867
            DBUG_RETURN(1);
848
868
          }
849
869
        }
850
870
 
858
878
        tmp.append('\0');                      // End of intervall
859
879
      }
860
880
    }
861
 
    if (my_write(file,(unsigned char*) tmp.ptr(),tmp.length(),MYF_RW))
862
 
      return(1);
 
881
    if (my_write(file,(uchar*) tmp.ptr(),tmp.length(),MYF_RW))
 
882
      DBUG_RETURN(1);
863
883
  }
864
884
  if (comment_length)
865
885
  {
868
888
    while ((field=it++))
869
889
    {
870
890
      if (field->comment.length)
871
 
        if (my_write(file, (unsigned char*) field->comment.str, field->comment.length,
 
891
        if (my_write(file, (uchar*) field->comment.str, field->comment.length,
872
892
                     MYF_RW))
873
 
          return(1);
 
893
          DBUG_RETURN(1);
874
894
    }
875
895
  }
876
 
  return(0);
 
896
  DBUG_RETURN(0);
877
897
}
878
898
 
879
899
 
880
 
/* save an empty record on start of formfile */
 
900
        /* save an empty record on start of formfile */
881
901
 
882
 
static bool make_empty_rec(THD *thd, File file,
883
 
                           enum legacy_db_type table_type __attribute__((unused)),
884
 
                           uint32_t table_options,
885
 
                           List<Create_field> &create_fields,
886
 
                           uint32_t reclength,
 
902
static bool make_empty_rec(THD *thd, File file,enum legacy_db_type table_type,
 
903
                           uint table_options,
 
904
                           List<Create_field> &create_fields,
 
905
                           uint reclength,
887
906
                           ulong data_offset,
888
907
                           handler *handler)
889
908
{
890
909
  int error= 0;
891
910
  Field::utype type;
892
 
  uint32_t null_count;
893
 
  unsigned char *buff,*null_pos;
894
 
  Table table;
 
911
  uint null_count;
 
912
  uchar *buff,*null_pos;
 
913
  TABLE table;
895
914
  TABLE_SHARE share;
896
915
  Create_field *field;
897
916
  enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
898
 
  
 
917
  DBUG_ENTER("make_empty_rec");
899
918
 
900
919
  /* We need a table to generate columns for default values */
901
 
  memset(&table, 0, sizeof(table));
902
 
  memset(&share, 0, sizeof(share));
 
920
  bzero((char*) &table, sizeof(table));
 
921
  bzero((char*) &share, sizeof(share));
903
922
  table.s= &share;
904
923
 
905
 
  if (!(buff=(unsigned char*) my_malloc((size_t) reclength,MYF(MY_WME | MY_ZEROFILL))))
 
924
  if (!(buff=(uchar*) my_malloc((size_t) reclength,MYF(MY_WME | MY_ZEROFILL))))
906
925
  {
907
 
    return(1);
 
926
    DBUG_RETURN(1);
908
927
  }
909
928
 
910
929
  table.in_use= thd;
953
972
      null_count++;
954
973
    }
955
974
 
 
975
    if (field->sql_type == MYSQL_TYPE_BIT && !f_bit_as_char(field->pack_flag))
 
976
      null_count+= field->length & 7;
 
977
 
956
978
    type= (Field::utype) MTYP_TYPENR(field->unireg_check);
957
979
 
958
980
    if (field->def)
967
989
        goto err;
968
990
      }
969
991
    }
970
 
    else if (regfield->real_type() == DRIZZLE_TYPE_ENUM &&
 
992
    else if (regfield->real_type() == MYSQL_TYPE_ENUM &&
971
993
             (field->flags & NOT_NULL_FLAG))
972
994
    {
973
995
      regfield->set_notnull();
974
 
      regfield->store((int64_t) 1, true);
 
996
      regfield->store((longlong) 1, TRUE);
975
997
    }
976
998
    else if (type == Field::YES)                // Old unireg type
977
999
      regfield->store(ER(ER_YES),(uint) strlen(ER(ER_YES)),system_charset_info);
980
1002
    else
981
1003
      regfield->reset();
982
1004
  }
983
 
  assert(data_offset == ((null_count + 7) / 8));
 
1005
  DBUG_ASSERT(data_offset == ((null_count + 7) / 8));
984
1006
 
985
1007
  /*
986
1008
    We need to set the unused bits to 1. If the number of bits is a multiple
987
1009
    of 8 there are no unused bits.
988
1010
  */
989
1011
  if (null_count & 7)
990
 
    *(null_pos + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
 
1012
    *(null_pos + null_count / 8)|= ~(((uchar) 1 << (null_count & 7)) - 1);
991
1013
 
992
1014
  error= my_write(file, buff, (size_t) reclength,MYF_RW) != 0;
993
1015
 
994
1016
err:
995
 
  free(buff);
 
1017
  my_free(buff, MYF(MY_FAE));
996
1018
  thd->count_cuted_fields= old_count_cuted_fields;
997
 
  return(error);
 
1019
  DBUG_RETURN(error);
998
1020
} /* make_empty_rec */