org.h2.table
Class Table

java.lang.Object
  extended by org.h2.engine.DbObjectBase
      extended by org.h2.schema.SchemaObjectBase
          extended by org.h2.table.Table
All Implemented Interfaces:
DbObject, SchemaObject
Direct Known Subclasses:
FunctionTable, MetaTable, RangeTable, TableData, TableLink, TableView

public abstract class Table
extends SchemaObjectBase

This is the base class for most tables. A table contains a list of columns and a list of rows.


Field Summary
protected  Column[] columns
          The columns of this table.
protected  CompareMode compareMode
          The compare mode used for this table.
protected  int memoryPerRow
          The amount of memory required for a row if all values would be very small.
static java.lang.String SYSTEM_TABLE
          The table type name for system tables.
static java.lang.String TABLE
          The table type name for regular data tables.
static java.lang.String TABLE_LINK
          The table type name for linked tables.
static int TYPE_CACHED
          The table type that means this table is a regular persistent table.
static int TYPE_MEMORY
          The table type that means this table is a regular persistent table.
static java.lang.String VIEW
          The table type name for views.
 
Fields inherited from class org.h2.engine.DbObjectBase
comment, database, trace
 
Fields inherited from interface org.h2.engine.DbObject
AGGREGATE, COMMENT, CONSTANT, CONSTRAINT, FUNCTION_ALIAS, INDEX, RIGHT, ROLE, SCHEMA, SEQUENCE, SETTING, TABLE_OR_VIEW, TRIGGER, USER, USER_DATATYPE
 
Method Summary
 void addConstraint(Constraint constraint)
          Add a constraint to the table.
 void addDependencies(java.util.HashSet<DbObject> dependencies)
          Add all objects that this table depends on to the hash set.
abstract  Index addIndex(Session session, java.lang.String indexName, int indexId, IndexColumn[] cols, IndexType indexType, int headPos, java.lang.String comment)
          Create an index for this table
abstract  void addRow(Session session, Row row)
          Add a row to the table and all indexes.
 void addSequence(Sequence sequence)
          Add a sequence to this table.
 void addTrigger(TriggerObject trigger)
          Add a trigger to this table.
 void addView(TableView view)
          Add a view to this table.
abstract  boolean canDrop()
          Check if this table can be dropped.
abstract  boolean canGetRowCount()
          Check if the row count can be retrieved quickly.
 boolean canTruncate()
          Check if this table can be truncated.
 void checkColumnIsNotReferenced(Column col)
          Check that this column is not referenced by a referential constraint or multi-column index.
 ObjectArray<Session> checkDeadlock(Session session, Session clash, java.util.Set<Session> visited)
          Check if a deadlock occurred.
abstract  void checkSupportAlter()
          Check if this table supports ALTER TABLE.
abstract  void close(Session session)
          Close the table object and flush changes.
 int compareTypeSave(Value a, Value b)
          Compare two values with the current comparison mode.
 Index findPrimaryKey()
          Get the primary key index if there is one, or null if there is none.
 void fireAfter(Session session)
          Fire the after update triggers for this table.
 void fireAfterRow(Session session, Row oldRow, Row newRow)
          Fire all triggers that need to be called after a row is updated.
 void fireBefore(Session session)
          Fire the before update triggers for this table.
 void fireBeforeRow(Session session, Row oldRow, Row newRow)
          Fire all triggers that need to be called before a row is updated.
 boolean fireRow()
          Check if row based triggers or constraints are defined.
 PlanItem getBestPlanItem(Session session, int[] masks)
          Get the best plan for the given search mask.
 boolean getCheckForeignKeyConstraints()
           
 ObjectArray<DbObject> getChildren()
          Get the list of dependent children (for tables, this includes indexes and so on).
 Column getColumn(int index)
          Get the column at the given index.
 Column getColumn(java.lang.String columnName)
          Get the column with the given name.
 Column[] getColumns()
           
 CompareMode getCompareMode()
           
 ObjectArray<Constraint> getConstraints()
           
 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
abstract  ObjectArray<Index> getIndexes()
          Get all indexes for this table.
 Index getIndexForColumn(Column column, boolean first)
          Get the index that has the given column as the first element.
