OmniSciDB  d2f719934e
 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 () 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 Member Functions inherited from DdlCommand
bool isDefaultServer (const std::string &server_name)
 
- Protected Attributes inherited from DdlCommand
const DdlCommandDataddl_data_
 
std::shared_ptr
< Catalog_Namespace::SessionInfo
const
session_ptr_
 

Detailed Description

Definition at line 66 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 651 of file DdlCommandExecutor.cpp.

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

654  : DdlCommand(ddl_data, session_ptr) {
655  auto& ddl_payload = extractPayload(ddl_data_);
656  CHECK(ddl_payload.HasMember("serverName"));
657  CHECK(ddl_payload["serverName"].IsString());
658  CHECK(ddl_payload.HasMember("alterType"));
659  CHECK(ddl_payload["alterType"].IsString());
660  if (ddl_payload["alterType"] == "SET_OPTIONS") {
661  CHECK(ddl_payload.HasMember("options"));
662  CHECK(ddl_payload["options"].IsObject());
663  } else if (ddl_payload["alterType"] == "SET_DATA_WRAPPER") {
664  CHECK(ddl_payload.HasMember("dataWrapper"));
665  CHECK(ddl_payload["dataWrapper"].IsString());
666  } else if (ddl_payload["alterType"] == "RENAME_SERVER") {
667  CHECK(ddl_payload.HasMember("newServerName"));
668  CHECK(ddl_payload["newServerName"].IsString());
669  } else if (ddl_payload["alterType"] == "CHANGE_OWNER") {
670  CHECK(ddl_payload.HasMember("newOwner"));
671  CHECK(ddl_payload["newOwner"].IsString());
672  } else {
673  UNREACHABLE(); // not-implemented alterType
674  }
675 }
#define UNREACHABLE()
Definition: Logger.h:255
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:211

+ Here is the call graph for this function:

Member Function Documentation

void AlterForeignServerCommand::changeForeignServerOwner ( )
private

Definition at line 705 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().

705  {
706  auto& ddl_payload = extractPayload(ddl_data_);
707  std::string server_name = ddl_payload["serverName"].GetString();
708  std::string new_owner = ddl_payload["newOwner"].GetString();
709  auto& sys_cat = Catalog_Namespace::SysCatalog::instance();
710  if (!session_ptr_->get_currentUser().isSuper) {
711  throw std::runtime_error(
712  "Only a super user can change a foreign server's owner. "
713  "Current user is not a super-user. "
714  "Foreign server with name \"" +
715  server_name + "\" will not have owner changed.");
716  }
717  Catalog_Namespace::UserMetadata user, original_owner;
718  if (!sys_cat.getMetadataForUser(new_owner, user)) {
719  throw std::runtime_error("User with username \"" + new_owner + "\" does not exist. " +
720  "Foreign server with name \"" + server_name +
721  "\" can not have owner changed.");
722  }
723  auto& cat = session_ptr_->getCatalog();
724  // get original owner metadata
725  bool original_owner_exists = sys_cat.getMetadataForUserById(
726  cat.getForeignServer(server_name)->user_id, original_owner);
727  // update catalog
728  cat.changeForeignServerOwner(server_name, user.userId);
729  try {
730  // update permissions
731  DBObject db_object(server_name, DBObjectType::ServerDBObjectType);
732  sys_cat.changeDBObjectOwnership(
733  user, original_owner, db_object, cat, original_owner_exists);
734  } catch (const std::runtime_error& e) {
735  // update permissions failed, revert catalog update
736  cat.changeForeignServerOwner(server_name, original_owner.userId);
737  throw;
738  }
739 }
std::string cat(Ts &&...args)
const DdlCommandData & ddl_data_
static SysCatalog & instance()
Definition: SysCatalog.h:326
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 ( )
overridevirtual

Executes the DDL command corresponding to provided JSON payload.

Parameters
_returnresult of DDL command execution (if applicable)

Implements DdlCommand.

Definition at line 677 of file DdlCommandExecutor.cpp.

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

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

