~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
17
/*
18
  Functions to create a unireg form-file from a FIELD and a fieldname-fieldinfo
19
  struct.
20
  In the following functions FIELD * is an ordinary field-structure with
21
  the following exeptions:
22
    sc_length,typepos,row,kol,dtype,regnr and field need not to be set.
23
    str is a (long) to record position where 0 is the first position.
24
*/
25
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
26
#include <drizzled/server_includes.h>
549 by Monty Taylor
Took gettext.h out of header files.
27
#include <drizzled/error.h>
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
28
#include <drizzled/virtual_column_info.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
29
#include <drizzled/session.h>
1 by brian
clean slate
30
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
31
/* For proto */
32
#include <string>
33
#include <fstream>
34
#include <drizzled/serialize/serialize.h>
35
using namespace std;
36
1 by brian
clean slate
37
#define FCOMP			17		/* Bytes for a packed field */
38
481 by Brian Aker
Remove all of uchar.
39
static unsigned char * pack_screens(List<Create_field> &create_fields,
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
40
                                    uint32_t *info_length, uint32_t *screens,
41
                                    bool small_file);
42
static uint32_t pack_keys(unsigned char *keybuff,uint32_t key_count,
43
                          KEY *key_info, ulong data_offset);
481 by Brian Aker
Remove all of uchar.
44
static bool pack_header(unsigned char *forminfo,
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
45
                        List<Create_field> &create_fields,
46
                        uint32_t info_length, uint32_t screens,
47
                        uint32_t table_options,
48
                        ulong data_offset, handler *file);
49
static uint32_t get_interval_id(uint32_t *int_count,
50
                                List<Create_field> &create_fields,
51
                                Create_field *last_field);
1 by brian
clean slate
52
static bool pack_fields(File file, List<Create_field> &create_fields,
53
                        ulong data_offset);
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
54
static bool make_empty_rec(Session *session, int file,
55
                           enum legacy_db_type table_type,
56
                           uint32_t table_options,
57
                           List<Create_field> &create_fields,
58
                           uint32_t reclength, ulong data_offset,
1 by brian
clean slate
59
                           handler *handler);
60
61
/**
62
  An interceptor to hijack ER_TOO_MANY_FIELDS error from
63
  pack_screens and retry again without UNIREG screens.
64
65
  XXX: what is a UNIREG  screen?
66
*/
67
68
struct Pack_header_error_handler: public Internal_error_handler
69
{
482 by Brian Aker
Remove uint.
70
  virtual bool handle_error(uint32_t sql_errno,
1 by brian
clean slate
71
                            const char *message,
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
72
                            DRIZZLE_ERROR::enum_warning_level level,
520.1.22 by Brian Aker
Second pass of thd cleanup
73
                            Session *session);
1 by brian
clean slate
74
  bool is_handled;
163 by Brian Aker
Merge Monty's code.
75
  Pack_header_error_handler() :is_handled(false) {}
1 by brian
clean slate
76
};
77
78
79
bool
80
Pack_header_error_handler::
482 by Brian Aker
Remove uint.
81
handle_error(uint32_t sql_errno,
1 by brian
clean slate
82
             const char * /* message */,
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
83
             DRIZZLE_ERROR::enum_warning_level /* level */,
520.1.22 by Brian Aker
Second pass of thd cleanup
84
             Session * /* session */)
1 by brian
clean slate
85
{
86
  is_handled= (sql_errno == ER_TOO_MANY_FIELDS);
87
  return is_handled;
88
}
89
90
/*
91
  Create a frm (table definition) file
92
93
  SYNOPSIS
94
    mysql_create_frm()
520.1.22 by Brian Aker
Second pass of thd cleanup
95
    session			Thread handler
1 by brian
clean slate
96
    file_name		Path for file (including database and .frm)
97
    db                  Name of database
98
    table               Name of table
99
    create_info		create info parameters
100
    create_fields	Fields to create
101
    keys		number of keys to create
102
    key_info		Keys to create
103
    db_file		Handler to use. May be zero, in which case we use
104
			create_info->db_type
105
  RETURN
106
    0  ok
107
    1  error
108
*/
109
520.1.22 by Brian Aker
Second pass of thd cleanup
110
bool mysql_create_frm(Session *session, const char *file_name,
1 by brian
clean slate
111
                      const char *db, const char *table,
112
		      HA_CREATE_INFO *create_info,
113
		      List<Create_field> &create_fields,
482 by Brian Aker
Remove uint.
114
		      uint32_t keys, KEY *key_info,
1 by brian
clean slate
115
		      handler *db_file)
