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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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, 0xFFFFFF,
  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, 0xFFFFFF,
  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>. Accepts any
  122. * Object converted to a String matching this pattern (surrounding "{}" are
  123. * optional, so {@link java.util.UUID}s are supported), or {@code null}.
  124. */
  125. GUID((byte) 0x0F, null, 16),
  126. /**
  127. * Corresponds to a java BigDecimal. Accepts any Number (using
  128. * {@link Number#doubleValue}), a BigInteger, a BigDecimal, Boolean as 1 or
  129. * 0, any Object converted to a String and parsed as BigDecimal, or
  130. * {@code null}. Equivalent to SQL {@link Types#NUMERIC}.
  131. */
  132. // for some reason numeric is "var len" even though it has a fixed size...
  133. NUMERIC((byte) 0x10, Types.NUMERIC, 17, true, false, 17, 17, 17,
  134. true, 0, 0, 28, 1, 18, 28, 1),
  135. /**
  136. * Unknown data (seems to be an alternative OLE type, used by
  137. * MSysAccessObjects table). Handled like a fixed length BINARY/OLE.
  138. */
  139. UNKNOWN_11((byte) 0x11, null, 3992);
  140. /** Map of SQL types to Access data types */
  141. private static Map<Integer, DataType> SQL_TYPES = new HashMap<Integer, DataType>();
  142. /** Alternate map of SQL types to Access data types */
  143. private static Map<Integer, DataType> ALT_SQL_TYPES = new HashMap<Integer, DataType>();
  144. static {
  145. for (DataType type : DataType.values()) {
  146. if (type._sqlType != null) {
  147. SQL_TYPES.put(type._sqlType, type);
  148. }
  149. }
  150. SQL_TYPES.put(Types.BIT, BYTE);
  151. SQL_TYPES.put(Types.BLOB, OLE);
  152. SQL_TYPES.put(Types.CLOB, MEMO);
  153. SQL_TYPES.put(Types.BIGINT, LONG);
  154. SQL_TYPES.put(Types.CHAR, TEXT);
  155. SQL_TYPES.put(Types.DATE, SHORT_DATE_TIME);
  156. SQL_TYPES.put(Types.REAL, DOUBLE);
  157. SQL_TYPES.put(Types.TIME, SHORT_DATE_TIME);
  158. SQL_TYPES.put(Types.VARBINARY, BINARY);
  159. // the "alternate" types allow for larger values
  160. ALT_SQL_TYPES.put(Types.VARCHAR, MEMO);
  161. ALT_SQL_TYPES.put(Types.VARBINARY, OLE);
  162. ALT_SQL_TYPES.put(Types.BINARY, OLE);
  163. }
  164. private static Map<Byte, DataType> DATA_TYPES = new HashMap<Byte, DataType>();
  165. static {
  166. for (DataType type : DataType.values()) {
  167. DATA_TYPES.put(type._value, type);
  168. }
  169. }
  170. /** is this a variable length field */
  171. private boolean _variableLength;
  172. /** is this a long value field */
  173. private boolean _longValue;
  174. /** does this field have scale/precision */
  175. private boolean _hasScalePrecision;
  176. /** Internal Access value */
  177. private byte _value;
  178. /** Size in bytes of fixed length columns */
  179. private Integer _fixedSize;
  180. /** min in bytes size for var length columns */
  181. private Integer _minSize;
  182. /** default size in bytes for var length columns */
  183. private Integer _defaultSize;
  184. /** Max size in bytes for var length columns */
  185. private Integer _maxSize;
  186. /** SQL type equivalent, or null if none defined */
  187. private Integer _sqlType;
  188. /** min scale value */
  189. private Integer _minScale;
  190. /** the default scale value */
  191. private Integer _defaultScale;
  192. /** max scale value */
  193. private Integer _maxScale;
  194. /** min precision value */
  195. private Integer _minPrecision;
  196. /** the default precision value */
  197. private Integer _defaultPrecision;
  198. /** max precision value */
  199. private Integer _maxPrecision;
  200. /** the number of bytes per "unit" for this data type */
  201. private int _unitSize;
  202. private DataType(byte value) {
  203. this(value, null, null);
  204. }
  205. private DataType(byte value, Integer sqlType, Integer fixedSize) {
  206. this(value, sqlType, fixedSize, false, false, null, null, null, 1);
  207. }
  208. private DataType(byte value, Integer sqlType, Integer fixedSize,
  209. boolean variableLength,
  210. boolean longValue,
  211. Integer minSize,
  212. Integer defaultSize,
  213. Integer maxSize,
  214. int unitSize) {
  215. this(value, sqlType, fixedSize, variableLength, longValue,
  216. minSize, defaultSize, maxSize,
  217. false, null, null, null, null, null, null, unitSize);
  218. }
  219. private DataType(byte value, Integer sqlType, Integer fixedSize,
  220. boolean variableLength,
  221. boolean longValue,
  222. Integer minSize,
  223. Integer defaultSize,
  224. Integer maxSize,
  225. boolean hasScalePrecision,
  226. Integer minScale,
  227. Integer defaultScale,
  228. Integer maxScale,
  229. Integer minPrecision,
  230. Integer defaultPrecision,
  231. Integer maxPrecision,
  232. int unitSize) {
  233. _value = value;
  234. _sqlType = sqlType;
  235. _fixedSize = fixedSize;
  236. _variableLength = variableLength;
  237. _longValue = longValue;
  238. _minSize = minSize;
  239. _defaultSize = defaultSize;
  240. _maxSize = maxSize;
  241. _hasScalePrecision = hasScalePrecision;
  242. _minScale = minScale;
  243. _defaultScale = defaultScale;
  244. _maxScale = maxScale;
  245. _minPrecision = minPrecision;
  246. _defaultPrecision = defaultPrecision;
  247. _maxPrecision = maxPrecision;
  248. _unitSize = unitSize;
  249. }
  250. public byte getValue() {
  251. return _value;
  252. }
  253. public boolean isVariableLength() {
  254. return _variableLength;
  255. }
  256. public boolean isTrueVariableLength() {
  257. // some "var len" fields do not really have a variable length,
  258. // e.g. NUMERIC
  259. return (isVariableLength() && (getMinSize() != getMaxSize()));
  260. }
  261. public boolean isLongValue() {
  262. return _longValue;
  263. }
  264. public boolean getHasScalePrecision() {
  265. return _hasScalePrecision;
  266. }
  267. public int getFixedSize() {
  268. if(_fixedSize != null) {
  269. return _fixedSize;
  270. }
  271. throw new IllegalArgumentException("FIX ME");
  272. }
  273. public int getMinSize() {
  274. return _minSize;
  275. }
  276. public int getDefaultSize() {
  277. return _defaultSize;
  278. }
  279. public int getMaxSize() {
  280. return _maxSize;
  281. }
  282. public int getSQLType() throws SQLException {
  283. if (_sqlType != null) {
  284. return _sqlType;
  285. }
  286. throw new SQLException("Unsupported data type: " + toString());
  287. }
  288. public int getMinScale() {
  289. return _minScale;
  290. }
  291. public int getDefaultScale() {
  292. return _defaultScale;
  293. }
  294. public int getMaxScale() {
  295. return _maxScale;
  296. }
  297. public int getMinPrecision() {
  298. return _minPrecision;
  299. }
  300. public int getDefaultPrecision() {
  301. return _defaultPrecision;
  302. }
  303. public int getMaxPrecision() {
  304. return _maxPrecision;
  305. }
  306. public int getUnitSize() {
  307. return _unitSize;
  308. }
  309. public boolean isValidSize(int size) {
  310. return isWithinRange(size, getMinSize(), getMaxSize());
  311. }
  312. public boolean isValidScale(int scale) {
  313. return isWithinRange(scale, getMinScale(), getMaxScale());
  314. }
  315. public boolean isValidPrecision(int precision) {
  316. return isWithinRange(precision, getMinPrecision(), getMaxPrecision());
  317. }
  318. private boolean isWithinRange(int value, int minValue, int maxValue) {
  319. return((value >= minValue) && (value <= maxValue));
  320. }
  321. public int toValidSize(int size) {
  322. return toValidRange(size, getMinSize(), getMaxSize());
  323. }
  324. public int toValidScale(int scale) {
  325. return toValidRange(scale, getMinScale(), getMaxScale());
  326. }
  327. public int toValidPrecision(int precision) {
  328. return toValidRange(precision, getMinPrecision(), getMaxPrecision());
  329. }
  330. private int toValidRange(int value, int minValue, int maxValue) {
  331. return((value > maxValue) ? maxValue :
  332. ((value < minValue) ? minValue : value));
  333. }
  334. public static DataType fromByte(byte b) throws IOException {
  335. DataType rtn = DATA_TYPES.get(b);
  336. if (rtn != null) {
  337. return rtn;
  338. }
  339. throw new IOException("Unrecognized data type: " + b);
  340. }
  341. public static DataType fromSQLType(int sqlType)
  342. throws SQLException
  343. {
  344. return fromSQLType(sqlType, 0);
  345. }
  346. public static DataType fromSQLType(int sqlType, int lengthInUnits)
  347. throws SQLException
  348. {
  349. DataType rtn = SQL_TYPES.get(sqlType);
  350. if(rtn == null) {
  351. throw new SQLException("Unsupported SQL type: " + sqlType);
  352. }
  353. // make sure size is reasonable
  354. int size = lengthInUnits * rtn.getUnitSize();
  355. if(rtn.isVariableLength() && !rtn.isValidSize(size)) {
  356. // try alternate type. we always accept alternate "long value" types
  357. // regardless of the given lengthInUnits
  358. DataType altRtn = ALT_SQL_TYPES.get(sqlType);
  359. if((altRtn != null) &&
  360. (altRtn.isLongValue() || altRtn.isValidSize(size))) {
  361. // use alternate type
  362. rtn = altRtn;
  363. }
  364. }
  365. return rtn;
  366. }
  367. }