org.h2.table

## Class TableView

- java.lang.Object
- - org.h2.engine.DbObjectBase
  - - org.h2.schema.SchemaObjectBase
    - - org.h2.table.Table
      - - org.h2.table.TableView

All Implemented Interfaces:
  DbObject, SchemaObject

    public class TableView
    extends Table

A view is a virtual table that is defined by a query.

### Field Summary

### Fields inherited from class org.h2.table.Table

`columns, compareMode, isHidden, TYPE_CACHED, TYPE_MEMORY`

### Fields inherited from class org.h2.engine.DbObjectBase

`comment, database, trace`

### Fields inherited from interface org.h2.engine.DbObject

`AGGREGATE, COMMENT, CONSTANT, CONSTRAINT, DOMAIN, FUNCTION_ALIAS, INDEX, RIGHT, ROLE, SCHEMA, SEQUENCE, SETTING, SYNONYM, TABLE_OR_VIEW, TRIGGER, USER`

### Constructor Summary

Constructors
Constructor and Description
`TableView(Schema schema, int id, java.lang.String name, java.lang.String querySQL, java.util.ArrayList<Parameter> params, Column[] columnTemplates, Session session, boolean allowRecursive, boolean literalsChecked, boolean isTableExpression, boolean isTemporary)`

### Method Summary

Methods
`void` `addDependencies(java.util.HashSet<DbObject> dependencies)` Add all objects that this table depends on to the hash set.
`Index` `addIndex(Session session, java.lang.String indexName, int indexId, IndexColumn[] cols, IndexType indexType, boolean create, java.lang.String indexComment)` Create an index for this table
`void` `addRow(Session session, Row row)` Add a row to the table and all indexes.
`boolean` `canDrop()` Check if this table can be dropped.
`boolean` `canGetRowCount()` Check if the row count can be retrieved quickly.
`boolean` `canReference()` Check if this table can be referenced.
`void` `checkRename()` Check if this object can be renamed.
`void` `checkSupportAlter()` Check if this table supports ALTER TABLE.
`static void` `clearIndexCaches(Database database)` Clear the cached indexes for all sessions.
`void` `close(Session session)` Close the table object and flush changes.
`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, Session 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, Session session, boolean literalsChecked, boolean isTableExpression, boolean isTemporary, Database db)` Create a view.
`static TableView` `createTempView(Session session, User owner, java.lang.String name, Query query, Query topQuery)` Create a temporary view out of the given query.
`static void` `destroyShadowTableForRecursiveExpression(boolean isTemporary, Session targetSession, Table recursiveTable)` Remove a table for a recursive query.
`PlanItem` `getBestPlanItem(Session session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder, AllColumnsForPlan allColumnsSet)` Get the best plan for the given search mask.
`java.lang.String` `getCreateSQL()` Build a SQL statement to re-create this object.
`java.lang.String` `getCreateSQL(boolean orReplace, boolean force)` Generate "CREATE" SQL statement for the view.
`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 table
`long` `getDiskSpaceUsed()`
`java.lang.String` `getDropSQL()` Build a SQL statement to drop this object.
`java.util.ArrayList<Index>` `getIndexes()` Get all indexes for this table.
`long` `getMaxDataModificationId()` Get the last data modification id.
`User` `getOwner()`
`int` `getParameterOffset(java.util.ArrayList<Parameter> additionalParameters)` Get the index of the first parameter.
`java.lang.String` `getQuery()`
`ResultInterface` `getRecursiveResult()`
`long` `getRowCount(Session session)` Get the row count for this table.
`long` `getRowCountApproximation()` Get the approximated row count for this table.
`Index` `getScanIndex(Session session)` Get the scan index to iterate through all rows.
`Index` `getScanIndex(Session 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, boolean alwaysQuote)` Appends the SQL name of this object (may be quoted) to the specified builder.
`java.util.List<Table>` `getTables()`
`TableType` `getTableType()` Get the table type name
`Query` `getTopQuery()`
`Index` `getUniqueIndex()` Get any unique index for this table if one exists.
`boolean` `isDeterministic()` Check if the table is deterministic.
`boolean` `isInvalid()` Check if this view is currently invalid.
`boolean` `isLockedExclusively()` Check if this table is locked exclusively.
`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.
`boolean` `isView()`
`boolean` `lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc)` Lock the table for the given session.
`DbException` `recompile(Session session, boolean force, boolean clearIndexCache)` Re-compile the view query and all views that depend on this object.
`void` `removeChildrenAndResources(Session session)` Remove all dependent objects and free all resources (files, blocks in files) of this object.
`void` `removeRow(Session session, Row row)` Remove a row from the table and all indexes.
`void` `replace(java.lang.String querySQL, Column[] newColumnTemplates, Session session, boolean recursive, boolean force, boolean literalsChecked)` Try to replace the SQL statement of the view and re-compile this and all dependent views.
`void` `setRecursiveResult(ResultInterface value)`
`void` `truncate(Session session)` Remove all rows from the table and indexes.
`void` `unlock(Session s)` Release the lock for this session.

