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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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),
  42. TEXT((byte) 0x0A, Types.VARCHAR, null, true, false, 0, 50 * 2,
  43. (int)JetFormat.TEXT_FIELD_MAX_LENGTH),
  44. OLE((byte) 0x0B, Types.LONGVARBINARY, null, true, true, 0, null, 0xFFFFFF),
  45. MEMO((byte) 0x0C, Types.LONGVARCHAR, null, true, true, 0, null, 0xFFFFFF),
  46. UNKNOWN_0D((byte) 0x0D),
  47. GUID((byte) 0x0F, null, 16),
  48. // for some reason numeric is "var len" even though it has a fixed size...
  49. NUMERIC((byte) 0x10, Types.NUMERIC, null, true, false, 17, 17, 17,
  50. true, 0, 0, 28, 1, 18, 28);
  51. /** Map of SQL types to Access data types */
  52. private static Map<Integer, DataType> SQL_TYPES = new HashMap<Integer, DataType>();
  53. static {
  54. for (DataType type : DataType.values()) {
  55. if (type._sqlType != null) {
  56. SQL_TYPES.put(type._sqlType, type);
  57. }
  58. }
  59. SQL_TYPES.put(Types.BIT, BYTE);
  60. SQL_TYPES.put(Types.BLOB, OLE);
  61. SQL_TYPES.put(Types.BIGINT, LONG);
  62. SQL_TYPES.put(Types.CHAR, TEXT);
  63. SQL_TYPES.put(Types.DATE, SHORT_DATE_TIME);
  64. SQL_TYPES.put(Types.REAL, DOUBLE);
  65. SQL_TYPES.put(Types.TIME, SHORT_DATE_TIME);
  66. SQL_TYPES.put(Types.VARBINARY, BINARY);
  67. }
  68. private static Map<Byte, DataType> DATA_TYPES = new HashMap<Byte, DataType>();
  69. static {
  70. for (DataType type : DataType.values()) {
  71. DATA_TYPES.put(type._value, type);
  72. }
  73. }
  74. /** is this a variable length field */
  75. private boolean _variableLength;
  76. /** is this a long value field */
  77. private boolean _longValue;
  78. /** does this field have scale/precision */
  79. private boolean _hasScalePrecision;
  80. /** Internal Access value */
  81. private byte _value;
  82. /** Size in bytes of fixed length columns */
  83. private Integer _fixedSize;
  84. /** min in bytes size for var length columns */
  85. private Integer _minSize;
  86. /** default size in bytes for var length columns */
  87. private Integer _defaultSize;
  88. /** Max size in bytes for var length columns */
  89. private Integer _maxSize;
  90. /** SQL type equivalent, or null if none defined */
  91. private Integer _sqlType;
  92. /** min scale value */
  93. private Integer _minScale;
  94. /** the default scale value */
  95. private Integer _defaultScale;
  96. /** max scale value */
  97. private Integer _maxScale;
  98. /** min precision value */
  99. private Integer _minPrecision;
  100. /** the default precision value */
  101. private Integer _defaultPrecision;
  102. /** max precision value */
  103. private Integer _maxPrecision;
  104. private DataType(byte value) {
  105. this(value, null, null);
  106. }
  107. private DataType(byte value, Integer sqlType, Integer fixedSize) {
  108. this(value, sqlType, fixedSize, false, false, null, null, null);
  109. }
  110. private DataType(byte value, Integer sqlType, Integer fixedSize,
  111. boolean variableLength,
  112. boolean longValue,
  113. Integer minSize,
  114. Integer defaultSize,
  115. Integer maxSize) {
  116. this(value, sqlType, fixedSize, variableLength, longValue,
  117. minSize, defaultSize, maxSize,
  118. false, null, null, null, null, null, null);
  119. }
  120. private DataType(byte value, Integer sqlType, Integer fixedSize,
  121. boolean variableLength,
  122. boolean longValue,
  123. Integer minSize,
  124. Integer defaultSize,
  125. Integer maxSize,
  126. boolean hasScalePrecision,
  127. Integer minScale,
  128. Integer defaultScale,
  129. Integer maxScale,
  130. Integer minPrecision,
  131. Integer defaultPrecision,
  132. Integer maxPrecision) {
  133. _value = value;
  134. _sqlType = sqlType;
  135. _fixedSize = fixedSize;
  136. _variableLength = variableLength;
  137. _longValue = longValue;
  138. _minSize = minSize;
  139. _defaultSize = defaultSize;
  140. _maxSize = maxSize;
  141. _hasScalePrecision = hasScalePrecision;
  142. _minScale = minScale;
  143. _defaultScale = defaultScale;
  144. _maxScale = maxScale;
  145. _minPrecision = minPrecision;
  146. _defaultPrecision = defaultPrecision;
  147. _maxPrecision = maxPrecision;
  148. }
  149. public byte getValue() {
  150. return _value;
  151. }
  152. public boolean isVariableLength() {
  153. return _variableLength;
  154. }
  155. public boolean isLongValue() {
  156. return _longValue;
  157. }
  158. public boolean getHasScalePrecision() {
  159. return _hasScalePrecision;
  160. }
  161. public int getFixedSize() {
  162. if(_fixedSize != null) {
  163. return _fixedSize;
  164. } else {
  165. throw new IllegalArgumentException("FIX ME");
  166. }
  167. }
  168. public int getMinSize() {
  169. return _minSize;
  170. }
  171. public int getDefaultSize() {
  172. return _defaultSize;
  173. }
  174. public int getMaxSize() {
  175. return _maxSize;
  176. }
  177. public int getSQLType() throws SQLException {
  178. if (_sqlType != null) {
  179. return _sqlType;
  180. } else {
  181. throw new SQLException("Unsupported data type: " + toString());
  182. }
  183. }
  184. public int getMinScale() {
  185. return _minScale;
  186. }
  187. public int getDefaultScale() {
  188. return _defaultScale;
  189. }
  190. public int getMaxScale() {
  191. return _maxScale;
  192. }
  193. public int getMinPrecision() {
  194. return _minPrecision;
  195. }
  196. public int getDefaultPrecision() {
  197. return _defaultPrecision;
  198. }
  199. public int getMaxPrecision() {
  200. return _maxPrecision;
  201. }
  202. public static DataType fromByte(byte b) throws IOException {
  203. DataType rtn = DATA_TYPES.get(b);
  204. if (rtn != null) {
  205. return rtn;
  206. } else {
  207. throw new IOException("Unrecognized data type: " + b);
  208. }
  209. }
  210. public static DataType fromSQLType(int sqlType) throws SQLException {
  211. DataType rtn = SQL_TYPES.get(sqlType);
  212. if (rtn != null) {
  213. return rtn;
  214. } else {
  215. throw new SQLException("Unsupported SQL type: " + sqlType);
  216. }
  217. }
  218. }