Class AbstractDataType<T>

java.lang.Object
org.tentackle.sql.datatypes.AbstractDataType<T>
All Implemented Interfaces:
DataType<T>
Direct Known Subclasses:
AbstractDateTimeType, AbstractNumberType, BinaryType, BMoneyType, BooleanType, CharacterType, ConvertibleType, DMoneyType, I18NTextType, StringType, UUIDType

public abstract class AbstractDataType<T> extends Object implements DataType<T>
Implements some common methods for data types.
  • Field Details

    • maxLinesInLiteral

      public static int maxLinesInLiteral
      Maximum number of lines in literal.
      > 0 if unlimited.
  • Constructor Details

    • AbstractDataType

      public AbstractDataType()
      Parent constructor.
  • Method Details

    • isPredefined

      public boolean isPredefined()
      Description copied from interface: DataType
      Returns whether this is a predefined type.
      Specified by:
      isPredefined in interface DataType<T>
      Returns:
      true if explicit methods provided by the persistence layer, false if generic methods must be used
    • getVariant

      public String getVariant()
      Description copied from interface: DataType
      Gets the optional variant of the type.
      Specified by:
      getVariant in interface DataType<T>
      Returns:
      the variant, empty string if none, never null
    • isColumnCountBackendSpecific

      public boolean isColumnCountBackendSpecific()
      Description copied from interface: DataType
      Returns whether the number of columns is backend-specific.
      For such types the following restrictions apply:
      • no CN_... will be generated for the individual columns, just the column basename
      • they cannot be used as wurblet arguments (to generate the WHERE-clause)
      • the application cannot refer to a column in a backend-agnostic way
      Specified by:
      isColumnCountBackendSpecific in interface DataType<T>
      Returns:
      false if not backend-specific (default), true if varying
    • getColumnCount

      public int getColumnCount(Backend backend)
      Description copied from interface: DataType
      Gets the number of database columns.
      A tentackle type may be mapped to more than one column for multi-column types such as BMoney.
      Specified by:
      getColumnCount in interface DataType<T>
      Parameters:
      backend - the backend, ignored if !DataType.isColumnCountBackendSpecific()
      Returns:
      default is 1
    • getSortableColumns

      public int[] getSortableColumns()
      Description copied from interface: DataType
      Gets the indexes to the sortable columns.
      The number and order of the indexes is reflected in the generated order-by SQL clause.
      Specified by:
      getSortableColumns in interface DataType<T>
      Returns:
      the column indexes, null or empty if datatype is not sortable in a meaningful manner by the database
    • getColumnSuffix

      public Optional<String> getColumnSuffix(Backend backend, int index)
      Predefined DataTypes must not override this method!
      See ResultSetWrapper#COL2_SUFFIX.

      Gets the optional suffix.
      Suffixes usually begin with an underscore.

      Specified by:
      getColumnSuffix in interface DataType<T>
      Parameters:
      backend - the backend, ignored if !DataType.isColumnCountBackendSpecific()
      index - the column index
      Returns:
      the optional suffix
    • getCommentSuffix

      public Optional<String> getCommentSuffix(Backend backend, int index)
      Description copied from interface: DataType
      Gets the optional comment suffix.
      If present, it will be appended to the comment.

      Important: for datatypes with more than one column, none or exactly one should be without suffix. See WurbletArgument in tentackle-wurblets.

      Specified by:
      getCommentSuffix in interface DataType<T>
      Parameters:
      backend - the backend, ignored if !DataType.isColumnCountBackendSpecific()
      index - the column index
      Returns:
      the optional suffix
    • createColumnNames

      public List<String> createColumnNames(Backend backend, String columnName)
      Description copied from interface: DataType
      Creates a list of column names.
      Specified by:
      createColumnNames in interface DataType<T>
      Parameters:
      backend - the backend, ignored if !DataType.isColumnCountBackendSpecific()
      columnName - the column basename
      Returns:
      the names as an unmodifiable list
    • createColumnNamesAsString

      public String createColumnNamesAsString(Backend backend, String columnName, String separator)
      Description copied from interface: DataType
      Creates a list of column names as a concatenated string.
      The string can be used in SQL INSERT and UPDATE statements.
      Specified by:
      createColumnNamesAsString in interface DataType<T>
      Parameters:
      backend - the backend, ignored if !DataType.isColumnCountBackendSpecific()
      columnName - the column basename
      separator - the separator string between names
      Returns:
      the names list
    • getSize

      public int getSize(Backend backend, int index, Integer size)
      Description copied from interface: DataType
      Gets the column size.
      Sizes may be differently aligned according to the backend.
      Specified by:
      getSize in interface DataType<T>
      Parameters:
      backend - the backend
      index - the column index
      size - the size from the model
      Returns:
      the column size
    • getScale

      public int getScale(Backend backend, int index, Integer scale)
      Description copied from interface: DataType
      Gets the column scale.
      The scale may be differently aligned according to the backend.
      Specified by:
      getScale in interface DataType<T>
      Parameters:
      backend - the backend
      index - the column index
      scale - the scale from the model
      Returns:
      the column scale
    • getColumnValue

      public Object getColumnValue(Backend backend, int index, T value)
      Description copied from interface: DataType
      Gets the value of a column.
      Specified by:
      getColumnValue in interface DataType<T>
      Parameters:
      backend - the backend, ignored if !DataType.isColumnCountBackendSpecific()
      index - the column index
      value - the datatype's value according to the model
      Returns:
      the column's value
    • getColumnGetter

      public String getColumnGetter(int index, String varName)
      Description copied from interface: DataType
      Returns the java code to get the value of a column.
      Specified by:
      getColumnGetter in interface DataType<T>
      Parameters:
      index - the column index
      varName - the java variable name
      Returns:
      the java code, empty string if this is a single column type
    • getColumnAlias

      public String getColumnAlias(int index)
      Description copied from interface: DataType
      Gets the alias name of a column.
      Specified by:
      getColumnAlias in interface DataType<T>
      Parameters:
      index - the column index
      Returns:
      the column's alias
    • isPrimitive

      public boolean isPrimitive()
      Description copied from interface: DataType
      Returns whether type is a primitive.
      Specified by:
      isPrimitive in interface DataType<T>
      Returns:
      true if primitive
    • toNonPrimitive

      public DataType<?> toNonPrimitive()
      Description copied from interface: DataType
      Gets the non-primitive type if this is a primitive.
      Specified by:
      toNonPrimitive in interface DataType<T>
      Returns:
      the non-primitive type, this type if it is already a non-primitive
    • toPrimitive

      public Optional<DataType<?>> toPrimitive()
      Description copied from interface: DataType
      Gets the primitive type if this is a non-primitive.
      Specified by:
      toPrimitive in interface DataType<T>
      Returns:
      the primitive type if there is one
    • isMutable

      public boolean isMutable()
      Description copied from interface: DataType
      Returns whether type is a mutable java object.
      Mutable objects may change their state and must implement Freezable.
      Specified by:
      isMutable in interface DataType<T>
      Returns:
      true if mutable and freezable
    • isNumeric

      public boolean isNumeric()
      Description copied from interface: DataType
      Returns whether this is a numeric type.
      Specified by:
      isNumeric in interface DataType<T>
      Returns:
      true if numeric
    • isDateOrTime

      public boolean isDateOrTime()
      Description copied from interface: DataType
      Returns whether this is a date and/or time type.
      Specified by:
      isDateOrTime in interface DataType<T>
      Returns:
      true if time, date or timestamp
    • isTimezoneApplicable

      public boolean isTimezoneApplicable()
      Description copied from interface: DataType
      Returns whether a Calendar-timezone can be applied to this date and/or time related type.
      Only for types where methods with Calendar argument are provided in ResultSet and PreparedStatement.
      Specified by:
      isTimezoneApplicable in interface DataType<T>
      Returns:
      true if methods exist with timezone correction
    • isBool

      public boolean isBool()
      Description copied from interface: DataType
      Returns whether this is a boolean or Boolean type.
      Specified by:
      isBool in interface DataType<T>
      Returns:
      true if bool
    • isMapNullSupported

      public boolean isMapNullSupported()
      Description copied from interface: DataType
      Returns whether this type supports mapping null values to some well-defined constant.
      Specified by:
      isMapNullSupported in interface DataType<T>
      Returns:
      true if mapNull supported
    • getMappedNullValue

      public Object getMappedNullValue(Backend backend, int index)
      Description copied from interface: DataType
      Gets the mapped non-null value that represents the model's null value.
      The method throws a BackendException, if !DataType.isMapNullSupported().
      Specified by:
      getMappedNullValue in interface DataType<T>
      Parameters:
      backend - the backend, null if not backend-specific
      index - the column index
      Returns:
      the non-null mapped value
    • isUTCSupported

      public boolean isUTCSupported()
      Description copied from interface: DataType
      Returns whether this type supports the UTC option.
      Specified by:
      isUTCSupported in interface DataType<T>
      Returns:
      true if UTC supported
    • isModelProvidingInnerType

      public boolean isModelProvidingInnerType()
      Description copied from interface: DataType
      Returns whether the model provides an inner type.
      Specified by:
      isModelProvidingInnerType in interface DataType<T>
      Returns:
      true if inner type is specified within angle brackets, false if standard type
    • isJavaTypeGenerified

      public boolean isJavaTypeGenerified()
      Description copied from interface: DataType
      Returns whether the inner type describes a generified java type.
      Implies DataType.isModelProvidingInnerType().
      Specified by:
      isJavaTypeGenerified in interface DataType<T>
      Returns:
      true if javaType<T>, else simple type
    • isDowncastNecessary

      public boolean isDowncastNecessary()
      Description copied from interface: DataType
      For certain numeric types, a downcast is necessary when assigning literals.
      Specified by:
      isDowncastNecessary in interface DataType<T>
      Returns:
      true if downcast is necessary
    • isLiteralSupported

      public boolean isLiteralSupported(Integer index)
      Description copied from interface: DataType
      Returns whether this type can be used literally in a query String.
      Specified by:
      isLiteralSupported in interface DataType<T>
      Parameters:
      index - the column index, null if applies to object of this type
      Returns:
      true if literal String supported, else false
    • toLiteral

      public String toLiteral(String str, Integer index)
      Description copied from interface: DataType
      Takes the string representation of a value and converts it to an SQL literal.
      Some types need single quotes, for example.
      Specified by:
      toLiteral in interface DataType<T>
      Parameters:
      str - the value string
      index - the column index, null if applies to object of this type
      Returns:
      the SQL literal
    • valueOfLiteralToCode

      public String valueOfLiteralToCode(String str, Integer index)
      Description copied from interface: DataType
      Creates the java code to apply the valueOf method to a String.
      Specified by:
      valueOfLiteralToCode in interface DataType<T>
      Parameters:
      str - the literal to be parsed
      index - the column index, null if applies to object of this type
      Returns:
      the java code
    • toString

      public String toString(T object)
      Description copied from interface: DataType
      Takes an object and converts it to a string that can in turn be parsed with DataType.valueOf(String).
      The method is used to print a literal (for example the default value of a dumped attribute).

      Notice: the method doesn't use any locale, so the results are always the same regardless of the JVM's locale.

      Specified by:
      toString in interface DataType<T>
      Parameters:
      object - the object of this DataType
      Returns:
      the printable string
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getDataTypeConstant

      public String getDataTypeConstant()
      Description copied from interface: DataType
      Gets the name of the datatype constant.
      Specified by:
      getDataTypeConstant in interface DataType<T>
      Returns:
      the DT_...
    • set

      public Object set(Backend backend, PreparedStatement statement, int pos, T object, int index, boolean mapNull, Integer size) throws SQLException
      Description copied from interface: DataType
      Sets a column of an object into a prepared statement.
      Must be implemented if DataType.isPredefined() returns false. Not invoked by framework otherwise.
      Specified by:
      set in interface DataType<T>
      Parameters:
      backend - the database backend
      statement - the prepared statement
      pos - the position of the first SQL value
      object - the object, may be null
      index - the column index
      mapNull - true if map null-values to non-null values
      size - the optional size specified in the model
      Returns:
      the object stored in the prepared statement
      Throws:
      SQLException - if failed
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • assertColumnCountNotBackendSpecific

      protected void assertColumnCountNotBackendSpecific(Backend backend)
      Checks that this type has a fixed number of columns or the backend is specified.
    • valueStringToCode

      protected String valueStringToCode(String str)
      Transforms the value string of an object to java code.
      Parameters:
      str - the value string
      Returns:
      the java code