abstract  long getMaxDataModificationId()
          Get the last data modification id.
 boolean getOnCommitDrop()
           
 boolean getOnCommitTruncate()
           
 Index getPrimaryKey()
           
abstract  long getRowCount(Session session)
          Get the row count for this table.
abstract  long getRowCountApproximation()
          Get the approximated row count for this table.
abstract  Index getScanIndex(Session session)
          Get the scan index to iterate through all rows.
abstract  java.lang.String getTableType()
          Get the table type name
 Row getTemplateRow()
           
 SearchRow getTemplateSimpleRow(boolean singleColumn)
          Get a new simple row object.
 int getType()
          Get the object type.
abstract  Index getUniqueIndex()
          Get any unique index for this table if one exists.
abstract  boolean isDeterministic()
          Check if the table is deterministic.
 boolean isGlobalTemporary()
           
abstract  boolean isLockedExclusively()
          Check if this table is locked exclusively.
 boolean isPersistData()
           
 boolean isPersistIndexes()
           
abstract  void lock(Session session, boolean exclusive, boolean force)
          Lock the table for the given session.
 void removeChildrenAndResources(Session session)
          Remove all dependent objects and free all resources (files, blocks in files) of this object.
 void removeConstraint(Constraint constraint)
          Remove the given constraint from the list.
 void removeIndex(Index index)
          Remove the given index from the list.
 void removeIndexOrTransferOwnership(Session session, Index index)
          If the index is still required by a constraint, transfer the ownership to it.
abstract  void removeRow(Session session, Row row)
          Remove a row from the table and all indexes.
 void removeSequence(Session session, Sequence sequence)
          Remove a sequence from the table.
 void removeTrigger(TriggerObject trigger)
          Remove the given trigger from the list.
 void rename(java.lang.String newName)
          Rename the object.
 void renameColumn(Column column, java.lang.String newName)
          Rename a column of this table.
 void setCheckForeignKeyConstraints(Session session, boolean enabled, boolean checkExisting)
          Enable or disable foreign key constraint checking for this table.
protected  void setColumns(Column[] columns)
           
 void setOnCommitDrop(boolean onCommitDrop)
           
 void setOnCommitTruncate(boolean onCommitTruncate)
           
abstract  void truncate(Session session)
          Remove all rows from the table and indexes.
abstract  void unlock(Session s)
          Release the lock for this session.
 void updateRows(Prepared prepared, Session session, RowList rows)
          Update a list of rows in this table.
 void validateConvertUpdateSequence(Session session, Row row)
          Validate all values in this row, convert the values if required, and update the sequence values if required.
 
Methods inherited from class org.h2.schema.SchemaObjectBase
getSchema, getSQL, initSchemaObjectBase
 
Methods inherited from class org.h2.engine.DbObjectBase
checkRename, getComment, getCreateSQL, getDatabase, getDropSQL, getHeadPos, getId, getModificationId, getName, initDbObjectBase, invalidate, isTemporary, 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
checkRename, getComment, getCreateSQL, getDatabase, getDropSQL, getHeadPos, getId, getModificationId, getName, isTemporary, setComment, setModified, setTemporary
 

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

TABLE_LINK

public static final java.lang.String TABLE_LINK
The table type name for linked tables.

See Also:
Constant Field Values

SYSTEM_TABLE

public static final java.lang.String SYSTEM_TABLE
The table type name for system tables.

See Also:
Constant Field Values

TABLE

public static final java.lang.String TABLE
The table type name for regular data tables.

See Also:
Constant Field Values

VIEW

public static final java.lang.String VIEW
The table type name for views.

See Also:
Constant Field Values

columns

protected Column[] columns
The columns of this table.


memoryPerRow

protected int memoryPerRow
The amount of memory required for a row if all values would be very small.


compareMode

protected CompareMode compareMode
The compare mode used for this table.

Method Detail

rename

public void rename(java.lang.String newName)
            throws java.sql.SQLException
Description copied from interface: DbObject
Rename the object.

Specified by:
rename in interface DbObject
Overrides:
rename in class DbObjectBase
Parameters:
newName - the new name
Throws:
java.sql.SQLException

lock

