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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. BOOLEAN((byte) 0x01, Types.BOOLEAN, 0),
  34. BYTE((byte) 0x02, Types.TINYINT, 1),
  35. INT((byte) 0x03, Types.SMALLINT, 2),
  36. LONG((byte) 0x04, Types.INTEGER, 4),
  37. MONEY((byte) 0x05, Types.DECIMAL, 8),
  38. FLOAT((byte) 0x06, Types.FLOAT, 4),
  39. DOUBLE((byte) 0x07, Types.DOUBLE, 8),
  40. SHORT_DATE_TIME((byte) 0x08, Types.TIMESTAMP, 8),
  41. BINARY((byte) 0x09, Types.BINARY, null, true, false, 0, 255, 255, 1),
  42. TEXT((byte) 0x0A, Types.VARCHAR, null, true, false, 0,
  43. 50 * JetFormat.TEXT_FIELD_UNIT_SIZE,
  44. (int)JetFormat.TEXT_FIELD_MAX_LENGTH, JetFormat.TEXT_FIELD_UNIT_SIZE),
  45. OLE((byte) 0x0B, Types.LONGVARBINARY, null, true, true, 0, null, 0xFFFFFF,
  46. 1),
  47. MEMO((byte) 0x0C, Types.LONGVARCHAR, null, true, true, 0, null, 0xFFFFFF,
  48. JetFormat.TEXT_FIELD_UNIT_SIZE),
  49. UNKNOWN_0D((byte) 0x0D),
  50. GUID((byte) 0x0F, null, 16),
  51. // for some reason numeric is "var len" even though it has a fixed size...
  52. NUMERIC((byte) 0x10, Types.NUMERIC, null, true, false, 17, 17, 17,
  53. true, 0, 0, 28, 1, 18, 28, 1);
  54. /** Map of SQL types to Access data types */
  55. private static Map<Integer, DataType> SQL_TYPES = new HashMap<Integer, DataType>();
  56. /** Alternate map of SQL types to Access data types */
  57. private static Map<Integer, DataType> ALT_SQL_TYPES = new HashMap<Integer, DataType>();
  58. static {
  59. for (DataType type : DataType.values()) {
  60. if (type._sqlType != null) {
  61. SQL_TYPES.put(type._sqlType, type);
  62. }
  63. }
  64. SQL_TYPES.put(Types.BIT, BYTE);
  65. SQL_TYPES.put(Types.BLOB, OLE);
  66. SQL_TYPES.put(Types.CLOB, MEMO);
  67. SQL_TYPES.put(Types.BIGINT, LONG);
  68. SQL_TYPES.put(Types.CHAR, TEXT);
  69. SQL_TYPES.put(Types.DATE, SHORT_DATE_TIME);
  70. SQL_TYPES.put(Types.REAL, DOUBLE);
  71. SQL_TYPES.put(Types.TIME, SHORT_DATE_TIME);
  72. SQL_TYPES.put(Types.VARBINARY, BINARY);
  73. // the "alternate" types allow for larger values
  74. ALT_SQL_TYPES.put(Types.VARCHAR, MEMO);
  75. ALT_SQL_TYPES.put(Types.VARBINARY, OLE);
  76. ALT_SQL_TYPES.put(Types.BINARY, OLE);
  77. }
  78. private static Map<Byte, DataType> DATA_TYPES = new HashMap<Byte, DataType>();
  79. static {
  80. for (DataType type : DataType.values()) {
  81. DATA_TYPES.put(type._value, type);
  82. }
  83. }
  84. /** is this a variable length field */
  85. private boolean _variableLength;
  86. /** is this a long value field */
  87. private boolean _longValue;
  88. /** does this field have scale/precision */
  89. private boolean _hasScalePrecision;
  90. /** Internal Access value */
  91. private byte _value;
  92. /** Size in bytes of fixed length columns */
  93. private Integer _fixedSize;
  94. /** min in bytes size for var length columns */
  95. private Integer _minSize;
  96. /** default size in bytes for var length columns */
  97. private Integer _defaultSize;
  98. /** Max size in bytes for var length columns */
  99. private Integer _maxSize;
  100. /** SQL type equivalent, or null if none defined */
  101. private Integer _sqlType;
  102. /** min scale value */
  103. private Integer _minScale;
  104. /** the default scale value */
  105. private Integer _defaultScale;
  106. /** max scale value */
  107. private Integer _maxScale;
  108. /** min precision value */
  109. private Integer _minPrecision;
  110. /** the default precision value */
  111. private Integer _defaultPrecision;
  112. /** max precision value */
  113. private Integer _maxPrecision;
  114. /** the number of bytes per "unit" for this data type */
  115. private int _unitSize;
  116. private DataType(byte value) {
  117. this(value, null, null);
  118. }
  119. private DataType(byte value, Integer sqlType, Integer fixedSize) {
  120. this(value, sqlType, fixedSize, false, false, null, null, null, 1);
  121. }
  122. private DataType(byte value, Integer sqlType, Integer fixedSize,
  123. boolean variableLength,
  124. boolean longValue,
  125. Integer minSize,
  126. Integer defaultSize,
  127. Integer maxSize,
  128. int unitSize) {
  129. this(value, sqlType, fixedSize, variableLength, longValue,
  130. minSize, defaultSize, maxSize,
  131. false, null, null, null, null, null, null, unitSize);
  132. }
  133. private DataType(byte value, Integer sqlType, Integer fixedSize,
  134. boolean variableLength,
  135. boolean longValue,
  136. Integer minSize,
  137. Integer defaultSize,
  138. Integer maxSize,
  139. boolean hasScalePrecision,
  140. Integer minScale,
  141. Integer defaultScale,
  142. Integer maxScale,
  143. Integer minPrecision,
  144. Integer defaultPrecision,
  145. Integer maxPrecision,
  146. int unitSize) {
  147. _value = value;
  148. _sqlType = sqlType;
  149. _fixedSize = fixedSize;
  150. _variableLength = variableLength;
  151. _longValue = longValue;
  152. _minSize = minSize;
  153. _defaultSize = defaultSize;
  154. _maxSize = maxSize;
  155. _hasScalePrecision = hasScalePrecision;
  156. _minScale = minScale;
  157. _defaultScale = defaultScale;
  158. _maxScale = maxScale;
  159. _minPrecision = minPrecision;
  160. _defaultPrecision = defaultPrecision;
  161. _maxPrecision = maxPrecision;
  162. _unitSize = unitSize;
  163. }
  164. public byte getValue() {
  165. return _value;
  166. }
  167. public boolean isVariableLength() {
  168. return _variableLength;
  169. }
  170. public boolean isLongValue() {
  171. return _longValue;
  172. }
  173. public boolean getHasScalePrecision() {
  174. return _hasScalePrecision;
  175. }
  176. public int getFixedSize() {
  177. if(_fixedSize != null) {
  178. return _fixedSize;
  179. } else {
  180. throw new IllegalArgumentException("FIX ME");
  181. }
  182. }
  183. public int getMinSize() {
  184. return _minSize;
  185. }
  186. public int getDefaultSize() {
  187. return _defaultSize;
  188. }
  189. public int getMaxSize() {
  190. return _maxSize;
  191. }
  192. public int getSQLType() throws SQLException {
  193. if (_sqlType != null) {
  194. return _sqlType;
  195. } else {
  196. throw new SQLException("Unsupported data type: " + toString());
  197. }
  198. }
  199. public int getMinScale() {
  200. return _minScale;
  201. }
  202. public int getDefaultScale() {
  203. return _defaultScale;
  204. }
  205. public int getMaxScale() {
  206. return _maxScale;
  207. }
  208. public int getMinPrecision() {
  209. return _minPrecision;
  210. }
  211. public int getDefaultPrecision() {
  212. return _defaultPrecision;
  213. }
  214. public int getMaxPrecision() {
  215. return _maxPrecision;
  216. }
  217. public int getUnitSize() {
  218. return _unitSize;
  219. }
  220. public boolean isValidSize(int size) {
  221. return isWithinRange(size, getMinSize(), getMaxSize());
  222. }
  223. public boolean isValidScale(int scale) {
  224. return isWithinRange(scale, getMinScale(), getMaxScale());
  225. }
  226. public boolean isValidPrecision(int precision) {
  227. return isWithinRange(precision, getMinPrecision(), getMaxPrecision());
  228. }
  229. private boolean isWithinRange(int value, int minValue, int maxValue) {
  230. return((value >= minValue) && (value <= maxValue));
  231. }
  232. public static DataType fromByte(byte b) throws IOException {
  233. DataType rtn = DATA_TYPES.get(b);
  234. if (rtn != null) {
  235. return rtn;
  236. } else {
  237. throw new IOException("Unrecognized data type: " + b);
  238. }
  239. }
  240. public static DataType fromSQLType(int sqlType)
  241. throws SQLException
  242. {
  243. return fromSQLType(sqlType, 0);
  244. }
  245. public static DataType fromSQLType(int sqlType, int lengthInUnits)
  246. throws SQLException
  247. {
  248. DataType rtn = SQL_TYPES.get(sqlType);
  249. if(rtn == null) {
  250. throw new SQLException("Unsupported SQL type: " + sqlType);
  251. }
  252. // make sure size is reasonable
  253. int size = lengthInUnits * rtn.getUnitSize();
  254. if(rtn.isVariableLength() && !rtn.isValidSize(size)) {
  255. // try alternate type
  256. DataType altRtn = ALT_SQL_TYPES.get(sqlType);
  257. if((altRtn != null) && altRtn.isValidSize(size)) {
  258. // use alternate type
  259. rtn = altRtn;
  260. }
  261. }
  262. return rtn;
  263. }
  264. }