1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.command.ddl; |
7 | |
8 | import org.h2.command.CommandInterface; |
9 | import org.h2.engine.Database; |
10 | import org.h2.engine.DbObject; |
11 | import org.h2.engine.Right; |
12 | import org.h2.engine.Session; |
13 | import org.h2.expression.Expression; |
14 | import org.h2.table.Column; |
15 | import org.h2.table.Table; |
16 | |
17 | /** |
18 | * This class represents the statement |
19 | * ALTER TABLE ALTER COLUMN RENAME |
20 | */ |
21 | public class AlterTableRenameColumn extends DefineCommand { |
22 | |
23 | private Table table; |
24 | private Column column; |
25 | private String newName; |
26 | |
27 | public AlterTableRenameColumn(Session session) { |
28 | super(session); |
29 | } |
30 | |
31 | public void setTable(Table table) { |
32 | this.table = table; |
33 | } |
34 | |
35 | public void setColumn(Column column) { |
36 | this.column = column; |
37 | } |
38 | |
39 | public void setNewColumnName(String newName) { |
40 | this.newName = newName; |
41 | } |
42 | |
43 | @Override |
44 | public int update() { |
45 | session.commit(true); |
46 | Database db = session.getDatabase(); |
47 | session.getUser().checkRight(table, Right.ALL); |
48 | table.checkSupportAlter(); |
49 | // we need to update CHECK constraint |
50 | // since it might reference the name of the column |
51 | Expression newCheckExpr = column.getCheckConstraint(session, newName); |
52 | table.renameColumn(column, newName); |
53 | column.removeCheckConstraint(); |
54 | column.addCheckConstraint(session, newCheckExpr); |
55 | table.setModified(); |
56 | db.updateMeta(session, table); |
57 | for (DbObject child : table.getChildren()) { |
58 | if (child.getCreateSQL() != null) { |
59 | db.updateMeta(session, child); |
60 | } |
61 | } |
62 | return 0; |
63 | } |
64 | |
65 | @Override |
66 | public int getType() { |
67 | return CommandInterface.ALTER_TABLE_ALTER_COLUMN_RENAME; |
68 | } |
69 | |
70 | } |