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.

ComplexColumnInfoImpl.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. Copyright (c) 2011 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.complex;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.Collection;
  17. import java.util.Collections;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.Map;
  21. import com.healthmarketscience.jackcess.Column;
  22. import com.healthmarketscience.jackcess.DataType;
  23. import com.healthmarketscience.jackcess.Database;
  24. import com.healthmarketscience.jackcess.IndexCursor;
  25. import com.healthmarketscience.jackcess.Row;
  26. import com.healthmarketscience.jackcess.RowId;
  27. import com.healthmarketscience.jackcess.RuntimeIOException;
  28. import com.healthmarketscience.jackcess.Table;
  29. import com.healthmarketscience.jackcess.complex.ComplexColumnInfo;
  30. import com.healthmarketscience.jackcess.complex.ComplexDataType;
  31. import com.healthmarketscience.jackcess.complex.ComplexValue;
  32. import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey;
  33. import com.healthmarketscience.jackcess.impl.ColumnImpl;
  34. import com.healthmarketscience.jackcess.impl.CustomToStringStyle;
  35. import com.healthmarketscience.jackcess.impl.TableImpl;
  36. /**
  37. * Base class for the additional information tracked for complex columns.
  38. *
  39. * @author James Ahlborn
  40. */
  41. public abstract class ComplexColumnInfoImpl<V extends ComplexValue>
  42. implements ComplexColumnInfo<V>
  43. {
  44. private static final int INVALID_ID_VALUE = -1;
  45. public static final ComplexValue.Id INVALID_ID = new ComplexValueIdImpl(
  46. INVALID_ID_VALUE, null);
  47. public static final ComplexValueForeignKey INVALID_FK =
  48. new ComplexValueForeignKeyImpl(null, INVALID_ID_VALUE);
  49. private final Column _column;
  50. private final int _complexTypeId;
  51. private final Table _flatTable;
  52. private final List<Column> _typeCols;
  53. private final Column _pkCol;
  54. private final Column _complexValFkCol;
  55. private IndexCursor _complexValIdCursor;
  56. protected ComplexColumnInfoImpl(Column column, int complexTypeId,
  57. Table typeObjTable, Table flatTable)
  58. throws IOException
  59. {
  60. _column = column;
  61. _complexTypeId = complexTypeId;
  62. _flatTable = flatTable;
  63. // the flat table has all the "value" columns and 2 extra columns, a
  64. // primary key for each row, and a LONG value which is essentially a
  65. // foreign key to the main table.
  66. List<Column> typeCols = new ArrayList<Column>();
  67. List<Column> otherCols = new ArrayList<Column>();
  68. diffFlatColumns(typeObjTable, flatTable, typeCols, otherCols);
  69. _typeCols = Collections.unmodifiableList(typeCols);
  70. Column pkCol = null;
  71. Column complexValFkCol = null;
  72. for(Column col : otherCols) {
  73. if(col.isAutoNumber()) {
  74. pkCol = col;
  75. } else if(col.getType() == DataType.LONG) {
  76. complexValFkCol = col;
  77. }
  78. }
  79. if((pkCol == null) || (complexValFkCol == null)) {
  80. throw new IOException("Could not find expected columns in flat table " +
  81. flatTable.getName() + " for complex column with id "
  82. + complexTypeId);
  83. }
  84. _pkCol = pkCol;
  85. _complexValFkCol = complexValFkCol;
  86. }
  87. public void postTableLoadInit() throws IOException {
  88. // nothing to do in base class
  89. }
  90. public Column getColumn() {
  91. return _column;
  92. }
  93. public Database getDatabase() {
  94. return getColumn().getDatabase();
  95. }
  96. public Column getPrimaryKeyColumn() {
  97. return _pkCol;
  98. }
  99. public Column getComplexValueForeignKeyColumn() {
  100. return _complexValFkCol;
  101. }
  102. protected List<Column> getTypeColumns() {
  103. return _typeCols;
  104. }
  105. public int countValues(int complexValueFk) throws IOException {
  106. return getRawValues(complexValueFk,
  107. Collections.singleton(_complexValFkCol.getName()))
  108. .size();
  109. }
  110. public List<Row> getRawValues(int complexValueFk)
  111. throws IOException
  112. {
  113. return getRawValues(complexValueFk, null);
  114. }
  115. private Iterator<Row> getComplexValFkIter(
  116. int complexValueFk, Collection<String> columnNames)
  117. throws IOException
  118. {
  119. if(_complexValIdCursor == null) {
  120. _complexValIdCursor = _flatTable.newCursor()
  121. .setIndexByColumns(_complexValFkCol)
  122. .toIndexCursor();
  123. }
  124. return _complexValIdCursor.newEntryIterable(complexValueFk)
  125. .setColumnNames(columnNames).iterator();
  126. }
  127. public List<Row> getRawValues(int complexValueFk,
  128. Collection<String> columnNames)
  129. throws IOException
  130. {
  131. Iterator<Row> entryIter =
  132. getComplexValFkIter(complexValueFk, columnNames);
  133. if(!entryIter.hasNext()) {
  134. return Collections.emptyList();
  135. }
  136. List<Row> values = new ArrayList<Row>();
  137. while(entryIter.hasNext()) {
  138. values.add(entryIter.next());
  139. }
  140. return values;
  141. }
  142. public List<V> getValues(ComplexValueForeignKey complexValueFk)
  143. throws IOException
  144. {
  145. List<Row> rawValues = getRawValues(complexValueFk.get());
  146. if(rawValues.isEmpty()) {
  147. return Collections.emptyList();
  148. }
  149. return toValues(complexValueFk, rawValues);
  150. }
  151. protected List<V> toValues(ComplexValueForeignKey complexValueFk,
  152. List<Row> rawValues)
  153. throws IOException
  154. {
  155. List<V> values = new ArrayList<V>();
  156. for(Row rawValue : rawValues) {
  157. values.add(toValue(complexValueFk, rawValue));
  158. }
  159. return values;
  160. }
  161. public ComplexValue.Id addRawValue(Map<String,?> rawValue)
  162. throws IOException
  163. {
  164. Object[] row = ((TableImpl)_flatTable).asRowWithRowId(rawValue);
  165. _flatTable.addRow(row);
  166. return getValueId(row);
  167. }
  168. public ComplexValue.Id addValue(V value) throws IOException {
  169. Object[] row = asRow(newRowArray(), value);
  170. _flatTable.addRow(row);
  171. ComplexValue.Id id = getValueId(row);
  172. value.setId(id);
  173. return id;
  174. }
  175. public void addValues(Collection<? extends V> values) throws IOException {
  176. for(V value : values) {
  177. addValue(value);
  178. }
  179. }
  180. public ComplexValue.Id updateRawValue(Row rawValue) throws IOException {
  181. _flatTable.updateRow(rawValue);
  182. return getValueId(rawValue);
  183. }
  184. public ComplexValue.Id updateValue(V value) throws IOException {
  185. ComplexValue.Id id = value.getId();
  186. updateRow(id, asRow(newRowArray(), value));
  187. return id;
  188. }
  189. public void updateValues(Collection<? extends V> values) throws IOException {
  190. for(V value : values) {
  191. updateValue(value);
  192. }
  193. }
  194. public void deleteRawValue(Row rawValue) throws IOException {
  195. deleteRow(rawValue.getId());
  196. }
  197. public void deleteValue(V value) throws IOException {
  198. deleteRow(value.getId().getRowId());
  199. }
  200. public void deleteValues(Collection<? extends V> values) throws IOException {
  201. for(V value : values) {
  202. deleteValue(value);
  203. }
  204. }
  205. public void deleteAllValues(int complexValueFk) throws IOException {
  206. Iterator<Row> entryIter =
  207. getComplexValFkIter(complexValueFk, Collections.<String>emptySet());
  208. try {
  209. while(entryIter.hasNext()) {
  210. entryIter.next();
  211. entryIter.remove();
  212. }
  213. } catch(RuntimeIOException e) {
  214. throw (IOException)e.getCause();
  215. }
  216. }
  217. public void deleteAllValues(ComplexValueForeignKey complexValueFk)
  218. throws IOException
  219. {
  220. deleteAllValues(complexValueFk.get());
  221. }
  222. private void updateRow(ComplexValue.Id id, Object[] row) throws IOException {
  223. ((TableImpl)_flatTable).updateRow(id.getRowId(), row);
  224. }
  225. private void deleteRow(RowId rowId) throws IOException {
  226. ((TableImpl)_flatTable).deleteRow(rowId);
  227. }
  228. protected ComplexValueIdImpl getValueId(Row row) {
  229. int idVal = (Integer)getPrimaryKeyColumn().getRowValue(row);
  230. return new ComplexValueIdImpl(idVal, row.getId());
  231. }
  232. protected ComplexValueIdImpl getValueId(Object[] row) {
  233. int idVal = (Integer)getPrimaryKeyColumn().getRowValue(row);
  234. return new ComplexValueIdImpl(idVal,
  235. ((TableImpl)_flatTable).getRowId(row));
  236. }
  237. protected Object[] asRow(Object[] row, V value)
  238. throws IOException
  239. {
  240. ComplexValue.Id id = value.getId();
  241. _pkCol.setRowValue(
  242. row, ((id != INVALID_ID) ? id : Column.AUTO_NUMBER));
  243. ComplexValueForeignKey cFk = value.getComplexValueForeignKey();
  244. _complexValFkCol.setRowValue(
  245. row, ((cFk != INVALID_FK) ? cFk : Column.AUTO_NUMBER));
  246. return row;
  247. }
  248. private Object[] newRowArray() {
  249. Object[] row = new Object[_flatTable.getColumnCount() + 1];
  250. row[row.length - 1] = ColumnImpl.RETURN_ROW_ID;
  251. return row;
  252. }
  253. @Override
  254. public String toString() {
  255. return CustomToStringStyle.valueBuilder(this)
  256. .append("complexType", getType())
  257. .append("complexTypeId", _complexTypeId)
  258. .toString();
  259. }
  260. protected static void diffFlatColumns(Table typeObjTable,
  261. Table flatTable,
  262. List<Column> typeCols,
  263. List<Column> otherCols)
  264. {
  265. // each "flat"" table has the columns from the "type" table, plus some
  266. // others. separate the "flat" columns into these 2 buckets
  267. for(Column col : flatTable.getColumns()) {
  268. if(((TableImpl)typeObjTable).hasColumn(col.getName())) {
  269. typeCols.add(col);
  270. } else {
  271. otherCols.add(col);
  272. }
  273. }
  274. }
  275. public abstract ComplexDataType getType();
  276. protected abstract V toValue(
  277. ComplexValueForeignKey complexValueFk,
  278. Row rawValues)
  279. throws IOException;
  280. protected static abstract class ComplexValueImpl implements ComplexValue
  281. {
  282. private Id _id;
  283. private ComplexValueForeignKey _complexValueFk;
  284. protected ComplexValueImpl(Id id, ComplexValueForeignKey complexValueFk) {
  285. _id = id;
  286. _complexValueFk = complexValueFk;
  287. }
  288. public Id getId() {
  289. return _id;
  290. }
  291. public void setId(Id id) {
  292. if(_id == id) {
  293. // harmless, ignore
  294. return;
  295. }
  296. if(_id != INVALID_ID) {
  297. throw new IllegalStateException("id may not be reset");
  298. }
  299. _id = id;
  300. }
  301. public ComplexValueForeignKey getComplexValueForeignKey() {
  302. return _complexValueFk;
  303. }
  304. public void setComplexValueForeignKey(ComplexValueForeignKey complexValueFk)
  305. {
  306. if(_complexValueFk == complexValueFk) {
  307. // harmless, ignore
  308. return;
  309. }
  310. if(_complexValueFk != INVALID_FK) {
  311. throw new IllegalStateException("complexValueFk may not be reset");
  312. }
  313. _complexValueFk = complexValueFk;
  314. }
  315. public Column getColumn() {
  316. return _complexValueFk.getColumn();
  317. }
  318. @Override
  319. public int hashCode() {
  320. return ((_id.get() * 37) ^ _complexValueFk.hashCode());
  321. }
  322. @Override
  323. public boolean equals(Object o) {
  324. return ((this == o) ||
  325. ((o != null) && (getClass() == o.getClass()) &&
  326. (_id == ((ComplexValueImpl)o)._id) &&
  327. _complexValueFk.equals(((ComplexValueImpl)o)._complexValueFk)));
  328. }
  329. }
  330. /**
  331. * Implementation of ComplexValue.Id.
  332. */
  333. private static final class ComplexValueIdImpl extends ComplexValue.Id
  334. {
  335. private static final long serialVersionUID = 20130318L;
  336. private final int _value;
  337. private final RowId _rowId;
  338. protected ComplexValueIdImpl(int value, RowId rowId) {
  339. _value = value;
  340. _rowId = rowId;
  341. }
  342. @Override
  343. public int get() {
  344. return _value;
  345. }
  346. @Override
  347. public RowId getRowId() {
  348. return _rowId;
  349. }
  350. }
  351. }