Package org.h2.schema
Class Schema
- java.lang.Object
-
- org.h2.engine.DbObject
-
- org.h2.schema.Schema
-
- All Implemented Interfaces:
HasSQL
- Direct Known Subclasses:
MetaSchema
public class Schema extends DbObject
A schema as created by the SQL statement CREATE SCHEMA
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.ConcurrentHashMap<java.lang.String,Constant>
constants
private java.util.concurrent.ConcurrentHashMap<java.lang.String,Constraint>
constraints
private java.util.concurrent.ConcurrentHashMap<java.lang.String,Domain>
domains
private java.util.concurrent.ConcurrentHashMap<java.lang.String,UserDefinedFunction>
functionsAndAggregates
private java.util.concurrent.ConcurrentHashMap<java.lang.String,Index>
indexes
private RightOwner
owner
private java.util.concurrent.ConcurrentHashMap<java.lang.String,Sequence>
sequences
private java.util.concurrent.ConcurrentHashMap<java.lang.String,TableSynonym>
synonyms
private boolean
system
private java.util.ArrayList<java.lang.String>
tableEngineParams
private java.util.concurrent.ConcurrentHashMap<java.lang.String,Table>
tablesAndViews
private java.util.HashSet<java.lang.String>
temporaryUniqueNames
The set of returned unique names that are not yet stored.private java.util.concurrent.ConcurrentHashMap<java.lang.String,TriggerObject>
triggers
-
Fields inherited from class org.h2.engine.DbObject
AGGREGATE, comment, COMMENT, CONSTANT, CONSTRAINT, database, DOMAIN, FUNCTION_ALIAS, INDEX, RIGHT, ROLE, SCHEMA, SEQUENCE, SETTING, SYNONYM, TABLE_OR_VIEW, trace, TRIGGER, USER
-
Fields inherited from interface org.h2.util.HasSQL
ADD_PLAN_INFORMATION, DEFAULT_SQL_FLAGS, NO_CASTS, QUOTE_ONLY_WHEN_REQUIRED, REPLACE_LOBS_FOR_TRACE, TRACE_SQL_FLAGS
-
-
Constructor Summary
Constructors Constructor Description Schema(Database database, int id, java.lang.String schemaName, RightOwner owner, boolean system)
Create a new schema object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(SchemaObject obj)
Add an object to this schema.boolean
canDrop()
Check if this schema can be dropped.TableSynonym
createSynonym(CreateSynonymData data)
Add a table synonym to the schema.Table
createTable(CreateTableData data)
Add a table to the schema.TableLink
createTableLink(int id, java.lang.String tableName, java.lang.String driver, java.lang.String url, java.lang.String user, java.lang.String password, java.lang.String originalSchema, java.lang.String originalTable, boolean emitUpdates, boolean force)
Add a linked table to the schema.UserAggregate
findAggregate(java.lang.String name)
Get the user defined aggregate function if it exists.Constant
findConstant(java.lang.String constantName)
Try to find a user defined constant with this name.Constraint
findConstraint(SessionLocal session, java.lang.String name)
Try to find a constraint with this name.Domain
findDomain(java.lang.String name)
Get the domain if it exists, or null if not.FunctionAlias
findFunction(java.lang.String functionAlias)
Try to find a user defined function with this name.UserDefinedFunction
findFunctionOrAggregate(java.lang.String name)
Try to find a user defined function or aggregate function with the specified name.Index
findIndex(SessionLocal session, java.lang.String name)
Try to find an index with this name.Sequence
findSequence(java.lang.String sequenceName)
Try to find a sequence with this name.Table
findTableOrView(SessionLocal session, java.lang.String name)
Try to find a table or view with this name.TriggerObject
findTrigger(java.lang.String name)
Try to find a trigger with this name.void
freeUniqueName(java.lang.String name)
Release a unique object name.void
getAll(int type, java.util.ArrayList<SchemaObject> addTo)
Get all objects of the given type.java.util.ArrayList<SchemaObject>
getAll(java.util.ArrayList<SchemaObject> addTo)
Get all objects.java.util.Collection<Constant>
getAllConstants()
java.util.Collection<Constraint>
getAllConstraints()
java.util.Collection<Domain>
getAllDomains()
java.util.Collection<UserDefinedFunction>
getAllFunctionsAndAggregates()
java.util.Collection<Index>
getAllIndexes()
java.util.Collection<Sequence>
getAllSequences()
java.util.Collection<TableSynonym>
getAllSynonyms()
java.util.Collection<Table>
getAllTablesAndViews(SessionLocal session)
Get all tables and views.java.util.Collection<TriggerObject>
getAllTriggers()
java.util.ArrayList<DbObject>
getChildren()
Get the list of dependent children (for tables, this includes indexes and so on).Constant
getConstant(java.lang.String constantName)
Get the user defined constant with the given name.Constraint
getConstraint(java.lang.String name)
Get the constraint with the given name.java.lang.String
getCreateSQL()
Construct the CREATE ...java.lang.String
getCreateSQLForCopy(Table table, java.lang.String quotedName)
Build a SQL statement to re-create the object, or to create a copy of the object with a different name or referencing a different tableDomain
getDomain(java.lang.String name)
Get the domain with the given name.Index
getIndex(java.lang.String name)
Get the index with the given name.private java.util.Map<java.lang.String,SchemaObject>
getMap(int type)
RightOwner
getOwner()
Get the owner of this schema.Sequence
getSequence(java.lang.String sequenceName)
Get the sequence with the given name.TableSynonym
getSynonym(java.lang.String name)
Try to find a synonym with this name.java.util.ArrayList<java.lang.String>
getTableEngineParams()
Get table engine params of this schema.Table
getTableOrView(SessionLocal session, java.lang.String name)
Get the table or view with the given name.Table
getTableOrViewByName(SessionLocal session, java.lang.String name)
Get the table with the given name, if any.int
getType()
Get the object type.java.lang.String
getUniqueConstraintName(SessionLocal session, Table table)
Create a unique constraint name.java.lang.String
getUniqueDomainConstraintName(SessionLocal session, Domain domain)
Create a unique constraint name.java.lang.String
getUniqueIndexName(SessionLocal session, Table table, java.lang.String prefix)
Create a unique index name.private java.lang.String
getUniqueName(DbObject obj, java.util.Map<java.lang.String,? extends SchemaObject> map, java.lang.String prefix)
boolean
isEmpty()
Return whether is this schema is empty (does not contain any objects).void
remove(SchemaObject obj)
Remove an object from this schema.void
removeChildrenAndResources(SessionLocal session)
Delete all dependent children objects and resources of this object.private void
removeChildrenFromMap(SessionLocal session, java.util.concurrent.ConcurrentHashMap<java.lang.String,? extends SchemaObject> map)
void
rename(SchemaObject obj, java.lang.String newName)
Rename an object.void
reserveUniqueName(java.lang.String name)
Reserve a unique object name.Table
resolveTableOrView(SessionLocal session, java.lang.String name)
Try to find a table or view with this name.void
setTableEngineParams(java.util.ArrayList<java.lang.String> tableEngineParams)
Set table engine params of this schema.-
Methods inherited from class org.h2.engine.DbObject
checkRename, getComment, getCreateSQLForMeta, getDatabase, getDropSQL, getId, getModificationId, getName, getSQL, getSQL, invalidate, isTemporary, isValid, rename, setComment, setModified, setObjectName, setTemporary, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.h2.util.HasSQL
getTraceSQL
-
-
-
-
Field Detail
-
owner
private RightOwner owner
-
system
private final boolean system
-
tableEngineParams
private java.util.ArrayList<java.lang.String> tableEngineParams
-
tablesAndViews
private final java.util.concurrent.ConcurrentHashMap<java.lang.String,Table> tablesAndViews
-
domains
private final java.util.concurrent.ConcurrentHashMap<java.lang.String,Domain> domains
-
synonyms
private final java.util.concurrent.ConcurrentHashMap<java.lang.String,TableSynonym> synonyms
-
indexes
private final java.util.concurrent.ConcurrentHashMap<java.lang.String,Index> indexes
-
sequences
private final java.util.concurrent.ConcurrentHashMap<java.lang.String,Sequence> sequences
-
triggers
private final java.util.concurrent.ConcurrentHashMap<java.lang.String,TriggerObject> triggers
-
constraints
private final java.util.concurrent.ConcurrentHashMap<java.lang.String,Constraint> constraints
-
constants
private final java.util.concurrent.ConcurrentHashMap<java.lang.String,Constant> constants
-
functionsAndAggregates
private final java.util.concurrent.ConcurrentHashMap<java.lang.String,UserDefinedFunction> functionsAndAggregates
-
temporaryUniqueNames
private final java.util.HashSet<java.lang.String> temporaryUniqueNames
The set of returned unique names that are not yet stored. It is used to avoid returning the same unique name twice when multiple threads concurrently create objects.
-
-
Constructor Detail
-
Schema
public Schema(Database database, int id, java.lang.String schemaName, RightOwner owner, boolean system)
Create a new schema object.- Parameters:
database
- the databaseid
- the object idschemaName
- the schema nameowner
- the owner of the schemasystem
- if this is a system schema (such a schema can not be dropped)
-
-
Method Detail
-
canDrop
public boolean canDrop()
Check if this schema can be dropped. System schemas can not be dropped.- Returns:
- true if it can be dropped
-
getCreateSQLForCopy
public java.lang.String getCreateSQLForCopy(Table table, java.lang.String quotedName)
Description copied from class:DbObject
Build a SQL statement to re-create the object, or to create a copy of the object with a different name or referencing a different table- Specified by:
getCreateSQLForCopy
in classDbObject
- Parameters:
table
- the new tablequotedName
- the quoted name- Returns:
- the SQL statement
-
getCreateSQL
public java.lang.String getCreateSQL()
Description copied from class:DbObject
Construct the CREATE ... SQL statement for this object.- Specified by:
getCreateSQL
in classDbObject
- Returns:
- the SQL statement
-
getType
public int getType()
Description copied from class:DbObject
Get the object type.
-
isEmpty
public boolean isEmpty()
Return whether is this schema is empty (does not contain any objects).- Returns:
true
if this schema is empty,false
otherwise
-
getChildren
public java.util.ArrayList<DbObject> getChildren()
Description copied from class:DbObject
Get the list of dependent children (for tables, this includes indexes and so on).- Overrides:
getChildren
in classDbObject
- Returns:
- the list of children, or
null
-
removeChildrenAndResources
public void removeChildrenAndResources(SessionLocal session)
Description copied from class:DbObject
Delete all dependent children objects and resources of this object.- Specified by:
removeChildrenAndResources
in classDbObject
- Parameters:
session
- the session
-
removeChildrenFromMap
private void removeChildrenFromMap(SessionLocal session, java.util.concurrent.ConcurrentHashMap<java.lang.String,? extends SchemaObject> map)
-
getOwner
public RightOwner getOwner()
Get the owner of this schema.- Returns:
- the owner
-
getTableEngineParams
public java.util.ArrayList<java.lang.String> getTableEngineParams()
Get table engine params of this schema.- Returns:
- default table engine params
-
setTableEngineParams
public void setTableEngineParams(java.util.ArrayList<java.lang.String> tableEngineParams)
Set table engine params of this schema.- Parameters:
tableEngineParams
- default table engine params
-
getMap
private java.util.Map<java.lang.String,SchemaObject> getMap(int type)
-
add
public void add(SchemaObject obj)
Add an object to this schema. This method must not be called within CreateSchemaObject; use Database.addSchemaObject() instead- Parameters:
obj
- the object to add
-
rename
public void rename(SchemaObject obj, java.lang.String newName)
Rename an object.- Parameters:
obj
- the object to renamenewName
- the new name
-
findTableOrView
public Table findTableOrView(SessionLocal session, java.lang.String name)
Try to find a table or view with this name. This method returns null if no object with this name exists. Local temporary tables are also returned. Synonyms are not returned or resolved.- Parameters:
session
- the sessionname
- the object name- Returns:
- the object or null
-
resolveTableOrView
public Table resolveTableOrView(SessionLocal session, java.lang.String name)
Try to find a table or view with this name. This method returns null if no object with this name exists. Local temporary tables are also returned. If a synonym with this name exists, the backing table of the synonym is returned- Parameters:
session
- the sessionname
- the object name- Returns:
- the object or null
-
getSynonym
public TableSynonym getSynonym(java.lang.String name)
Try to find a synonym with this name. This method returns null if no object with this name exists.- Parameters:
name
- the object name- Returns:
- the object or null
-
findDomain
public Domain findDomain(java.lang.String name)
Get the domain if it exists, or null if not.- Parameters:
name
- the name of the domain- Returns:
- the domain or null
-
findIndex
public Index findIndex(SessionLocal session, java.lang.String name)
Try to find an index with this name. This method returns null if no object with this name exists.- Parameters:
session
- the sessionname
- the object name- Returns:
- the object or null
-
findTrigger
public TriggerObject findTrigger(java.lang.String name)
Try to find a trigger with this name. This method returns null if no object with this name exists.- Parameters:
name
- the object name- Returns:
- the object or null
-
findSequence
public Sequence findSequence(java.lang.String sequenceName)
Try to find a sequence with this name. This method returns null if no object with this name exists.- Parameters:
sequenceName
- the object name- Returns:
- the object or null
-
findConstraint
public Constraint findConstraint(SessionLocal session, java.lang.String name)
Try to find a constraint with this name. This method returns null if no object with this name exists.- Parameters:
session
- the sessionname
- the object name- Returns:
- the object or null
-
findConstant
public Constant findConstant(java.lang.String constantName)
Try to find a user defined constant with this name. This method returns null if no object with this name exists.- Parameters:
constantName
- the object name- Returns:
- the object or null
-
findFunction
public FunctionAlias findFunction(java.lang.String functionAlias)
Try to find a user defined function with this name. This method returns null if no object with this name exists.- Parameters:
functionAlias
- the object name- Returns:
- the object or null
-
findAggregate
public UserAggregate findAggregate(java.lang.String name)
Get the user defined aggregate function if it exists. This method returns null if no object with this name exists.- Parameters:
name
- the name of the user defined aggregate function- Returns:
- the aggregate function or null
-
findFunctionOrAggregate
public UserDefinedFunction findFunctionOrAggregate(java.lang.String name)
Try to find a user defined function or aggregate function with the specified name. This method returns null if no object with this name exists.- Parameters:
name
- the object name- Returns:
- the object or null
-
reserveUniqueName
public void reserveUniqueName(java.lang.String name)
Reserve a unique object name.- Parameters:
name
- the object name
-
freeUniqueName
public void freeUniqueName(java.lang.String name)
Release a unique object name.- Parameters:
name
- the object name
-
getUniqueName
private java.lang.String getUniqueName(DbObject obj, java.util.Map<java.lang.String,? extends SchemaObject> map, java.lang.String prefix)
-
getUniqueConstraintName
public java.lang.String getUniqueConstraintName(SessionLocal session, Table table)
Create a unique constraint name.- Parameters:
session
- the sessiontable
- the constraint table- Returns:
- the unique name
-
getUniqueDomainConstraintName
public java.lang.String getUniqueDomainConstraintName(SessionLocal session, Domain domain)
Create a unique constraint name.- Parameters:
session
- the sessiondomain
- the constraint domain- Returns:
- the unique name
-
getUniqueIndexName
public java.lang.String getUniqueIndexName(SessionLocal session, Table table, java.lang.String prefix)
Create a unique index name.- Parameters:
session
- the sessiontable
- the indexed tableprefix
- the index name prefix- Returns:
- the unique name
-
getTableOrView
public Table getTableOrView(SessionLocal session, java.lang.String name)
Get the table or view with the given name. Local temporary tables are also returned.- Parameters:
session
- the sessionname
- the table or view name- Returns:
- the table or view
- Throws:
DbException
- if no such object exists
-
getDomain
public Domain getDomain(java.lang.String name)
Get the domain with the given name.- Parameters:
name
- the domain name- Returns:
- the domain
- Throws:
DbException
- if no such object exists
-
getIndex
public Index getIndex(java.lang.String name)
Get the index with the given name.- Parameters:
name
- the index name- Returns:
- the index
- Throws:
DbException
- if no such object exists
-
getConstraint
public Constraint getConstraint(java.lang.String name)
Get the constraint with the given name.- Parameters:
name
- the constraint name- Returns:
- the constraint
- Throws:
DbException
- if no such object exists
-
getConstant
public Constant getConstant(java.lang.String constantName)
Get the user defined constant with the given name.- Parameters:
constantName
- the constant name- Returns:
- the constant
- Throws:
DbException
- if no such object exists
-
getSequence
public Sequence getSequence(java.lang.String sequenceName)
Get the sequence with the given name.- Parameters:
sequenceName
- the sequence name- Returns:
- the sequence
- Throws:
DbException
- if no such object exists
-
getAll
public java.util.ArrayList<SchemaObject> getAll(java.util.ArrayList<SchemaObject> addTo)
Get all objects.- Parameters:
addTo
- list to add objects to, ornull
to allocate a new list- Returns:
- the specified list with added objects, or a new (possibly empty) list with all objects
-
getAll
public void getAll(int type, java.util.ArrayList<SchemaObject> addTo)
Get all objects of the given type.- Parameters:
type
- the object typeaddTo
- list to add objects to
-
getAllDomains
public java.util.Collection<Domain> getAllDomains()
-
getAllConstraints
public java.util.Collection<Constraint> getAllConstraints()
-
getAllConstants
public java.util.Collection<Constant> getAllConstants()
-
getAllSequences
public java.util.Collection<Sequence> getAllSequences()
-
getAllTriggers
public java.util.Collection<TriggerObject> getAllTriggers()
-
getAllTablesAndViews
public java.util.Collection<Table> getAllTablesAndViews(SessionLocal session)
Get all tables and views.- Parameters:
session
- the session,null
to exclude meta tables- Returns:
- a (possible empty) list of all objects
-
getAllIndexes
public java.util.Collection<Index> getAllIndexes()
-
getAllSynonyms
public java.util.Collection<TableSynonym> getAllSynonyms()
-
getAllFunctionsAndAggregates
public java.util.Collection<UserDefinedFunction> getAllFunctionsAndAggregates()
-
getTableOrViewByName
public Table getTableOrViewByName(SessionLocal session, java.lang.String name)
Get the table with the given name, if any.- Parameters:
session
- the sessionname
- the table name- Returns:
- the table or null if not found
-
remove
public void remove(SchemaObject obj)
Remove an object from this schema.- Parameters:
obj
- the object to remove
-
createTable
public Table createTable(CreateTableData data)
Add a table to the schema.- Parameters:
data
- the create table information- Returns:
- the created
Table
object
-
createSynonym
public TableSynonym createSynonym(CreateSynonymData data)
Add a table synonym to the schema.- Parameters:
data
- the create synonym information- Returns:
- the created
TableSynonym
object
-
createTableLink
public TableLink createTableLink(int id, java.lang.String tableName, java.lang.String driver, java.lang.String url, java.lang.String user, java.lang.String password, java.lang.String originalSchema, java.lang.String originalTable, boolean emitUpdates, boolean force)
Add a linked table to the schema.- Parameters:
id
- the object idtableName
- the table name of the aliasdriver
- the driver class nameurl
- the database URLuser
- the user namepassword
- the passwordoriginalSchema
- the schema name of the target tableoriginalTable
- the table name of the target tableemitUpdates
- if updates should be emitted instead of delete/insertforce
- create the object even if the database can not be accessed- Returns:
- the
TableLink
object
-
-