OmniSciDB  a5dc49c757
 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 1324 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 1327 of file ParserNode.h.

Constructor & Destructor Documentation

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

Definition at line 5167 of file ParserNode.cpp.

References CHECK, json_str(), and tablesToRename_.

5167  {
5168  CHECK(payload.HasMember("tableNames"));
5169  CHECK(payload["tableNames"].IsArray());
5170  const auto elements = payload["tableNames"].GetArray();
5171  for (const auto& element : elements) {
5172  CHECK(element.HasMember("name"));
5173  CHECK(element.HasMember("newName"));
5174  tablesToRename_.emplace_back(new std::string(json_str(element["name"])),
5175  new std::string(json_str(element["newName"])));
5176  }
5177 }
const std::string json_str(const rapidjson::Value &obj) noexcept
Definition: JsonAccessors.h:46
std::list< TableNamePair > tablesToRename_
Definition: ParserNode.h:1342
#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 5179 of file ParserNode.cpp.

References tablesToRename_.

5179  {
5180  tablesToRename_.emplace_back(tab_name, new_tab_name);
5181 }
std::list< TableNamePair > tablesToRename_
Definition: ParserNode.h:1342
Parser::RenameTableStmt::RenameTableStmt ( std::list< std::pair< std::string, std::string >>  tableNames)

Definition at line 5183 of file ParserNode.cpp.

References tablesToRename_.

5184  {
5185  for (auto item : tableNames) {
5186  tablesToRename_.emplace_back(new std::string(item.first),
5187  new std::string(item.second));
5188  }
5189 }
std::list< TableNamePair > tablesToRename_
Definition: ParserNode.h:1342

Member Function Documentation

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

Implements Parser::DDLStmt.

Definition at line 5257 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, Parser::anonymous_namespace{ParserNode.cpp}::generateUniqueTableName(), Catalog_Namespace::SessionInfo::getCatalog(), legacylockmgr::getExecuteWriteLock(), 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().

5258  {
5259  if (read_only_mode) {
5260  throw std::runtime_error("RENAME TABLE invalid in read only mode.");
5261  }
5262  auto& catalog = session.getCatalog();
5263 
5264  // TODO(adb): the catalog should be handling this locking (see AddColumStmt)
5265  const auto execute_write_lock = legacylockmgr::getExecuteWriteLock();
5266 
5267  // accumulated vector of table names: oldName->newName
5268  std::vector<std::pair<std::string, std::string>> names;
5269 
5270  SubstituteMap tableSubtituteMap;
5271 
5272  for (auto& item : tablesToRename_) {
5273  std::string curTableName = *(item.first);
5274  std::string newTableName = *(item.second);
5275 
5276  // Note: if rename (a->b, b->a)
5277  // requires a tmp name change (a->tmp, b->a, tmp->a),
5278  // inject that here because
5279  // catalog.renameTable() assumes cleanliness else will fail
5280 
5281  std::string altCurTableName = loadTable(catalog, tableSubtituteMap, curTableName);
5282  std::string altNewTableName = loadTable(catalog, tableSubtituteMap, newTableName);
5283 
5284  if (altCurTableName != curTableName && altCurTableName != EMPTY_NAME) {
5285  // rename is a one-shot deal, reset the mapping once used
5286  recordRename(tableSubtituteMap, curTableName, curTableName);
5287  }
5288 
5289  // Check to see if the command (as-entered) will likely execute cleanly (logic-wise)
5290  // src tables exist before coping from
5291  // destination table collisions
5292  // handled (a->b, b->a)
5293  // or flagged (pre-existing a,b ... "RENAME TABLE a->c, b->c" )
5294  // handle mulitple chained renames, tmp names (a_>tmp, b->a, tmp->a)
5295  // etc.
5296  //
5297  if (hasData(tableSubtituteMap, altCurTableName)) {
5298  const TableDescriptor* td = catalog.getMetadataForTable(altCurTableName);
5299  if (td) {
5300  // Tables *and* views may be renamed here, foreign tables not
5301  // -> just block foreign tables
5303  check_alter_table_privilege(session, td);
5304  }
5305 
5306  if (hasData(tableSubtituteMap, altNewTableName)) {
5307  std::string tmpNewTableName = generateUniqueTableName(altNewTableName);
5308  // rename: newTableName to tmpNewTableName to get it out of the way
5309  // because it was full
5310  recordRename(tableSubtituteMap, altCurTableName, EMPTY_NAME);
5311  recordRename(tableSubtituteMap, altNewTableName, tmpNewTableName);
5312  recordRename(tableSubtituteMap, tmpNewTableName, tmpNewTableName);
5313  names.emplace_back(altNewTableName, tmpNewTableName);
5314  names.emplace_back(altCurTableName, altNewTableName);
5315  } else {
5316  // rename: curNewTableName to newTableName
5317  recordRename(tableSubtituteMap, altCurTableName, EMPTY_NAME);
5318  recordRename(tableSubtituteMap, altNewTableName, altNewTableName);
5319  names.emplace_back(altCurTableName, altNewTableName);
5320  }
5321  } else {
5322  throw std::runtime_error("Source table \'" + curTableName + "\' does not exist.");
5323  }
5324  }
5325  checkNameSubstition(tableSubtituteMap);
5326 
5327  catalog.renameTables(names);
5328 
5329  // just to be explicit, clean out the list, the unique_ptr will delete
5330  while (!tablesToRename_.empty()) {
5331  tablesToRename_.pop_front();
5332  }
5333 } // 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:1342
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)
auto getExecuteWriteLock()
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 1342 of file ParserNode.h.

Referenced by execute(), and RenameTableStmt().


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