Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DataType.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.sql.SQLException;
  24. import java.sql.Types;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. /**
  28. * Access data type
  29. * @author Tim McCune
  30. */
  31. public enum DataType {
  32. BOOLEAN((byte) 0x01, Types.BOOLEAN, 0),
  33. BYTE((byte) 0x02, Types.TINYINT, 1),
  34. INT((byte) 0x03, Types.SMALLINT, 2),
  35. LONG((byte) 0x04, Types.INTEGER, 4),
  36. MONEY((byte) 0x05, Types.DECIMAL, 8),
  37. FLOAT((byte) 0x06, Types.FLOAT, 4),
  38. DOUBLE((byte) 0x07, Types.DOUBLE, 8),
  39. SHORT_DATE_TIME((byte) 0x08, Types.TIMESTAMP, 8),
  40. BINARY((byte) 0x09, Types.BINARY, 255, true),
  41. TEXT((byte) 0x0A, Types.VARCHAR, 50 * 2, true),
  42. OLE((byte) 0x0B, Types.LONGVARBINARY, 12, true),
  43. MEMO((byte) 0x0C, Types.LONGVARCHAR, 12, true),
  44. UNKNOWN_0D((byte) 0x0D),
  45. GUID((byte) 0x0F, null, 16),
  46. NUMERIC((byte) 0x10, Types.NUMERIC, 17);
  47. /** Map of SQL types to Access data types */
  48. private static Map<Integer, DataType> SQL_TYPES = new HashMap<Integer, DataType>();
  49. static {
  50. for (DataType type : DataType.values()) {
  51. if (type._sqlType != null) {
  52. SQL_TYPES.put(type._sqlType, type);
  53. }
  54. }
  55. SQL_TYPES.put(Types.BIT, BYTE);
  56. SQL_TYPES.put(Types.BLOB, OLE);
  57. SQL_TYPES.put(Types.BIGINT, LONG);
  58. SQL_TYPES.put(Types.CHAR, TEXT);
  59. SQL_TYPES.put(Types.DATE, SHORT_DATE_TIME);
  60. SQL_TYPES.put(Types.REAL, DOUBLE);
  61. SQL_TYPES.put(Types.TIME, SHORT_DATE_TIME);
  62. SQL_TYPES.put(Types.VARBINARY, BINARY);
  63. }
  64. private static Map<Byte, DataType> DATA_TYPES = new HashMap<Byte, DataType>();
  65. static {
  66. for (DataType type : DataType.values()) {
  67. DATA_TYPES.put(type._value, type);
  68. }
  69. }
  70. private boolean _variableLength = false;
  71. /** Internal Access value */
  72. private byte _value;
  73. /** Size in bytes */
  74. private Integer _size;
  75. /** SQL type equivalent, or null if none defined */
  76. private Integer _sqlType;
  77. private DataType(byte value) {
  78. _value = value;
  79. }
  80. private DataType(byte value, Integer sqlType, Integer size) {
  81. this(value);
  82. _sqlType = sqlType;
  83. _size = size;
  84. }
  85. private DataType(byte value, Integer sqlType, Integer size,
  86. boolean variableLength) {
  87. this(value, sqlType, size);
  88. _variableLength = variableLength;
  89. }
  90. public byte getValue() {
  91. return _value;
  92. }
  93. public boolean isVariableLength() {
  94. return _variableLength;
  95. }
  96. public int getSize() {
  97. if (_size != null) {
  98. return _size;
  99. } else {
  100. throw new IllegalArgumentException("FIX ME");
  101. }
  102. }
  103. public int getSQLType() throws SQLException {
  104. if (_sqlType != null) {
  105. return _sqlType;
  106. } else {
  107. throw new SQLException("Unsupported data type: " + toString());
  108. }
  109. }
  110. public static DataType fromByte(byte b) throws SQLException {
  111. DataType rtn = DATA_TYPES.get(b);
  112. if (rtn != null) {
  113. return rtn;
  114. } else {
  115. throw new SQLException("Unrecognized data type: " + b);
  116. }
  117. }
  118. public static DataType fromSQLType(int sqlType) throws SQLException {
  119. DataType rtn = SQL_TYPES.get(sqlType);
  120. if (rtn != null) {
  121. return rtn;
  122. } else {
  123. throw new SQLException("Unsupported SQL type: " + sqlType);
  124. }
  125. }
  126. }