1#include "duckdb/parser/column_definition.hpp"
2#include "duckdb/parser/constraint.hpp"
3#include "duckdb/parser/constraints/list.hpp"
4#include "duckdb/parser/transformer.hpp"
5
6using namespace duckdb;
7using namespace std;
8
9unique_ptr<Constraint> Transformer::TransformConstraint(PGListCell *cell) {
10 auto constraint = reinterpret_cast<PGConstraint *>(cell->data.ptr_value);
11 switch (constraint->contype) {
12 case PG_CONSTR_UNIQUE:
13 case PG_CONSTR_PRIMARY: {
14 bool is_primary_key = constraint->contype == PG_CONSTR_PRIMARY;
15 vector<string> columns;
16 for (auto kc = constraint->keys->head; kc; kc = kc->next) {
17 columns.push_back(string(reinterpret_cast<PGValue *>(kc->data.ptr_value)->val.str));
18 }
19 return make_unique<UniqueConstraint>(columns, is_primary_key);
20 }
21 default:
22 throw NotImplementedException("Constraint type not handled yet!");
23 }
24}
25
26unique_ptr<Constraint> Transformer::TransformConstraint(PGListCell *cell, ColumnDefinition &column, idx_t index) {
27 auto constraint = reinterpret_cast<PGConstraint *>(cell->data.ptr_value);
28 assert(constraint);
29 switch (constraint->contype) {
30 case PG_CONSTR_NOTNULL:
31 return make_unique<NotNullConstraint>(index);
32 case PG_CONSTR_CHECK: {
33 auto expression = TransformExpression(constraint->raw_expr);
34 if (expression->HasSubquery()) {
35 throw ParserException("subqueries prohibited in CHECK constraints");
36 }
37 if (expression->IsAggregate()) {
38 throw ParserException("aggregates prohibited in CHECK constraints");
39 }
40 return make_unique<CheckConstraint>(TransformExpression(constraint->raw_expr));
41 }
42 case PG_CONSTR_PRIMARY:
43 return make_unique<UniqueConstraint>(index, true);
44 case PG_CONSTR_UNIQUE:
45 return make_unique<UniqueConstraint>(index, false);
46 case PG_CONSTR_NULL:
47 return nullptr;
48 case PG_CONSTR_DEFAULT:
49 column.default_value = TransformExpression(constraint->raw_expr);
50 return nullptr;
51 case PG_CONSTR_FOREIGN:
52 default:
53 throw NotImplementedException("Constraint not implemented!");
54 }
55}
56