Package org.h2.table

Class Table

    • Field Detail

      • TYPE_CACHED

        public static final int TYPE_CACHED
        The table type that means this table is a regular persistent table.
        See Also:
        Constant Field Values
      • TYPE_MEMORY

        public static final int TYPE_MEMORY
        The table type that means this table is a regular persistent table.
        See Also:
        Constant Field Values
      • columns

        protected Column[] columns
        The columns of this table.
      • compareMode

        protected CompareMode compareMode
        The compare mode used for this table.
      • isHidden

        protected boolean isHidden
        Protected tables are not listed in the meta data and are excluded when using the SCRIPT command.
      • columnMap

        private final java.util.HashMap<java.lang.String,​Column> columnMap
      • persistIndexes

        private final boolean persistIndexes
      • persistData

        private final boolean persistData
      • constraints

        private java.util.ArrayList<Constraint> constraints
      • sequences

        private java.util.ArrayList<Sequence> sequences
      • dependentViews

        private final java.util.concurrent.CopyOnWriteArrayList<TableView> dependentViews
        views that depend on this table
      • synonyms

        private java.util.ArrayList<TableSynonym> synonyms
      • checkForeignKeyConstraints

        private boolean checkForeignKeyConstraints
        Is foreign key constraint checking enabled for this table.
      • onCommitDrop

        private boolean onCommitDrop
      • onCommitTruncate

        private boolean onCommitTruncate
      • nullRow

        private volatile Row nullRow
      • tableExpression

        private boolean tableExpression
    • Constructor Detail

      • Table

        protected Table​(Schema schema,
                        int id,
                        java.lang.String name,
                        boolean persistIndexes,
                        boolean persistData)
    • Method Detail

      • rename

        public void rename​(java.lang.String newName)
        Description copied from class: DbObject
        Rename the object.
        Overrides:
        rename in class DbObject
        Parameters:
        newName - the new name
      • isView

        public boolean isView()
      • lock

        public boolean lock​(SessionLocal session,
                            int lockType)
        Lock the table for the given session. This method waits until the lock is granted.
        Parameters:
        session - the session
        lockType - the type of lock
        Returns:
        true if the table was already exclusively locked by this session.
        Throws:
        DbException - if a lock timeout occurred
      • close

        public abstract void close​(SessionLocal session)
        Close the table object and flush changes.
        Parameters:
        session - the session
      • unlock

        public void unlock​(SessionLocal s)
        Release the lock for this session.
        Parameters:
        s - the session
      • addIndex

        public abstract 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 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
      • getRow

        public Row getRow​(SessionLocal session,
                          long key)
        Get the given row.
        Parameters:
        session - the session
        key - the primary key
        Returns:
        the row
      • isInsertable

        public boolean isInsertable()
        Returns whether this table is insertable.
        Returns:
        whether this table is insertable
      • removeRow

        public abstract void removeRow​(SessionLocal session,
                                       Row row)
        Remove a row from the table and all indexes.
        Parameters:
        session - the session
        row - the row
      • lockRow

        public Row lockRow​(SessionLocal session,
                           Row row)
        Locks row, preventing any updated to it, except from the session specified.
        Parameters:
        session - the session
        row - to lock
        Returns:
        locked row, or null if row does not exist anymore
      • truncate

        public abstract long truncate​(SessionLocal session)
        Remove all rows from the table and indexes.
        Parameters:
        session - the session
        Returns:
        number of removed rows, possibly including uncommitted rows
      • addRow

        public abstract void addRow​(SessionLocal session,
                                    Row row)
        Add a row to the table and all indexes.
        Parameters:
        session - the session
        row - the row
        Throws:
        DbException - if a constraint was violated
      • updateRow

        public void updateRow​(SessionLocal session,
                              Row oldRow,
                              Row newRow)
        Update a row to the table and all indexes.
        Parameters:
        session - the session
        oldRow - the row to update
        newRow - the row with updated values (_rowid_ suppose to be the same)
        Throws:
        DbException - if a constraint was violated
      • checkSupportAlter

        public abstract void checkSupportAlter()
        Check if this table supports ALTER TABLE.
        Throws:
        DbException - if it is not supported
      • getTableType

        public abstract TableType getTableType()
        Get the table type name
        Returns:
        the table type name
      • getSQLTableType

        public java.lang.String getSQLTableType()
        Return SQL table type for INFORMATION_SCHEMA.
        Returns:
        SQL table type for INFORMATION_SCHEMA
      • getScanIndex

        public abstract Index getScanIndex​(SessionLocal session)
        Get the scan index to iterate through all rows.
        Parameters:
        session - the session
        Returns:
        the index
      • getScanIndex

        public Index getScanIndex​(SessionLocal session,
                                  int[] masks,
                                  TableFilter[] filters,
                                  int filter,
                                  SortOrder sortOrder,
                                  AllColumnsForPlan allColumnsSet)
        Get the scan index for this 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
      • getIndexes

        public abstract java.util.ArrayList<Index> getIndexes()
        Get all indexes for this table.
        Returns:
        the list of indexes
      • getIndex

        public Index getIndex​(java.lang.String indexName)
        Get an index by name.
        Parameters:
        indexName - the index name to search for
        Returns:
        the found index
      • isLockedExclusively

        public boolean isLockedExclusively()
        Check if this table is locked exclusively.
        Returns:
        true if it is.
      • getMaxDataModificationId

        public abstract long getMaxDataModificationId()
        Get the last data modification id.
        Returns:
        the modification id
      • isDeterministic

        public abstract boolean isDeterministic()
        Check if the table is deterministic.
        Returns:
        true if it is
      • canGetRowCount

        public abstract boolean canGetRowCount​(SessionLocal session)
        Check if the row count can be retrieved quickly.
        Parameters:
        session - the session
        Returns:
        true if it can
      • canReference

        public boolean canReference()
        Check if this table can be referenced.
        Returns:
        true if it can
      • canDrop

        public abstract boolean canDrop()
        Check if this table can be dropped.
        Returns:
        true if it can
      • getRowCount

        public abstract long getRowCount​(SessionLocal session)
        Get the row count for this table.
        Parameters:
        session - the session
        Returns:
        the row count
      • getRowCountApproximation

        public abstract long getRowCountApproximation​(SessionLocal session)
        Get the approximated row count for this table.
        Parameters:
        session - the session
        Returns:
        the approximated row count
      • getDiskSpaceUsed

        public long getDiskSpaceUsed()
      • getRowIdColumn

        public Column getRowIdColumn()
        Get the row id column if this table has one.
        Returns:
        the row id column, or null
      • 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 class DbObject
        Parameters:
        table - the new table
        quotedName - the quoted name
        Returns:
        the SQL statement
      • isQueryComparable

        public boolean isQueryComparable()
        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.
        Returns:
        true if the table contains no query-comparable column
      • addDependencies

        public void addDependencies​(java.util.HashSet<DbObject> dependencies)
        Add all objects that this table depends on to the hash set.
        Parameters:
        dependencies - the current set of dependencies
      • 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 class DbObject
        Returns:
        the list of children, or null
      • setColumns

        protected void setColumns​(Column[] columns)
      • renameColumn

        public void renameColumn​(Column column,
                                 java.lang.String newName)
        Rename a column of this table.
        Parameters:
        column - the column to rename
        newName - the new column name
      • isLockedExclusivelyBy

        public boolean isLockedExclusivelyBy​(SessionLocal session)
        Check if the table is exclusively locked by this session.
        Parameters:
        session - the session
        Returns:
        true if it is
      • updateRows

        public void updateRows​(Prepared prepared,
                               SessionLocal session,
                               LocalResult rows)
        Update a list of rows in this table.
        Parameters:
        prepared - the prepared statement
        session - the session
        rows - a list of row pairs of the form old row, new row, old row, new row,...
      • getDependentViews

        public java.util.concurrent.CopyOnWriteArrayList<TableView> getDependentViews()
      • 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 class DbObject
        Parameters:
        session - the session
      • dropMultipleColumnsConstraintsAndIndexes

        public void dropMultipleColumnsConstraintsAndIndexes​(SessionLocal session,
                                                             java.util.ArrayList<Column> columnsToDrop)
        Check that these columns are not referenced by a multi-column constraint or multi-column index. If it is, an exception is thrown. Single-column references and indexes are dropped.
        Parameters:
        session - the session
        columnsToDrop - the columns to drop
        Throws:
        DbException - if the column is referenced by multi-column constraints or indexes
      • getRowFactory

        public RowFactory getRowFactory()
      • createRow

        public Row createRow​(Value[] data,
                             int memory)
        Create a new row for this table.
        Parameters:
        data - the values
        memory - the estimated memory usage in bytes
        Returns:
        the created row
      • getTemplateRow

        public Row getTemplateRow()
      • getTemplateSimpleRow

        public SearchRow getTemplateSimpleRow​(boolean singleColumn)
        Get a new simple row object.
        Parameters:
        singleColumn - if only one value need to be stored
        Returns:
        the simple row object
      • getNullRow

        public Row getNullRow()
      • getColumns

        public Column[] getColumns()
      • getType

        public int getType()
        Description copied from class: DbObject
        Get the object type.
        Specified by:
        getType in class DbObject
        Returns:
        the object type
      • getColumn

        public Column getColumn​(int index)
        Get the column at the given index.
        Parameters:
        index - the column index (0, 1,...)
        Returns:
        the column
      • getColumn

        public Column getColumn​(java.lang.String columnName)
        Get the column with the given name.
        Parameters:
        columnName - the column name
        Returns:
        the column
        Throws:
        DbException - if the column was not found
      • getColumn

        public Column getColumn​(java.lang.String columnName,
                                boolean ifExists)
        Get the column with the given name.
        Parameters:
        columnName - the column name
        ifExists - if (@code true) return null if column does not exist
        Returns:
        the column
        Throws:
        DbException - if the column was not found
      • findColumn

        public Column findColumn​(java.lang.String columnName)
        Get the column with the given name if it exists.
        Parameters:
        columnName - the column name, or null
        Returns:
        the column
      • doesColumnExist

        public boolean doesColumnExist​(java.lang.String columnName)
        Does the column with the given name exist?
        Parameters:
        columnName - the column name
        Returns:
        true if the column exists
      • getIdentityColumn

        public Column getIdentityColumn()
        Returns first identity column, or null.
        Returns:
        first identity column, or null
      • getBestPlanItem

        public PlanItem getBestPlanItem​(SessionLocal session,
                                        int[] masks,
                                        TableFilter[] filters,
                                        int filter,
                                        SortOrder sortOrder,
                                        AllColumnsForPlan allColumnsSet)
        Get the best plan for the given search mask.
        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
      • isIndexExcludedByHints

        private static boolean isIndexExcludedByHints​(IndexHints indexHints,
                                                      Index index)
      • findPrimaryKey

        public Index findPrimaryKey()
        Get the primary key index if there is one, or null if there is none.
        Returns:
        the primary key index or null
      • getPrimaryKey

        public Index getPrimaryKey()
      • convertInsertRow

        public void convertInsertRow​(SessionLocal session,
                                     Row row,
                                     java.lang.Boolean overridingSystem)
        Prepares the specified row for INSERT operation. Identity, default, and generated values are evaluated, all values are converted to target data types and validated. Base value of identity column is updated when required by compatibility mode.
        Parameters:
        session - the session
        overridingSystem - Boolean.TRUE for OVERRIDING SYSTEM VALUES, Boolean.FALSE for OVERRIDING USER VALUES, null if override clause is not specified
        row - the row
      • convertUpdateRow

        public void convertUpdateRow​(SessionLocal session,
                                     Row row,
                                     boolean fromTrigger)
        Prepares the specified row for UPDATE operation. Default and generated values are evaluated, all values are converted to target data types and validated. Base value of identity column is updated when required by compatibility mode.
        Parameters:
        session - the session
        row - the row
        fromTrigger - true if row was modified by INSERT or UPDATE trigger
      • remove

        private static void remove​(java.util.ArrayList<? extends DbObject> list,
                                   DbObject obj)
      • removeIndex

        public void removeIndex​(Index index)
        Remove the given index from the list.
        Parameters:
        index - the index to remove
      • removeDependentView

        public void removeDependentView​(TableView view)
        Remove the given view from the dependent views list.
        Parameters:
        view - the view to remove
      • removeSynonym

        public void removeSynonym​(TableSynonym synonym)
        Remove the given view from the list.
        Parameters:
        synonym - the synonym to remove
      • removeConstraint

        public void removeConstraint​(Constraint constraint)
        Remove the given constraint from the list.
        Parameters:
        constraint - the constraint to remove
      • removeSequence

        public final void removeSequence​(Sequence sequence)
        Remove a sequence from the table. Sequences are used as identity columns.
        Parameters:
        sequence - the sequence to remove
      • removeTrigger

        public void removeTrigger​(TriggerObject trigger)
        Remove the given trigger from the list.
        Parameters:
        trigger - the trigger to remove
      • addDependentView

        public void addDependentView​(TableView view)
        Add a view to this table.
        Parameters:
        view - the view to add
      • addSynonym

        public void addSynonym​(TableSynonym synonym)
        Add a synonym to this table.
        Parameters:
        synonym - the synonym to add
      • addConstraint

        public void addConstraint​(Constraint constraint)
        Add a constraint to the table.
        Parameters:
        constraint - the constraint to add
      • getConstraints

        public java.util.ArrayList<Constraint> getConstraints()
      • addSequence

        public void addSequence​(Sequence sequence)
        Add a sequence to this table.
        Parameters:
        sequence - the sequence to add
      • addTrigger

        public void addTrigger​(TriggerObject trigger)
        Add a trigger to this table.
        Parameters:
        trigger - the trigger to add
      • add

        private static <T> java.util.ArrayList<T> add​(java.util.ArrayList<T> list,
                                                      T obj)
      • fire

        public void fire​(SessionLocal session,
                         int type,
                         boolean beforeAction)
        Fire the triggers for this table.
        Parameters:
        session - the session
        type - the trigger type
        beforeAction - whether 'before' triggers should be called
      • hasSelectTrigger

        public boolean hasSelectTrigger()
        Check whether this table has a select trigger.
        Returns:
        true if it has
      • fireRow

        public boolean fireRow()
        Check if row based triggers or constraints are defined. In this case the fire after and before row methods need to be called.
        Returns:
        if there are any triggers or rows defined
      • fireBeforeRow

        public boolean fireBeforeRow​(SessionLocal session,
                                     Row oldRow,
                                     Row newRow)
        Fire all triggers that need to be called before a row is updated.
        Parameters:
        session - the session
        oldRow - the old data or null for an insert
        newRow - the new data or null for a delete
        Returns:
        true if no further action is required (for 'instead of' triggers)
      • fireConstraints

        private void fireConstraints​(SessionLocal session,
                                     Row oldRow,
                                     Row newRow,
                                     boolean before)
      • fireAfterRow

        public void fireAfterRow​(SessionLocal session,
                                 Row oldRow,
                                 Row newRow,
                                 boolean rollback)
        Fire all triggers that need to be called after a row is updated.
        Parameters:
        session - the session
        oldRow - the old data or null for an insert
        newRow - the new data or null for a delete
        rollback - when the operation occurred within a rollback
      • fireRow

        private boolean fireRow​(SessionLocal session,
                                Row oldRow,
                                Row newRow,
                                boolean beforeAction,
                                boolean rollback)
      • isGlobalTemporary

        public boolean isGlobalTemporary()
      • canTruncate

        public boolean canTruncate()
        Check if this table can be truncated.
        Returns:
        true if it can
      • setCheckForeignKeyConstraints

        public void setCheckForeignKeyConstraints​(SessionLocal session,
                                                  boolean enabled,
                                                  boolean checkExisting)
        Enable or disable foreign key constraint checking for this table.
        Parameters:
        session - the session
        enabled - true if checking should be enabled
        checkExisting - true if existing rows must be checked during this call
      • getCheckForeignKeyConstraints

        public boolean getCheckForeignKeyConstraints()
        Returns:
        is foreign key constraint checking enabled for this table.
      • getIndexForColumn

        public Index getIndexForColumn​(Column column,
                                       boolean needGetFirstOrLast,
                                       boolean needFindNext)
        Get the index that has the given column as the first element. This method returns null if no matching index is found.
        Parameters:
        column - the column
        needGetFirstOrLast - if the returned index must be able to do Index.canGetFirstOrLast()
        needFindNext - if the returned index must be able to do Index.findNext(SessionLocal, SearchRow, SearchRow)
        Returns:
        the index or null
      • getOnCommitDrop

        public boolean getOnCommitDrop()
      • setOnCommitDrop

        public void setOnCommitDrop​(boolean onCommitDrop)
      • getOnCommitTruncate

        public boolean getOnCommitTruncate()
      • setOnCommitTruncate

        public void setOnCommitTruncate​(boolean onCommitTruncate)
      • removeIndexOrTransferOwnership

        public void removeIndexOrTransferOwnership​(SessionLocal session,
                                                   Index index)
        If the index is still required by a constraint, transfer the ownership to it. Otherwise, the index is removed.
        Parameters:
        session - the session
        index - the index that is no longer required
      • removeColumnExpressionsDependencies

        public void removeColumnExpressionsDependencies​(SessionLocal session)
        Removes dependencies of column expressions, used for tables with circular dependencies.
        Parameters:
        session - the session
      • checkDeadlock

        public java.util.ArrayList<SessionLocal> checkDeadlock​(SessionLocal session,
                                                               SessionLocal clash,
                                                               java.util.Set<SessionLocal> visited)
        Check if a deadlock occurred. This method is called recursively. There is a circle if the session to be tested has already being visited. If this session is part of the circle (if it is the clash session), the method must return an empty object array. Once a deadlock has been detected, the methods must add the session to the list. If this session is not part of the circle, or if no deadlock is detected, this method returns null.
        Parameters:
        session - the session to be tested for
        clash - set with sessions already visited, and null when starting verification
        visited - set with sessions already visited, and null when starting verification
        Returns:
        an object array with the sessions involved in the deadlock, or null
      • isPersistIndexes

        public boolean isPersistIndexes()
      • isPersistData

        public boolean isPersistData()
      • compareValues

        public int compareValues​(CastDataProvider provider,
                                 Value a,
                                 Value b)
        Compare two values with the current comparison mode. The values may be of different type.
        Parameters:
        provider - the cast information provider
        a - the first value
        b - the second value
        Returns:
        0 if both values are equal, -1 if the first value is smaller, and 1 otherwise
      • checkWritingAllowed

        public void checkWritingAllowed()
        Tests if the table can be written. Usually, this depends on the database.checkWritingAllowed method, but some tables (eg. TableLink) overwrite this default behaviour.
      • isHidden

        public boolean isHidden()
        Description copied from class: SchemaObject
        Check whether this is a hidden object that doesn't appear in the meta data and in the script, and is not dropped on DROP ALL OBJECTS.
        Overrides:
        isHidden in class SchemaObject
        Returns:
        true if it is hidden
      • setHidden

        public void setHidden​(boolean hidden)
      • isRowLockable

        public boolean isRowLockable()
        Views, function tables, links, etc. do not support locks
        Returns:
        true if table supports row-level locks
      • setTableExpression

        public void setTableExpression​(boolean tableExpression)
      • isTableExpression

        public boolean isTableExpression()
      • getTriggers

        public java.util.ArrayList<TriggerObject> getTriggers()
        Return list of triggers.
        Returns:
        list of triggers