| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://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.constraint.ConstraintReferential; |
| 10 |
import org.h2.engine.Database; |
| 11 |
import org.h2.engine.DbObject; |
| 12 |
import org.h2.engine.SessionLocal; |
| 13 |
import org.h2.schema.Schema; |
| 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 AlterTable { |
| 22 |
|
| 23 |
private boolean ifExists; |
| 24 |
private String oldName; |
| 25 |
private String newName; |
| 26 |
|
| 27 |
public AlterTableRenameColumn(SessionLocal session, Schema schema) { |
| 28 |
super(session, schema); |
| 29 |
} |
| 30 |
|
| 31 |
public void setIfExists(boolean b) { |
| 32 |
this.ifExists = b; |
| 33 |
} |
| 34 |
|
| 35 |
public void setOldColumnName(String oldName) { |
| 36 |
this.oldName = oldName; |
| 37 |
} |
| 38 |
|
| 39 |
public void setNewColumnName(String newName) { |
| 40 |
this.newName = newName; |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public long update(Table table) { |
| 45 |
Column column = table.getColumn(oldName, ifExists); |
| 46 |
if (column == null) { |
| 47 |
return 0; |
| 48 |
} |
| 49 |
table.checkSupportAlter(); |
| 50 |
table.renameColumn(column, newName); |
| 51 |
table.setModified(); |
| 52 |
Database db = getDatabase(); |
| 53 |
db.updateMeta(session, table); |
| 54 |
|
| 55 |
// if we have foreign key constraints pointing at this table, we need to update them |
| 56 |
for (DbObject childDbObject : table.getChildren()) { |
| 57 |
if (childDbObject instanceof ConstraintReferential) { |
| 58 |
ConstraintReferential ref = (ConstraintReferential) childDbObject; |
| 59 |
ref.updateOnTableColumnRename(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
for (DbObject child : table.getChildren()) { |
| 64 |
if (child.getCreateSQL() != null) { |
| 65 |
db.updateMeta(session, child); |
| 66 |
} |
| 67 |
} |
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public int getType() { |
| 73 |
return CommandInterface.ALTER_TABLE_ALTER_COLUMN_RENAME; |
| 74 |
} |
| 75 |
|
| 76 |
} |