Package org.h2.table
Class TableView
- java.lang.Object
-
- org.h2.engine.DbObject
-
- org.h2.schema.SchemaObject
-
- org.h2.table.Table
-
- org.h2.table.TableView
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
TableView.CacheKey
The key of the index cache for views.
-
Field Summary
Fields Modifier and Type Field Description private boolean
allowRecursive
private Column[]
columnTemplates
private DbException
createException
private ViewIndex
index
private boolean
isRecursiveQueryDetected
private boolean
isTableExpression
private long
lastModificationCheck
private long
maxDataModificationId
private User
owner
private java.lang.String
querySQL
private ResultInterface
recursiveResult
private static long
ROW_COUNT_APPROXIMATION
private java.util.ArrayList<Table>
tables
private Query
topQuery
private Query
viewQuery
-
Fields inherited from class org.h2.table.Table
columns, compareMode, EXCLUSIVE_LOCK, isHidden, READ_LOCK, TYPE_CACHED, TYPE_MEMORY, WRITE_LOCK
-
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 TableView(Schema schema, int id, java.lang.String name, java.lang.String querySQL, java.util.ArrayList<Parameter> params, Column[] columnTemplates, SessionLocal session, boolean allowRecursive, boolean literalsChecked, boolean isTableExpression, boolean isTemporary)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addDependencies(java.util.HashSet<DbObject> dependencies)
Add all objects that this table depends on to the hash set.private void
addDependentViewToTables()
Index
addIndex(SessionLocal session, java.lang.String indexName, int indexId, IndexColumn[] cols, int uniqueColumnCount, IndexType indexType, boolean create, java.lang.String indexComment)
Create an index for this tablevoid
addRow(SessionLocal session, Row row)
Add a row to the table and all indexes.boolean
canDrop()
Check if this table can be dropped.boolean
canGetRowCount(SessionLocal session)
Check if the row count can be retrieved quickly.boolean
canReference()
Check if this table can be referenced.void
checkSupportAlter()
Check if this table supports ALTER TABLE.static void
clearIndexCaches(Database database)
Clear the cached indexes for all sessions.void
close(SessionLocal session)
Close the table object and flush changes.private Query
compileViewQuery(SessionLocal session, java.lang.String sql, boolean literalsChecked)
static java.util.List<Column>
createQueryColumnTemplateList(java.lang.String[] cols, Query theQuery, java.lang.String[] querySQLOutput)
Creates a list of column templates from a query (usually from WITH query, but could be any query)static Table
createShadowTableForRecursiveTableExpression(boolean isTemporary, SessionLocal targetSession, java.lang.String cteViewName, Schema schema, java.util.List<Column> columns, Database db)
Create a table for a recursive query.static TableView
createTableViewMaybeRecursive(Schema schema, int id, java.lang.String name, java.lang.String querySQL, java.util.ArrayList<Parameter> parameters, Column[] columnTemplates, SessionLocal session, boolean literalsChecked, boolean isTableExpression, boolean isTemporary, Database db)
Create a view.static TableView
createTempView(SessionLocal session, User owner, java.lang.String name, Column[] columnTemplates, Query query, Query topQuery)
Create a temporary view out of the given query.static void
destroyShadowTableForRecursiveExpression(boolean isTemporary, SessionLocal targetSession, Table recursiveTable)
Remove a table for a recursive query.PlanItem
getBestPlanItem(SessionLocal session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder, AllColumnsForPlan allColumnsSet)
Get the best plan for the given search mask.java.lang.String
getCreateSQL()
Construct the CREATE ...java.lang.String
getCreateSQL(boolean orReplace, boolean force)
Generate "CREATE" SQL statement for the view.private java.lang.String
getCreateSQL(boolean orReplace, boolean force, java.lang.String quotedName)
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 tablejava.lang.String
getDropSQL()
Construct a DROP ...java.util.ArrayList<Index>
getIndexes()
Get all indexes for this table.long
getMaxDataModificationId()
Get the last data modification id.private static int
getMaxParameterIndex(java.util.ArrayList<Parameter> parameters)
User
getOwner()
int
getParameterOffset(java.util.ArrayList<Parameter> additionalParameters)
Get the index of the first parameter.java.lang.String
getQuery()
ResultInterface
getRecursiveResult()
long
getRowCount(SessionLocal session)
Get the row count for this table.long
getRowCountApproximation(SessionLocal session)
Get the approximated row count for this table.Index
getScanIndex(SessionLocal session)
Get the scan index to iterate through all rows.Index
getScanIndex(SessionLocal session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder, AllColumnsForPlan allColumnsSet)
Get the scan index for this table.java.lang.StringBuilder
getSQL(java.lang.StringBuilder builder, int sqlFlags)
Appends the SQL statement of this object to the specified builder.java.util.List<Table>
getTables()
TableType
getTableType()
Get the table type nameQuery
getTopQuery()
private void
init(java.lang.String querySQL, java.util.ArrayList<Parameter> params, Column[] columnTemplates, SessionLocal session, boolean allowRecursive, boolean literalsChecked, boolean isTableExpression)
private void
initColumnsAndTables(SessionLocal session, boolean literalsChecked)
boolean
isDeterministic()
Check if the table is deterministic.boolean
isInsertable()
Returns whether this table is insertable.boolean
isInvalid()
Check if this view is currently invalid.boolean
isQueryComparable()
Check whether the table (or view) contains no columns that prevent index conditions to be used.boolean
isRecursive()
boolean
isRecursiveQueryDetected()
Was query recursion detected during compiling.private boolean
isRecursiveQueryExceptionDetected(DbException exception)
Does exception indicate query recursion?boolean
isView()
DbException
recompile(SessionLocal session, boolean force, boolean clearIndexCache)
Re-compile the view query and all views that depend on this object.void
removeChildrenAndResources(SessionLocal session)
Delete all dependent children objects and resources of this object.private void
removeCurrentViewFromOtherTables()
void
removeRow(SessionLocal session, Row row)
Remove a row from the table and all indexes.void
replace(java.lang.String querySQL, Column[] newColumnTemplates, SessionLocal session, boolean recursive, boolean force, boolean literalsChecked)
Try to replace the SQL statement of the view and re-compile this and all dependent views.private void
setOwner(User owner)
void
setRecursiveResult(ResultInterface value)
private void
setTopQuery(Query topQuery)
long
truncate(SessionLocal session)
Remove all rows from the table and indexes.-
Methods inherited from class org.h2.table.Table
addConstraint, addDependentView, addSequence, addSynonym, addTrigger, canTruncate, checkDeadlock, checkWritingAllowed, compareValues, convertInsertRow, convertUpdateRow, createRow, doesColumnExist, dropMultipleColumnsConstraintsAndIndexes, findColumn, findPrimaryKey, fire, fireAfterRow, fireBeforeRow, fireRow, getCheckForeignKeyConstraints, getChildren, getColumn, getColumn, getColumn, getColumns, getCompareMode, getConstraints, getDependentViews, getDiskSpaceUsed, getIdentityColumn, getIndex, getIndexForColumn, getMainIndexColumn, getNullRow, getOnCommitDrop, getOnCommitTruncate, getPrimaryKey, getRow, getRowFactory, getRowIdColumn, getSQLTableType, getTemplateRow, getTemplateSimpleRow, getTriggers, getType, hasSelectTrigger, isGlobalTemporary, isHidden, isLockedExclusively, isLockedExclusivelyBy, isPersistData, isPersistIndexes, isRowLockable, isTableExpression, lock, lockRow, removeColumnExpressionsDependencies, removeConstraint, removeDependentView, removeIndex, removeIndexOrTransferOwnership, removeSequence, removeSynonym, removeTrigger, rename, renameColumn, setCheckForeignKeyConstraints, setColumns, setHidden, setOnCommitDrop, setOnCommitTruncate, setTableExpression, unlock, updateRow, updateRows
-
Methods inherited from class org.h2.schema.SchemaObject
getSchema, getSQL
-
Methods inherited from class org.h2.engine.DbObject
checkRename, getComment, getCreateSQLForMeta, getDatabase, getId, getModificationId, getName, invalidate, isTemporary, isValid, 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
-
ROW_COUNT_APPROXIMATION
private static final long ROW_COUNT_APPROXIMATION
- See Also:
- Constant Field Values
-
querySQL
private java.lang.String querySQL
-
tables
private java.util.ArrayList<Table> tables
-
columnTemplates
private Column[] columnTemplates
-
viewQuery
private Query viewQuery
-
index
private ViewIndex index
-
allowRecursive
private boolean allowRecursive
-
createException
private DbException createException
-
lastModificationCheck
private long lastModificationCheck
-
maxDataModificationId
private long maxDataModificationId
-
owner
private User owner
-
topQuery
private Query topQuery
-
recursiveResult
private ResultInterface recursiveResult
-
isRecursiveQueryDetected
private boolean isRecursiveQueryDetected
-
isTableExpression
private boolean isTableExpression
-
-
Constructor Detail
-
TableView
public TableView(Schema schema, int id, java.lang.String name, java.lang.String querySQL, java.util.ArrayList<Parameter> params, Column[] columnTemplates, SessionLocal session, boolean allowRecursive, boolean literalsChecked, boolean isTableExpression, boolean isTemporary)
-
-
Method Detail
-
replace
public void replace(java.lang.String querySQL, Column[] newColumnTemplates, SessionLocal session, boolean recursive, boolean force, boolean literalsChecked)
Try to replace the SQL statement of the view and re-compile this and all dependent views.- Parameters:
querySQL
- the SQL statementnewColumnTemplates
- the columnssession
- the sessionrecursive
- whether this is a recursive viewforce
- if errors should be ignoredliteralsChecked
- if literals have been checked
-
init
private void init(java.lang.String querySQL, java.util.ArrayList<Parameter> params, Column[] columnTemplates, SessionLocal session, boolean allowRecursive, boolean literalsChecked, boolean isTableExpression)
-
compileViewQuery
private Query compileViewQuery(SessionLocal session, java.lang.String sql, boolean literalsChecked)
-
recompile
public DbException recompile(SessionLocal session, boolean force, boolean clearIndexCache)
Re-compile the view query and all views that depend on this object.- Parameters:
session
- the sessionforce
- if exceptions should be ignoredclearIndexCache
- if we need to clear view index cache- Returns:
- the exception if re-compiling this or any dependent view failed (only when force is disabled)
-
initColumnsAndTables
private void initColumnsAndTables(SessionLocal session, boolean literalsChecked)
-
isInvalid
public boolean isInvalid()
Check if this view is currently invalid.- Returns:
- true if it is
-
getBestPlanItem
public PlanItem getBestPlanItem(SessionLocal session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder, AllColumnsForPlan allColumnsSet)
Description copied from class:Table
Get the best plan for the given search mask.- Overrides:
getBestPlanItem
in classTable
- Parameters:
session
- the sessionmasks
- per-column comparison bit masks, null means 'always false', see constants in IndexConditionfilters
- all joined table filtersfilter
- the current table filter indexsortOrder
- the sort orderallColumnsSet
- the set of all columns- Returns:
- the plan item
-
isQueryComparable
public boolean isQueryComparable()
Description copied from class:Table
Check whether the table (or view) contains no columns that prevent index conditions to be used. For example, a view that contains the ROWNUM() pseudo-column prevents this.- Overrides:
isQueryComparable
in classTable
- Returns:
- true if the table contains no query-comparable column
-
getTopQuery
public Query getTopQuery()
-
getDropSQL
public java.lang.String getDropSQL()
Description copied from class:DbObject
Construct a DROP ... SQL statement for this object.- Overrides:
getDropSQL
in classDbObject
- Returns:
- the SQL statement
-
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- Overrides:
getCreateSQLForCopy
in classTable
- 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
-
getCreateSQL
public java.lang.String getCreateSQL(boolean orReplace, boolean force)
Generate "CREATE" SQL statement for the view.- Parameters:
orReplace
- if true, then include the OR REPLACE clauseforce
- if true, then include the FORCE clause- Returns:
- the SQL statement
-
getCreateSQL
private java.lang.String getCreateSQL(boolean orReplace, boolean force, java.lang.String quotedName)
-
close
public void close(SessionLocal session)
Description copied from class:Table
Close the table object and flush changes.
-
addIndex
public Index addIndex(SessionLocal session, java.lang.String indexName, int indexId, IndexColumn[] cols, int uniqueColumnCount, IndexType indexType, boolean create, java.lang.String indexComment)
Description copied from class:Table
Create an index for this table- Specified by:
addIndex
in classTable
- Parameters:
session
- the sessionindexName
- the name of the indexindexId
- the idcols
- the index columnsuniqueColumnCount
- the count of unique columnsindexType
- the index typecreate
- whether this is a new indexindexComment
- the comment- Returns:
- the index
-
isInsertable
public boolean isInsertable()
Description copied from class:Table
Returns whether this table is insertable.- Overrides:
isInsertable
in classTable
- Returns:
- whether this table is insertable
-
removeRow
public void removeRow(SessionLocal session, Row row)
Description copied from class:Table
Remove a row from the table and all indexes.
-
addRow
public void addRow(SessionLocal session, Row row)
Description copied from class:Table
Add a row to the table and all indexes.
-
checkSupportAlter
public void checkSupportAlter()
Description copied from class:Table
Check if this table supports ALTER TABLE.- Specified by:
checkSupportAlter
in classTable
-
truncate
public long truncate(SessionLocal session)
Description copied from class:Table
Remove all rows from the table and indexes.
-
getRowCount
public long getRowCount(SessionLocal session)
Description copied from class:Table
Get the row count for this table.- Specified by:
getRowCount
in classTable
- Parameters:
session
- the session- Returns:
- the row count
-
canGetRowCount
public boolean canGetRowCount(SessionLocal session)
Description copied from class:Table
Check if the row count can be retrieved quickly.- Specified by:
canGetRowCount
in classTable
- Parameters:
session
- the session- Returns:
- true if it can
-
canDrop
public boolean canDrop()
Description copied from class:Table
Check if this table can be dropped.
-
getTableType
public TableType getTableType()
Description copied from class:Table
Get the table type name- Specified by:
getTableType
in classTable
- Returns:
- the table type name
-
removeChildrenAndResources
public void removeChildrenAndResources(SessionLocal session)
Description copied from class:DbObject
Delete all dependent children objects and resources of this object.- Overrides:
removeChildrenAndResources
in classTable
- Parameters:
session
- the session
-
clearIndexCaches
public static void clearIndexCaches(Database database)
Clear the cached indexes for all sessions.- Parameters:
database
- the database
-
getSQL
public java.lang.StringBuilder getSQL(java.lang.StringBuilder builder, int sqlFlags)
Description copied from interface:HasSQL
Appends the SQL statement of this object to the specified builder.- Specified by:
getSQL
in interfaceHasSQL
- Overrides:
getSQL
in classSchemaObject
- Parameters:
builder
- string buildersqlFlags
- formatting flags- Returns:
- the specified string builder
-
getQuery
public java.lang.String getQuery()
-
getScanIndex
public Index getScanIndex(SessionLocal session)
Description copied from class:Table
Get the scan index to iterate through all rows.- Specified by:
getScanIndex
in classTable
- Parameters:
session
- the session- Returns:
- the index
-
getScanIndex
public Index getScanIndex(SessionLocal session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder, AllColumnsForPlan allColumnsSet)
Description copied from class:Table
Get the scan index for this table.- Overrides:
getScanIndex
in classTable
- Parameters:
session
- the sessionmasks
- the search maskfilters
- the table filtersfilter
- the filter indexsortOrder
- the sort orderallColumnsSet
- all columns- Returns:
- the scan index
-
canReference
public boolean canReference()
Description copied from class:Table
Check if this table can be referenced.- Overrides:
canReference
in classTable
- Returns:
- true if it can
-
getIndexes
public java.util.ArrayList<Index> getIndexes()
Description copied from class:Table
Get all indexes for this table.- Specified by:
getIndexes
in classTable
- Returns:
- the list of indexes
-
getMaxDataModificationId
public long getMaxDataModificationId()
Description copied from class:Table
Get the last data modification id.- Specified by:
getMaxDataModificationId
in classTable
- Returns:
- the modification id
-
removeCurrentViewFromOtherTables
private void removeCurrentViewFromOtherTables()
-
addDependentViewToTables
private void addDependentViewToTables()
-
setOwner
private void setOwner(User owner)
-
getOwner
public User getOwner()
-
createTempView
public static TableView createTempView(SessionLocal session, User owner, java.lang.String name, Column[] columnTemplates, Query query, Query topQuery)
Create a temporary view out of the given query.- Parameters:
session
- the sessionowner
- the owner of the queryname
- the view namecolumnTemplates
- column templates, ornull
query
- the querytopQuery
- the top level query- Returns:
- the view table
-
setTopQuery
private void setTopQuery(Query topQuery)
-
getRowCountApproximation
public long getRowCountApproximation(SessionLocal session)
Description copied from class:Table
Get the approximated row count for this table.- Specified by:
getRowCountApproximation
in classTable
- Parameters:
session
- the session- Returns:
- the approximated row count
-
getParameterOffset
public int getParameterOffset(java.util.ArrayList<Parameter> additionalParameters)
Get the index of the first parameter.- Parameters:
additionalParameters
- additional parameters- Returns:
- the index of the first parameter
-
getMaxParameterIndex
private static int getMaxParameterIndex(java.util.ArrayList<Parameter> parameters)
-
isRecursive
public boolean isRecursive()
-
isDeterministic
public boolean isDeterministic()
Description copied from class:Table
Check if the table is deterministic.- Specified by:
isDeterministic
in classTable
- Returns:
- true if it is
-
setRecursiveResult
public void setRecursiveResult(ResultInterface value)
-
getRecursiveResult
public ResultInterface getRecursiveResult()
-
addDependencies
public void addDependencies(java.util.HashSet<DbObject> dependencies)
Description copied from class:Table
Add all objects that this table depends on to the hash set.- Overrides:
addDependencies
in classTable
- Parameters:
dependencies
- the current set of dependencies
-
isRecursiveQueryDetected
public boolean isRecursiveQueryDetected()
Was query recursion detected during compiling.- Returns:
- true if yes
-
isRecursiveQueryExceptionDetected
private boolean isRecursiveQueryExceptionDetected(DbException exception)
Does exception indicate query recursion?
-
getTables
public java.util.List<Table> getTables()
-
createTableViewMaybeRecursive
public static TableView createTableViewMaybeRecursive(Schema schema, int id, java.lang.String name, java.lang.String querySQL, java.util.ArrayList<Parameter> parameters, Column[] columnTemplates, SessionLocal session, boolean literalsChecked, boolean isTableExpression, boolean isTemporary, Database db)
Create a view.- Parameters:
schema
- the schemaid
- the view idname
- the view namequerySQL
- the queryparameters
- the parameterscolumnTemplates
- the columnssession
- the sessionliteralsChecked
- whether literals in the query are checkedisTableExpression
- if this is a table expressionisTemporary
- whether the view is persisteddb
- the database- Returns:
- the view
-
createQueryColumnTemplateList
public static java.util.List<Column> createQueryColumnTemplateList(java.lang.String[] cols, Query theQuery, java.lang.String[] querySQLOutput)
Creates a list of column templates from a query (usually from WITH query, but could be any query)- Parameters:
cols
- - an optional list of column names (can be specified by WITH clause overriding usual select names)theQuery
- - the query object we want the column list forquerySQLOutput
- - array of length 1 to receive extra 'output' field in addition to return value - containing the SQL query of the Query object- Returns:
- a list of column object returned by withQuery
-
createShadowTableForRecursiveTableExpression
public static Table createShadowTableForRecursiveTableExpression(boolean isTemporary, SessionLocal targetSession, java.lang.String cteViewName, Schema schema, java.util.List<Column> columns, Database db)
Create a table for a recursive query.- Parameters:
isTemporary
- whether the table is persistedtargetSession
- the sessioncteViewName
- the nameschema
- the schemacolumns
- the columnsdb
- the database- Returns:
- the table
-
destroyShadowTableForRecursiveExpression
public static void destroyShadowTableForRecursiveExpression(boolean isTemporary, SessionLocal targetSession, Table recursiveTable)
Remove a table for a recursive query.- Parameters:
isTemporary
- whether the table is persistedtargetSession
- the sessionrecursiveTable
- the table
-
-