Package com.mckoi.database
Class Caster
- java.lang.Object
-
- com.mckoi.database.Caster
-
public class Caster extends java.lang.Object
Methods to choose and perform casts from database type to Java types.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.Class[]
bigClasses
private static java.lang.String[]
bigPrims
private static java.lang.Class[]
bitClasses
private static java.lang.String[]
bitPrims
private static java.lang.Class[]
dateClasses
private static java.lang.String[]
datePrims
private static java.lang.Class[]
doubleClasses
private static java.lang.String[]
doublePrims
private static java.lang.Class[]
floatClasses
private static java.lang.String[]
floatPrims
private static java.lang.Class[]
intClasses
private static java.lang.String[]
intPrims
private static BigNumber
maxBigNumByte
The maximum positive byte value as a BigNumber.private static BigNumber
maxBigNumDouble
The maximum positive double value as a BigNumber.private static BigNumber
maxBigNumFloat
The maximum positive float value as a BigNumber.private static BigNumber
maxBigNumInt
The maximum positive integer value as a BigNumber.private static BigNumber
maxBigNumLong
The maximum positive long value as a BigNumber.private static BigNumber
maxBigNumShort
The maximum positive short value as a BigNumber.private static BigNumber
minBigNumByte
The maximum positive byte value as a BigNumber.private static BigNumber
minBigNumFloat
The minimum positive float value as a BigNumber.private static BigNumber
minBigNumInt
The maximum positive integer value as a BigNumber.private static BigNumber
minBigNumLong
The maximum positive long value as a BigNumber.private static BigNumber
minBigNumShort
The maximum positive short value as a BigNumber.static int
OBJECT_COST
The cost to cast to the closes Java object type.static int
PRIMITIVE_COST
The cost to cast to the closest Java primitive type.private static java.lang.Class[]
smallClasses
private static java.lang.String[]
smallPrims
private static java.lang.Class[]
stringClasses
private static java.lang.String[]
stringPrims
private static java.lang.Class[]
timeClasses
private static java.lang.String[]
timePrims
private static java.lang.Class[]
timestampClasses
private static java.lang.String[]
timestampPrims
private static java.lang.Class[]
tinyClasses
private static java.lang.String[]
tinyPrims
-
Constructor Summary
Constructors Constructor Description Caster()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description (package private) static java.lang.Object
castArg(TObject arg, java.lang.Class target)
Cast the object to the specified target.(package private) static java.lang.Object[]
castArgs(TObject[] args, java.lang.Class[] targets)
Cast the given arguments to the specified classes.static java.lang.Object[]
castArgsToConstructor(TObject[] args, java.lang.reflect.Constructor constructor)
Cast the given arguments to the specified constructors parameter types.static void
deserializeJavaObjects(TObject[] args)
Find any JAVA_OBJECTs in the args and deserialize them into real Java objects.static java.lang.reflect.Constructor
findBestConstructor(java.lang.reflect.Constructor[] constructs, TObject[] args)
Search for the best constructor that we can use with the given argument types.static java.lang.String
getArgTypesString(TObject[] args)
Get a string giving the database types of all of the arguments.(package private) static int
getCastingCost(TObject[] args, int[] argSqlTypes, java.lang.Class[] targets)
Get the cost for casting the given arg types to the desired target classes.(package private) static int
getCastingCost(TObject arg, int argSqlType, java.lang.Class target)
Get the cost to cast an SQL type to the desired target class.(package private) static int
getCastingCost(TObject arg, java.lang.String[] prims, java.lang.Class[] objects, java.lang.Class target)
Get the cost to cast to the specified target from the set of allowable primitives and object classes.static int
getSqlType(TObject arg)
Get the SQL type for a database argument.static int[]
getSqlTypes(TObject[] args)
Get the SQL types for the given database arguments.
-
-
-
Field Detail
-
PRIMITIVE_COST
public static final int PRIMITIVE_COST
The cost to cast to the closest Java primitive type.- See Also:
- Constant Field Values
-
OBJECT_COST
public static final int OBJECT_COST
The cost to cast to the closes Java object type.- See Also:
- Constant Field Values
-
maxBigNumByte
private static final BigNumber maxBigNumByte
The maximum positive byte value as a BigNumber.
-
minBigNumByte
private static final BigNumber minBigNumByte
The maximum positive byte value as a BigNumber.
-
maxBigNumShort
private static final BigNumber maxBigNumShort
The maximum positive short value as a BigNumber.
-
minBigNumShort
private static final BigNumber minBigNumShort
The maximum positive short value as a BigNumber.
-
maxBigNumInt
private static final BigNumber maxBigNumInt
The maximum positive integer value as a BigNumber.
-
minBigNumInt
private static final BigNumber minBigNumInt
The maximum positive integer value as a BigNumber.
-
maxBigNumLong
private static final BigNumber maxBigNumLong
The maximum positive long value as a BigNumber.
-
minBigNumLong
private static final BigNumber minBigNumLong
The maximum positive long value as a BigNumber.
-
maxBigNumFloat
private static final BigNumber maxBigNumFloat
The maximum positive float value as a BigNumber.
-
minBigNumFloat
private static BigNumber minBigNumFloat
The minimum positive float value as a BigNumber.
-
maxBigNumDouble
private static BigNumber maxBigNumDouble
The maximum positive double value as a BigNumber.
-
bitPrims
private static java.lang.String[] bitPrims
-
bitClasses
private static java.lang.Class[] bitClasses
-
tinyPrims
private static java.lang.String[] tinyPrims
-
tinyClasses
private static java.lang.Class[] tinyClasses
-
smallPrims
private static java.lang.String[] smallPrims
-
smallClasses
private static java.lang.Class[] smallClasses
-
intPrims
private static java.lang.String[] intPrims
-
intClasses
private static java.lang.Class[] intClasses
-
bigPrims
private static java.lang.String[] bigPrims
-
bigClasses
private static java.lang.Class[] bigClasses
-
floatPrims
private static java.lang.String[] floatPrims
-
floatClasses
private static java.lang.Class[] floatClasses
-
doublePrims
private static java.lang.String[] doublePrims
-
doubleClasses
private static java.lang.Class[] doubleClasses
-
stringPrims
private static java.lang.String[] stringPrims
-
stringClasses
private static java.lang.Class[] stringClasses
-
datePrims
private static java.lang.String[] datePrims
-
dateClasses
private static java.lang.Class[] dateClasses
-
timePrims
private static java.lang.String[] timePrims
-
timeClasses
private static java.lang.Class[] timeClasses
-
timestampPrims
private static java.lang.String[] timestampPrims
-
timestampClasses
private static java.lang.Class[] timestampClasses
-
-
Method Detail
-
deserializeJavaObjects
public static void deserializeJavaObjects(TObject[] args)
Find any JAVA_OBJECTs in the args and deserialize them into real Java objects.- Parameters:
args
- The args to deserialize. Any JAVA_OBJECT args are converted in-place to a new TObject with a value which is the deserialized object.
-
findBestConstructor
public static java.lang.reflect.Constructor findBestConstructor(java.lang.reflect.Constructor[] constructs, TObject[] args)
Search for the best constructor that we can use with the given argument types.- Parameters:
constructs
- The set of constructors from which to select.argSqlTypes
- The SQL types of the database arguments to be passed to the constructor.- Returns:
- The constructor with the lowest cost, or null if there are no constructors that match the args.
-
getSqlTypes
public static int[] getSqlTypes(TObject[] args)
Get the SQL types for the given database arguments.- Parameters:
args
- The database args.- Returns:
- The SQL types of the args.
-
getSqlType
public static int getSqlType(TObject arg)
Get the SQL type for a database argument. If the actual value does not fit into the declared type, the returned type is widened as required for the value to fit.- Parameters:
arg
- The database argument.- Returns:
- The SQL type of the arg.
-
getArgTypesString
public static java.lang.String getArgTypesString(TObject[] args)
Get a string giving the database types of all of the arguments. Useful for error messages.- Parameters:
args
- The arguments.- Returns:
- A string with the types of all of the arguments, using comma as a separator.
-
getCastingCost
static int getCastingCost(TObject[] args, int[] argSqlTypes, java.lang.Class[] targets)
Get the cost for casting the given arg types to the desired target classes.- Parameters:
args
- The database arguments from which we are casting.argSqlTypes
- The SQL types of the args.targets
- The java classes to which we are casting.- Returns:
- The cost of doing the cast for all arguments, or -1 if the args can not be cast to the targets.
-
getCastingCost
static int getCastingCost(TObject arg, int argSqlType, java.lang.Class target)
Get the cost to cast an SQL type to the desired target class. The cost is 0 to cast to TObject, 100 to cast to the closest primitive, or 200 to cast to the closest Object, plus 1 for each widening away from the closest.- Parameters:
arg
- The argument to cast.argSqlType
- The SQL type of the arg.target
- The target to which to cast.- Returns:
- The cost to do the cast, or -1 if the cast can not be done.
-
getCastingCost
static int getCastingCost(TObject arg, java.lang.String[] prims, java.lang.Class[] objects, java.lang.Class target)
Get the cost to cast to the specified target from the set of allowable primitives and object classes.- Parameters:
arg
- The value being cast.prims
- The set of allowable Java primitive types to which we can cast, ordered with the preferred types first. If the value of the arg is null, it can not be cast to a primitive type.objects
- The set of allowable Java Object types to which we can cast, ordered with the preferred types first.target
- The target class to which we are casting.- Returns:
- The cost of the cast, or -1 if the cast is not allowed.
-
castArgsToConstructor
public static java.lang.Object[] castArgsToConstructor(TObject[] args, java.lang.reflect.Constructor constructor)
Cast the given arguments to the specified constructors parameter types. The caller must already have checked to make sure the argument count and types match the constructor.- Parameters:
args
- The database arguments from which to cast.constructor
- The constructor to which to cast.- Returns:
- The cast arguments.
-
castArgs
static java.lang.Object[] castArgs(TObject[] args, java.lang.Class[] targets)
Cast the given arguments to the specified classes. The caller must already have checked to make sure the argument count and types match the constructor.- Parameters:
args
- The database arguments from which to cast.targets
- The java classes to which to cast.- Returns:
- The cast arguments.
-
castArg
static java.lang.Object castArg(TObject arg, java.lang.Class target)
Cast the object to the specified target.- Parameters:
arg
- The database argumument from which to cast.target
- The java class to which to cast.- Returns:
- The cast object.
-
-