~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_proto_write.cc

  • Committer: Monty Taylor
  • Date: 2010-03-11 18:27:20 UTC
  • mfrom: (1333 staging)
  • mto: This revision was merged to the branch mainline in revision 1348.
  • Revision ID: mordred@inaugust.com-20100311182720-hd1h87y6cb1b1mp0
Merged trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
 
40
40
namespace drizzled {
41
41
 
42
 
int fill_table_proto(message::Table *table_proto,
43
 
                     const char *table_name,
44
 
                     List<CreateField> &create_fields,
45
 
                     HA_CREATE_INFO *create_info,
46
 
                     uint32_t keys,
47
 
                     KEY *key_info)
 
42
static int fill_table_proto(message::Table &table_proto,
 
43
                            const char *table_name,
 
44
                            List<CreateField> &create_fields,
 
45
                            HA_CREATE_INFO *create_info,
 
46
                            uint32_t keys,
 
47
                            KEY *key_info)
48
48
{
49
49
  CreateField *field_arg;
50
50
  List_iterator<CreateField> it(create_fields);
51
 
  message::Table::TableOptions *table_options= table_proto->mutable_options();
 
51
  message::Table::TableOptions *table_options= table_proto.mutable_options();
52
52
 
53
53
  if (create_fields.elements > MAX_FIELDS)
54
54
  {
56
56
    return(1);
57
57
  }
58
58
 
59
 
  assert(strcmp(table_proto->engine().name().c_str(),
 
59
  assert(strcmp(table_proto.engine().name().c_str(),
60
60
                create_info->db_type->getName().c_str())==0);
61
61
 
62
 
  assert(strcmp(table_proto->name().c_str(),table_name)==0);
 
62
  assert(strcmp(table_proto.name().c_str(),table_name)==0);
63
63
 
64
64
  int field_number= 0;
65
 
  bool use_existing_fields= table_proto->field_size() > 0;
 
65
  bool use_existing_fields= table_proto.field_size() > 0;
66
66
  while ((field_arg= it++))
67
67
  {
68
68
    message::Table::Field *attribute;
72
72
       filled out Field messages */
73
73
 
74
74
    if (use_existing_fields)
75
 
      attribute= table_proto->mutable_field(field_number++);
 
75
      attribute= table_proto.mutable_field(field_number++);
76
76
    else
77
77
    {
78
78
      /* Other code paths still have to fill out the proto */
79
 
      attribute= table_proto->add_field();
 
79
      attribute= table_proto.add_field();
80
80
 
81
81
      if(field_arg->flags & NOT_NULL_FLAG)
82
82
      {
247
247
      field_options->set_update_value("NOW()");
248
248
    }
249
249
 
 
250
    if (field_arg->def == NULL  && attribute->constraints().is_nullable())
 
251
    {
 
252
      message::Table::Field::FieldOptions *field_options;
 
253
      field_options= attribute->mutable_options();
 
254
 
 
255
      field_options->set_default_null(true);
 
256
    }
250
257
    if(field_arg->def)
251
258
    {
252
259
      message::Table::Field::FieldOptions *field_options;
253
260
      field_options= attribute->mutable_options();
254
 
 
 
261
 
255
262
      if(field_arg->def->is_null())
256
263
      {
257
264
        field_options->set_default_null(true);
297
304
 
298
305
  }
299
306
 
300
 
  assert(! use_existing_fields || (field_number == table_proto->field_size()));
 
307
  assert(! use_existing_fields || (field_number == table_proto.field_size()));
301
308
 
302
309
  switch(create_info->row_type)
303
310
  {
304
311
  case ROW_TYPE_DEFAULT:
305
 
    table_options->set_row_type(message::Table::TableOptions::ROW_TYPE_DEFAULT);
 
312
    /* No use setting a default row type... just adds redundant info to message */
306
313
    break;
307
314
  case ROW_TYPE_FIXED:
308
315
    table_options->set_row_type(message::Table::TableOptions::ROW_TYPE_FIXED);
326
333
    abort();
327
334
  }
328
335
 
329
 
  table_options->set_pack_record(create_info->table_options
330
 
                                 & HA_OPTION_PACK_RECORD);
 
336
  if (create_info->table_options & HA_OPTION_PACK_RECORD)
 
337
    table_options->set_pack_record(true);
331
338
 
332
339
  if (table_options->has_comment())
333
340
  {
357
364
  if (create_info->auto_increment_value)
358
365
    table_options->set_auto_increment_value(create_info->auto_increment_value);
359
366
 
360
 
  for (unsigned int i= 0; i < keys; i++)
 
367
  for (uint32_t i= 0; i < keys; i++)
361
368
  {
362
369
    message::Table::Index *idx;
363
370
 
364
 
    idx= table_proto->add_indexes();
 
371
    idx= table_proto.add_indexes();
365
372
 
366
373
    assert(test(key_info[i].flags & HA_USES_COMMENT) ==
367
374
           (key_info[i].comment.length > 0));
440
447
 
441
448
      idx->set_comment(key_info[i].comment.str);
442
449
    }
443
 
    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))
 
450
    if (key_info[i].flags & 
 
451
        ~(HA_NOSAME | HA_PACK_KEY | HA_USES_BLOCK_SIZE | 
 
452
          HA_BINARY_PACK_KEY | HA_VAR_LENGTH_PART | HA_NULL_PART_KEY | 
 
453
          HA_KEY_HAS_PART_KEY_SEG | HA_GENERATED_KEY | HA_USES_COMMENT))
444
454
      abort(); // Invalid (unknown) index flag.
445
455
 
446
456
    for(unsigned int j=0; j< key_info[i].key_parts; j++)
482
492
  return internal::my_delete(new_path.c_str(), MYF(0));
483
493
}
484
494
 
485
 
int drizzle_write_proto_file(const std::string file_name,
486
 
                             message::Table *table_proto)
487
 
{
488
 
  int fd= open(file_name.c_str(), O_RDWR|O_CREAT|O_TRUNC, internal::my_umask);
489
 
 
490
 
  if (fd == -1)
491
 
    return errno;
492
 
 
493
 
  google::protobuf::io::ZeroCopyOutputStream* output=
494
 
    new google::protobuf::io::FileOutputStream(fd);
495
 
 
496
 
  if (table_proto->SerializeToZeroCopyStream(output) == false)
497
 
  {
498
 
    delete output;
499
 
    close(fd);
500
 
    return errno;
501
 
  }
502
 
 
503
 
  delete output;
504
 
  close(fd);
505
 
  return 0;
506
 
}
507
 
 
508
495
/*
509
496
  Create a table definition proto file and the tables
510
497
 
526
513
 
527
514
int rea_create_table(Session *session,
528
515
                     TableIdentifier &identifier,
529
 
                     message::Table *table_proto,
 
516
                     message::Table &table_proto,
530
517
                     HA_CREATE_INFO *create_info,
531
518
                     List<CreateField> &create_fields,
532
519
                     uint32_t keys, KEY *key_info)
535
522
                      keys, key_info))
536
523
    return 1;
537
524
 
538
 
  string new_path(identifier.getPath());
539
 
  string file_ext = ".dfe";
540
 
 
541
 
  new_path.append(file_ext);
542
 
 
543
 
  int err= 0;
544
 
 
545
 
  plugin::StorageEngine* engine= plugin::StorageEngine::findByName(*session,
546
 
                                                                   table_proto->engine().name());
547
 
  if (engine->check_flag(HTON_BIT_HAS_DATA_DICTIONARY) == false)
548
 
    err= drizzle_write_proto_file(new_path, table_proto);
549
 
 
550
 
  if (err != 0)
551
 
  {
552
 
    if (err == ENOENT)
553
 
      my_error(ER_BAD_DB_ERROR,MYF(0), identifier.getDBName());
554
 
    else
555
 
      my_error(ER_CANT_CREATE_TABLE, MYF(0), identifier.getTableName(), err);
556
 
 
557
 
    goto err_handler;
558
 
  }
559
 
 
560
525
  if (plugin::StorageEngine::createTable(*session,
561
526
                                         identifier,
562
 
                                         false, *table_proto))
563
 
    goto err_handler;
 
527
                                         false, table_proto))
 
528
  {
 
529
    return 1;
 
530
  }
 
531
 
564
532
  return 0;
565
533
 
566
 
err_handler:
567
 
  if (engine->check_flag(HTON_BIT_HAS_DATA_DICTIONARY) == false)
568
 
    plugin::StorageEngine::deleteDefinitionFromPath(identifier);
569
 
 
570
 
  return 1;
571
534
} /* rea_create_table */
572
535
 
573
536
} /* namespace drizzled */