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.5KB

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