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 15KB

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