1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/parser/statement/insert_statement.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/parser/parsed_expression.hpp"
12#include "duckdb/parser/statement/select_statement.hpp"
13#include "duckdb/parser/query_node.hpp"
14#include "duckdb/parser/statement/update_statement.hpp"
15
16namespace duckdb {
17class ExpressionListRef;
18class UpdateSetInfo;
19
20enum class OnConflictAction : uint8_t {
21 THROW,
22 NOTHING,
23 UPDATE,
24 REPLACE // Only used in transform/bind step, changed to UPDATE later
25};
26
27enum class InsertColumnOrder : uint8_t { INSERT_BY_POSITION = 0, INSERT_BY_NAME = 1 };
28
29class OnConflictInfo {
30public:
31 OnConflictInfo();
32
33public:
34 unique_ptr<OnConflictInfo> Copy() const;
35
36public:
37 OnConflictAction action_type;
38
39 vector<string> indexed_columns;
40 //! The SET information (if action_type == UPDATE)
41 unique_ptr<UpdateSetInfo> set_info;
42 //! The condition determining whether we apply the DO .. for conflicts that arise
43 unique_ptr<ParsedExpression> condition;
44
45protected:
46 OnConflictInfo(const OnConflictInfo &other);
47};
48
49class InsertStatement : public SQLStatement {
50public:
51 static constexpr const StatementType TYPE = StatementType::INSERT_STATEMENT;
52
53public:
54 InsertStatement();
55
56 //! The select statement to insert from
57 unique_ptr<SelectStatement> select_statement;
58 //! Column names to insert into
59 vector<string> columns;
60
61 //! Table name to insert to
62 string table;
63 //! Schema name to insert to
64 string schema;
65 //! The catalog name to insert to
66 string catalog;
67
68 //! keep track of optional returningList if statement contains a RETURNING keyword
69 vector<unique_ptr<ParsedExpression>> returning_list;
70
71 unique_ptr<OnConflictInfo> on_conflict_info;
72 unique_ptr<TableRef> table_ref;
73
74 //! CTEs
75 CommonTableExpressionMap cte_map;
76
77 //! Whether or not this a DEFAULT VALUES
78 bool default_values = false;
79
80 //! INSERT BY POSITION or INSERT BY NAME
81 InsertColumnOrder column_order = InsertColumnOrder::INSERT_BY_POSITION;
82
83protected:
84 InsertStatement(const InsertStatement &other);
85
86public:
87 static string OnConflictActionToString(OnConflictAction action);
88 string ToString() const override;
89 unique_ptr<SQLStatement> Copy() const override;
90
91 //! If the INSERT statement is inserted DIRECTLY from a values list (i.e. INSERT INTO tbl VALUES (...)) this returns
92 //! the expression list Otherwise, this returns NULL
93 optional_ptr<ExpressionListRef> GetValuesList() const;
94};
95
96} // namespace duckdb
97