public ColumnBuilder(String name) {
this(name, null);
}
-
+
public ColumnBuilder(String name, DataType type) {
_name = name;
_type = type;
public ColumnBuilder setSQLType(int type) throws SQLException {
return setSQLType(type, 0, null);
}
-
+
/**
* Sets the type for the new column based on the given SQL type and target
* data length (in type specific units).
* Sets the length (in type specific units) for the new column.
*/
public ColumnBuilder setLengthInUnits(int unitLength) {
- return setLength(_type.getUnitSize() * unitLength);
+ return setLength(_type.fromUnitSize(unitLength));
}
-
+
/**
* Sets the length for the new column to the max length for the type. Does
* nothing for types which are not variable length.
}
return this;
}
-
+
/**
* Sets whether of not the new column is an auto-number column.
*/
private PropertyMap.Property getProperty(String name) {
return ((_props != null) ? _props.get(name) : null);
}
-
+
/**
* Sets all attributes except name from the given Column template (including
* all column properties except GUID).
*/
- public ColumnBuilder setFromColumn(Column template)
+ public ColumnBuilder setFromColumn(Column template)
throws IOException
{
DataType type = template.getType();
setProperty(colProp.getName(), colProp);
}
}
-
+
return this;
}
if(template._props != null) {
_props = new HashMap<String,PropertyMap.Property>(template._props);
}
-
+
return this;
}
throw new IllegalArgumentException(withErrorContext(
"Database format " + format + " does not support type " + getType()));
}
-
+
if(!getType().isVariableLength()) {
if(getLength() < getType().getFixedSize()) {
throw new IllegalArgumentException(withErrorContext(
if(!getType().isValidPrecision(getPrecision())) {
throw new IllegalArgumentException(withErrorContext(
"Precision must be from " + getType().getMinPrecision() + " to " +
- getType().getMaxPrecision() + " inclusive, found " +
+ getType().getMaxPrecision() + " inclusive, found " +
getPrecision()));
}
}
/**
* Supported access data types.
- *
+ *
* @author Tim McCune
* @usage _general_class_
*/
* null}. Equivalent to SQL {@link Types#VARCHAR}, {@link Types#CHAR}.
*/
TEXT((byte) 0x0A, Types.VARCHAR, null, true, false, 0,
- JetFormat.TEXT_FIELD_MAX_LENGTH, JetFormat.TEXT_FIELD_MAX_LENGTH,
+ JetFormat.TEXT_FIELD_MAX_LENGTH, JetFormat.TEXT_FIELD_MAX_LENGTH,
JetFormat.TEXT_FIELD_UNIT_SIZE),
/**
* Corresponds to a java {@code byte[]} of max length 16777215 bytes.
* Complex type corresponds to a special {@link #LONG} autonumber field
* which is the key for a secondary table which holds the "real" data.
*/
- COMPLEX_TYPE((byte) 0x12, null, 4),
+ COMPLEX_TYPE((byte) 0x12, null, 4),
/**
* Corresponds to a java {@link Long}. Accepts any {@link Number} (using
* {@link Number#longValue}), Boolean as 1 or 0, any Object converted to a
addNewSqlType("TIME_WITH_TIMEZONE", SHORT_DATE_TIME, null);
addNewSqlType("TIMESTAMP_WITH_TIMEZONE", SHORT_DATE_TIME, null);
}
-
+
private static Map<Byte, DataType> DATA_TYPES = new HashMap<Byte, DataType>();
static {
for (DataType type : DataType.values()) {
private final int _maxPrecision;
/** the number of bytes per "unit" for this data type */
private final int _unitSize;
-
+
private DataType(byte value) {
this(value, null, null);
}
-
+
private DataType(byte value, Integer sqlType, Integer fixedSize) {
this(value, sqlType, fixedSize, false, false, 0, 0, 0, 1);
}
minSize, defaultSize, maxSize,
false, 0, 0, 0, 0, 0, 0, unitSize);
}
-
+
private DataType(byte value, Integer sqlType, Integer fixedSize,
boolean variableLength,
boolean longValue,
_maxPrecision = maxPrecision;
_unitSize = unitSize;
}
-
+
public byte getValue() {
return _value;
}
-
+
public boolean isVariableLength() {
return _variableLength;
}
// e.g. NUMERIC
return (isVariableLength() && (getMinSize() != getMaxSize()));
}
-
+
public boolean isLongValue() {
return _longValue;
}
public int getFixedSize() {
return getFixedSize(null);
}
-
+
public int getFixedSize(Short colLength) {
if(_fixedSize != null) {
if(colLength != null) {
if(colLength != null) {
return colLength;
}
- throw new IllegalArgumentException("Unexpected fixed length column " +
+ throw new IllegalArgumentException("Unexpected fixed length column " +
this);
}
public int getMaxSize() {
return _maxSize;
}
-
+
public int getSQLType() throws SQLException {
if (_sqlType != null) {
return _sqlType;
public int getDefaultScale() {
return _defaultScale;
}
-
+
public int getMaxScale() {
return _maxScale;
}
-
+
public int getMinPrecision() {
return _minPrecision;
}
-
+
public int getDefaultPrecision() {
return _defaultPrecision;
}
-
+
public int getMaxPrecision() {
return _maxPrecision;
}
return _unitSize;
}
- public int toUnitSize(int size)
- {
+ public int toUnitSize(int size) {
return(size / getUnitSize());
}
- public int fromUnitSize(int unitSize)
- {
+ public int fromUnitSize(int unitSize) {
return(unitSize * getUnitSize());
}
private static boolean isWithinRange(int value, int minValue, int maxValue) {
return((value >= minValue) && (value <= maxValue));
}
-
+
public int toValidSize(int size) {
return toValidRange(size, getMinSize(), getMaxSize());
}
public boolean isUnsupported() {
return((this == UNSUPPORTED_FIXEDLEN) || (this == UNSUPPORTED_VARLEN));
}
-
+
private static int toValidRange(int value, int minValue, int maxValue) {
return((value > maxValue) ? maxValue :
((value < minValue) ? minValue : value));
}
-
+
public static DataType fromByte(byte b) throws IOException {
DataType rtn = DATA_TYPES.get(b);
if (rtn != null) {
}
throw new IOException("Unrecognized data type: " + b);
}
-
+
public static DataType fromSQLType(int sqlType)
throws SQLException
{
return fromSQLType(sqlType, 0, null);
}
-
+
public static DataType fromSQLType(int sqlType, int lengthInUnits)
throws SQLException
{
}
// make sure size is reasonable
- int size = lengthInUnits * rtn.getUnitSize();
+ int size = rtn.fromUnitSize(lengthInUnits);
if(rtn.isVariableLength() && !rtn.isValidSize(size)) {
// try alternate type. we always accept alternate "long value" types
// regardless of the given lengthInUnits
rtn = altRtn;
}
}
-
+
return rtn;
}
* Adds mappings for a sql type which was added after jdk 1.5 (using
* reflection).
*/
- private static void addNewSqlType(String typeName, DataType type,
+ private static void addNewSqlType(String typeName, DataType type,
DataType altType)
{
try {