Class SqlColumn<T>

java.lang.Object
org.mybatis.dynamic.sql.SqlColumn<T>
Type Parameters:
T - the Java type associated with the column
All Implemented Interfaces:
BasicColumn, BindableColumn<T>, SortSpecification

public class SqlColumn<T> extends Object implements BindableColumn<T>, SortSpecification
This class represents the definition of a column in a table.

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:

  1. Create a class that extends SqlColumn
  2. In your extended class, create a static builder class that extends SqlColumn.AbstractBuilder
  3. Add your desired attributes to the class and the builder
  4. 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 superclass populateBaseBuilder(AbstractBuilder) method to set the attributes from the base SqlColumn, then populate your extended attributes. During normal usage, the library may create additional instances of your class. If you do not override the copyBuilder() method properly, then your extended attributes will be lost.
  5. 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.
  6. 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.

  • Field Details

    • name

      protected final String name
    • table

      protected final SqlTable table
    • jdbcType

      protected final @Nullable JDBCType jdbcType
    • descendingPhrase

      protected final String descendingPhrase
    • alias

      protected final @Nullable String alias
    • typeHandler

      protected final @Nullable String typeHandler
    • renderingStrategy

      protected final @Nullable RenderingStrategy renderingStrategy
    • parameterTypeConverter

      protected final ParameterTypeConverter<T,?> parameterTypeConverter
    • tableQualifier

      protected final @Nullable String tableQualifier
    • javaType

      protected final @Nullable Class<T> javaType
    • javaProperty

      protected final @Nullable String javaProperty
  • Constructor Details

  • Method Details

    • name

      public String name()
    • table

      public SqlTable table()
    • jdbcType

      public Optional<JDBCType> jdbcType()
      Specified by:
      jdbcType in interface BasicColumn
    • alias

      public Optional<String> alias()
      Description copied from interface: BasicColumn
      Returns the columns alias if one has been specified.
      Specified by:
      alias in interface BasicColumn
      Returns:
      the column alias
    • typeHandler

      public Optional<String> typeHandler()
      Specified by:
      typeHandler in interface BasicColumn
    • javaType

      public Optional<Class<T>> javaType()
      Specified by:
      javaType in interface BindableColumn<T>
    • javaProperty

      public Optional<String> javaProperty()
    • convertParameterType

      public @Nullable Object convertParameterType(@Nullable T value)
      Specified by:
      convertParameterType in interface BindableColumn<T>
    • descending

      public SqlColumn<T> descending()
      Create a new column instance that will render as descending when used in an order by phrase.
      Specified by:
      descending in interface SortSpecification
      Returns:
      a new column instance that will render as descending when used in an order by phrase
    • as

      public SqlColumn<T> as(String alias)
      Create a new column instance with the specified alias that will render as "as alias" in a column list.
      Specified by:
      as in interface BasicColumn
      Specified by:
      as in interface BindableColumn<T>
      Parameters:
      alias - the column alias to set
      Returns:
      a new column instance with the specified alias
    • qualifiedWith

      public SqlColumn<T> qualifiedWith(String tableQualifier)
      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

      public SqlColumn<T> asCamelCase()
      Set an alias with a camel-cased string based on the column name. This can be useful for queries using the CommonSelectMapper where 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

      public FragmentAndParameters renderForOrderBy(RenderingContext renderingContext)
      Description copied from interface: SortSpecification
      Return a fragment rendered for use in an ORDER BY clause. The fragment should include "DESC" if a descending order is desired.
      Specified by:
      renderForOrderBy in interface SortSpecification
      Parameters:
      renderingContext - the current rendering context
      Returns:
      a rendered fragment and parameters if applicable
    • render

      public FragmentAndParameters render(RenderingContext renderingContext)
      Description copied from interface: BasicColumn
      Returns 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:
      render in interface BasicColumn
      Parameters:
      renderingContext - the rendering context (strategy, sequence, etc.)
      Returns:
      a rendered SQL fragment and, optionally, parameters associated with the fragment
    • renderingStrategy

      public Optional<RenderingStrategy> renderingStrategy()
      Specified by:
      renderingStrategy in interface BasicColumn
    • withTypeHandler

      public <S> SqlColumn<S> withTypeHandler(String typeHandler)
      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

      public <S> SqlColumn<S> withRenderingStrategy(RenderingStrategy renderingStrategy)
      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

      public <S> SqlColumn<S> withJavaType(Class<S> javaType)
      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

      public <S> SqlColumn<S> withJavaProperty(String javaProperty)
      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

      protected SqlColumn.AbstractBuilder<T,?,?> 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 of SqlColumn MUST override this method.

      Returns:
      a new Builder instance with all current values populated
    • cast

      protected <S extends SqlColumn<?>> S cast(SqlColumn<?> column)
    • populateBaseBuilder

      protected <B extends SqlColumn.AbstractBuilder<T,?,?>> B populateBaseBuilder(B builder)
      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

      public static <T> SqlColumn<T> of(String name, SqlTable table)
    • of

      public static <T> SqlColumn<T> of(String name, SqlTable table, JDBCType jdbcType)