450
452
message::Table src_proto;
451
StorageEngine* engine;
453
StorageEngine *engine;
453
455
error_proto= StorageEngine::getTableDefinition(session, identifier, src_proto);
455
457
if (error_proto == ER_CORRUPT_TABLE_DEFINITION)
457
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
458
src_proto.InitializationErrorString().c_str());
459
string error_message;
461
error_message.append(identifier.getSQLPath());
462
error_message.append(" : ");
463
error_message.append(src_proto.InitializationErrorString());
465
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), error_message.c_str());
459
467
return ER_CORRUPT_TABLE_DEFINITION;
466
std::string path(identifier.getPath());
467
engine->setTransactionReadWrite(session);
468
error= engine->doDropTable(session, identifier);
472
if (not engine->check_flag(HTON_BIT_HAS_DATA_DICTIONARY))
474
uint64_t counter; // @todo We need to refactor to check that.
476
for_each(vector_of_schema_engines.begin(), vector_of_schema_engines.end(),
477
DropTable(session, identifier, counter));
474
error= StorageEngine::dropTable(session, *engine, identifier);
482
477
if (error_proto && error == 0)
483
int StorageEngine::dropTable(Session& session,
484
StorageEngine &engine,
485
TableIdentifier &identifier)
489
engine.setTransactionReadWrite(session);
490
error= engine.doDropTable(session, identifier);
494
if (not engine.check_flag(HTON_BIT_HAS_DATA_DICTIONARY))
496
uint64_t counter; // @todo We need to refactor to check that.
498
for_each(vector_of_schema_engines.begin(), vector_of_schema_engines.end(),
499
DropTable(session, identifier, counter));
489
507
Initiates table-file and calls appropriate database-creator.
627
645
class StorageEngineGetSchemaDefinition: public unary_function<StorageEngine *, bool>
629
const std::string &schema_name;
647
std::string schema_name;
630
648
message::Schema &schema_proto;
633
StorageEngineGetSchemaDefinition(const std::string &schema_name_arg,
651
StorageEngineGetSchemaDefinition(const std::string schema_name_arg,
634
652
message::Schema &schema_proto_arg) :
635
653
schema_name(schema_name_arg),
636
654
schema_proto(schema_proto_arg)
656
transform(schema_name.begin(), schema_name.end(),
657
schema_name.begin(), ::tolower);
639
660
result_type operator() (argument_type engine)
831
864
class DropTables: public unary_function<StorageEngine *, void>
833
866
Session &session;
834
TableNameList &set_of_names;
867
TableIdentifierList &table_identifiers;
838
DropTables(Session &session_arg, set<string>& of_names) :
871
DropTables(Session &session_arg, TableIdentifierList &table_identifiers_arg) :
839
872
session(session_arg),
840
set_of_names(of_names)
873
table_identifiers(table_identifiers_arg)
843
876
result_type operator() (argument_type engine)
845
for (TableNameList::iterator iter= set_of_names.begin();
846
iter != set_of_names.end();
878
for (TableIdentifierList::iterator iter= table_identifiers.begin();
879
iter != table_identifiers.end();
849
TableIdentifier dummy((*iter).c_str());
850
int error= engine->doDropTable(session, dummy);
882
int error= engine->doDropTable(session, const_cast<TableIdentifier&>(*iter));
852
884
// On a return of zero we know we found and deleted the table. So we
853
885
// remove it from our search.
855
set_of_names.erase(iter);
887
table_identifiers.erase(iter);
891
923
path+= directory;
892
924
path+= FN_LIBCHAR;
893
925
path+= entry->filename;
894
set_of_table_names.insert(path);
926
message::Table definition;
927
if (StorageEngine::readTableFile(path, definition))
929
TableIdentifier identifier(definition.schema(), definition.name(), path);
930
table_identifiers.push_back(identifier);
897
934
for_each(vector_of_engines.begin(), vector_of_engines.end(),
898
DropTables(session, set_of_table_names));
935
DropTables(session, table_identifiers));
901
938
Now we just clean up anything that might left over.
1202
1239
int StorageEngine::renameDefinitionFromPath(TableIdentifier &dest, TableIdentifier &src)
1241
message::Table table_message;
1204
1242
string src_path(src.getPath());
1205
1243
string dest_path(dest.getPath());
1207
1245
src_path.append(DEFAULT_DEFINITION_FILE_EXT);
1208
1246
dest_path.append(DEFAULT_DEFINITION_FILE_EXT);
1210
int fd= open(src_path.c_str(), O_RDONLY);
1214
perror(src_path.c_str());
1218
google::protobuf::io::ZeroCopyInputStream* input=
1219
new google::protobuf::io::FileInputStream(fd);
1222
return HA_ERR_CRASHED_ON_USAGE;
1224
message::Table table_message;
1225
if (not table_message.ParseFromZeroCopyStream(input))
1229
if (not table_message.IsInitialized())
1231
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
1232
table_message.InitializationErrorString().c_str());
1233
return ER_CORRUPT_TABLE_DEFINITION;
1236
return HA_ERR_CRASHED_ON_USAGE;
1248
bool was_read= StorageEngine::readTableFile(src_path.c_str(), table_message);
1241
1255
dest.copyToTableMessage(table_message);
1243
// We have to update the source to have the right information before we
1244
// make it the master.
1245
int error= StorageEngine::writeDefinitionFromPath(src, table_message);
1257
int error= StorageEngine::writeDefinitionFromPath(dest, table_message);
1248
perror(src_path.c_str());
1261
if (unlink(src_path.c_str()))
1262
perror(src_path.c_str());
1252
return internal::my_rename(src_path.c_str(), dest_path.c_str(), MYF(MY_WME));
1255
1268
int StorageEngine::writeDefinitionFromPath(TableIdentifier &identifier, message::Table &table_message)
1270
char definition_file_tmp[FN_REFLEN];
1257
1271
string file_name(identifier.getPath());
1259
1273
file_name.append(DEFAULT_DEFINITION_FILE_EXT);
1261
int fd= open(file_name.c_str(), O_RDWR|O_CREAT|O_TRUNC, internal::my_umask);
1275
snprintf(definition_file_tmp, sizeof(definition_file_tmp), "%s.%sXXXXXX", file_name.c_str(), DEFAULT_DEFINITION_FILE_EXT.c_str());
1277
int fd= mkstemp(definition_file_tmp);
1265
perror(file_name.c_str());
1281
perror(definition_file_tmp);
1269
1285
google::protobuf::io::ZeroCopyOutputStream* output=
1270
1286
new google::protobuf::io::FileOutputStream(fd);
1272
if (table_message.SerializeToZeroCopyStream(output) == false)
1288
if (not table_message.SerializeToZeroCopyStream(output))
1290
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
1291
table_message.InitializationErrorString().c_str());
1276
perror(file_name.c_str());
1294
if (close(fd) == -1)
1295
perror(definition_file_tmp);
1297
if (unlink(definition_file_tmp) == -1)
1298
perror(definition_file_tmp);
1300
return ER_CORRUPT_TABLE_DEFINITION;
1305
if (close(fd) == -1)
1308
perror(definition_file_tmp);
1310
if (unlink(definition_file_tmp))
1311
perror(definition_file_tmp);
1316
if (rename(definition_file_tmp, file_name.c_str()) == -1)
1319
perror(definition_file_tmp);
1321
if (unlink(definition_file_tmp))
1322
perror(definition_file_tmp);