OmniSciDB  ba1bac9284
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Parser::RenameTableStmt Class Reference

#include <ParserNode.h>

+ Inheritance diagram for Parser::RenameTableStmt:
+ Collaboration diagram for Parser::RenameTableStmt:

Public Types

using TableNamePair = std::pair< std::unique_ptr< std::string >, std::unique_ptr< std::string >>
 

Public Member Functions

 RenameTableStmt (const rapidjson::Value &payload)
 
 RenameTableStmt (std::string *tab_name, std::string *new_tab_name)
 
 RenameTableStmt (std::list< std::pair< std::string, std::string >> tableNames)
 
void execute (const Catalog_Namespace::SessionInfo &session) override
 
- Public Member Functions inherited from Parser::DDLStmt
void setColumnDescriptor (ColumnDescriptor &cd, const ColumnDef *coldef)
 
- Public Member Functions inherited from Parser::Node
virtual ~Node ()
 

Private Attributes

std::list< TableNamePairtablesToRename
 

Detailed Description

Definition at line 1305 of file ParserNode.h.

Member Typedef Documentation

using Parser::RenameTableStmt::TableNamePair = std::pair<std::unique_ptr<std::string>, std::unique_ptr<std::string>>

Definition at line 1308 of file ParserNode.h.

Constructor & Destructor Documentation

Parser::RenameTableStmt::RenameTableStmt ( const rapidjson::Value &  payload)

Definition at line 3916 of file ParserNode.cpp.

References CHECK, and json_str().

3916  {
3917  CHECK(payload.HasMember("command"));
3918 
3919  CHECK(payload.HasMember("tableNames"));
3920  CHECK(payload["tableNames"].IsArray());
3921  const auto elements = payload["tableNames"].GetArray();
3922  for (const auto& element : elements) {
3923  CHECK(element.HasMember("name"));
3924  CHECK(element.HasMember("newName"));
3925  tablesToRename.emplace_back(new std::string(json_str(element["name"])),
3926  new std::string(json_str(element["newName"])));
3927  }
3928 }
const std::string json_str(const rapidjson::Value &obj) noexcept
Definition: JsonAccessors.h:44
std::list< TableNamePair > tablesToRename
Definition: ParserNode.h:1322
#define CHECK(condition)
Definition: Logger.h:206

+ Here is the call graph for this function:

Parser::RenameTableStmt::RenameTableStmt ( std::string *  tab_name,
std::string *  new_tab_name 
)

Definition at line 3930 of file ParserNode.cpp.

3930  {
3931  tablesToRename.emplace_back(tab_name, new_tab_name);
3932 }
std::list< TableNamePair > tablesToRename
Definition: ParserNode.h:1322
Parser::RenameTableStmt::RenameTableStmt ( std::list< std::pair< std::string, std::string >>  tableNames)

Definition at line 3934 of file ParserNode.cpp.

3935  {
3936  for (auto item : tableNames) {
3937  tablesToRename.emplace_back(new std::string(item.first),
3938  new std::string(item.second));
3939  }
3940 }
std::list< TableNamePair > tablesToRename
Definition: ParserNode.h:1322

Member Function Documentation

void Parser::RenameTableStmt::execute ( const Catalog_Namespace::SessionInfo session)
overridevirtual

Implements Parser::DDLStmt.

Definition at line 3998 of file ParserNode.cpp.

References Parser::check_alter_table_privilege(), Parser::anonymous_namespace{ParserNode.cpp}::checkNameSubstition(), Parser::anonymous_namespace{ParserNode.cpp}::EMPTY_NAME, legacylockmgr::ExecutorOuterLock, Parser::anonymous_namespace{ParserNode.cpp}::generateUniqueTableName(), Catalog_Namespace::SessionInfo::getCatalog(), legacylockmgr::LockMgr< MutexType, KeyType >::getMutex(), Parser::anonymous_namespace{ParserNode.cpp}::hasData(), Parser::anonymous_namespace{ParserNode.cpp}::loadTable(), Parser::anonymous_namespace{ParserNode.cpp}::recordRename(), ddl_utils::TABLE, and ddl_utils::validate_table_type().

Referenced by Parser::AlterTableStmt::delegateExecute(), and omnisci.cursor.Cursor::executemany().

