~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_proto_write.cc

Moved the last of the libdrizzleclient calls into Protocol.

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