116
{
117
  LEX_STRING str_db_type;
482 by Brian Aker
Remove uint.
118
  uint32_t reclength, info_length, screens, key_info_length, maxlength, tmp_len;
1 by brian
clean slate
119
  ulong key_buff_length;
120
  File file;
121
  ulong filepos, data_offset;
481 by Brian Aker
Remove all of uchar.
122
  unsigned char fileinfo[64],forminfo[288],*keybuff;
1 by brian
clean slate
123
  TYPELIB formnames;
481 by Brian Aker
Remove all of uchar.
124
  unsigned char *screen_buff;
1 by brian
clean slate
125
  char buff[128];
482 by Brian Aker
Remove uint.
126
  const uint32_t format_section_header_size= 8;
127
  uint32_t format_section_len;
1 by brian
clean slate
128
  Pack_header_error_handler pack_header_error_handler;
129
  int error;
130
51.2.2 by Patrick Galbraith
Removed DBUGs from
131
  assert(*fn_rext((char*)file_name)); // Check .frm extension
1 by brian
clean slate
132
  formnames.type_names=0;
133
  if (!(screen_buff=pack_screens(create_fields,&info_length,&screens,0)))
51.2.2 by Patrick Galbraith
Removed DBUGs from
134
    return(1);
135
  assert(db_file != NULL);
1 by brian
clean slate
136
137
 /* If fixed row records, we need one bit to check for deleted rows */
138
  if (!(create_info->table_options & HA_OPTION_PACK_RECORD))
139
    create_info->null_bits++;
140
  data_offset= (create_info->null_bits + 7) / 8;
141
520.1.22 by Brian Aker
Second pass of thd cleanup
142
  session->push_internal_handler(&pack_header_error_handler);
1 by brian
clean slate
143
411 by Brian Aker
Removed legacy bits around enum.
144
  error= pack_header(forminfo,
1 by brian
clean slate
145
                     create_fields,info_length,
146
                     screens, create_info->table_options,
147
                     data_offset, db_file);
148
520.1.22 by Brian Aker
Second pass of thd cleanup
149
  session->pop_internal_handler();
1 by brian
clean slate
150
151
  if (error)
152
  {
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
153
    free(screen_buff);
1 by brian
clean slate
154
    if (! pack_header_error_handler.is_handled)
51.2.2 by Patrick Galbraith
Removed DBUGs from
155
      return(1);
1 by brian
clean slate
156
157
    // Try again without UNIREG screens (to get more columns)
158
    if (!(screen_buff=pack_screens(create_fields,&info_length,&screens,1)))
51.2.2 by Patrick Galbraith
Removed DBUGs from
159
      return(1);
411 by Brian Aker
Removed legacy bits around enum.
160
    if (pack_header(forminfo,
1 by brian
clean slate
161
                    create_fields,info_length,
162
		    screens, create_info->table_options, data_offset, db_file))
163
    {
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
164
      free(screen_buff);
51.2.2 by Patrick Galbraith
Removed DBUGs from
165
      return(1);
1 by brian
clean slate
166
    }
167
  }
168
  reclength=uint2korr(forminfo+266);
169
170
  /* Calculate extra data segment length */
171
  str_db_type.str= (char *) ha_resolve_storage_engine_name(create_info->db_type);
172
  str_db_type.length= strlen(str_db_type.str);
173
  /* str_db_type */
174
  create_info->extra_size= (2 + str_db_type.length +
175
                            2 + create_info->connect_string.length);
176
  /*
177
    Partition:
178
      Length of partition info = 4 byte
179
      Potential NULL byte at end of partition info string = 1 byte
180
      Indicator if auto-partitioned table = 1 byte
181
      => Total 6 byte
182
  */
183
  create_info->extra_size+= 6;
184
185
  /* Add space for storage type and field format array of fields */
186
  format_section_len=
106 by Brian Aker
Tablespace removal.
187
    format_section_header_size + 1 + create_fields.elements;
1 by brian
clean slate
188
  create_info->extra_size+= format_section_len;
189
190
  tmp_len= system_charset_info->cset->charpos(system_charset_info,
191
                                              create_info->comment.str,
192
                                              create_info->comment.str +
193
                                              create_info->comment.length, 
194
                                              TABLE_COMMENT_MAXLEN);
195
196
  if (tmp_len < create_info->comment.length)
197
  {
198
    my_error(ER_WRONG_STRING_LENGTH, MYF(0),
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
199
             create_info->comment.str,"Table COMMENT",
1 by brian
clean slate
200
             (uint) TABLE_COMMENT_MAXLEN);
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
201
    free(screen_buff);
51.2.2 by Patrick Galbraith
Removed DBUGs from
202
    return(1);
1 by brian
clean slate
203
  }
204
205
  //if table comment is larger than 180 bytes, store into extra segment.
206
  if (create_info->comment.length > 180)
207
  {
208
    forminfo[46]=255;
209
    create_info->extra_size+= 2 + create_info->comment.length;
210
  }
211
  else{
212
    strmake((char*) forminfo+47, create_info->comment.str ?
213
            create_info->comment.str : "", create_info->comment.length);
481 by Brian Aker
Remove all of uchar.
214
    forminfo[46]=(unsigned char) create_info->comment.length;
1 by brian
clean slate
215
#ifdef EXTRA_DEBUG
216
    /*
217
      EXTRA_DEBUG causes strmake() to initialize its buffer behind the
218
      payload with a magic value to detect wrong buffer-sizes. We
219
      explicitly zero that segment again.
220
    */
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
221
    memset(forminfo+47 + forminfo[46], 0, 61 - forminfo[46]);
1 by brian
clean slate
222
#endif
223
  }
224
520.1.22 by Brian Aker
Second pass of thd cleanup
225
  if ((file=create_frm(session, file_name, db, table, reclength, fileinfo,
1 by brian
clean slate
226
		       create_info, keys, key_info)) < 0)
227
  {
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
228
    free(screen_buff);
51.2.2 by Patrick Galbraith
Removed DBUGs from
229
    return(1);
1 by brian
clean slate
230
  }
231
232
  key_buff_length= uint4korr(fileinfo+47);
481 by Brian Aker
Remove all of uchar.
233
  keybuff=(unsigned char*) my_malloc(key_buff_length, MYF(0));
1 by brian
clean slate
234
  key_info_length= pack_keys(keybuff, keys, key_info, data_offset);
398.1.10 by Monty Taylor
Actually removed VOID() this time.
235
  get_form_pos(file,fileinfo,&formnames);
1 by brian
clean slate
236
  if (!(filepos=make_new_entry(file,fileinfo,&formnames,"")))
237
    goto err;
238
  maxlength=(uint) next_io_size((ulong) (uint2korr(forminfo)+1000));
239
  int2store(forminfo+2,maxlength);
240
  int4store(fileinfo+10,(ulong) (filepos+maxlength));
481 by Brian Aker
Remove all of uchar.
241
  fileinfo[26]= (unsigned char) test((create_info->max_rows == 1) &&
1 by brian
clean slate
242
			     (create_info->min_rows == 1) && (keys == 0));
243
  int2store(fileinfo+28,key_info_length);
244
245
246
  int2store(fileinfo+59,db_file->extra_rec_buf_length());
247
31 by Brian Aker
Removed my versions of pread/pwrite from the Kernel
248
  if (pwrite(file, fileinfo, 64, 0L) == 0 ||
249
      pwrite(file, keybuff, key_info_length, (ulong) uint2korr(fileinfo+6)) == 0)
1 by brian
clean slate
250
    goto err;
398.1.10 by Monty Taylor
Actually removed VOID() this time.
251
  my_seek(file,
1 by brian
clean slate
252
	       (ulong) uint2korr(fileinfo+6)+ (ulong) key_buff_length,
398.1.10 by Monty Taylor
Actually removed VOID() this time.
253
	       MY_SEEK_SET,MYF(0));
520.1.22 by Brian Aker
Second pass of thd cleanup
254
  if (make_empty_rec(session,file,ha_legacy_type(create_info->db_type),
1 by brian
clean slate
255
                     create_info->table_options,
256
		     create_fields,reclength, data_offset, db_file))
257
    goto err;
258
259
  int2store(buff, create_info->connect_string.length);
481 by Brian Aker
Remove all of uchar.
260
  if (my_write(file, (const unsigned char*)buff, 2, MYF(MY_NABP)) ||
261
      my_write(file, (const unsigned char*)create_info->connect_string.str,
1 by brian
clean slate
262
               create_info->connect_string.length, MYF(MY_NABP)))
263
      goto err;
264
265
  int2store(buff, str_db_type.length);
481 by Brian Aker
Remove all of uchar.
266
  if (my_write(file, (const unsigned char*)buff, 2, MYF(MY_NABP)) ||
267
      my_write(file, (const unsigned char*)str_db_type.str,
1 by brian
clean slate
268
               str_db_type.length, MYF(MY_NABP)))
269
    goto err;
270
271
  {
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
272
    memset(buff, 0, 6);
481 by Brian Aker
Remove all of uchar.
273
    if (my_write(file, (unsigned char*) buff, 6, MYF_RW))
1 by brian
clean slate
274
      goto err;
275
  }
276
481 by Brian Aker
Remove all of uchar.
277
  if (forminfo[46] == (unsigned char)255)
1 by brian
clean slate
278
  {
481 by Brian Aker
Remove all of uchar.
279
    unsigned char comment_length_buff[2];
1 by brian
clean slate
280
    int2store(comment_length_buff,create_info->comment.length);
281
    if (my_write(file, comment_length_buff, 2, MYF(MY_NABP)) ||
481 by Brian Aker
Remove all of uchar.
282
        my_write(file, (unsigned char*)create_info->comment.str,
1 by brian
clean slate
283
                  create_info->comment.length, MYF(MY_NABP)))
284
      goto err;
285
  }
286
287
  /* Store storage type and field format array of fields */
288
  {
289
    /* prepare header */
290
    {
482 by Brian Aker
Remove uint.
291
      uint32_t flags= 0;
1 by brian
clean slate
292
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
293
      memset(buff, 0, format_section_header_size);
1 by brian
clean slate
294
      /* length of section 2 bytes*/
295
      int2store(buff+0, format_section_len);
296
      /* flags of section 4 bytes*/
297
      int4store(buff+2, flags);
298
      /* 2 bytes left for future use */
299
    }
300
    /* write header */
481 by Brian Aker
Remove all of uchar.
301
    if (my_write(file, (const unsigned char*)buff, format_section_header_size, MYF_RW))
1 by brian
clean slate
302
      goto err;
303
    buff[0]= 0;
481 by Brian Aker
Remove all of uchar.
304
    if (my_write(file, (const unsigned char*)buff, 1, MYF_RW))
1 by brian
clean slate
305
      goto err;
306
    /* write column info, 1 byte per column */
307
    {
308
      List_iterator<Create_field> it(create_fields);
309
      Create_field *field;
481 by Brian Aker
Remove all of uchar.
310
      unsigned char column_format, write_byte;
1 by brian
clean slate
311
      while ((field=it++))
312
      {
481 by Brian Aker
Remove all of uchar.
313
        column_format= (unsigned char)field->column_format();
101 by Brian Aker
Second pass on storage_type.
314
        write_byte= (column_format << COLUMN_FORMAT_SHIFT);
1 by brian
clean slate
315
        if (my_write(file, &write_byte, 1, MYF_RW))
316
          goto err;
317
      }
318
    }
319
  }
398.1.10 by Monty Taylor
Actually removed VOID() this time.
320
  my_seek(file,filepos,MY_SEEK_SET,MYF(0));
1 by brian
clean slate
321
  if (my_write(file, forminfo, 288, MYF_RW) ||
322
      my_write(file, screen_buff, info_length, MYF_RW) ||
323
      pack_fields(file, create_fields, data_offset))
324
    goto err;
325
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
326
  free(screen_buff);
327
  free(keybuff);
1 by brian
clean slate
328
377.1.2 by Brian Aker
Remove random dead variables.
329
  if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
1 by brian
clean slate
330
      (my_sync(file, MYF(MY_WME)) ||
331
       my_sync_dir_by_file(file_name, MYF(MY_WME))))
