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.

ComplexColumnSupport.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. Copyright (c) 2013 James Ahlborn
  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. */
  16. package com.healthmarketscience.jackcess.impl;
  17. import java.io.IOException;
  18. import java.nio.ByteBuffer;
  19. import java.util.EnumSet;
  20. import java.util.List;
  21. import java.util.Set;
  22. import com.healthmarketscience.jackcess.Column;
  23. import com.healthmarketscience.jackcess.CursorBuilder;
  24. import com.healthmarketscience.jackcess.DataType;
  25. import com.healthmarketscience.jackcess.IndexCursor;
  26. import com.healthmarketscience.jackcess.Row;
  27. import com.healthmarketscience.jackcess.Table;
  28. import com.healthmarketscience.jackcess.complex.ComplexColumnInfo;
  29. import com.healthmarketscience.jackcess.complex.ComplexValue;
  30. import com.healthmarketscience.jackcess.impl.complex.AttachmentColumnInfoImpl;
  31. import com.healthmarketscience.jackcess.impl.complex.MultiValueColumnInfoImpl;
  32. import com.healthmarketscience.jackcess.impl.complex.UnsupportedColumnInfoImpl;
  33. import com.healthmarketscience.jackcess.impl.complex.VersionHistoryColumnInfoImpl;
  34. import org.apache.commons.logging.Log;
  35. import org.apache.commons.logging.LogFactory;
  36. /**
  37. * Utility code for loading complex columns.
  38. *
  39. * @author James Ahlborn
  40. */
  41. public class ComplexColumnSupport
  42. {
  43. private static final Log LOG = LogFactory.getLog(ComplexColumnSupport.class);
  44. private static final String COL_COMPLEX_TYPE_OBJECT_ID = "ComplexTypeObjectID";
  45. private static final String COL_TABLE_ID = "ConceptualTableID";
  46. private static final String COL_FLAT_TABLE_ID = "FlatTableID";
  47. private static final Set<DataType> MULTI_VALUE_TYPES = EnumSet.of(
  48. DataType.BYTE, DataType.INT, DataType.LONG, DataType.FLOAT,
  49. DataType.DOUBLE, DataType.GUID, DataType.NUMERIC, DataType.TEXT);
  50. /**
  51. * Creates a ComplexColumnInfo for a complex column.
  52. */
  53. public static ComplexColumnInfo<? extends ComplexValue> create(
  54. ColumnImpl column, ByteBuffer buffer, int offset)
  55. throws IOException
  56. {
  57. int complexTypeId = buffer.getInt(
  58. offset + column.getFormat().OFFSET_COLUMN_COMPLEX_ID);
  59. DatabaseImpl db = column.getDatabase();
  60. TableImpl complexColumns = db.getSystemComplexColumns();
  61. IndexCursor cursor = CursorBuilder.createCursor(
  62. complexColumns.getPrimaryKeyIndex());
  63. if(!cursor.findFirstRowByEntry(complexTypeId)) {
  64. throw new IOException(
  65. "Could not find complex column info for complex column with id " +
  66. complexTypeId);
  67. }
  68. Row cColRow = cursor.getCurrentRow();
  69. int tableId = (Integer)cColRow.get(COL_TABLE_ID);
  70. if(tableId != column.getTable().getTableDefPageNumber()) {
  71. throw new IOException(
  72. "Found complex column for table " + tableId + " but expected table " +
  73. column.getTable().getTableDefPageNumber());
  74. }
  75. int flatTableId = (Integer)cColRow.get(COL_FLAT_TABLE_ID);
  76. int typeObjId = (Integer)cColRow.get(COL_COMPLEX_TYPE_OBJECT_ID);
  77. TableImpl typeObjTable = db.getTable(typeObjId);
  78. TableImpl flatTable = db.getTable(flatTableId);
  79. if((typeObjTable == null) || (flatTable == null)) {
  80. throw new IOException(
  81. "Could not find supporting tables (" + typeObjId + ", " + flatTableId
  82. + ") for complex column with id " + complexTypeId);
  83. }
  84. // we inspect the structore of the "type table" to determine what kind of
  85. // complex info we are dealing with
  86. if(isMultiValueColumn(typeObjTable)) {
  87. return new MultiValueColumnInfoImpl(column, complexTypeId, typeObjTable,
  88. flatTable);
  89. } else if(isAttachmentColumn(typeObjTable)) {
  90. return new AttachmentColumnInfoImpl(column, complexTypeId, typeObjTable,
  91. flatTable);
  92. } else if(isVersionHistoryColumn(typeObjTable)) {
  93. return new VersionHistoryColumnInfoImpl(column, complexTypeId, typeObjTable,
  94. flatTable);
  95. }
  96. LOG.warn("Unsupported complex column type " + typeObjTable.getName());
  97. return new UnsupportedColumnInfoImpl(column, complexTypeId, typeObjTable,
  98. flatTable);
  99. }
  100. public static boolean isMultiValueColumn(Table typeObjTable) {
  101. // if we found a single value of a "simple" type, then we are dealing with
  102. // a multi-value column
  103. List<? extends Column> typeCols = typeObjTable.getColumns();
  104. return ((typeCols.size() == 1) &&
  105. MULTI_VALUE_TYPES.contains(typeCols.get(0).getType()));
  106. }
  107. public static boolean isAttachmentColumn(Table typeObjTable) {
  108. // attachment data has these columns FileURL(MEMO), FileName(TEXT),
  109. // FileType(TEXT), FileData(OLE), FileTimeStamp(SHORT_DATE_TIME),
  110. // FileFlags(LONG)
  111. List<? extends Column> typeCols = typeObjTable.getColumns();
  112. if(typeCols.size() < 6) {
  113. return false;
  114. }
  115. int numMemo = 0;
  116. int numText = 0;
  117. int numDate = 0;
  118. int numOle= 0;
  119. int numLong = 0;
  120. for(Column col : typeCols) {
  121. switch(col.getType()) {
  122. case TEXT:
  123. ++numText;
  124. break;
  125. case LONG:
  126. ++numLong;
  127. break;
  128. case SHORT_DATE_TIME:
  129. ++numDate;
  130. break;
  131. case OLE:
  132. ++numOle;
  133. break;
  134. case MEMO:
  135. ++numMemo;
  136. break;
  137. default:
  138. // ignore
  139. }
  140. }
  141. // be flexible, allow for extra columns...
  142. return((numMemo >= 1) && (numText >= 2) && (numOle >= 1) &&
  143. (numDate >= 1) && (numLong >= 1));
  144. }
  145. public static boolean isVersionHistoryColumn(Table typeObjTable) {
  146. // version history data has these columns <value>(MEMO),
  147. // <modified>(SHORT_DATE_TIME)
  148. List<? extends Column> typeCols = typeObjTable.getColumns();
  149. if(typeCols.size() < 2) {
  150. return false;
  151. }
  152. int numMemo = 0;
  153. int numDate = 0;
  154. for(Column col : typeCols) {
  155. switch(col.getType()) {
  156. case SHORT_DATE_TIME:
  157. ++numDate;
  158. break;
  159. case MEMO:
  160. ++numMemo;
  161. break;
  162. default:
  163. // ignore
  164. }
  165. }
  166. // be flexible, allow for extra columns...
  167. return((numMemo >= 1) && (numDate >= 1));
  168. }
  169. }