~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_proto_write.cc

  • Committer: Brian Aker
  • Date: 2009-07-11 19:23:04 UTC
  • mfrom: (1089.1.14 merge)
  • Revision ID: brian@gaz-20090711192304-ootijyl5yf9jq9kd
Merge Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
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
 
 
26
16
#include <drizzled/server_includes.h>
27
 
#include <drizzled/drizzled_error_messages.h>
28
 
 
29
 
#define FCOMP                   17              /* Bytes for a packed field */
30
 
 
31
 
static uchar * pack_screens(List<Create_field> &create_fields,
32
 
                            uint *info_length, uint *screens, bool small_file);
33
 
static uint pack_keys(uchar *keybuff,uint key_count, KEY *key_info,
34
 
                      ulong data_offset);
35
 
static bool pack_header(uchar *forminfo,enum legacy_db_type table_type,
36
 
                        List<Create_field> &create_fields,
37
 
                        uint info_length, uint screens, uint table_options,
38
 
                        ulong data_offset, handler *file);
39
 
static uint get_interval_id(uint *int_count,List<Create_field> &create_fields,
40
 
                            Create_field *last_field);
41
 
static bool pack_fields(File file, List<Create_field> &create_fields,
42
 
                        ulong data_offset);
43
 
static bool make_empty_rec(THD *thd, int file, enum legacy_db_type table_type,
44
 
                           uint table_options,
45
 
                           List<Create_field> &create_fields,
46
 
                           uint reclength, ulong data_offset,
47
 
                           handler *handler);
48
 
 
49
 
/**
50
 
  An interceptor to hijack ER_TOO_MANY_FIELDS error from
51
 
  pack_screens and retry again without UNIREG screens.
52
 
 
53
 
  XXX: what is a UNIREG  screen?
54
 
*/
55
 
 
56
 
struct Pack_header_error_handler: public Internal_error_handler
57
 
{
58
 
  virtual bool handle_error(uint sql_errno,
59
 
                            const char *message,
60
 
                            DRIZZLE_ERROR::enum_warning_level level,
61
 
                            THD *thd);
62
 
  bool is_handled;
63
 
  Pack_header_error_handler() :is_handled(false) {}
64
 
};
65
 
 
66
 
 
67
 
bool
68
 
Pack_header_error_handler::
69
 
handle_error(uint sql_errno,
70
 
             const char * /* message */,
71
 
             DRIZZLE_ERROR::enum_warning_level /* level */,
72
 
             THD * /* thd */)
73
 
