| 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.schema; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.Collection; |
| 10 |
import java.util.HashSet; |
| 11 |
import java.util.Map; |
| 12 |
import java.util.concurrent.ConcurrentHashMap; |
| 13 |
|
| 14 |
import org.h2.api.ErrorCode; |
| 15 |
import org.h2.command.ddl.CreateSynonymData; |
| 16 |
import org.h2.command.ddl.CreateTableData; |
| 17 |
import org.h2.constraint.Constraint; |
| 18 |
import org.h2.engine.Database; |
| 19 |
import org.h2.engine.DbObject; |
| 20 |
import org.h2.engine.DbSettings; |
| 21 |
import org.h2.engine.Right; |
| 22 |
import org.h2.engine.RightOwner; |
| 23 |
import org.h2.engine.SessionLocal; |
| 24 |
import org.h2.engine.SysProperties; |
| 25 |
import org.h2.index.Index; |
| 26 |
import org.h2.message.DbException; |
| 27 |
import org.h2.message.Trace; |
| 28 |
import org.h2.table.MaterializedView; |
| 29 |
import org.h2.table.MetaTable; |
| 30 |
import org.h2.table.Table; |
| 31 |
import org.h2.table.TableLink; |
| 32 |
import org.h2.table.TableSynonym; |
| 33 |
import org.h2.util.Utils; |
| 34 |
|
| 35 |
/** |
| 36 |
* A schema as created by the SQL statement |
| 37 |
* CREATE SCHEMA |
| 38 |
*/ |
| 39 |
public class Schema extends DbObject { |
| 40 |
|
| 41 |
private RightOwner owner; |
| 42 |
private final boolean system; |
| 43 |
private ArrayList<String> tableEngineParams; |
| 44 |
|
| 45 |
private final ConcurrentHashMap<String, Table> tablesAndViews; |
| 46 |
private final ConcurrentHashMap<String, Domain> domains; |
| 47 |
private final ConcurrentHashMap<String, TableSynonym> synonyms; |
| 48 |
private final ConcurrentHashMap<String, Index> indexes; |
| 49 |
private final ConcurrentHashMap<String, Sequence> sequences; |
| 50 |
private final ConcurrentHashMap<String, TriggerObject> triggers; |
| 51 |
private final ConcurrentHashMap<String, Constraint> constraints; |
| 52 |
private final ConcurrentHashMap<String, Constant> constants; |
| 53 |
private final ConcurrentHashMap<String, UserDefinedFunction> functionsAndAggregates; |
| 54 |
|
| 55 |
/** |
| 56 |
* The set of returned unique names that are not yet stored. It is used to |
| 57 |
* avoid returning the same unique name twice when multiple threads |
| 58 |
* concurrently create objects. |
| 59 |
*/ |
| 60 |
private final HashSet<String> temporaryUniqueNames = new HashSet<>(); |
| 61 |
|
| 62 |
/** |
| 63 |
* Create a new schema object. |
| 64 |
* |
| 65 |
* @param database the database |
| 66 |
* @param id the object id |
| 67 |
* @param schemaName the schema name |
| 68 |
* @param owner the owner of the schema |
| 69 |
* @param system if this is a system schema (such a schema can not be |
| 70 |
* dropped) |
| 71 |
*/ |
| 72 |
public Schema(Database database, int id, String schemaName, RightOwner owner, boolean system) { |
| 73 |
super(database, id, schemaName, Trace.SCHEMA); |
| 74 |
tablesAndViews = database.newConcurrentStringMap(); |
| 75 |
domains = database.newConcurrentStringMap(); |
| 76 |
synonyms = database.newConcurrentStringMap(); |
| 77 |
indexes = database.newConcurrentStringMap(); |
| 78 |
sequences = database.newConcurrentStringMap(); |
| 79 |
triggers = database.newConcurrentStringMap(); |
| 80 |
constraints = database.newConcurrentStringMap(); |
| 81 |
constants = database.newConcurrentStringMap(); |
| 82 |
functionsAndAggregates = database.newConcurrentStringMap(); |
| 83 |
this.owner = owner; |
| 84 |
this.system = system; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Check if this schema can be dropped. System schemas can not be dropped. |
| 89 |
* |
| 90 |
* @return true if it can be dropped |
| 91 |
*/ |
| 92 |
public boolean canDrop() { |
| 93 |
return !system; |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public String getCreateSQL() { |
| 98 |
if (system) { |
| 99 |
return null; |
| 100 |
} |
| 101 |
StringBuilder builder = new StringBuilder("CREATE SCHEMA IF NOT EXISTS "); |
| 102 |
getSQL(builder, DEFAULT_SQL_FLAGS).append(" AUTHORIZATION "); |
| 103 |
owner.getSQL(builder, DEFAULT_SQL_FLAGS); |
| 104 |
return builder.toString(); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public int getType() { |
| 109 |
return DbObject.SCHEMA; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Return whether is this schema is empty (does not contain any objects). |
| 114 |
* |
| 115 |
* @return {@code true} if this schema is empty, {@code false} otherwise |
| 116 |
*/ |
| 117 |
public boolean isEmpty() { |
| 118 |
return tablesAndViews.isEmpty() && domains.isEmpty() && synonyms.isEmpty() && indexes.isEmpty() |
| 119 |
&& sequences.isEmpty() && triggers.isEmpty() && constraints.isEmpty() && constants.isEmpty() |
| 120 |
&& functionsAndAggregates.isEmpty(); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public ArrayList<DbObject> getChildren() { |
| 125 |
ArrayList<DbObject> children = Utils.newSmallArrayList(); |
| 126 |
ArrayList<Right> rights = database.getAllRights(); |
| 127 |
for (Right right : rights) { |
| 128 |
if (right.getGrantedObject() == this) { |
| 129 |
children.add(right); |
| 130 |
} |
| 131 |
} |
| 132 |
return children; |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public void removeChildrenAndResources(SessionLocal session) { |
| 137 |
removeChildrenFromMap(session, triggers); |
| 138 |
removeChildrenFromMap(session, constraints); |
| 139 |
// There can be dependencies between tables e.g. using computed columns, |
| 140 |
// so we might need to loop over them multiple times. |
| 141 |
boolean modified = true; |
| 142 |
while (!tablesAndViews.isEmpty()) { |
| 143 |
boolean newModified = false; |
| 144 |
for (Table obj : tablesAndViews.values()) { |
| 145 |
if (obj.getName() != null) { |
| 146 |
// Database.removeSchemaObject() removes the object from |
| 147 |
// the map too, but it is safe for ConcurrentHashMap. |
| 148 |
Table dependentTable = database.getDependentTable(obj, obj); |
| 149 |
if (dependentTable == null) { |
| 150 |
database.removeSchemaObject(session, obj); |
| 151 |
newModified = true; |
| 152 |
} else if (dependentTable.getSchema() != this) { |
| 153 |
throw DbException.get(ErrorCode.CANNOT_DROP_2, // |
| 154 |
obj.getTraceSQL(), dependentTable.getTraceSQL()); |
| 155 |
} else if (!modified) { |
| 156 |
dependentTable.removeColumnExpressionsDependencies(session); |
| 157 |
dependentTable.setModified(); |
| 158 |
database.updateMeta(session, dependentTable); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
modified = newModified; |
| 163 |
} |
| 164 |
removeChildrenFromMap(session, domains); |
| 165 |
removeChildrenFromMap(session, indexes); |
| 166 |
removeChildrenFromMap(session, sequences); |
| 167 |
removeChildrenFromMap(session, constants); |
| 168 |
removeChildrenFromMap(session, functionsAndAggregates); |
| 169 |
for (Right right : database.getAllRights()) { |
| 170 |
if (right.getGrantedObject() == this) { |
| 171 |
database.removeDatabaseObject(session, right); |
| 172 |
} |
| 173 |
} |
| 174 |
database.removeMeta(session, getId()); |
| 175 |
owner = null; |
| 176 |
invalidate(); |
| 177 |
} |
| 178 |
|
| 179 |
private void removeChildrenFromMap(SessionLocal session, ConcurrentHashMap<String, ? extends SchemaObject> map) { |
| 180 |
if (!map.isEmpty()) { |
| 181 |
for (SchemaObject obj : map.values()) { |
| 182 |
/* |
| 183 |
* Referential constraints are dropped when unique or PK |
| 184 |
* constraint is dropped, but iterator may return already |
| 185 |
* removed objects in some cases. |
| 186 |
*/ |
| 187 |
if (obj.isValid()) { |
| 188 |
// Database.removeSchemaObject() removes the object from |
| 189 |
// the map too, but it is safe for ConcurrentHashMap. |
| 190 |
database.removeSchemaObject(session, obj); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Get the owner of this schema. |
| 198 |
* |
| 199 |
* @return the owner |
| 200 |
*/ |
| 201 |
public RightOwner getOwner() { |
| 202 |
return owner; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Get table engine params of this schema. |
| 207 |
* |
| 208 |
* @return default table engine params |
| 209 |
*/ |
| 210 |
public ArrayList<String> getTableEngineParams() { |
| 211 |
return tableEngineParams; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Set table engine params of this schema. |
| 216 |
* @param tableEngineParams default table engine params |
| 217 |
*/ |
| 218 |
public void setTableEngineParams(ArrayList<String> tableEngineParams) { |
| 219 |
this.tableEngineParams = tableEngineParams; |
| 220 |
} |
| 221 |
|
| 222 |
@SuppressWarnings("unchecked") |
| 223 |
private Map<String, SchemaObject> getMap(int type) { |
| 224 |
Map<String, ? extends SchemaObject> result; |
| 225 |
switch (type) { |
| 226 |
case DbObject.TABLE_OR_VIEW: |
| 227 |
result = tablesAndViews; |
| 228 |
break; |
| 229 |
case DbObject.DOMAIN: |
| 230 |
result = domains; |
| 231 |
break; |
| 232 |
case DbObject.SYNONYM: |
| 233 |
result = synonyms; |
| 234 |
break; |
| 235 |
case DbObject.SEQUENCE: |
| 236 |
result = sequences; |
| 237 |
break; |
| 238 |
case DbObject.INDEX: |
| 239 |
result = indexes; |
| 240 |
break; |
| 241 |
case DbObject.TRIGGER: |
| 242 |
result = triggers; |
| 243 |
break; |
| 244 |
case DbObject.CONSTRAINT: |
| 245 |
result = constraints; |
| 246 |
break; |
| 247 |
case DbObject.CONSTANT: |
| 248 |
result = constants; |
| 249 |
break; |
| 250 |
case DbObject.FUNCTION_ALIAS: |
| 251 |
case DbObject.AGGREGATE: |
| 252 |
result = functionsAndAggregates; |
| 253 |
break; |
| 254 |
default: |
| 255 |
throw DbException.getInternalError("type=" + type); |
| 256 |
} |
| 257 |
return (Map<String, SchemaObject>) result; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Add an object to this schema. |
| 262 |
* This method must not be called within CreateSchemaObject; |
| 263 |
* use Database.addSchemaObject() instead |
| 264 |
* |
| 265 |
* @param obj the object to add |
| 266 |
*/ |
| 267 |
public void add(SchemaObject obj) { |
| 268 |
if (obj.getSchema() != this) { |
| 269 |
throw DbException.getInternalError("wrong schema"); |
| 270 |
} |
| 271 |
String name = obj.getName(); |
| 272 |
Map<String, SchemaObject> map = getMap(obj.getType()); |
| 273 |
if (map.putIfAbsent(name, obj) != null) { |
| 274 |
throw DbException.getInternalError("object already exists: " + name); |
| 275 |
} |
| 276 |
freeUniqueName(name); |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Rename an object. |
| 281 |
* |
| 282 |
* @param obj the object to rename |
| 283 |
* @param newName the new name |
| 284 |
*/ |
| 285 |
public void rename(SchemaObject obj, String newName) { |
| 286 |
int type = obj.getType(); |
| 287 |
Map<String, SchemaObject> map = getMap(type); |
| 288 |
if (SysProperties.CHECK) { |
| 289 |
if (!map.containsKey(obj.getName()) && !(obj instanceof MetaTable)) { |
| 290 |
throw DbException.getInternalError("not found: " + obj.getName()); |
| 291 |
} |
| 292 |
if (obj.getName().equals(newName) || map.containsKey(newName)) { |
| 293 |
throw DbException.getInternalError("object already exists: " + newName); |
| 294 |
} |
| 295 |
} |
| 296 |
obj.checkRename(); |
| 297 |
map.remove(obj.getName()); |
| 298 |
freeUniqueName(obj.getName()); |
| 299 |
obj.rename(newName); |
| 300 |
map.put(newName, obj); |
| 301 |
freeUniqueName(newName); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Try to find a table or view with this name. This method returns null if |
| 306 |
* no object with this name exists. Local temporary tables are also |
| 307 |
* returned. Synonyms are not returned or resolved. |
| 308 |
* |
| 309 |
* @param session the session |
| 310 |
* @param name the object name |
| 311 |
* @return the object or null |
| 312 |
*/ |
| 313 |
public Table findTableOrView(SessionLocal session, String name) { |
| 314 |
Table table = tablesAndViews.get(name); |
| 315 |
if (table == null && session != null) { |
| 316 |
table = session.findLocalTempTable(name); |
| 317 |
} |
| 318 |
return table; |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Try to find a table or view with this name. This method returns null if |
| 323 |
* no object with this name exists. Local temporary tables are also |
| 324 |
* returned. If a synonym with this name exists, the backing table of the |
| 325 |
* synonym is returned |
| 326 |
* |
| 327 |
* @param session the session |
| 328 |
* @param name the object name |
| 329 |
* @return the object or null |
| 330 |
*/ |
| 331 |
public Table resolveTableOrView(SessionLocal session, String name) { |
| 332 |
return resolveTableOrView(session, name, /*resolveMaterializedView*/true); |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Try to find a table or view with this name. This method returns null if |
| 337 |
* no object with this name exists. Local temporary tables are also |
| 338 |
* returned. If a synonym with this name exists, the backing table of the |
| 339 |
* synonym is returned |
| 340 |
* |
| 341 |
* @param session the session |
| 342 |
* @param name the object name |
| 343 |
* @param resolveMaterializedView if true, and the object is a materialized |
| 344 |
* view, return the underlying Table object. |
| 345 |
* @return the object or null |
| 346 |
*/ |
| 347 |
public Table resolveTableOrView(SessionLocal session, String name, boolean resolveMaterializedView) { |
| 348 |
Table table = findTableOrView(session, name); |
| 349 |
if (table == null) { |
| 350 |
TableSynonym synonym = synonyms.get(name); |
| 351 |
if (synonym != null) { |
| 352 |
return synonym.getSynonymFor(); |
| 353 |
} |
| 354 |
} |
| 355 |
if (resolveMaterializedView && table instanceof MaterializedView) { |
| 356 |
MaterializedView matView = (MaterializedView) table; |
| 357 |
return matView.getUnderlyingTable(); |
| 358 |
} |
| 359 |
return table; |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Try to find a synonym with this name. This method returns null if |
| 364 |
* no object with this name exists. |
| 365 |
* |
| 366 |
* @param name the object name |
| 367 |
* @return the object or null |
| 368 |
*/ |
| 369 |
public TableSynonym getSynonym(String name) { |
| 370 |
return synonyms.get(name); |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Get the domain if it exists, or null if not. |
| 375 |
* |
| 376 |
* @param name the name of the domain |
| 377 |
* @return the domain or null |
| 378 |
*/ |
| 379 |
public Domain findDomain(String name) { |
| 380 |
return domains.get(name); |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Try to find an index with this name. This method returns null if |
| 385 |
* no object with this name exists. |
| 386 |
* |
| 387 |
* @param session the session |
| 388 |
* @param name the object name |
| 389 |
* @return the object or null |
| 390 |
*/ |
| 391 |
public Index findIndex(SessionLocal session, String name) { |
| 392 |
Index index = indexes.get(name); |
| 393 |
if (index == null) { |
| 394 |
index = session.findLocalTempTableIndex(name); |
| 395 |
} |
| 396 |
return index; |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Try to find a trigger with this name. This method returns null if |
| 401 |
* no object with this name exists. |
| 402 |
* |
| 403 |
* @param name the object name |
| 404 |
* @return the object or null |
| 405 |
*/ |
| 406 |
public TriggerObject findTrigger(String name) { |
| 407 |
return triggers.get(name); |
| 408 |
} |
| 409 |
|
| 410 |
/** |
| 411 |
* Try to find a sequence with this name. This method returns null if |
| 412 |
* no object with this name exists. |
| 413 |
* |
| 414 |
* @param sequenceName the object name |
| 415 |
* @return the object or null |
| 416 |
*/ |
| 417 |
public Sequence findSequence(String sequenceName) { |
| 418 |
return sequences.get(sequenceName); |
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* Try to find a constraint with this name. This method returns null if no |
| 423 |
* object with this name exists. |
| 424 |
* |
| 425 |
* @param session the session |
| 426 |
* @param name the object name |
| 427 |
* @return the object or null |
| 428 |
*/ |
| 429 |
public Constraint findConstraint(SessionLocal session, String name) { |
| 430 |
Constraint constraint = constraints.get(name); |
| 431 |
if (constraint == null) { |
| 432 |
constraint = session.findLocalTempTableConstraint(name); |
| 433 |
} |
| 434 |
return constraint; |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* Try to find a user defined constant with this name. This method returns |
| 439 |
* null if no object with this name exists. |
| 440 |
* |
| 441 |
* @param constantName the object name |
| 442 |
* @return the object or null |
| 443 |
*/ |
| 444 |
public Constant findConstant(String constantName) { |
| 445 |
return constants.get(constantName); |
| 446 |
} |
| 447 |
|
| 448 |
/** |
| 449 |
* Try to find a user defined function with this name. This method returns |
| 450 |
* null if no object with this name exists. |
| 451 |
* |
| 452 |
* @param functionAlias the object name |
| 453 |
* @return the object or null |
| 454 |
*/ |
| 455 |
public FunctionAlias findFunction(String functionAlias) { |
| 456 |
UserDefinedFunction userDefinedFunction = findFunctionOrAggregate(functionAlias); |
| 457 |
return userDefinedFunction instanceof FunctionAlias ? (FunctionAlias) userDefinedFunction : null; |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* Get the user defined aggregate function if it exists. This method returns |
| 462 |
* null if no object with this name exists. |
| 463 |
* |
| 464 |
* @param name the name of the user defined aggregate function |
| 465 |
* @return the aggregate function or null |
| 466 |
*/ |
| 467 |
public UserAggregate findAggregate(String name) { |
| 468 |
UserDefinedFunction userDefinedFunction = findFunctionOrAggregate(name); |
| 469 |
return userDefinedFunction instanceof UserAggregate ? (UserAggregate) userDefinedFunction : null; |
| 470 |
} |
| 471 |
|
| 472 |
/** |
| 473 |
* Try to find a user defined function or aggregate function with the |
| 474 |
* specified name. This method returns null if no object with this name |
| 475 |
* exists. |
| 476 |
* |
| 477 |
* @param name |
| 478 |
* the object name |
| 479 |
* @return the object or null |
| 480 |
*/ |
| 481 |
public UserDefinedFunction findFunctionOrAggregate(String name) { |
| 482 |
return functionsAndAggregates.get(name); |
| 483 |
} |
| 484 |
|
| 485 |
/** |
| 486 |
* Reserve a unique object name. |
| 487 |
* |
| 488 |
* @param name the object name |
| 489 |
*/ |
| 490 |
public void reserveUniqueName(String name) { |
| 491 |
if (name != null) { |
| 492 |
synchronized (temporaryUniqueNames) { |
| 493 |
temporaryUniqueNames.add(name); |
| 494 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
/** |
| 499 |
* Release a unique object name. |
| 500 |
* |
| 501 |
* @param name the object name |
| 502 |
*/ |
| 503 |
public void freeUniqueName(String name) { |
| 504 |
if (name != null) { |
| 505 |
synchronized (temporaryUniqueNames) { |
| 506 |
temporaryUniqueNames.remove(name); |
| 507 |
} |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
private String getUniqueName(DbObject obj, Map<String, ? extends SchemaObject> map, String prefix) { |
| 512 |
StringBuilder nameBuilder = new StringBuilder(prefix); |
| 513 |
String hash = Integer.toHexString(obj.getName().hashCode()); |
| 514 |
synchronized (temporaryUniqueNames) { |
| 515 |
for (int i = 0, len = hash.length(); i < len; i++) { |
| 516 |
char c = hash.charAt(i); |
| 517 |
String name = nameBuilder.append(c >= 'a' ? (char) (c - 0x20) : c).toString(); |
| 518 |
if (!map.containsKey(name) && temporaryUniqueNames.add(name)) { |
| 519 |
return name; |
| 520 |
} |
| 521 |
} |
| 522 |
int nameLength = nameBuilder.append('_').length(); |
| 523 |
for (int i = 0;; i++) { |
| 524 |
String name = nameBuilder.append(i).toString(); |
| 525 |
if (!map.containsKey(name) && temporaryUniqueNames.add(name)) { |
| 526 |
return name; |
| 527 |
} |
| 528 |
nameBuilder.setLength(nameLength); |
| 529 |
} |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
/** |
| 534 |
* Create a unique constraint name. |
| 535 |
* |
| 536 |
* @param session the session |
| 537 |
* @param table the constraint table |
| 538 |
* @return the unique name |
| 539 |
*/ |
| 540 |
public String getUniqueConstraintName(SessionLocal session, Table table) { |
| 541 |
Map<String, Constraint> tableConstraints; |
| 542 |
if (table.isTemporary() && !table.isGlobalTemporary()) { |
| 543 |
tableConstraints = session.getLocalTempTableConstraints(); |
| 544 |
} else { |
| 545 |
tableConstraints = constraints; |
| 546 |
} |
| 547 |
return getUniqueName(table, tableConstraints, "CONSTRAINT_"); |
| 548 |
} |
| 549 |
|
| 550 |
/** |
| 551 |
* Create a unique constraint name. |
| 552 |
* |
| 553 |
* @param session the session |
| 554 |
* @param domain the constraint domain |
| 555 |
* @return the unique name |
| 556 |
*/ |
| 557 |
public String getUniqueDomainConstraintName(SessionLocal session, Domain domain) { |
| 558 |
return getUniqueName(domain, constraints, "CONSTRAINT_"); |
| 559 |
} |
| 560 |
|
| 561 |
/** |
| 562 |
* Create a unique index name. |
| 563 |
* |
| 564 |
* @param session the session |
| 565 |
* @param table the indexed table |
| 566 |
* @param prefix the index name prefix |
| 567 |
* @return the unique name |
| 568 |
*/ |
| 569 |
public String getUniqueIndexName(SessionLocal session, Table table, String prefix) { |
| 570 |
Map<String, Index> tableIndexes; |
| 571 |
if (table.isTemporary() && !table.isGlobalTemporary()) { |
| 572 |
tableIndexes = session.getLocalTempTableIndexes(); |
| 573 |
} else { |
| 574 |
tableIndexes = indexes; |
| 575 |
} |
| 576 |
return getUniqueName(table, tableIndexes, prefix); |
| 577 |
} |
| 578 |
|
| 579 |
/** |
| 580 |
* Get the table or view with the given name. |
| 581 |
* Local temporary tables are also returned. |
| 582 |
* |
| 583 |
* @param session the session |
| 584 |
* @param name the table or view name |
| 585 |
* @return the table or view |
| 586 |
* @throws DbException if no such object exists |
| 587 |
*/ |
| 588 |
public Table getTableOrView(SessionLocal session, String name) { |
| 589 |
Table table = tablesAndViews.get(name); |
| 590 |
if (table == null) { |
| 591 |
if (session != null) { |
| 592 |
table = session.findLocalTempTable(name); |
| 593 |
} |
| 594 |
if (table == null) { |
| 595 |
throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, name); |
| 596 |
} |
| 597 |
} |
| 598 |
return table; |
| 599 |
} |
| 600 |
|
| 601 |
/** |
| 602 |
* Get the domain with the given name. |
| 603 |
* |
| 604 |
* @param name the domain name |
| 605 |
* @return the domain |
| 606 |
* @throws DbException if no such object exists |
| 607 |
*/ |
| 608 |
public Domain getDomain(String name) { |
| 609 |
Domain domain = domains.get(name); |
| 610 |
if (domain == null) { |
| 611 |
throw DbException.get(ErrorCode.DOMAIN_NOT_FOUND_1, name); |
| 612 |
} |
| 613 |
return domain; |
| 614 |
} |
| 615 |
|
| 616 |
/** |
| 617 |
* Get the index with the given name. |
| 618 |
* |
| 619 |
* @param name the index name |
| 620 |
* @return the index |
| 621 |
* @throws DbException if no such object exists |
| 622 |
*/ |
| 623 |
public Index getIndex(String name) { |
| 624 |
Index index = indexes.get(name); |
| 625 |
if (index == null) { |
| 626 |
throw DbException.get(ErrorCode.INDEX_NOT_FOUND_1, name); |
| 627 |
} |
| 628 |
return index; |
| 629 |
} |
| 630 |
|
| 631 |
/** |
| 632 |
* Get the constraint with the given name. |
| 633 |
* |
| 634 |
* @param name the constraint name |
| 635 |
* @return the constraint |
| 636 |
* @throws DbException if no such object exists |
| 637 |
*/ |
| 638 |
public Constraint getConstraint(String name) { |
| 639 |
Constraint constraint = constraints.get(name); |
| 640 |
if (constraint == null) { |
| 641 |
throw DbException.get(ErrorCode.CONSTRAINT_NOT_FOUND_1, name); |
| 642 |
} |
| 643 |
return constraint; |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
* Get the user defined constant with the given name. |
| 648 |
* |
| 649 |
* @param constantName the constant name |
| 650 |
* @return the constant |
| 651 |
* @throws DbException if no such object exists |
| 652 |
*/ |
| 653 |
public Constant getConstant(String constantName) { |
| 654 |
Constant constant = constants.get(constantName); |
| 655 |
if (constant == null) { |
| 656 |
throw DbException.get(ErrorCode.CONSTANT_NOT_FOUND_1, constantName); |
| 657 |
} |
| 658 |
return constant; |
| 659 |
} |
| 660 |
|
| 661 |
/** |
| 662 |
* Get the sequence with the given name. |
| 663 |
* |
| 664 |
* @param sequenceName the sequence name |
| 665 |
* @return the sequence |
| 666 |
* @throws DbException if no such object exists |
| 667 |
*/ |
| 668 |
public Sequence getSequence(String sequenceName) { |
| 669 |
Sequence sequence = sequences.get(sequenceName); |
| 670 |
if (sequence == null) { |
| 671 |
throw DbException.get(ErrorCode.SEQUENCE_NOT_FOUND_1, sequenceName); |
| 672 |
} |
| 673 |
return sequence; |
| 674 |
} |
| 675 |
|
| 676 |
/** |
| 677 |
* Get all objects. |
| 678 |
* |
| 679 |
* @param addTo |
| 680 |
* list to add objects to, or {@code null} to allocate a new |
| 681 |
* list |
| 682 |
* @return the specified list with added objects, or a new (possibly empty) list |
| 683 |
* with all objects |
| 684 |
*/ |
| 685 |
public ArrayList<SchemaObject> getAll(ArrayList<SchemaObject> addTo) { |
| 686 |
if (addTo == null) { |
| 687 |
addTo = Utils.newSmallArrayList(); |
| 688 |
} |
| 689 |
addTo.addAll(tablesAndViews.values()); |
| 690 |
addTo.addAll(domains.values()); |
| 691 |
addTo.addAll(synonyms.values()); |
| 692 |
addTo.addAll(sequences.values()); |
| 693 |
addTo.addAll(indexes.values()); |
| 694 |
addTo.addAll(triggers.values()); |
| 695 |
addTo.addAll(constraints.values()); |
| 696 |
addTo.addAll(constants.values()); |
| 697 |
addTo.addAll(functionsAndAggregates.values()); |
| 698 |
return addTo; |
| 699 |
} |
| 700 |
|
| 701 |
/** |
| 702 |
* Get all objects of the given type. |
| 703 |
* |
| 704 |
* @param type |
| 705 |
* the object type |
| 706 |
* @param addTo |
| 707 |
* list to add objects to |
| 708 |
*/ |
| 709 |
public void getAll(int type, ArrayList<SchemaObject> addTo) { |
| 710 |
addTo.addAll(getMap(type).values()); |
| 711 |
} |
| 712 |
|
| 713 |
public Collection<Domain> getAllDomains() { |
| 714 |
return domains.values(); |
| 715 |
} |
| 716 |
|
| 717 |
public Collection<Constraint> getAllConstraints() { |
| 718 |
return constraints.values(); |
| 719 |
} |
| 720 |
|
| 721 |
public Collection<Constant> getAllConstants() { |
| 722 |
return constants.values(); |
| 723 |
} |
| 724 |
|
| 725 |
public Collection<Sequence> getAllSequences() { |
| 726 |
return sequences.values(); |
| 727 |
} |
| 728 |
|
| 729 |
public Collection<TriggerObject> getAllTriggers() { |
| 730 |
return triggers.values(); |
| 731 |
} |
| 732 |
|
| 733 |
/** |
| 734 |
* Get all tables and views. |
| 735 |
* |
| 736 |
* @param session the session, {@code null} to exclude meta tables |
| 737 |
* @return a (possible empty) list of all objects |
| 738 |
*/ |
| 739 |
public Collection<Table> getAllTablesAndViews(SessionLocal session) { |
| 740 |
return tablesAndViews.values(); |
| 741 |
} |
| 742 |
|
| 743 |
public Collection<Index> getAllIndexes() { |
| 744 |
return indexes.values(); |
| 745 |
} |
| 746 |
|
| 747 |
public Collection<TableSynonym> getAllSynonyms() { |
| 748 |
return synonyms.values(); |
| 749 |
} |
| 750 |
|
| 751 |
public Collection<UserDefinedFunction> getAllFunctionsAndAggregates() { |
| 752 |
return functionsAndAggregates.values(); |
| 753 |
} |
| 754 |
|
| 755 |
/** |
| 756 |
* Get the table with the given name, if any. |
| 757 |
* |
| 758 |
* @param session the session |
| 759 |
* @param name the table name |
| 760 |
* @return the table or null if not found |
| 761 |
*/ |
| 762 |
public Table getTableOrViewByName(SessionLocal session, String name) { |
| 763 |
return tablesAndViews.get(name); |
| 764 |
} |
| 765 |
|
| 766 |
/** |
| 767 |
* Remove an object from this schema. |
| 768 |
* |
| 769 |
* @param obj the object to remove |
| 770 |
*/ |
| 771 |
public void remove(SchemaObject obj) { |
| 772 |
String objName = obj.getName(); |
| 773 |
Map<String, SchemaObject> map = getMap(obj.getType()); |
| 774 |
if (map.remove(objName) == null) { |
| 775 |
throw DbException.getInternalError("not found: " + objName); |
| 776 |
} |
| 777 |
freeUniqueName(objName); |
| 778 |
} |
| 779 |
|
| 780 |
/** |
| 781 |
* Add a table to the schema. |
| 782 |
* |
| 783 |
* @param data the create table information |
| 784 |
* @return the created {@link Table} object |
| 785 |
*/ |
| 786 |
public Table createTable(CreateTableData data) { |
| 787 |
synchronized (database) { |
| 788 |
if (!data.temporary || data.globalTemporary) { |
| 789 |
database.lockMeta(data.session); |
| 790 |
} |
| 791 |
data.schema = this; |
| 792 |
String tableEngine = data.tableEngine; |
| 793 |
if (tableEngine == null) { |
| 794 |
DbSettings s = database.getSettings(); |
| 795 |
tableEngine = s.defaultTableEngine; |
| 796 |
if (tableEngine == null) { |
| 797 |
return database.getStore().createTable(data); |
| 798 |
} |
| 799 |
data.tableEngine = tableEngine; |
| 800 |
} |
| 801 |
if (data.tableEngineParams == null) { |
| 802 |
data.tableEngineParams = this.tableEngineParams; |
| 803 |
} |
| 804 |
return database.getTableEngine(tableEngine).createTable(data); |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
/** |
| 809 |
* Add a table synonym to the schema. |
| 810 |
* |
| 811 |
* @param data the create synonym information |
| 812 |
* @return the created {@link TableSynonym} object |
| 813 |
*/ |
| 814 |
public TableSynonym createSynonym(CreateSynonymData data) { |
| 815 |
synchronized (database) { |
| 816 |
database.lockMeta(data.session); |
| 817 |
data.schema = this; |
| 818 |
return new TableSynonym(data); |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
/** |
| 823 |
* Add a linked table to the schema. |
| 824 |
* |
| 825 |
* @param id the object id |
| 826 |
* @param tableName the table name of the alias |
| 827 |
* @param driver the driver class name |
| 828 |
* @param url the database URL |
| 829 |
* @param user the user name |
| 830 |
* @param password the password |
| 831 |
* @param originalSchema the schema name of the target table |
| 832 |
* @param originalTable the table name of the target table |
| 833 |
* @param emitUpdates if updates should be emitted instead of delete/insert |
| 834 |
* @param force create the object even if the database can not be accessed |
| 835 |
* @return the {@link TableLink} object |
| 836 |
*/ |
| 837 |
public TableLink createTableLink(int id, String tableName, String driver, |
| 838 |
String url, String user, String password, String originalSchema, |
| 839 |
String originalTable, boolean emitUpdates, boolean force) { |
| 840 |
synchronized (database) { |
| 841 |
return new TableLink(this, id, tableName, |
| 842 |
driver, url, user, password, |
| 843 |
originalSchema, originalTable, emitUpdates, force); |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
} |