net.sourceforge.sqlunit.utils
Class TypeUtils

java.lang.Object
  extended by net.sourceforge.sqlunit.utils.TypeUtils

public final class TypeUtils
extends java.lang.Object

Provides commonly used functionality to convert between SQL, Java and XML data type names.

Version:
$Revision: 1.7 $
Author:
Sujit Pal (spal@users.sourceforge.net)

Method Summary
static boolean checkIfNull(IType typeObj1, IType typeObj2)
          Returns true if either of the IType arguments contain null values.
static int compareNulls(IType typeObj1, IType typeObj2)
          Compares two IType object values, one or both of which is NULL.
static int computeScale(java.lang.String value)
          Computes the scale value of a value that maps to a BigDecimalType class by counting the number of digits after the decimal point.
static int convertStringToInt(java.lang.String s, int def)
          Convenience method to convert a String to an int, and replace with a default value if the conversion fails.
static java.lang.Object convertToObject(java.lang.String value, java.lang.String xmlType)
          Converts a String representation of a specific xmlType to the corresponding Java object for use by PreparedStatement and its children.
static java.lang.String convertToString(java.lang.Object obj, int sqlType)
          Converts an Object with the specified type to the corresponding String representation.
static int getSqlTypeFromXmlType(java.lang.String xmlType)
          Returns the SQL Type given the XML Type name.
static java.lang.String getXmlTypeFromSqlType(int sqlType)
          Returns the XML Type given the SQL Type Id.
static boolean hasJavaObjectSupport()
          Returns true if Java Support has been turned on.
static boolean isValueOfBigDecimalEqual(java.lang.String bd1, java.lang.String bd2)
          Returns true if both String values resolve to BigDecimal objects with identical values and scale.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getXmlTypeFromSqlType

public static java.lang.String getXmlTypeFromSqlType(int sqlType)
                                              throws SQLUnitException
Returns the XML Type given the SQL Type Id. For example, return INTEGER when passed a value of java.sql.Types.INTEGER.

Parameters:
sqlType - a valid java.sql.Types value.
Returns:
the XML Type name.
Throws:
SQLUnitException - if the sqlType is invalid. Should never occur since we call this only when parsing a Result.

getSqlTypeFromXmlType

public static int getSqlTypeFromXmlType(java.lang.String xmlType)
                                 throws SQLUnitException
Returns the SQL Type given the XML Type name. For example, returns java.sql.Types.INTEGER when passed in the String INTEGER.

Parameters:
xmlType - a valid String corresponding to a java.sql.Type constant.
Returns:
the corresponding java.sql.Types constant value.
Throws:
SQLUnitException - if the xmlType is incorrectly specified.

convertToObject

public static java.lang.Object convertToObject(java.lang.String value,
                                               java.lang.String xmlType)
                                        throws SQLUnitException
Converts a String representation of a specific xmlType to the corresponding Java object for use by PreparedStatement and its children. This is used to convert the input parameters for a Stored procedure from the String representation to the appropriate object representation.

Parameters:
value - the String representation of the value to convert.
xmlType - the XML Type of the target object.
Returns:
an Object of the required java type.
Throws:
SQLUnitException - if there was a problem converting.

convertToString

public static java.lang.String convertToString(java.lang.Object obj,
                                               int sqlType)
                                        throws SQLUnitException
Converts an Object with the specified type to the corresponding String representation. Only a few types are given special treatment by SQLUnit.

Parameters:
obj - the Object to convert to a String.
sqlType - the type of the object as defined by java.sql.Types.
Returns:
a String representation of the object.
Throws:
SQLUnitException - if there was a problem during conversion.

convertStringToInt

public static int convertStringToInt(java.lang.String s,
                                     int def)
Convenience method to convert a String to an int, and replace with a default value if the conversion fails.

Parameters:
s - the String to convert to an int.
def - the default int value to use.
Returns:
the int after the conversion.

hasJavaObjectSupport

public static boolean hasJavaObjectSupport()
Returns true if Java Support has been turned on.

Returns:
true if Java Support is turned on, else false.

computeScale

public static int computeScale(java.lang.String value)
Computes the scale value of a value that maps to a BigDecimalType class by counting the number of digits after the decimal point.

Parameters:
value - the String value of a parameter that maps to the BigDecimalType object.
Returns:
the scale of this value.

isValueOfBigDecimalEqual

public static boolean isValueOfBigDecimalEqual(java.lang.String bd1,
                                               java.lang.String bd2)
Returns true if both String values resolve to BigDecimal objects with identical values and scale.

Parameters:
bd1 - the String value of a BigDecimal number.
bd2 - the String value of another BigDecimal number.
Returns:
true if the two numbers are equal.

checkIfNull

public static boolean checkIfNull(IType typeObj1,
                                  IType typeObj2)
Returns true if either of the IType arguments contain null values.

Parameters:
typeObj1 - an IType object to compare.
typeObj2 - another IType object to compare.
Returns:
true if either of the IType objects contain null values.

compareNulls

public static int compareNulls(IType typeObj1,
                               IType typeObj2)
Compares two IType object values, one or both of which is NULL. If both are null, then returns 0. If value of typeObj1 is NULL, then returns -1, else returns 1. We assume that the order of precedence in the comparison is that NULL is lesser of the two.

Parameters:
typeObj1 - an IType object to compare.
typeObj2 - another IType object to compare.
Returns:
true -1, 0, or 1, according as value of typeObj1 is less than, equal to or greater than typeObj2.