### Methods inherited from class org.h2.table.Table

`addConstraint, addDependentView, addSequence, addSynonym, addTrigger, canTruncate, checkDeadlock, checkWritingAllowed, compareValues, createRow, doesColumnExist, dropMultipleColumnsConstraintsAndIndexes, findPrimaryKey, fire, fireAfterRow, fireBeforeRow, fireRow, getCheckForeignKeyConstraints, getChildren, getColumn, getColumn, getColumns, getCompareMode, getConstraints, getDefaultValue, getDependentViews, getIndex, getIndexForColumn, getOnCommitDrop, getOnCommitTruncate, getOnUpdateValue, getPrimaryKey, getRow, getRowIdColumn, getTemplateRow, getTemplateSimpleRow, getType, hasSelectTrigger, isGlobalTemporary, isHidden, isLockedExclusivelyBy, isMVStore, isPersistData, isPersistIndexes, isTableExpression, lockRow, removeConstraint, removeDependentView, removeIndex, removeIndexOrTransferOwnership, removeSequence, removeSynonym, removeTrigger, rename, renameColumn, setCheckForeignKeyConstraints, setColumns, setHidden, setOnCommitDrop, setOnCommitTruncate, setTableExpression, updateRow, updateRows, validateConvertUpdateSequence`

### Methods inherited from class org.h2.schema.SchemaObjectBase

`getSchema, getSQL`

### Methods inherited from class org.h2.engine.DbObjectBase

`getComment, 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.engine.DbObject

`getComment, getDatabase, getId, getName, isTemporary, setComment, setTemporary`

### Constructor Detail

#### TableView

    public TableView(Schema schema,
             int id,
             java.lang.String name,
             java.lang.String querySQL,
             java.util.ArrayList<Parameter> params,
             Column[] columnTemplates,
             Session session,
             boolean allowRecursive,
             boolean literalsChecked,
             boolean isTableExpression,
             boolean isTemporary)

### Method Detail

#### replace

    public void replace(java.lang.String querySQL,
               Column[] newColumnTemplates,
               Session 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 statement
  `newColumnTemplates` - the columns
  `session` - the session
  `recursive` - whether this is a recursive view
  `force` - if errors should be ignored
  `literalsChecked` - if literals have been checked

#### recompile

    public DbException recompile(Session session,
                        boolean force,
                        boolean clearIndexCache)

Re-compile the view query and all views that depend on this object.

Parameters:
  `session` - the session
  `force` - if exceptions should be ignored
  `clearIndexCache` - 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)

#### isView

    public boolean isView()

Overrides:
  `isView` in class `Table`

#### isInvalid

    public boolean isInvalid()

Check if this view is currently invalid.

Returns:
  true if it is

#### getBestPlanItem

    public PlanItem getBestPlanItem(Session 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 class `Table`
Parameters:
  `session` - the session
  `masks` - per-column comparison bit masks, null means 'always false', see constants in IndexCondition
  `filters` - all joined table filters
  `filter` - the current table filter index
  `sortOrder` - the sort order
  `allColumnsSet` - 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 class `Table`
Returns:
  true if the table contains no query-comparable column

#### getTopQuery

    public Query getTopQuery()

#### getDropSQL

    public java.lang.String getDropSQL()

Description copied from class: `DbObjectBase`
Build a SQL statement to drop this object.

Specified by:
  `getDropSQL` in interface `DbObject`
Specified by:
  `getDropSQL` in class `DbObjectBase`
Returns:
  the SQL statement

#### getCreateSQLForCopy

    public java.lang.String getCreateSQLForCopy(Table table,
                                       java.lang.String quotedName)

Description copied from interface: `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 interface `DbObject`
Overrides:
  `getCreateSQLForCopy` in class `Table`
