Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::InsertStatement Class Reference
Inheritance diagram for duckdb::InsertStatement:
Collaboration diagram for duckdb::InsertStatement:

Public Member Functions

string ToString () const override
unique_ptr< SQLStatement > Copy () const override
 Create a copy of this SelectStatement.
optional_ptr< ExpressionListRef > GetValuesList () const
Public Member Functions inherited from duckdb::SQLStatement
 SQLStatement (StatementType type)
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const

Static Public Member Functions

static string OnConflictActionToString (OnConflictAction action)

Public Attributes

unique_ptr< SelectStatement > select_statement
 The select statement to insert from.
vector< string > columns
 Column names to insert into.
string table
 Table name to insert to.
string schema
 Schema name to insert to.
string catalog
 The catalog name to insert to.
vector< unique_ptr< ParsedExpression > > returning_list
 keep track of optional returningList if statement contains a RETURNING keyword
unique_ptr< OnConflictInfo > on_conflict_info
unique_ptr< TableRef > table_ref
CommonTableExpressionMap cte_map
 CTEs.
bool default_values = false
 Whether or not this a DEFAULT VALUES.
InsertColumnOrder column_order = InsertColumnOrder::INSERT_BY_POSITION
 INSERT BY POSITION or INSERT BY NAME.
Public Attributes inherited from duckdb::SQLStatement
StatementType type
 The statement type.
idx_t stmt_location = 0
 The statement location within the query string.
idx_t stmt_length = 0
 The statement length within the query string.
case_insensitive_map_t< idx_t > named_param_map
 The map of named parameter to param index.
string query
 The query text that corresponds to this SQL statement.

Static Public Attributes

static constexpr const StatementType TYPE = StatementType::INSERT_STATEMENT
Static Public Attributes inherited from duckdb::SQLStatement
static constexpr const StatementType TYPE = StatementType::INVALID_STATEMENT

Protected Member Functions

 InsertStatement (const InsertStatement &other)
Protected Member Functions inherited from duckdb::SQLStatement
 SQLStatement (const SQLStatement &other)=default

Constructor & Destructor Documentation

◆ InsertStatement() [1/2]

duckdb::InsertStatement::InsertStatement ( )
349350 : SQLStatement(StatementType::INSERT_STATEMENT), schema(DEFAULT_SCHEMA), catalog(INVALID_CATALOG) {
349351}
string schema
Schema name to insert to.
Definition duckdb.cpp:61338
string catalog
The catalog name to insert to.
Definition duckdb.cpp:61340

◆ InsertStatement() [2/2]

duckdb::InsertStatement::InsertStatement ( const InsertStatement & other)
protected
349354 : SQLStatement(other), select_statement(unique_ptr_cast<SQLStatement, SelectStatement>(
349355 other.select_statement ? other.select_statement->Copy() : nullptr)),
349356 columns(other.columns), table(other.table), schema(other.schema), catalog(other.catalog),
349357 default_values(other.default_values), column_order(other.column_order) {
349358 cte_map = other.cte_map.Copy();
349359 for (auto &expr : other.returning_list) {
349360 returning_list.emplace_back(expr->Copy());
349361 }
349362 if (other.table_ref) {
349363 table_ref = other.table_ref->Copy();
349364 }
349365 if (other.on_conflict_info) {
349366 on_conflict_info = other.on_conflict_info->Copy();
349367 }
349368}
vector< unique_ptr< ParsedExpression > > returning_list
keep track of optional returningList if statement contains a RETURNING keyword
Definition duckdb.cpp:61343
string table
Table name to insert to.
Definition duckdb.cpp:61336
CommonTableExpressionMap cte_map
CTEs.
Definition duckdb.cpp:61349
bool default_values
Whether or not this a DEFAULT VALUES.
Definition duckdb.cpp:61352
unique_ptr< SelectStatement > select_statement
The select statement to insert from.
Definition duckdb.cpp:61331
InsertColumnOrder column_order
INSERT BY POSITION or INSERT BY NAME.
Definition duckdb.cpp:61355
vector< string > columns
Column names to insert into.
Definition duckdb.cpp:61333

Member Function Documentation

◆ OnConflictActionToString()

string duckdb::InsertStatement::OnConflictActionToString ( OnConflictAction action)
static
349370 {
349371 switch (action) {
349372 case OnConflictAction::NOTHING:
349373 return "DO NOTHING";
349374 case OnConflictAction::REPLACE:
349375 case OnConflictAction::UPDATE:
349376 return "DO UPDATE";
349377 case OnConflictAction::THROW:
349378 // Explicitly left empty, for ToString purposes
349379 return "";
349380 default: {
349381 throw NotImplementedException("type not implemented for OnConflictActionType");
349382 }
349383 }
349384}

◆ ToString()

string duckdb::InsertStatement::ToString ( ) const
overridevirtual

Implements duckdb::SQLStatement.

