OmniSciDB  72c90bc290
 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 5146 of file ParserNode.cpp.

References CHECK, json_str(), and tablesToRename_.

5146  {
5147  CHECK(payload.HasMember("tableNames"));
5148  CHECK(payload["tableNames"].IsArray());
5149  const auto elements = payload["tableNames"].GetArray();
5150  for (const auto& element : elements) {
5151  CHECK(element.HasMember("name"));
5152  CHECK(element.HasMember("newName"));
5153  tablesToRename_.emplace_back(new std::string(json_str(element["name"])),
5154  new std::string(json_str(element["newName"])));
5155  }
5156 }
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 5158 of file ParserNode.cpp.

References tablesToRename_.

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

Definition at line 5162 of file ParserNode.cpp.

References tablesToRename_.

5163  {
5164  for (auto item : tableNames) {
5165  tablesToRename_.emplace_back(new std::string(item.first),
5166  new std::string(item.second));
5167  }
5168 }
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 5236 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().

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