428
public unary_function<StorageEngine *, void>
430
uint64_t &success_count;
431
TableIdentifier &identifier;
436
DropTable(Session &session_arg, TableIdentifier &arg, uint64_t &count_arg) :
437
success_count(count_arg),
443
result_type operator() (argument_type engine)
445
// @todo someday check that at least one engine said "true"
446
std::string path(identifier.getPath());
447
bool success= engine->doDropTable(session, identifier, path);
439
456
returns ENOENT if the file doesn't exists.
441
458
int StorageEngine::dropTable(Session& session,
442
const TableIdentifier &identifier)
459
TableIdentifier &identifier)
446
message::table::shared_ptr src_proto;
447
StorageEngine *engine;
463
message::Table src_proto;
464
StorageEngine* engine;
449
466
error_proto= StorageEngine::getTableDefinition(session, identifier, src_proto);
451
468
if (error_proto == ER_CORRUPT_TABLE_DEFINITION)
453
std::string error_message;
454
identifier.getSQLPath(error_message);
456
error_message.append(" : ");
457
error_message.append(src_proto->InitializationErrorString());
459
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), error_message.c_str());
470
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
471
src_proto.InitializationErrorString().c_str());
461
472
return ER_CORRUPT_TABLE_DEFINITION;
465
engine= StorageEngine::findByName(session, src_proto->engine().name());
467
engine= StorageEngine::findByName(session, "");
475
engine= StorageEngine::findByName(session, src_proto.engine().name());
471
std::string error_message;
472
identifier.getSQLPath(error_message);
473
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), error_message.c_str());
475
return ER_CORRUPT_TABLE_DEFINITION;
479
std::string path(identifier.getPath());
480
engine->setTransactionReadWrite(session);
481
error= engine->doDropTable(session, identifier, path);
485
if (not engine->check_flag(HTON_BIT_HAS_DATA_DICTIONARY))
487
uint64_t counter; // @todo We need to refactor to check that.
489
for_each(vector_of_schema_engines.begin(), vector_of_schema_engines.end(),
490
DropTable(session, identifier, counter));
478
error= StorageEngine::dropTable(session, *engine, identifier);
480
495
if (error_proto && error == 0)
486
int StorageEngine::dropTable(Session& session,
487
StorageEngine &engine,
488
const TableIdentifier &identifier)
492
engine.setTransactionReadWrite(session);
494
if (unlikely(plugin::EventObserver::beforeDropTable(session, identifier)))
496
error= ER_EVENT_OBSERVER_PLUGIN;
500
error= engine.doDropTable(session, identifier);
501
if (unlikely(plugin::EventObserver::afterDropTable(session, identifier, error)))
503
error= ER_EVENT_OBSERVER_PLUGIN;
507
drizzled::message::Cache::singleton().erase(identifier);
514
502
Initiates table-file and calls appropriate database-creator.
509
@todo refactor to remove goto
521
int StorageEngine::createTable(Session &session,
522
const TableIdentifier &identifier,
511
int StorageEngine::createTable(Session& session,
512
TableIdentifier &identifier,
513
bool update_create_info,
523
514
message::Table& table_message)
526
TableShare share(identifier);
527
table::Shell table(share);
518
TableShare share(identifier.getDBName().c_str(), 0, identifier.getTableName().c_str(), identifier.getPath().c_str());
528
519
message::Table tmp_proto;
530
if (share.parse_table_proto(session, table_message) || share.open_table_from_share(&session, identifier, "", 0, 0, table))
532
// @note Error occured, we should probably do a little more here.
536
/* Check for legal operations against the Engine using the proto (if used) */
537
if (table_message.type() == message::Table::TEMPORARY &&
538
share.storage_engine->check_flag(HTON_BIT_TEMPORARY_NOT_SUPPORTED) == true)
540
error= HA_ERR_UNSUPPORTED;
542
else if (table_message.type() != message::Table::TEMPORARY &&
543
share.storage_engine->check_flag(HTON_BIT_TEMPORARY_ONLY) == true)
545
error= HA_ERR_UNSUPPORTED;
549
share.storage_engine->setTransactionReadWrite(session);
551
error= share.storage_engine->doCreateTable(session,
560
identifier.getSQLPath(path);
561
my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), path.c_str(), error);
564
table.delete_table();
521
if (parse_table_proto(session, table_message, &share))
524
if (open_table_from_share(&session, &share, "", 0, 0,
528
if (update_create_info)
529
table.updateCreateInfo(&table_message);
531
/* Check for legal operations against the Engine using the proto (if used) */
532
if (table_message.type() == message::Table::TEMPORARY &&
533
share.storage_engine->check_flag(HTON_BIT_TEMPORARY_NOT_SUPPORTED) == true)
535
error= HA_ERR_UNSUPPORTED;
538
else if (table_message.type() != message::Table::TEMPORARY &&
539
share.storage_engine->check_flag(HTON_BIT_TEMPORARY_ONLY) == true)
541
error= HA_ERR_UNSUPPORTED;
546
if (not share.storage_engine->check_flag(HTON_BIT_HAS_DATA_DICTIONARY))
548
int protoerr= StorageEngine::writeDefinitionFromPath(identifier, table_message);
557
share.storage_engine->setTransactionReadWrite(session);
559
error= share.storage_engine->doCreateTable(&session,
568
if (not share.storage_engine->check_flag(HTON_BIT_HAS_DATA_DICTIONARY))
569
plugin::StorageEngine::deleteDefinitionFromPath(identifier);
571
my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), identifier.getSQLPath().c_str(), error);
574
table.closefrm(false);
577
share.free_table_share();
567
578
return(error != 0);
570
Cursor *StorageEngine::getCursor(Table &arg)
581
Cursor *StorageEngine::getCursor(TableShare &share, memory::Root *alloc)
583
return create(share, alloc);
575
class AddTableIdentifier :
576
public std::unary_function<StorageEngine *, void>
587
TODO -> Remove this to force all engines to implement their own file. Solves the "we only looked at dfe" problem.
589
void StorageEngine::doGetTableNames(CachedDirectory&, string&, set<string>&)
593
public unary_function<StorageEngine *, void>
578
CachedDirectory &directory;
579
const SchemaIdentifier &identifier;
580
TableIdentifier::vector &set_of_identifiers;
596
CachedDirectory& directory;
597
TableNameList &set_of_names;
584
AddTableIdentifier(CachedDirectory &directory_arg, const SchemaIdentifier &identifier_arg, TableIdentifier::vector &of_names) :
601
AddTableName(CachedDirectory& directory_arg, const string& database_name, set<string>& of_names) :
585
602
directory(directory_arg),
586
identifier(identifier_arg),
587
set_of_identifiers(of_names)
591
result_type operator() (argument_type engine)
593
engine->doGetTableIdentifiers(directory, identifier, set_of_identifiers);
598
void StorageEngine::getIdentifiers(Session &session, const SchemaIdentifier &schema_identifier, TableIdentifier::vector &set_of_identifiers)
600
static SchemaIdentifier INFORMATION_SCHEMA_IDENTIFIER("information_schema");
601
static SchemaIdentifier DATA_DICTIONARY_IDENTIFIER("data_dictionary");
603
CachedDirectory directory(schema_identifier.getPath(), set_of_table_definition_ext);
605
if (schema_identifier == INFORMATION_SCHEMA_IDENTIFIER)
607
else if (schema_identifier == DATA_DICTIONARY_IDENTIFIER)
603
set_of_names(of_names)
608
result_type operator() (argument_type engine)
610
engine->doGetTableNames(directory, db, set_of_names);
614
class AddSchemaNames :
615
public unary_function<StorageEngine *, void>
617
SchemaNameList &set_of_names;
621
AddSchemaNames(set<string>& of_names) :
622
set_of_names(of_names)
626
result_type operator() (argument_type engine)
628
engine->doGetSchemaNames(set_of_names);
632
void StorageEngine::getSchemaNames(SchemaNameList &set_of_names)
634
// Add hook here for engines to register schema.
635
for_each(vector_of_schema_engines.begin(), vector_of_schema_engines.end(),
636
AddSchemaNames(set_of_names));
638
plugin::Authorization::pruneSchemaNames(current_session->getSecurityContext(),
642
class StorageEngineGetSchemaDefinition: public unary_function<StorageEngine *, bool>
644
const std::string &schema_name;
645
message::Schema &schema_proto;
648
StorageEngineGetSchemaDefinition(const std::string &schema_name_arg,
649
message::Schema &schema_proto_arg) :
650
schema_name(schema_name_arg),
651
schema_proto(schema_proto_arg)
654
result_type operator() (argument_type engine)
656
return engine->doGetSchemaDefinition(schema_name, schema_proto);
661
Return value is "if parsed"
663
bool StorageEngine::getSchemaDefinition(const std::string &schema_name, message::Schema &proto)
667
EngineVector::iterator iter=
668
find_if(vector_of_schema_engines.begin(), vector_of_schema_engines.end(),
669
StorageEngineGetSchemaDefinition(schema_name, proto));
671
if (iter != vector_of_schema_engines.end())
679
bool StorageEngine::doesSchemaExist(const std::string &schema_name)
681
message::Schema proto;
683
return StorageEngine::getSchemaDefinition(schema_name, proto);
687
const CHARSET_INFO *StorageEngine::getSchemaCollation(const std::string &schema_name)
689
message::Schema schmema_proto;
692
found= StorageEngine::getSchemaDefinition(schema_name, schmema_proto);
694
if (found && schmema_proto.has_collation())
696
const string buffer= schmema_proto.collation();
697
const CHARSET_INFO* cs= get_charset_by_name(buffer.c_str());
701
errmsg_printf(ERRMSG_LVL_ERROR,
702
_("Error while loading database options: '%s':"), schema_name.c_str());
703
errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_UNKNOWN_COLLATION), buffer.c_str());
705
return default_charset_info;
711
return default_charset_info;
715
public unary_function<StorageEngine *, void>
717
const drizzled::message::Schema &schema_message;
721
CreateSchema(const drizzled::message::Schema &arg) :
726
result_type operator() (argument_type engine)
728
// @todo eomeday check that at least one engine said "true"
729
(void)engine->doCreateSchema(schema_message);
733
bool StorageEngine::createSchema(const drizzled::message::Schema &schema_message)
735
// Add hook here for engines to register schema.
736
for_each(vector_of_schema_engines.begin(), vector_of_schema_engines.end(),
737
CreateSchema(schema_message));
743
public unary_function<StorageEngine *, void>
745
uint64_t &success_count;
746
const string &schema_name;
750
DropSchema(const string &arg, uint64_t &count_arg) :
751
success_count(count_arg),
756
result_type operator() (argument_type engine)
758
// @todo someday check that at least one engine said "true"
759
bool success= engine->doDropSchema(schema_name);
766
bool StorageEngine::dropSchema(const string &schema_name)
769
// Add hook here for engines to register schema.
770
for_each(vector_of_schema_engines.begin(), vector_of_schema_engines.end(),
771
DropSchema(schema_name, counter));
773
return counter ? true : false;
777
public unary_function<StorageEngine *, void>
779
uint64_t &success_count;
780
const drizzled::message::Schema &schema_message;
784
AlterSchema(const drizzled::message::Schema &arg, uint64_t &count_arg) :
785
success_count(count_arg),
790
result_type operator() (argument_type engine)
792
// @todo eomeday check that at least one engine said "true"
793
bool success= engine->doAlterSchema(schema_message);
800
bool StorageEngine::alterSchema(const drizzled::message::Schema &schema_message)
802
uint64_t success_count= 0;
804
for_each(vector_of_schema_engines.begin(), vector_of_schema_engines.end(),
805
AlterSchema(schema_message, success_count));
807
return success_count ? true : false;
811
void StorageEngine::getTableNames(const string &schema_name, TableNameList &set_of_names)
815
build_table_filename(tmp_path, schema_name.c_str(), "", false);
817
CachedDirectory directory(tmp_path, set_of_table_definition_ext);
819
if (not schema_name.compare("information_schema"))
821
else if (not schema_name.compare("data_dictionary"))
613
827
errno= directory.getError();
614
828
if (errno == ENOENT)
617
schema_identifier.getSQLPath(path);
618
my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), path.c_str());
829
my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), schema_name.c_str());
622
831
my_error(ER_CANT_READ_DIR, MYF(ME_BELL+ME_WAITTANG), directory.getPath(), errno);
629
std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
630
AddTableIdentifier(directory, schema_identifier, set_of_identifiers));
632
session.doGetTableIdentifiers(directory, schema_identifier, set_of_identifiers);
836
for_each(vector_of_engines.begin(), vector_of_engines.end(),
837
AddTableName(directory, schema_name, set_of_names));
839
Session *session= current_session;
841
session->doGetTableNames(directory, schema_name, set_of_names);
635
class DropTable: public std::unary_function<TableIdentifier&, bool>
638
StorageEngine *engine;
642
DropTable(Session &session_arg, StorageEngine *engine_arg) :
643
session(session_arg),
647
result_type operator() (argument_type identifier)
649
return engine->doDropTable(session, identifier) == 0;
653
845
/* This will later be converted to TableIdentifiers */
654
class DropTables: public std::unary_function<StorageEngine *, void>
846
class DropTables: public unary_function<StorageEngine *, void>
656
848
Session &session;
657
TableIdentifier::vector &table_identifiers;
849
TableNameList &set_of_names;
661
DropTables(Session &session_arg, TableIdentifier::vector &table_identifiers_arg) :
853
DropTables(Session &session_arg, set<string>& of_names) :
662
854
session(session_arg),
663
table_identifiers(table_identifiers_arg)
855
set_of_names(of_names)
666
858
result_type operator() (argument_type engine)
668
// True returning from DropTable means the table has been successfully
669
// deleted, so it should be removed from the list of tables to drop
670
table_identifiers.erase(std::remove_if(table_identifiers.begin(),
671
table_identifiers.end(),
672
DropTable(session, engine)),
673
table_identifiers.end());
860
for (TableNameList::iterator iter= set_of_names.begin();
861
iter != set_of_names.end();
864
TableIdentifier dummy((*iter).c_str());
865
int error= engine->doDropTable(session, dummy, *iter);
867
// On a return of zero we know we found and deleted the table. So we
868
// remove it from our search.
870
set_of_names.erase(iter);
1008
int StorageEngine::deleteDefinitionFromPath(const TableIdentifier &identifier)
1208
int StorageEngine::deleteDefinitionFromPath(TableIdentifier &identifier)
1010
std::string path(identifier.getPath());
1210
string path(identifier.getPath());
1012
1212
path.append(DEFAULT_DEFINITION_FILE_EXT);
1014
1214
return internal::my_delete(path.c_str(), MYF(0));
1017
int StorageEngine::renameDefinitionFromPath(const TableIdentifier &dest, const TableIdentifier &src)
1217
int StorageEngine::renameDefinitionFromPath(TableIdentifier &dest, TableIdentifier &src)
1019
message::Table table_message;
1020
std::string src_path(src.getPath());
1021
std::string dest_path(dest.getPath());
1219
string src_path(src.getPath());
1220
string dest_path(dest.getPath());
1023
1222
src_path.append(DEFAULT_DEFINITION_FILE_EXT);
1024
1223
dest_path.append(DEFAULT_DEFINITION_FILE_EXT);
1026
bool was_read= StorageEngine::readTableFile(src_path.c_str(), table_message);
1033
dest.copyToTableMessage(table_message);
1035
int error= StorageEngine::writeDefinitionFromPath(dest, table_message);
1039
if (unlink(src_path.c_str()))
1040
perror(src_path.c_str());
1225
return internal::my_rename(src_path.c_str(), dest_path.c_str(), MYF(MY_WME));
1046
int StorageEngine::writeDefinitionFromPath(const TableIdentifier &identifier, message::Table &table_message)
1228
int StorageEngine::writeDefinitionFromPath(TableIdentifier &identifier, message::Table &table_message)
1048
char definition_file_tmp[FN_REFLEN];
1049
std::string file_name(identifier.getPath());
1230
string file_name(identifier.getPath());
1051
1232
file_name.append(DEFAULT_DEFINITION_FILE_EXT);
1053
snprintf(definition_file_tmp, sizeof(definition_file_tmp), "%sXXXXXX", file_name.c_str());
1055
int fd= mkstemp(definition_file_tmp);
1234
int fd= open(file_name.c_str(), O_RDWR|O_CREAT|O_TRUNC, internal::my_umask);
1059
perror(definition_file_tmp);
1063
1239
google::protobuf::io::ZeroCopyOutputStream* output=
1064
1240
new google::protobuf::io::FileOutputStream(fd);
1070
success= table_message.SerializeToZeroCopyStream(output);
1079
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
1080
table_message.InitializationErrorString().c_str());
1242
if (table_message.SerializeToZeroCopyStream(output) == false)
1083
if (close(fd) == -1)
1084
perror(definition_file_tmp);
1086
if (unlink(definition_file_tmp) == -1)
1087
perror(definition_file_tmp);
1089
return ER_CORRUPT_TABLE_DEFINITION;
1094
if (close(fd) == -1)
1097
perror(definition_file_tmp);
1099
if (unlink(definition_file_tmp))
1100
perror(definition_file_tmp);
1105
if (rename(definition_file_tmp, file_name.c_str()) == -1)
1108
perror(definition_file_tmp);
1110
if (unlink(definition_file_tmp))
1111
perror(definition_file_tmp);
1119
class CanCreateTable: public std::unary_function<StorageEngine *, bool>
1254
class CanCreateTable: public unary_function<StorageEngine *, bool>
1121
1256
const TableIdentifier &identifier;