~drizzle-pbxt/drizzle/drizzle-pbxt-2

« back to all changes in this revision

Viewing changes to drizzled/table_proto_write.cc

  • Committer: Paul McCullagh
  • Date: 2009-11-10 14:18:39 UTC
  • mfrom: (1038.1.7 drizzle-pbxt-pre-merge)
  • Revision ID: paul.mccullagh@primebase.org-20091110141839-2j3k43b17ag6f605
Merged Drizzle trunk and PBXT 1.0.09

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include <drizzled/message/table.pb.h>
26
26
#include <google/protobuf/io/zero_copy_stream.h>
27
27
#include <google/protobuf/io/zero_copy_stream_impl.h>
 
28
 
 
29
#include <drizzled/table_proto.h>
 
30
 
28
31
using namespace std;
29
32
 
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;
50
 
}
51
 
 
52
 
static int fill_table_proto(drizzled::message::Table *table_proto,
53
 
                            const char *table_name,
54
 
                            List<Create_field> &create_fields,
55
 
                            HA_CREATE_INFO *create_info,
56
 
                            uint32_t keys,
57
 
                            KEY *key_info)
58
 
{
59
 
  Create_field *field_arg;
60
 
  List_iterator<Create_field> it(create_fields);
61
 
  drizzled::message::Table::StorageEngine *engine= table_proto->mutable_engine();
62
 
  drizzled::message::Table::TableOptions *table_options= table_proto->mutable_options();
 
33
namespace drizzled {
 
34
 
 
35
int fill_table_proto(message::Table *table_proto,
 
36
                     const char *table_name,
 
37
                     List<CreateField> &create_fields,
 
38
                     HA_CREATE_INFO *create_info,
 
39
                     uint32_t keys,
 
40
                     KEY *key_info)
 
41
{
 
42
  CreateField *field_arg;
 
43
  List_iterator<CreateField> it(create_fields);
 
44
  message::Table::TableOptions *table_options= table_proto->mutable_options();
63
45
 
64
46
  if (create_fields.elements > MAX_FIELDS)
65
47
  {
67
49
    return(1);
68
50
  }
69
51
 
70
 
  engine->set_name(create_info->db_type->getName());
 
52
  assert(strcmp(table_proto->engine().name().c_str(),
 
53
                create_info->db_type->getName().c_str())==0);
71
54
 
72
55
  assert(strcmp(table_proto->name().c_str(),table_name)==0);
73
56
 
74
57
  while ((field_arg= it++))
75
58
  {
76
 
    drizzled::message::Table::Field *attribute;
 
59
    message::Table::Field *attribute;
77
60
 
78
61
    attribute= table_proto->add_field();
79
62
    attribute->set_name(field_arg->field_name);
80
63
 
81
 
    attribute->set_pack_flag(field_arg->pack_flag); /* TODO: MUST DIE */
82
 
 
83
 
    if(f_maybe_null(field_arg->pack_flag))
 
64
    if(! (field_arg->flags & NOT_NULL_FLAG))
84
65
    {
85
 
      drizzled::message::Table::Field::FieldConstraints *constraints;
 
66
      message::Table::Field::FieldConstraints *constraints;
86
67
 
87
68
      constraints= attribute->mutable_constraints();
88
69
      constraints->set_is_nullable(true);
89
70
    }
90
71
 
91
72
    switch (field_arg->sql_type) {
92
 
    case DRIZZLE_TYPE_TINY:
93
 
      attribute->set_type(drizzled::message::Table::Field::TINYINT);
94
 
      break;
95
73
    case DRIZZLE_TYPE_LONG:
96
 
      attribute->set_type(drizzled::message::Table::Field::INTEGER);
 
74
      attribute->set_type(message::Table::Field::INTEGER);
97
75
      break;
98
76
    case DRIZZLE_TYPE_DOUBLE:
99
 
      attribute->set_type(drizzled::message::Table::Field::DOUBLE);
 
77
      {
 
78
        attribute->set_type(drizzled::message::Table::Field::DOUBLE);
 
79
 
 
80
        /* 
 
81
         * For DOUBLE, we only add a specific scale and precision iff
 
82
         * the fixed decimal point has been specified...
 
83
         */
 
84
        if (field_arg->decimals != NOT_FIXED_DEC)
 
85
        {
 
86
          drizzled::message::Table::Field::NumericFieldOptions *numeric_field_options;
 
87
          
 
88
          numeric_field_options= attribute->mutable_numeric_options();
 
89
          /* 
 
90
           * Precision and scale are specified like so:
 
91
           *
 
92
           * DOUBLE(P,S)
 
93
           *
 
94
           * From the CreateField, we get the "length", which is the *total* length
 
95
           * of the double storage space, including the decimal point if there is a
 
96
           * scale argument and a 1 byte length header.  We also get the "decimals", 
 
97
           * which is actually the scale (the number of numbers stored after the decimal point)
 
98
           *
 
99
           * Therefore, PRECISION= LENGTH - 1 - (SCALE ? SCALE + 1 : 0)
 
100
           */
 
101
          if (field_arg->decimals)
 
102
            numeric_field_options->set_precision(field_arg->length - 2); /* One for the decimal, one for the header */
 
103
          else
 
104
            numeric_field_options->set_precision(field_arg->length - 1); /* for the header */
 
105
          numeric_field_options->set_scale(field_arg->decimals);
 
106
        }
 
107
      }
100
108
      break;
101
109
    case DRIZZLE_TYPE_NULL  :
102
110
      assert(1); /* Not a user definable type */
103
111
    case DRIZZLE_TYPE_TIMESTAMP:
104
 
      attribute->set_type(drizzled::message::Table::Field::TIMESTAMP);
 
112
      attribute->set_type(message::Table::Field::TIMESTAMP);
105
113
      break;
106
114
    case DRIZZLE_TYPE_LONGLONG:
107
 
      attribute->set_type(drizzled::message::Table::Field::BIGINT);
 
115
      attribute->set_type(message::Table::Field::BIGINT);
108
116
      break;
109
117
    case DRIZZLE_TYPE_DATETIME:
110
 
      attribute->set_type(drizzled::message::Table::Field::DATETIME);
 
118
      attribute->set_type(message::Table::Field::DATETIME);
111
119
      break;
112
120
    case DRIZZLE_TYPE_DATE:
113
 
      attribute->set_type(drizzled::message::Table::Field::DATE);
 
121
      attribute->set_type(message::Table::Field::DATE);
114
122
      break;
115
123
    case DRIZZLE_TYPE_VARCHAR:
116
124
      {
117
 
        drizzled::message::Table::Field::StringFieldOptions *string_field_options;
 
125
        message::Table::Field::StringFieldOptions *string_field_options;
118
126
 
119
127
        string_field_options= attribute->mutable_string_options();
120
 
        attribute->set_type(drizzled::message::Table::Field::VARCHAR);
 
128
        attribute->set_type(message::Table::Field::VARCHAR);
121
129
        string_field_options->set_length(field_arg->length
122
130
                                         / field_arg->charset->mbmaxlen);
123
131
        string_field_options->set_collation_id(field_arg->charset->number);
127
135
      }
128
136
    case DRIZZLE_TYPE_NEWDECIMAL:
129
137
      {
130
 
        drizzled::message::Table::Field::NumericFieldOptions *numeric_field_options;
 
138
        message::Table::Field::NumericFieldOptions *numeric_field_options;
131
139
 
132
 
        attribute->set_type(drizzled::message::Table::Field::DECIMAL);
 
140
        attribute->set_type(message::Table::Field::DECIMAL);
133
141
        numeric_field_options= attribute->mutable_numeric_options();
134
142
        /* This is magic, I hate magic numbers -Brian */
135
143
        numeric_field_options->set_precision(field_arg->length + ( field_arg->decimals ? -2 : -1));
138
146
      }
139
147
    case DRIZZLE_TYPE_ENUM:
140
148
      {
141
 
        drizzled::message::Table::Field::SetFieldOptions *set_field_options;
 
149
        message::Table::Field::SetFieldOptions *set_field_options;
142
150
 
143
151
        assert(field_arg->interval);
144
152
 
145
 
        attribute->set_type(drizzled::message::Table::Field::ENUM);
 
153
        attribute->set_type(message::Table::Field::ENUM);
146
154
        set_field_options= attribute->mutable_set_options();
147
155
 
148
156
        for (uint32_t pos= 0; pos < field_arg->interval->count; pos++)
158
166
      }
159
167
    case DRIZZLE_TYPE_BLOB:
160
168
      {
161
 
        attribute->set_type(drizzled::message::Table::Field::BLOB);
 
169
        attribute->set_type(message::Table::Field::BLOB);
162
170
 
163
 
        drizzled::message::Table::Field::StringFieldOptions *string_field_options;
 
171
        message::Table::Field::StringFieldOptions *string_field_options;
164
172
 
165
173
        string_field_options= attribute->mutable_string_options();
166
174
        string_field_options->set_collation_id(field_arg->charset->number);
182
190
    case COLUMN_FORMAT_TYPE_NOT_USED:
183
191
      break;
184
192
    case COLUMN_FORMAT_TYPE_DEFAULT:
185
 
      attribute->set_format(drizzled::message::Table::Field::DefaultFormat);
 
193
      attribute->set_format(message::Table::Field::DefaultFormat);
186
194
      break;
187
195
    case COLUMN_FORMAT_TYPE_FIXED:
188
 
      attribute->set_format(drizzled::message::Table::Field::FixedFormat);
 
196
      attribute->set_format(message::Table::Field::FixedFormat);
189
197
      break;
190
198
    case COLUMN_FORMAT_TYPE_DYNAMIC:
191
 
      attribute->set_format(drizzled::message::Table::Field::DynamicFormat);
 
199
      attribute->set_format(message::Table::Field::DynamicFormat);
192
200
      break;
193
201
    default:
194
202
      assert(0); /* Tell us, since this shouldn't happend */
216
224
 
217
225
    if(field_arg->unireg_check == Field::NEXT_NUMBER)
218
226
    {
219
 
      drizzled::message::Table::Field::NumericFieldOptions *field_options;
 
227
      message::Table::Field::NumericFieldOptions *field_options;
220
228
      field_options= attribute->mutable_numeric_options();
221
229
      field_options->set_is_autoincrement(true);
222
230
    }
224
232
    if(field_arg->unireg_check == Field::TIMESTAMP_DN_FIELD
225
233
       || field_arg->unireg_check == Field::TIMESTAMP_DNUN_FIELD)
226
234
    {
227
 
      drizzled::message::Table::Field::FieldOptions *field_options;
 
235
      message::Table::Field::FieldOptions *field_options;
228
236
      field_options= attribute->mutable_options();
229
237
      field_options->set_default_value("NOW()");
230
238
    }
232
240
    if(field_arg->unireg_check == Field::TIMESTAMP_UN_FIELD
233
241
       || field_arg->unireg_check == Field::TIMESTAMP_DNUN_FIELD)
234
242
    {
235
 
      drizzled::message::Table::Field::FieldOptions *field_options;
 
243
      message::Table::Field::FieldOptions *field_options;
236
244
      field_options= attribute->mutable_options();
237
245
      field_options->set_update_value("NOW()");
238
246
    }
239
247
 
240
248
    if(field_arg->def)
241
249
    {
242
 
      drizzled::message::Table::Field::FieldOptions *field_options;
 
250
      message::Table::Field::FieldOptions *field_options;
243
251
      field_options= attribute->mutable_options();
244
252
 
245
253
      if(field_arg->def->is_null())
280
288
    }
281
289
 
282
290
    {
283
 
      drizzled::message::Table::Field::FieldOptions *field_options;
 
291
      message::Table::Field::FieldOptions *field_options;
284
292
      field_options= attribute->mutable_options();
285
293
 
286
294
      field_options->set_length(field_arg->length);
294
302
 
295
303
  }
296
304
 
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
305
  switch(create_info->row_type)
345
306
  {
346
307
  case ROW_TYPE_DEFAULT:
347
 
    table_options->set_row_type(drizzled::message::Table::TableOptions::ROW_TYPE_DEFAULT);
 
308
    table_options->set_row_type(message::Table::TableOptions::ROW_TYPE_DEFAULT);
348
309
    break;
349
310
  case ROW_TYPE_FIXED:
350
 
    table_options->set_row_type(drizzled::message::Table::TableOptions::ROW_TYPE_FIXED);
 
311
    table_options->set_row_type(message::Table::TableOptions::ROW_TYPE_FIXED);
351
312
    break;
352
313
  case ROW_TYPE_DYNAMIC:
353
 
    table_options->set_row_type(drizzled::message::Table::TableOptions::ROW_TYPE_DYNAMIC);
 
314
    table_options->set_row_type(message::Table::TableOptions::ROW_TYPE_DYNAMIC);
354
315
    break;
355
316
  case ROW_TYPE_COMPRESSED:
356
 
    table_options->set_row_type(drizzled::message::Table::TableOptions::ROW_TYPE_COMPRESSED);
 
317
    table_options->set_row_type(message::Table::TableOptions::ROW_TYPE_COMPRESSED);
357
318
    break;
358
319
  case ROW_TYPE_REDUNDANT:
359
 
    table_options->set_row_type(drizzled::message::Table::TableOptions::ROW_TYPE_REDUNDANT);
 
320
    table_options->set_row_type(message::Table::TableOptions::ROW_TYPE_REDUNDANT);
360
321
    break;
361
322
  case ROW_TYPE_COMPACT:
362
 
    table_options->set_row_type(drizzled::message::Table::TableOptions::ROW_TYPE_COMPACT);
 
323
    table_options->set_row_type(message::Table::TableOptions::ROW_TYPE_COMPACT);
363
324
    break;
364
325
  case ROW_TYPE_PAGE:
365
 
    table_options->set_row_type(drizzled::message::Table::TableOptions::ROW_TYPE_PAGE);
 
326
    table_options->set_row_type(message::Table::TableOptions::ROW_TYPE_PAGE);
366
327
    break;
367
328
  default:
368
329
    abort();
371
332
  table_options->set_pack_record(create_info->table_options
372
333
                                 & HA_OPTION_PACK_RECORD);
373
334
 
374
 
  if (create_info->comment.length)
 
335
  if (table_options->has_comment())
375
336
  {
376
337
    uint32_t tmp_len;
377
338
    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);
 
339
                                                table_options->comment().c_str(),
 
340
                                                table_options->comment().c_str() +
 
341
                                                table_options->comment().length(),
 
342
                                                TABLE_COMMENT_MAXLEN);
382
343
 
383
 
    if (tmp_len < create_info->comment.length)
 
344
    if (tmp_len < table_options->comment().length())
384
345
    {
385
346
      my_error(ER_WRONG_STRING_LENGTH, MYF(0),
386
 
               create_info->comment.str,"Table COMMENT",
387
 
               (uint32_t) TABLE_COMMENT_MAXLEN);
 
347
               table_options->comment().c_str(),"Table COMMENT",
 
348
               (uint32_t) TABLE_COMMENT_MAXLEN);
388
349
      return(1);
389
350
    }
 
351
  }
390
352
 
391
 
    table_options->set_comment(create_info->comment.str);
392
 
  }
393
353
  if (create_info->default_table_charset)
394
354
  {
395
355
    table_options->set_collation_id(
397
357
    table_options->set_collation(create_info->default_table_charset->name);
398
358
  }
399
359
 
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
360
  if (create_info->auto_increment_value)
416
361
    table_options->set_auto_increment_value(create_info->auto_increment_value);
417
362
 
418
 
  if (create_info->avg_row_length)
419
 
    table_options->set_avg_row_length(create_info->avg_row_length);
420
 
 
421
363
  if (create_info->key_block_size)
422
364
    table_options->set_key_block_size(create_info->key_block_size);
423
365
 
424
 
  if (create_info->block_size)
425
 
    table_options->set_block_size(create_info->block_size);
426
 
 
427
366
  for (unsigned int i= 0; i < keys; i++)
428
367
  {
429
 
    drizzled::message::Table::Index *idx;
 
368
    message::Table::Index *idx;
430
369
 
431
370
    idx= table_proto->add_indexes();
432
371
 
445
384
    switch(key_info[i].algorithm)
446
385
    {
447
386
    case HA_KEY_ALG_HASH:
448
 
      idx->set_type(drizzled::message::Table::Index::HASH);
 
387
      idx->set_type(message::Table::Index::HASH);
449
388
      break;
450
389
 
451
390
    case HA_KEY_ALG_BTREE:
452
 
      idx->set_type(drizzled::message::Table::Index::BTREE);
 
391
      idx->set_type(message::Table::Index::BTREE);
453
392
      break;
454
393
 
455
394
    case HA_KEY_ALG_RTREE:
456
 
      idx->set_type(drizzled::message::Table::Index::RTREE);
 
395
      idx->set_type(message::Table::Index::RTREE);
457
396
    case HA_KEY_ALG_FULLTEXT:
458
 
      idx->set_type(drizzled::message::Table::Index::FULLTEXT);
 
397
      idx->set_type(message::Table::Index::FULLTEXT);
459
398
    case HA_KEY_ALG_UNDEF:
460
 
      idx->set_type(drizzled::message::Table::Index::UNKNOWN_INDEX);
 
399
      idx->set_type(message::Table::Index::UNKNOWN_INDEX);
461
400
      break;
462
401
 
463
402
    default:
469
408
    else
470
409
      idx->set_is_unique(false);
471
410
 
472
 
    drizzled::message::Table::Index::IndexOptions *index_options= idx->mutable_options();
 
411
    message::Table::Index::IndexOptions *index_options= idx->mutable_options();
473
412
 
474
413
    if(key_info[i].flags & HA_USES_BLOCK_SIZE)
475
414
      index_options->set_key_block_size(key_info[i].block_size);
516
455
 
517
456
    for(unsigned int j=0; j< key_info[i].key_parts; j++)
518
457
    {
519
 
      drizzled::message::Table::Index::IndexPart *idxpart;
 
458
      message::Table::Index::IndexPart *idxpart;
520
459
 
521
460
      idxpart= idx->add_index_part();
522
461
 
532
471
  return 0;
533
472
}
534
473
 
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
474
int rename_table_proto_file(const char *from, const char* to)
549
475
{
550
476
  string from_path(from);
566
492
  return my_delete(new_path.c_str(), MYF(0));
567
493
}
568
494
 
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<Create_field> &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
 
  }
 
495
int drizzle_write_proto_file(const std::string file_name,
 
496
                             message::Table *table_proto)
 
497
{
 
498
  int fd= open(file_name.c_str(), O_RDWR|O_CREAT|O_TRUNC, my_umask);
 
499
 
 
500
  if (fd == -1)
 
501
    return errno;
611
502
 
612
503
  google::protobuf::io::ZeroCopyOutputStream* output=
613
504
    new google::protobuf::io::FileOutputStream(fd);
614
505
 
615
 
  if (!table_proto->SerializeToZeroCopyStream(output))
 
506
  if (table_proto->SerializeToZeroCopyStream(output) == false)
616
507
  {
617
508
    delete output;
618
509
    close(fd);
638
529
    keys                number of keys to create
639
530
    key_info            Keys to create
640
531
    file                Handler to use
641
 
    is_like             is true for mysql_create_like_schema_frm
 
532
    is_like             is true for drizzled::create_like_schema_frm
642
533
 
643
534
  RETURN
644
535
    0  ok
647
538
 
648
539
int rea_create_table(Session *session, const char *path,
649
540
                     const char *db, const char *table_name,
650
 
                     drizzled::message::Table *table_proto,
 
541
                     message::Table *table_proto,
651
542
                     HA_CREATE_INFO *create_info,
652
 
                     List<Create_field> &create_fields,
653
 
                     uint32_t keys, KEY *key_info,
654
 
                     bool is_like)
 
543
                     List<CreateField> &create_fields,
 
544
                     uint32_t keys, KEY *key_info)
655
545
{
656
546
  /* Proto will blow up unless we give a name */
657
547
  assert(table_name);
658
548
 
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
 
  // Make sure mysql_create_frm din't remove extension
679
 
  if (session->variables.keep_files_on_create)
680
 
    create_info->options|= HA_CREATE_KEEP_FILES;
681
 
 
682
 
  if (ha_create_table(session, path, db, table_name,
683
 
                      create_info,0))
 
549
  if (fill_table_proto(table_proto, table_name, create_fields, create_info,
 
550
                      keys, key_info))
 
551
    return 1;
 
552
 
 
553
  string new_path(path);
 
554
  string file_ext = ".dfe";
 
555
 
 
556
  new_path.append(file_ext);
 
557
 
 
558
  int err= 0;
 
559
 
 
560
  plugin::StorageEngine* engine= plugin::StorageEngine::findByName(session,
 
561
                                            table_proto->engine().name());
 
562
  if (engine->check_flag(HTON_BIT_HAS_DATA_DICTIONARY) == false)
 
563
    err= drizzle_write_proto_file(new_path, table_proto);
 
564
 
 
565
  if (err != 0)
 
566
  {
 
567
    if (err == ENOENT)
 
568
      my_error(ER_BAD_DB_ERROR,MYF(0),db);
 
569
    else
 
570
      my_error(ER_CANT_CREATE_TABLE,MYF(0),table_name,err);
 
571
 
 
572
    goto err_handler;
 
573
  }
 
574
 
 
575
  if (plugin::StorageEngine::createTable(session, path, db, table_name,
 
576
                                         create_info, false, table_proto))
684
577
    goto err_handler;
685
578
  return 0;
686
579
 
687
580
err_handler:
688
 
  delete_table_proto_file(path);
 
581
  if (engine->check_flag(HTON_BIT_HAS_DATA_DICTIONARY) == false)
 
582
    delete_table_proto_file(path);
689
583
 
690
584
  return 1;
691
585
} /* rea_create_table */
 
586
 
 
587
} /* namespace drizzled */