332
      goto err2;
333
334
  if (my_close(file,MYF(MY_WME)))
335
    goto err3;
336
337
  {
338
    /* 
339
      Restore all UCS2 intervals.
340
      HEX representation of them is not needed anymore.
341
    */
342
    List_iterator<Create_field> it(create_fields);
343
    Create_field *field;
344
    while ((field=it++))
345
    {
346
      if (field->save_interval)
347
      {
348
        field->interval= field->save_interval;
349
        field->save_interval= 0;
350
      }
351
    }
352
  }
51.2.2 by Patrick Galbraith
Removed DBUGs from
353
  return(0);
1 by brian
clean slate
354
355
err:
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
356
  free(screen_buff);
357
  free(keybuff);
1 by brian
clean slate
358
err2:
398.1.10 by Monty Taylor
Actually removed VOID() this time.
359
  my_close(file,MYF(MY_WME));
1 by brian
clean slate
360
err3:
361
  my_delete(file_name,MYF(0));
51.2.2 by Patrick Galbraith
Removed DBUGs from
362
  return(1);
1 by brian
clean slate
363
} /* mysql_create_frm */
364
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
365
static void fill_table_proto(drizzle::Table *table_proto,
366
                             const char *table_name,
367
                             List<Create_field> &create_fields,
590.1.2 by Stewart Smith
store indexes in table definition protobuf
368
                             HA_CREATE_INFO *create_info,
369
                             uint32_t keys,
370
                             KEY *key_info)
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
371
{
372
  Create_field *field_arg;
373
  List_iterator<Create_field> it(create_fields);
374
  drizzle::Table::StorageEngine *engine= table_proto->mutable_engine();
375
  drizzle::Table::TableOptions *table_options= table_proto->mutable_options();
376
377
  engine->set_name(create_info->db_type->name);
378
379
  table_proto->set_name(table_name);
380
  table_proto->set_type(drizzle::Table::STANDARD);
381
382
  while ((field_arg= it++))
383
  {
384
    drizzle::Table::Field *attribute;
385
    //drizzle::Table::Field::FieldConstraints *constraints;
386
387
    attribute= table_proto->add_field();
388
    attribute->set_name(field_arg->field_name);
389
    switch (field_arg->sql_type) {
390
    case DRIZZLE_TYPE_TINY:
391
      attribute->set_type(drizzle::Table::Field::TINYINT);
392
      break;
393
    case DRIZZLE_TYPE_LONG:
394
      attribute->set_type(drizzle::Table::Field::INTEGER);
395
      break;
396
    case DRIZZLE_TYPE_DOUBLE:
397
      attribute->set_type(drizzle::Table::Field::DOUBLE);
398
      break;
399
    case DRIZZLE_TYPE_NULL  :
400
      assert(1); /* Not a user definable type */
401
    case DRIZZLE_TYPE_TIMESTAMP:
402
      attribute->set_type(drizzle::Table::Field::TIMESTAMP);
403
      break;
404
    case DRIZZLE_TYPE_LONGLONG:
405
      attribute->set_type(drizzle::Table::Field::BIGINT);
406
      break;
407
    case DRIZZLE_TYPE_TIME:
408
      attribute->set_type(drizzle::Table::Field::TIME);
409
      break;
410
    case DRIZZLE_TYPE_DATETIME:
411
      attribute->set_type(drizzle::Table::Field::DATETIME);
412
      break;
587 by Brian Aker
Merge stewert
413
    case DRIZZLE_TYPE_DATE:
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
414
      attribute->set_type(drizzle::Table::Field::DATE);
415
      break;
416
    case DRIZZLE_TYPE_VARCHAR:
417
      {
418
        drizzle::Table::Field::StringFieldOptions *string_field_options;
419
420
        string_field_options= attribute->mutable_string_options();
421
        attribute->set_type(drizzle::Table::Field::VARCHAR);
422
        string_field_options->set_length(field_arg->char_length);
423
        string_field_options->set_collation_id(field_arg->charset->number);
424
        string_field_options->set_collation(field_arg->charset->name);
425
426
        break;
427
      }
428
    case DRIZZLE_TYPE_NEWDECIMAL:
429
      {
430
        drizzle::Table::Field::NumericFieldOptions *numeric_field_options;
431
432
        attribute->set_type(drizzle::Table::Field::DECIMAL);
433
        numeric_field_options= attribute->mutable_numeric_options();
434
        /* This is magic, I hate magic numbers -Brian */
435
        numeric_field_options->set_precision(field_arg->length + ( field_arg->decimals ? -2 : -1));
436
        numeric_field_options->set_scale(field_arg->decimals);
437
        break;
438
      }
439
    case DRIZZLE_TYPE_ENUM:
440
      {
441
        drizzle::Table::Field::SetFieldOptions *set_field_options;
442
443
        assert(field_arg->interval);
444
445
        attribute->set_type(drizzle::Table::Field::ENUM);
446
        set_field_options= attribute->mutable_set_options();
447
448
        for (uint32_t pos= 0; pos < field_arg->interval->count; pos++)
449
        {
450
          const char *src= field_arg->interval->type_names[pos];
451
452
          set_field_options->add_value(src);
453
        }
454
        set_field_options->set_count_elements(set_field_options->value_size());
455
        break;
456
      }
457
    case DRIZZLE_TYPE_BLOB:
458
      attribute->set_type(drizzle::Table::Field::BLOB);
459
      break;
460
    default:
461
      assert(1);
462
    }
463
464
#ifdef NOTDONE
465
    field_constraints= attribute->mutable_constraints();
466
    constraints->set_is_nullable(field_arg->def->null_value);
467
#endif
468
469
    /* Set the comment */
470
    if (field_arg->comment.length)
471
      attribute->set_comment(field_arg->comment.str);
472
  }
473
474
  if (create_info->comment.length)
475
    table_options->set_comment(create_info->comment.str);
476
477
  if (create_info->table_charset)
478
  {
479
    table_options->set_collation_id(field_arg->charset->number);
480
    table_options->set_collation(field_arg->charset->name);
481
  }
482
483
  if (create_info->connect_string.length)
484
    table_options->set_connect_string(create_info->connect_string.str);
485
486
  if (create_info->data_file_name)
487
    table_options->set_data_file_name(create_info->data_file_name);
488
489
  if (create_info->index_file_name)
490
    table_options->set_index_file_name(create_info->index_file_name);
491
492
  if (create_info->max_rows)
493
    table_options->set_max_rows(create_info->max_rows);
494
495
  if (create_info->min_rows)
496
    table_options->set_min_rows(create_info->min_rows);
497
498
  if (create_info->auto_increment_value)
499
    table_options->set_auto_increment_value(create_info->auto_increment_value);
500
501
  if (create_info->avg_row_length)
502
    table_options->set_avg_row_length(create_info->avg_row_length);
503
504
  if (create_info->key_block_size)
505
    table_options->set_key_block_size(create_info->key_block_size);
506
507
  if (create_info->block_size)
508
    table_options->set_block_size(create_info->block_size);
590.1.2 by Stewart Smith
store indexes in table definition protobuf
509
510
  for (unsigned int i= 0; i < keys; i++)
511
  {
512
    drizzle::Table::Index *idx;
513
514
    idx= table_proto->add_index();
515
516
    assert(test(key_info[i].flags & HA_USES_COMMENT) ==
517
           (key_info[i].comment.length > 0));
518
519
    idx->set_name(key_info[i].name);
520
521
    if(is_primary_key_name(key_info[i].name))
522
      idx->set_is_primary(true);
523
    else
524
      idx->set_is_primary(false);
525
526
    switch(key_info[i].algorithm)
527
    {
528
    case HA_KEY_ALG_HASH:
529
      idx->set_type(drizzle::Table::Index::HASH);
530
      break;
531
532
    case HA_KEY_ALG_BTREE:
533
      idx->set_type(drizzle::Table::Index::BTREE);
534
      break;
535
536
    case HA_KEY_ALG_RTREE:
537
    case HA_KEY_ALG_FULLTEXT:
538
    case HA_KEY_ALG_UNDEF:
539
      idx->set_type(drizzle::Table::Index::UNKNOWN_INDEX);
540
      break;
541
542
    default:
543
      abort(); /* Somebody's brain broke. haven't added index type to proto */
544
      break;
545
    }
546
547
    if (key_info[i].flags & HA_NOSAME)
548
      idx->set_is_unique(true);
549
    else
550
      idx->set_is_unique(false);
551
552
    /* FIXME: block_size ? */
553
554
    for(unsigned int j=0; j< key_info[i].key_parts; j++)
555
    {
556
      drizzle::Table::Index::IndexPart *idxpart;
557
      drizzle::Table::Field *field;
558
559
      idxpart= idx->add_index_part();
560
561
      field= idxpart->mutable_field();
562
      *field= table_proto->field(key_info[i].key_part[j].fieldnr);
563
564
      idxpart->set_compare_length(key_info[i].key_part[j].length);
565
    }
566
567
    if (key_info[i].flags & HA_USES_COMMENT)
568
      idx->set_comment(key_info[i].comment.str);
569
  }
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
570
}
571
584.2.7 by Stewart Smith
rename and delete tabledefinition protobuf file
572
int rename_table_proto_file(const char *from, const char* to)
573
{
574
  string from_path(from);
575
  string to_path(to);
576
  string file_ext = ".tabledefinition";
577
578
  from_path.append(file_ext);
579
  to_path.append(file_ext);
580
581
  return my_rename(from_path.c_str(),to_path.c_str(),MYF(MY_WME));
582
}
583
584
int delete_table_proto_file(char *file_name)
585
{
586
  string new_path(file_name);
587
  string file_ext = ".tabledefinition";
588
589
  new_path.append(file_ext);
590
  return my_delete(new_path.c_str(), MYF(0));
591
}
592
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
593
int create_table_proto_file(char *file_name,
594
                            const char *table_name,
595
                            HA_CREATE_INFO *create_info,
596
                            List<Create_field> &create_fields,
590.1.2 by Stewart Smith
store indexes in table definition protobuf
597
                            uint32_t keys,
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
598
                            KEY *key_info)
