OmniSciDB  c1a53651b2
 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 4827 of file ParserNode.cpp.

References CHECK, json_str(), and tablesToRename_.

4827  {
4828  CHECK(payload.HasMember("tableNames"));
4829  CHECK(payload["tableNames"].IsArray());
4830  const auto elements = payload["tableNames"].GetArray();
4831  for (const auto& element : elements) {
4832  CHECK(element.HasMember("name"));
4833  CHECK(element.HasMember("newName"));
4834  tablesToRename_.emplace_back(new std::string(json_str(element["name"])),
4835  new std::string(json_str(element["newName"])));
4836  }
4837 }
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:291

+ Here is the call graph for this function:

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

Definition at line 4839 of file ParserNode.cpp.

References tablesToRename_.

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

Definition at line 4843 of file ParserNode.cpp.

References tablesToRename_.

4844  {
4845  for (auto item : tableNames) {
4846  tablesToRename_.emplace_back(new std::string(item.first),
4847  new std::string(item.second));
4848  }
4849 }
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 4917 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(), legacylockmgr::LockMgr< MutexType, KeyType >::getMutex(), Parser::anonymous_namespace{ParserNode.cpp}::hasData(), Parser::anonymous_namespace{ParserNode.cpp}::loadTable(), Parser::anonymous_namespace{ParserNode.cpp}::recordRename(), and tablesToRename_.

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

4918  {
4919  if (read_only_mode) {
4920  throw std::runtime_error("RENAME TABLE invalid in read only mode.");
4921  }
4922  auto& catalog = session.getCatalog();
4923 
4924  // TODO(adb): the catalog should be handling this locking (see AddColumStmt)
4925  const auto execute_write_lock =
4929 
4930  // accumulated vector of table names: oldName->newName
4931  std::vector<std::pair<std::string, std::string>> names;
4932 
4933  SubstituteMap tableSubtituteMap;
4934 
4935  for (auto& item : tablesToRename_) {
4936  std::string curTableName = *(item.first);
4937  std::string newTableName = *(item.second);
4938 
4939  // Note: if rename (a->b, b->a)
4940  // requires a tmp name change (a->tmp, b->a, tmp->a),
4941  // inject that here because
4942  // catalog.renameTable() assumes cleanliness else will fail
4943 
4944  std::string altCurTableName = loadTable(catalog, tableSubtituteMap, curTableName);
4945  std::string altNewTableName = loadTable(catalog, tableSubtituteMap, newTableName);
4946 
4947  if (altCurTableName != curTableName && altCurTableName != EMPTY_NAME) {
4948  // rename is a one-shot deal, reset the mapping once used
4949  recordRename(tableSubtituteMap, curTableName, curTableName);
4950  }
4951 
4952  // Check to see if the command (as-entered) will likely execute cleanly (logic-wise)
4953  // src tables exist before coping from
4954  // destination table collisions
4955  // handled (a->b, b->a)
4956  // or flagged (pre-existing a,b ... "RENAME TABLE a->c, b->c" )
4957  // handle mulitple chained renames, tmp names (a_>tmp, b->a, tmp->a)
4958  // etc.
4959  //
4960  if (hasData(tableSubtituteMap, altCurTableName)) {
4961  const TableDescriptor* td = catalog.getMetadataForTable(altCurTableName);
4962  if (td) {
4963  // Tables *and* views may be renamed here, foreign tables not
4964  // -> just block foreign tables
4966  check_alter_table_privilege(session, td);
4967  }
4968 
4969  if (hasData(tableSubtituteMap, altNewTableName)) {
4970  std::string tmpNewTableName = generateUniqueTableName(altNewTableName);
4971  // rename: newTableName to tmpNewTableName to get it out of the way
4972  // because it was full
4973  recordRename(tableSubtituteMap, altCurTableName, EMPTY_NAME);
4974  recordRename(tableSubtituteMap, altNewTableName, tmpNewTableName);
4975  recordRename(tableSubtituteMap, tmpNewTableName, tmpNewTableName);
4976  names.emplace_back(altNewTableName, tmpNewTableName);
4977  names.emplace_back(altCurTableName, altNewTableName);
4978  } else {
4979  // rename: curNewTableName to newTableName
4980  recordRename(tableSubtituteMap, altCurTableName, EMPTY_NAME);
4981  recordRename(tableSubtituteMap, altNewTableName, altNewTableName);
4982  names.emplace_back(altCurTableName, altNewTableName);
4983  }
4984  } else {
4985  throw std::runtime_error("Source table \'" + curTableName + "\' does not exist.");
4986  }
4987  }
4988  checkNameSubstition(tableSubtituteMap);
4989 
4990  catalog.renameTable(names);
4991 
4992  // just to be explicit, clean out the list, the unique_ptr will delete
4993  while (!tablesToRename_.empty()) {
4994  tablesToRename_.pop_front();
4995  }
4996 } // 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:75
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: