OmniSciDB  c0231cc57d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AlterForeignServerCommand Class Reference

#include <DdlCommandExecutor.h>

+ Inheritance diagram for AlterForeignServerCommand:
+ Collaboration diagram for AlterForeignServerCommand:

Public Member Functions

 AlterForeignServerCommand (const DdlCommandData &ddl_data, std::shared_ptr< Catalog_Namespace::SessionInfo const > session_ptr)
 
ExecutionResult execute (bool read_only_mode) override
 
- Public Member Functions inherited from DdlCommand
 DdlCommand (const DdlCommandData &ddl_data, std::shared_ptr< Catalog_Namespace::SessionInfo const > session_ptr)
 

Private Member Functions

void changeForeignServerOwner ()
 
void renameForeignServer ()
 
void setForeignServerOptions ()
 
void setForeignServerDataWrapper ()
 
bool hasAlterServerPrivileges ()
 

Additional Inherited Members

- Protected Attributes inherited from DdlCommand
const DdlCommandDataddl_data_
 
std::shared_ptr
< Catalog_Namespace::SessionInfo
const > 
session_ptr_
 

Detailed Description

Definition at line 65 of file DdlCommandExecutor.h.

Constructor & Destructor Documentation

AlterForeignServerCommand::AlterForeignServerCommand ( const DdlCommandData ddl_data,
std::shared_ptr< Catalog_Namespace::SessionInfo const >  session_ptr 
)

Definition at line 801 of file DdlCommandExecutor.cpp.

References CHECK, DdlCommand::ddl_data_, anonymous_namespace{DdlCommandExecutor.cpp}::extractPayload(), g_enable_fsi, and UNREACHABLE.

804  : DdlCommand(ddl_data, session_ptr) {
805  if (!g_enable_fsi) {
806  throw std::runtime_error("Unsupported command: ALTER FOREIGN SERVER");
807  }
808  auto& ddl_payload = extractPayload(ddl_data_);
809  CHECK(ddl_payload.HasMember("serverName"));
810  CHECK(ddl_payload["serverName"].IsString());
811  CHECK(ddl_payload.HasMember("alterType"));
812  CHECK(ddl_payload["alterType"].IsString());
813  if (ddl_payload["alterType"] == "SET_OPTIONS") {
814  CHECK(ddl_payload.HasMember("options"));
815  CHECK(ddl_payload["options"].IsObject());
816  } else if (ddl_payload["alterType"] == "SET_DATA_WRAPPER") {
817  CHECK(ddl_payload.HasMember("dataWrapper"));
818  CHECK(ddl_payload["dataWrapper"].IsString());
819  } else if (ddl_payload["alterType"] == "RENAME_SERVER") {
820  CHECK(ddl_payload.HasMember("newServerName"));
821  CHECK(ddl_payload["newServerName"].IsString());
822  } else if (ddl_payload["alterType"] == "CHANGE_OWNER") {
823  CHECK(ddl_payload.HasMember("newOwner"));
824  CHECK(ddl_payload["newOwner"].IsString());
825  } else {
826  UNREACHABLE(); // not-implemented alterType
827  }
828 }
#define UNREACHABLE()
Definition: Logger.h:266
const DdlCommandData & ddl_data_
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
DdlCommand(const DdlCommandData &ddl_data, std::shared_ptr< Catalog_Namespace::SessionInfo const > session_ptr)
#define CHECK(condition)
Definition: Logger.h:222
bool g_enable_fsi
Definition: Catalog.cpp:96

+ Here is the call graph for this function:

Member Function Documentation

void AlterForeignServerCommand::changeForeignServerOwner ( )
private

Definition at line 861 of file DdlCommandExecutor.cpp.

References cat(), DdlCommand::ddl_data_, anonymous_namespace{DdlCommandExecutor.cpp}::extractPayload(), Catalog_Namespace::SysCatalog::instance(), ServerDBObjectType, DdlCommand::session_ptr_, and Catalog_Namespace::UserMetadata::userId.

Referenced by execute().