{
74
 
  is_handled= (sql_errno == ER_TOO_MANY_FIELDS);
75
 
  return is_handled;
 
17
#include <drizzled/error.h>
 
18
#include <drizzled/session.h>
 
19
#include <drizzled/unireg.h>
 
20
 
 
21
/* For proto */
 
22
#include <string>
 
23
#include <fstream>
 
24
#include <drizzled/message/schema.pb.h>
 
25
#include <drizzled/message/table.pb.h>
 
26
#include <google/protobuf/io/zero_copy_stream.h>
 
27
#include <google/protobuf/io/zero_copy_stream_impl.h>
 
28
using namespace std;
 
29
 
 
30
int drizzle_read_table_proto(const char* path, drizzled::message::Table* table)
 
31
{
 
32
  int fd= open(path, O_RDONLY);
 
33
 
 
34
  if(fd==-1)
 
35
    return errno;
 
36
 
 
37
  google::protobuf::io::ZeroCopyInputStream* input=
 
38
    new google::protobuf::io::FileInputStream(fd);
 
39
 
 
40
  if (!table->ParseFromZeroCopyStream(input))
 
41
  {
 
42
    delete input;
 
43
    close(fd);
 
44
    return -1;
 
45
  }
 
46
 
 
47
  delete input;
 
48
  close(fd);
 
49
  return 0;
76
50
}
77
51
 
78
 
/*
79
 
  Create a frm (table definition) file
80
 
 
81
 
  SYNOPSIS
82
 
    mysql_create_frm()
83
 
    thd                 Thread handler
84
 
    file_name           Path for file (including database and .frm)
85
 
    db                  Name of database
86
 
    table               Name of table
87
 
    create_info         create info parameters
88
 
    create_fields       Fields to create
89
 
    keys                number of keys to create
90
 
    key_info            Keys to create
91
 
    db_file             Handler to use. May be zero, in which case we use
92
 
                        create_info->db_type
93
 
  RETURN
94
 
    0  ok
95
 
    1  error
96
 
*/
97
 
 
98
 
bool mysql_create_frm(THD *thd, const char *file_name,
99
 
                      const char *db, const char *table,
100
 
                      HA_CREATE_INFO *create_info,
101
 
                      List<Create_field> &create_fields,
102
 
                      uint keys, KEY *key_info,
103
 
                      handler *db_file)
 
52
static int fill_table_proto(drizzled::message::Table *table_proto,
 
53
                            const char *table_name,
 
54
                            List<CreateField> &create_fields,
 
55
                            HA_CREATE_INFO *create_info,
 
56
                            uint32_t keys,
 
57
                            KEY *key_info)
104
58
{
105
 
  LEX_STRING str_db_type;
106
 
  uint reclength, info_length, screens, key_info_length, maxlength, tmp_len;
107
 
  ulong key_buff_length;
108
 
  File file;
109
 
  ulong filepos, data_offset;
110
 
  uchar fileinfo[64],forminfo[288],*keybuff;
111
 
  TYPELIB formnames;
112
 
  uchar *screen_buff;
113
 
  char buff[128];
114
 
  const uint format_section_header_size= 8;
115
 
  uint format_section_len;
116
 
  Pack_header_error_handler pack_header_error_handler;
117
 
  int error;
 
59
  CreateField *field_arg;
 
60
  List_iterator<CreateField> it(create_fields);
 
61
  drizzled::message::Table::TableOptions *table_options= table_proto->mutable_options();
118
62
 
119
 
  assert(*fn_rext((char*)file_name)); // Check .frm extension
120
 
  formnames.type_names=0;
121
 
  if (!(screen_buff=pack_screens(create_fields,&info_length,&screens,0)))
 
63
  if (create_fields.elements > MAX_FIELDS)
 
64
  {
 
65
    my_error(ER_TOO_MANY_FIELDS, MYF(0), ER(ER_TOO_MANY_FIELDS));
122
66
    return(1);
123
 
  assert(db_file != NULL);
124
 
 
125
 
 /* If fixed row records, we need one bit to check for deleted rows */
126
 
  if (!(create_info->table_options & HA_OPTION_PACK_RECORD))
127
 
    create_info->null_bits++;
128
 
  data_offset= (create_info->null_bits + 7) / 8;
129
 
 
130
 
  thd->push_internal_handler(&pack_header_error_handler);
131
 
 
132
 
  error= pack_header(forminfo, ha_legacy_type(create_info->db_type),
133
 
                     create_fields,info_length,
134
 
                     screens, create_info->table_options,
135
 
                     data_offset, db_file);
136
 
 
137
 
  thd->pop_internal_handler();
138
 
 
139
 
  if (error)
 
67
  }
 
68
 
 
69
  assert(strcmp(table_proto->engine().name().c_str(),
 
70
                create_info->db_type->getName().c_str())==0);
 
71
 
 
72
  assert(strcmp(table_proto->name().c_str(),table_name)==0);
 
73
 
 
74
  while ((field_arg= it++))
140
75
  {
141
 
    my_free(screen_buff, MYF(0));
142
 
    if (! pack_header_error_handler.is_handled)
143
 
      return(1);
144
 
 
145
 
    // Try again without UNIREG screens (to get more columns)
146
 
    if (!(screen_buff=pack_screens(create_fields,&info_length,&screens,1)))
147
 
      return(1);
148
 
    if (pack_header(forminfo, ha_legacy_type(create_info->db_type),
149
 
                    create_fields,info_length,
150
 
                    screens, create_info->table_options, data_offset, db_file))
 
76
    drizzled::message::Table::Field *attribute;
 
77
 
 
78
    attribute= table_proto->add_field();
 
79
    attribute->set_name(field_arg->field_name);
 
80
 
 
81
    attribute->set_pack_flag(field_arg->pack_flag); /* TODO: MUST DIE */
 
82
 
 
83
    if(f_maybe_null(field_arg->pack_flag))
151
84
    {
152
 
      my_free(screen_buff, MYF(0));
153
 
      return(1);
154
 
    }
155
 
  }
156
 
  reclength=uint2korr(forminfo+266);
157
 
 
158
 
  /* Calculate extra data segment length */
159
 
  str_db_type.str= (char *) ha_resolve_storage_engine_name(create_info->db_type);
160
 
  str_db_type.length= strlen(str_db_type.str);
161
 
  /* str_db_type */
162
 
  create_info->extra_size= (2 + str_db_type.length +
163
 
                            2 + create_info->connect_string.length);
164
 
  /*
165
 
    Partition:
166
 
      Length of partition info = 4 byte
167
 
      Potential NULL byte at end of partition info string = 1 byte
168
 
      Indicator if auto-partitioned table = 1 byte
169
 
      => Total 6 byte
170
 
  */
171
 
  create_info->extra_size+= 6;
172
 
 
173
 
  /* Add space for storage type and field format array of fields */
174
 
  format_section_len=
175
 
    format_section_header_size + 1 + create_fields.elements;
176
 
  create_info->extra_size+= format_section_len;
177
 
 
178
 
  tmp_len= system_charset_info->cset->charpos(system_charset_info,
179
 
                                              create_info->comment.str,
180
 
                                              create_info->comment.str +
181
 
                                              create_info->comment.length, 
182
 
                                              TABLE_COMMENT_MAXLEN);
183
 
 
184
 
  if (tmp_len < create_info->comment.length)
185
 
  {
186
 
    my_error(ER_WRONG_STRING_LENGTH, MYF(0),
187
 
             create_info->comment.str,"TABLE COMMENT",
188
 
             (uint) TABLE_COMMENT_MAXLEN);
189
 
    my_free(screen_buff,MYF(0));
190
 
    return(1);
191
 
  }
192
 
 
193
 
  //if table comment is larger than 180 bytes, store into extra segment.
194
 
  if (create_info->comment.length > 180)
195
 
  {
196
 
    forminfo[46]=255;
197
 
    create_info->extra_size+= 2 + create_info->comment.length;
198
 
  }
199
 
  else{
200
 
    strmake((char*) forminfo+47, create_info->comment.str ?
201
 
            create_info->comment.str : "", create_info->comment.length);
202
 
    forminfo[46]=(uchar) create_info->comment.length;
203
 
#ifdef EXTRA_DEBUG
204
 
    /*
205
 
      EXTRA_DEBUG causes strmake() to initialize its buffer behind the
206
 
      payload with a magic value to detect wrong buffer-sizes. We
207
 
      explicitly zero that segment again.
208
 
    */
209
 
    memset(forminfo+47 + forminfo[46], 0, 61 - forminfo[46]);
 
85
      drizzled::message::Table::Field::FieldConstraints *constraints;
 
86
 
 
87
      constraints= attribute->mutable_constraints();
 
88
      constraints->set_is_nullable(true);
 
89
    }
 
90
 
 
91
    switch (field_arg->sql_type) {
 
92
    case DRIZZLE_TYPE_TINY:
 
93
      attribute->set_type(drizzled::message::Table::Field::TINYINT);
 
94
      break;
 
95
    case DRIZZLE_TYPE_LONG:
 
96
      attribute->set_type(drizzled::message::Table::Field::INTEGER);
 
97
      break;
 
98
    case DRIZZLE_TYPE_DOUBLE:
 
99
      attribute->set_type(drizzled::message::Table::Field::DOUBLE);
 
100
      break;
 
101
    case DRIZZLE_TYPE_NULL  :
 
102
      assert(1); /* Not a user definable type */
 
103
    case DRIZZLE_TYPE_TIMESTAMP:
 
104
      attribute->set_type(drizzled::message::Table::Field::TIMESTAMP);
 
105
      break;
 
106
    case DRIZZLE_TYPE_LONGLONG:
 
107
      attribute->set_type(drizzled::message::Table::Field::BIGINT);
 
108
      break;
 
109
    case DRIZZLE_TYPE_DATETIME:
 
110
      attribute->set_type(drizzled::message::Table::Field::DATETIME);
 
111
      break;
 
112
    case DRIZZLE_TYPE_DATE:
 
113
      attribute->set_type(drizzled::message::Table::Field::DATE);
 
114
      break;
 
115
    case DRIZZLE_TYPE_VARCHAR:
 
116
      {
 
117
        drizzled::message::Table::Field::StringFieldOptions *string_field_options;
 
118
 
 
119
        string_field_options= attribute->mutable_string_options();
 
120
        attribute->set_type(drizzled::message::Table::Field::VARCHAR);
 
121
        string_field_options->set_length(field_arg->length
 
122
                                         / field_arg->charset->mbmaxlen);
 
123
        string_field_options->set_collation_id(field_arg->charset->number);
 
124
        string_field_options->set_collation(field_arg->charset->name);
 
125
 
 
126
        break;
 
127
      }
 
128
    case DRIZZLE_TYPE_NEWDECIMAL:
 
129
      {
 
130
        drizzled::message::Table::Field::NumericFieldOptions *numeric_field_options;
 
131
 
 
132
        attribute->set_type(drizzled::message::Table::Field::DECIMAL);
 
133
        numeric_field_options= attribute->mutable_numeric_options();
 
134
        /* This is magic, I hate magic numbers -Brian */
 
135
        numeric_field_options->set_precision(field_arg->length + ( field_arg->decimals ? -2 : -1));
 
136
        numeric_field_options->set_scale(field_arg->decimals);
 
137
        break;
 
138
      }
 
139
    case DRIZZLE_TYPE_ENUM:
 
140
      {
 
141
        drizzled::message::Table::Field::SetFieldOptions *set_field_options;
 
142
 
 
143
        assert(field_arg->interval);
 
144
 
 
145
        attribute->set_type(drizzled::message::Table::Field::ENUM);
 
146
        set_field_options= attribute->mutable_set_options();
 
147
 
 
148
        for (uint32_t pos= 0; pos < field_arg->interval->count; pos++)
 
149
        {
 
150
          const char *src= field_arg->interval->type_names[pos];
 
151
 
 
152
          set_field_options->add_field_value(src);
 
153
        }
 
154
        set_field_options->set_count_elements(set_field_options->field_value_size());
 
155
        set_field_options->set_collation_id(field_arg->charset->number);
 
156
        set_field_options->set_collation(field_arg->charset->name);
 
157
        break;
 
158
      }
 
159
    case DRIZZLE_TYPE_BLOB:
 
160
      {
 
161
        attribute->set_type(drizzled::message::Table::Field::BLOB);
 
162
 
 
163
        drizzled::message::Table::Field::StringFieldOptions *string_field_options;
 
164
 
 
165
        string_field_options= attribute->mutable_string_options();
 
166
        string_field_options->set_collation_id(field_arg->charset->number);
 
167
        string_field_options->set_collation(field_arg->charset->name);
 
168
      }
 
169
 
 
170
      break;
 
171
    default:
 
172
      assert(0); /* Tell us, since this shouldn't happend */
 
173
    }
 
174
 
 
175
#ifdef NOTDONE
 
176
    field_constraints= attribute->mutable_constraints();
 
177
    constraints->set_is_nullable(field_arg->def->null_value);
210
178
#endif
211
 
  }
212
 
 
213
 
  if ((file=create_frm(thd, file_name, db, table, reclength, fileinfo,
214
 
                       create_info, keys, key_info)) < 0)
215
 
  {
216
 
    my_free(screen_buff, MYF(0));
217
 
    return(1);
218
 
  }
219
 
 
220
 
  key_buff_length= uint4korr(fileinfo+47);
221
 
  keybuff=(uchar*) my_malloc(key_buff_length, MYF(0));
222
 
  key_info_length= pack_keys(keybuff, keys, key_info, data_offset);
223
 
  VOID(get_form_pos(file,fileinfo,&formnames));
224
 
  if (!(filepos=make_new_entry(file,fileinfo,&formnames,"")))
225
 
    goto err;
226
 
  maxlength=(uint) next_io_size((ulong) (uint2korr(forminfo)+1000));
227
 
  int2store(forminfo+2,maxlength);
228
 
  int4store(fileinfo+10,(ulong) (filepos+maxlength));
229
 
  fileinfo[26]= (uchar) test((create_info->max_rows == 1) &&
230
 
                             (create_info->min_rows == 1) && (keys == 0));
231
 
  int2store(fileinfo+28,key_info_length);
232
 
 
233
 
 
234
 
  int2store(fileinfo+59,db_file->extra_rec_buf_length());
235
 
 
236
 
  if (pwrite(file, fileinfo, 64, 0L) == 0 ||
237
 
      pwrite(file, keybuff, key_info_length, (ulong) uint2korr(fileinfo+6)) == 0)
238
 
    goto err;
239
 
  VOID(my_seek(file,
240
 
               (ulong) uint2korr(fileinfo+6)+ (ulong) key_buff_length,
241
 
               MY_SEEK_SET,MYF(0)));
242
 
  if (make_empty_rec(thd,file,ha_legacy_type(create_info->db_type),
243
 
                     create_info->table_options,
244
 
                     create_fields,reclength, data_offset, db_file))
245
 
    goto err;
246
 
 
247
 
  int2store(buff, create_info->connect_string.length);
248
 
  if (my_write(file, (const uchar*)buff, 2, MYF(MY_NABP)) ||
249
 
      my_write(file, (const uchar*)create_info->connect_string.str,
250
 
               create_info->connect_string.length, MYF(MY_NABP)))
251
 
      goto err;
252
 
 
253
 
  int2store(buff, str_db_type.length);
254
 
  if (my_write(file, (const uchar*)buff, 2, MYF(MY_NABP)) ||
255
 
      my_write(file, (const uchar*)str_db_type.str,
256
 
               str_db_type.length, MYF(MY_NABP)))
257
 
    goto err;
258
 
 
259
 
  {
260
 
    memset(buff, 0, 6);
261
 
    if (my_write(file, (uchar*) buff, 6, MYF_RW))
262
 
      goto err;
263
 
  }
264
 
 
265
 
  if (forminfo[46] == (uchar)255)
266
 
  {
267
 
    uchar comment_length_buff[2];
268
 
    int2store(comment_length_buff,create_info->comment.length);
269
 
    if (my_write(file, comment_length_buff, 2, MYF(MY_NABP)) ||
270
 
        my_write(file, (uchar*)create_info->comment.str,
271
 
                  create_info->comment.length, MYF(MY_NABP)))
272
 
      goto err;
273
 
  }
274
 
 
275
 
  /* Store storage type and field format array of fields */
276
 
  {
277
 
    /* prepare header */
278
 
    {
279
 
      uint flags= 0;
280
 
 
281
 
      memset(buff, 0, format_section_header_size);
282
 
      /* length of section 2 bytes*/
283
 
      int2store(buff+0, format_section_len);
284
 
      /* flags of section 4 bytes*/
285
 
      int4store(buff+2, flags);
286
 
      /* 2 bytes left for future use */
287
 
    }
288
 
    /* write header */
289
 
    if (my_write(file, (const uchar*)buff, format_section_header_size, MYF_RW))
290
 
      goto err;
291
 
    buff[0]= 0;
292
 
    if (my_write(file, (const uchar*)buff, 1, MYF_RW))
293
 
      goto err;
294
 
    /* write column info, 1 byte per column */
295
 
    {
296
 
      List_iterator<Create_field> it(create_fields);
297
 
      Create_field *field;
298
 
      uchar column_format, write_byte;
299
 
      while ((field=it++))
300
 
      {
301
 
        column_format= (uchar)field->column_format();
302
 
        write_byte= (column_format << COLUMN_FORMAT_SHIFT);
303
 
        if (my_write(file, &write_byte, 1, MYF_RW))
304
 
          goto err;
305
 
      }
306
 
    }
307
 
  }
308
 
  VOID(my_seek(file,filepos,MY_SEEK_SET,MYF(0)));
309
 
  if (my_write(file, forminfo, 288, MYF_RW) ||
310
 
      my_write(file, screen_buff, info_length, MYF_RW) ||
311
 
      pack_fields(file, create_fields, data_offset))
312
 
    goto err;
313
 
 
314
 
  my_free(screen_buff,MYF(0));
315
 
  my_free(keybuff, MYF(0));
316
 
 
317
 
  if (opt_sync_frm && !(create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
318
 
      (my_sync(file, MYF(MY_WME)) ||
319
 
       my_sync_dir_by_file(file_name, MYF(MY_WME))))
320
 
      goto err2;
321
 
 
322
 
  if (my_close(file,MYF(MY_WME)))
323
 
    goto err3;
324
 
 
325
 
  {
326
 
    /* 
327
 
      Restore all UCS2 intervals.
328
 
      HEX representation of them is not needed anymore.
329
 
    */
330
 
    List_iterator<Create_field> it(create_fields);
331
 
    Create_field *field;
332
 
    while ((field=it++))
333
 
    {
334
 
      if (field->save_interval)
335
 
      {
336
 
        field->interval= field->save_interval;
337
 
        field->save_interval= 0;
338
 
      }
339
 
    }
340
 
  }
341
 
  return(0);
342
 
 
343
 
err:
344
 
  my_free(screen_buff, MYF(0));
345
 
  my_free(keybuff, MYF(0));
346
 
err2:
347
 
  VOID(my_close(file,MYF(MY_WME)));
348
 
err3:
349
 
  my_delete(file_name,MYF(0));
350
 
  return(1);
351
 
} /* mysql_create_frm */
352
 
 
 
179
 
 
180
    switch(field_arg->column_format())
 
181
    {
 
182
    case COLUMN_FORMAT_TYPE_NOT_USED:
 
183
      break;
 
184
    case COLUMN_FORMAT_TYPE_DEFAULT:
 
185
      attribute->set_format(drizzled::message::Table::Field::DefaultFormat);
 
186
      break;
 
187
    case COLUMN_FORMAT_TYPE_FIXED:
 
188
      attribute->set_format(drizzled::message::Table::Field::FixedFormat);
 
189
      break;
 
190
    case COLUMN_FORMAT_TYPE_DYNAMIC:
 
191
      attribute->set_format(drizzled::message::Table::Field::DynamicFormat);
 
192
      break;
 
193
    default:
 
194
      assert(0); /* Tell us, since this shouldn't happend */
 
195
    }
 
196
 
 
197
    if (field_arg->comment.length)
 
198
    {
 
199
      uint32_t tmp_len;
 
200
      tmp_len= system_charset_info->cset->charpos(system_charset_info,
 
201
                                                  field_arg->comment.str,
 
202
                                                  field_arg->comment.str +
 
203
                                                  field_arg->comment.length,
 
204
                                                  COLUMN_COMMENT_MAXLEN);
 
205
 
 
206
      if (tmp_len < field_arg->comment.length)
 
207
      {
 
208
        my_error(ER_WRONG_STRING_LENGTH, MYF(0),
 
209
                 field_arg->comment.str,"COLUMN COMMENT",
 
210
                 (uint32_t) COLUMN_COMMENT_MAXLEN);
 
211
        return(1);
 
212
      }
 
213
 
 
214
      attribute->set_comment(field_arg->comment.str);
 
215
    }
 
216
 
 
217
    if(field_arg->unireg_check == Field::NEXT_NUMBER)
 
218
    {
 
219
      drizzled::message::Table::Field::NumericFieldOptions *field_options;
 
220
      field_options= attribute->mutable_numeric_options();
 
221
      field_options->set_is_autoincrement(true);
 
222
    }
 
223
 
 
224
    if(field_arg->unireg_check == Field::TIMESTAMP_DN_FIELD
 
225
       || field_arg->unireg_check == Field::TIMESTAMP_DNUN_FIELD)
 
226
    {
 
227
      drizzled::message::Table::Field::FieldOptions *field_options;
 
228
      field_options= attribute->mutable_options();
 
229
      field_options->set_default_value("NOW()");
 
230
    }
 
231
 
 
232
    if(field_arg->unireg_check == Field::TIMESTAMP_UN_FIELD
 
233
       || field_arg->unireg_check == Field::TIMESTAMP_DNUN_FIELD)
 
234
    {
 
235
      drizzled::message::Table::Field::FieldOptions *field_options;
 
236
      field_options= attribute->mutable_options();
 
237
      field_options->set_update_value("NOW()");
 
238
    }
 
239
 
 
240
    if(field_arg->def)
 
241
    {
 
242
      drizzled::message::Table::Field::FieldOptions *field_options;
 
243
      field_options= attribute->mutable_options();
 
244
 
 
245
      if(field_arg->def->is_null())
 
246
      {
 
247
        field_options->set_default_null(true);
 
248
      }
 
249
      else
 
250
      {
 
251
        String d;
 
252
        String *default_value= field_arg->def->val_str(&d);
 
253
 
 
254
        assert(default_value);
 
255
 
 
256
        if((field_arg->sql_type==DRIZZLE_TYPE_VARCHAR
 
257
           || field_arg->sql_type==DRIZZLE_TYPE_BLOB)
 
258
           && ((field_arg->length / field_arg->charset->mbmaxlen)
 
259
           < default_value->length()))
 
260
        {
 
261
          my_error(ER_INVALID_DEFAULT, MYF(0), field_arg->field_name);
 
262
          return 1;
 
263
        }
 
264
 
 
265
        if((field_arg->sql_type==DRIZZLE_TYPE_VARCHAR
 
266
            && field_arg->charset==&my_charset_bin)
 
267
           || (field_arg->sql_type==DRIZZLE_TYPE_BLOB
 
268
            && field_arg->charset==&my_charset_bin))
 
269
        {
 
270
          string bin_default;
 
271
          bin_default.assign(default_value->c_ptr(),
 
272
                             default_value->length());
 
273
          field_options->set_default_bin_value(bin_default);
 
274
        }
 
275
        else
 
276
        {
 
277
          field_options->set_default_value(default_value->c_ptr());
 
278
        }
 
279
      }
 
280
    }
 
281
 
 
282
    {
 
283
      drizzled::message::Table::Field::FieldOptions *field_options;
 
284
      field_options= attribute->mutable_options();
 
285
 
 
286
      field_options->set_length(field_arg->length);
 
287
    }
 
288
 
 
289
    assert(field_arg->unireg_check == Field::NONE
 
290
           || field_arg->unireg_check == Field::NEXT_NUMBER
 
291
           || field_arg->unireg_check == Field::TIMESTAMP_DN_FIELD
 
292
           || field_arg->unireg_check == Field::TIMESTAMP_UN_FIELD
 
293
           || field_arg->unireg_check == Field::TIMESTAMP_DNUN_FIELD);
 
294
 
 
295
  }
 
