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

#include <DdlCommandExecutor.h>

+ Inheritance diagram for AlterForeignTableCommand:
+ Collaboration diagram for AlterForeignTableCommand:

Public Member Functions

 AlterForeignTableCommand (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 alterOptions (const foreign_storage::ForeignTable &foreign_table)
 
void renameTable (const foreign_storage::ForeignTable *foreign_table)
 
void renameColumn (const foreign_storage::ForeignTable *foreign_table)
 

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

Constructor & Destructor Documentation

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

Definition at line 3230 of file DdlCommandExecutor.cpp.

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

3233  : DdlCommand(ddl_data, session_ptr) {
3234  if (!g_enable_fsi) {
3235  throw std::runtime_error("Unsupported command: ALTER FOREIGN TABLE");
3236  }
3237  auto& ddl_payload = extractPayload(ddl_data_);
3238  CHECK(ddl_payload.HasMember("tableName"));
3239  CHECK(ddl_payload["tableName"].IsString());
3240  CHECK(ddl_payload.HasMember("alterType"));
3241  CHECK(ddl_payload["alterType"].IsString());
3242  if (ddl_payload["alterType"] == "RENAME_TABLE") {
3243  CHECK(ddl_payload.HasMember("newTableName"));
3244  CHECK(ddl_payload["newTableName"].IsString());
3245  } else if (ddl_payload["alterType"] == "RENAME_COLUMN") {
3246  CHECK(ddl_payload.HasMember("oldColumnName"));
3247  CHECK(ddl_payload["oldColumnName"].IsString());
3248  CHECK(ddl_payload.HasMember("newColumnName"));
3249  CHECK(ddl_payload["newColumnName"].IsString());
3250  } else if (ddl_payload["alterType"] == "ALTER_OPTIONS") {
3251  CHECK(ddl_payload.HasMember("options"));
3252  CHECK(ddl_payload["options"].IsObject());
3253  } else {
3254  UNREACHABLE() << "Not a valid alter foreign table command: "
3255  << ddl_payload["alterType"].GetString();
3256  }
3257 }
#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
bool g_enable_fsi
Definition: Catalog.cpp:96

+ Here is the call graph for this function:

Member Function Documentation

void AlterForeignTableCommand::alterOptions ( const foreign_storage::ForeignTable foreign_table)
private

Definition at line 3344 of file DdlCommandExecutor.cpp.

References cat(), foreign_storage::ForeignTable::createOptionsMap(), DdlCommand::ddl_data_, anonymous_namespace{DdlCommandExecutor.cpp}::extractPayload(), DdlCommand::session_ptr_, foreign_storage::ForeignTable::validateAlterOptions(), and foreign_storage::ForeignTable::validateSupportedOptionKeys().

Referenced by execute().

3345  {
3346  auto& ddl_payload = extractPayload(ddl_data_);
3347  const std::string& table_name = ddl_payload["tableName"].GetString();
3348  auto& cat = session_ptr_->getCatalog();
3349  auto new_options_map =
3350  foreign_storage::ForeignTable::createOptionsMap(ddl_payload["options"]);
3351  foreign_table.validateSupportedOptionKeys(new_options_map);
3352  foreign_table.validateAlterOptions(new_options_map);
3353  cat.setForeignTableOptions(table_name, new_options_map, false);
3354 }
std::string cat(Ts &&...args)
void validateAlterOptions(const OptionsMap &options_map) const
Verifies that the given options map only contains options that can be legally altered.
void validateSupportedOptionKeys(const OptionsMap &options_map) const
Verifies that the options_map contains the keys required by a foreign table; including those specifie...
static OptionsMap createOptionsMap(const rapidjson::Value &json_options)
Creates an options map from given options. Converts options that must be upper case appropriately...
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:

ExecutionResult AlterForeignTableCommand::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 3259 of file DdlCommandExecutor.cpp.

References AccessPrivileges::ALTER_TABLE, alterOptions(), CHECK, DdlCommand::ddl_data_, anonymous_namespace{DdlCommandExecutor.cpp}::extractPayload(), ddl_utils::FOREIGN_TABLE, legacylockmgr::getExecuteReadLock(), legacylockmgr::getExecuteWriteLock(), lockmgr::TableLockMgrImpl< TableDataLockMgr >::getWriteLockForTable(), renameColumn(), renameTable(), DdlCommand::session_ptr_, TableDBObjectType, and ddl_utils::validate_table_type().

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

3259  {
3260  if (read_only_mode) {
3261  throw std::runtime_error("ALTER FOREIGN TABLE invalid in read only mode.");
3262  }
3263 
3264  auto& ddl_payload = extractPayload(ddl_data_);
3265  std::string alter_type = ddl_payload["alterType"].GetString();
3266 
3267  // We only need a write lock if we are renaming a table, otherwise a read lock will do.
3270 
3271  if (alter_type == "RENAME_TABLE") {
3272  write_lock = legacylockmgr::getExecuteWriteLock();
3273  } else {
3274  read_lock = legacylockmgr::getExecuteReadLock();
3275  }
3276 
3277  auto& catalog = session_ptr_->getCatalog();
3278  const std::string& table_name = ddl_payload["tableName"].GetString();
3279  auto [td, td_with_lock] =
3280  get_table_descriptor_with_lock<lockmgr::WriteLock>(catalog, table_name, false);
3281 
3283 
3284  if (!session_ptr_->checkDBAccessPrivileges(
3286  throw std::runtime_error(
3287  "Current user does not have the privilege to alter foreign table: " + table_name);
3288  }
3289 
3290  auto table_data_write_lock =
3292  auto foreign_table = dynamic_cast<const foreign_storage::ForeignTable*>(td);
3293  CHECK(foreign_table);
3294 
3295  // std::string alter_type = ddl_payload["alterType"].GetString();
3296  if (alter_type == "RENAME_TABLE") {
3297  renameTable(foreign_table);
3298  } else if (alter_type == "RENAME_COLUMN") {
3299  renameColumn(foreign_table);
3300  } else if (alter_type == "ALTER_OPTIONS") {
3301  alterOptions(*foreign_table);
3302  }
3303 
3304  return ExecutionResult();
3305 }
void alterOptions(const foreign_storage::ForeignTable &foreign_table)
static const AccessPrivileges ALTER_TABLE
Definition: DBObject.h:165
auto getExecuteReadLock()
std::unique_lock< WrapperType< std::shared_mutex >> ExecutorWriteLock
const DdlCommandData & ddl_data_
std::shared_lock< WrapperType< std::shared_mutex >> ExecutorReadLock
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
static WriteLock getWriteLockForTable(const Catalog_Namespace::Catalog &cat, const std::string &table_name)
void validate_table_type(const TableDescriptor *td, const TableType expected_table_type, const std::string &command)
Definition: DdlUtils.cpp:745
void renameTable(const foreign_storage::ForeignTable *foreign_table)
#define CHECK(condition)
Definition: Logger.h:291
auto getExecuteWriteLock()
std::shared_ptr< Catalog_Namespace::SessionInfo const > session_ptr_
void renameColumn(const foreign_storage::ForeignTable *foreign_table)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void AlterForeignTableCommand::renameColumn ( const foreign_storage::ForeignTable foreign_table)
private

Definition at line 3322 of file DdlCommandExecutor.cpp.

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

Referenced by execute().

3323  {
3324  auto& ddl_payload = extractPayload(ddl_data_);
3325  auto& cat = session_ptr_->getCatalog();
3326  const std::string& old_column_name = ddl_payload["oldColumnName"].GetString();
3327  const std::string& new_column_name = ddl_payload["newColumnName"].GetString();
3328  auto column = cat.getMetadataForColumn(foreign_table->tableId, old_column_name);
3329  if (!column) {
3330  throw std::runtime_error("Column with name \"" + old_column_name +
3331  "\" can not be renamed to \"" + new_column_name + "\". " +
3332  "Column with name \"" + old_column_name +
3333  "\" does not exist.");
3334  }
3335  if (cat.getMetadataForColumn(foreign_table->tableId, new_column_name)) {
3336  throw std::runtime_error("Column with name \"" + old_column_name +
3337  "\" can not be renamed to \"" + new_column_name + "\". " +
3338  "A column with name \"" + new_column_name +
3339  "\" already exists.");
3340  }
3341  cat.renameColumn(foreign_table, column, new_column_name);
3342 }
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 AlterForeignTableCommand::renameTable ( const foreign_storage::ForeignTable foreign_table)
private

Definition at line 3307 of file DdlCommandExecutor.cpp.

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

Referenced by execute().

3308  {
3309  auto& ddl_payload = extractPayload(ddl_data_);
3310  auto& cat = session_ptr_->getCatalog();
3311  const std::string& table_name = ddl_payload["tableName"].GetString();
3312  const std::string& new_table_name = ddl_payload["newTableName"].GetString();
3313  if (cat.getForeignTable(new_table_name)) {
3314  throw std::runtime_error("Foreign table with name \"" + table_name +
3315  "\" can not be renamed to \"" + new_table_name + "\". " +
3316  "A different table with name \"" + new_table_name +
3317  "\" already exists.");
3318  }
3319  cat.renameTable(foreign_table, new_table_name);
3320 }
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:


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