OmniSciDB  eb3a3d0a03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 1310 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 1313 of file ParserNode.h.

Constructor & Destructor Documentation

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

Definition at line 4066 of file ParserNode.cpp.

References CHECK, json_str(), and tablesToRename_.

4066  {
4067  CHECK(payload.HasMember("tableNames"));
4068  CHECK(payload["tableNames"].IsArray());
4069  const auto elements = payload["tableNames"].GetArray();
4070  for (const auto& element : elements) {
4071  CHECK(element.HasMember("name"));
4072  CHECK(element.HasMember("newName"));
4073  tablesToRename_.emplace_back(new std::string(json_str(element["name"])),
4074  new std::string(json_str(element["newName"])));
4075  }
4076 }
const std::string json_str(const rapidjson::Value &obj) noexcept
Definition: JsonAccessors.h:44
std::list< TableNamePair > tablesToRename_
Definition: ParserNode.h:1327
#define CHECK(condition)
Definition: Logger.h:209

+ Here is the call graph for this function:

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

Definition at line 4078 of file ParserNode.cpp.

References tablesToRename_.

4078  {
4079  tablesToRename_.emplace_back(tab_name, new_tab_name);
4080 }
std::list< TableNamePair > tablesToRename_
Definition: ParserNode.h:1327
Parser::RenameTableStmt::RenameTableStmt ( std::list< std::pair< std::string, std::string >>  tableNames)

Definition at line 4082 of file ParserNode.cpp.

References tablesToRename_.

4083  {
4084  for (auto item : tableNames) {
4085  tablesToRename_.emplace_back(new std::string(item.first),
4086  new std::string(item.second));
4087  }
4088 }
std::list< TableNamePair > tablesToRename_
Definition: ParserNode.h:1327

Member Function Documentation

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

Implements Parser::DDLStmt.

Definition at line 4146 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, tablesToRename_, and ddl_utils::validate_table_type().

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

4146  {
4147  auto& catalog = session.getCatalog();
4148 
4149  // TODO(adb): the catalog should be handling this locking (see AddColumStmt)
4150  const auto execute_write_lock = mapd_unique_lock<mapd_shared_mutex>(
4153 
4154  // accumulated vector of table names: oldName->newName
4155  std::vector<std::pair<std::string, std::string>> names;
4156 
4157  SubstituteMap tableSubtituteMap;
4158 
4159  for (auto& item : tablesToRename_) {
4160  std::string curTableName = *(item.first);
4161  std::string newTableName = *(item.second);
4162 
4163  // Note: if rename (a->b, b->a)
4164  // requires a tmp name change (a->tmp, b->a, tmp->a),
4165  // inject that here because
4166  // catalog.renameTable() assumes cleanliness else will fail
4167 
4168  std::string altCurTableName = loadTable(catalog, tableSubtituteMap, curTableName);
4169  std::string altNewTableName = loadTable(catalog, tableSubtituteMap, newTableName);
4170 
4171  if (altCurTableName != curTableName && altCurTableName != EMPTY_NAME) {
4172  // rename is a one-shot deal, reset the mapping once used
4173  recordRename(tableSubtituteMap, curTableName, curTableName);
4174  }
4175 
4176  // Check to see if the command (as-entered) will likely execute cleanly (logic-wise)
4177  // src tables exist before coping from
4178  // destination table collisions
4179  // handled (a->b, b->a)
4180  // or flagged (pre-existing a,b ... "RENAME TABLE a->c, b->c" )
4181  // handle mulitple chained renames, tmp names (a_>tmp, b->a, tmp->a)
4182  // etc.
4183  //
4184  if (hasData(tableSubtituteMap, altCurTableName)) {
4185  const TableDescriptor* td = catalog.getMetadataForTable(altCurTableName);
4186  if (td) {
4187  // any table that pre-exists must pass these tests
4189  check_alter_table_privilege(session, td);
4190  }
4191 
4192  if (hasData(tableSubtituteMap, altNewTableName)) {
4193  std::string tmpNewTableName = generateUniqueTableName(altNewTableName);
4194  // rename: newTableName to tmpNewTableName to get it out of the way
4195  // because it was full
4196  recordRename(tableSubtituteMap, altCurTableName, EMPTY_NAME);
4197  recordRename(tableSubtituteMap, altNewTableName, tmpNewTableName);
4198  recordRename(tableSubtituteMap, tmpNewTableName, tmpNewTableName);
4199  names.push_back(
4200  std::pair<std::string, std::string>(altNewTableName, tmpNewTableName));
4201  names.push_back(
4202  std::pair<std::string, std::string>(altCurTableName, altNewTableName));
4203  } else {
4204  // rename: curNewTableName to newTableName
4205  recordRename(tableSubtituteMap, altCurTableName, EMPTY_NAME);
4206  recordRename(tableSubtituteMap, altNewTableName, altNewTableName);
4207  names.push_back(
4208  std::pair<std::string, std::string>(altCurTableName, altNewTableName));
4209  }
4210  } else {
4211  throw std::runtime_error("Source table \'" + curTableName + "\' does not exist.");
4212  }
4213  }
4214  checkNameSubstition(tableSubtituteMap);
4215 
4216  catalog.renameTable(names);
4217 
4218  // just to be explicit, clean out the list, the unique_ptr will delete
4219  while (!tablesToRename_.empty()) {
4220  tablesToRename_.pop_front();
4221  }
4222 } // namespace Parser
bool hasData(SubstituteMap &sMap, std::string tableName)
std::string generateUniqueTableName(std::string name)
void recordRename(SubstituteMap &sMap, std::string oldName, std::string newName)
std::list< TableNamePair > tablesToRename_
Definition: ParserNode.h:1327
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:650
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 1327 of file ParserNode.h.

Referenced by execute(), and RenameTableStmt().


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