diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2018-12-29 05:27:46 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2018-12-29 05:27:46 +0000 |
commit | f3a4168bd5943cb6c9bf210c037fa33ac5d9f95e (patch) | |
tree | d7464919a212301be6e3b1ea2662313ea12352d4 | |
parent | cbaecd70fff730a0d6d0fb7177e95c1fd36fb761 (diff) | |
download | jackcess-f3a4168bd5943cb6c9bf210c037fa33ac5d9f95e.tar.gz jackcess-f3a4168bd5943cb6c9bf210c037fa33ac5d9f95e.zip |
use fromUnitSize
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1256 f203690c-595d-4dc9-a70b-905162fa7fd2
-rw-r--r-- | src/main/java/com/healthmarketscience/jackcess/ColumnBuilder.java | 22 | ||||
-rw-r--r-- | src/main/java/com/healthmarketscience/jackcess/DataType.java | 56 |
2 files changed, 38 insertions, 40 deletions
diff --git a/src/main/java/com/healthmarketscience/jackcess/ColumnBuilder.java b/src/main/java/com/healthmarketscience/jackcess/ColumnBuilder.java index 35aeca3..48fa1cf 100644 --- a/src/main/java/com/healthmarketscience/jackcess/ColumnBuilder.java +++ b/src/main/java/com/healthmarketscience/jackcess/ColumnBuilder.java @@ -68,7 +68,7 @@ public class ColumnBuilder { public ColumnBuilder(String name) { this(name, null); } - + public ColumnBuilder(String name, DataType type) { _name = name; _type = type; @@ -96,7 +96,7 @@ public class ColumnBuilder { 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). @@ -182,9 +182,9 @@ public class ColumnBuilder { * 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. @@ -196,7 +196,7 @@ public class ColumnBuilder { } return this; } - + /** * Sets whether of not the new column is an auto-number column. */ @@ -295,12 +295,12 @@ public class ColumnBuilder { 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(); @@ -325,7 +325,7 @@ public class ColumnBuilder { setProperty(colProp.getName(), colProp); } } - + return this; } @@ -349,7 +349,7 @@ public class ColumnBuilder { if(template._props != null) { _props = new HashMap<String,PropertyMap.Property>(template._props); } - + return this; } @@ -410,7 +410,7 @@ public class ColumnBuilder { throw new IllegalArgumentException(withErrorContext( "Database format " + format + " does not support type " + getType())); } - + if(!getType().isVariableLength()) { if(getLength() < getType().getFixedSize()) { throw new IllegalArgumentException(withErrorContext( @@ -433,7 +433,7 @@ public class ColumnBuilder { if(!getType().isValidPrecision(getPrecision())) { throw new IllegalArgumentException(withErrorContext( "Precision must be from " + getType().getMinPrecision() + " to " + - getType().getMaxPrecision() + " inclusive, found " + + getType().getMaxPrecision() + " inclusive, found " + getPrecision())); } } diff --git a/src/main/java/com/healthmarketscience/jackcess/DataType.java b/src/main/java/com/healthmarketscience/jackcess/DataType.java index 6850ab6..33496cf 100644 --- a/src/main/java/com/healthmarketscience/jackcess/DataType.java +++ b/src/main/java/com/healthmarketscience/jackcess/DataType.java @@ -30,7 +30,7 @@ import com.healthmarketscience.jackcess.impl.JetFormat; /** * Supported access data types. - * + * * @author Tim McCune * @usage _general_class_ */ @@ -104,7 +104,7 @@ public enum DataType { * 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. @@ -151,7 +151,7 @@ public enum DataType { * 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 @@ -206,7 +206,7 @@ public enum DataType { 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()) { @@ -249,11 +249,11 @@ public enum DataType { 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); } @@ -269,7 +269,7 @@ public enum DataType { minSize, defaultSize, maxSize, false, 0, 0, 0, 0, 0, 0, unitSize); } - + private DataType(byte value, Integer sqlType, Integer fixedSize, boolean variableLength, boolean longValue, @@ -301,11 +301,11 @@ public enum DataType { _maxPrecision = maxPrecision; _unitSize = unitSize; } - + public byte getValue() { return _value; } - + public boolean isVariableLength() { return _variableLength; } @@ -315,7 +315,7 @@ public enum DataType { // e.g. NUMERIC return (isVariableLength() && (getMinSize() != getMaxSize())); } - + public boolean isLongValue() { return _longValue; } @@ -327,7 +327,7 @@ public enum DataType { public int getFixedSize() { return getFixedSize(null); } - + public int getFixedSize(Short colLength) { if(_fixedSize != null) { if(colLength != null) { @@ -338,7 +338,7 @@ public enum DataType { if(colLength != null) { return colLength; } - throw new IllegalArgumentException("Unexpected fixed length column " + + throw new IllegalArgumentException("Unexpected fixed length column " + this); } @@ -353,7 +353,7 @@ public enum DataType { public int getMaxSize() { return _maxSize; } - + public int getSQLType() throws SQLException { if (_sqlType != null) { return _sqlType; @@ -368,19 +368,19 @@ public enum DataType { 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; } @@ -389,13 +389,11 @@ public enum DataType { 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()); } @@ -414,7 +412,7 @@ public enum DataType { 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()); } @@ -442,12 +440,12 @@ public enum DataType { 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) { @@ -455,13 +453,13 @@ public enum DataType { } 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 { @@ -493,7 +491,7 @@ public enum DataType { } // 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 @@ -504,7 +502,7 @@ public enum DataType { rtn = altRtn; } } - + return rtn; } @@ -512,7 +510,7 @@ public enum DataType { * 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 { |