296
 
 
297
  if (create_info->used_fields & HA_CREATE_USED_PACK_KEYS)
 
298
  {
 
299
    if(create_info->table_options & HA_OPTION_PACK_KEYS)
 
300
      table_options->set_pack_keys(true);
 
301
    else if(create_info->table_options & HA_OPTION_NO_PACK_KEYS)
 
302
      table_options->set_pack_keys(false);
 
303
  }
 
304
  else
 
305
    if(create_info->table_options & HA_OPTION_PACK_KEYS)
 
306
      table_options->set_pack_keys(true);
 
307
 
 
308
 
 
309
  if (create_info->used_fields & HA_CREATE_USED_CHECKSUM)
 
310
  {
 
311
    assert(create_info->table_options & (HA_OPTION_CHECKSUM | HA_OPTION_NO_CHECKSUM));
 
312
 
 
313
    if(create_info->table_options & HA_OPTION_CHECKSUM)
 
314
      table_options->set_checksum(true);
 
315
    else
 
316
      table_options->set_checksum(false);
 
317
  }
 
318
  else if(create_info->table_options & HA_OPTION_CHECKSUM)
 
319
    table_options->set_checksum(true);
 
320
 
 
321
 
 
322
  if (create_info->used_fields & HA_CREATE_USED_PAGE_CHECKSUM)
 
