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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. /**
  29. * Access data type
  30. * @author Tim McCune
  31. */
  32. public enum DataType {
  33. /**
  34. * Corresponds to a java Boolean. Accepts Boolean or {@code null} (which is
  35. * considered {@code false}). Equivalent to SQL {@link Types#BOOLEAN}.
  36. */
  37. BOOLEAN((byte) 0x01, Types.BOOLEAN, 0),
  38. /**
  39. * Corresponds to a java Byte. Accepts any Number (using
  40. * {@link Number#byteValue}), Boolean as 1 or 0, any Object converted to a
  41. * String and parsed as Double, or {@code null}. Equivalent to SQL
  42. * {@link Types#TINYINT}, {@link Types#BIT}.
  43. */
  44. BYTE((byte) 0x02, Types.TINYINT, 1),
  45. /**
  46. * Corresponds to a java Short. Accepts any Number (using
  47. * {@link Number#shortValue}), Boolean as 1 or 0, any Object converted to a
  48. * String and parsed as Double, or {@code null}. Equivalent to SQL
  49. * {@link Types#SMALLINT}.
  50. */
  51. INT((byte) 0x03, Types.SMALLINT, 2),
  52. /**
  53. * Corresponds to a java Integer. Accepts any Number (using
  54. * {@link Number#intValue}), Boolean as 1 or 0, any Object converted to a
  55. * String and parsed as Double, or {@code null}. Equivalent to SQL
  56. * {@link Types#INTEGER}, {@link Types#BIGINT}.
  57. */
  58. LONG((byte) 0x04, Types.INTEGER, 4),
  59. /**
  60. * Corresponds to a java BigDecimal with at most 4 decimal places. Accepts
  61. * any Number (using {@link Number#doubleValue}), a BigInteger, a BigDecimal
  62. * (with at most 4 decimal places), Boolean as 1 or 0, any Object converted
  63. * to a String and parsed as BigDecimal, or {@code null}. Equivalent to SQL
  64. * {@link Types#DECIMAL}.
  65. */
  66. MONEY((byte) 0x05, Types.DECIMAL, 8),
  67. /**
  68. * Corresponds to a java Float. Accepts any Number (using
  69. * {@link Number#floatValue}), Boolean as 1 or 0, any Object converted to a
  70. * String and parsed as Double, or {@code null}. Equivalent to SQL
  71. * {@link Types#FLOAT}.
  72. */
  73. FLOAT((byte) 0x06, Types.FLOAT, 4),
  74. /**
  75. * Corresponds to a java Double. Accepts any Number (using
  76. * {@link Number#doubleValue}), Boolean as 1 or 0, any Object converted to a
  77. * String and parsed as Double, or {@code null}. Equivalent to SQL
  78. * {@link Types#DOUBLE}, {@link Types#REAL}.
  79. */
  80. DOUBLE((byte) 0x07, Types.DOUBLE, 8),
  81. /**
  82. * Corresponds to a java Date. Accepts a Date, any Number (using
  83. * {@link Number#longValue}), or {@code null}. Equivalent to SQL
  84. * {@link Types#TIMESTAMP}, {@link Types#DATE}, {@link Types#TIME}.
  85. */
  86. SHORT_DATE_TIME((byte) 0x08, Types.TIMESTAMP, 8),
  87. /**
  88. * Corresponds to a java {@code byte[]} of max length 255 bytes. Accepts a
  89. * {@code byte[]}, or {@code null}. Equivalent to SQL {@link Types#BINARY},
  90. * {@link Types#VARBINARY}.
  91. */
  92. BINARY((byte) 0x09, Types.BINARY, null, true, false, 0, 255, 255, 1),
  93. /**
  94. * Corresponds to a java String of max length 255 chars. Accepts any
  95. * CharSequence, any Object converted to a String , or {@code null}.
  96. * Equivalent to SQL {@link Types#VARCHAR}, {@link Types#CHAR}.
  97. */
  98. TEXT((byte) 0x0A, Types.VARCHAR, null, true, false, 0,
  99. 50 * JetFormat.TEXT_FIELD_UNIT_SIZE,
  100. (int)JetFormat.TEXT_FIELD_MAX_LENGTH, JetFormat.TEXT_FIELD_UNIT_SIZE),
  101. /**
  102. * Corresponds to a java {@code byte[]} of max length 16777215 bytes.
  103. * Accepts a {@code byte[]}, or {@code null}. Equivalent to SQL
  104. * {@link Types#LONGVARBINARY}, {@link Types#BLOB}.
  105. */
  106. OLE((byte) 0x0B, Types.LONGVARBINARY, null, true, true, 0, null, 0x3FFFFFFF,
  107. 1),
  108. /**
  109. * Corresponds to a java String of max length 8388607 chars. Accepts any
  110. * CharSequence, any Object converted to a String , or {@code null}.
  111. * Equivalent to SQL {@link Types#LONGVARCHAR}, {@link Types#CLOB}.
  112. */
  113. MEMO((byte) 0x0C, Types.LONGVARCHAR, null, true, true, 0, null, 0x3FFFFFFF,
  114. JetFormat.TEXT_FIELD_UNIT_SIZE),
  115. /**
  116. * Unknown data. Handled like BINARY.
  117. */
  118. UNKNOWN_0D((byte) 0x0D, null, null, true, false, 0, 255, 255, 1),
  119. /**
  120. * Corresponds to a java 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 BigDecimal. Accepts any Number (using
  129. * {@link Number#doubleValue}), a BigInteger, a BigDecimal, Boolean as 1 or
  130. * 0, any Object converted to a String and parsed as BigDecimal, or
  131. * {@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 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 LONG autonumber field which is the
  143. * 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 BINARY.
  149. */
  150. UNSUPPORTED_FIXEDLEN((byte) 0xFE, null, null),
  151. /**
  152. * Placeholder type for a variable length type which is not currently supported.
  153. * Handled like BINARY.
  154. */
  155. UNSUPPORTED_VARLEN((byte) 0xFF, null, null, true, false, 0, null, 0x3FFFFFFF,
  156. 1);
  157. /** Map of SQL types to Access data types */
  158. private static Map<Integer, DataType> SQL_TYPES = new HashMap<Integer, DataType>();
  159. /** Alternate map of SQL types to Access data types */
  160. private static Map<Integer, DataType> ALT_SQL_TYPES = new HashMap<Integer, DataType>();
  161. static {
  162. for (DataType type : DataType.values()) {
  163. if (type._sqlType != null) {
  164. SQL_TYPES.put(type._sqlType, type);
  165. }
  166. }
  167. SQL_TYPES.put(Types.BIT, BYTE);
  168. SQL_TYPES.put(Types.BLOB, OLE);
  169. SQL_TYPES.put(Types.CLOB, MEMO);
  170. SQL_TYPES.put(Types.BIGINT, LONG);
  171. SQL_TYPES.put(Types.CHAR, TEXT);
  172. SQL_TYPES.put(Types.DATE, SHORT_DATE_TIME);
  173. SQL_TYPES.put(Types.REAL, DOUBLE);
  174. SQL_TYPES.put(Types.TIME, SHORT_DATE_TIME);
  175. SQL_TYPES.put(Types.VARBINARY, BINARY);
  176. // the "alternate" types allow for larger values
  177. ALT_SQL_TYPES.put(Types.VARCHAR, MEMO);
  178. ALT_SQL_TYPES.put(Types.VARBINARY, OLE);
  179. ALT_SQL_TYPES.put(Types.BINARY, OLE);
  180. }
  181. private static Map<Byte, DataType> DATA_TYPES = new HashMap<Byte, DataType>();
  182. static {
  183. for (DataType type : DataType.values()) {
  184. if(type.isUnsupported()) {
  185. continue;
  186. }
  187. DATA_TYPES.put(type._value, type);
  188. }
  189. }
  190. /** is this a variable length field */
  191. private boolean _variableLength;
  192. /** is this a long value field */
  193. private boolean _longValue;
  194. /** does this field have scale/precision */
  195. private boolean _hasScalePrecision;
  196. /** Internal Access value */
  197. private byte _value;
  198. /** Size in bytes of fixed length columns */
  199. private Integer _fixedSize;
  200. /** min in bytes size for var length columns */
  201. private Integer _minSize;
  202. /** default size in bytes for var length columns */
  203. private Integer _defaultSize;
  204. /** Max size in bytes for var length columns */
  205. private Integer _maxSize;
  206. /** SQL type equivalent, or null if none defined */
  207. private Integer _sqlType;
  208. /** min scale value */
  209. private Integer _minScale;
  210. /** the default scale value */
  211. private Integer _defaultScale;
  212. /** max scale value */
  213. private Integer _maxScale;
  214. /** min precision value */
  215. private Integer _minPrecision;
  216. /** the default precision value */
  217. private Integer _defaultPrecision;
  218. /** max precision value */
  219. private Integer _maxPrecision;
  220. /** the number of bytes per "unit" for this data type */
  221. private int _unitSize;
  222. private DataType(byte value) {
  223. this(value, null, null);
  224. }
  225. private DataType(byte value, Integer sqlType, Integer fixedSize) {
  226. this(value, sqlType, fixedSize, false, false, null, null, null, 1);
  227. }
  228. private DataType(byte value, Integer sqlType, Integer fixedSize,
  229. boolean variableLength,
  230. boolean longValue,
  231. Integer minSize,
  232. Integer defaultSize,
  233. Integer maxSize,
  234. int unitSize) {
  235. this(value, sqlType, fixedSize, variableLength, longValue,
  236. minSize, defaultSize, maxSize,
  237. false, null, null, null, null, null, null, unitSize);
  238. }
  239. private DataType(byte value, Integer sqlType, Integer fixedSize,
  240. boolean variableLength,
  241. boolean longValue,
  242. Integer minSize,
  243. Integer defaultSize,
  244. Integer maxSize,
  245. boolean hasScalePrecision,
  246. Integer minScale,
  247. Integer defaultScale,
  248. Integer maxScale,
  249. Integer minPrecision,
  250. Integer defaultPrecision,
  251. Integer maxPrecision,
  252. int unitSize) {
  253. _value = value;
  254. _sqlType = sqlType;
  255. _fixedSize = fixedSize;
  256. _variableLength = variableLength;
  257. _longValue = longValue;
  258. _minSize = minSize;
  259. _defaultSize = defaultSize;
  260. _maxSize = maxSize;
  261. _hasScalePrecision = hasScalePrecision;
  262. _minScale = minScale;
  263. _defaultScale = defaultScale;
  264. _maxScale = maxScale;
  265. _minPrecision = minPrecision;
  266. _defaultPrecision = defaultPrecision;
  267. _maxPrecision = maxPrecision;
  268. _unitSize = unitSize;
  269. }
  270. public byte getValue() {
  271. return _value;
  272. }
  273. public boolean isVariableLength() {
  274. return _variableLength;
  275. }
  276. public boolean isTrueVariableLength() {
  277. // some "var len" fields do not really have a variable length,
  278. // e.g. NUMERIC
  279. return (isVariableLength() && (getMinSize() != getMaxSize()));
  280. }
  281. public boolean isLongValue() {
  282. return _longValue;
  283. }
  284. public boolean getHasScalePrecision() {
  285. return _hasScalePrecision;
  286. }
  287. public int getFixedSize() {
  288. return getFixedSize(null);
  289. }
  290. public int getFixedSize(Short colLength) {
  291. if(_fixedSize != null) {
  292. if(colLength != null) {
  293. return Math.max(_fixedSize, colLength);
  294. }
  295. return _fixedSize;
  296. }
  297. if(colLength != null) {
  298. return colLength;
  299. }
  300. throw new IllegalArgumentException("Unexpected fixed length column " +
  301. this);
  302. }
  303. public int getMinSize() {
  304. return _minSize;
  305. }
  306. public int getDefaultSize() {
  307. return _defaultSize;
  308. }
  309. public int getMaxSize() {
  310. return _maxSize;
  311. }
  312. public int getSQLType() throws SQLException {
  313. if (_sqlType != null) {
  314. return _sqlType;
  315. }
  316. throw new SQLException("Unsupported data type: " + toString());
  317. }
  318. public int getMinScale() {
  319. return _minScale;
  320. }
  321. public int getDefaultScale() {
  322. return _defaultScale;
  323. }
  324. public int getMaxScale() {
  325. return _maxScale;
  326. }
  327. public int getMinPrecision() {
  328. return _minPrecision;
  329. }
  330. public int getDefaultPrecision() {
  331. return _defaultPrecision;
  332. }
  333. public int getMaxPrecision() {
  334. return _maxPrecision;
  335. }
  336. public int getUnitSize() {
  337. return _unitSize;
  338. }
  339. public int toUnitSize(int size)
  340. {
  341. return(size / getUnitSize());
  342. }
  343. public int fromUnitSize(int unitSize)
  344. {
  345. return(unitSize * getUnitSize());
  346. }
  347. public boolean isValidSize(int size) {
  348. return isWithinRange(size, getMinSize(), getMaxSize());
  349. }
  350. public boolean isValidScale(int scale) {
  351. return isWithinRange(scale, getMinScale(), getMaxScale());
  352. }
  353. public boolean isValidPrecision(int precision) {
  354. return isWithinRange(precision, getMinPrecision(), getMaxPrecision());
  355. }
  356. private static boolean isWithinRange(int value, int minValue, int maxValue) {
  357. return((value >= minValue) && (value <= maxValue));
  358. }
  359. public int toValidSize(int size) {
  360. return toValidRange(size, getMinSize(), getMaxSize());
  361. }
  362. public int toValidScale(int scale) {
  363. return toValidRange(scale, getMinScale(), getMaxScale());
  364. }
  365. public int toValidPrecision(int precision) {
  366. return toValidRange(precision, getMinPrecision(), getMaxPrecision());
  367. }
  368. public boolean isTextual() {
  369. return ((this == TEXT) || (this == MEMO));
  370. }
  371. public boolean mayBeAutoNumber() {
  372. return((this == LONG) || (this == GUID) || (this == COMPLEX_TYPE));
  373. }
  374. public boolean isMultipleAutoNumberAllowed() {
  375. return (this == COMPLEX_TYPE);
  376. }
  377. public boolean isUnsupported() {
  378. return((this == UNSUPPORTED_FIXEDLEN) || (this == UNSUPPORTED_VARLEN));
  379. }
  380. private static int toValidRange(int value, int minValue, int maxValue) {
  381. return((value > maxValue) ? maxValue :
  382. ((value < minValue) ? minValue : value));
  383. }
  384. public static DataType fromByte(byte b) throws IOException {
  385. DataType rtn = DATA_TYPES.get(b);
  386. if (rtn != null) {
  387. return rtn;
  388. }
  389. throw new IOException("Unrecognized data type: " + b);
  390. }
  391. public static DataType fromSQLType(int sqlType)
  392. throws SQLException
  393. {
  394. return fromSQLType(sqlType, 0);
  395. }
  396. public static DataType fromSQLType(int sqlType, int lengthInUnits)
  397. throws SQLException
  398. {
  399. DataType rtn = SQL_TYPES.get(sqlType);
  400. if(rtn == null) {
  401. throw new SQLException("Unsupported SQL type: " + sqlType);
  402. }
  403. // make sure size is reasonable
  404. int size = lengthInUnits * rtn.getUnitSize();
  405. if(rtn.isVariableLength() && !rtn.isValidSize(size)) {
  406. // try alternate type. we always accept alternate "long value" types
  407. // regardless of the given lengthInUnits
  408. DataType altRtn = ALT_SQL_TYPES.get(sqlType);
  409. if((altRtn != null) &&
  410. (altRtn.isLongValue() || altRtn.isValidSize(size))) {
  411. // use alternate type
  412. rtn = altRtn;
  413. }
  414. }
  415. return rtn;
  416. }
  417. }