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.

MultiValueColumnInfoImpl.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 com.healthmarketscience.jackcess.Column;
  16. import com.healthmarketscience.jackcess.Row;
  17. import com.healthmarketscience.jackcess.Table;
  18. import com.healthmarketscience.jackcess.complex.ComplexDataType;
  19. import com.healthmarketscience.jackcess.complex.ComplexValue;
  20. import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey;
  21. import com.healthmarketscience.jackcess.complex.MultiValueColumnInfo;
  22. import com.healthmarketscience.jackcess.complex.SingleValue;
  23. /**
  24. * Complex column info for a column holding multiple simple values per row.
  25. *
  26. * @author James Ahlborn
  27. */
  28. public class MultiValueColumnInfoImpl extends ComplexColumnInfoImpl<SingleValue>
  29. implements MultiValueColumnInfo
  30. {
  31. private final Column _valueCol;
  32. public MultiValueColumnInfoImpl(Column column, int complexId,
  33. Table typeObjTable, Table flatTable)
  34. throws IOException
  35. {
  36. super(column, complexId, typeObjTable, flatTable);
  37. _valueCol = getTypeColumns().get(0);
  38. }
  39. @Override
  40. public ComplexDataType getType()
  41. {
  42. return ComplexDataType.MULTI_VALUE;
  43. }
  44. public Column getValueColumn() {
  45. return _valueCol;
  46. }
  47. @Override
  48. protected SingleValueImpl toValue(
  49. ComplexValueForeignKey complexValueFk,
  50. Row rawValue)
  51. {
  52. ComplexValue.Id id = getValueId(rawValue);
  53. Object value = getValueColumn().getRowValue(rawValue);
  54. return new SingleValueImpl(id, complexValueFk, value);
  55. }
  56. @Override
  57. protected Object[] asRow(Object[] row, SingleValue value) throws IOException {
  58. super.asRow(row, value);
  59. getValueColumn().setRowValue(row, value.get());
  60. return row;
  61. }
  62. public static SingleValue newSingleValue(Object value) {
  63. return newSingleValue(INVALID_FK, value);
  64. }
  65. public static SingleValue newSingleValue(
  66. ComplexValueForeignKey complexValueFk, Object value) {
  67. return new SingleValueImpl(INVALID_ID, complexValueFk, value);
  68. }
  69. private static class SingleValueImpl extends ComplexValueImpl
  70. implements SingleValue
  71. {
  72. private Object _value;
  73. private SingleValueImpl(Id id, ComplexValueForeignKey complexValueFk,
  74. Object value)
  75. {
  76. super(id, complexValueFk);
  77. _value = value;
  78. }
  79. public Object get() {
  80. return _value;
  81. }
  82. public void set(Object value) {
  83. _value = value;
  84. }
  85. public void update() throws IOException {
  86. getComplexValueForeignKey().updateMultiValue(this);
  87. }
  88. public void delete() throws IOException {
  89. getComplexValueForeignKey().deleteMultiValue(this);
  90. }
  91. @Override
  92. public String toString()
  93. {
  94. return "SingleValue(" + getComplexValueForeignKey() + "," + getId() +
  95. ") " + get();
  96. }
  97. }
  98. }