You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DataType.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. Copyright (c) 2005 Health Market Science, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess;
  14. import java.io.IOException;
  15. import java.sql.SQLException;
  16. import java.sql.Types;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import java.util.Date;
  20. import java.math.BigDecimal;
  21. import java.math.BigInteger;
  22. import com.healthmarketscience.jackcess.impl.JetFormat;
  23. /**
  24. * Supported access data types.
  25. *
  26. * @author Tim McCune
  27. * @usage _general_class_
  28. */
  29. public enum DataType {
  30. /**
  31. * Corresponds to a java {@link Boolean}. Accepts Boolean or {@code null}
  32. * (which is considered {@code false}). Equivalent to SQL {@link
  33. * Types#BOOLEAN}.
  34. */
  35. BOOLEAN((byte) 0x01, Types.BOOLEAN, 0),
  36. /**
  37. * Corresponds to a java {@link Byte}. Accepts any {@link Number} (using
  38. * {@link Number#byteValue}), Boolean as 1 or 0, any Object converted to a
  39. * String and parsed as Double, or {@code null}. Equivalent to SQL
  40. * {@link Types#TINYINT}, {@link Types#BIT}.
  41. */
  42. BYTE((byte) 0x02, Types.TINYINT, 1),
  43. /**
  44. * Corresponds to a java {@link Short}. Accepts any {@link Number} (using
  45. * {@link Number#shortValue}), Boolean as 1 or 0, any Object converted to a
  46. * String and parsed as Double, or {@code null}. Equivalent to SQL
  47. * {@link Types#SMALLINT}.
  48. */
  49. INT((byte) 0x03, Types.SMALLINT, 2),
  50. /**
  51. * Corresponds to a java {@link Integer}. Accepts any {@link Number} (using
  52. * {@link Number#intValue}), Boolean as 1 or 0, any Object converted to a
  53. * String and parsed as Double, or {@code null}. Equivalent to SQL
  54. * {@link Types#INTEGER}, {@link Types#BIGINT}.
  55. */
  56. LONG((byte) 0x04, Types.INTEGER, 4),
  57. /**
  58. * Corresponds to a java {@link BigDecimal} with at most 4 decimal places.
  59. * Accepts any {@link Number} (using {@link Number#doubleValue}), a
  60. * BigInteger, a BigDecimal (with at most 4 decimal places), Boolean as 1 or
  61. * 0, any Object converted to a String and parsed as BigDecimal, or {@code
  62. * null}. Equivalent to SQL {@link Types#DECIMAL}.
  63. */
  64. MONEY((byte) 0x05, Types.DECIMAL, 8, false, false, 0, 0, 0, false, 4, 4, 4,
  65. 19, 19, 19, 1),
  66. /**
  67. * Corresponds to a java {@link Float}. Accepts any {@link Number} (using
  68. * {@link Number#floatValue}), Boolean as 1 or 0, any Object converted to a
  69. * String and parsed as Double, or {@code null}. Equivalent to SQL
  70. * {@link Types#FLOAT}.
  71. */
  72. FLOAT((byte) 0x06, Types.FLOAT, 4),
  73. /**
  74. * Corresponds to a java {@link Double}. Accepts any {@link Number} (using
  75. * {@link Number#doubleValue}), Boolean as 1 or 0, any Object converted to a
  76. * String and parsed as Double, or {@code null}. Equivalent to SQL
  77. * {@link Types#DOUBLE}, {@link Types#REAL}.
  78. */
  79. DOUBLE((byte) 0x07, Types.DOUBLE, 8),
  80. /**
  81. * Corresponds to a java {@link Date}. Accepts a Date, any {@link Number}
  82. * (using {@link Number#longValue}), or {@code null}. Equivalent to SQL
  83. * {@link Types#TIMESTAMP}, {@link Types#DATE}, {@link Types#TIME}.
  84. */
  85. SHORT_DATE_TIME((byte) 0x08, Types.TIMESTAMP, 8),
  86. /**
  87. * Corresponds to a java {@code byte[]} of max length 255 bytes. Accepts a
  88. * {@code byte[]}, or {@code null}. Equivalent to SQL {@link Types#BINARY},
  89. * {@link Types#VARBINARY}.
  90. */
  91. BINARY((byte) 0x09, Types.BINARY, null, true, false, 0, 255, 255, 1),
  92. /**
  93. * Corresponds to a java {@link String} of max length 255 chars. Accepts
  94. * any {@link CharSequence}, any Object converted to a String , or {@code
  95. * null}. Equivalent to SQL {@link Types#VARCHAR}, {@link Types#CHAR}.
  96. */
  97. TEXT((byte) 0x0A, Types.VARCHAR, null, true, false, 0,
  98. JetFormat.TEXT_FIELD_MAX_LENGTH, JetFormat.TEXT_FIELD_MAX_LENGTH,
  99. JetFormat.TEXT_FIELD_UNIT_SIZE),
  100. /**
  101. * Corresponds to a java {@code byte[]} of max length 16777215 bytes.
  102. * Accepts a {@code byte[]}, or {@code null}. Equivalent to SQL
  103. * {@link Types#LONGVARBINARY}, {@link Types#BLOB}.
  104. */
  105. OLE((byte) 0x0B, Types.LONGVARBINARY, null, true, true, 0, 0, 0x3FFFFFFF,
  106. 1),
  107. /**
  108. * Corresponds to a java {@link String} of max length 8388607 chars.
  109. * Accepts any {@link CharSequence}, any Object converted to a String , or
  110. * {@code null}. Equivalent to SQL {@link Types#LONGVARCHAR}, {@link
  111. * Types#CLOB}.
  112. */
  113. MEMO((byte) 0x0C, Types.LONGVARCHAR, null, true, true, 0, 0, 0x3FFFFFFF,
  114. JetFormat.TEXT_FIELD_UNIT_SIZE),
  115. /**
  116. * Unknown data. Handled like {@link #BINARY}.
  117. */
  118. UNKNOWN_0D((byte) 0x0D, null, null, true, false, 0, 255, 255, 1),
  119. /**
  120. * Corresponds to a java {@link String} with the pattern
  121. * <code>"{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"</code>, also known as a
  122. * "Replication ID" in Access. Accepts any
  123. * Object converted to a String matching this pattern (surrounding "{}" are
  124. * optional, so {@link java.util.UUID}s are supported), or {@code null}.
  125. */
  126. GUID((byte) 0x0F, null, 16),
  127. /**
  128. * Corresponds to a java {@link BigDecimal}. Accepts any {@link Number}
  129. * (using {@link Number#doubleValue}), a {@link BigInteger}, a BigDecimal,
  130. * Boolean as 1 or 0, any Object converted to a String and parsed as
  131. * BigDecimal, or {@code null}. Equivalent to SQL {@link Types#NUMERIC}.
  132. */
  133. // for some reason numeric is "var len" even though it has a fixed size...
  134. NUMERIC((byte) 0x10, Types.NUMERIC, 17, true, false, 17, 17, 17,
  135. true, 0, 0, 28, 1, 18, 28, 1),
  136. /**
  137. * Unknown data (seems to be an alternative {@link #OLE} type, used by
  138. * MSysAccessObjects table). Handled like a fixed length BINARY/OLE.
  139. */
  140. UNKNOWN_11((byte) 0x11, null, 3992),
  141. /**
  142. * Complex type corresponds to a special {@link #LONG} autonumber field
  143. * which is the key for a secondary table which holds the "real" data.
  144. */
  145. COMPLEX_TYPE((byte) 0x12, null, 4),
  146. /**
  147. * Dummy type for a fixed length type which is not currently supported.
  148. * Handled like a fixed length {@link #BINARY}.
  149. */
  150. UNSUPPORTED_FIXEDLEN((byte) 0xFE, null, null),
  151. /**
  152. * Placeholder type for a variable length type which is not currently
  153. * supported. Handled like {@link #BINARY}.
  154. */
  155. UNSUPPORTED_VARLEN((byte) 0xFF, null, null, true, false, 0, 0, 0x3FFFFFFF,
  156. 1);
  157. /** Map of SQL types to Access data types */
  158. private static final Map<Integer, DataType> SQL_TYPES =
  159. new HashMap<Integer, DataType>();
  160. /** Alternate map of SQL types to Access data types */
  161. private static final Map<Integer, DataType> ALT_SQL_TYPES =
  162. new HashMap<Integer, DataType>();
  163. static {
  164. for (DataType type : DataType.values()) {
  165. if (type._sqlType != null) {
  166. SQL_TYPES.put(type._sqlType, type);
  167. }
  168. }
  169. SQL_TYPES.put(Types.BIT, BYTE);
  170. SQL_TYPES.put(Types.BLOB, OLE);
  171. SQL_TYPES.put(Types.CLOB, MEMO);
  172. SQL_TYPES.put(Types.BIGINT, LONG);
  173. SQL_TYPES.put(Types.CHAR, TEXT);
  174. SQL_TYPES.put(Types.DATE, SHORT_DATE_TIME);
  175. SQL_TYPES.put(Types.REAL, DOUBLE);
  176. SQL_TYPES.put(Types.TIME, SHORT_DATE_TIME);
  177. SQL_TYPES.put(Types.VARBINARY, BINARY);
  178. // the "alternate" types allow for larger values
  179. ALT_SQL_TYPES.put(Types.VARCHAR, MEMO);
  180. ALT_SQL_TYPES.put(Types.VARBINARY, OLE);
  181. ALT_SQL_TYPES.put(Types.BINARY, OLE);
  182. // add newer sql types if available in this jvm
  183. addNewSqlType("NCHAR", TEXT, null);
  184. addNewSqlType("NVARCHAR", TEXT, MEMO);
  185. addNewSqlType("LONGNVARCHAR", MEMO, null);
  186. addNewSqlType("NCLOB", MEMO, null);
  187. addNewSqlType("TIME_WITH_TIMEZONE", SHORT_DATE_TIME, null);
  188. addNewSqlType("TIMESTAMP_WITH_TIMEZONE", SHORT_DATE_TIME, null);
  189. }
  190. private static Map<Byte, DataType> DATA_TYPES = new HashMap<Byte, DataType>();
  191. static {
  192. for (DataType type : DataType.values()) {
  193. if(type.isUnsupported()) {
  194. continue;
  195. }
  196. DATA_TYPES.put(type._value, type);
  197. }
  198. }
  199. /** is this a variable length field */
  200. private final boolean _variableLength;
  201. /** is this a long value field */
  202. private final boolean _longValue;
  203. /** does this field have scale/precision */
  204. private final boolean _hasScalePrecision;
  205. /** Internal Access value */
  206. private final byte _value;
  207. /** Size in bytes of fixed length columns */
  208. private final Integer _fixedSize;
  209. /** min in bytes size for var length columns */
  210. private final int _minSize;
  211. /** default size in bytes for var length columns */
  212. private final int _defaultSize;
  213. /** Max size in bytes for var length columns */
  214. private final int _maxSize;
  215. /** SQL type equivalent, or null if none defined */
  216. private final Integer _sqlType;
  217. /** min scale value */
  218. private final int _minScale;
  219. /** the default scale value */
  220. private final int _defaultScale;
  221. /** max scale value */
  222. private final int _maxScale;
  223. /** min precision value */
  224. private final int _minPrecision;
  225. /** the default precision value */
  226. private final int _defaultPrecision;
  227. /** max precision value */
  228. private final int _maxPrecision;
  229. /** the number of bytes per "unit" for this data type */
  230. private final int _unitSize;
  231. private DataType(byte value) {
  232. this(value, null, null);
  233. }
  234. private DataType(byte value, Integer sqlType, Integer fixedSize) {
  235. this(value, sqlType, fixedSize, false, false, 0, 0, 0, 1);
  236. }
  237. private DataType(byte value, Integer sqlType, Integer fixedSize,
  238. boolean variableLength,
  239. boolean longValue,
  240. int minSize,
  241. int defaultSize,
  242. int maxSize,
  243. int unitSize) {
  244. this(value, sqlType, fixedSize, variableLength, longValue,
  245. minSize, defaultSize, maxSize,
  246. false, 0, 0, 0, 0, 0, 0, unitSize);
  247. }
  248. private DataType(byte value, Integer sqlType, Integer fixedSize,
  249. boolean variableLength,
  250. boolean longValue,
  251. int minSize,
  252. int defaultSize,
  253. int maxSize,
  254. boolean hasScalePrecision,
  255. int minScale,
  256. int defaultScale,
  257. int maxScale,
  258. int minPrecision,
  259. int defaultPrecision,
  260. int maxPrecision,
  261. int unitSize) {
  262. _value = value;
  263. _sqlType = sqlType;
  264. _fixedSize = fixedSize;
  265. _variableLength = variableLength;
  266. _longValue = longValue;
  267. _minSize = minSize;
  268. _defaultSize = defaultSize;
  269. _maxSize = maxSize;
  270. _hasScalePrecision = hasScalePrecision;
  271. _minScale = minScale;
  272. _defaultScale = defaultScale;
  273. _maxScale = maxScale;
  274. _minPrecision = minPrecision;
  275. _defaultPrecision = defaultPrecision;
  276. _maxPrecision = maxPrecision;
  277. _unitSize = unitSize;
  278. }
  279. public byte getValue() {
  280. return _value;
  281. }
  282. public boolean isVariableLength() {
  283. return _variableLength;
  284. }
  285. public boolean isTrueVariableLength() {
  286. // some "var len" fields do not really have a variable length,
  287. // e.g. NUMERIC
  288. return (isVariableLength() && (getMinSize() != getMaxSize()));
  289. }
  290. public boolean isLongValue() {
  291. return _longValue;
  292. }
  293. public boolean getHasScalePrecision() {
  294. return _hasScalePrecision;
  295. }
  296. public int getFixedSize() {
  297. return getFixedSize(null);
  298. }
  299. public int getFixedSize(Short colLength) {
  300. if(_fixedSize != null) {
  301. if(colLength != null) {
  302. return Math.max(_fixedSize, colLength);
  303. }
  304. return _fixedSize;
  305. }
  306. if(colLength != null) {
  307. return colLength;
  308. }
  309. throw new IllegalArgumentException("Unexpected fixed length column " +
  310. this);
  311. }
  312. public int getMinSize() {
  313. return _minSize;
  314. }
  315. public int getDefaultSize() {
  316. return _defaultSize;
  317. }
  318. public int getMaxSize() {
  319. return _maxSize;
  320. }
  321. public int getSQLType() throws SQLException {
  322. if (_sqlType != null) {
  323. return _sqlType;
  324. }
  325. throw new SQLException("Unsupported data type: " + toString());
  326. }
  327. public int getMinScale() {
  328. return _minScale;
  329. }
  330. public int getDefaultScale() {
  331. return _defaultScale;
  332. }
  333. public int getMaxScale() {
  334. return _maxScale;
  335. }
  336. public int getMinPrecision() {
  337. return _minPrecision;
  338. }
  339. public int getDefaultPrecision() {
  340. return _defaultPrecision;
  341. }
  342. public int getMaxPrecision() {
  343. return _maxPrecision;
  344. }
  345. public int getUnitSize() {
  346. return _unitSize;
  347. }
  348. public int toUnitSize(int size)
  349. {
  350. return(size / getUnitSize());
  351. }
  352. public int fromUnitSize(int unitSize)
  353. {
  354. return(unitSize * getUnitSize());
  355. }
  356. public boolean isValidSize(int size) {
  357. return isWithinRange(size, getMinSize(), getMaxSize());
  358. }
  359. public boolean isValidScale(int scale) {
  360. return isWithinRange(scale, getMinScale(), getMaxScale());
  361. }
  362. public boolean isValidPrecision(int precision) {
  363. return isWithinRange(precision, getMinPrecision(), getMaxPrecision());
  364. }
  365. private static boolean isWithinRange(int value, int minValue, int maxValue) {
  366. return((value >= minValue) && (value <= maxValue));
  367. }
  368. public int toValidSize(int size) {
  369. return toValidRange(size, getMinSize(), getMaxSize());
  370. }
  371. public int toValidScale(int scale) {
  372. return toValidRange(scale, getMinScale(), getMaxScale());
  373. }
  374. public int toValidPrecision(int precision) {
  375. return toValidRange(precision, getMinPrecision(), getMaxPrecision());
  376. }
  377. public boolean isTextual() {
  378. return ((this == TEXT) || (this == MEMO));
  379. }
  380. public boolean mayBeAutoNumber() {
  381. return((this == LONG) || (this == GUID) || (this == COMPLEX_TYPE));
  382. }
  383. public boolean isMultipleAutoNumberAllowed() {
  384. return (this == COMPLEX_TYPE);
  385. }
  386. public boolean isUnsupported() {
  387. return((this == UNSUPPORTED_FIXEDLEN) || (this == UNSUPPORTED_VARLEN));
  388. }
  389. private static int toValidRange(int value, int minValue, int maxValue) {
  390. return((value > maxValue) ? maxValue :
  391. ((value < minValue) ? minValue : value));
  392. }
  393. public static DataType fromByte(byte b) throws IOException {
  394. DataType rtn = DATA_TYPES.get(b);
  395. if (rtn != null) {
  396. return rtn;
  397. }
  398. throw new IOException("Unrecognized data type: " + b);
  399. }
  400. public static DataType fromSQLType(int sqlType)
  401. throws SQLException
  402. {
  403. return fromSQLType(sqlType, 0);
  404. }
  405. public static DataType fromSQLType(int sqlType, int lengthInUnits)
  406. throws SQLException
  407. {
  408. DataType rtn = SQL_TYPES.get(sqlType);
  409. if(rtn == null) {
  410. throw new SQLException("Unsupported SQL type: " + sqlType);
  411. }
  412. // make sure size is reasonable
  413. int size = lengthInUnits * rtn.getUnitSize();
  414. if(rtn.isVariableLength() && !rtn.isValidSize(size)) {
  415. // try alternate type. we always accept alternate "long value" types
  416. // regardless of the given lengthInUnits
  417. DataType altRtn = ALT_SQL_TYPES.get(sqlType);
  418. if((altRtn != null) &&
  419. (altRtn.isLongValue() || altRtn.isValidSize(size))) {
  420. // use alternate type
  421. rtn = altRtn;
  422. }
  423. }
  424. return rtn;
  425. }
  426. /**
  427. * Adds mappings for a sql type which was added after jdk 1.5 (using
  428. * reflection).
  429. */
  430. private static void addNewSqlType(String typeName, DataType type,
  431. DataType altType)
  432. {
  433. try {
  434. java.lang.reflect.Field sqlTypeField = Types.class.getField(typeName);
  435. Integer value = (Integer)sqlTypeField.get(null);
  436. SQL_TYPES.put(value, type);
  437. if(altType != null) {
  438. ALT_SQL_TYPES.put(value, altType);
  439. }
  440. } catch(Exception ignored) {
  441. // must not be available
  442. }
  443. }
  444. }