OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AlterTableCommand Class Reference

#include <DdlCommandExecutor.h>

+ Inheritance diagram for AlterTableCommand:
+ Collaboration diagram for AlterTableCommand:

Public Member Functions

 AlterTableCommand (const DdlCommandData &ddl_data, std::shared_ptr< Catalog_Namespace::SessionInfo const > session_ptr)
 
ExecutionResult execute (bool read_only_mode) override
 
- Public Member Functions inherited from DdlCommand
 DdlCommand (const DdlCommandData &ddl_data, std::shared_ptr< Catalog_Namespace::SessionInfo const > session_ptr)
 

Additional Inherited Members

- Protected Attributes inherited from DdlCommand
const DdlCommandDataddl_data_
 
std::shared_ptr
< Catalog_Namespace::SessionInfo
const > 
session_ptr_
 

Detailed Description

Definition at line 159 of file DdlCommandExecutor.h.

Constructor & Destructor Documentation

AlterTableCommand::AlterTableCommand ( const DdlCommandData ddl_data,
std::shared_ptr< Catalog_Namespace::SessionInfo const >  session_ptr 
)

Definition at line 2741 of file DdlCommandExecutor.cpp.

References CHECK, DdlCommand::ddl_data_, and anonymous_namespace{DdlCommandExecutor.cpp}::extractPayload().

2744  : DdlCommand(ddl_data, session_ptr) {
2745  auto& ddl_payload = extractPayload(ddl_data_);
2746 
2747  CHECK(ddl_payload.HasMember("tableName"));
2748  CHECK(ddl_payload["tableName"].IsString());
2749 
2750  CHECK(ddl_payload.HasMember("alterType"));
2751  CHECK(ddl_payload["alterType"].IsString());
2752 }
const DdlCommandData & ddl_data_
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
DdlCommand(const DdlCommandData &ddl_data, std::shared_ptr< Catalog_Namespace::SessionInfo const > session_ptr)
#define CHECK(condition)
Definition: Logger.h:291

+ Here is the call graph for this function:

Member Function Documentation

ExecutionResult AlterTableCommand::execute ( bool  read_only_mode)
overridevirtual

Executes the DDL command corresponding to provided JSON payload.

Parameters
_returnresult of DDL command execution (if applicable)

Implements DdlCommand.

Definition at line 3108 of file DdlCommandExecutor.cpp.

References CHECK, Parser::column_from_json(), DdlCommand::ddl_data_, anonymous_namespace{DdlCommandExecutor.cpp}::extractPayload(), logger::FATAL, json_i64(), json_str(), LOG, DdlCommand::session_ptr_, split(), and run_benchmark_import::type.

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

