OmniSciDB  4201147b46
 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 4739 of file ParserNode.cpp.

References CHECK, json_str(), and tablesToRename_.

4739  {
4740  CHECK(payload.HasMember("tableNames"));
4741  CHECK(payload["tableNames"].IsArray());
4742  const auto elements = payload["tableNames"].GetArray();
4743  for (const auto& element : elements) {
4744  CHECK(element.HasMember("name"));
4745  CHECK(element.HasMember("newName"));
4746  tablesToRename_.emplace_back(new std::string(json_str(element["name"])),
4747  new std::string(json_str(element["newName"])));
4748  }
4749 }
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 4751 of file ParserNode.cpp.

References tablesToRename_.

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

Definition at line 4755 of file ParserNode.cpp.

References tablesToRename_.

4756  {
4757  for (auto item : tableNames) {
4758  tablesToRename_.emplace_back(new std::string(item.first),
4759  new std::string(item.second));
4760  }
4761 }
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 4829 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().

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