Parameters:
  `table` - the new table
  `quotedName` - the quoted name
Returns:
  the SQL statement

#### getCreateSQL

    public java.lang.String getCreateSQL()

Description copied from class: `DbObjectBase`
Build a SQL statement to re-create this object.

Specified by:
  `getCreateSQL` in interface `DbObject`
Specified by:
  `getCreateSQL` in class `DbObjectBase`
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 clause
  `force` - if true, then include the FORCE clause
Returns:
  the SQL statement

#### checkRename

    public void checkRename()

Description copied from class: `DbObjectBase`
Check if this object can be renamed. System objects may not be renamed.

Specified by:
  `checkRename` in interface `DbObject`
Specified by:
  `checkRename` in class `DbObjectBase`

#### lock

    public boolean lock(Session session,
               boolean exclusive,
               boolean forceLockEvenInMvcc)

Description copied from class: `Table`
Lock the table for the given session. This method waits until the lock is granted.

Specified by:
  `lock` in class `Table`
Parameters:
  `session` - the session
  `exclusive` - true for write locks, false for read locks
  `forceLockEvenInMvcc` - lock even in the MVCC mode
Returns:
  true if the table was already exclusively locked by this session.

#### close

    public void close(Session session)

Description copied from class: `Table`
Close the table object and flush changes.

Specified by:
  `close` in class `Table`
Parameters:
  `session` - the session

#### unlock

    public void unlock(Session s)

Description copied from class: `Table`
Release the lock for this session.

Specified by:
  `unlock` in class `Table`
Parameters:
  `s` - the session

#### isLockedExclusively

    public boolean isLockedExclusively()

Description copied from class: `Table`
Check if this table is locked exclusively.

Specified by:
  `isLockedExclusively` in class `Table`
Returns:
  true if it is.

#### addIndex

    public Index addIndex(Session session,
                 java.lang.String indexName,
                 int indexId,
                 IndexColumn[] cols,
                 IndexType indexType,
                 boolean create,
                 java.lang.String indexComment)

Description copied from class: `Table`
Create an index for this table

Specified by:
  `addIndex` in class `Table`
Parameters:
  `session` - the session
  `indexName` - the name of the index
  `indexId` - the id
  `cols` - the index columns
  `indexType` - the index type
  `create` - whether this is a new index
  `indexComment` - the comment
Returns:
  the index

#### removeRow

    public void removeRow(Session session,
                 Row row)

Description copied from class: `Table`
Remove a row from the table and all indexes.

Specified by:
  `removeRow` in class `Table`
Parameters:
  `session` - the session
  `row` - the row

#### addRow

    public void addRow(Session session,
              Row row)

Description copied from class: `Table`
Add a row to the table and all indexes.

Specified by:
  `addRow` in class `Table`
Parameters:
  `session` - the session
  `row` - the row

#### checkSupportAlter

    public void checkSupportAlter()

Description copied from class: `Table`
Check if this table supports ALTER TABLE.

Specified by:
  `checkSupportAlter` in class `Table`

#### truncate

    public void truncate(Session session)

Description copied from class: `Table`
Remove all rows from the table and indexes.

Specified by:
  `truncate` in class `Table`
Parameters:
  `session` - the session

#### getRowCount

    public long getRowCount(Session session)

Description copied from class: `Table`
Get the row count for this table.

Specified by:
  `getRowCount` in class `Table`
Parameters:
  `session` - the session
Returns:
  the row count

#### canGetRowCount

    public boolean canGetRowCount()

Description copied from class: `Table`
Check if the row count can be retrieved quickly.

Specified by:
  `canGetRowCount` in class `Table`
Returns:
  true if it can

#### canDrop

    public boolean canDrop()

Description copied from class: `Table`
Check if this table can be dropped.

Specified by:
  `canDrop` in class `Table`
Returns:
  true if it can

#### getTableType

    public TableType getTableType()