599
{
600
  (void)key_info;
601
602
  drizzle::Table table_proto;
603
  string new_path(file_name);
604
  string file_ext = ".tabledefinition";
605
590.1.2 by Stewart Smith
store indexes in table definition protobuf
606
  fill_table_proto(&table_proto, table_name, create_fields, create_info,
607
                   keys, key_info);
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
608
609
  new_path.replace(new_path.find(".frm"), file_ext.length(), file_ext );
610
611
  fstream output(new_path.c_str(), ios::out | ios::trunc | ios::binary);
612
  if (!table_proto.SerializeToOstream(&output))
613
  {
614
    printf("Failed to write schema.\n");
615
    fprintf(stderr, "Failed to write schema.\n");
616
    return -1;
617
  }
618
619
  return 0;
620
}
1 by brian
clean slate
621
622
/*
623
  Create a frm (table definition) file and the tables
624
625
  SYNOPSIS
626
    rea_create_table()
520.1.22 by Brian Aker
Second pass of thd cleanup
627
    session			Thread handler
1 by brian
clean slate
628
    path		Name of file (including database, without .frm)
629
    db			Data base name
630
    table_name		Table name
631
    create_info		create info parameters
632
    create_fields	Fields to create
633
    keys		number of keys to create
634
    key_info		Keys to create
635
    file		Handler to use
636
637
  RETURN
638
    0  ok
639
    1  error
640
*/
641
520.1.22 by Brian Aker
Second pass of thd cleanup
642
int rea_create_table(Session *session, const char *path,
1 by brian
clean slate
643
                     const char *db, const char *table_name,
644
                     HA_CREATE_INFO *create_info,
645
                     List<Create_field> &create_fields,
482 by Brian Aker
Remove uint.
646
                     uint32_t keys, KEY *key_info, handler *file)
1 by brian
clean slate
647
{
648
  char frm_name[FN_REFLEN];
461 by Monty Taylor
Removed NullS. bu-bye.
649
  strxmov(frm_name, path, reg_ext, NULL);
520.1.22 by Brian Aker
Second pass of thd cleanup
650
  if (mysql_create_frm(session, frm_name, db, table_name, create_info,
1 by brian
clean slate
651
                       create_fields, keys, key_info, file))
652
51.2.2 by Patrick Galbraith
Removed DBUGs from
653
    return(1);
1 by brian
clean slate
654
584.2.5 by Stewart Smith
store a protobuf tabledefinition along with FRM
655
  if (create_table_proto_file(frm_name, table_name, create_info,
656
                              create_fields, keys, key_info) != 0)
657
    return 1;
658
1 by brian
clean slate
659
  // Make sure mysql_create_frm din't remove extension
51.2.2 by Patrick Galbraith
Removed DBUGs from
660
  assert(*fn_rext(frm_name));
520.1.22 by Brian Aker
Second pass of thd cleanup
661
  if (session->variables.keep_files_on_create)
1 by brian
clean slate
662
    create_info->options|= HA_CREATE_KEEP_FILES;
663
  if (file->ha_create_handler_files(path, NULL, CHF_CREATE_FLAG, create_info))
664
    goto err_handler;
590.1.3 by Stewart Smith
remove frm_only create_info option
665
  if ( ha_create_table(session, path, db, table_name,
1 by brian
clean slate
666
                                                create_info,0))
667
    goto err_handler;
51.2.2 by Patrick Galbraith
Removed DBUGs from
668
  return(0);
1 by brian
clean slate
669
670
err_handler:
398.1.10 by Monty Taylor
Actually removed VOID() this time.
671
  file->ha_create_handler_files(path, NULL, CHF_DELETE_FLAG, create_info);
1 by brian
clean slate
672
  my_delete(frm_name, MYF(0));
51.2.2 by Patrick Galbraith
Removed DBUGs from
673
  return(1);
1 by brian
clean slate
674
} /* rea_create_table */
675
676
677
	/* Pack screens to a screen for save in a form-file */
678
481 by Brian Aker
Remove all of uchar.
679
static unsigned char *pack_screens(List<Create_field> &create_fields,
482 by Brian Aker
Remove uint.
680
                           uint32_t *info_length, uint32_t *screens,
1 by brian
clean slate
681
                           bool small_file)
