Class SqlColumn<T>
- Type Parameters:
T- the Java type associated with the column
- All Implemented Interfaces:
BasicColumn, BindableColumn<T>, SortSpecification
The class contains many attributes that are helpful for use in MyBatis and Spring runtime
environments, but the only required attributes are the name of the column and a reference to
the SqlTable the column is a part of.
The class can be extended if you wish to associate additional attributes with a column for your
own purposes. Extending the class is a bit more challenging than you might expect because you may need to
handle the covariant types for many methods in SqlColumn. Additionally, many methods in SqlColumn
create new instances of the class in keeping with the library's primary strategy of immutability. You will also
need to ensure that these methods create instances of your extended class, rather than the base SqlColumn
class. We have worked to keep this process as simple as possible.
Extending the class involves the following activities:
- Create a class that extends
SqlColumn - In your extended class, create a static builder class that extends
SqlColumn.AbstractBuilder - Add your desired attributes to the class and the builder
- You MUST override the
copyBuilder()method and return a new instance of your builder with all attributes set. In the overridden method you should call the superclasspopulateBaseBuilder(AbstractBuilder)method to set the attributes from the baseSqlColumn, then populate your extended attributes. During normal usage, the library may create additional instances of your class. If you do not override thecopyBuilder()method properly, then your extended attributes will be lost. - You MAY override the following methods. These methods are used with regular operations in the library and
create new instances of the class. However, these methods are not typically chained, so losing the specific
type may not be a problem. If you want to preserve the type, then you can override these methods
to specify the covariant return type. See below for usage of the
cast(SqlColumn)method to make it easier to override these methods. - You SHOULD override the following methods. These methods can be used to add additional attributes to a
column by creating a new instance with a specified attribute set. These methods are used during the
construction of columns. If you do not override these methods, and a user calls them, then the specific type
will be lost. If you want to preserve the type, then you can override these methods
to specify the covariant return type. See below for usage of the
cast(SqlColumn)method to make it easier to override these methods.
For all overridden methods except copyBuilder(), the process is to call the superclass
method and cast the result properly. We provide a cast(SqlColumn) method to aid with this
process. For example, overriding the descending method could look like this:
@Override
public MyExtendedColumn<T> descending() {
return cast(super.descending());
}
The test code for this library contains an example of a fully executed extension of this class.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classSqlColumn.AbstractBuilder<T, C extends SqlColumn<T>, B extends SqlColumn.AbstractBuilder<T,C, B>> static class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final @Nullable Stringprotected final Stringprotected final @Nullable Stringprotected final @Nullable JDBCTypeprotected final Stringprotected final ParameterTypeConverter<T, ?> protected final @Nullable RenderingStrategyprotected final SqlTableprotected final @Nullable Stringprotected final @Nullable String -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionalias()Returns the columns alias if one has been specified.Create a new column instance with the specified alias that will render as "as alias" in a column list.Set an alias with a camel-cased string based on the column name.protected <S extends SqlColumn<?>>
S@Nullable ObjectconvertParameterType(@Nullable T value) protected SqlColumn.AbstractBuilder<T, ?, ?> Create a new Builder, then populate all attributes in the builder with current values.Create a new column instance that will render as descending when used in an order by phrase.javaType()jdbcType()name()static <T> SqlColumn<T> static <T> SqlColumn<T> protected <B extends SqlColumn.AbstractBuilder<T,?, ?>>
BpopulateBaseBuilder(B builder) This method will add all current attributes to the specified builder.qualifiedWith(String tableQualifier) Override the calculated table qualifier if there is one.render(RenderingContext renderingContext) Returns a rendering of the column.renderForOrderBy(RenderingContext renderingContext) Return a fragment rendered for use in an ORDER BY clause.table()<S> SqlColumn<S> withJavaProperty(String javaProperty) Create a new column instance with the specified Java property.<S> SqlColumn<S> withJavaType(Class<S> javaType) Create a new column instance with the specified Java type.<S> SqlColumn<S> withParameterTypeConverter(ParameterTypeConverter<S, ?> parameterTypeConverter) Create a new column instance with the specified parameter type converter.<S> SqlColumn<S> withRenderingStrategy(RenderingStrategy renderingStrategy) Create a new column instance with the specified rendering strategy.<S> SqlColumn<S> withTypeHandler(String typeHandler) Create a new column instance with the specified type handler.
-
Field Details
-
name
-
table
-
jdbcType
-
descendingPhrase
-
alias
-
typeHandler
-
renderingStrategy
-
parameterTypeConverter
-
tableQualifier
-
javaType
-
javaProperty
-
-
Constructor Details
-
SqlColumn
-
-
Method Details
-
name
-
table
-
jdbcType
- Specified by:
jdbcTypein interfaceBasicColumn
-
alias
Description copied from interface:BasicColumnReturns the columns alias if one has been specified.- Specified by:
aliasin interfaceBasicColumn- Returns:
- the column alias
-
typeHandler
- Specified by:
typeHandlerin interfaceBasicColumn
-
javaType
-
javaProperty
-
convertParameterType
- Specified by:
convertParameterTypein interfaceBindableColumn<T>
-
descending
Create a new column instance that will render as descending when used in an order by phrase.- Specified by:
descendingin interfaceSortSpecification- Returns:
- a new column instance that will render as descending when used in an order by phrase
-
as
Create a new column instance with the specified alias that will render as "as alias" in a column list.- Specified by:
asin interfaceBasicColumn- Specified by:
asin interfaceBindableColumn<T>- Parameters:
alias- the column alias to set- Returns:
- a new column instance with the specified alias
-
qualifiedWith
Override the calculated table qualifier if there is one. This is useful for sub-queries where the calculated table qualifier may not be correct in all cases.- Parameters:
tableQualifier- the table qualifier to apply to the rendered column name- Returns:
- a new column that will be rendered with the specified table qualifier
-
asCamelCase
Set an alias with a camel-cased string based on the column name. This can be useful for queries using theCommonSelectMapperwhere the columns are placed into a map based on the column name returned from the database.A camel case string is a mixed case string, and most databases do not support unquoted mixed case strings as identifiers. Therefore, the generated alias will be surrounded by double quotes thereby making it a quoted identifier. Most databases will respect quoted mixed case identifiers.
- Returns:
- a new column aliased with a camel case version of the column name
-
renderForOrderBy
Description copied from interface:SortSpecificationReturn a fragment rendered for use in an ORDER BY clause. The fragment should include "DESC" if a descending order is desired.- Specified by:
renderForOrderByin interfaceSortSpecification- Parameters:
renderingContext- the current rendering context- Returns:
- a rendered fragment and parameters if applicable
-
render
Description copied from interface:BasicColumnReturns a rendering of the column. The rendered fragment should include the table alias based on the TableAliasCalculator in the RenderingContext. The fragment could contain prepared statement parameter markers and associated parameter values if desired.- Specified by:
renderin interfaceBasicColumn- Parameters:
renderingContext- the rendering context (strategy, sequence, etc.)- Returns:
- a rendered SQL fragment and, optionally, parameters associated with the fragment
-
renderingStrategy
- Specified by:
renderingStrategyin interfaceBasicColumn
-
withTypeHandler
Create a new column instance with the specified type handler.This method uses a different type (S). This allows it to be chained with the other with* methods. Using new types forces the compiler to delay type inference until the end of a call chain. Without this different type (for example, if we used T), the compiler would erase the type after the call and method chaining would not work. This is a workaround for Java's lack of reification.
- Type Parameters:
S- the type of the new column (will be the same as T)- Parameters:
typeHandler- the type handler to set- Returns:
- a new column instance with the specified type handler
-
withRenderingStrategy
Create a new column instance with the specified rendering strategy.This method uses a different type (S). This allows it to be chained with the other with* methods. Using new types forces the compiler to delay type inference until the end of a call chain. Without this different type (for example, if we used T), the compiler would erase the type after the call and method chaining would not work. This is a workaround for Java's lack of reification.
- Type Parameters:
S- the type of the new column (will be the same as T)- Parameters:
renderingStrategy- the rendering strategy to set- Returns:
- a new column instance with the specified type handler
-
withParameterTypeConverter
public <S> SqlColumn<S> withParameterTypeConverter(ParameterTypeConverter<S, ?> parameterTypeConverter) Create a new column instance with the specified parameter type converter.Parameter type converters are useful with Spring JDBC. Typically, they are not needed for MyBatis.
This method uses a different type (S). This allows it to be chained with the other with* methods. Using new types forces the compiler to delay type inference until the end of a call chain. Without this different type (for example, if we used T), the compiler would erase the type after the call and method chaining would not work. This is a workaround for Java's lack of reification.
- Type Parameters:
S- the type of the new column (will be the same as T)- Parameters:
parameterTypeConverter- the parameter type converter to set- Returns:
- a new column instance with the specified type handler
-
withJavaType
Create a new column instance with the specified Java type.Specifying a Java type will force rendering of the Java type for MyBatis parameters. This can be useful with some MyBatis type handlers.
This method uses a different type (S). This allows it to be chained with the other with* methods. Using new types forces the compiler to delay type inference until the end of a call chain. Without this different type (for example, if we used T), the compiler would erase the type after the call and method chaining would not work. This is a workaround for Java's lack of reification.
- Type Parameters:
S- the type of the new column (will be the same as T)- Parameters:
javaType- the Java type to set- Returns:
- a new column instance with the specified type handler
-
withJavaProperty
Create a new column instance with the specified Java property.Specifying a Java property in the column will allow usage of the column as a "mapped column" in record-based insert statements.
This method uses a different type (S). This allows it to be chained with the other with* methods. Using new types forces the compiler to delay type inference until the end of a call chain. Without this different type (for example, if we used T), the compiler would erase the type after the call and method chaining would not work. This is a workaround for Java's lack of reification.
- Type Parameters:
S- the type of the new column (will be the same as T)- Parameters:
javaProperty- the Java property to set- Returns:
- a new column instance with the specified type handler
-
copyBuilder
Create a new Builder, then populate all attributes in the builder with current values.This method is used to create copies of the class during normal operations (e.g. when calling the
as(String)method). Any subclass ofSqlColumnMUST override this method.- Returns:
- a new Builder instance with all current values populated
-
cast
-
populateBaseBuilder
This method will add all current attributes to the specified builder. It is useful when creating new class instances that only change one attribute - we set all current attributes, then change the one attribute. This utility can be used with the with* methods and other methods that create new instances.- Type Parameters:
B- the concrete builder type- Returns:
- the populated builder
-
of
-
of
-