OmniSciDB  ba1bac9284
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 523 of file DdlCommandExecutor.cpp.

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

526  : DdlCommand(ddl_data, session_ptr) {
527  auto& ddl_payload = extractPayload(ddl_data_);
528  CHECK(ddl_payload.HasMember("serverName"));
529  CHECK(ddl_payload["serverName"].IsString());
530  CHECK(ddl_payload.HasMember("alterType"));
531  CHECK(ddl_payload["alterType"].IsString());
532  if (ddl_payload["alterType"] == "SET_OPTIONS") {
533  CHECK(ddl_payload.HasMember("options"));
534  CHECK(ddl_payload["options"].IsObject());
535  } else if (ddl_payload["alterType"] == "SET_DATA_WRAPPER") {
536  CHECK(ddl_payload.HasMember("dataWrapper"));
537  CHECK(ddl_payload["dataWrapper"].IsString());
538  } else if (ddl_payload["alterType"] == "RENAME_SERVER") {
539  CHECK(ddl_payload.HasMember("newServerName"));
540  CHECK(ddl_payload["newServerName"].IsString());
541  } else if (ddl_payload["alterType"] == "CHANGE_OWNER") {
542  CHECK(ddl_payload.HasMember("newOwner"));
543  CHECK(ddl_payload["newOwner"].IsString());
544  } else {
545  UNREACHABLE(); // not-implemented alterType
546  }
547 }
#define UNREACHABLE()
Definition: Logger.h:250
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:206

+ Here is the call graph for this function:

Member Function Documentation

void AlterForeignServerCommand::changeForeignServerOwner ( )
private

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

577  {
578  auto& ddl_payload = extractPayload(ddl_data_);
579  std::string server_name = ddl_payload["serverName"].GetString();
580  std::string new_owner = ddl_payload["newOwner"].GetString();
581  auto& sys_cat = Catalog_Namespace::SysCatalog::instance();
582  if (!session_ptr_->get_currentUser().isSuper) {
583  throw std::runtime_error(
584  "Only a super user can change a foreign server's owner. "
585  "Current user is not a super-user. "
586  "Foreign server with name \"" +
587  server_name + "\" will not have owner changed.");
588  }
589  Catalog_Namespace::UserMetadata user, original_owner;
590  if (!sys_cat.getMetadataForUser(new_owner, user)) {
591  throw std::runtime_error("User with username \"" + new_owner + "\" does not exist. " +
592  "Foreign server with name \"" + server_name +
593  "\" can not have owner changed.");
594  }
595  auto& cat = session_ptr_->getCatalog();
596  // get original owner metadata
597  bool original_owner_exists = sys_cat.getMetadataForUserById(
598  cat.getForeignServer(server_name)->user_id, original_owner);
599  // update catalog
600  cat.changeForeignServerOwner(server_name, user.userId);
601  try {
602  // update permissions
603  DBObject db_object(server_name, DBObjectType::ServerDBObjectType);
604  sys_cat.changeDBObjectOwnership(
605  user, original_owner, db_object, cat, original_owner_exists);
606  } catch (const std::runtime_error& e) {
607  // update permissions failed, revert catalog update
608  cat.changeForeignServerOwner(server_name, original_owner.userId);
609  throw;
610  }
611 }
std::string cat(Ts &&...args)
const DdlCommandData & ddl_data_
static SysCatalog & instance()
Definition: SysCatalog.h:292
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 549 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().

549  {
550  auto& ddl_payload = extractPayload(ddl_data_);
551  std::string server_name = ddl_payload["serverName"].GetString();
552  if (isDefaultServer(server_name)) {
553  throw std::runtime_error{"OmniSci default servers cannot be altered."};
554  }
555  if (!session_ptr_->getCatalog().getForeignServer(server_name)) {
556  throw std::runtime_error{"Foreign server with name \"" + server_name +
557  "\" does not exist and can not be altered."};
558  }
559  if (!hasAlterServerPrivileges()) {
560  throw std::runtime_error("Server " + server_name +
561  " can not be altered. User has no ALTER SERVER privileges.");
562  }
563  std::string alter_type = ddl_payload["alterType"].GetString();
564  if (alter_type == "CHANGE_OWNER") {
566  } else if (alter_type == "SET_DATA_WRAPPER") {
568  } else if (alter_type == "SET_OPTIONS") {
570  } else if (alter_type == "RENAME_SERVER") {
572  }
573 
574  return ExecutionResult();
575 }
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 666 of file DdlCommandExecutor.cpp.

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

Referenced by execute().

666  {
667  // TODO: implement `GRANT/REVOKE ALTER_SERVER` DDL commands
668  auto& ddl_payload = extractPayload(ddl_data_);
669  std::string server_name = ddl_payload["serverName"].GetString();
670  return session_ptr_->checkDBAccessPrivileges(
672 }
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 613 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().

613  {
614  auto& ddl_payload = extractPayload(ddl_data_);
615  std::string server_name = ddl_payload["serverName"].GetString();
616  std::string new_server_name = ddl_payload["newServerName"].GetString();
617  if (isDefaultServer(new_server_name)) {
618  throw std::runtime_error{"OmniSci prefix can not be used for new name of server."};
619  }
620  auto& cat = session_ptr_->getCatalog();
621  // check for a conflicting server
622  if (cat.getForeignServer(new_server_name)) {
623  throw std::runtime_error("Foreign server with name \"" + server_name +
624  "\" can not be renamed to \"" + new_server_name + "\"." +
625  "Foreign server with name \"" + new_server_name +
626  "\" exists.");
627  }
628  // update catalog
629  cat.renameForeignServer(server_name, new_server_name);
630  try {
631  // migrate object privileges
632  auto& sys_cat = Catalog_Namespace::SysCatalog::instance();
633  sys_cat.renameDBObject(server_name,
634  new_server_name,
636  cat.getForeignServer(new_server_name)->id,
637  cat);
638  } catch (const std::runtime_error& e) {
639  // permission migration failed, revert catalog update
640  cat.renameForeignServer(new_server_name, server_name);
641  throw;
642  }
643 }
std::string cat(Ts &&...args)
const DdlCommandData & ddl_data_
static SysCatalog & instance()
Definition: SysCatalog.h:292
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 657 of file DdlCommandExecutor.cpp.

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

Referenced by execute().

657  {
658  auto& ddl_payload = extractPayload(ddl_data_);
659  std::string server_name = ddl_payload["serverName"].GetString();
660  std::string data_wrapper = ddl_payload["dataWrapper"].GetString();
661  auto& cat = session_ptr_->getCatalog();
662  // update catalog
663  cat.setForeignServerDataWrapper(server_name, data_wrapper);
664 }
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 645 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().

645  {
646  auto& ddl_payload = extractPayload(ddl_data_);
647  std::string server_name = ddl_payload["serverName"].GetString();
648  auto& cat = session_ptr_->getCatalog();
649  // update catalog
650  const auto foreign_server = cat.getForeignServer(server_name);
652  opt.populateOptionsMap(foreign_server->getOptionsAsJsonString());
653  opt.populateOptionsMap(ddl_payload["options"]);
654  cat.setForeignServerOptions(server_name, opt.getOptionsAsJsonString());
655 }
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: