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, TableBase, 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.
static java.lang.String EXTERNAL_TABLE_ENGINE
          The table type name for external table engines.
protected  boolean isHidden
          Protected tables are not listed in the meta data and are excluded when using the SCRIPT command.
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
 
Constructor Summary
Table(Schema schema, int id, java.lang.String name, boolean persistIndexes, boolean persistData)
           
 
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, boolean create, java.lang.String indexComment)
          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 canReference()
          Check if this table can be referenced.
 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.
 java.util.ArrayList<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.
 void checkWritingAllowed()
          Tests if the table can be written.
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 fire(Session session, int type, boolean beforeAction)
          Fire the triggers for this table.
 void fireAfterRow(Session session, Row oldRow, Row newRow, boolean rollback)
          Fire all triggers that need to be called after a row is updated.
 boolean 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()
           
 java.util.ArrayList<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()
           
 java.util.ArrayList<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
 Value getDefaultValue(Session session, Column column)
          Get or generate a default value for the given column.
abstract  java.util.ArrayList<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.
 java.util.ArrayList<TableView> getViews()
           
 boolean hasSelectTrigger()
          Check whether this table has a select trigger.
abstract  boolean isDeterministic()
          Check if the table is deterministic.
 boolean isGlobalTemporary()
           
 boolean isHidden()
          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.
abstract  boolean isLockedExclusively()
          Check if this table is locked exclusively.
 boolean isLockedExclusivelyBy(Session session)
          Check if the table is exclusively locked by this session.
 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, 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, 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

EXTERNAL_TABLE_ENGINE

public static final java.lang.String EXTERNAL_TABLE_ENGINE
The table type name for external table engines.

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.

Constructor Detail

Table

public 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 interface: DbObject
Rename the object.

Specified by:
rename in interface DbObject
Overrides:
rename in class DbObjectBase
Parameters:
newName - the new name

lock

public abstract void lock(Session session,
                          boolean exclusive,
                          boolean force)
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:
SQLException - if a lock timeout occurred

close

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

Parameters:
session - the session

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,
                               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
indexType - the index type
create - whether this is a new index
indexComment - the comment
Returns:
the index

removeRow

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

Parameters:
session - the session
row - the row

truncate

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

Parameters:
session - the session

addRow

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

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

checkSupportAlter

public abstract void checkSupportAlter()
Check if this table supports ALTER TABLE.

Throws:
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)
Get the scan index to iterate through all rows.

Parameters:
session - the session
Returns:
the index

getUniqueIndex

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

Returns:
a unique index

getIndexes

public abstract java.util.ArrayList<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

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

Parameters:
session - the session
Returns:
the row count

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 java.util.ArrayList<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)

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(Session 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,
                       Session session,
                       RowList 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,...

getViews

public java.util.ArrayList<TableView> getViews()

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
Specified by:
removeChildrenAndResources in class DbObjectBase
Parameters:
session - the session

checkColumnIsNotReferenced

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

Parameters:
col - the column
Throws:
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)
Get the column with the given name.

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

getBestPlanItem

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

Parameters:
session - the session
masks - null means 'always false'
Returns:
the plan item

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()

validateConvertUpdateSequence

public void validateConvertUpdateSequence(Session session,
                                          Row row)
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

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

fire

public void fire(Session 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(Session 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)

fireAfterRow

public void fireAfterRow(Session 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

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)
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()

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

checkDeadlock

public java.util.ArrayList<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)
Compare two values with the current comparison mode. The values may be of different 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

getCompareMode

public CompareMode getCompareMode()

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.


getDefaultValue

public Value getDefaultValue(Session session,
                             Column column)
Get or generate a default value for the given column.

Parameters:
session - the session
column - the column
Returns:
the value

isHidden

public boolean isHidden()
Description copied from interface: 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.

Specified by:
isHidden in interface SchemaObject
Overrides:
isHidden in class SchemaObjectBase
Returns:
true if it is hidden