| 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.engine; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
|
| 10 |
import org.h2.command.Parser; |
| 11 |
import org.h2.message.DbException; |
| 12 |
import org.h2.message.Trace; |
| 13 |
import org.h2.table.Table; |
| 14 |
import org.h2.util.HasSQL; |
| 15 |
import org.h2.util.ParserUtil; |
| 16 |
|
| 17 |
/** |
| 18 |
* A database object such as a table, an index, or a user. |
| 19 |
*/ |
| 20 |
public abstract class DbObject implements HasSQL { |
| 21 |
|
| 22 |
/** |
| 23 |
* The object is of the type table or view. |
| 24 |
*/ |
| 25 |
public static final int TABLE_OR_VIEW = 0; |
| 26 |
|
| 27 |
/** |
| 28 |
* This object is an index. |
| 29 |
*/ |
| 30 |
public static final int INDEX = 1; |
| 31 |
|
| 32 |
/** |
| 33 |
* This object is a user. |
| 34 |
*/ |
| 35 |
public static final int USER = 2; |
| 36 |
|
| 37 |
/** |
| 38 |
* This object is a sequence. |
| 39 |
*/ |
| 40 |
public static final int SEQUENCE = 3; |
| 41 |
|
| 42 |
/** |
| 43 |
* This object is a trigger. |
| 44 |
*/ |
| 45 |
public static final int TRIGGER = 4; |
| 46 |
|
| 47 |
/** |
| 48 |
* This object is a constraint (check constraint, unique constraint, or |
| 49 |
* referential constraint). |
| 50 |
*/ |
| 51 |
public static final int CONSTRAINT = 5; |
| 52 |
|
| 53 |
/** |
| 54 |
* This object is a setting. |
| 55 |
*/ |
| 56 |
public static final int SETTING = 6; |
| 57 |
|
| 58 |
/** |
| 59 |
* This object is a role. |
| 60 |
*/ |
| 61 |
public static final int ROLE = 7; |
| 62 |
|
| 63 |
/** |
| 64 |
* This object is a right. |
| 65 |
*/ |
| 66 |
public static final int RIGHT = 8; |
| 67 |
|
| 68 |
/** |
| 69 |
* This object is an alias for a Java function. |
| 70 |
*/ |
| 71 |
public static final int FUNCTION_ALIAS = 9; |
| 72 |
|
| 73 |
/** |
| 74 |
* This object is a schema. |
| 75 |
*/ |
| 76 |
public static final int SCHEMA = 10; |
| 77 |
|
| 78 |
/** |
| 79 |
* This object is a constant. |
| 80 |
*/ |
| 81 |
public static final int CONSTANT = 11; |
| 82 |
|
| 83 |
/** |
| 84 |
* This object is a domain. |
| 85 |
*/ |
| 86 |
public static final int DOMAIN = 12; |
| 87 |
|
| 88 |
/** |
| 89 |
* This object is a comment. |
| 90 |
*/ |
| 91 |
public static final int COMMENT = 13; |
| 92 |
|
| 93 |
/** |
| 94 |
* This object is a user-defined aggregate function. |
| 95 |
*/ |
| 96 |
public static final int AGGREGATE = 14; |
| 97 |
|
| 98 |
/** |
| 99 |
* This object is a synonym. |
| 100 |
*/ |
| 101 |
public static final int SYNONYM = 15; |
| 102 |
|
| 103 |
/** |
| 104 |
* The database. |
| 105 |
*/ |
| 106 |
protected Database database; |
| 107 |
|
| 108 |
/** |
| 109 |
* The trace module. |
| 110 |
*/ |
| 111 |
protected Trace trace; |
| 112 |
|
| 113 |
/** |
| 114 |
* The comment (if set). |
| 115 |
*/ |
| 116 |
protected String comment; |
| 117 |
|
| 118 |
private int id; |
| 119 |
|
| 120 |
private String objectName; |
| 121 |
|
| 122 |
private long modificationId; |
| 123 |
|
| 124 |
private boolean temporary; |
| 125 |
|
| 126 |
/** |
| 127 |
* Initialize some attributes of this object. |
| 128 |
* |
| 129 |
* @param db the database |
| 130 |
* @param objectId the object id |
| 131 |
* @param name the name |
| 132 |
* @param traceModuleId the trace module id |
| 133 |
*/ |
| 134 |
protected DbObject(Database db, int objectId, String name, int traceModuleId) { |
| 135 |
this.database = db; |
| 136 |
this.trace = db.getTrace(traceModuleId); |
| 137 |
this.id = objectId; |
| 138 |
this.objectName = name; |
| 139 |
this.modificationId = db.getModificationMetaId(); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Tell the object that is was modified. |
| 144 |
*/ |
| 145 |
public final void setModified() { |
| 146 |
this.modificationId = database == null ? -1 : database.getNextModificationMetaId(); |
| 147 |
} |
| 148 |
|
| 149 |
public final long getModificationId() { |
| 150 |
return modificationId; |
| 151 |
} |
| 152 |
|
| 153 |
protected final void setObjectName(String name) { |
| 154 |
objectName = name; |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public String getSQL(int sqlFlags) { |
| 159 |
return Parser.quoteIdentifier(objectName, sqlFlags); |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 164 |
return ParserUtil.quoteIdentifier(builder, objectName, sqlFlags); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Get the list of dependent children (for tables, this includes indexes and |
| 169 |
* so on). |
| 170 |
* |
| 171 |
* @return the list of children, or {@code null} |
| 172 |
*/ |
| 173 |
public ArrayList<DbObject> getChildren() { |
| 174 |
return null; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Get the database. |
| 179 |
* |
| 180 |
* @return the database |
| 181 |
*/ |
| 182 |
public final Database getDatabase() { |
| 183 |
return database; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Get the unique object id. |
| 188 |
* |
| 189 |
* @return the object id |
| 190 |
*/ |
| 191 |
public final int getId() { |
| 192 |
return id; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Get the name. |
| 197 |
* |
| 198 |
* @return the name |
| 199 |
*/ |
| 200 |
public final String getName() { |
| 201 |
return objectName; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Set the main attributes to null to make sure the object is no longer |
| 206 |
* used. |
| 207 |
*/ |
| 208 |
protected void invalidate() { |
| 209 |
if (id == -1) { |
| 210 |
throw DbException.getInternalError(); |
| 211 |
} |
| 212 |
setModified(); |
| 213 |
id = -1; |
| 214 |
database = null; |
| 215 |
trace = null; |
| 216 |
objectName = null; |
| 217 |
} |
| 218 |
|
| 219 |
public final boolean isValid() { |
| 220 |
return id != -1; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Build a SQL statement to re-create the object, or to create a copy of the |
| 225 |
* object with a different name or referencing a different table |
| 226 |
* |
| 227 |
* @param table the new table |
| 228 |
* @param quotedName the quoted name |
| 229 |
* @return the SQL statement |
| 230 |
*/ |
| 231 |
public String getCreateSQLForCopy(Table table, String quotedName) { |
| 232 |
throw DbException.getInternalError(toString()); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Construct the CREATE ... SQL statement for this object for meta table. |
| 237 |
* |
| 238 |
* @return the SQL statement |
| 239 |
*/ |
| 240 |
public String getCreateSQLForMeta() { |
| 241 |
return getCreateSQL(); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Construct the CREATE ... SQL statement for this object. |
| 246 |
* |
| 247 |
* @return the SQL statement |
| 248 |
*/ |
| 249 |
public abstract String getCreateSQL(); |
| 250 |
|
| 251 |
/** |
| 252 |
* Construct a DROP ... SQL statement for this object. |
| 253 |
* |
| 254 |
* @return the SQL statement |
| 255 |
*/ |
| 256 |
public String getDropSQL() { |
| 257 |
return null; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Get the object type. |
| 262 |
* |
| 263 |
* @return the object type |
| 264 |
*/ |
| 265 |
public abstract int getType(); |
| 266 |
|
| 267 |
/** |
| 268 |
* Delete all dependent children objects and resources of this object. |
| 269 |
* |
| 270 |
* @param session the session |
| 271 |
*/ |
| 272 |
public abstract void removeChildrenAndResources(SessionLocal session); |
| 273 |
|
| 274 |
/** |
| 275 |
* Check if renaming is allowed. Does nothing when allowed. |
| 276 |
*/ |
| 277 |
public void checkRename() { |
| 278 |
// Allowed by default |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Rename the object. |
| 283 |
* |
| 284 |
* @param newName the new name |
| 285 |
*/ |
| 286 |
public void rename(String newName) { |
| 287 |
checkRename(); |
| 288 |
objectName = newName; |
| 289 |
setModified(); |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Check if this object is temporary (for example, a temporary table). |
| 294 |
* |
| 295 |
* @return true if is temporary |
| 296 |
*/ |
| 297 |
public boolean isTemporary() { |
| 298 |
return temporary; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Tell this object that it is temporary or not. |
| 303 |
* |
| 304 |
* @param temporary the new value |
| 305 |
*/ |
| 306 |
public void setTemporary(boolean temporary) { |
| 307 |
this.temporary = temporary; |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Change the comment of this object. |
| 312 |
* |
| 313 |
* @param comment the new comment, or null for no comment |
| 314 |
*/ |
| 315 |
public void setComment(String comment) { |
| 316 |
this.comment = comment != null && !comment.isEmpty() ? comment : null; |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Get the current comment of this object. |
| 321 |
* |
| 322 |
* @return the comment, or null if not set |
| 323 |
*/ |
| 324 |
public String getComment() { |
| 325 |
return comment; |
| 326 |
} |
| 327 |
|
| 328 |
@Override |
| 329 |
public String toString() { |
| 330 |
return objectName + ":" + id + ":" + super.toString(); |
| 331 |
} |
| 332 |
|
| 333 |
} |