| 1 | #include "duckdb/parser/statement/alter_statement.hpp" |
|---|---|
| 2 | #include "duckdb/parser/transformer.hpp" |
| 3 | |
| 4 | namespace duckdb { |
| 5 | |
| 6 | unique_ptr<AlterStatement> Transformer::TransformRename(duckdb_libpgquery::PGRenameStmt &stmt) { |
| 7 | if (!stmt.relation) { |
| 8 | throw NotImplementedException("Altering schemas is not yet supported"); |
| 9 | } |
| 10 | |
| 11 | unique_ptr<AlterInfo> info; |
| 12 | |
| 13 | AlterEntryData data; |
| 14 | data.if_not_found = TransformOnEntryNotFound(missing_ok: stmt.missing_ok); |
| 15 | data.catalog = stmt.relation->catalogname ? stmt.relation->catalogname : INVALID_CATALOG; |
| 16 | data.schema = stmt.relation->schemaname ? stmt.relation->schemaname : INVALID_SCHEMA; |
| 17 | if (stmt.relation->relname) { |
| 18 | data.name = stmt.relation->relname; |
| 19 | } |
| 20 | // first we check the type of ALTER |
| 21 | switch (stmt.renameType) { |
| 22 | case duckdb_libpgquery::PG_OBJECT_COLUMN: { |
| 23 | // change column name |
| 24 | |
| 25 | // get the old name and the new name |
| 26 | string old_name = stmt.subname; |
| 27 | string new_name = stmt.newname; |
| 28 | info = make_uniq<RenameColumnInfo>(args: std::move(data), args&: old_name, args&: new_name); |
| 29 | break; |
| 30 | } |
| 31 | case duckdb_libpgquery::PG_OBJECT_TABLE: { |
| 32 | // change table name |
| 33 | string new_name = stmt.newname; |
| 34 | info = make_uniq<RenameTableInfo>(args: std::move(data), args&: new_name); |
| 35 | break; |
| 36 | } |
| 37 | case duckdb_libpgquery::PG_OBJECT_VIEW: { |
| 38 | // change view name |
| 39 | string new_name = stmt.newname; |
| 40 | info = make_uniq<RenameViewInfo>(args: std::move(data), args&: new_name); |
| 41 | break; |
| 42 | } |
| 43 | case duckdb_libpgquery::PG_OBJECT_DATABASE: |
| 44 | default: |
| 45 | throw NotImplementedException("Schema element not supported yet!"); |
| 46 | } |
| 47 | D_ASSERT(info); |
| 48 | |
| 49 | auto result = make_uniq<AlterStatement>(); |
| 50 | result->info = std::move(info); |
| 51 | return result; |
| 52 | } |
| 53 | |
| 54 | } // namespace duckdb |
| 55 |