Package org.h2.table
Class Column
- java.lang.Object
-
- org.h2.table.Column
-
- All Implemented Interfaces:
ColumnTemplate,HasSQL,Typed
public final class Column extends java.lang.Object implements HasSQL, Typed, ColumnTemplate
This class represents a column in a table.
-
-
Field Summary
Fields Modifier and Type Field Description private intcolumnIdprivate java.lang.Stringcommentprivate ExpressiondefaultExpressionprivate booleandefaultOnNullprivate Domaindomainprivate GeneratedColumnResolvergeneratedTableFilterprivate SequenceOptionsidentityOptionsprivate booleanisGeneratedAlwaysprivate java.lang.Stringnamestatic intNOT_NULLABLEThis column is not nullable.private booleannullablestatic intNULLABLEThis column is nullable.static intNULLABLE_UNKNOWNIt is not know whether this column is nullable.private ExpressiononUpdateExpressionprivate booleanprimaryKeyprivate booleanrowIdstatic java.lang.StringROWIDThe name of the rowid pseudo column.private intselectivityprivate Sequencesequenceprivate Tabletableprivate TypeInfotypeprivate booleanvisible-
Fields inherited from interface org.h2.util.HasSQL
ADD_PLAN_INFORMATION, DEFAULT_SQL_FLAGS, NO_CASTS, QUOTE_ONLY_WHEN_REQUIRED, REPLACE_LOBS_FOR_TRACE, TRACE_SQL_FLAGS
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Valueconvert(CastDataProvider provider, Value v)Convert a value to this column's type without precision and scale checks.voidcopy(Column source)Copy the data of the source column into the current column.booleanequals(java.lang.Object o)ColumngetClone()intgetColumnId()java.lang.StringgetComment()java.lang.StringgetCreateSQL()java.lang.StringgetCreateSQL(boolean forMeta)Get this columns part of CREATE TABLE SQL statement.private java.lang.StringgetCreateSQL(java.lang.StringBuilder builder, boolean forMeta)java.lang.StringgetCreateSQLWithoutName()private DbExceptiongetDataConversionError(Value value, DbException cause)ExpressiongetDefaultExpression()private ValuegetDefaultOrGenerated(SessionLocal session, Row row)java.lang.StringgetDefaultSQL()DomaingetDomain()ExpressiongetEffectiveDefaultExpression()ExpressiongetEffectiveOnUpdateExpression()SequenceOptionsgetIdentityOptions()Returns identity column options, ornullif sequence was already created or this column is not an identity column.java.lang.StringgetName()ExpressiongetOnUpdateExpression()java.lang.StringgetOnUpdateSQL()intgetSelectivity()Get the selectivity of the column.SequencegetSequence()java.lang.StringgetSQL(int sqlFlags)Get the SQL statement of this expression.java.lang.StringBuildergetSQL(java.lang.StringBuilder builder, int sqlFlags)Appends the SQL statement of this object to the specified builder.java.lang.StringBuildergetSQLWithTable(java.lang.StringBuilder builder, int sqlFlags)Appends the table name and column name to the specified builder.TablegetTable()TypeInfogetType()Returns the data type.booleangetVisible()inthashCode()booleanhasIdentityOptions()Whether the column has any identity options.voidinitializeSequence(SessionLocal session, Schema schema, int id, boolean temporary)Initialize the sequence for this column.booleanisDefaultOnNull()(package private) booleanisEverything(ExpressionVisitor visitor)Visit the default expression, the check constraint, and the sequence (if any).booleanisGenerated()Returns whether this column is a generated column.booleanisGeneratedAlways()Returns whether this column is a generated column or always generated identity column.booleanisIdentity()Returns whether this column is an identity column.booleanisNullable()booleanisPrimaryKey()booleanisRowId()Returns whether this column is a row identity column.booleanisWideningConversion(Column newColumn)Check whether the new column is of the same type and not more restricted than this column.voidprepareExpressions(SessionLocal session)Prepare all expressions of this column or domain.private voidremoveNonIdentityProperties()voidrename(java.lang.String newName)Rename the column.voidsetComment(java.lang.String comment)voidsetDefaultExpression(SessionLocal session, Expression defaultExpression)Set the default expression.voidsetDefaultOnNull(boolean defaultOnNull)voidsetDomain(Domain domain)voidsetGeneratedExpression(Expression expression)Set the default value in the form of a generated expression of other columns.voidsetIdentityOptions(SequenceOptions identityOptions, boolean generatedAlways)Set the identity options of this column.voidsetNullable(boolean b)voidsetOnUpdateExpression(SessionLocal session, Expression onUpdateExpression)Set the on update expression.voidsetPrimaryKey(boolean primaryKey)voidsetRowId(boolean rowId)Set row identity flag.voidsetSelectivity(int selectivity)Set the new selectivity of a column.voidsetSequence(Sequence sequence, boolean generatedAlways)Set the sequence to generate the value.voidsetTable(Table table, int columnId)Set the table and column id.voidsetVisible(boolean b)java.lang.StringtoString()private voidupdateSequenceIfRequired(SessionLocal session, long value)(package private) ValuevalidateConvertUpdateSequence(SessionLocal session, Value value, Row row)Validate the value, convert it if required, and update the sequence value if required.static java.lang.StringBuilderwriteColumns(java.lang.StringBuilder builder, Column[] columns, int sqlFlags)Appends the specified columns to the specified builder.static java.lang.StringBuilderwriteColumns(java.lang.StringBuilder builder, Column[] columns, java.lang.String separator, java.lang.String suffix, int sqlFlags)Appends the specified columns to the specified builder.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.h2.util.HasSQL
getTraceSQL
-
-
-
-
Field Detail
-
ROWID
public static final java.lang.String ROWID
The name of the rowid pseudo column.- See Also:
- Constant Field Values
-
NOT_NULLABLE
public static final int NOT_NULLABLE
This column is not nullable.- See Also:
- Constant Field Values
-
NULLABLE
public static final int NULLABLE
This column is nullable.- See Also:
- Constant Field Values
-
NULLABLE_UNKNOWN
public static final int NULLABLE_UNKNOWN
It is not know whether this column is nullable.- See Also:
- Constant Field Values
-
type
private TypeInfo type
-
table
private Table table
-
name
private java.lang.String name
-
columnId
private int columnId
-
nullable
private boolean nullable
-
defaultExpression
private Expression defaultExpression
-
onUpdateExpression
private Expression onUpdateExpression
-
identityOptions
private SequenceOptions identityOptions
-
defaultOnNull
private boolean defaultOnNull
-
sequence
private Sequence sequence
-
isGeneratedAlways
private boolean isGeneratedAlways
-
generatedTableFilter
private GeneratedColumnResolver generatedTableFilter
-
selectivity
private int selectivity
-
comment
private java.lang.String comment
-
primaryKey
private boolean primaryKey
-
visible
private boolean visible
-
rowId
private boolean rowId
-
domain
private Domain domain
-
-
Method Detail
-
writeColumns
public static java.lang.StringBuilder writeColumns(java.lang.StringBuilder builder, Column[] columns, int sqlFlags)Appends the specified columns to the specified builder.- Parameters:
builder- string buildercolumns- columnssqlFlags- formatting flags- Returns:
- the specified string builder
-
writeColumns
public static java.lang.StringBuilder writeColumns(java.lang.StringBuilder builder, Column[] columns, java.lang.String separator, java.lang.String suffix, int sqlFlags)Appends the specified columns to the specified builder.- Parameters:
builder- string buildercolumns- columnsseparator- separatorsuffix- additional SQL to append after each columnsqlFlags- formatting flags- Returns:
- the specified string builder
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
getClone
public Column getClone()
-
convert
public Value convert(CastDataProvider provider, Value v)
Convert a value to this column's type without precision and scale checks.- Parameters:
provider- the cast information providerv- the value- Returns:
- the value
-
isIdentity
public boolean isIdentity()
Returns whether this column is an identity column.- Returns:
- whether this column is an identity column
-
isGenerated
public boolean isGenerated()
Returns whether this column is a generated column.- Returns:
- whether this column is a generated column
-
isGeneratedAlways
public boolean isGeneratedAlways()
Returns whether this column is a generated column or always generated identity column.- Returns:
- whether this column is a generated column or always generated identity column
-
setGeneratedExpression
public void setGeneratedExpression(Expression expression)
Set the default value in the form of a generated expression of other columns.- Parameters:
expression- the computed expression
-
setTable
public void setTable(Table table, int columnId)
Set the table and column id.- Parameters:
table- the tablecolumnId- the column index
-
getTable
public Table getTable()
-
setDefaultExpression
public void setDefaultExpression(SessionLocal session, Expression defaultExpression)
Description copied from interface:ColumnTemplateSet the default expression.- Specified by:
setDefaultExpressionin interfaceColumnTemplate- Parameters:
session- the sessiondefaultExpression- the default expression
-
setOnUpdateExpression
public void setOnUpdateExpression(SessionLocal session, Expression onUpdateExpression)
Description copied from interface:ColumnTemplateSet the on update expression.- Specified by:
setOnUpdateExpressionin interfaceColumnTemplate- Parameters:
session- the sessiononUpdateExpression- the on update expression
-
getColumnId
public int getColumnId()
-
getSQL
public java.lang.String getSQL(int sqlFlags)
Description copied from interface:HasSQLGet the SQL statement of this expression. This may not always be the original SQL statement, specially after optimization.
-
getSQL
public java.lang.StringBuilder getSQL(java.lang.StringBuilder builder, int sqlFlags)Description copied from interface:HasSQLAppends the SQL statement of this object to the specified builder.
-
getSQLWithTable
public java.lang.StringBuilder getSQLWithTable(java.lang.StringBuilder builder, int sqlFlags)Appends the table name and column name to the specified builder.- Parameters:
builder- the string buildersqlFlags- formatting flags- Returns:
- the specified string builder
-
getName
public java.lang.String getName()
-
setNullable
public void setNullable(boolean b)
-
getVisible
public boolean getVisible()
-
setVisible
public void setVisible(boolean b)
-
getDomain
public Domain getDomain()
- Specified by:
getDomainin interfaceColumnTemplate
-
setDomain
public void setDomain(Domain domain)
- Specified by:
setDomainin interfaceColumnTemplate
-
isRowId
public boolean isRowId()
Returns whether this column is a row identity column.- Returns:
- true for _ROWID_ column, false otherwise
-
setRowId
public void setRowId(boolean rowId)
Set row identity flag.- Parameters:
rowId- true _ROWID_ column, false otherwise
-
validateConvertUpdateSequence
Value validateConvertUpdateSequence(SessionLocal session, Value value, Row row)
Validate the value, convert it if required, and update the sequence value if required. If the value is null, the default value (NULL if no default is set) is returned. Domain constraints are validated as well.- Parameters:
session- the sessionvalue- the value or nullrow- the row- Returns:
- the new or converted value
-
getDefaultOrGenerated
private Value getDefaultOrGenerated(SessionLocal session, Row row)
-
getDataConversionError
private DbException getDataConversionError(Value value, DbException cause)
-
updateSequenceIfRequired
private void updateSequenceIfRequired(SessionLocal session, long value)
-
initializeSequence
public void initializeSequence(SessionLocal session, Schema schema, int id, boolean temporary)
Initialize the sequence for this column.- Parameters:
session- the sessionschema- the schema where the sequence should be generatedid- the object idtemporary- true if the sequence is temporary and does not need to be stored
-
prepareExpressions
public void prepareExpressions(SessionLocal session)
Description copied from interface:ColumnTemplatePrepare all expressions of this column or domain.- Specified by:
prepareExpressionsin interfaceColumnTemplate- Parameters:
session- the session
-
getCreateSQLWithoutName
public java.lang.String getCreateSQLWithoutName()
-
getCreateSQL
public java.lang.String getCreateSQL()
-
getCreateSQL
public java.lang.String getCreateSQL(boolean forMeta)
Get this columns part of CREATE TABLE SQL statement.- Parameters:
forMeta- whether this is for the metadata table- Returns:
- the SQL statement
-
getCreateSQL
private java.lang.String getCreateSQL(java.lang.StringBuilder builder, boolean forMeta)
-
isNullable
public boolean isNullable()
-
getDefaultExpression
public Expression getDefaultExpression()
- Specified by:
getDefaultExpressionin interfaceColumnTemplate
-
getEffectiveDefaultExpression
public Expression getEffectiveDefaultExpression()
- Specified by:
getEffectiveDefaultExpressionin interfaceColumnTemplate
-
getOnUpdateExpression
public Expression getOnUpdateExpression()
- Specified by:
getOnUpdateExpressionin interfaceColumnTemplate
-
getEffectiveOnUpdateExpression
public Expression getEffectiveOnUpdateExpression()
- Specified by:
getEffectiveOnUpdateExpressionin interfaceColumnTemplate
-
hasIdentityOptions
public boolean hasIdentityOptions()
Whether the column has any identity options.- Returns:
- true if yes
-
setIdentityOptions
public void setIdentityOptions(SequenceOptions identityOptions, boolean generatedAlways)
Set the identity options of this column.- Parameters:
identityOptions- identity column optionsgeneratedAlways- whether value should be always generated
-
removeNonIdentityProperties
private void removeNonIdentityProperties()
-
getIdentityOptions
public SequenceOptions getIdentityOptions()
Returns identity column options, ornullif sequence was already created or this column is not an identity column.- Returns:
- identity column options, or
null
-
setDefaultOnNull
public void setDefaultOnNull(boolean defaultOnNull)
-
isDefaultOnNull
public boolean isDefaultOnNull()
-
rename
public void rename(java.lang.String newName)
Rename the column. This method will only set the column name to the new value.- Parameters:
newName- the new column name
-
setSequence
public void setSequence(Sequence sequence, boolean generatedAlways)
Set the sequence to generate the value.- Parameters:
sequence- the sequencegeneratedAlways- whether the value of the sequence is always used
-
getSequence
public Sequence getSequence()
-
getSelectivity
public int getSelectivity()
Get the selectivity of the column. Selectivity 100 means values are unique, 10 means every distinct value appears 10 times on average.- Returns:
- the selectivity
-
setSelectivity
public void setSelectivity(int selectivity)
Set the new selectivity of a column.- Parameters:
selectivity- the new value
-
getDefaultSQL
public java.lang.String getDefaultSQL()
- Specified by:
getDefaultSQLin interfaceColumnTemplate
-
getOnUpdateSQL
public java.lang.String getOnUpdateSQL()
- Specified by:
getOnUpdateSQLin interfaceColumnTemplate
-
setComment
public void setComment(java.lang.String comment)
-
getComment
public java.lang.String getComment()
-
setPrimaryKey
public void setPrimaryKey(boolean primaryKey)
-
isEverything
boolean isEverything(ExpressionVisitor visitor)
Visit the default expression, the check constraint, and the sequence (if any).- Parameters:
visitor- the visitor- Returns:
- true if every visited expression returned true, or if there are no expressions
-
isPrimaryKey
public boolean isPrimaryKey()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
isWideningConversion
public boolean isWideningConversion(Column newColumn)
Check whether the new column is of the same type and not more restricted than this column.- Parameters:
newColumn- the new (target) column- Returns:
- true if the new column is compatible
-
copy
public void copy(Column source)
Copy the data of the source column into the current column.- Parameters:
source- the source column
-
-