Package org.h2.table

Class TableView

All Implemented Interfaces:
HasSQL

public class TableView extends Table
A view is a virtual table that is defined by a query.
  • Field Details

    • ROW_COUNT_APPROXIMATION

      private static final long ROW_COUNT_APPROXIMATION
      See Also:
    • querySQL

      private String querySQL
    • tables

      private 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 Details

  • Method Details

    • replace

      public void replace(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 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
    • init

      private void init(String querySQL, ArrayList<Parameter> params, Column[] columnTemplates, SessionLocal session, boolean allowRecursive, boolean literalsChecked, boolean isTableExpression)
    • compileViewQuery

      private Query compileViewQuery(SessionLocal session, 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 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)
    • initColumnsAndTables

      private void initColumnsAndTables(SessionLocal session, boolean literalsChecked)
    • 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(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 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 String getDropSQL()
      Description copied from class: DbObject
      Construct a DROP ... SQL statement for this object.
      Overrides:
      getDropSQL in class DbObject
      Returns:
      the SQL statement
    • getCreateSQLForCopy

      public String getCreateSQLForCopy(Table table, 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 class Table
      Parameters:
      table - the new table
      quotedName - the quoted name
      Returns:
      the SQL statement
    • getCreateSQL

      public String getCreateSQL()
      Description copied from class: DbObject
      Construct the CREATE ... SQL statement for this object.
      Specified by:
      getCreateSQL in class DbObject
      Returns:
      the SQL statement
    • getCreateSQL

      public 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
    • getCreateSQL

      private String getCreateSQL(boolean orReplace, boolean force, String quotedName)
    • close

      public void close(SessionLocal session)
      Description copied from class: Table
      Close the table object and flush changes.
      Specified by:
      close in class Table
      Parameters:
      session - the session
    • addIndex

      public Index addIndex(SessionLocal session, String indexName, int indexId, IndexColumn[] cols, int uniqueColumnCount, IndexType indexType, boolean create, 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
      uniqueColumnCount - the count of unique columns
      indexType - the index type
      create - whether this is a new index
      indexComment - the comment
      Returns:
      the index
    • isInsertable

      public boolean isInsertable()
      Description copied from class: Table
      Returns whether this table is insertable.
      Overrides:
      isInsertable in class Table
      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.
      Specified by:
      removeRow in class Table
      Parameters:
      session - the session
      row - the row
    • addRow

      public void addRow(SessionLocal 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 long truncate(SessionLocal session)
      Description copied from class: Table
      Remove all rows from the table and indexes.
      Specified by:
      truncate in class Table
      Parameters:
      session - the session
      Returns:
      number of removed rows, possibly including uncommitted rows
    • getRowCount

      public long getRowCount(SessionLocal 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(SessionLocal session)
      Description copied from class: Table
      Check if the row count can be retrieved quickly.
      Specified by:
      canGetRowCount in class Table
      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.
      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(SessionLocal session)
      Description copied from class: DbObject
      Delete all dependent children objects and resources of this object.
      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 StringBuilder getSQL(StringBuilder builder, int sqlFlags)
      Description copied from interface: HasSQL
      Appends the SQL statement of this object to the specified builder.
      Specified by:
      getSQL in interface HasSQL
      Overrides:
      getSQL in class SchemaObject
      Parameters:
      builder - string builder
      sqlFlags - formatting flags
      Returns:
      the specified string builder
    • getQuery

      public 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 class Table
      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 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 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
    • 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, String name, Column[] columnTemplates, 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
      columnTemplates - column templates, or null
      query - the query
      topQuery - 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 class Table
      Parameters:
      session - the session
      Returns:
      the approximated row count
    • getParameterOffset

      public int getParameterOffset(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(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 class Table
      Returns:
      true if it is
    • setRecursiveResult

      public void setRecursiveResult(ResultInterface value)
    • getRecursiveResult

      public ResultInterface getRecursiveResult()
    • addDependencies

      public void addDependencies(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
    • isRecursiveQueryExceptionDetected

      private boolean isRecursiveQueryExceptionDetected(DbException exception)
      Does exception indicate query recursion?
    • getTables

      public List<Table> getTables()
    • createTableViewMaybeRecursive

      public static TableView createTableViewMaybeRecursive(Schema schema, int id, String name, String querySQL, ArrayList<Parameter> parameters, Column[] columnTemplates, SessionLocal 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 List<Column> createQueryColumnTemplateList(String[] cols, Query theQuery, 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, SessionLocal targetSession, String cteViewName, Schema schema, 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, SessionLocal targetSession, Table recursiveTable)
      Remove a table for a recursive query.
      Parameters:
      isTemporary - whether the table is persisted
      targetSession - the session
      recursiveTable - the table