682
{
482 by Brian Aker
Remove uint.
683
  register uint32_t i;
684
  uint32_t row,start_row,end_row,fields_on_screen;
685
  uint32_t length,cols;
481 by Brian Aker
Remove all of uchar.
686
  unsigned char *info,*pos,*start_screen;
482 by Brian Aker
Remove uint.
687
  uint32_t fields=create_fields.elements;
1 by brian
clean slate
688
  List_iterator<Create_field> it(create_fields);
51.2.2 by Patrick Galbraith
Removed DBUGs from
689
  
1 by brian
clean slate
690
691
  start_row=4; end_row=22; cols=80; fields_on_screen=end_row+1-start_row;
692
693
  *screens=(fields-1)/fields_on_screen+1;
694
  length= (*screens) * (SC_INFO_LENGTH+ (cols>> 1)+4);
695
696
  Create_field *field;
697
  while ((field=it++))
698
    length+=(uint) strlen(field->field_name)+1+TE_INFO_LENGTH+cols/2;
699
481 by Brian Aker
Remove all of uchar.
700
  if (!(info=(unsigned char*) my_malloc(length,MYF(MY_WME))))
51.2.2 by Patrick Galbraith
Removed DBUGs from
701
    return(0);
1 by brian
clean slate
702
703
  start_screen=0;
704
  row=end_row;
705
  pos=info;
706
  it.rewind();
707
  for (i=0 ; i < fields ; i++)
708
  {
709
    Create_field *cfield=it++;
710
    if (row++ == end_row)
711
    {
712
      if (i)
713
      {
714
	length=(uint) (pos-start_screen);
715
	int2store(start_screen,length);
481 by Brian Aker
Remove all of uchar.
716
	start_screen[2]=(unsigned char) (fields_on_screen+1);
717
	start_screen[3]=(unsigned char) (fields_on_screen);
1 by brian
clean slate
718
      }
719
      row=start_row;
720
      start_screen=pos;
721
      pos+=4;
481 by Brian Aker
Remove all of uchar.
722
      pos[0]= (unsigned char) start_row-2;	/* Header string */
723
      pos[1]= (unsigned char) (cols >> 2);
724
      pos[2]= (unsigned char) (cols >> 1) +1;
1 by brian
clean slate
725
      strfill((char *) pos+3,(uint) (cols >> 1),' ');
726
      pos+=(cols >> 1)+4;
727
    }
728
    length=(uint) strlen(cfield->field_name);
729
    if (length > cols-3)
730
      length=cols-3;
731
732
    if (!small_file)
733
    {
481 by Brian Aker
Remove all of uchar.
734
      pos[0]=(unsigned char) row;
1 by brian
clean slate
735
      pos[1]=0;
481 by Brian Aker
Remove all of uchar.
736
      pos[2]=(unsigned char) (length+1);
737
      pos=(unsigned char*) strmake((char*) pos+3,cfield->field_name,length)+1;
1 by brian
clean slate
738
    }
206 by Brian Aker
Removed final uint dead types.
739
    cfield->row=(uint8_t) row;
740
    cfield->col=(uint8_t) (length+1);
398.1.4 by Monty Taylor
Renamed max/min.
741
    cfield->sc_length=(uint8_t) cmin(cfield->length,(uint32_t)cols-(length+2));
1 by brian
clean slate
742
  }
743
  length=(uint) (pos-start_screen);
744
  int2store(start_screen,length);
481 by Brian Aker
Remove all of uchar.
745
  start_screen[2]=(unsigned char) (row-start_row+2);
746
  start_screen[3]=(unsigned char) (row-start_row+1);
1 by brian
clean slate
747
748
  *info_length=(uint) (pos-info);
51.2.2 by Patrick Galbraith
Removed DBUGs from
749
  return(info);
1 by brian
clean slate
750
} /* pack_screens */
751
752
753
	/* Pack keyinfo and keynames to keybuff for save in form-file. */
754
482 by Brian Aker
Remove uint.
755
static uint32_t pack_keys(unsigned char *keybuff, uint32_t key_count, KEY *keyinfo,
1 by brian
clean slate
756
                      ulong data_offset)
757
{
482 by Brian Aker
Remove uint.
758
  uint32_t key_parts,length;
481 by Brian Aker
Remove all of uchar.
759
  unsigned char *pos, *keyname_pos;
1 by brian
clean slate
760
  KEY *key,*end;
761
  KEY_PART_INFO *key_part,*key_part_end;
51.2.2 by Patrick Galbraith
Removed DBUGs from
762
  
1 by brian
clean slate
763
764
  pos=keybuff+6;
765
  key_parts=0;
766
  for (key=keyinfo,end=keyinfo+key_count ; key != end ; key++)
767
  {
768
    int2store(pos, (key->flags ^ HA_NOSAME));
769
    int2store(pos+2,key->key_length);
481 by Brian Aker
Remove all of uchar.
770
    pos[4]= (unsigned char) key->key_parts;
771
    pos[5]= (unsigned char) key->algorithm;
1 by brian
clean slate
772
    int2store(pos+6, key->block_size);
773
    pos+=8;
774
    key_parts+=key->key_parts;
775
    for (key_part=key->key_part,key_part_end=key_part+key->key_parts ;
776
	 key_part != key_part_end ;
777
	 key_part++)
778
779
    {
482 by Brian Aker
Remove uint.
780
      uint32_t offset;
1 by brian
clean slate
781
      int2store(pos,key_part->fieldnr+1+FIELD_NAME_USED);
782
      offset= (uint) (key_part->offset+data_offset+1);
783
      int2store(pos+2, offset);
784
      pos[4]=0;					// Sort order
785
      int2store(pos+5,key_part->key_type);
786
      int2store(pos+7,key_part->length);
787
      pos+=9;
788
    }
789
  }
790
	/* Save keynames */
791
  keyname_pos=pos;
481 by Brian Aker
Remove all of uchar.
792
  *pos++=(unsigned char) NAMES_SEP_CHAR;
1 by brian
clean slate
793
  for (key=keyinfo ; key != end ; key++)
794
  {
481 by Brian Aker
Remove all of uchar.
795
    unsigned char *tmp=(unsigned char*) my_stpcpy((char*) pos,key->name);
796
    *tmp++= (unsigned char) NAMES_SEP_CHAR;
1 by brian
clean slate
797
    *tmp=0;
798
    pos=tmp;
799
  }
800
  *(pos++)=0;
801
802
  for (key=keyinfo,end=keyinfo+key_count ; key != end ; key++)
803
  {
804
    if (key->flags & HA_USES_COMMENT)
805
    {
806
      int2store(pos, key->comment.length);
481 by Brian Aker
Remove all of uchar.
807
      unsigned char *tmp= (unsigned char*)my_stpncpy((char*) pos+2,key->comment.str,key->comment.length);
1 by brian
clean slate
808
      pos= tmp;
809
    }
810
  }
811
812
  if (key_count > 127 || key_parts > 127)
813
  {
814
    keybuff[0]= (key_count & 0x7f) | 0x80;
815
    keybuff[1]= key_count >> 7;
816
    int2store(keybuff+2,key_parts);
817
  }
818
  else
819
  {
481 by Brian Aker
Remove all of uchar.
820
    keybuff[0]=(unsigned char) key_count;
821
    keybuff[1]=(unsigned char) key_parts;
1 by brian
clean slate
822
    keybuff[2]= keybuff[3]= 0;
823
  }
824
  length=(uint) (pos-keyname_pos);
825
  int2store(keybuff+4,length);
51.2.2 by Patrick Galbraith
Removed DBUGs from
826
  return((uint) (pos-keybuff));
1 by brian
clean slate
827
} /* pack_keys */
828
829
77.1.45 by Monty Taylor
Warning fixes.
830
/* Make formheader */
1 by brian
clean slate
831
481 by Brian Aker
Remove all of uchar.
832
static bool pack_header(unsigned char *forminfo,
77.1.45 by Monty Taylor
Warning fixes.
833
                        List<Create_field> &create_fields,
482 by Brian Aker
Remove uint.
834
                        uint32_t info_length, uint32_t screens, uint32_t table_options,
1 by brian
clean slate
835
                        ulong data_offset, handler *file)
836
{
482 by Brian Aker
Remove uint.
837
  uint32_t length,int_count,int_length,no_empty, int_parts;
838
  uint32_t time_stamp_pos,null_fields;
383.7.1 by Andrey Zhakov
Initial submit of code and tests
839
  ulong reclength, totlength, n_length, com_length, vcol_info_length;
77.1.45 by Monty Taylor
Warning fixes.
840
1 by brian
clean slate
841
842
  if (create_fields.elements > MAX_FIELDS)
843
  {
844
    my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0));
51.2.2 by Patrick Galbraith
Removed DBUGs from
845
    return(1);
1 by brian
clean slate
846
  }
847
848
  totlength= 0L;
849
  reclength= data_offset;
850
  no_empty=int_count=int_parts=int_length=time_stamp_pos=null_fields=