public abstract void lock(Session session,
                          boolean exclusive,
                          boolean force)
                   throws java.sql.SQLException
Lock the table for the given session. This method waits until the lock is granted.

Parameters:
session - the session
exclusive - true for write locks, false for read locks
force - lock even in the MVCC mode
Throws:
java.sql.SQLException - if a lock timeout occurred

close

public abstract void close(Session session)
                    throws java.sql.SQLException
Close the table object and flush changes.

Parameters:
session - the session
Throws:
java.sql.SQLException

unlock

public abstract void unlock(Session s)
Release the lock for this session.

Parameters:
s - the session

addIndex

public abstract Index addIndex(Session session,
                               java.lang.String indexName,
                               int indexId,
                               IndexColumn[] cols,
                               IndexType indexType,
                               int headPos,
                               java.lang.String comment)
                        throws java.sql.SQLException
Create an index for this table

Parameters:
session - the session
indexName - the name of the index
indexId - the id
cols - the index columns
indexType - the index type
headPos - the position of the head (if the index already exists)
comment - the comment
Returns:
the index
Throws:
java.sql.SQLException

removeRow

public abstract void removeRow(Session session,
                               Row row)
                        throws java.sql.SQLException
Remove a row from the table and all indexes.

Parameters:
session - the session
row - the row
Throws:
java.sql.SQLException

truncate

public abstract void truncate(Session session)
                       throws java.sql.SQLException
Remove all rows from the table and indexes.

Parameters:
session - the session
Throws:
java.sql.SQLException

addRow

public abstract void addRow(Session session,
                            Row row)
                     throws java.sql.SQLException
Add a row to the table and all indexes.

Parameters:
session - the session
row - the row
Throws:
java.sql.SQLException - if a constraint was violated

checkSupportAlter

public abstract void checkSupportAlter()
                                throws java.sql.SQLException
Check if this table supports ALTER TABLE.

Throws:
java.sql.SQLException - if it is not supported

getTableType

public abstract java.lang.String getTableType()
Get the table type name

Returns:
the table type name

getScanIndex

public abstract Index getScanIndex(Session session)
                            throws java.sql.SQLException
Get the scan index to iterate through all rows.

Parameters:
session - the session
Returns:
the index
Throws:
java.sql.SQLException

getUniqueIndex

public abstract Index getUniqueIndex()
Get any unique index for this table if one exists.

Returns:
a unique index

getIndexes

public abstract ObjectArray<Index> getIndexes()
Get all indexes for this table.

Returns:
the list of indexes

isLockedExclusively

public abstract 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()
Check if the row count can be retrieved quickly.

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(Session session)
                          throws java.sql.SQLException
Get the row count for this table.

Parameters:
session - the session
Returns:
the row count
Throws:
java.sql.SQLException

getRowCountApproximation

public abstract long getRowCountApproximation()
Get the approximated row count for this table.

Returns:
the approximated row count

getCreateSQLForCopy

public java.lang.String getCreateSQLForCopy(Table table,
                                            java.lang.String quotedName)
Description copied from class: DbObjectBase
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
Specified by:
getCreateSQLForCopy in class DbObjectBase
Parameters:
table - the new table name
quotedName - the new quoted name
Returns:
the SQL statement

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 ObjectArray<DbObject> getChildren()
Description copied from interface: DbObject
Get the list of dependent children (for tables, this includes indexes and so on).

Specified by:
getChildren in interface DbObject
Overrides:
getChildren in class DbObjectBase
Returns:
the list of children

setColumns

protected void setColumns(Column[] columns)
                   throws java.sql.SQLException
Throws:
java.sql.SQLException

renameColumn

public void renameColumn(Column column,
                         java.lang.String newName)
                  throws java.sql.SQLException
Rename a column of this table.

Parameters:
column - the column to rename
newName - the new column name
Throws:
java.sql.SQLException

updateRows

public void updateRows(Prepared prepared,
                       Session session,
                       RowList rows)
                throws java.sql.SQLException
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,...
Throws:
java.sql.SQLException

removeChildrenAndResources

public void removeChildrenAndResources(Session session)
                                throws java.sql.SQLException
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
Specified by:
removeChildrenAndResources in class DbObjectBase
Parameters:
session - the session
Throws:
java.sql.SQLException

