OmniSciDB  cde582ebc3
 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, bool read_only_mode) 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 1337 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 1340 of file ParserNode.h.

Constructor & Destructor Documentation

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

Definition at line 4749 of file ParserNode.cpp.

References CHECK, json_str(), and tablesToRename_.

4749  {
4750  CHECK(payload.HasMember("tableNames"));
4751  CHECK(payload["tableNames"].IsArray());
4752  const auto elements = payload["tableNames"].GetArray();
4753  for (const auto& element : elements) {
4754  CHECK(element.HasMember("name"));
4755  CHECK(element.HasMember("newName"));
4756  tablesToRename_.emplace_back(new std::string(json_str(element["name"])),
4757  new std::string(json_str(element["newName"])));
4758  }
4759 }
const std::string json_str(const rapidjson::Value &obj) noexcept
Definition: JsonAccessors.h:44
std::list< TableNamePair > tablesToRename_
Definition: ParserNode.h:1355
#define CHECK(condition)
Definition: Logger.h:222

+ Here is the call graph for this function:

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

Definition at line 4761 of file ParserNode.cpp.

References tablesToRename_.

4761  {
4762  tablesToRename_.emplace_back(tab_name, new_tab_name);
4763 }
std::list< TableNamePair > tablesToRename_
Definition: ParserNode.h:1355
Parser::RenameTableStmt::RenameTableStmt ( std::list< std::pair< std::string, std::string >>  tableNames)

Definition at line 4765 of file ParserNode.cpp.

References tablesToRename_.

4766  {
4767  for (auto item : tableNames) {
4768  tablesToRename_.emplace_back(new std::string(item.first),
4769  new std::string(item.second));
4770  }
4771 }
std::list< TableNamePair > tablesToRename_
Definition: ParserNode.h:1355

Member Function Documentation

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

Implements Parser::DDLStmt.

Definition at line 4839 of file ParserNode.cpp.

References Parser::check_alter_table_privilege(), Parser::anonymous_namespace{ParserNode.cpp}::checkNameSubstition(), Parser::anonymous_namespace{ParserNode.cpp}::disable_foreign_tables(), Parser::anonymous_namespace{ParserNode.cpp}::EMPTY_NAME, legacylockmgr::ExecutorOuterLock, Parser::anonymous_namespace{ParserNode.cpp}::generateUniqueTableName(), Catalog_Namespace::SessionInfo::getCatalog(), Catalog_Namespace::Catalog::getMetadataForTable(), legacylockmgr::LockMgr< MutexType, KeyType >::getMutex(), Parser::anonymous_namespace{ParserNode.cpp}::hasData(), Parser::anonymous_namespace{ParserNode.cpp}::loadTable(), Parser::anonymous_namespace{ParserNode.cpp}::recordRename(), Catalog_Namespace::Catalog::renameTable(), and tablesToRename_.

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

4840  {
4841  if (read_only_mode) {
4842  throw std::runtime_error("RENAME TABLE invalid in read only mode.");
4843  }
4844  auto& catalog = session.getCatalog();
4845 
4846  // TODO(adb): the catalog should be handling this locking (see AddColumStmt)
4847  const auto execute_write_lock =
4851 
4852  // accumulated vector of table names: oldName->newName
4853  std::vector<std::pair<std::string, std::string>> names;
4854 
4855  SubstituteMap tableSubtituteMap;
4856 
4857  for (auto& item : tablesToRename_) {
4858  std::string curTableName = *(item.first);
4859  std::string newTableName = *(item.second);
4860 
4861  // Note: if rename (a->b, b->a)
4862  // requires a tmp name change (a->tmp, b->a, tmp->a),
4863  // inject that here because
4864  // catalog.renameTable() assumes cleanliness else will fail
4865 
4866  std::string altCurTableName = loadTable(catalog, tableSubtituteMap, curTableName);
4867  std::string altNewTableName = loadTable(catalog, tableSubtituteMap, newTableName);
4868 
4869  if (altCurTableName != curTableName && altCurTableName != EMPTY_NAME) {
4870  // rename is a one-shot deal, reset the mapping once used
4871  recordRename(tableSubtituteMap, curTableName, curTableName);
4872  }
4873 
4874  // Check to see if the command (as-entered) will likely execute cleanly (logic-wise)
4875  // src tables exist before coping from
4876  // destination table collisions
4877  // handled (a->b, b->a)
4878  // or flagged (pre-existing a,b ... "RENAME TABLE a->c, b->c" )
4879  // handle mulitple chained renames, tmp names (a_>tmp, b->a, tmp->a)
4880  // etc.
4881  //
4882  if (hasData(tableSubtituteMap, altCurTableName)) {
4883  const TableDescriptor* td = catalog.getMetadataForTable(altCurTableName);
4884  if (td) {
4885  // Tables *and* views may be renamed here, foreign tables not
4886  // -> just block foreign tables
4888  check_alter_table_privilege(session, td);
4889  }
4890 
4891  if (hasData(tableSubtituteMap, altNewTableName)) {
4892  std::string tmpNewTableName = generateUniqueTableName(altNewTableName);
4893  // rename: newTableName to tmpNewTableName to get it out of the way
4894  // because it was full
4895  recordRename(tableSubtituteMap, altCurTableName, EMPTY_NAME);
4896  recordRename(tableSubtituteMap, altNewTableName, tmpNewTableName);
4897  recordRename(tableSubtituteMap, tmpNewTableName, tmpNewTableName);
4898  names.emplace_back(altNewTableName, tmpNewTableName);
4899  names.emplace_back(altCurTableName, altNewTableName);
4900  } else {
4901  // rename: curNewTableName to newTableName
4902  recordRename(tableSubtituteMap, altCurTableName, EMPTY_NAME);
4903  recordRename(tableSubtituteMap, altNewTableName, altNewTableName);
4904  names.emplace_back(altCurTableName, altNewTableName);
4905  }
4906  } else {
4907  throw std::runtime_error("Source table \'" + curTableName + "\' does not exist.");
4908  }
4909  }
4910  checkNameSubstition(tableSubtituteMap);
4911 
4912  catalog.renameTable(names);
4913 
4914  // just to be explicit, clean out the list, the unique_ptr will delete
4915  while (!tablesToRename_.empty()) {
4916  tablesToRename_.pop_front();
4917  }
4918 } // namespace Parser
static std::shared_ptr< WrapperType< MutexType > > getMutex(const LockType lockType, const KeyType &key)
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:1355
std::unique_lock< T > unique_lock
void check_alter_table_privilege(const Catalog_Namespace::SessionInfo &session, const TableDescriptor *td)
Catalog & getCatalog() const
Definition: SessionInfo.h:65
void disable_foreign_tables(const TableDescriptor *td)
void checkNameSubstition(SubstituteMap &sMap)
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 1355 of file ParserNode.h.

Referenced by execute(), and RenameTableStmt().


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