~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/unireg.cc

  • Committer: Brian Aker
  • Date: 2008-11-16 02:03:36 UTC
  • mfrom: (584.2.8 drizzle-nofrm)
  • Revision ID: brian@tangent.org-20081116020336-89horp2vrgqoqv0f
Merge stewert

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include <drizzled/server_includes.h>
27
27
#include <drizzled/error.h>
28
28
 
 
29
/* For proto */
 
30
#include <string>
 
31
#include <fstream>
 
32
#include <drizzled/serialize/serialize.h>
 
33
using namespace std;
 
34
 
29
35
#define FCOMP                   17              /* Bytes for a packed field */
30
36
 
31
37
static unsigned char * pack_screens(List<Create_field> &create_fields,
350
356
  return(1);
351
357
} /* mysql_create_frm */
352
358
 
 
359
static void fill_table_proto(drizzle::Table *table_proto,
 
360
                             const char *table_name,
 
361
                             List<Create_field> &create_fields,
 
362
                             HA_CREATE_INFO *create_info)
 
363
{
 
364
  Create_field *field_arg;
 
365
  List_iterator<Create_field> it(create_fields);
 
366
  drizzle::Table::StorageEngine *engine= table_proto->mutable_engine();
 
367
  drizzle::Table::TableOptions *table_options= table_proto->mutable_options();
 
368
 
 
369
  engine->set_name(create_info->db_type->name);
 
370
 
 
371
  table_proto->set_name(table_name);
 
372
  table_proto->set_type(drizzle::Table::STANDARD);
 
373
 
 
374
  while ((field_arg= it++))
 
375
  {
 
376
    drizzle::Table::Field *attribute;
 
377
    //drizzle::Table::Field::FieldConstraints *constraints;
 
378
 
 
379
    attribute= table_proto->add_field();
 
380
    attribute->set_name(field_arg->field_name);
 
381
    switch (field_arg->sql_type) {
 
382
    case DRIZZLE_TYPE_TINY:
 
383
      attribute->set_type(drizzle::Table::Field::TINYINT);
 
384
      break;
 
385
    case DRIZZLE_TYPE_LONG:
 
386
      attribute->set_type(drizzle::Table::Field::INTEGER);
 
387
      break;
 
388
    case DRIZZLE_TYPE_DOUBLE:
 
389
      attribute->set_type(drizzle::Table::Field::DOUBLE);
 
390
      break;
 
391
    case DRIZZLE_TYPE_NULL  :
 
392
      assert(1); /* Not a user definable type */
 
393
    case DRIZZLE_TYPE_TIMESTAMP:
 
394
      attribute->set_type(drizzle::Table::Field::TIMESTAMP);
 
395
      break;
 
396
    case DRIZZLE_TYPE_LONGLONG:
 
397
      attribute->set_type(drizzle::Table::Field::BIGINT);
 
398
      break;
 
399
    case DRIZZLE_TYPE_TIME:
 
400
      attribute->set_type(drizzle::Table::Field::TIME);
 
401
      break;
 
402
    case DRIZZLE_TYPE_DATETIME:
 
403
      attribute->set_type(drizzle::Table::Field::DATETIME);
 
404
      break;
 
405
    case DRIZZLE_TYPE_DATE:
 
406
      attribute->set_type(drizzle::Table::Field::DATE);
 
407
      break;
 
408
    case DRIZZLE_TYPE_VARCHAR:
 
409
      {
 
410
        drizzle::Table::Field::StringFieldOptions *string_field_options;
 
411
 
 
412
        string_field_options= attribute->mutable_string_options();
 
413
        attribute->set_type(drizzle::Table::Field::VARCHAR);
 
414
        string_field_options->set_length(field_arg->char_length);
 
415
        string_field_options->set_collation_id(field_arg->charset->number);
 
416
        string_field_options->set_collation(field_arg->charset->name);
 
417
 
 
418
        break;
 
419
      }
 
420
    case DRIZZLE_TYPE_NEWDECIMAL:
 
421
      {
 
422
        drizzle::Table::Field::NumericFieldOptions *numeric_field_options;
 
423
 
 
424
        attribute->set_type(drizzle::Table::Field::DECIMAL);
 
425
        numeric_field_options= attribute->mutable_numeric_options();
 
426
        /* This is magic, I hate magic numbers -Brian */
 
427
        numeric_field_options->set_precision(field_arg->length + ( field_arg->decimals ? -2 : -1));
 
428
        numeric_field_options->set_scale(field_arg->decimals);
 
429
        break;
 
430
      }
 
431
    case DRIZZLE_TYPE_ENUM:
 
432
      {
 
433
        drizzle::Table::Field::SetFieldOptions *set_field_options;
 
434
 
 
435
        assert(field_arg->interval);
 
436
 
 
437
        attribute->set_type(drizzle::Table::Field::ENUM);
 
438
        set_field_options= attribute->mutable_set_options();
 
439
 
 
440
        for (uint32_t pos= 0; pos < field_arg->interval->count; pos++)
 
441
        {
 
442
          const char *src= field_arg->interval->type_names[pos];
 
443
 
 
444
          set_field_options->add_value(src);
 
445
        }
 
446
        set_field_options->set_count_elements(set_field_options->value_size());
 
447
        break;
 
448
      }
 
449
    case DRIZZLE_TYPE_BLOB:
 
450
      attribute->set_type(drizzle::Table::Field::BLOB);
 
451
      break;
 
452
    default:
 
453
      assert(1);
 
454
    }
 
455
 
 
456
#ifdef NOTDONE
 
457
    field_constraints= attribute->mutable_constraints();
 
458
    constraints->set_is_nullable(field_arg->def->null_value);
 
459
#endif
 
460
 
 
461
    /* Set the comment */
 
462
    if (field_arg->comment.length)
 
463
      attribute->set_comment(field_arg->comment.str);
 
464
  }
 
465
 
 
466
  if (create_info->comment.length)
 
467
    table_options->set_comment(create_info->comment.str);
 
468
 
 
469
  if (create_info->table_charset)
 
470
  {
 
471
    table_options->set_collation_id(field_arg->charset->number);
 
472
    table_options->set_collation(field_arg->charset->name);
 
473
  }
 
474
 
 
475
  if (create_info->connect_string.length)
 
476
    table_options->set_connect_string(create_info->connect_string.str);
 
477
 
 
478
  if (create_info->data_file_name)
 
479
    table_options->set_data_file_name(create_info->data_file_name);
 
480
 
 
481
  if (create_info->index_file_name)
 
482
    table_options->set_index_file_name(create_info->index_file_name);
 
483
 
 
484
  if (create_info->max_rows)
 
485
    table_options->set_max_rows(create_info->max_rows);
 
486
 
 
487
  if (create_info->min_rows)
 
488
    table_options->set_min_rows(create_info->min_rows);
 
489
 
 
490
  if (create_info->auto_increment_value)
 
491
    table_options->set_auto_increment_value(create_info->auto_increment_value);
 
492
 
 
493
  if (create_info->avg_row_length)
 
494
    table_options->set_avg_row_length(create_info->avg_row_length);
 
495
 
 
496
  if (create_info->key_block_size)
 
497
    table_options->set_key_block_size(create_info->key_block_size);
 
498
 
 
499
  if (create_info->block_size)
 
500
    table_options->set_block_size(create_info->block_size);
 
501
}
 