349386 {
349387 bool or_replace_shorthand_set = false;
349388 string result;
349389
349390 result = cte_map.ToString();
349391 result += "INSERT";
349392 if (on_conflict_info && on_conflict_info->action_type == OnConflictAction::REPLACE) {
349393 or_replace_shorthand_set = true;
349394 result += " OR REPLACE";
349395 }
349396 result += " INTO ";
349397 if (!catalog.empty()) {
349399 }
349400 if (!schema.empty()) {
349402 }
349404 // Write the (optional) alias of the insert target
349405 if (table_ref && !table_ref->alias.empty()) {
349406 result += StringUtil::Format(" AS %s", KeywordHelper::WriteOptionallyQuoted(table_ref->alias));
349407 }
349408 if (column_order == InsertColumnOrder::INSERT_BY_NAME) {
349409 result += " BY NAME";
349410 }
349411 if (!columns.empty()) {
349412 result += " (";
349413 for (idx_t i = 0; i < columns.size(); i++) {
349414 if (i > 0) {
349415 result += ", ";
349416 }
349418 }
349419 result += " )";
349420 }
349421 result += " ";
349422 auto values_list = GetValuesList();
349423 if (values_list) {
349424 D_ASSERT(!default_values);
349425 auto saved_alias = values_list->alias;
349426 values_list->alias = string();
349427 result += values_list->ToString();
349428 values_list->alias = saved_alias;
349429 } else if (select_statement) {
349430 D_ASSERT(!default_values);
349431 result += select_statement->ToString();
349432 } else {
349433 D_ASSERT(default_values);
349434 result += "DEFAULT VALUES";
349435 }
349436 if (!or_replace_shorthand_set && on_conflict_info) {
349437 auto &conflict_info = *on_conflict_info;
349438 result += " ON CONFLICT ";
349439 // (optional) conflict target
349440 if (!conflict_info.indexed_columns.empty()) {
349441 result += "(";
349442 auto &columns = conflict_info.indexed_columns;
349443 for (auto it = columns.begin(); it != columns.end();) {
349444 result += StringUtil::Lower(*it);
349445 if (++it != columns.end()) {
349446 result += ", ";
349447 }
349448 }
349449 result += " )";
349450 }
349451
349452 // (optional) where clause
349453 if (conflict_info.condition) {
349454 result += " WHERE " + conflict_info.condition->ToString();
349455 }
349456 result += " " + OnConflictActionToString(conflict_info.action_type);
349457 if (conflict_info.set_info) {
349458 D_ASSERT(conflict_info.action_type == OnConflictAction::UPDATE);
349459 result += " SET ";
349460 auto &set_info = *conflict_info.set_info;
349461 D_ASSERT(set_info.columns.size() == set_info.expressions.size());
349462 // SET <column_name> = <expression>
349463 for (idx_t i = 0; i < set_info.columns.size(); i++) {
349464 auto &column = set_info.columns[i];
349465 auto &expr = set_info.expressions[i];
349466 if (i) {
349467 result += ", ";
349468 }
349469 result += StringUtil::Lower(column) + " = " + expr->ToString();
349470 }
349471 // (optional) where clause
349472 if (set_info.condition) {
349473 result += " WHERE " + set_info.condition->ToString();
349474 }
349475 }
349476 }
349477 if (!returning_list.empty()) {
349478 result += " RETURNING ";
349479 for (idx_t i = 0; i < returning_list.size(); i++) {
349480 if (i > 0) {
349481 result += ", ";
349482 }
349483 auto column = returning_list[i]->ToString();
349484 if (!returning_list[i]->GetAlias().empty()) {
349485 column +=
349487 }
349488 result += column;
349489 }
349490 }
349491 return result;
349492}
optional_ptr< ExpressionListRef > GetValuesList() const
Definition duckdb.cpp:349498
static string WriteOptionallyQuoted(const string &text, char quote='"', bool allow_caps = true, KeywordCategory category = KeywordCategory::KEYWORD_NONE)
Writes a string that is optionally quoted + escaped so it can be used as an identifier.
Definition duckdb.cpp:338034
static DUCKDB_API string Lower(const string &str)
Convert a string to lowercase.
Definition duckdb.cpp:109630
static string Format(const string fmt_str, ARGS... params)
Format a string using printf semantics.
Definition duckdb.hpp:4002
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ Copy()

unique_ptr< SQLStatement > duckdb::InsertStatement::Copy ( ) const
overridevirtual

Create a copy of this SelectStatement.

Implements duckdb::SQLStatement.

349494 {
349495 return unique_ptr<InsertStatement>(new InsertStatement(*this));
349496}

◆ GetValuesList()

optional_ptr< ExpressionListRef > duckdb::InsertStatement::GetValuesList ( ) const

If the INSERT statement is inserted DIRECTLY from a values list (i.e. INSERT INTO tbl VALUES (...)) this returns the expression list Otherwise, this returns NULL

349498 {
349499 if (!select_statement) {
349500 return nullptr;
349501 }
349502 if (select_statement->node->type != QueryNodeType::SELECT_NODE) {
349503 return nullptr;
349504 }
349505 auto &node = select_statement->node->Cast<SelectNode>();
349506 if (node.where_clause || node.qualify || node.having) {
349507 return nullptr;
349508 }
349509 if (!node.cte_map.map.empty()) {
349510 return nullptr;
349511 }
349512 if (!node.groups.grouping_sets.empty()) {
349513 return nullptr;
349514 }
349515 if (node.aggregate_handling != AggregateHandling::STANDARD_HANDLING) {
349516 return nullptr;
349517 }
349518 if (node.select_list.size() != 1 || node.select_list[0]->GetExpressionType() != ExpressionType::STAR) {
349519 return nullptr;
349520 }
349521 if (!node.from_table || node.from_table->type != TableReferenceType::EXPRESSION_LIST) {
349522 return nullptr;
349523 }
349524 return &node.from_table->Cast<ExpressionListRef>();
349525}

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