3108  {
3109  if (read_only_mode) {
3110  throw std::runtime_error("ALTER TABLE invalid in read only mode.");
3111  }
3112 
3113  auto& ddl_payload = extractPayload(ddl_data_);
3114  const auto tableName = std::string(ddl_payload["tableName"].GetString());
3115 
3116  CHECK(ddl_payload.HasMember("alterType"));
3117  auto type = json_str(ddl_payload["alterType"]);
3118 
3119  if (type == "RENAME_TABLE") {
3120  CHECK(ddl_payload.HasMember("newTableName"));
3121  auto newTableName = json_str(ddl_payload["newTableName"]);
3122  std::unique_ptr<Parser::DDLStmt>(
3123  new Parser::RenameTableStmt(new std::string(tableName),
3124  new std::string(newTableName)))
3125  ->execute(*session_ptr_, read_only_mode);
3126  return {};
3127 
3128  } else if (type == "RENAME_COLUMN") {
3129  CHECK(ddl_payload.HasMember("columnName"));
3130  auto columnName = json_str(ddl_payload["columnName"]);
3131  CHECK(ddl_payload.HasMember("newColumnName"));
3132  auto newColumnName = json_str(ddl_payload["newColumnName"]);
3133  std::unique_ptr<Parser::DDLStmt>(
3134  new Parser::RenameColumnStmt(new std::string(tableName),
3135  new std::string(columnName),
3136  new std::string(newColumnName)))
3137  ->execute(*session_ptr_, read_only_mode);
3138  return {};
3139 
3140  } else if (type == "ALTER_COLUMN") {
3141  return AlterTableAlterColumnCommand{ddl_data_, session_ptr_}.execute(read_only_mode);
3142  } else if (type == "ADD_COLUMN") {
3143  CHECK(ddl_payload.HasMember("columnData"));
3144  CHECK(ddl_payload["columnData"].IsArray());
3145 
3146  // New Columns go into this list
3147  std::list<Parser::ColumnDef*>* table_element_list_ =
3148  new std::list<Parser::ColumnDef*>;
3149 
3150  const auto elements = ddl_payload["columnData"].GetArray();
3151  for (const auto& element : elements) {
3152  CHECK(element.IsObject());
3153  CHECK(element.HasMember("type"));
3154  if (json_str(element["type"]) == "SQL_COLUMN_DECLARATION") {
3155  auto col_def = Parser::column_from_json(element);
3156  table_element_list_->emplace_back(col_def.release());
3157  } else {
3158  LOG(FATAL) << "Unsupported element type for ALTER TABLE: "
3159  << element["type"].GetString();
3160  }
3161  }
3162 
3163  std::unique_ptr<Parser::DDLStmt>(
3164  new Parser::AddColumnStmt(new std::string(tableName), table_element_list_))
3165  ->execute(*session_ptr_, read_only_mode);
3166  return {};
3167 
3168  } else if (type == "DROP_COLUMN") {
3169  CHECK(ddl_payload.HasMember("columnData"));
3170  auto columnData = json_str(ddl_payload["columnData"]);
3171  // Convert columnData to std::list<std::string*>*
3172  // allocate std::list<> as DropColumnStmt will delete it;
3173  std::list<std::string*>* cols = new std::list<std::string*>;
3174  std::vector<std::string> cols1;
3175  boost::split(cols1, columnData, boost::is_any_of(","));
3176  for (auto s : cols1) {
3177  // strip leading/trailing spaces/quotes/single quotes
3178  boost::algorithm::trim_if(s, boost::is_any_of(" \"'`"));
3179  std::string* str = new std::string(s);
3180  cols->emplace_back(str);
3181  }
3182 
3183  std::unique_ptr<Parser::DDLStmt>(
3184  new Parser::DropColumnStmt(new std::string(tableName), cols))
3185  ->execute(*session_ptr_, read_only_mode);
3186  return {};
3187 
3188  } else if (type == "ALTER_OPTIONS") {
3189  CHECK(ddl_payload.HasMember("options"));
3190  const auto& options = ddl_payload["options"];
3191  if (options.IsObject()) {
3192  for (auto itr = options.MemberBegin(); itr != options.MemberEnd(); ++itr) {
3193  std::string* option_name = new std::string(json_str(itr->name));
3194  Parser::Literal* literal_value;
3195  if (itr->value.IsString()) {
3196  std::string literal_string = json_str(itr->value);
3197 
3198  // iff this string can be converted to INT
3199  // ... do so because it is necessary for AlterTableParamStmt
3200  std::size_t sz;
3201  int iVal = std::stoi(literal_string, &sz);
3202  if (sz == literal_string.size()) {
3203  literal_value = new Parser::IntLiteral(iVal);
3204  } else {
3205  literal_value = new Parser::StringLiteral(&literal_string);
3206  }
3207  } else if (itr->value.IsInt() || itr->value.IsInt64()) {
3208  literal_value = new Parser::IntLiteral(json_i64(itr->value));
3209  } else if (itr->value.IsNull()) {
3210  literal_value = new Parser::NullLiteral();
3211  } else {
3212  throw std::runtime_error("Unable to handle literal for " + *option_name);
3213  }
3214  CHECK(literal_value);
3216  new Parser::NameValueAssign(option_name, literal_value);
3217  std::unique_ptr<Parser::DDLStmt>(
3218  new Parser::AlterTableParamStmt(new std::string(tableName), nv))
3219  ->execute(*session_ptr_, read_only_mode);
3220  return {};
3221  }
3222  } else {
3223  CHECK(options.IsNull());
3224  }
3225  }
3226 
3227  return ExecutionResult();
3228 }
#define LOG(tag)
Definition: Logger.h:285
const std::string json_str(const rapidjson::Value &obj) noexcept
Definition: JsonAccessors.h:46
std::vector< std::string > split(std::string_view str, std::string_view delim, std::optional< size_t > maxsplit)
split apart a string into a vector of substrings
const DdlCommandData & ddl_data_
const int64_t json_i64(const rapidjson::Value &obj) noexcept
Definition: JsonAccessors.h:41
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
ExecutionResult execute(bool read_only_mode) override
#define CHECK(condition)
Definition: Logger.h:291
std::unique_ptr< ColumnDef > column_from_json(const rapidjson::Value &element)
std::shared_ptr< Catalog_Namespace::SessionInfo const > session_ptr_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:


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