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.engine; |
7 | |
8 | import java.util.ArrayList; |
9 | import org.h2.command.Parser; |
10 | import org.h2.message.Trace; |
11 | |
12 | /** |
13 | * The base class for all database objects. |
14 | */ |
15 | public abstract class DbObjectBase implements DbObject { |
16 | |
17 | /** |
18 | * The database. |
19 | */ |
20 | protected Database database; |
21 | |
22 | /** |
23 | * The trace module. |
24 | */ |
25 | protected Trace trace; |
26 | |
27 | /** |
28 | * The comment (if set). |
29 | */ |
30 | protected String comment; |
31 | |
32 | private int id; |
33 | private String objectName; |
34 | private long modificationId; |
35 | private boolean temporary; |
36 | |
37 | /** |
38 | * Initialize some attributes of this object. |
39 | * |
40 | * @param db the database |
41 | * @param objectId the object id |
42 | * @param name the name |
43 | * @param traceModule the trace module name |
44 | */ |
45 | protected void initDbObjectBase(Database db, int objectId, String name, |
46 | String traceModule) { |
47 | this.database = db; |
48 | this.trace = db.getTrace(traceModule); |
49 | this.id = objectId; |
50 | this.objectName = name; |
51 | this.modificationId = db.getModificationMetaId(); |
52 | } |
53 | |
54 | /** |
55 | * Build a SQL statement to re-create this object. |
56 | * |
57 | * @return the SQL statement |
58 | */ |
59 | @Override |
60 | public abstract String getCreateSQL(); |
61 | |
62 | /** |
63 | * Build a SQL statement to drop this object. |
64 | * |
65 | * @return the SQL statement |
66 | */ |
67 | @Override |
68 | public abstract String getDropSQL(); |
69 | |
70 | /** |
71 | * Remove all dependent objects and free all resources (files, blocks in |
72 | * files) of this object. |
73 | * |
74 | * @param session the session |
75 | */ |
76 | @Override |
77 | public abstract void removeChildrenAndResources(Session session); |
78 | |
79 | /** |
80 | * Check if this object can be renamed. System objects may not be renamed. |
81 | */ |
82 | @Override |
83 | public abstract void checkRename(); |
84 | |
85 | /** |
86 | * Tell the object that is was modified. |
87 | */ |
88 | public void setModified() { |
89 | this.modificationId = database == null ? |
90 | -1 : database.getNextModificationMetaId(); |
91 | } |
92 | |
93 | public long getModificationId() { |
94 | return modificationId; |
95 | } |
96 | |
97 | protected void setObjectName(String name) { |
98 | objectName = name; |
99 | } |
100 | |
101 | @Override |
102 | public String getSQL() { |
103 | return Parser.quoteIdentifier(objectName); |
104 | } |
105 | |
106 | @Override |
107 | public ArrayList<DbObject> getChildren() { |
108 | return null; |
109 | } |
110 | |
111 | @Override |
112 | public Database getDatabase() { |
113 | return database; |
114 | } |
115 | |
116 | @Override |
117 | public int getId() { |
118 | return id; |
119 | } |
120 | |
121 | @Override |
122 | public String getName() { |
123 | return objectName; |
124 | } |
125 | |
126 | /** |
127 | * Set the main attributes to null to make sure the object is no longer |
128 | * used. |
129 | */ |
130 | protected void invalidate() { |
131 | setModified(); |
132 | id = -1; |
133 | database = null; |
134 | trace = null; |
135 | objectName = null; |
136 | } |
137 | |
138 | @Override |
139 | public void rename(String newName) { |
140 | checkRename(); |
141 | objectName = newName; |
142 | setModified(); |
143 | } |
144 | |
145 | @Override |
146 | public boolean isTemporary() { |
147 | return temporary; |
148 | } |
149 | |
150 | @Override |
151 | public void setTemporary(boolean temporary) { |
152 | this.temporary = temporary; |
153 | } |
154 | |
155 | @Override |
156 | public void setComment(String comment) { |
157 | this.comment = comment; |
158 | } |
159 | |
160 | @Override |
161 | public String getComment() { |
162 | return comment; |
163 | } |
164 | |
165 | @Override |
166 | public String toString() { |
167 | return objectName + ":" + id + ":" + super.toString(); |
168 | } |
169 | |
170 | } |