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

#include <DdlCommandExecutor.h>

+ Inheritance diagram for AlterDatabaseCommand:
+ Collaboration diagram for AlterDatabaseCommand:

Public Member Functions

 AlterDatabaseCommand (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 changeOwner ()
 
void rename ()
 

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 387 of file DdlCommandExecutor.h.

Constructor & Destructor Documentation

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

Definition at line 937 of file DdlCommandExecutor.cpp.

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

940  : DdlCommand(ddl_data, session_ptr) {
941  auto& ddl_payload = extractPayload(ddl_data_);
942  CHECK(ddl_payload.HasMember("databaseName"));
943  CHECK(ddl_payload["databaseName"].IsString());
944  CHECK(ddl_payload.HasMember("alterType"));
945  CHECK(ddl_payload["alterType"].IsString());
946  if (ddl_payload["alterType"] == "RENAME_DATABASE") {
947  CHECK(ddl_payload.HasMember("newDatabaseName"));
948  CHECK(ddl_payload["newDatabaseName"].IsString());
949  } else if (ddl_payload["alterType"] == "CHANGE_OWNER") {
950  CHECK(ddl_payload.HasMember("newOwner"));
951  CHECK(ddl_payload["newOwner"].IsString());
952  } else {
953  UNREACHABLE(); // not-implemented alterType
954  }
955 }
#define UNREACHABLE()
Definition: Logger.h:338
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:291

+ Here is the call graph for this function:

Member Function Documentation

void AlterDatabaseCommand::changeOwner ( )
private

Definition at line 999 of file DdlCommandExecutor.cpp.

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

Referenced by execute().

999  {
1000  auto& ddl_payload = extractPayload(ddl_data_);
1001  std::string database_name = ddl_payload["databaseName"].GetString();
1002  std::string new_owner = ddl_payload["newOwner"].GetString();
1003  auto& sys_cat = Catalog_Namespace::SysCatalog::instance();
1004  if (!session_ptr_->get_currentUser().isSuper) {
1005  throw std::runtime_error(
1006  "Only a super user can change a database's owner. "
1007  "Current user is not a super-user. "
1008  "Database with name \"" +
1009  database_name + "\" will not have owner changed.");
1010  }
1011 
1012  sys_cat.changeDatabaseOwner(database_name, new_owner);
1013 }
const DdlCommandData & ddl_data_
static SysCatalog & instance()
Definition: SysCatalog.h:343
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 AlterDatabaseCommand::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 957 of file DdlCommandExecutor.cpp.

References changeOwner(), DdlCommand::ddl_data_, anonymous_namespace{DdlCommandExecutor.cpp}::extractPayload(), legacylockmgr::getExecuteWriteLock(), Catalog_Namespace::SysCatalog::instance(), and rename().

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

957  {
958  auto execute_write_lock = legacylockmgr::getExecuteWriteLock();
959 
960  if (read_only_mode) {
961  throw std::runtime_error("ALTER DATABASE invalid in read only mode.");
962  }
963  auto& ddl_payload = extractPayload(ddl_data_);
964  std::string databaseName = ddl_payload["databaseName"].GetString();
965 
966  auto& sys_cat = Catalog_Namespace::SysCatalog::instance();
968  if (!sys_cat.getMetadataForDB(databaseName, db)) {
969  throw std::runtime_error("Database " + databaseName + " does not exists.");
970  }
971 
972  std::string alter_type = ddl_payload["alterType"].GetString();
973  if (alter_type == "CHANGE_OWNER") {
974  changeOwner();
975  } else if (alter_type == "RENAME_DATABASE") {
976  rename();
977  }
978 
979  return ExecutionResult();
980 }
const DdlCommandData & ddl_data_
static SysCatalog & instance()
Definition: SysCatalog.h:343
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
auto getExecuteWriteLock()

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void AlterDatabaseCommand::rename ( )
private

Definition at line 982 of file DdlCommandExecutor.cpp.

References CHECK, Catalog_Namespace::DBMetadata::dbOwner, DdlCommand::ddl_data_, anonymous_namespace{DdlCommandExecutor.cpp}::extractPayload(), Catalog_Namespace::SysCatalog::instance(), Catalog_Namespace::SysCatalog::renameDatabase(), and DdlCommand::session_ptr_.

Referenced by execute().

982  {
983  auto& ddl_payload = extractPayload(ddl_data_);
984  std::string database_name = ddl_payload["databaseName"].GetString();
985  std::string new_database_name = ddl_payload["newDatabaseName"].GetString();
986 
988  CHECK(Catalog_Namespace::SysCatalog::instance().getMetadataForDB(database_name, db));
989 
990  if (!session_ptr_->get_currentUser().isSuper &&
991  session_ptr_->get_currentUser().userId != db.dbOwner) {
992  throw std::runtime_error("Only a super user or the owner can rename the database.");
993  }
994 
996  new_database_name);
997 }
const DdlCommandData & ddl_data_
static SysCatalog & instance()
Definition: SysCatalog.h:343
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
void renameDatabase(std::string const &old_name, std::string const &new_name)
#define CHECK(condition)
Definition: Logger.h:291
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: