Class SQLDecimal
- All Implemented Interfaces:
Externalizable
,Serializable
,Comparable
,Formatable
,Storable
,TypedFormat
,DataValueDescriptor
,NumberDataValue
,Orderable
,VariableSizeDataValue
Because OrderableDataType is a subtype of DataType, SQLDecimal can play a role in either a DataType/Row or a OrderableDataType/Row, interchangeably.
We assume the store has a flag for nullness of the value, and simply return a 0-length array for the stored form when the value is null.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
private static final int
private byte[]
See comments for valueprivate int
See comments for valueprivate BigDecimal
object state.Fields inherited from class org.apache.derby.iapi.types.NumberDataType
MAXLONG_PLUS_ONE, MINLONG_MINUS_ONE
Fields inherited from interface org.apache.derby.iapi.types.DataValueDescriptor
UNKNOWN_LOGICAL_LENGTH
Fields inherited from interface org.apache.derby.iapi.types.NumberDataValue
MAX_DECIMAL_PRECISION_SCALE, MIN_DECIMAL_DIVIDE_SCALE
Fields inherited from interface org.apache.derby.iapi.types.Orderable
ORDER_OP_EQUALS, ORDER_OP_GREATEROREQUALS, ORDER_OP_GREATERTHAN, ORDER_OP_LESSOREQUALS, ORDER_OP_LESSTHAN
Fields inherited from interface org.apache.derby.iapi.types.VariableSizeDataValue
IGNORE_PRECISION
-
Constructor Summary
ConstructorsConstructorDescriptionno-arg constructor, required by FormattableSQLDecimal
(String val) SQLDecimal
(BigDecimal val) SQLDecimal
(BigDecimal val, int nprecision, int scale) -
Method Summary
Modifier and TypeMethodDescriptioncloneValue
(boolean forceMaterialization) Clone this DataValueDescriptor.divide
(NumberDataValue dividend, NumberDataValue divisor, NumberDataValue result) This method implements the / operator for BigDecimal/BigDecimaldivide
(NumberDataValue dividend, NumberDataValue divisor, NumberDataValue result, int scale) This method implements the / operator for BigDecimal/BigDecimalint
Estimate the memory usage in bytes of the data value and the overhead of the class.private BigDecimal
static BigDecimal
getBigDecimal
(DataValueDescriptor value) Get a BigDecimal representing the value of a DataValueDescriptorboolean
Gets the value in the data value descriptor as a boolean.byte
getByte()
Gets the value in the data value descriptor as a byte.int
Return the SQL scale of this value, number of digits after the decimal point, or zero for a whole number.int
Return the SQL scale of this value, number of digits after the decimal point, or zero for a whole number.double
If we have a value that is greater than the maximum double, exception is thrown.float
getFloat()
Gets the value in the data value descriptor as a float.int
getInt()
Gets the value in the data value descriptor as a int.int
Gets the length of the data value.long
getLong()
Gets the value in the data value descriptor as a long.Get a new null value of the same type as this data value.Gets the value in the data value descriptor as a int.short
getShort()
Gets the value in the data value descriptor as a short.Gets the value in the data value descriptor as a String.int
Return my format identifier.Get the SQL name of the datatypeprivate static int
getWholeDigits
(BigDecimal decimalValue) Calculate the number of digits to the left of the decimal point of the passed in value.int
hashCode()
protected boolean
This method implements the isNegative method.boolean
isNull()
Return whether the value is null or not.minus
(NumberDataValue result) This method implements the unary minus operator for double.minus
(NumberDataValue left, NumberDataValue right, NumberDataValue result) This method implements the - operator for "decimal - decimal".void
normalize
(DataTypeDescriptor desiredType, DataValueDescriptor source) Normalization method - this method may be called when putting a value into a SQLDecimal, for example, when inserting into a SQLDecimal column.plus
(NumberDataValue addend1, NumberDataValue addend2, NumberDataValue result) This method implements the + operator for DECIMAL.void
Note the use of rawData: we reuse the array if the incoming array is the same length or smaller than the array length.void
Restore this object to its (SQL)null value.void
setBigDecimal
(BigDecimal theValue) Only to be called when the application sets a value using BigDecimal through setBigDecimal calls.private void
setCoreValue
(double theValue) private void
setCoreValue
(BigDecimal theValue) protected void
setFrom
(DataValueDescriptor theValue) Set the value of this DataValueDescriptor based on the value of the specified DataValueDescriptor.final void
setInto
(PreparedStatement ps, int position) Set the value into a PreparedStatement.(package private) void
Set the value from a correctly typed BigDecimal object.void
setValue
(boolean theValue) Set the value.void
setValue
(double theValue) Set the value of this DataValueDescriptor to the given double value At DataType level just throws an error lower classes will overridevoid
setValue
(float theValue) Set the value of this DataValueDescriptor to the given float value At DataType level just throws an error lower classes will overridevoid
setValue
(int theValue) Set the value of this DataValueDescriptor to the given int value At DataType level just throws an error lower classes will overridevoid
setValue
(long theValue) Set the value of this DataValueDescriptor to the given long value At DataType level just throws an error lower classes will overridevoid
Called when setting a DECIMAL value internally or from through a procedure or function.void
WARNING there is no checking to make sure that theValue doesn't exceed the precision/scale of the current SQLDecimal.void
setValueFromResultSet
(ResultSet resultSet, int colNumber, boolean isNullable) Set the value based on the value for the specified DataValueDescriptor from the specified ResultSet.void
setWidth
(int desiredPrecision, int desiredScale, boolean errorOnTrunc) Set the precision/scale of the to the desired values.times
(NumberDataValue left, NumberDataValue right, NumberDataValue result) This method implements the * operator for "double * double".toString()
protected int
Compare this (not null) to a non-null value.int
Each built-in type in JSQL has a precedence.int
DECIMAL implementation.void
Distill the BigDecimal to a byte array and write out: scale (zero or positive) as a byte length of byte array as a byte the byte arrayMethods inherited from class org.apache.derby.iapi.types.NumberDataType
absolute, compare, compare, mod, normalizeDOUBLE, normalizeREAL, normalizeREAL, objectNull, setValue, setValue, sqrt
Methods inherited from class org.apache.derby.iapi.types.DataType
checkHostVariable, cloneHolder, coalesce, compare, compare, compareTo, dataTypeConversion, equals, equals, flip, genericSetObject, getBytes, getDate, getStream, getTime, getTimestamp, getTraceString, getTypeName, greaterOrEquals, greaterThan, hasStream, in, invalidFormat, isNotNull, isNullOp, lessOrEquals, lessThan, notEquals, outOfRange, readExternalFromArray, recycle, setInto, setObjectForCast, setToNull, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, throwLangSetMismatch, throwLangSetMismatch
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.apache.derby.iapi.types.DataValueDescriptor
checkHostVariable, cloneHolder, coalesce, compare, compare, equals, getBytes, getDate, getStream, getTime, getTimestamp, getTraceString, greaterOrEquals, greaterThan, hasStream, in, isNotNull, isNullOp, lessOrEquals, lessThan, notEquals, readExternalFromArray, recycle, setInto, setObjectForCast, setToNull, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue
-
Field Details
-
value
object state. Note that scale and precision are always determined dynamically from value when it is not null. The field value can be null without the data value being null. In this case the value is stored in rawData and rawScale. This is to allow the minimal amount of work to read a SQLDecimal from disk. Creating the BigDecimal is expensive as it requires allocating three objects, the last two are a waste in the case the row does not qualify or the row will be written out by the sorter before being returned to the application.This means that this field must be accessed for read indirectly through the getBigDecimal() method, and when setting it the rawData field must be set to null.
-
rawData
private byte[] rawDataSee comments for value -
rawScale
private int rawScaleSee comments for value -
BASE_MEMORY_USAGE
private static final int BASE_MEMORY_USAGE -
BIG_DECIMAL_MEMORY_USAGE
private static final int BIG_DECIMAL_MEMORY_USAGE
-
-
Constructor Details
-
SQLDecimal
public SQLDecimal()no-arg constructor, required by Formattable -
SQLDecimal
-
SQLDecimal
- Throws:
StandardException
-
SQLDecimal
-
-
Method Details
-
estimateMemoryUsage
public int estimateMemoryUsage()Description copied from interface:DataValueDescriptor
Estimate the memory usage in bytes of the data value and the overhead of the class.- Specified by:
estimateMemoryUsage
in interfaceDataValueDescriptor
- Returns:
- the estimated memory usage
-
getInt
Description copied from class:DataType
Gets the value in the data value descriptor as a int. Throws an exception if the data value is not receivable as a int.- Specified by:
getInt
in interfaceDataValueDescriptor
- Overrides:
getInt
in classDataType
- Returns:
- The data value as a int.
- Throws:
StandardException
- thrown on failure to convert
-
getByte
Description copied from class:DataType
Gets the value in the data value descriptor as a byte. Throws an exception if the data value is not receivable as a byte.- Specified by:
getByte
in interfaceDataValueDescriptor
- Overrides:
getByte
in classDataType
- Returns:
- The data value as a byte.
- Throws:
StandardException
- thrown on failure to convert
-
getShort
Description copied from class:DataType
Gets the value in the data value descriptor as a short. Throws an exception if the data value is not receivable as a short.- Specified by:
getShort
in interfaceDataValueDescriptor
- Overrides:
getShort
in classDataType
- Returns:
- The data value as a short.
- Throws:
StandardException
- thrown on failure to convert
-
getLong
Description copied from class:DataType
Gets the value in the data value descriptor as a long. Throws an exception if the data value is not receivable as a long.- Specified by:
getLong
in interfaceDataValueDescriptor
- Overrides:
getLong
in classDataType
- Returns:
- The data value as a long.
- Throws:
StandardException
- thrown on failure to convert
-
getFloat
Description copied from class:DataType
Gets the value in the data value descriptor as a float. Throws an exception if the data value is not receivable as a float.- Specified by:
getFloat
in interfaceDataValueDescriptor
- Overrides:
getFloat
in classDataType
- Returns:
- The data value as a float.
- Throws:
StandardException
- thrown on failure to convert
-
getDouble
If we have a value that is greater than the maximum double, exception is thrown. Otherwise, ok. If the value is less than can be represented by a double, ti will get set to the smallest double value.- Specified by:
getDouble
in interfaceDataValueDescriptor
- Overrides:
getDouble
in classDataType
- Returns:
- The data value as a double.
- Throws:
StandardException
- thrown on failure to convert
-
getBigDecimal
-
typeToBigDecimal
public int typeToBigDecimal()DECIMAL implementation. Convert to a BigDecimal using getObject which will return a BigDecimal- Specified by:
typeToBigDecimal
in interfaceDataValueDescriptor
- Overrides:
typeToBigDecimal
in classNumberDataType
- Returns:
- Types.CHAR for String conversion through getString Types.DECIMAL for BigDecimal through getObject or Types.BIGINT for long conversion through getLong
-
getBoolean
public boolean getBoolean()Description copied from class:DataType
Gets the value in the data value descriptor as a boolean. Throws an exception if the data value is not receivable as a boolean.- Specified by:
getBoolean
in interfaceDataValueDescriptor
- Overrides:
getBoolean
in classDataType
- Returns:
- The data value as a boolean.
-
getString
Description copied from interface:DataValueDescriptor
Gets the value in the data value descriptor as a String. Throws an exception if the data value is not a string.- Specified by:
getString
in interfaceDataValueDescriptor
- Returns:
- The data value as a String.
-
getObject
Description copied from class:DataType
Gets the value in the data value descriptor as a int. Throws an exception if the data value is not receivable as a int.- Specified by:
getObject
in interfaceDataValueDescriptor
- Overrides:
getObject
in classDataType
- Returns:
- The data value as a int.
-
setObject
Set the value from a correctly typed BigDecimal object.- Overrides:
setObject
in classNumberDataType
- Throws:
StandardException
-
setFrom
Description copied from class:DataType
Set the value of this DataValueDescriptor based on the value of the specified DataValueDescriptor.- Overrides:
setFrom
in classDataType
- Parameters:
theValue
- The DataValueDescriptor that holds the value to which we want to set this DataValueDescriptor's value.- Throws:
StandardException
-
getLength
public int getLength()Description copied from interface:DataValueDescriptor
Gets the length of the data value. The meaning of this is implementation-dependent. For string types, it is the number of characters in the string. For numeric types, it is the number of bytes used to store the number. This is the actual length of this value, not the length of the type it was defined as. For example, a VARCHAR value may be shorter than the declared VARCHAR (maximum) length.- Specified by:
getLength
in interfaceDataValueDescriptor
- Returns:
- The length of the data value
-
getTypeName
Description copied from interface:DataValueDescriptor
Get the SQL name of the datatype- Specified by:
getTypeName
in interfaceDataValueDescriptor
- Returns:
- The SQL name of the datatype
-
getTypeFormatId
public int getTypeFormatId()Return my format identifier.- Specified by:
getTypeFormatId
in interfaceTypedFormat
- Returns:
- The identifier. (A UUID stuffed in an array of 16 bytes).
- See Also:
-
isNull
public boolean isNull()Description copied from interface:Storable
Return whether the value is null or not. -
writeExternal
Distill the BigDecimal to a byte array and write out:- scale (zero or positive) as a byte
- length of byte array as a byte
- the byte array
- Specified by:
writeExternal
in interfaceExternalizable
- Throws:
IOException
-
readExternal
Note the use of rawData: we reuse the array if the incoming array is the same length or smaller than the array length.- Specified by:
readExternal
in interfaceExternalizable
- Throws:
IOException
- See Also:
-
restoreToNull
public void restoreToNull()Description copied from interface:Storable
Restore this object to its (SQL)null value.- Specified by:
restoreToNull
in interfaceStorable
- See Also:
-
typeCompare
Description copied from class:NumberDataType
Compare this (not null) to a non-null value.- Specified by:
typeCompare
in classNumberDataType
- Throws:
StandardException
- Thrown on error
-
cloneValue
Description copied from interface:DataValueDescriptor
Clone this DataValueDescriptor. Results in a new object that has the same value as this but can be modified independently.Even though the objects can be modified independently regardless of the value of
forceMaterialization
, both the clone and the original may be dependent on the store state ifforceMaterialization
is set tofalse
. An example is if you need to access the value you just read usingcloneValue
after the current transaction has ended, or after the source result set has been closed.- Specified by:
cloneValue
in interfaceDataValueDescriptor
- Parameters:
forceMaterialization
- any streams representing the data value will be materialized iftrue
, the data value will be kept as a stream if possible iffalse
- Returns:
- A clone of the
DataValueDescriptor
with the same initial value as this. - See Also:
-
getNewNull
Description copied from interface:DataValueDescriptor
Get a new null value of the same type as this data value.- Specified by:
getNewNull
in interfaceDataValueDescriptor
- See Also:
-
setValueFromResultSet
public void setValueFromResultSet(ResultSet resultSet, int colNumber, boolean isNullable) throws SQLException Description copied from interface:DataValueDescriptor
Set the value based on the value for the specified DataValueDescriptor from the specified ResultSet.- Specified by:
setValueFromResultSet
in interfaceDataValueDescriptor
- Parameters:
resultSet
- The specified ResultSet.colNumber
- The 1-based column # into the resultSet.isNullable
- Whether or not the column is nullable (No need to call wasNull() if not)- Throws:
SQLException
- Thrown on error- See Also:
-
setInto
Set the value into a PreparedStatement.- Specified by:
setInto
in interfaceDataValueDescriptor
- Overrides:
setInto
in classDataType
- Throws:
SQLException
- Error setting value in PreparedStatement
-
setValue
WARNING there is no checking to make sure that theValue doesn't exceed the precision/scale of the current SQLDecimal. It is just assumed that the SQLDecimal is supposed to take the precision/scale of the BigDecimalized String.- Specified by:
setValue
in interfaceDataValueDescriptor
- Overrides:
setValue
in classDataType
- Parameters:
theValue
- The BigDecimal value to set this DataValueDescriptor to- Throws:
StandardException
- throws NumberFormatException when the String format is not recognized.
-
setValue
Description copied from class:DataType
Set the value of this DataValueDescriptor to the given double value At DataType level just throws an error lower classes will override- Specified by:
setValue
in interfaceDataValueDescriptor
- Overrides:
setValue
in classDataType
- Parameters:
theValue
- The value to set this DataValueDescriptor to- Throws:
StandardException
- Thrown on error- See Also:
-
setValue
Description copied from class:DataType
Set the value of this DataValueDescriptor to the given float value At DataType level just throws an error lower classes will override- Specified by:
setValue
in interfaceDataValueDescriptor
- Overrides:
setValue
in classDataType
- Parameters:
theValue
- The value to set this DataValueDescriptor to- Throws:
StandardException
- Thrown on error- See Also:
-
setValue
public void setValue(long theValue) Description copied from class:DataType
Set the value of this DataValueDescriptor to the given long value At DataType level just throws an error lower classes will override- Specified by:
setValue
in interfaceDataValueDescriptor
- Overrides:
setValue
in classDataType
- Parameters:
theValue
- The value to set this DataValueDescriptor to- See Also:
-
setValue
public void setValue(int theValue) Description copied from class:DataType
Set the value of this DataValueDescriptor to the given int value At DataType level just throws an error lower classes will override- Specified by:
setValue
in interfaceDataValueDescriptor
- Overrides:
setValue
in classDataType
- Parameters:
theValue
- The value to set this DataValueDescriptor to- See Also:
-
setBigDecimal
Only to be called when the application sets a value using BigDecimal through setBigDecimal calls.- Specified by:
setBigDecimal
in interfaceDataValueDescriptor
- Overrides:
setBigDecimal
in classNumberDataType
- Parameters:
theValue
- required to be a BigDecimal or null.- Throws:
StandardException
-
setValue
Called when setting a DECIMAL value internally or from through a procedure or function. Handles long in addition to BigDecimal to handle identity being stored as a long but returned as a DECIMAL.- Specified by:
setValue
in interfaceNumberDataValue
- Overrides:
setValue
in classNumberDataType
- Parameters:
theValue
- An Number containing the value to set this NumberDataValue to. Null means set the value to SQL null.- Throws:
StandardException
- Thrown on error- See Also:
-
setValue
public void setValue(boolean theValue) Description copied from class:DataType
Set the value. At DataType level just throws an error lower classes will override- Specified by:
setValue
in interfaceDataValueDescriptor
- Overrides:
setValue
in classDataType
- Parameters:
theValue
- Contains the boolean value to set this to- See Also:
-
typePrecedence
public int typePrecedence()Description copied from class:DataType
Each built-in type in JSQL has a precedence. This precedence determines how to do type promotion when using binary operators. For example, float has a higher precedence than int, so when adding an int to a float, the result type is float. The precedence for some types is arbitrary. For example, it doesn't matter what the precedence of the boolean type is, since it can't be mixed with other types. But the precedence for the number types is critical. The SQL standard requires that exact numeric types be promoted to approximate numeric when one operator uses both. Also, the precedence is arranged so that one will not lose precision when promoting a type.- Specified by:
typePrecedence
in interfaceDataValueDescriptor
- Overrides:
typePrecedence
in classDataType
- Returns:
- The precedence of this type.
- See Also:
-
setCoreValue
-
setCoreValue
private void setCoreValue(double theValue) -
normalize
public void normalize(DataTypeDescriptor desiredType, DataValueDescriptor source) throws StandardException Normalization method - this method may be called when putting a value into a SQLDecimal, for example, when inserting into a SQLDecimal column. See NormalizeResultSet in execution.Note that truncation is allowed on the decimal portion of a numeric only.
- Specified by:
normalize
in interfaceDataValueDescriptor
- Overrides:
normalize
in classDataType
- Parameters:
desiredType
- The type to normalize the source column tosource
- The value to normalize- Throws:
StandardException
- Thrown for null into non-nullable column, and for truncation error
-
plus
public NumberDataValue plus(NumberDataValue addend1, NumberDataValue addend2, NumberDataValue result) throws StandardException This method implements the + operator for DECIMAL.- Specified by:
plus
in interfaceNumberDataValue
- Overrides:
plus
in classNumberDataType
- Parameters:
addend1
- One of the addendsaddend2
- The other addendresult
- The result of a previous call to this method, null if not called yet- Returns:
- A SQLDecimal containing the result of the addition
- Throws:
StandardException
- Thrown on error
-
minus
public NumberDataValue minus(NumberDataValue left, NumberDataValue right, NumberDataValue result) throws StandardException This method implements the - operator for "decimal - decimal".- Specified by:
minus
in interfaceNumberDataValue
- Overrides:
minus
in classNumberDataType
- Parameters:
left
- The value to be subtracted fromright
- The value to be subtractedresult
- The result of a previous call to this method, null if not called yet- Returns:
- A SQLDecimal containing the result of the subtraction
- Throws:
StandardException
- Thrown on error
-
times
public NumberDataValue times(NumberDataValue left, NumberDataValue right, NumberDataValue result) throws StandardException This method implements the * operator for "double * double".- Specified by:
times
in interfaceNumberDataValue
- Parameters:
left
- The first value to be multipliedright
- The second value to be multipliedresult
- The result of a previous call to this method, null if not called yet- Returns:
- A SQLDecimal containing the result of the multiplication
- Throws:
StandardException
- Thrown on error
-
divide
public NumberDataValue divide(NumberDataValue dividend, NumberDataValue divisor, NumberDataValue result) throws StandardException This method implements the / operator for BigDecimal/BigDecimal- Specified by:
divide
in interfaceNumberDataValue
- Overrides:
divide
in classNumberDataType
- Parameters:
dividend
- The numeratordivisor
- The denominatorresult
- The result of a previous call to this method, null if not called yet- Returns:
- A SQLDecimal containing the result of the division
- Throws:
StandardException
- Thrown on error
-
divide
public NumberDataValue divide(NumberDataValue dividend, NumberDataValue divisor, NumberDataValue result, int scale) throws StandardException This method implements the / operator for BigDecimal/BigDecimal- Specified by:
divide
in interfaceNumberDataValue
- Overrides:
divide
in classNumberDataType
- Parameters:
dividend
- The numeratordivisor
- The denominatorresult
- The result of a previous call to this method, null if not called yetscale
- The result scale, if < 0, calculate the scale according to the actual values' sizes- Returns:
- A SQLDecimal containing the result of the division
- Throws:
StandardException
- Thrown on error
-
minus
This method implements the unary minus operator for double.- Specified by:
minus
in interfaceNumberDataValue
- Parameters:
result
- The result of a previous call to this method, null if not called yet- Returns:
- A SQLDecimal containing the result of the division
- Throws:
StandardException
- Thrown on error
-
isNegative
protected boolean isNegative()This method implements the isNegative method.- Specified by:
isNegative
in classNumberDataType
- Returns:
- A boolean. If this.value is negative, return true. For positive values or null, return false.
-
toString
-
hashCode
public int hashCode() -
setWidth
public void setWidth(int desiredPrecision, int desiredScale, boolean errorOnTrunc) throws StandardException Set the precision/scale of the to the desired values. Used when CASTing. Ideally we'd recycle normalize(), but the use is different.- Specified by:
setWidth
in interfaceVariableSizeDataValue
- Parameters:
desiredPrecision
- the desired precision -- IGNORE_PREICISION if it is to be ignored.desiredScale
- the desired scaleerrorOnTrunc
- throw error on truncation (ignored -- always thrown if we truncate the non-decimal part of the value)- Throws:
StandardException
- Thrown on non-zero truncation if errorOnTrunc is true
-
getDecimalValuePrecision
public int getDecimalValuePrecision()Return the SQL scale of this value, number of digits after the decimal point, or zero for a whole number. This does not match the return from BigDecimal.scale() since in J2SE 5.0 onwards that can return negative scales. -
getDecimalValueScale
public int getDecimalValueScale()Return the SQL scale of this value, number of digits after the decimal point, or zero for a whole number. This does not match the return from BigDecimal.scale() since in J2SE 5.0 onwards that can return negative scales. -
getBigDecimal
Get a BigDecimal representing the value of a DataValueDescriptor- Parameters:
value
- Non-null value to be converted- Returns:
- BigDecimal value
- Throws:
StandardException
- Invalid conversion or out of range.
-
getWholeDigits
Calculate the number of digits to the left of the decimal point of the passed in value.- Parameters:
decimalValue
- Value to get whole digits from, never null.- Returns:
- number of whole digits.
-