351
357
} /* mysql_create_frm */
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)
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();
369
engine->set_name(create_info->db_type->name);
371
table_proto->set_name(table_name);
372
table_proto->set_type(drizzle::Table::STANDARD);
374
while ((field_arg= it++))
376
drizzle::Table::Field *attribute;
377
//drizzle::Table::Field::FieldConstraints *constraints;
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);
385
case DRIZZLE_TYPE_LONG:
386
attribute->set_type(drizzle::Table::Field::INTEGER);
388
case DRIZZLE_TYPE_DOUBLE:
389
attribute->set_type(drizzle::Table::Field::DOUBLE);
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);
396
case DRIZZLE_TYPE_LONGLONG:
397
attribute->set_type(drizzle::Table::Field::BIGINT);
399
case DRIZZLE_TYPE_TIME:
400
attribute->set_type(drizzle::Table::Field::TIME);
402
case DRIZZLE_TYPE_DATETIME:
403
attribute->set_type(drizzle::Table::Field::DATETIME);
405
case DRIZZLE_TYPE_DATE:
406
attribute->set_type(drizzle::Table::Field::DATE);
408
case DRIZZLE_TYPE_VARCHAR:
410
drizzle::Table::Field::StringFieldOptions *string_field_options;
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);
420
case DRIZZLE_TYPE_NEWDECIMAL:
422
drizzle::Table::Field::NumericFieldOptions *numeric_field_options;
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);
431
case DRIZZLE_TYPE_ENUM:
433
drizzle::Table::Field::SetFieldOptions *set_field_options;
435
assert(field_arg->interval);
437
attribute->set_type(drizzle::Table::Field::ENUM);
438
set_field_options= attribute->mutable_set_options();
440
for (uint32_t pos= 0; pos < field_arg->interval->count; pos++)
442
const char *src= field_arg->interval->type_names[pos];
444
set_field_options->add_value(src);
446
set_field_options->set_count_elements(set_field_options->value_size());
449
case DRIZZLE_TYPE_BLOB:
450
attribute->set_type(drizzle::Table::Field::BLOB);
457
field_constraints= attribute->mutable_constraints();
458
constraints->set_is_nullable(field_arg->def->null_value);
461
/* Set the comment */
462
if (field_arg->comment.length)
463
attribute->set_comment(field_arg->comment.str);
466
if (create_info->comment.length)
467
table_options->set_comment(create_info->comment.str);
469
if (create_info->table_charset)
471
table_options->set_collation_id(field_arg->charset->number);
472
table_options->set_collation(field_arg->charset->name);
475
if (create_info->connect_string.length)
476
table_options->set_connect_string(create_info->connect_string.str);
478
if (create_info->data_file_name)
479
table_options->set_data_file_name(create_info->data_file_name);
481
if (create_info->index_file_name)
482
table_options->set_index_file_name(create_info->index_file_name);
484
if (create_info->max_rows)
485
table_options->set_max_rows(create_info->max_rows);
487
if (create_info->min_rows)
488
table_options->set_min_rows(create_info->min_rows);
490
if (create_info->auto_increment_value)
491
table_options->set_auto_increment_value(create_info->auto_increment_value);
493
if (create_info->avg_row_length)
494
table_options->set_avg_row_length(create_info->avg_row_length);
496
if (create_info->key_block_size)
497
table_options->set_key_block_size(create_info->key_block_size);
499
if (create_info->block_size)
500
table_options->set_block_size(create_info->block_size);
503
int rename_table_proto_file(const char *from, const char* to)
505
string from_path(from);
507
string file_ext = ".tabledefinition";
509
from_path.append(file_ext);
510
to_path.append(file_ext);
512
return my_rename(from_path.c_str(),to_path.c_str(),MYF(MY_WME));
515
int delete_table_proto_file(char *file_name)
517
string new_path(file_name);
518
string file_ext = ".tabledefinition";
520
new_path.append(file_ext);
521
return my_delete(new_path.c_str(), MYF(0));
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)),
533
drizzle::Table table_proto;
534
string new_path(file_name);
535
string file_ext = ".tabledefinition";
537
fill_table_proto(&table_proto, table_name, create_fields, create_info);
539
new_path.replace(new_path.find(".frm"), file_ext.length(), file_ext );
541
fstream output(new_path.c_str(), ios::out | ios::trunc | ios::binary);
542
if (!table_proto.SerializeToOstream(&output))
544
printf("Failed to write schema.\n");
545
fprintf(stderr, "Failed to write schema.\n");
355
553
Create a frm (table definition) file and the tables