383.7.1 by Andrey Zhakov
Initial submit of code and tests
851
    com_length=vcol_info_length=0;
1 by brian
clean slate
852
  n_length=2L;
853
854
	/* Check fields */
855
856
  List_iterator<Create_field> it(create_fields);
857
  Create_field *field;
858
  while ((field=it++))
859
  {
482 by Brian Aker
Remove uint.
860
    uint32_t tmp_len= system_charset_info->cset->charpos(system_charset_info,
1 by brian
clean slate
861
                                                     field->comment.str,
862
                                                     field->comment.str +
863
                                                     field->comment.length,
864
                                                     COLUMN_COMMENT_MAXLEN);
865
866
    if (tmp_len < field->comment.length)
867
    {
868
      my_error(ER_WRONG_STRING_LENGTH, MYF(0),
869
               field->comment.str,"COLUMN COMMENT",
870
               (uint) COLUMN_COMMENT_MAXLEN);
51.2.2 by Patrick Galbraith
Removed DBUGs from
871
      return(1);
1 by brian
clean slate
872
    }
383.7.1 by Andrey Zhakov
Initial submit of code and tests
873
    if (field->vcol_info)
874
    {
875
      tmp_len= system_charset_info->cset->charpos(system_charset_info,
876
                                                  field->vcol_info->expr_str.str,
877
                                                  field->vcol_info->expr_str.str +
878
                                                  field->vcol_info->expr_str.length,
879
                                                  VIRTUAL_COLUMN_EXPRESSION_MAXLEN);
880
881
      if (tmp_len < field->vcol_info->expr_str.length)
882
      {
883
        my_error(ER_WRONG_STRING_LENGTH, MYF(0),
884
                 field->vcol_info->expr_str.str,"VIRTUAL COLUMN EXPRESSION",
885
                 (uint) VIRTUAL_COLUMN_EXPRESSION_MAXLEN);
886
        return(1);
887
      }
888
      /*
889
        Sum up the length of the expression string and mandatory header bytes
890
        to the total length.
891
      */
892
      vcol_info_length+= field->vcol_info->expr_str.length+(uint)FRM_VCOL_HEADER_SIZE;
893
    }
1 by brian
clean slate
894
895
    totlength+= field->length;
896
    com_length+= field->comment.length;
897
    if (MTYP_TYPENR(field->unireg_check) == Field::NOEMPTY ||
898
	field->unireg_check & MTYP_NOEMPTY_BIT)
899
    {
900
      field->unireg_check= (Field::utype) ((uint) field->unireg_check |
901
					   MTYP_NOEMPTY_BIT);
902
      no_empty++;
903
    }
904
    /* 
905
      We mark first TIMESTAMP field with NOW() in DEFAULT or ON UPDATE 
906
      as auto-update field.
907
    */
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
908
    if (field->sql_type == DRIZZLE_TYPE_TIMESTAMP &&
1 by brian
clean slate
909
        MTYP_TYPENR(field->unireg_check) != Field::NONE &&
910
	!time_stamp_pos)
911
      time_stamp_pos= (uint) field->offset+ (uint) data_offset + 1;
912
    length=field->pack_length;
913
    if ((uint) field->offset+ (uint) data_offset+ length > reclength)
914
      reclength=(uint) (field->offset+ data_offset + length);
915
    n_length+= (ulong) strlen(field->field_name)+1;
916
    field->interval_id=0;
917
    field->save_interval= 0;
918
    if (field->interval)
919
    {
482 by Brian Aker
Remove uint.
920
      uint32_t old_int_count=int_count;
1 by brian
clean slate
921
922
      if (field->charset->mbminlen > 1)
923
      {
924
        /* 
925
          Escape UCS2 intervals using HEX notation to avoid
926
          problems with delimiters between enum elements.
927
          As the original representation is still needed in 
928
          the function make_empty_rec to create a record of
929
          filled with default values it is saved in save_interval
930
          The HEX representation is created from this copy.
931
        */
932
        field->save_interval= field->interval;
933
        field->interval= (TYPELIB*) sql_alloc(sizeof(TYPELIB));
934
        *field->interval= *field->save_interval; 
935
        field->interval->type_names= 
936
          (const char **) sql_alloc(sizeof(char*) * 
937
				    (field->interval->count+1));
938
        field->interval->type_names[field->interval->count]= 0;
939
        field->interval->type_lengths=
482 by Brian Aker
Remove uint.
940
          (uint32_t *) sql_alloc(sizeof(uint) * field->interval->count);
1 by brian
clean slate
941
 
482 by Brian Aker
Remove uint.
942
        for (uint32_t pos= 0; pos < field->interval->count; pos++)
1 by brian
clean slate
943
        {
944
          char *dst;
945
          const char *src= field->save_interval->type_names[pos];
482 by Brian Aker
Remove uint.
946
          uint32_t hex_length;
1 by brian
clean slate
947
          length= field->save_interval->type_lengths[pos];
948
          hex_length= length * 2;
949
          field->interval->type_lengths[pos]= hex_length;
950
          field->interval->type_names[pos]= dst= (char*) sql_alloc(hex_length +
951
                                                                   1);
952
          octet2hex(dst, src, length);
953
        }
954
      }
955
956
      field->interval_id=get_interval_id(&int_count,create_fields,field);
957
      if (old_int_count != int_count)
958
      {
959
	for (const char **pos=field->interval->type_names ; *pos ; pos++)
960
	  int_length+=(uint) strlen(*pos)+1;	// field + suffix prefix
961
	int_parts+=field->interval->count+1;
962
      }
963
    }
964
    if (f_maybe_null(field->pack_flag))
965
      null_fields++;
966
  }
967
  int_length+=int_count*2;			// 255 prefix + 0 suffix
968
969
	/* Save values in forminfo */
970
971
  if (reclength > (ulong) file->max_record_length())
972
  {
973
    my_error(ER_TOO_BIG_ROWSIZE, MYF(0), (uint) file->max_record_length());
51.2.2 by Patrick Galbraith
Removed DBUGs from
974
    return(1);
1 by brian
clean slate
975
  }
976
  /* Hack to avoid bugs with small static rows in MySQL */
398.1.4 by Monty Taylor
Renamed max/min.
977
  reclength=cmax((ulong)file->min_record_length(table_options),reclength);
1 by brian
clean slate
978
  if (info_length+(ulong) create_fields.elements*FCOMP+288+
383.7.1 by Andrey Zhakov
Initial submit of code and tests
979
      n_length+int_length+com_length+vcol_info_length > 65535L || 
980
      int_count > 255)
1 by brian
clean slate
981
  {
982
    my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0));
51.2.2 by Patrick Galbraith
Removed DBUGs from
983
    return(1);
1 by brian
clean slate
984
  }
985
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
986
  memset(forminfo, 0, 288);
1 by brian
clean slate
987
  length=(info_length+create_fields.elements*FCOMP+288+n_length+int_length+
383.7.1 by Andrey Zhakov
Initial submit of code and tests
988
	  com_length+vcol_info_length);
1 by brian
clean slate
989
  int2store(forminfo,length);
206 by Brian Aker
Removed final uint dead types.
990
  forminfo[256] = (uint8_t) screens;
1 by brian
clean slate
991
  int2store(forminfo+258,create_fields.elements);
992
  int2store(forminfo+260,info_length);
993
  int2store(forminfo+262,totlength);
994
  int2store(forminfo+264,no_empty);
995
  int2store(forminfo+266,reclength);
996
  int2store(forminfo+268,n_length);
997
  int2store(forminfo+270,int_count);
998
  int2store(forminfo+272,int_parts);
999
  int2store(forminfo+274,int_length);
1000
  int2store(forminfo+276,time_stamp_pos);
1001
  int2store(forminfo+278,80);			/* Columns needed */
1002
  int2store(forminfo+280,22);			/* Rows needed */
1003
  int2store(forminfo+282,null_fields);
1004
  int2store(forminfo+284,com_length);
383.7.1 by Andrey Zhakov
Initial submit of code and tests
1005
  int2store(forminfo+286,vcol_info_length);
