com.j256.ormlite.field.types
Class BaseDataType

java.lang.Object
  extended by com.j256.ormlite.field.BaseFieldConverter
      extended by com.j256.ormlite.field.types.BaseDataType
All Implemented Interfaces:
DataPersister, FieldConverter
Direct Known Subclasses:
BaseDateType, BaseEnumType, BigDecimalNumericType, BigDecimalStringType, BigIntegerType, BooleanObjectType, ByteArrayType, ByteObjectType, CharacterObjectType, DateTimeType, DoubleObjectType, FloatObjectType, IntegerObjectType, LongObjectType, SerializableType, ShortObjectType, StringBytesType, StringType, UuidType, VoidType

public abstract class BaseDataType
extends BaseFieldConverter
implements DataPersister

Base data type that defines the default persistance methods for the various data types.

Here's a good page about the mapping for a number of database types:

NOTE: If you are creating your own custom database persister, you probably will need to override the BaseFieldConverter.sqlArgToJava(FieldType, Object, int) method as well which converts from a SQL data to java.

Author:
graywatson

Constructor Summary
BaseDataType(SqlType sqlType, Class<?>[] classes)
           
 
Method Summary
 Object convertIdNumber(Number number)
          Convert a Number object to its primitive object suitable for assigning to an ID field.
 boolean dataIsEqual(Object fieldObj1, Object fieldObj2)
          Compare two fields of this type returning true if equals else false.
 Object generateId()
          Return a generated id if appropriate or null if none.
 Class<?>[] getAssociatedClasses()
          Return the classes that should be associated with this.
 String[] getAssociatedClassNames()
          Return the class names that should be associated with this or null.
 int getDefaultWidth()
          Return the default width associated with this type or 0 if none.
 Class<?> getPrimaryClass()
          Return the class most associated with this persister or null if none.
 SqlType getSqlType()
          Return the SQL type that is stored in the database for this argument.
 boolean isAppropriateId()
          Return true if this data type can be an id column in a class.
 boolean isArgumentHolderRequired()
          Must use ArgumentHolder when querying for values of this type.
 boolean isComparable()
          Return true if this data type be compared in SQL statements.
 boolean isEscapedDefaultValue()
          Return whether this field's default value should be escaped in SQL.
 boolean isEscapedValue()
          Return whether we need to escape this value in SQL expressions.
 boolean isPrimitive()
          Return whether this field is a primitive type or not.
 boolean isSelfGeneratedId()
          Return true if this type creates its own generated ids else false to have the database do it.
 boolean isValidForField(Field field)
          Return true if the field is appropriate for this persister otherwise false.
 boolean isValidForVersion()
          Return true if this is a valid field for the DatabaseField.version().
 boolean isValidGeneratedType()
          Return true if this type can be auto-generated by the database.
 Object makeConfigObject(FieldType fieldType)
          This makes a configuration object for the data-type or returns null if none.
 Object moveToNextValue(Object currentValue)
          Move the current-value to the next value.
abstract  Object parseDefaultString(FieldType fieldType, String defaultStr)
          Convert a default string object and return the appropriate argument to a SQL insert or update statement.
 Object resultStringToJava(FieldType fieldType, String stringValue, int columnPos)
          Convert a string result value to the related Java field.
abstract  Object resultToSqlArg(FieldType fieldType, DatabaseResults results, int columnPos)
          Return the SQL argument object extracted from the results associated with column in position columnPos.
 
Methods inherited from class com.j256.ormlite.field.BaseFieldConverter
isStreamType, javaToSqlArg, resultToJava, sqlArgToJava
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.j256.ormlite.field.FieldConverter
isStreamType, javaToSqlArg, resultToJava, sqlArgToJava
 

Constructor Detail

BaseDataType

public BaseDataType(SqlType sqlType,
                    Class<?>[] classes)
Parameters:
sqlType - Type of the class as it is persisted in the databases.
classes - Associated classes for this type. These should be specified if you want this type to be always used for these Java classes. If this is a custom persister then this array should be empty.
Method Detail

parseDefaultString

public abstract Object parseDefaultString(FieldType fieldType,
                                          String defaultStr)
                                   throws SQLException
Description copied from interface: FieldConverter
Convert a default string object and return the appropriate argument to a SQL insert or update statement.

Specified by:
parseDefaultString in interface FieldConverter
Throws:
SQLException

resultToSqlArg

public abstract Object resultToSqlArg(FieldType fieldType,
                                      DatabaseResults results,
                                      int columnPos)
                               throws SQLException
Description copied from interface: FieldConverter
Return the SQL argument object extracted from the results associated with column in position columnPos. For example, if the type is a date-long then this will return a long value or null.

Specified by:
resultToSqlArg in interface FieldConverter
Parameters:
fieldType - Associated FieldType which may be null.
Throws:
SQLException - If there is a problem accessing the results data.

