File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\ddl\AlterTableRename.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.ddl;
8
9 import org.h2.command.CommandInterface;
10 import org.h2.constant.ErrorCode;
11 import org.h2.engine.Database;
12 import org.h2.engine.Right;
13 import org.h2.engine.Session;
14 import org.h2.message.DbException;
15 import org.h2.schema.Schema;
16 import org.h2.table.Table;
17
18 /**
19 * This class represents the statement
20 * ALTER TABLE RENAME
21 */
22 public class AlterTableRename extends SchemaCommand {
23
24 private Table oldTable;
25 private String newTableName;
26 private boolean hidden;
27
28 public AlterTableRename(Session session, Schema schema) {
29 super(session, schema);
30 }
31
32 public void setOldTable(Table table) {
33 oldTable = table;
34 }
35
36 public void setNewTableName(String name) {
37 newTableName = name;
38 }
39
40 public int update() {
41 session.commit(true);
42 Database db = session.getDatabase();
43 session.getUser().checkRight(oldTable, Right.ALL);
44 Table t = getSchema().findTableOrView(session, newTableName);
45 if (t != null && hidden && newTableName.equals(oldTable.getName())) {
46 if (!t.isHidden()) {
47 t.setHidden(hidden);
48 oldTable.setHidden(true);
49 db.update(session, oldTable);
50 }
51 return 0;
52 }
53 if (t != null || newTableName.equals(oldTable.getName())) {
54 throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, newTableName);
55 }
56 if (oldTable.isTemporary()) {
57 throw DbException.getUnsupportedException("temp table");
58 }
59 db.renameSchemaObject(session, oldTable, newTableName);
60 return 0;
61 }
62
63 public int getType() {
64 return CommandInterface.ALTER_TABLE_RENAME;
65 }
66
67 public void setHidden(boolean hidden) {
68 this.hidden = hidden;
69 }
70
71 }
72