1006
  /* forminfo+288 is free to use for additional information */
51.2.2 by Patrick Galbraith
Removed DBUGs from
1007
  return(0);
1 by brian
clean slate
1008
} /* pack_header */
1009
1010
1011
	/* get each unique interval each own id */
1012
482 by Brian Aker
Remove uint.
1013
static uint32_t get_interval_id(uint32_t *int_count,List<Create_field> &create_fields,
1 by brian
clean slate
1014
			    Create_field *last_field)
1015
{
1016
  List_iterator<Create_field> it(create_fields);
1017
  Create_field *field;
1018
  TYPELIB *interval=last_field->interval;
1019
1020
  while ((field=it++) != last_field)
1021
  {
1022
    if (field->interval_id && field->interval->count == interval->count)
1023
    {
1024
      const char **a,**b;
1025
      for (a=field->interval->type_names, b=interval->type_names ;
1026
	   *a && !strcmp(*a,*b);
1027
	   a++,b++) ;
1028
1029
      if (! *a)
1030
      {
1031
	return field->interval_id;		// Re-use last interval
1032
      }
1033
    }
1034
  }
1035
  return ++*int_count;				// New unique interval
1036
}
1037
1038
1039
	/* Save fields, fieldnames and intervals */
1040
1041
static bool pack_fields(File file, List<Create_field> &create_fields,
1042
                        ulong data_offset)
1043
{
482 by Brian Aker
Remove uint.
1044
  register uint32_t i;
481.3.1 by Monty Taylor
Merged vcol stuff.
1045
  uint32_t int_count, comment_length=0, vcol_info_length=0;
481 by Brian Aker
Remove all of uchar.
1046
  unsigned char buff[MAX_FIELD_WIDTH];
1 by brian
clean slate
1047
  Create_field *field;
51.2.2 by Patrick Galbraith
Removed DBUGs from
1048
  
1 by brian
clean slate
1049
1050
	/* Write field info */
1051
1052
  List_iterator<Create_field> it(create_fields);
1053
1054
  int_count=0;
1055
  while ((field=it++))
1056
  {
482 by Brian Aker
Remove uint.
1057
    uint32_t recpos;
481.3.1 by Monty Taylor
Merged vcol stuff.
1058
    uint32_t cur_vcol_expr_len= 0;
481 by Brian Aker
Remove all of uchar.
1059
    buff[0]= (unsigned char) field->row;
1060
    buff[1]= (unsigned char) field->col;
1061
    buff[2]= (unsigned char) field->sc_length;
1 by brian
clean slate
1062
    int2store(buff+3, field->length);
1063
    /* The +1 is here becasue the col offset in .frm file have offset 1 */
1064
    recpos= field->offset+1 + (uint) data_offset;
1065
    int3store(buff+5,recpos);
1066
    int2store(buff+8,field->pack_flag);
1067
    int2store(buff+10,field->unireg_check);
481 by Brian Aker
Remove all of uchar.
1068
    buff[12]= (unsigned char) field->interval_id;
1069
    buff[13]= (unsigned char) field->sql_type; 
1 by brian
clean slate
1070
    if (field->charset) 
481 by Brian Aker
Remove all of uchar.
1071
      buff[14]= (unsigned char) field->charset->number;
1 by brian
clean slate
1072
    else
1073
      buff[14]= 0;				// Numerical
383.7.1 by Andrey Zhakov
Initial submit of code and tests
1074
    if (field->vcol_info)
1075
    {
1076
      /* 
1077
        Use the interval_id place in the .frm file to store the length of
1078
        virtual field's data.
1079
      */
1080
      buff[12]= cur_vcol_expr_len= field->vcol_info->expr_str.length +
1081
                (uint)FRM_VCOL_HEADER_SIZE;
1082
      vcol_info_length+= cur_vcol_expr_len+(uint)FRM_VCOL_HEADER_SIZE;
481.3.1 by Monty Taylor
Merged vcol stuff.
1083
      buff[13]= (unsigned char) DRIZZLE_TYPE_VIRTUAL;
383.7.1 by Andrey Zhakov
Initial submit of code and tests
1084
    }
1 by brian
clean slate
1085
    int2store(buff+15, field->comment.length);
1086
    comment_length+= field->comment.length;
1087
    set_if_bigger(int_count,field->interval_id);
1088
    if (my_write(file, buff, FCOMP, MYF_RW))
51.2.2 by Patrick Galbraith
Removed DBUGs from
1089
      return(1);
1 by brian
clean slate
1090
  }
1091
1092
	/* Write fieldnames */
481 by Brian Aker
Remove all of uchar.
1093
  buff[0]=(unsigned char) NAMES_SEP_CHAR;
1 by brian
clean slate
1094
  if (my_write(file, buff, 1, MYF_RW))
51.2.2 by Patrick Galbraith
Removed DBUGs from
1095
    return(1);
1 by brian
clean slate
1096
  i=0;
1097
  it.rewind();
1098
  while ((field=it++))
1099
  {
411.1.1 by Brian Aker
Work on removing GNU specific calls.
1100
    char *pos= my_stpcpy((char*) buff,field->field_name);
1 by brian
clean slate
1101
    *pos++=NAMES_SEP_CHAR;
1102
    if (i == create_fields.elements-1)
1103
      *pos++=0;
1104
    if (my_write(file, buff, (size_t) (pos-(char*) buff),MYF_RW))
51.2.2 by Patrick Galbraith
Removed DBUGs from
1105
      return(1);
1 by brian
clean slate
1106
    i++;
1107
  }
1108
1109
	/* Write intervals */
1110
  if (int_count)
1111
  {
1112
    String tmp((char*) buff,sizeof(buff), &my_charset_bin);
1113
    tmp.length(0);
1114
    it.rewind();
1115
    int_count=0;
1116
    while ((field=it++))
1117
    {
1118
      if (field->interval_id > int_count)
1119
      {
1120
        unsigned char  sep= 0;
1121
        unsigned char  occ[256];
482 by Brian Aker
Remove uint.
1122
        uint32_t           i;
1 by brian
clean slate
1123
        unsigned char *val= NULL;
1124
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
1125
        memset(occ, 0, sizeof(occ));
1 by brian
clean slate
1126
1127
        for (i=0; (val= (unsigned char*) field->interval->type_names[i]); i++)
482 by Brian Aker
Remove uint.
1128
          for (uint32_t j = 0; j < field->interval->type_lengths[i]; j++)
1 by brian
clean slate
1129
            occ[(unsigned int) (val[j])]= 1;
1130
1131
        if (!occ[(unsigned char)NAMES_SEP_CHAR])
1132
          sep= (unsigned char) NAMES_SEP_CHAR;
1133
        else if (!occ[(unsigned int)','])
1134
          sep= ',';
1135
        else
1136
        {
482 by Brian Aker
Remove uint.
1137
          for (uint32_t i=1; i<256; i++)
1 by brian
clean slate
1138
          {
1139
            if(!occ[i])
1140
            {
1141
              sep= i;
1142
              break;
1143
            }
1144
          }
1145
1146
          if(!sep)    /* disaster, enum uses all characters, none left as separator */
1147
          {
1148
            my_message(ER_WRONG_FIELD_TERMINATORS,ER(ER_WRONG_FIELD_TERMINATORS),
1149
                       MYF(0));
51.2.2 by Patrick Galbraith
Removed DBUGs from
1150
            return(1);
1 by brian
clean slate
1151
          }
1152
        }
1153
1154
        int_count= field->interval_id;
1155
        tmp.append(sep);
1156
        for (const char **pos=field->interval->type_names ; *pos ; pos++)
1157
        {
1158
          tmp.append(*pos);
1159
          tmp.append(sep);
1160
        }
1161
        tmp.append('\0');                      // End of intervall
1162
      }
1163
    }
481 by Brian Aker
Remove all of uchar.
1164
    if (my_write(file,(unsigned char*) tmp.ptr(),tmp.length(),MYF_RW))
51.2.2 by Patrick Galbraith
Removed DBUGs from
1165
      return(1);
1 by brian
clean slate
1166
  }
1167
  if (comment_length)
1168
  {
1169
    it.rewind();
1170
    int_count=0;
1171
    while ((field=it++))
1172
    {
1173
      if (field->comment.length)
481 by Brian Aker
Remove all of uchar.
1174
	if (my_write(file, (unsigned char*) field->comment.str, field->comment.length,
1 by brian
clean slate
1175
		     MYF_RW))
51.2.2 by Patrick Galbraith
Removed DBUGs from
1176
	  return(1);
1 by brian
clean slate
1177
    }
1178
  }
383.7.1 by Andrey Zhakov
Initial submit of code and tests
1179
  if (vcol_info_length)
1180
  {
1181
    it.rewind();
1182
    int_count=0;
1183
    while ((field=it++))
1184
    {
1185
      /*
1186
        Pack each virtual field as follows:
1187
        byte 1      = 1 (always 1 to allow for future extensions)
1188
        byte 2      = sql_type
1189
        byte 3      = flags (as of now, 0 - no flags, 1 - field is physically stored)
1190
        byte 4-...  = virtual column expression (text data)
1191
      */
1192
      if (field->vcol_info && field->vcol_info->expr_str.length)
1193
      {
481.3.1 by Monty Taylor
Merged vcol stuff.
1194
        buff[0]= (unsigned char)1;
1195
        buff[1]= (unsigned char) field->sql_type;
1196
        buff[2]= (unsigned char) field->is_stored;
383.7.1 by Andrey Zhakov
Initial submit of code and tests
1197
        if (my_write(file, buff, 3, MYF_RW))
1198
          return(1);
1199
        if (my_write(file, 
481.3.1 by Monty Taylor
Merged vcol stuff.
1200
                     (unsigned char*) field->vcol_info->expr_str.str, 
383.7.1 by Andrey Zhakov
Initial submit of code and tests
1201
                     field->vcol_info->expr_str.length,
1202
                     MYF_RW))
1203
          return(1);
1204
      }
1205
    }
1206
  }
51.2.2 by Patrick Galbraith
Removed DBUGs from
1207
  return(0);
1 by brian
clean slate
1208
}
1209
1210
77.1.45 by Monty Taylor
Warning fixes.
1211
/* save an empty record on start of formfile */
1 by brian
clean slate
1212
520.1.22 by Brian Aker
Second pass of thd cleanup
1213
static bool make_empty_rec(Session *session, File file,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1214
                           enum legacy_db_type table_type __attribute__((unused)),
482 by Brian Aker
Remove uint.
1215
                           uint32_t table_options,
77.1.45 by Monty Taylor
Warning fixes.
1216
                           List<Create_field> &create_fields,
482 by Brian Aker
Remove uint.
1217
                           uint32_t reclength,
1 by brian
clean slate
1218
                           ulong data_offset,
1219
                           handler *handler)
