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

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