323
  {
 
324
    if (create_info->page_checksum == HA_CHOICE_YES)
 
325
      table_options->set_page_checksum(true);
 
326
    else if (create_info->page_checksum == HA_CHOICE_NO)
 
327
      table_options->set_page_checksum(false);
 
328
  }
 
329
  else if (create_info->page_checksum == HA_CHOICE_YES)
 
330
    table_options->set_page_checksum(true);
 
331
 
 
332
 
 
333
  if (create_info->used_fields & HA_CREATE_USED_DELAY_KEY_WRITE)
 
334
  {
 
335
    if(create_info->table_options & HA_OPTION_DELAY_KEY_WRITE)
 
336
      table_options->set_delay_key_write(true);
 
337
    else if(create_info->table_options & HA_OPTION_NO_DELAY_KEY_WRITE)
 
338
      table_options->set_delay_key_write(false);
 
339
  }
 
340
  else if(create_info->table_options & HA_OPTION_DELAY_KEY_WRITE)
 
341
    table_options->set_delay_key_write(true);
 
342
 
 
343
 
 
344
  switch(create_info->row_type)
 
345
  {
 
346
  case ROW_TYPE_DEFAULT:
 
347
    table_options->set_row_type(drizzled::message::Table::TableOptions::ROW_TYPE_DEFAULT);
 
348
    break;
 
349
  case ROW_TYPE_FIXED:
 
350
    table_options->set_row_type(drizzled::message::Table::TableOptions::ROW_TYPE_FIXED);
 
351
    break;
 
352
  case ROW_TYPE_DYNAMIC:
 
353
    table_options->set_row_type(drizzled::message::Table::TableOptions::ROW_TYPE_DYNAMIC);
 
354
    break;
 
355
  case ROW_TYPE_COMPRESSED:
 
356
    table_options->set_row_type(drizzled::message::Table::TableOptions::ROW_TYPE_COMPRESSED);
 
357
    break;
 
358
  case ROW_TYPE_REDUNDANT:
 
359
    table_options->set_row_type(drizzled::message::Table::TableOptions::ROW_TYPE_REDUNDANT);
 
360
    break;
 
361
  case ROW_TYPE_COMPACT:
 
362
    table_options->set_row_type(drizzled::message::Table::TableOptions::ROW_TYPE_COMPACT);
 
363
    break;
 
364
  case ROW_TYPE_PAGE:
 
365
    table_options->set_row_type(drizzled::message::Table::TableOptions::ROW_TYPE_PAGE);
 
366
    break;
 
367
  default:
 
368
    abort();
 
369
  }
 
370
 
 
371
  table_options->set_pack_record(create_info->table_options
 
372
                                 & HA_OPTION_PACK_RECORD);
 
373
 
 
374
  if (create_info->comment.length)
 
375
  {
 
376
    uint32_t tmp_len;
 
377
    tmp_len= system_charset_info->cset->charpos(system_charset_info,
 
378
                                                create_info->comment.str,
 
379
                                                create_info->comment.str +
 
380
                                                create_info->comment.length,
 
381
                                                TABLE_COMMENT_MAXLEN);
 
382
 
 
383
    if (tmp_len < create_info->comment.length)
 
384
    {
 
385
      my_error(ER_WRONG_STRING_LENGTH, MYF(0),
 
386
               create_info->comment.str,"Table COMMENT",
 
387
               (uint32_t) TABLE_COMMENT_MAXLEN);
 
388
      return(1);
 
389
    }
 
390
 
 
391
    table_options->set_comment(create_info->comment.str);
 
392
  }
 
393
  if (create_info->default_table_charset)
 
394
  {
 
395
    table_options->set_collation_id(
 
396
                               create_info->default_table_charset->number);
 
397
    table_options->set_collation(create_info->default_table_charset->name);
 
398
  }
 
399
 
 
400
  if (create_info->connect_string.length)
 
401
    table_options->set_connect_string(create_info->connect_string.str);
 
402
 
 
403
  if (create_info->data_file_name)
 
404
    table_options->set_data_file_name(create_info->data_file_name);
 
405
 
 
406
  if (create_info->index_file_name)
 
407
    table_options->set_index_file_name(create_info->index_file_name);
 
408
 
 
409
  if (create_info->max_rows)
 
410
    table_options->set_max_rows(create_info->max_rows);
 
411
 
 
412
  if (create_info->min_rows)
 
413
    table_options->set_min_rows(create_info->min_rows);
 
414
 
 
415
  if (create_info->auto_increment_value)
 
416
    table_options->set_auto_increment_value(create_info->auto_increment_value);
 
417
 
 
418
  if (create_info->avg_row_length)
 
419
    table_options->set_avg_row_length(create_info->avg_row_length);
 
420
 
 
421
  if (create_info->key_block_size)
 
422
    table_options->set_key_block_size(create_info->key_block_size);
 
423
 
 
424
  if (create_info->block_size)
 
425
    table_options->set_block_size(create_info->block_size);
 
426
 
 
427
  for (unsigned int i= 0; i < keys; i++)
 