502
 
 
503
int rename_table_proto_file(const char *from, const char* to)
 
504
{
 
505
  string from_path(from);
 
506
  string to_path(to);
 
507
  string file_ext = ".tabledefinition";
 
508
 
 
509
  from_path.append(file_ext);
 
510
  to_path.append(file_ext);
 
511
 
 
512
  return my_rename(from_path.c_str(),to_path.c_str(),MYF(MY_WME));
 
513
}
 
514
 
 
515
int delete_table_proto_file(char *file_name)
 
516
{
 
517
  string new_path(file_name);
 
518
  string file_ext = ".tabledefinition";
 
519
 
 
520
  new_path.append(file_ext);
 
521
  return my_delete(new_path.c_str(), MYF(0));
 
522
}
 
523
 
 
524
int create_table_proto_file(char *file_name,
 
525
                            const char *table_name,
 
526
                            HA_CREATE_INFO *create_info,
 
527
                            List<Create_field> &create_fields,
 
528
                            uint32_t keys __attribute__((unused)),
 
529
                            KEY *key_info)
 
530
{
 
531
  (void)key_info;
 
532
 
 
533
  drizzle::Table table_proto;
 
534
  string new_path(file_name);
 
535
  string file_ext = ".tabledefinition";
 
536
 
 
537
  fill_table_proto(&table_proto, table_name, create_fields, create_info);
 
538
 
 
539
  new_path.replace(new_path.find(".frm"), file_ext.length(), file_ext );
 
540
 
 
541
  fstream output(new_path.c_str(), ios::out | ios::trunc | ios::binary);
 
542
  if (!table_proto.SerializeToOstream(&output))
 
543
  {
 
544
    printf("Failed to write schema.\n");
 
545
    fprintf(stderr, "Failed to write schema.\n");
 
546
    return -1;
 
547
  }
 
548
 
 
549
  return 0;
 
550
}
353
551
 
354
552
/*
355
553
  Create a frm (table definition) file and the tables
377
575
                     List<Create_field> &create_fields,
378
576
                     uint32_t keys, KEY *key_info, handler *file)
379
577
{
380
 
  
381
 
 
382
578
  char frm_name[FN_REFLEN];
383
579
  strxmov(frm_name, path, reg_ext, NULL);
384
580
  if (mysql_create_frm(session, frm_name, db, table_name, create_info,
386
582
 
387
583
    return(1);
388
584
 
 
585
  if (create_table_proto_file(frm_name, table_name, create_info,
 
586
                              create_fields, keys, key_info) != 0)
 
587
    return 1;
 
588
 
389
589
  // Make sure mysql_create_frm din't remove extension
390
590
  assert(*fn_rext(frm_name));
391
591
  if (session->variables.keep_files_on_create)