isValidForField

public boolean isValidForField(Field field)
Description copied from interface: DataPersister
Return true if the field is appropriate for this persister otherwise false.

Specified by:
isValidForField in interface DataPersister

getPrimaryClass

public Class<?> getPrimaryClass()
Description copied from interface: DataPersister
Return the class most associated with this persister or null if none.

Specified by:
getPrimaryClass in interface DataPersister

makeConfigObject

public Object makeConfigObject(FieldType fieldType)
                        throws SQLException
Description copied from interface: DataPersister
This makes a configuration object for the data-type or returns null if none. The object can be accessed later via FieldType.getDataTypeConfigObj().

Specified by:
makeConfigObject in interface DataPersister
Throws:
SQLException - If there are problems creating the config object. Needed for subclasses.

getSqlType

public SqlType getSqlType()
Description copied from interface: FieldConverter
Return the SQL type that is stored in the database for this argument.

Specified by:
getSqlType in interface FieldConverter

getAssociatedClasses

public Class<?>[] getAssociatedClasses()
Description copied from interface: DataPersister
Return the classes that should be associated with this.

Specified by:
getAssociatedClasses in interface DataPersister

getAssociatedClassNames

public String[] getAssociatedClassNames()
Description copied from interface: DataPersister
Return the class names that should be associated with this or null. This is used by reflection classes so we can discover if a Field matches _without_ needed the class dependency in -core.

Specified by:
getAssociatedClassNames in interface DataPersister

convertIdNumber

public Object convertIdNumber(Number number)
Description copied from interface: DataPersister
Convert a Number object to its primitive object suitable for assigning to an ID field.

Specified by:
convertIdNumber in interface DataPersister

isValidGeneratedType

public boolean isValidGeneratedType()
Description copied from interface: DataPersister
Return true if this type can be auto-generated by the database. Probably only numbers will return true.

Specified by:
isValidGeneratedType in interface DataPersister

isEscapedDefaultValue

public boolean isEscapedDefaultValue()
Description copied from interface: DataPersister
Return whether this field's default value should be escaped in SQL.

Specified by:
isEscapedDefaultValue in interface DataPersister

isEscapedValue

public boolean isEscapedValue()
Description copied from interface: DataPersister
Return whether we need to escape this value in SQL expressions. Numbers _must_ not be escaped but most other values should be.

Specified by:
isEscapedValue in interface DataPersister

isPrimitive

public boolean isPrimitive()
Description copied from interface: DataPersister
Return whether this field is a primitive type or not. This is used to know if we should throw if the field value is null.

Specified by:
isPrimitive in interface DataPersister

isComparable

public boolean isComparable()
Description copied from interface: DataPersister
Return true if this data type be compared in SQL statements.

Specified by:
isComparable in interface DataPersister

isAppropriateId

public boolean isAppropriateId()
Description copied from interface: DataPersister
Return true if this data type can be an id column in a class.

Specified by:
isAppropriateId in interface DataPersister

isArgumentHolderRequired

public boolean isArgumentHolderRequired()
Description copied from interface: DataPersister
Must use ArgumentHolder when querying for values of this type.

Specified by:
isArgumentHolderRequired in interface DataPersister

isSelfGeneratedId

public boolean isSelfGeneratedId()
Description copied from interface: DataPersister
Return true if this type creates its own generated ids else false to have the database do it.

Specified by:
isSelfGeneratedId in interface DataPersister

generateId

public Object generateId()
Description copied from interface: DataPersister
Return a generated id if appropriate or null if none.

Specified by:
generateId in interface DataPersister

getDefaultWidth

public int getDefaultWidth()
Description copied from interface: DataPersister
Return the default width associated with this type or 0 if none.

Specified by:
getDefaultWidth in interface DataPersister

dataIsEqual

public boolean dataIsEqual(Object fieldObj1,
                           Object fieldObj2)
Description copied from interface: DataPersister
Compare two fields of this type returning true if equals else false.

Specified by:
dataIsEqual in interface DataPersister

isValidForVersion

public boolean isValidForVersion()
Description copied from interface: DataPersister
Return true if this is a valid field for the DatabaseField.version().

Specified by:
isValidForVersion in interface DataPersister

moveToNextValue

public Object moveToNextValue(Object currentValue)
Description copied from interface: DataPersister
Move the current-value to the next value. Used for the version field.

Specified by:
moveToNextValue in interface DataPersister

resultStringToJava

public Object resultStringToJava(FieldType fieldType,
                                 String stringValue,
                                 int columnPos)
                          throws SQLException
Description copied from interface: FieldConverter
Convert a string result value to the related Java field.

Specified by:
resultStringToJava in interface FieldConverter
Throws:
SQLException


This documentation is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.