3998  {
3999  auto& catalog = session.getCatalog();
4000 
4001  // TODO(adb): the catalog should be handling this locking (see AddColumStmt)
4002  const auto execute_write_lock = mapd_unique_lock<mapd_shared_mutex>(
4005 
4006  // accumulated vector of table names: oldName->newName
4007  std::vector<std::pair<std::string, std::string>> names;
4008 
4009  SubstituteMap tableSubtituteMap;
4010 
4011  for (auto& item : tablesToRename) {
4012  std::string curTableName = *(item.first);
4013  std::string newTableName = *(item.second);
4014 
4015  // Note: if rename (a->b, b->a)
4016  // requires a tmp name change (a->tmp, b->a, tmp->a),
4017  // inject that here because
4018  // catalog.renameTable() assumes cleanliness else will fail
4019 
4020  std::string altCurTableName = loadTable(catalog, tableSubtituteMap, curTableName);
4021  std::string altNewTableName = loadTable(catalog, tableSubtituteMap, newTableName);
4022 
4023  if (altCurTableName != curTableName && altCurTableName != EMPTY_NAME) {
4024  // rename is a one-shot deal, reset the mapping once used
4025  recordRename(tableSubtituteMap, curTableName, curTableName);
4026  }
4027 
4028  // Check to see if the command (as-entered) will likely execute cleanly (logic-wise)
4029  // src tables exist before coping from
4030  // destination table collisions
4031  // handled (a->b, b->a)
4032  // or flagged (pre-existing a,b ... "RENAME TABLE a->c, b->c" )
4033  // handle mulitple chained renames, tmp names (a_>tmp, b->a, tmp->a)
4034  // etc.
4035  //
4036  if (hasData(tableSubtituteMap, altCurTableName)) {
4037  const TableDescriptor* td = catalog.getMetadataForTable(altCurTableName);
4038  if (td) {
4039  // any table that pre-exists must pass these tests
4041  check_alter_table_privilege(session, td);
4042  }
4043 
4044  if (hasData(tableSubtituteMap, altNewTableName)) {
4045  std::string tmpNewTableName = generateUniqueTableName(altNewTableName);
4046  // rename: newTableName to tmpNewTableName to get it out of the way
4047  // because it was full
4048  recordRename(tableSubtituteMap, altCurTableName, EMPTY_NAME);
4049  recordRename(tableSubtituteMap, altNewTableName, tmpNewTableName);
4050  recordRename(tableSubtituteMap, tmpNewTableName, tmpNewTableName);
4051  names.push_back(
4052  std::pair<std::string, std::string>(altNewTableName, tmpNewTableName));
4053  names.push_back(
4054  std::pair<std::string, std::string>(altCurTableName, altNewTableName));
4055  } else {
4056  // rename: curNewTableName to newTableName
4057  recordRename(tableSubtituteMap, altCurTableName, EMPTY_NAME);
4058  recordRename(tableSubtituteMap, altNewTableName, altNewTableName);
4059  names.push_back(
4060  std::pair<std::string, std::string>(altCurTableName, altNewTableName));
4061  }
4062  } else {
4063  throw std::runtime_error("Source table \'" + curTableName + "\' does not exist.");
4064  }
4065  }
4066  checkNameSubstition(tableSubtituteMap);
4067 
4068  catalog.renameTable(names);
4069 
4070  // just to be explicit, clean out the list, the unique_ptr will delete
4071  while (!tablesToRename.empty()) {
4072  tablesToRename.pop_front();
4073  }
4074 } // namespace Parser
bool hasData(SubstituteMap &sMap, std::string tableName)
std::list< TableNamePair > tablesToRename
Definition: ParserNode.h:1322
std::string generateUniqueTableName(std::string name)
void recordRename(SubstituteMap &sMap, std::string oldName, std::string newName)
void check_alter_table_privilege(const Catalog_Namespace::SessionInfo &session, const TableDescriptor *td)
Catalog & getCatalog() const
Definition: SessionInfo.h:67
void checkNameSubstition(SubstituteMap &sMap)
static std::shared_ptr< MutexType > getMutex(const LockType lockType, const KeyType &key)
Definition: LegacyLockMgr.h:51
void validate_table_type(const TableDescriptor *td, const TableType expected_table_type, const std::string &command)
Definition: DdlUtils.cpp:554
std::map< std::string, std::string > SubstituteMap
static constexpr char const * EMPTY_NAME
std::string loadTable(Catalog_Namespace::Catalog &catalog, SubstituteMap &sMap, std::string tableName)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

std::list<TableNamePair> Parser::RenameTableStmt::tablesToRename
private

Definition at line 1322 of file ParserNode.h.


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