428
  {
 
429
    drizzled::message::Table::Index *idx;
 
430
 
 
431
    idx= table_proto->add_indexes();
 
432
 
 
433
    assert(test(key_info[i].flags & HA_USES_COMMENT) ==
 
434
           (key_info[i].comment.length > 0));
 
435
 
 
436
    idx->set_name(key_info[i].name);
 
437
 
 
438
    idx->set_key_length(key_info[i].key_length);
 
439
 
 
440
    if(is_primary_key_name(key_info[i].name))
 
441
      idx->set_is_primary(true);
 
442
    else
 
443
      idx->set_is_primary(false);
 
444
 
 
445
    switch(key_info[i].algorithm)
 
446
    {
 
447
    case HA_KEY_ALG_HASH:
 
448
      idx->set_type(drizzled::message::Table::Index::HASH);
 
449
      break;
 
450
 
 
451
    case HA_KEY_ALG_BTREE:
 
452
      idx->set_type(drizzled::message::Table::Index::BTREE);
 
453
      break;
 
454
 
 
455
    case HA_KEY_ALG_RTREE:
 
456
      idx->set_type(drizzled::message::Table::Index::RTREE);
 
457
    case HA_KEY_ALG_FULLTEXT:
 
458
      idx->set_type(drizzled::message::Table::Index::FULLTEXT);
 
459
    case HA_KEY_ALG_UNDEF:
 
460
      idx->set_type(drizzled::message::Table::Index::UNKNOWN_INDEX);
 
461
      break;
 
462
 
 
463
    default:
 
464
      abort(); /* Somebody's brain broke. haven't added index type to proto */
 
465
    }
 
466
 
 
467
    if (key_info[i].flags & HA_NOSAME)
 
468
      idx->set_is_unique(true);
 
469
    else
 
470
      idx->set_is_unique(false);
 
471
 
 
472
    drizzled::message::Table::Index::IndexOptions *index_options= idx->mutable_options();
 
473
 
 
474
    if(key_info[i].flags & HA_USES_BLOCK_SIZE)
 
475
      index_options->set_key_block_size(key_info[i].block_size);
 
476
 
 
477
    if(key_info[i].flags & HA_PACK_KEY)
 
478
      index_options->set_pack_key(true);
 
479
 
 
480
    if(key_info[i].flags & HA_BINARY_PACK_KEY)
 
481
      index_options->set_binary_pack_key(true);
 
482
 
 
483
    if(key_info[i].flags & HA_VAR_LENGTH_PART)
 
484
      index_options->set_var_length_key(true);
 
485
 
 
486
    if(key_info[i].flags & HA_NULL_PART_KEY)
 
487
      index_options->set_null_part_key(true);
 
488
 
 
489
    if(key_info[i].flags & HA_KEY_HAS_PART_KEY_SEG)
 
490
      index_options->set_has_partial_segments(true);
 
491
 
 
492
    if(key_info[i].flags & HA_GENERATED_KEY)
 
493
      index_options->set_auto_generated_key(true);
 
494
 
 
495
    if (key_info[i].flags & HA_USES_COMMENT)
 
496
    {
 
497
      uint32_t tmp_len;
 
498
      tmp_len= system_charset_info->cset->charpos(system_charset_info,
 
499
                                                  key_info[i].comment.str,
 
500
                                                  key_info[i].comment.str +
 
501
                                                  key_info[i].comment.length,
 
502
                                                  TABLE_COMMENT_MAXLEN);
 
503
 
 
504
      if (tmp_len < key_info[i].comment.length)
 
505
      {
 
506
        my_error(ER_WRONG_STRING_LENGTH, MYF(0),
 
507
                 key_info[i].comment.str,"Index COMMENT",
 
508
                 (uint32_t) TABLE_COMMENT_MAXLEN);
 
509
        return(1);
 
510
      }
 
511
 
 
512
      idx->set_comment(key_info[i].comment.str);
 
513
    }
 
514
    if(key_info[i].flags & ~(HA_NOSAME | HA_PACK_KEY | HA_USES_BLOCK_SIZE | HA_BINARY_PACK_KEY | HA_VAR_LENGTH_PART | HA_NULL_PART_KEY | HA_KEY_HAS_PART_KEY_SEG | HA_GENERATED_KEY | HA_USES_COMMENT))
 
515
      abort(); // Invalid (unknown) index flag.
 
516
 
 
517
    for(unsigned int j=0; j< key_info[i].key_parts; j++)
 
518
    {
 
519
      drizzled::message::Table::Index::IndexPart *idxpart;
 
520
 
 
521
      idxpart= idx->add_index_part();
 
522
 
 
523
      idxpart->set_fieldnr(key_info[i].key_part[j].fieldnr);
 
524
 
 
525
      idxpart->set_compare_length(key_info[i].key_part[j].length);
 
526
 
 
527
      idxpart->set_key_type(key_info[i].key_part[j].key_type);
 
528
 
 
529
    }
 
530
  }
 
531
 
 
532
  return 0;
 
533
}
 
534
 
 
535
int copy_table_proto_file(const char *from, const char* to)
 
536
{
 
537
  string dfesrc(from);
 
538
  string dfedst(to);
 
539
  string file_ext = ".dfe";
 
540
 
 
541
  dfesrc.append(file_ext);
 
542
  dfedst.append(file_ext);
 
543
 
 
544
  return my_copy(dfesrc.c_str(), dfedst.c_str(),
 
545
                 MYF(MY_DONT_OVERWRITE_FILE));
 
546
}
 
547
 
 
548
int rename_table_proto_file(const char *from, const char* to)
 
549
{
 
550
  string from_path(from);
 
551
  string to_path(to);
 
552
  string file_ext = ".dfe";
 
553
 
 
554
  from_path.append(file_ext);
 
555
  to_path.append(file_ext);
 
556
 
 
557
  return my_rename(from_path.c_str(),to_path.c_str(),MYF(MY_WME));
 
558
}
 
559
 
 
560
int delete_table_proto_file(const char *file_name)
 
561
{
 
562
  string new_path(file_name);
 
563
  string file_ext = ".dfe";
 
564
 
 
565
  new_path.append(file_ext);
 
566
  return my_delete(new_path.c_str(), MYF(0));
 
567
}
 
568
 
 
569
int table_proto_exists(const char *path)
 
570
{
 
571
  string proto_path(path);
 
572
  string file_ext(".dfe");
 
573
  proto_path.append(file_ext);
 
574
 
 
575
  int error= access(proto_path.c_str(), F_OK);
 
576
 
 
577
  if (error == 0)
 
578
    return EEXIST;
 
579
  else
 
580
    return errno;
 
581
}
 
582
 
 
583
static int create_table_proto_file(const char *file_name,
 
584
                                   const char *db,
 
585
                                   const char *table_name,
 
586
                                   drizzled::message::Table *table_proto,
 
587
                                   HA_CREATE_INFO *create_info,
 
588
                                   List<CreateField> &create_fields,
 
589
                                   uint32_t keys,
 
590
                                   KEY *key_info)
 
591
{
 
592
  string new_path(file_name);
 
593
  string file_ext = ".dfe";
 
594
 
 
595
  if(fill_table_proto(table_proto, table_name, create_fields, create_info,
 
596
                      keys, key_info))
 
597
    return -1;
 
598
 
 
599
  new_path.append(file_ext);
 
600
 
 
601
  int fd= open(new_path.c_str(), O_RDWR|O_CREAT|O_TRUNC, my_umask);
 
602
 
 
603
  if(fd==-1)
 
604
  {
 
605
    if(errno==ENOENT)
 
606
      my_error(ER_BAD_DB_ERROR,MYF(0),db);
 
607
    else
 
608
      my_error(ER_CANT_CREATE_TABLE,MYF(0),table_name,errno);
 
609
    return errno;
 
610
  }
 
611
 
 
612
  google::protobuf::io::ZeroCopyOutputStream* output=
 
613
    new google::protobuf::io::FileOutputStream(fd);
 
614
 
 
615
  if (!table_proto->SerializeToZeroCopyStream(output))
 
616
  {
 
617
    delete output;
 
618
    close(fd);
 
619
    return errno;
 
620
  }
 
621
 
 
622
  delete output;
 
623
  close(fd);
 
624
  return 0;
 
625
}
353
626
 
354
627
/*
355
 
  Create a frm (table definition) file and the tables
 
628
  Create a table definition proto file and the tables
356
629
 
357
630
  SYNOPSIS
358
631
    rea_create_table()
359
 
    thd                 Thread handler
 
632
    session                     Thread handler
360
633
    path                Name of file (including database, without .frm)
361
634
    db                  Data base name
362
635
    table_name          Table name
365
638
    keys                number of keys to create
366
639
    key_info            Keys to create
367
640
    file                Handler to use
 
641
    is_like             is true for mysql_create_like_schema_frm
368
642
 
369
643
  RETURN
370
644
    0  ok
371
645
    1  error
372
646
*/
373
647
 
374
 
int rea_create_table(THD *thd, const char *path,
 
648
int rea_create_table(Session *session, const char *path,
375
649
                     const char *db, const char *table_name,
 
650
                     drizzled::message::Table *table_proto,
376
651
                     HA_CREATE_INFO *create_info,
377
 
                     List<Create_field> &create_fields,
378
 
                     uint keys, KEY *key_info, handler *file)
 
652
                     List<CreateField> &create_fields,
 
653
                     uint32_t keys, KEY *key_info,
 
654
                     bool is_like)
