Class TType

java.lang.Object
com.mckoi.database.TType
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
TArrayType, TBinaryType, TBooleanType, TDateType, TJavaObjectType, TNullType, TNumericType, TQueryPlanType, TStringType

public abstract class TType extends Object implements Serializable
A TType object represents a type in a database engine. For example, an implementation might represent a STRING or a NUMBER. This is an immutable class. See implementations of this object for further examples.
Author:
Tobias Downer
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final TArrayType
    A type that represents an array.
    static final TBinaryType
    A default binary (SQL BLOB) type of unlimited maximum size.
    static final TBooleanType
    A default boolean (SQL BIT) type.
    static final TDateType
    A default date (SQL TIMESTAMP) type.
    static final TNullType
    A default NULL type.
    static final TNumericType
    A default numeric (SQL NUMERIC) type of unlimited size and scale.
    static final TQueryPlanType
    A type that represents a query plan (sub-select).
    static final TStringType
    A default string (SQL VARCHAR) type of unlimited maximum size and null locale.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    TType(int sql_type)
    Constructs the type object.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Encodes a TType into a string which is a useful way to serialize a TType.
    static String
    Given an array of TType, returns a String that that is the encoded form of the array and that can be later decoded back into an array of TType.
    Returns this TType as a fully parsable declared SQL type.
    static TType
    binaryType(int sql_type, int size)
    Returns a TBinaryType object.
    static TType
    booleanType(int sql_type)
    Returns a TBooleanType object.
    abstract int
    Calculates the approximate memory usage of an object of this type in bytes.
    static Object
    Casts the given Java object to the given type.
    abstract boolean
    Returns true if the type of this object is logically comparable to the type of the given object.
    abstract int
    Compares two objects that are logically comparable under this type.
    static TType
    dateType(int sql_type)
    Returns a TDateType object.
    static TType
    decodeString(String encoded_str)
    Decodes a String that has been encoded with the 'asEncodedString' method and returns a TType that represented the type.
    static TType[]
    decodeTypes(String encoded_str)
    Decodes a list (or array) of TType objects that was previously encoded with the 'asEncodedString(Type[])' method.
    static TType
    Given a java class, this will return a default TType object that can encapsulate Java objects of this type.
    int
    Returns the SQL type of this.
    static TType
    Assuming that the two types are numeric types, this will return the 'widest' of the two types.
    abstract Class
    Returns the Java Class that is used to represent this type of object.
    static TType
    javaObjectType(String class_name)
    Returns a TBinaryType constrained for the given class.
    static TType
    numericType(int sql_type, int size, int scale)
    Returns a TNumericType object of the given size and scale.
    static TType
    stringType(int sql_type, int size, String locale, int strength, int decomposition)
    Returns a TStringType object of the given size and locale information.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • BOOLEAN_TYPE

      public static final TBooleanType BOOLEAN_TYPE
      A default boolean (SQL BIT) type.
    • STRING_TYPE

      public static final TStringType STRING_TYPE
      A default string (SQL VARCHAR) type of unlimited maximum size and null locale.
    • NUMERIC_TYPE

      public static final TNumericType NUMERIC_TYPE
      A default numeric (SQL NUMERIC) type of unlimited size and scale.
    • DATE_TYPE

      public static final TDateType DATE_TYPE
      A default date (SQL TIMESTAMP) type.
    • BINARY_TYPE

      public static final TBinaryType BINARY_TYPE
      A default binary (SQL BLOB) type of unlimited maximum size.
    • NULL_TYPE

      public static final TNullType NULL_TYPE
      A default NULL type.
    • QUERY_PLAN_TYPE

      public static final TQueryPlanType QUERY_PLAN_TYPE
      A type that represents a query plan (sub-select).
    • ARRAY_TYPE

      public static final TArrayType ARRAY_TYPE
      A type that represents an array.
  • Constructor Details

    • TType

      protected TType(int sql_type)
      Constructs the type object.
  • Method Details

    • getSQLType

      public int getSQLType()
      Returns the SQL type of this.
    • asSQLString

      public String asSQLString()
      Returns this TType as a fully parsable declared SQL type. For example, if this represents a string we might return "VARCHAR(30) COLLATE 'jpJP'" This method is used for debugging and display purposes only and we would not expect to actually feed this back into an SQL parser.
    • comparableTypes

      public abstract boolean comparableTypes(TType type)
      Returns true if the type of this object is logically comparable to the type of the given object. For example, VARCHAR and LONGVARCHAR are comparable types. DOUBLE and FLOAT are comparable types. DOUBLE and VARCHAR are not comparable types.
    • compareObs

      public abstract int compareObs(Object ob1, Object ob2)
      Compares two objects that are logically comparable under this type. Returns 0 if the values are equal, >1 if ob1 is greater than ob2, and <1 if ob1 is less than ob2. It is illegal to pass NULL values for ob1 or ob2 into this method.
    • calculateApproximateMemoryUse

      public abstract int calculateApproximateMemoryUse(Object ob)
      Calculates the approximate memory usage of an object of this type in bytes.
    • javaClass

      public abstract Class javaClass()
      Returns the Java Class that is used to represent this type of object. For example, string types would return String.class.
    • asEncodedString

      public static String asEncodedString(TType type)
      Encodes a TType into a string which is a useful way to serialize a TType. The encoded string should be understandable when read.
    • asEncodedString

      public static String asEncodedString(TType[] types)
      Given an array of TType, returns a String that that is the encoded form of the array and that can be later decoded back into an array of TType. Useful for serializing a list of TType information.
    • decodeString

      public static TType decodeString(String encoded_str)
      Decodes a String that has been encoded with the 'asEncodedString' method and returns a TType that represented the type.
    • decodeTypes

      public static TType[] decodeTypes(String encoded_str)
      Decodes a list (or array) of TType objects that was previously encoded with the 'asEncodedString(Type[])' method.
    • javaObjectType

      public static TType javaObjectType(String class_name)
      Returns a TBinaryType constrained for the given class.
    • stringType

      public static TType stringType(int sql_type, int size, String locale, int strength, int decomposition)
      Returns a TStringType object of the given size and locale information. If locale is null then collation is lexicographical.
    • numericType

      public static TType numericType(int sql_type, int size, int scale)
      Returns a TNumericType object of the given size and scale.
    • booleanType

      public static TType booleanType(int sql_type)
      Returns a TBooleanType object.
    • dateType

      public static TType dateType(int sql_type)
      Returns a TDateType object.
    • binaryType

      public static TType binaryType(int sql_type, int size)
      Returns a TBinaryType object.
    • castObjectToTType

      public static Object castObjectToTType(Object ob, TType type)
      Casts the given Java object to the given type. For example, given a BigNumber object and STRING_TYPE, this would return the number as a string.
    • fromClass

      public static TType fromClass(Class c)
      Given a java class, this will return a default TType object that can encapsulate Java objects of this type. For example, given java.lang.String, this will return a TStringType with no locale and maximum size.

      Note that using this method is generally not recommended unless you really can't determine more type information than from the Java object itself.

    • getWidestType

      public static TType getWidestType(TType t1, TType t2)
      Assuming that the two types are numeric types, this will return the 'widest' of the two types. For example, an INTEGER is a wider type than a SHORT, and a FLOAT is wider than an INTEGER.

      Code by Jim McBeath.