checkColumnIsNotReferenced

public void checkColumnIsNotReferenced(Column col)
                                throws java.sql.SQLException
Check that this column is not referenced by a referential constraint or multi-column index.

Parameters:
col - the column
Throws:
java.sql.SQLException - if the column is referenced

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

getColumns

public Column[] getColumns()

getType

public int getType()
Description copied from class: DbObjectBase
Get the object type.

Specified by:
getType in interface DbObject
Specified by:
getType in class DbObjectBase
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)
                 throws java.sql.SQLException
Get the column with the given name.

Parameters:
columnName - the column name
Returns:
the column
Throws:
java.sql.SQLException - if the column was not found

getBestPlanItem

public PlanItem getBestPlanItem(Session session,
                                int[] masks)
                         throws java.sql.SQLException
Get the best plan for the given search mask.

Parameters:
session - the session
masks - null means 'always false'
Returns:
the plan item
Throws:
java.sql.SQLException

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()
                    throws java.sql.SQLException
Throws:
java.sql.SQLException

validateConvertUpdateSequence

public void validateConvertUpdateSequence(Session session,
                                          Row row)
                                   throws java.sql.SQLException
Validate all values in this row, convert the values if required, and update the sequence values if required. This call will also set the default values if required and set the computed column if there are any.

Parameters:
session - the session
row - the row
Throws:
java.sql.SQLException

removeIndex

public void removeIndex(Index index)
Remove the given index from the list.

Parameters:
index - the index to remove

removeConstraint

public void removeConstraint(Constraint constraint)
Remove the given constraint from the list.

Parameters:
constraint - the constraint to remove

removeSequence

public void removeSequence(Session session,
                           Sequence sequence)
Remove a sequence from the table. Sequences are used as identity columns.

Parameters:
session - the session
sequence - the sequence to remove

removeTrigger

public void removeTrigger(TriggerObject trigger)
Remove the given trigger from the list.

Parameters:
trigger - the trigger to remove

addView

public void addView(TableView view)
Add a view to this table.

Parameters:
view - the view to add

addConstraint

public void addConstraint(Constraint constraint)
Add a constraint to the table.

Parameters:
constraint - the constraint to add

getConstraints

public ObjectArray<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

fireBefore

public void fireBefore(Session session)
                throws java.sql.SQLException
Fire the before update triggers for this table.

Parameters:
session - the session
Throws:
java.sql.SQLException

fireAfter

public void fireAfter(Session session)
               throws java.sql.SQLException
Fire the after update triggers for this table.

Parameters:
session - the session
Throws:
java.sql.SQLException

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 void fireBeforeRow(Session session,
                          Row oldRow,
                          Row newRow)
                   throws java.sql.SQLException
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
Throws:
java.sql.SQLException

fireAfterRow

public void fireAfterRow(Session session,
                         Row oldRow,
                         Row newRow)
                  throws java.sql.SQLException
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
Throws:
java.sql.SQLException

isGlobalTemporary

public boolean isGlobalTemporary()

canTruncate

public boolean canTruncate()
Check if this table can be truncated.

Returns:
true if it can

setCheckForeignKeyConstraints

public void setCheckForeignKeyConstraints(Session session,
                                          boolean enabled,
                                          boolean checkExisting)
                                   throws java.sql.SQLException
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
Throws:
java.sql.SQLException

getCheckForeignKeyConstraints

public boolean getCheckForeignKeyConstraints()

getIndexForColumn

public Index getIndexForColumn(Column column,
                               boolean first)
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
first - if the min value should be returned
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(Session session,
                                           Index index)
                                    throws java.sql.SQLException
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
Throws:
java.sql.SQLException

checkDeadlock

public ObjectArray<Session> checkDeadlock(Session session,
                                          Session clash,
                                          java.util.Set<Session> 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()

compareTypeSave

public int compareTypeSave(Value a,
                           Value b)
                    throws java.sql.SQLException
Compare two values with the current comparison mode. The values must be of the same type.

Parameters:
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
Throws:
java.sql.SQLException

getCompareMode

public CompareMode getCompareMode()