1220
{
1221
  int error= 0;
1222
  Field::utype type;
482 by Brian Aker
Remove uint.
1223
  uint32_t null_count;
481 by Brian Aker
Remove all of uchar.
1224
  unsigned char *buff,*null_pos;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1225
  Table table;
1 by brian
clean slate
1226
  TABLE_SHARE share;
1227
  Create_field *field;
520.1.22 by Brian Aker
Second pass of thd cleanup
1228
  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
51.2.2 by Patrick Galbraith
Removed DBUGs from
1229
  
1 by brian
clean slate
1230
1231
  /* We need a table to generate columns for default values */
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
1232
  memset(&table, 0, sizeof(table));
1233
  memset(&share, 0, sizeof(share));
1 by brian
clean slate
1234
  table.s= &share;
1235
481 by Brian Aker
Remove all of uchar.
1236
  if (!(buff=(unsigned char*) my_malloc((size_t) reclength,MYF(MY_WME | MY_ZEROFILL))))
1 by brian
clean slate
1237
  {
51.2.2 by Patrick Galbraith
Removed DBUGs from
1238
    return(1);
1 by brian
clean slate
1239
  }
1240
520.1.22 by Brian Aker
Second pass of thd cleanup
1241
  table.in_use= session;
1 by brian
clean slate
1242
  table.s->db_low_byte_first= handler->low_byte_first();
1243
  table.s->blob_ptr_size= portable_sizeof_char_ptr;
1244
1245
  null_count=0;
1246
  if (!(table_options & HA_OPTION_PACK_RECORD))
1247
  {
1248
    null_count++;			// Need one bit for delete mark
1249
    *buff|= 1;
1250
  }
1251
  null_pos= buff;
1252
1253
  List_iterator<Create_field> it(create_fields);
520.1.22 by Brian Aker
Second pass of thd cleanup
1254
  session->count_cuted_fields= CHECK_FIELD_WARN;    // To find wrong default values
1 by brian
clean slate
1255
  while ((field=it++))
1256
  {
1257
    /*
1258
      regfield don't have to be deleted as it's allocated with sql_alloc()
1259
    */
1260
    Field *regfield= make_field(&share,
1261
                                buff+field->offset + data_offset,
1262
                                field->length,
1263
                                null_pos + null_count / 8,
1264
                                null_count & 7,
1265
                                field->pack_flag,
1266
                                field->sql_type,
1267
                                field->charset,
1268
                                field->unireg_check,
1269
                                field->save_interval ? field->save_interval :
1270
                                field->interval, 
1271
                                field->field_name);
1272
    if (!regfield)
1273
    {
1274
      error= 1;
1275
      goto err;                                 // End of memory
1276
    }
1277
1278
    /* save_in_field() will access regfield->table->in_use */
1279
    regfield->init(&table);
1280
1281
    if (!(field->flags & NOT_NULL_FLAG))
1282
    {
1283
      *regfield->null_ptr|= regfield->null_bit;
1284
      null_count++;
1285
    }
1286
1287
    type= (Field::utype) MTYP_TYPENR(field->unireg_check);
1288
1289
    if (field->def)
1290
    {
1291
      int res= field->def->save_in_field(regfield, 1);
1292
      /* If not ok or warning of level 'note' */
1293
      if (res != 0 && res != 3)
1294
      {
1295
        my_error(ER_INVALID_DEFAULT, MYF(0), regfield->field_name);
1296
        error= 1;
1297
        delete regfield; //To avoid memory leak
1298
        goto err;
1299
      }
1300
    }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1301
    else if (regfield->real_type() == DRIZZLE_TYPE_ENUM &&
1 by brian
clean slate
1302
	     (field->flags & NOT_NULL_FLAG))
1303
    {
1304
      regfield->set_notnull();
163 by Brian Aker
Merge Monty's code.
1305
      regfield->store((int64_t) 1, true);
1 by brian
clean slate
1306
    }
1307
    else if (type == Field::YES)		// Old unireg type
1308
      regfield->store(ER(ER_YES),(uint) strlen(ER(ER_YES)),system_charset_info);
1309
    else if (type == Field::NO)			// Old unireg type
1310
      regfield->store(ER(ER_NO), (uint) strlen(ER(ER_NO)),system_charset_info);
1311
    else
1312
      regfield->reset();
1313
  }
51.2.2 by Patrick Galbraith
Removed DBUGs from
1314
  assert(data_offset == ((null_count + 7) / 8));
1 by brian
clean slate
1315
1316
  /*
1317
    We need to set the unused bits to 1. If the number of bits is a multiple
1318
    of 8 there are no unused bits.
1319
  */
1320
  if (null_count & 7)
481 by Brian Aker
Remove all of uchar.
1321
    *(null_pos + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1 by brian
clean slate
1322
1323
  error= my_write(file, buff, (size_t) reclength,MYF_RW) != 0;
1324
1325
err:
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
1326
  free(buff);
520.1.22 by Brian Aker
Second pass of thd cleanup
1327
  session->count_cuted_fields= old_count_cuted_fields;
51.2.2 by Patrick Galbraith
Removed DBUGs from
1328
  return(error);
1 by brian
clean slate
1329
} /* make_empty_rec */