379
655
{
380
 
  
381
 
 
382
 
  char frm_name[FN_REFLEN];
383
 
  strxmov(frm_name, path, reg_ext, NullS);
384
 
  if (mysql_create_frm(thd, frm_name, db, table_name, create_info,
385
 
                       create_fields, keys, key_info, file))
386
 
 
387
 
    return(1);
388
 
 
389
 
  // Make sure mysql_create_frm din't remove extension
390
 
  assert(*fn_rext(frm_name));
391
 
  if (thd->variables.keep_files_on_create)
392
 
    create_info->options|= HA_CREATE_KEEP_FILES;
393
 
  if (file->ha_create_handler_files(path, NULL, CHF_CREATE_FLAG, create_info))
394
 
    goto err_handler;
395
 
  if (!create_info->frm_only && ha_create_table(thd, path, db, table_name,
396
 
                                                create_info,0))
397
 
    goto err_handler;
398
 
  return(0);
 
656
  /* Proto will blow up unless we give a name */
 
657
  assert(table_name);
 
658
 
 
659
  /* For is_like we return once the file has been created */
 
660
  if (is_like)
 
661
  {
 
662
    if (create_table_proto_file(path, db, table_name, table_proto,
 
663
                                create_info,
 
664
                                create_fields, keys, key_info)!=0)
 
665
      return 1;
 
666
 
 
667
    return 0;
 
668
  }
 
669
  /* Here we need to build the full frm from the path */
 
670
  else
 
671
  {
 
672
    if (create_table_proto_file(path, db, table_name, table_proto,
 
673
                                create_info,
 
674
                                create_fields, keys, key_info))
 
675
      return 1;
 
676
  }
 
677
 
 
678
  if (ha_create_table(session, path, db, table_name,
 
679
                      create_info,0))
 
680
    goto err_handler;
 
681
  return 0;
399
682
 
400
683
err_handler:
401
 
  VOID(file->ha_create_handler_files(path, NULL, CHF_DELETE_FLAG, create_info));
402
 
  my_delete(frm_name, MYF(0));
403
 
  return(1);
 
684
  delete_table_proto_file(path);
 
685
 
 
686
  return 1;
404
687
} /* rea_create_table */
405
 
 
406
 
 
407
 
        /* Pack screens to a screen for save in a form-file */
408
 
 
409
 
static uchar *pack_screens(List<Create_field> &create_fields,
410
 
                           uint *info_length, uint *screens,
411
 
                           bool small_file)
412
 
{
413
 
  register uint i;
414
 
  uint row,start_row,end_row,fields_on_screen;
415
 
  uint length,cols;
416
 
  uchar *info,*pos,*start_screen;
417
 
  uint fields=create_fields.elements;
418
 
  List_iterator<Create_field> it(create_fields);
419
 
  
420
 
 
421
 
  start_row=4; end_row=22; cols=80; fields_on_screen=end_row+1-start_row;
422
 
 
423
 
  *screens=(fields-1)/fields_on_screen+1;
424
 
  length= (*screens) * (SC_INFO_LENGTH+ (cols>> 1)+4);
425
 
 
426
 
  Create_field *field;
427
 
  while ((field=it++))
428
 
    length+=(uint) strlen(field->field_name)+1+TE_INFO_LENGTH+cols/2;
429
 
 
430
 
  if (!(info=(uchar*) my_malloc(length,MYF(MY_WME))))
431
 
    return(0);
432
 
 
433
 
  start_screen=0;
434
 
  row=end_row;
435
 
  pos=info;
436
 
  it.rewind();
437
 
  for (i=0 ; i < fields ; i++)
438
 
  {
439
 
    Create_field *cfield=it++;
440
 
    if (row++ == end_row)
441
 
    {
442
 
      if (i)
443
 
      {
444
 
        length=(uint) (pos-start_screen);
445
 
        int2store(start_screen,length);
446
 
        start_screen[2]=(uchar) (fields_on_screen+1);
447
 
        start_screen[3]=(uchar) (fields_on_screen);
448
 
      }
449
 
      row=start_row;
450
 
      start_screen=pos;
451
 
      pos+=4;
452
 
      pos[0]= (uchar) start_row-2;      /* Header string */
453
 
      pos[1]= (uchar) (cols >> 2);
454
 
      pos[2]= (uchar) (cols >> 1) +1;
455
 
      strfill((char *) pos+3,(uint) (cols >> 1),' ');
456
 
      pos+=(cols >> 1)+4;
457
 
    }
458
 
    length=(uint) strlen(cfield->field_name);
459
 
    if (length > cols-3)
460
 
      length=cols-3;
461
 
 
462
 
    if (!small_file)
463
 
    {
464
 
      pos[0]=(uchar) row;
465
 
      pos[1]=0;
466
 
      pos[2]=(uchar) (length+1);
467
 
      pos=(uchar*) strmake((char*) pos+3,cfield->field_name,length)+1;
468
 
    }
469
 
    cfield->row=(uint8_t) row;
470
 
    cfield->col=(uint8_t) (length+1);
471
 
    cfield->sc_length=(uint8_t) min(cfield->length,(uint32_t)cols-(length+2));
472
 
  }
473
 
  length=(uint) (pos-start_screen);
474
 
  int2store(start_screen,length);
475
 
  start_screen[2]=(uchar) (row-start_row+2);
476
 
  start_screen[3]=(uchar) (row-start_row+1);
477
 
 
478
 
  *info_length=(uint) (pos-info);
479
 
  return(info);
480
 
} /* pack_screens */
481
 
 
482
 
 
483
 
        /* Pack keyinfo and keynames to keybuff for save in form-file. */
484
 
 
485
 
static uint pack_keys(uchar *keybuff, uint key_count, KEY *keyinfo,
486
 
                      ulong data_offset)
487
 
{
488
 
  uint key_parts,length;
489
 
  uchar *pos, *keyname_pos;
490
 
  KEY *key,*end;
491
 
  KEY_PART_INFO *key_part,*key_part_end;
492
 
  
493
 
 
494
 
  pos=keybuff+6;
495
 
  key_parts=0;
496
 
  for (key=keyinfo,end=keyinfo+key_count ; key != end ; key++)
497
 
  {
498
 
    int2store(pos, (key->flags ^ HA_NOSAME));
499
 
    int2store(pos+2,key->key_length);
500
 
    pos[4]= (uchar) key->key_parts;
501
 
    pos[5]= (uchar) key->algorithm;
502
 
    int2store(pos+6, key->block_size);
503
 
    pos+=8;
504
 
    key_parts+=key->key_parts;
505
 
    for (key_part=key->key_part,key_part_end=key_part+key->key_parts ;
506
 
         key_part != key_part_end ;
507
 
         key_part++)
508
 
 
509
 
    {
510
 
      uint offset;
511
 
      int2store(pos,key_part->fieldnr+1+FIELD_NAME_USED);
512
 
      offset= (uint) (key_part->offset+data_offset+1);
513
 
      int2store(pos+2, offset);
514
 
      pos[4]=0;                                 // Sort order
515
 
      int2store(pos+5,key_part->key_type);
516
 
      int2store(pos+7,key_part->length);
517
 
      pos+=9;
518
 
    }
519
 
  }
520
 
        /* Save keynames */
521
 
  keyname_pos=pos;
522
 
  *pos++=(uchar) NAMES_SEP_CHAR;
523
 
  for (key=keyinfo ; key != end ; key++)
524
 
  {
525
 
    uchar *tmp=(uchar*) stpcpy((char*) pos,key->name);
526
 
    *tmp++= (uchar) NAMES_SEP_CHAR;
527
 
    *tmp=0;
528
 
    pos=tmp;
529
 
  }
530
 
  *(pos++)=0;
531
 
 
532
 
  for (key=keyinfo,end=keyinfo+key_count ; key != end ; key++)
533
 
  {
534
 
    if (key->flags & HA_USES_COMMENT)
535
 
    {
536
 
      int2store(pos, key->comment.length);
537
 
      uchar *tmp= (uchar*)stpncpy((char*) pos+2,key->comment.str,key->comment.length);
538
 
      pos= tmp;
539
 
    }
540
 
  }
541
 
 
542
 
  if (key_count > 127 || key_parts > 127)
543
 
  {
544
 
    keybuff[0]= (key_count & 0x7f) | 0x80;
545
 
    keybuff[1]= key_count >> 7;
546
 
    int2store(keybuff+2,key_parts);
547
 
  }
548
 
  else
549
 
  {
550
 
    keybuff[0]=(uchar) key_count;
551
 
    keybuff[1]=(uchar) key_parts;
552
 
    keybuff[2]= keybuff[3]= 0;
553
 
  }
554
 
  length=(uint) (pos-keyname_pos);
555
 
  int2store(keybuff+4,length);
556
 
  return((uint) (pos-keybuff));
557
 
} /* pack_keys */
558
 
 
559
 
 
560
 
/* Make formheader */
561
 
 
562
 
static bool pack_header(uchar *forminfo,
563
 
                        enum legacy_db_type table_type __attribute__((unused)),
564
 
                        List<Create_field> &create_fields,
565
 
                        uint info_length, uint screens, uint table_options,
566
 
                        ulong data_offset, handler *file)
567
 
{
568
 
  uint length,int_count,int_length,no_empty, int_parts;
569
 
  uint time_stamp_pos,null_fields;
570
 
  ulong reclength, totlength, n_length, com_length;
571
 
 
572
 
 
573
 
  if (create_fields.elements > MAX_FIELDS)
574
 
  {
575
 
    my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0));
576
 
    return(1);
577
 
  }
578
 
 
579
 
  totlength= 0L;
580
 
  reclength= data_offset;
