001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.engine;
007:
008: import java.sql.SQLException;
009:
010: import org.h2.command.Parser;
011: import org.h2.message.Message;
012: import org.h2.message.Trace;
013: import org.h2.table.Table;
014: import org.h2.util.ObjectArray;
015:
016: /**
017: * The base class for all database objects.
018: */
019: public abstract class DbObjectBase implements DbObject {
020:
021: /**
022: * The database.
023: */
024: protected Database database;
025:
026: /**
027: * The trace module.
028: */
029: protected Trace trace;
030:
031: /**
032: * The comment (if set).
033: */
034: protected String comment;
035:
036: private int id;
037: private String objectName;
038: private long modificationId;
039: private boolean temporary;
040:
041: /**
042: * Build a SQL statement to re-create the object, or to create a copy of the
043: * object with a different name or referencing a different table
044: *
045: * @param table
046: * the new table name
047: * @param quotedName
048: * the new quoted name
049: * @return the SQL statement
050: */
051: public abstract String getCreateSQLForCopy(Table table,
052: String quotedName);
053:
054: /**
055: * Build a SQL statement to re-create this object.
056: *
057: * @return the SQL statement
058: */
059: public abstract String getCreateSQL();
060:
061: /**
062: * Build a SQL statement to drop this object.
063: *
064: * @return the SQL statement
065: */
066: public abstract String getDropSQL();
067:
068: /**
069: * Get the object type.
070: *
071: * @return the object type
072: */
073: public abstract int getType();
074:
075: /**
076: * Remove all dependent objects and free all resources (files, blocks in
077: * files) of this object.
078: *
079: * @param session the session
080: */
081: public abstract void removeChildrenAndResources(Session session)
082: throws SQLException;
083:
084: /**
085: * Check if this object can be renamed. System objects may not be renamed.
086: */
087: public abstract void checkRename() throws SQLException;
088:
089: protected DbObjectBase(Database database, int id, String name,
090: String traceModule) {
091: this .database = database;
092: this .trace = database.getTrace(traceModule);
093: this .id = id;
094: this .objectName = name;
095: this .modificationId = database.getModificationMetaId();
096: }
097:
098: public void setModified() {
099: this .modificationId = database == null ? -1 : database
100: .getNextModificationMetaId();
101: }
102:
103: public long getModificationId() {
104: return modificationId;
105: }
106:
107: protected void setObjectName(String name) {
108: objectName = name;
109: }
110:
111: public String getSQL() {
112: return Parser.quoteIdentifier(objectName);
113: }
114:
115: public ObjectArray getChildren() {
116: return null;
117: }
118:
119: public Database getDatabase() {
120: return database;
121: }
122:
123: public int getId() {
124: return id;
125: }
126:
127: public String getName() {
128: return objectName;
129: }
130:
131: protected void invalidate() {
132: setModified();
133: id = -1;
134: database = null;
135: trace = null;
136: objectName = null;
137: }
138:
139: public int getHeadPos() {
140: return 0;
141: }
142:
143: public void rename(String newName) throws SQLException {
144: checkRename();
145: objectName = newName;
146: setModified();
147: }
148:
149: public boolean getTemporary() {
150: return temporary;
151: }
152:
153: public void setTemporary(boolean temporary) {
154: this .temporary = temporary;
155: }
156:
157: public void setComment(String comment) {
158: this .comment = comment;
159: }
160:
161: public String getComment() {
162: return comment;
163: }
164:
165: static int getCreateOrder(int type) {
166: switch (type) {
167: case SETTING:
168: return 0;
169: case USER:
170: return 1;
171: case SCHEMA:
172: return 2;
173: case USER_DATATYPE:
174: return 3;
175: case SEQUENCE:
176: return 4;
177: case CONSTANT:
178: return 5;
179: case FUNCTION_ALIAS:
180: return 6;
181: case TABLE_OR_VIEW:
182: return 7;
183: case INDEX:
184: return 8;
185: case CONSTRAINT:
186: return 9;
187: case TRIGGER:
188: return 10;
189: case ROLE:
190: return 11;
191: case RIGHT:
192: return 12;
193: case AGGREGATE:
194: return 13;
195: case COMMENT:
196: return 14;
197: default:
198: throw Message.getInternalError("type=" + type);
199: }
200: }
201:
202: }
|