[#456] Add runtime support for PRECISION, SCALE, and LENGTH attributes -
Added the precision/scale/length API
This commit is contained in:
parent
e9a2308105
commit
3d63aa82a0
@ -181,6 +181,79 @@ public interface DataType<T> extends Serializable {
|
||||
*/
|
||||
List<T> convert(Collection<?> objects);
|
||||
|
||||
/**
|
||||
* Return a new data type like this, with a new precision value
|
||||
* <p>
|
||||
* This will have no effect if {@link #hasPrecision()} is <code>false</code>
|
||||
*
|
||||
* @param precision The new precision value
|
||||
* @return The new data type
|
||||
*/
|
||||
DataType<T> precision(int precision);
|
||||
|
||||
/**
|
||||
* Get the precision of this data type
|
||||
*
|
||||
* @return The precision of this data type
|
||||
*/
|
||||
int precision();
|
||||
|
||||
/**
|
||||
* Whether this data type has a precision
|
||||
*
|
||||
* @return Whether this data type has a precision
|
||||
*/
|
||||
boolean hasPrecision();
|
||||
|
||||
/**
|
||||
* Return a new data type like this, with a new scale value
|
||||
* <p>
|
||||
* This will have no effect if {@link #hasScale()} is <code>false</code>
|
||||
*
|
||||
* @param scale The new scale value
|
||||
* @return The new data type
|
||||
*/
|
||||
DataType<T> scale(int scale);
|
||||
|
||||
/**
|
||||
* Get the scale of this data type
|
||||
*
|
||||
* @return The scale of this data type
|
||||
*/
|
||||
int scale();
|
||||
|
||||
/**
|
||||
* Whether this data type has a scale
|
||||
*
|
||||
* @return Whether this data type has a scale
|
||||
*/
|
||||
boolean hasScale();
|
||||
|
||||
/**
|
||||
* Return a new data type like this, with a new length value
|
||||
* <p>
|
||||
* This will have no effect if {@link #hasLength()} is <code>false</code>
|
||||
*
|
||||
* @param length The new length value
|
||||
* @return The new data type
|
||||
*/
|
||||
DataType<T> length(int length);
|
||||
|
||||
/**
|
||||
* Get the length of this data type
|
||||
*
|
||||
* @return The length of this data type
|
||||
*/
|
||||
int length();
|
||||
|
||||
/**
|
||||
* Whether this data type has a length
|
||||
*
|
||||
* @return Whether this data type has a length
|
||||
*/
|
||||
boolean hasLength();
|
||||
|
||||
|
||||
/**
|
||||
* Whether this data type is any numeric data type.
|
||||
* <p>
|
||||
|
||||
@ -189,22 +189,22 @@ public class DefaultDataType<T> implements DataType<T> {
|
||||
}
|
||||
|
||||
public DefaultDataType(SQLDialect dialect, DataType<T> sqlDataType, String typeName) {
|
||||
this(dialect, sqlDataType, sqlDataType.getType(), typeName, typeName);
|
||||
this(dialect, sqlDataType, sqlDataType.getType(), typeName, typeName, 0, 0, 0);
|
||||
}
|
||||
|
||||
public DefaultDataType(SQLDialect dialect, DataType<T> sqlDataType, String typeName, String castTypeName) {
|
||||
this(dialect, sqlDataType, sqlDataType.getType(), typeName, castTypeName);
|
||||
this(dialect, sqlDataType, sqlDataType.getType(), typeName, castTypeName, 0, 0, 0);
|
||||
}
|
||||
|
||||
public DefaultDataType(SQLDialect dialect, Class<T> type, String typeName) {
|
||||
this(dialect, null, type, typeName, typeName);
|
||||
this(dialect, null, type, typeName, typeName, 0, 0, 0);
|
||||
}
|
||||
|
||||
public DefaultDataType(SQLDialect dialect, Class<T> type, String typeName, String castTypeName) {
|
||||
this(dialect, null, type, typeName, castTypeName);
|
||||
this(dialect, null, type, typeName, castTypeName, 0, 0, 0);
|
||||
}
|
||||
|
||||
private DefaultDataType(SQLDialect dialect, DataType<T> sqlDataType, Class<T> type, String typeName, String castTypeName) {
|
||||
private DefaultDataType(SQLDialect dialect, DataType<T> sqlDataType, Class<T> type, String typeName, String castTypeName, int precision, int scale, int length) {
|
||||
this.dialect = dialect;
|
||||
|
||||
// [#858] SQLDataTypes should reference themselves for more convenience
|
||||
@ -214,9 +214,9 @@ public class DefaultDataType<T> implements DataType<T> {
|
||||
this.castTypeName = castTypeName;
|
||||
this.arrayType = (Class<T[]>) Array.newInstance(type, 0).getClass();
|
||||
|
||||
this.precision = 0;
|
||||
this.scale = 0;
|
||||
this.length = 0;
|
||||
this.precision = precision;
|
||||
this.scale = scale;
|
||||
this.length = length;
|
||||
|
||||
init();
|
||||
}
|
||||
@ -247,6 +247,74 @@ public class DefaultDataType<T> implements DataType<T> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public final DataType<T> precision(int p) {
|
||||
if (hasPrecision()) {
|
||||
return new DefaultDataType<T>(dialect, sqlDataType, type, typeName, castTypeName, p, scale, length);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int precision() {
|
||||
return precision;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean hasPrecision() {
|
||||
return sqlDataType == SQLDataType.DECIMAL
|
||||
|| sqlDataType == SQLDataType.DECIMAL_INTEGER
|
||||
|| sqlDataType == SQLDataType.NUMERIC;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final DataType<T> scale(int s) {
|
||||
if (hasScale()) {
|
||||
return new DefaultDataType<T>(dialect, sqlDataType, type, typeName, castTypeName, precision, s, length);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int scale() {
|
||||
return scale;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean hasScale() {
|
||||
return hasPrecision();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final DataType<T> length(int l) {
|
||||
if (hasLength()) {
|
||||
return new DefaultDataType<T>(dialect, sqlDataType, type, typeName, castTypeName, precision, scale, l);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int length() {
|
||||
return length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean hasLength() {
|
||||
return sqlDataType == SQLDataType.BINARY
|
||||
|| sqlDataType == SQLDataType.BIT
|
||||
|| sqlDataType == SQLDataType.CHAR
|
||||
|| sqlDataType == SQLDataType.LONGNVARCHAR
|
||||
|| sqlDataType == SQLDataType.LONGVARBINARY
|
||||
|| sqlDataType == SQLDataType.LONGVARCHAR
|
||||
|| sqlDataType == SQLDataType.NCHAR
|
||||
|| sqlDataType == SQLDataType.NVARCHAR
|
||||
|| sqlDataType == SQLDataType.VARBINARY
|
||||
|| sqlDataType == SQLDataType.VARCHAR;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final DataType<T> getSQLDataType() {
|
||||
return sqlDataType;
|
||||
@ -437,12 +505,12 @@ public class DefaultDataType<T> implements DataType<T> {
|
||||
|
||||
@Override
|
||||
public final <A extends ArrayRecord<T>> DataType<A> asArrayDataType(Class<A> arrayDataType) {
|
||||
return new DefaultDataType<A>(dialect, null, arrayDataType, typeName, castTypeName);
|
||||
return new DefaultDataType<A>(dialect, arrayDataType, typeName, castTypeName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final <E extends EnumType> DataType<E> asEnumDataType(Class<E> enumDataType) {
|
||||
return new DefaultDataType<E>(dialect, null, enumDataType, typeName, castTypeName);
|
||||
return new DefaultDataType<E>(dialect, enumDataType, typeName, castTypeName);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -481,11 +549,11 @@ public class DefaultDataType<T> implements DataType<T> {
|
||||
}
|
||||
|
||||
public static DataType<Object> getDefaultDataType(String typeName) {
|
||||
return new DefaultDataType<Object>(SQLDialect.SQL99, null, Object.class, typeName, typeName);
|
||||
return new DefaultDataType<Object>(SQLDialect.SQL99, Object.class, typeName, typeName);
|
||||
}
|
||||
|
||||
public static DataType<Object> getDefaultDataType(SQLDialect dialect, String typeName) {
|
||||
return new DefaultDataType<Object>(dialect, null, Object.class, typeName, typeName);
|
||||
return new DefaultDataType<Object>(dialect, Object.class, typeName, typeName);
|
||||
}
|
||||
|
||||
public static DataType<?> getDataType(SQLDialect dialect, String typeName) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user