581
 
  no_empty=int_count=int_parts=int_length=time_stamp_pos=null_fields=
582
 
    com_length=0;
583
 
  n_length=2L;
584
 
 
585
 
        /* Check fields */
586
 
 
587
 
  List_iterator<Create_field> it(create_fields);
588
 
  Create_field *field;
589
 
  while ((field=it++))
590
 
  {
591
 
    uint tmp_len= system_charset_info->cset->charpos(system_charset_info,
592
 
                                                     field->comment.str,
593
 
                                                     field->comment.str +
594
 
                                                     field->comment.length,
595
 
                                                     COLUMN_COMMENT_MAXLEN);
596
 
 
597
 
    if (tmp_len < field->comment.length)
598
 
    {
599
 
      my_error(ER_WRONG_STRING_LENGTH, MYF(0),
600
 
               field->comment.str,"COLUMN COMMENT",
601
 
               (uint) COLUMN_COMMENT_MAXLEN);
602
 
      return(1);
603
 
    }
604
 
 
605
 
    totlength+= field->length;
606
 
    com_length+= field->comment.length;
607
 
    if (MTYP_TYPENR(field->unireg_check) == Field::NOEMPTY ||
608
 
        field->unireg_check & MTYP_NOEMPTY_BIT)
609
 
    {
610
 
      field->unireg_check= (Field::utype) ((uint) field->unireg_check |
611
 
                                           MTYP_NOEMPTY_BIT);
612
 
      no_empty++;
613
 
    }
614
 
    /* 
615
 
      We mark first TIMESTAMP field with NOW() in DEFAULT or ON UPDATE 
616
 
      as auto-update field.
617
 
    */
618
 
    if (field->sql_type == DRIZZLE_TYPE_TIMESTAMP &&
619
 
        MTYP_TYPENR(field->unireg_check) != Field::NONE &&
620
 
        !time_stamp_pos)
621
 
      time_stamp_pos= (uint) field->offset+ (uint) data_offset + 1;
622
 
    length=field->pack_length;
623
 
    /* Ensure we don't have any bugs when generating offsets */
624
 
    assert(reclength == field->offset + data_offset);
625
 
    if ((uint) field->offset+ (uint) data_offset+ length > reclength)
626
 
      reclength=(uint) (field->offset+ data_offset + length);
627
 
    n_length+= (ulong) strlen(field->field_name)+1;
628
 
    field->interval_id=0;
629
 
    field->save_interval= 0;
630
 
    if (field->interval)
631
 
    {
632
 
      uint old_int_count=int_count;
633
 
 
634
 
      if (field->charset->mbminlen > 1)
635
 
      {
636
 
        /* 
637
 
          Escape UCS2 intervals using HEX notation to avoid
638
 
          problems with delimiters between enum elements.
639
 
          As the original representation is still needed in 
640
 
          the function make_empty_rec to create a record of
641
 
          filled with default values it is saved in save_interval
642
 
          The HEX representation is created from this copy.
643
 
        */
644
 
        field->save_interval= field->interval;
645
 
        field->interval= (TYPELIB*) sql_alloc(sizeof(TYPELIB));
646
 
        *field->interval= *field->save_interval; 
647
 
        field->interval->type_names= 
648
 
          (const char **) sql_alloc(sizeof(char*) * 
649
 
                                    (field->interval->count+1));
650
 
        field->interval->type_names[field->interval->count]= 0;
651
 
        field->interval->type_lengths=
652
 
          (uint *) sql_alloc(sizeof(uint) * field->interval->count);
653
 
 
654
 
        for (uint pos= 0; pos < field->interval->count; pos++)
655
 
        {
656
 
          char *dst;
657
 
          const char *src= field->save_interval->type_names[pos];
658
 
          uint hex_length;
659
 
          length= field->save_interval->type_lengths[pos];
660
 
          hex_length= length * 2;
661
 
          field->interval->type_lengths[pos]= hex_length;
662
 
          field->interval->type_names[pos]= dst= (char*) sql_alloc(hex_length +
663
 
                                                                   1);
664
 
          octet2hex(dst, src, length);
665
 
        }
666
 
      }
667
 
 
668
 
      field->interval_id=get_interval_id(&int_count,create_fields,field);
669
 
      if (old_int_count != int_count)
670
 
      {
671
 
        for (const char **pos=field->interval->type_names ; *pos ; pos++)
672
 
          int_length+=(uint) strlen(*pos)+1;    // field + suffix prefix
673
 
        int_parts+=field->interval->count+1;
674
 
      }
675
 
    }
676
 
    if (f_maybe_null(field->pack_flag))
677
 
      null_fields++;
678
 
  }
679
 
  int_length+=int_count*2;                      // 255 prefix + 0 suffix
680
 
 
681
 
        /* Save values in forminfo */
682
 
 
683
 
  if (reclength > (ulong) file->max_record_length())
684
 
  {
685
 
    my_error(ER_TOO_BIG_ROWSIZE, MYF(0), (uint) file->max_record_length());
686
 
    return(1);
687
 
  }
688
 
  /* Hack to avoid bugs with small static rows in MySQL */
689
 
  reclength=max((ulong)file->min_record_length(table_options),reclength);
690
 
  if (info_length+(ulong) create_fields.elements*FCOMP+288+
691
 
      n_length+int_length+com_length > 65535L || int_count > 255)
692
 
  {
693
 
    my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0));
694
 
    return(1);
695
 
  }
696
 
 
697
 
  memset(forminfo, 0, 288);
698
 
  length=(info_length+create_fields.elements*FCOMP+288+n_length+int_length+
699
 
          com_length);
700
 
  int2store(forminfo,length);
701
 
  forminfo[256] = (uint8_t) screens;
702
 
  int2store(forminfo+258,create_fields.elements);
703
 
  int2store(forminfo+260,info_length);
704
 
  int2store(forminfo+262,totlength);
705
 
  int2store(forminfo+264,no_empty);
706
 
  int2store(forminfo+266,reclength);
707
 
  int2store(forminfo+268,n_length);
708
 
  int2store(forminfo+270,int_count);
709
 
  int2store(forminfo+272,int_parts);
710
 
  int2store(forminfo+274,int_length);
711
 
  int2store(forminfo+276,time_stamp_pos);
712
 
  int2store(forminfo+278,80);                   /* Columns needed */
713
 
  int2store(forminfo+280,22);                   /* Rows needed */
714
 
  int2store(forminfo+282,null_fields);
715
 
  int2store(forminfo+284,com_length);
716
 
  /* Up to forminfo+288 is free to use for additional information */
717
 
  return(0);
718
 
} /* pack_header */
719
 
 
720
 
 
721
 
        /* get each unique interval each own id */
722
 
 
723
 
static uint get_interval_id(uint *int_count,List<Create_field> &create_fields,
724
 
                            Create_field *last_field)
725
 
{
726
 
  List_iterator<Create_field> it(create_fields);
727
 
  Create_field *field;
728
 
  TYPELIB *interval=last_field->interval;
729
 
 
730
 
  while ((field=it++) != last_field)
731
 
  {
732
 
    if (field->interval_id && field->interval->count == interval->count)
733
 
    {
734
 
      const char **a,**b;
735
 
      for (a=field->interval->type_names, b=interval->type_names ;
736
 
           *a && !strcmp(*a,*b);
737
 
           a++,b++) ;
738
 
 
739
 
      if (! *a)
740
 
      {
741
 
        return field->interval_id;              // Re-use last interval
742
 
      }
743
 
    }
744
 
  }
745
 
  return ++*int_count;                          // New unique interval
746
 
}
747
 
 
748
 
 
749
 
        /* Save fields, fieldnames and intervals */
750
 
 
751
 
static bool pack_fields(File file, List<Create_field> &create_fields,
752
 
                        ulong data_offset)
753
 