861  {
862  auto& ddl_payload = extractPayload(ddl_data_);
863  std::string server_name = ddl_payload["serverName"].GetString();
864  std::string new_owner = ddl_payload["newOwner"].GetString();
865  auto& sys_cat = Catalog_Namespace::SysCatalog::instance();
866  if (!session_ptr_->get_currentUser().isSuper) {
867  throw std::runtime_error(
868  "Only a super user can change a foreign server's owner. "
869  "Current user is not a super-user. "
870  "Foreign server with name \"" +
871  server_name + "\" will not have owner changed.");
872  }
873  Catalog_Namespace::UserMetadata user, original_owner;
874  if (!sys_cat.getMetadataForUser(new_owner, user)) {
875  throw std::runtime_error("User with username \"" + new_owner + "\" does not exist. " +
876  "Foreign server with name \"" + server_name +
877  "\" can not have owner changed.");
878  }
879  auto& cat = session_ptr_->getCatalog();
880  // get original owner metadata
881  bool original_owner_exists = sys_cat.getMetadataForUserById(
882  cat.getForeignServer(server_name)->user_id, original_owner);
883  // update catalog
884  cat.changeForeignServerOwner(server_name, user.userId);
885  try {
886  // update permissions
887  DBObject db_object(server_name, DBObjectType::ServerDBObjectType);
888  sys_cat.changeDBObjectOwnership(
889  user, original_owner, db_object, cat, original_owner_exists);
890  } catch (const std::runtime_error& e) {
891  // update permissions failed, revert catalog update
892  cat.changeForeignServerOwner(server_name, original_owner.userId);
893  throw;
894  }
895 }
std::string cat(Ts &&...args)
const DdlCommandData & ddl_data_
static SysCatalog & instance()
Definition: SysCatalog.h:341
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
std::shared_ptr< Catalog_Namespace::SessionInfo const > session_ptr_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ExecutionResult AlterForeignServerCommand::execute ( bool  read_only_mode)
overridevirtual

Executes the DDL command corresponding to provided JSON payload.

Parameters
_returnresult of DDL command execution (if applicable)

Implements DdlCommand.

Definition at line 830 of file DdlCommandExecutor.cpp.

References changeForeignServerOwner(), DdlCommand::ddl_data_, anonymous_namespace{DdlCommandExecutor.cpp}::extractPayload(), hasAlterServerPrivileges(), anonymous_namespace{DdlCommandExecutor.cpp}::is_default_server(), renameForeignServer(), DdlCommand::session_ptr_, setForeignServerDataWrapper(), and setForeignServerOptions().

Referenced by heavydb.cursor.Cursor::executemany().

830  {
831  if (read_only_mode) {
832  throw std::runtime_error("ALTER FOREIGN SERVER invalid in read only mode.");
833  }
834  auto& ddl_payload = extractPayload(ddl_data_);
835  std::string server_name = ddl_payload["serverName"].GetString();
836  if (is_default_server(server_name)) {
837  throw std::runtime_error{"Default servers cannot be altered."};
838  }
839  if (!session_ptr_->getCatalog().getForeignServer(server_name)) {
840  throw std::runtime_error{"Foreign server with name \"" + server_name +
841  "\" does not exist and can not be altered."};
842  }
843  if (!hasAlterServerPrivileges()) {
844  throw std::runtime_error("Server " + server_name +
845  " can not be altered. User has no ALTER SERVER privileges.");
846  }
847  std::string alter_type = ddl_payload["alterType"].GetString();
848  if (alter_type == "CHANGE_OWNER") {
850  } else if (alter_type == "SET_DATA_WRAPPER") {
852  } else if (alter_type == "SET_OPTIONS") {
854  } else if (alter_type == "RENAME_SERVER") {
856  }
857 
858  return ExecutionResult();
859 }
const DdlCommandData & ddl_data_
bool is_default_server(const std::string &server_name)
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
std::shared_ptr< Catalog_Namespace::SessionInfo const > session_ptr_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool AlterForeignServerCommand::hasAlterServerPrivileges ( )
private

Definition at line 950 of file DdlCommandExecutor.cpp.

References AccessPrivileges::ALTER_SERVER, DdlCommand::ddl_data_, anonymous_namespace{DdlCommandExecutor.cpp}::extractPayload(), ServerDBObjectType, and DdlCommand::session_ptr_.

Referenced by execute().