677  {
678  auto& ddl_payload = extractPayload(ddl_data_);
679  std::string server_name = ddl_payload["serverName"].GetString();
680  if (isDefaultServer(server_name)) {
681  throw std::runtime_error{"OmniSci default servers cannot be altered."};
682  }
683  if (!session_ptr_->getCatalog().getForeignServer(server_name)) {
684  throw std::runtime_error{"Foreign server with name \"" + server_name +
685  "\" does not exist and can not be altered."};
686  }
687  if (!hasAlterServerPrivileges()) {
688  throw std::runtime_error("Server " + server_name +
689  " can not be altered. User has no ALTER SERVER privileges.");
690  }
691  std::string alter_type = ddl_payload["alterType"].GetString();
692  if (alter_type == "CHANGE_OWNER") {
694  } else if (alter_type == "SET_DATA_WRAPPER") {
696  } else if (alter_type == "SET_OPTIONS") {
698  } else if (alter_type == "RENAME_SERVER") {
700  }
701 
702  return ExecutionResult();
703 }
const DdlCommandData & ddl_data_
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
bool isDefaultServer(const std::string &server_name)
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 794 of file DdlCommandExecutor.cpp.

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

Referenced by execute().

794  {
795  // TODO: implement `GRANT/REVOKE ALTER_SERVER` DDL commands
796  auto& ddl_payload = extractPayload(ddl_data_);
797  std::string server_name = ddl_payload["serverName"].GetString();
798  return session_ptr_->checkDBAccessPrivileges(
800 }
static const AccessPrivileges ALTER_SERVER
Definition: DBObject.h:192
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 741 of file DdlCommandExecutor.cpp.

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

Referenced by execute().

741  {
742  auto& ddl_payload = extractPayload(ddl_data_);
743  std::string server_name = ddl_payload["serverName"].GetString();
744  std::string new_server_name = ddl_payload["newServerName"].GetString();
745  if (isDefaultServer(new_server_name)) {
746  throw std::runtime_error{"OmniSci prefix can not be used for new name of server."};
747  }
748  auto& cat = session_ptr_->getCatalog();
749  // check for a conflicting server
750  if (cat.getForeignServer(new_server_name)) {
751  throw std::runtime_error("Foreign server with name \"" + server_name +
752  "\" can not be renamed to \"" + new_server_name + "\"." +
753  "Foreign server with name \"" + new_server_name +
754  "\" exists.");
755  }
756  // update catalog
757  cat.renameForeignServer(server_name, new_server_name);
758  try {
759  // migrate object privileges
760  auto& sys_cat = Catalog_Namespace::SysCatalog::instance();
761  sys_cat.renameDBObject(server_name,
762  new_server_name,
764  cat.getForeignServer(new_server_name)->id,
765  cat);
766  } catch (const std::runtime_error& e) {
767  // permission migration failed, revert catalog update
768  cat.renameForeignServer(new_server_name, server_name);
769  throw;
770  }
771 }
std::string cat(Ts &&...args)
const DdlCommandData & ddl_data_
static SysCatalog & instance()
Definition: SysCatalog.h:326
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
bool isDefaultServer(const std::string &server_name)
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 785 of file DdlCommandExecutor.cpp.

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

Referenced by execute().

785  {
786  auto& ddl_payload = extractPayload(ddl_data_);
787  std::string server_name = ddl_payload["serverName"].GetString();
788  std::string data_wrapper = ddl_payload["dataWrapper"].GetString();
789  auto& cat = session_ptr_->getCatalog();
790  // update catalog
791  cat.setForeignServerDataWrapper(server_name, data_wrapper);
792 }
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 773 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().

773  {
774  auto& ddl_payload = extractPayload(ddl_data_);
775  std::string server_name = ddl_payload["serverName"].GetString();
776  auto& cat = session_ptr_->getCatalog();
777  // update catalog
778  const auto foreign_server = cat.getForeignServer(server_name);
780  opt.populateOptionsMap(foreign_server->getOptionsAsJsonString());
781  opt.populateOptionsMap(ddl_payload["options"]);
782  cat.setForeignServerOptions(server_name, opt.getOptionsAsJsonString());
783 }
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: