1#include "duckdb/parser/statement/insert_statement.hpp"
2#include "duckdb/parser/tableref/basetableref.hpp"
3#include "duckdb/parser/tableref/expressionlistref.hpp"
4#include "duckdb/parser/transformer.hpp"
5
6using namespace duckdb;
7using namespace std;
8
9unique_ptr<TableRef> Transformer::TransformValuesList(PGList *list) {
10 auto result = make_unique<ExpressionListRef>();
11 for (auto value_list = list->head; value_list != NULL; value_list = value_list->next) {
12 auto target = (PGList *)(value_list->data.ptr_value);
13
14 vector<unique_ptr<ParsedExpression>> insert_values;
15 if (!TransformExpressionList(target, insert_values)) {
16 throw ParserException("Could not parse expression list!");
17 }
18 if (result->values.size() > 0) {
19 if (result->values[0].size() != insert_values.size()) {
20 throw ParserException("VALUES lists must all be the same length");
21 }
22 }
23 result->values.push_back(move(insert_values));
24 }
25 result->alias = "valueslist";
26 return move(result);
27}
28
29unique_ptr<InsertStatement> Transformer::TransformInsert(PGNode *node) {
30 auto stmt = reinterpret_cast<PGInsertStmt *>(node);
31 assert(stmt);
32
33 auto result = make_unique<InsertStatement>();
34
35 // first check if there are any columns specified
36 if (stmt->cols) {
37 for (auto c = stmt->cols->head; c != NULL; c = lnext(c)) {
38 auto target = (PGResTarget *)(c->data.ptr_value);
39 result->columns.push_back(string(target->name));
40 }
41 }
42 result->select_statement = TransformSelect(stmt->selectStmt);
43
44 auto ref = TransformRangeVar(stmt->relation);
45 auto &table = *reinterpret_cast<BaseTableRef *>(ref.get());
46 result->table = table.table_name;
47 result->schema = table.schema_name;
48 return result;
49}
50