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.

MultiValueColumnInfo.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Copyright (c) 2011 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.complex;
  17. import java.io.IOException;
  18. import java.util.EnumSet;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Set;
  22. import com.healthmarketscience.jackcess.Column;
  23. import com.healthmarketscience.jackcess.DataType;
  24. import com.healthmarketscience.jackcess.Table;
  25. /**
  26. * Complex column info for a column holding multiple values per row.
  27. *
  28. * @author James Ahlborn
  29. */
  30. public class MultiValueColumnInfo extends ComplexColumnInfo<SingleValue>
  31. {
  32. private static final Set<DataType> VALUE_TYPES = EnumSet.of(
  33. DataType.BYTE, DataType.INT, DataType.LONG, DataType.FLOAT,
  34. DataType.DOUBLE, DataType.GUID, DataType.NUMERIC, DataType.TEXT);
  35. private final Column _valueCol;
  36. public MultiValueColumnInfo(Column column, int complexId,
  37. Table typeObjTable, Table flatTable)
  38. throws IOException
  39. {
  40. super(column, complexId, typeObjTable, flatTable);
  41. _valueCol = getTypeColumns().get(0);
  42. }
  43. @Override
  44. public ComplexDataType getType()
  45. {
  46. return ComplexDataType.MULTI_VALUE;
  47. }
  48. public Column getValueColumn() {
  49. return _valueCol;
  50. }
  51. @Override
  52. protected SingleValueImpl toValue(
  53. ComplexValueForeignKey complexValueFk,
  54. Map<String,Object> rawValue)
  55. {
  56. int id = (Integer)getPrimaryKeyColumn().getRowValue(rawValue);
  57. Object value = getValueColumn().getRowValue(rawValue);
  58. return new SingleValueImpl(id, complexValueFk, value);
  59. }
  60. @Override
  61. protected Object[] asRow(Object[] row, SingleValue value) throws IOException {
  62. super.asRow(row, value);
  63. getValueColumn().setRowValue(row, value.get());
  64. return row;
  65. }
  66. public static SingleValue newSingleValue(Object value) {
  67. return newSingleValue(INVALID_COMPLEX_VALUE_ID, value);
  68. }
  69. public static SingleValue newSingleValue(
  70. ComplexValueForeignKey complexValueFk, Object value) {
  71. return new SingleValueImpl(INVALID_ID, complexValueFk, value);
  72. }
  73. public static boolean isMultiValueColumn(Table typeObjTable) {
  74. // if we found a single value of a "simple" type, then we are dealing with
  75. // a multi-value column
  76. List<Column> typeCols = typeObjTable.getColumns();
  77. return ((typeCols.size() == 1) &&
  78. VALUE_TYPES.contains(typeCols.get(0).getType()));
  79. }
  80. private static class SingleValueImpl extends ComplexValueImpl
  81. implements SingleValue
  82. {
  83. private Object _value;
  84. private SingleValueImpl(int id, ComplexValueForeignKey complexValueFk,
  85. Object value)
  86. {
  87. super(id, complexValueFk);
  88. _value = value;
  89. }
  90. public Object get() {
  91. return _value;
  92. }
  93. public void set(Object value) {
  94. _value = value;
  95. }
  96. public void update() throws IOException {
  97. getComplexValueForeignKey().updateMultiValue(this);
  98. }
  99. public void delete() throws IOException {
  100. getComplexValueForeignKey().deleteMultiValue(this);
  101. }
  102. @Override
  103. public String toString()
  104. {
  105. return "SingleValue(" + getComplexValueForeignKey() + "," + getId() +
  106. ") " + get();
  107. }
  108. }
  109. }