950  {
951  // TODO: implement `GRANT/REVOKE ALTER_SERVER` DDL commands
952  auto& ddl_payload = extractPayload(ddl_data_);
953  std::string server_name = ddl_payload["serverName"].GetString();
954  return session_ptr_->checkDBAccessPrivileges(
956 }
static const AccessPrivileges ALTER_SERVER
Definition: DBObject.h:190
const DdlCommandData & ddl_data_
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
std::shared_ptr< Catalog_Namespace::SessionInfo const > session_ptr_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void AlterForeignServerCommand::renameForeignServer ( )
private

Definition at line 897 of file DdlCommandExecutor.cpp.

References cat(), DdlCommand::ddl_data_, anonymous_namespace{DdlCommandExecutor.cpp}::extractPayload(), Catalog_Namespace::SysCatalog::instance(), anonymous_namespace{DdlCommandExecutor.cpp}::is_default_server(), ServerDBObjectType, DdlCommand::session_ptr_, and anonymous_namespace{DdlCommandExecutor.cpp}::throw_reserved_server_prefix_exception().

Referenced by execute().

897  {
898  auto& ddl_payload = extractPayload(ddl_data_);
899  std::string server_name = ddl_payload["serverName"].GetString();
900  std::string new_server_name = ddl_payload["newServerName"].GetString();
901  if (is_default_server(new_server_name)) {
903  }
904  auto& cat = session_ptr_->getCatalog();
905  // check for a conflicting server
906  if (cat.getForeignServer(new_server_name)) {
907  throw std::runtime_error("Foreign server with name \"" + server_name +
908  "\" can not be renamed to \"" + new_server_name + "\"." +
909  "Foreign server with name \"" + new_server_name +
910  "\" exists.");
911  }
912  // update catalog
913  cat.renameForeignServer(server_name, new_server_name);
914  try {
915  // migrate object privileges
916  auto& sys_cat = Catalog_Namespace::SysCatalog::instance();
917  sys_cat.renameDBObject(server_name,
918  new_server_name,
920  cat.getForeignServer(new_server_name)->id,
921  cat);
922  } catch (const std::runtime_error& e) {
923  // permission migration failed, revert catalog update
924  cat.renameForeignServer(new_server_name, server_name);
925  throw;
926  }
927 }
std::string cat(Ts &&...args)
const DdlCommandData & ddl_data_
bool is_default_server(const std::string &server_name)
static SysCatalog & instance()
Definition: SysCatalog.h:341
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
std::shared_ptr< Catalog_Namespace::SessionInfo const > session_ptr_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void AlterForeignServerCommand::setForeignServerDataWrapper ( )
private

Definition at line 941 of file DdlCommandExecutor.cpp.

References cat(), DdlCommand::ddl_data_, anonymous_namespace{DdlCommandExecutor.cpp}::extractPayload(), and DdlCommand::session_ptr_.

Referenced by execute().

941  {
942  auto& ddl_payload = extractPayload(ddl_data_);
943  std::string server_name = ddl_payload["serverName"].GetString();
944  std::string data_wrapper = ddl_payload["dataWrapper"].GetString();
945  auto& cat = session_ptr_->getCatalog();
946  // update catalog
947  cat.setForeignServerDataWrapper(server_name, data_wrapper);
948 }
std::string cat(Ts &&...args)
const DdlCommandData & ddl_data_
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
std::shared_ptr< Catalog_Namespace::SessionInfo const > session_ptr_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void AlterForeignServerCommand::setForeignServerOptions ( )
private

Definition at line 929 of file DdlCommandExecutor.cpp.

References cat(), DdlCommand::ddl_data_, anonymous_namespace{DdlCommandExecutor.cpp}::extractPayload(), foreign_storage::OptionsContainer::getOptionsAsJsonString(), foreign_storage::OptionsContainer::populateOptionsMap(), and DdlCommand::session_ptr_.

Referenced by execute().

929  {
930  auto& ddl_payload = extractPayload(ddl_data_);
931  std::string server_name = ddl_payload["serverName"].GetString();
932  auto& cat = session_ptr_->getCatalog();
933  // update catalog
934  const auto foreign_server = cat.getForeignServer(server_name);
936  opt.populateOptionsMap(foreign_server->getOptionsAsJsonString());
937  opt.populateOptionsMap(ddl_payload["options"]);
938  cat.setForeignServerOptions(server_name, opt.getOptionsAsJsonString());
939 }
std::string cat(Ts &&...args)
std::string getOptionsAsJsonString() const
const DdlCommandData & ddl_data_
void populateOptionsMap(OptionsMap &&options_map, bool clear=false)
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
std::shared_ptr< Catalog_Namespace::SessionInfo const > session_ptr_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:


The documentation for this class was generated from the following files: