1 | #include "duckdb/parser/statement/alter_table_statement.hpp" |
---|---|
2 | #include "duckdb/parser/transformer.hpp" |
3 | |
4 | using namespace duckdb; |
5 | using namespace std; |
6 | |
7 | unique_ptr<AlterTableStatement> Transformer::TransformRename(PGNode *node) { |
8 | auto stmt = reinterpret_cast<PGRenameStmt *>(node); |
9 | assert(stmt); |
10 | assert(stmt->relation); |
11 | |
12 | unique_ptr<AlterTableInfo> info; |
13 | |
14 | // first we check the type of ALTER |
15 | switch (stmt->renameType) { |
16 | case PG_OBJECT_COLUMN: { |
17 | // change column name |
18 | |
19 | // get the table and schema |
20 | string schema = INVALID_SCHEMA; |
21 | string table; |
22 | assert(stmt->relation->relname); |
23 | if (stmt->relation->relname) { |
24 | table = stmt->relation->relname; |
25 | } |
26 | if (stmt->relation->schemaname) { |
27 | schema = stmt->relation->schemaname; |
28 | } |
29 | // get the old name and the new name |
30 | string old_name = stmt->subname; |
31 | string new_name = stmt->newname; |
32 | info = make_unique<RenameColumnInfo>(schema, table, old_name, new_name); |
33 | break; |
34 | } |
35 | case PG_OBJECT_TABLE: { |
36 | // change table name |
37 | |
38 | // get the table and schema |
39 | string schema = DEFAULT_SCHEMA; |
40 | string table; |
41 | assert(stmt->relation->relname); |
42 | if (stmt->relation->relname) { |
43 | table = stmt->relation->relname; |
44 | } |
45 | if (stmt->relation->schemaname) { |
46 | schema = stmt->relation->schemaname; |
47 | } |
48 | string new_name = stmt->newname; |
49 | info = make_unique<RenameTableInfo>(schema, table, new_name); |
50 | break; |
51 | } |
52 | |
53 | case PG_OBJECT_DATABASE: |
54 | default: |
55 | throw NotImplementedException("Schema element not supported yet!"); |
56 | } |
57 | assert(info); |
58 | return make_unique<AlterTableStatement>(move(info)); |
59 | } |
60 |