{
754
 
  register uint i;
755
 
  uint int_count, comment_length=0;
756
 
  uchar buff[MAX_FIELD_WIDTH];
757
 
  Create_field *field;
758
 
  
759
 
 
760
 
        /* Write field info */
761
 
 
762
 
  List_iterator<Create_field> it(create_fields);
763
 
 
764
 
  int_count=0;
765
 
  while ((field=it++))
766
 
  {
767
 
    uint recpos;
768
 
    buff[0]= (uchar) field->row;
769
 
    buff[1]= (uchar) field->col;
770
 
    buff[2]= (uchar) field->sc_length;
771
 
    int2store(buff+3, field->length);
772
 
    /* The +1 is here becasue the col offset in .frm file have offset 1 */
773
 
    recpos= field->offset+1 + (uint) data_offset;
774
 
    int3store(buff+5,recpos);
775
 
    int2store(buff+8,field->pack_flag);
776
 
    int2store(buff+10,field->unireg_check);
777
 
    buff[12]= (uchar) field->interval_id;
778
 
    buff[13]= (uchar) field->sql_type; 
779
 
    if (field->charset) 
780
 
      buff[14]= (uchar) field->charset->number;
781
 
    else
782
 
      buff[14]= 0;                              // Numerical
783
 
    int2store(buff+15, field->comment.length);
784
 
    comment_length+= field->comment.length;
785
 
    set_if_bigger(int_count,field->interval_id);
786
 
    if (my_write(file, buff, FCOMP, MYF_RW))
787
 
      return(1);
788
 
  }
789
 
 
790
 
        /* Write fieldnames */
791
 
  buff[0]=(uchar) NAMES_SEP_CHAR;
792
 
  if (my_write(file, buff, 1, MYF_RW))
793
 
    return(1);
794
 
  i=0;
795
 
  it.rewind();
796
 
  while ((field=it++))
797
 
  {
798
 
    char *pos= stpcpy((char*) buff,field->field_name);
799
 
    *pos++=NAMES_SEP_CHAR;
800
 
    if (i == create_fields.elements-1)
801
 
      *pos++=0;
802
 
    if (my_write(file, buff, (size_t) (pos-(char*) buff),MYF_RW))
803
 
      return(1);
804
 
    i++;
805
 
  }
806
 
 
807
 
        /* Write intervals */
808
 
  if (int_count)
809
 
  {
810
 
    String tmp((char*) buff,sizeof(buff), &my_charset_bin);
811
 
    tmp.length(0);
812
 
    it.rewind();
813
 
    int_count=0;
814
 
    while ((field=it++))
815
 
    {
816
 
      if (field->interval_id > int_count)
817
 
      {
818
 
        unsigned char  sep= 0;
819
 
        unsigned char  occ[256];
820
 
        uint           i;
821
 
        unsigned char *val= NULL;
822
 
 
823
 
        memset(occ, 0, sizeof(occ));
824
 
 
825
 
        for (i=0; (val= (unsigned char*) field->interval->type_names[i]); i++)
826
 
          for (uint j = 0; j < field->interval->type_lengths[i]; j++)
827
 
            occ[(unsigned int) (val[j])]= 1;
828
 
 
829
 
        if (!occ[(unsigned char)NAMES_SEP_CHAR])
830
 
          sep= (unsigned char) NAMES_SEP_CHAR;
831
 
        else if (!occ[(unsigned int)','])
832
 
          sep= ',';
833
 
        else
834
 
        {
835
 
          for (uint i=1; i<256; i++)
836
 
          {
837
 
            if(!occ[i])
838
 
            {
839
 
              sep= i;
840
 
              break;
841
 
            }
842
 
          }
843
 
 
844
 
          if(!sep)    /* disaster, enum uses all characters, none left as separator */
845
 
          {
846
 
            my_message(ER_WRONG_FIELD_TERMINATORS,ER(ER_WRONG_FIELD_TERMINATORS),
847
 
                       MYF(0));
848
 
            return(1);
849
 
          }
850
 
        }
851
 
 
852
 
        int_count= field->interval_id;
853
 
        tmp.append(sep);
854
 
        for (const char **pos=field->interval->type_names ; *pos ; pos++)
855
 
        {
856
 
          tmp.append(*pos);
857
 
          tmp.append(sep);
858
 
        }
859
 
        tmp.append('\0');                      // End of intervall
860
 
      }
861
 
    }
862
 
    if (my_write(file,(uchar*) tmp.ptr(),tmp.length(),MYF_RW))
863
 
      return(1);
864
 
  }
865
 
  if (comment_length)
866
 
  {
867
 
    it.rewind();
868
 
    int_count=0;
869
 
    while ((field=it++))
870
 
    {
871
 
      if (field->comment.length)
872
 
        if (my_write(file, (uchar*) field->comment.str, field->comment.length,
873
 
                     MYF_RW))
874
 
          return(1);
875
 
    }
876
 
  }
877
 
  return(0);
878
 
}
879
 
 
880
 
 
881
 
/* save an empty record on start of formfile */
882
 
 
883
 
static bool make_empty_rec(THD *thd, File file,
884
 
                           enum legacy_db_type table_type __attribute__((unused)),
885
 
                           uint table_options,
886
 
                           List<Create_field> &create_fields,
887
 
                           uint reclength,
888
 
                           ulong data_offset,
889
 
                           handler *handler)
890
 
{
891
 
  int error= 0;
892
 
  Field::utype type;
893
 
  uint null_count;
894
 
  uchar *buff,*null_pos;
895
 
  TABLE table;
896
 
  TABLE_SHARE share;
897
 
  Create_field *field;
898
 
  enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
899
 
  
900
 
 
901
 
  /* We need a table to generate columns for default values */
902
 
  memset(&table, 0, sizeof(table));
903
 
  memset(&share, 0, sizeof(share));
904
 
  table.s= &share;
905
 
 
906
 
  if (!(buff=(uchar*) my_malloc((size_t) reclength,MYF(MY_WME | MY_ZEROFILL))))
907
 
  {
908
 
    return(1);
909
 
  }
910
 
 
911
 
  table.in_use= thd;
912
 
  table.s->db_low_byte_first= handler->low_byte_first();
913
 
  table.s->blob_ptr_size= portable_sizeof_char_ptr;
914
 
 
915
 
  null_count=0;
916
 
  if (!(table_options & HA_OPTION_PACK_RECORD))
917
 
  {
918
 
    null_count++;                       // Need one bit for delete mark
919
 
    *buff|= 1;
920
 
  }
921
 
  null_pos= buff;
922
 
 
923
 
  List_iterator<Create_field> it(create_fields);
924
 
  thd->count_cuted_fields= CHECK_FIELD_WARN;    // To find wrong default values
925
 
  while ((field=it++))
926
 
  {
927
 
    /*
928
 
      regfield don't have to be deleted as it's allocated with sql_alloc()
929
 
    */
930
 
    Field *regfield= make_field(&share,
931
 
                                buff+field->offset + data_offset,
932
 
                                field->length,
933
 
                                null_pos + null_count / 8,
934
 
                                null_count & 7,
935
 
                                field->pack_flag,
936
 
                                field->sql_type,
937
 
                                field->charset,
938
 
                                field->unireg_check,
939
 
                                field->save_interval ? field->save_interval :
940
 
                                field->interval, 
941
 
                                field->field_name);
942
 
    if (!regfield)
943
 
    {
944
 
      error= 1;
945
 
      goto err;                                 // End of memory
946
 
    }
947
 
 
948
 
    /* save_in_field() will access regfield->table->in_use */
949
 
    regfield->init(&table);
950
 
 
951
 
    if (!(field->flags & NOT_NULL_FLAG))
952
 
    {
953
 
      *regfield->null_ptr|= regfield->null_bit;
954
 
      null_count++;
955
 
    }
956
 
 
957
 
    type= (Field::utype) MTYP_TYPENR(field->unireg_check);
958
 
 
959
 
    if (field->def)
960
 
    {
961
 
      int res= field->def->save_in_field(regfield, 1);
962
 
      /* If not ok or warning of level 'note' */
963
 
      if (res != 0 && res != 3)
964
 
      {
965
 
        my_error(ER_INVALID_DEFAULT, MYF(0), regfield->field_name);
966
 
        error= 1;
967
 
        delete regfield; //To avoid memory leak
968
 
        goto err;
969
 
      }
970
 
    }
971
 
    else if (regfield->real_type() == DRIZZLE_TYPE_ENUM &&
972
 
             (field->flags & NOT_NULL_FLAG))
973
 
    {
974
 
      regfield->set_notnull();
975
 
      regfield->store((int64_t) 1, true);
976
 
    }
977
 
    else if (type == Field::YES)                // Old unireg type
978
 
      regfield->store(ER(ER_YES),(uint) strlen(ER(ER_YES)),system_charset_info);
979
 
    else if (type == Field::NO)                 // Old unireg type
980
 
      regfield->store(ER(ER_NO), (uint) strlen(ER(ER_NO)),system_charset_info);
981
 
    else
982
 
      regfield->reset();
983
 
  }
984
 
  assert(data_offset == ((null_count + 7) / 8));
985
 
 
986
 
  /*
987
 
    We need to set the unused bits to 1. If the number of bits is a multiple
988
 
    of 8 there are no unused bits.
989
 
  */
990
 
  if (null_count & 7)
991
 
    *(null_pos + null_count / 8)|= ~(((uchar) 1 << (null_count & 7)) - 1);
992
 
 
993
 
  error= my_write(file, buff, (size_t) reclength,MYF_RW) != 0;
994
 
 
995
 
err:
996
 
  my_free(buff, MYF(MY_FAE));
997
 
  thd->count_cuted_fields= old_count_cuted_fields;
998
 
  return(error);
999
 
} /* make_empty_rec */