Description copied from class: `Table`
Get the table type name

Specified by:
  `getTableType` in class `Table`
Returns:
  the table type name

#### removeChildrenAndResources

    public void removeChildrenAndResources(Session session)

Description copied from class: `DbObjectBase`
Remove all dependent objects and free all resources (files, blocks in files) of this object.

Specified by:
  `removeChildrenAndResources` in interface `DbObject`
Overrides:
  `removeChildrenAndResources` in class `Table`
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,
                                 boolean alwaysQuote)

Description copied from interface: `DbObject`
Appends the SQL name of this object (may be quoted) to the specified builder.

Specified by:
  `getSQL` in interface `DbObject`
Overrides:
  `getSQL` in class `SchemaObjectBase`
Parameters:
  `builder` - string builder
  `alwaysQuote` - quote all identifiers
Returns:
  the specified string builder

#### getQuery

    public java.lang.String getQuery()

#### getScanIndex

    public Index getScanIndex(Session session)

Description copied from class: `Table`
Get the scan index to iterate through all rows.

Specified by:
  `getScanIndex` in class `Table`
Parameters:
  `session` - the session
Returns:
  the index

#### getScanIndex

    public Index getScanIndex(Session 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 class `Table`
Parameters:
  `session` - the session
  `masks` - the search mask
  `filters` - the table filters
  `filter` - the filter index
  `sortOrder` - the sort order
  `allColumnsSet` - 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 class `Table`
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 class `Table`
Returns:
  the list of indexes

#### getMaxDataModificationId

    public long getMaxDataModificationId()

Description copied from class: `Table`
Get the last data modification id.

Specified by:
  `getMaxDataModificationId` in class `Table`
Returns:
  the modification id

#### getUniqueIndex

    public Index getUniqueIndex()

Description copied from class: `Table`
Get any unique index for this table if one exists.

Specified by:
  `getUniqueIndex` in class `Table`
Returns:
  a unique index

#### getOwner

    public User getOwner()

#### createTempView

    public static TableView createTempView(Session session,
                           User owner,
                           java.lang.String name,
                           Query query,
                           Query topQuery)

Create a temporary view out of the given query.

Parameters:
  `session` - the session
  `owner` - the owner of the query
  `name` - the view name
  `query` - the query
  `topQuery` - the top level query
Returns:
  the view table

#### getRowCountApproximation

    public long getRowCountApproximation()

Description copied from class: `Table`
Get the approximated row count for this table.

Specified by:
  `getRowCountApproximation` in class `Table`
Returns:
  the approximated row count

#### getDiskSpaceUsed

    public long getDiskSpaceUsed()

Specified by:
  `getDiskSpaceUsed` in class `Table`

#### 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

#### isRecursive

    public boolean isRecursive()

#### isDeterministic

    public boolean isDeterministic()

Description copied from class: `Table`
Check if the table is deterministic.

Specified by:
  `isDeterministic` in class `Table`
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 class `Table`
Parameters:
  `dependencies` - the current set of dependencies

#### isRecursiveQueryDetected

    public boolean isRecursiveQueryDetected()

Was query recursion detected during compiling.

Returns:
  true if yes

#### 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,
                                          Session session,
                                          boolean literalsChecked,
                                          boolean isTableExpression,
                                          boolean isTemporary,
                                          Database db)

Create a view.

Parameters:
  `schema` - the schema
  `id` - the view id
  `name` - the view name
  `querySQL` - the query
  `parameters` - the parameters
  `columnTemplates` - the columns
  `session` - the session
  `literalsChecked` - whether literals in the query are checked
  `isTableExpression` - if this is a table expression
  `isTemporary` - whether the view is persisted
  `db` - 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 for
  `querySQLOutput` - - 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,
                                                     Session 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 persisted
  `targetSession` - the session
  `cteViewName` - the name
  `schema` - the schema
  `columns` - the columns
  `db` - the database
Returns:
  the table

#### destroyShadowTableForRecursiveExpression

    public static void destroyShadowTableForRecursiveExpression(boolean isTemporary,
                                                Session targetSession,
                                                Table recursiveTable)

Remove a table for a recursive query.

Parameters:
  `isTemporary` - whether the table is persisted
  `targetSession` - the session
  `recursiveTable` - the table