123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- /*
- Copyright (c) 2011 James Ahlborn
-
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
-
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- USA
- */
-
- package com.healthmarketscience.jackcess.complex;
-
- import java.io.IOException;
- import java.util.EnumSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
-
- import com.healthmarketscience.jackcess.Column;
- import com.healthmarketscience.jackcess.DataType;
- import com.healthmarketscience.jackcess.Table;
-
- /**
- * Complex column info for a column holding multiple values per row.
- *
- * @author James Ahlborn
- */
- public class MultiValueColumnInfo extends ComplexColumnInfo<SingleValue>
- {
- private static final Set<DataType> VALUE_TYPES = EnumSet.of(
- DataType.BYTE, DataType.INT, DataType.LONG, DataType.FLOAT,
- DataType.DOUBLE, DataType.GUID, DataType.NUMERIC, DataType.TEXT);
-
- private final Column _valueCol;
-
- public MultiValueColumnInfo(Column column, int complexId,
- Table typeObjTable, Table flatTable)
- throws IOException
- {
- super(column, complexId, typeObjTable, flatTable);
-
- _valueCol = getTypeColumns().get(0);
- }
-
- @Override
- public ComplexDataType getType()
- {
- return ComplexDataType.MULTI_VALUE;
- }
-
- public Column getValueColumn() {
- return _valueCol;
- }
-
- @Override
- protected SingleValueImpl toValue(
- ComplexValueForeignKey complexValueFk,
- Map<String,Object> rawValue)
- {
- int id = (Integer)getPrimaryKeyColumn().getRowValue(rawValue);
- Object value = getValueColumn().getRowValue(rawValue);
-
- return new SingleValueImpl(id, complexValueFk, value);
- }
-
- @Override
- protected Object[] asRow(Object[] row, SingleValue value) throws IOException {
- super.asRow(row, value);
- getValueColumn().setRowValue(row, value.get());
- return row;
- }
-
- public static SingleValue newSingleValue(Object value) {
- return newSingleValue(INVALID_COMPLEX_VALUE_ID, value);
- }
-
- public static SingleValue newSingleValue(
- ComplexValueForeignKey complexValueFk, Object value) {
- return new SingleValueImpl(INVALID_ID, complexValueFk, value);
- }
-
- public static boolean isMultiValueColumn(Table typeObjTable) {
- // if we found a single value of a "simple" type, then we are dealing with
- // a multi-value column
- List<Column> typeCols = typeObjTable.getColumns();
- return ((typeCols.size() == 1) &&
- VALUE_TYPES.contains(typeCols.get(0).getType()));
- }
-
- private static class SingleValueImpl extends ComplexValueImpl
- implements SingleValue
- {
- private Object _value;
-
- private SingleValueImpl(int id, ComplexValueForeignKey complexValueFk,
- Object value)
- {
- super(id, complexValueFk);
- _value = value;
- }
-
- public Object get() {
- return _value;
- }
-
- public void set(Object value) {
- _value = value;
- }
-
- public void update() throws IOException {
- getComplexValueForeignKey().updateMultiValue(this);
- }
-
- public void delete() throws IOException {
- getComplexValueForeignKey().deleteMultiValue(this);
- }
-
- @Override
- public String toString()
- {
- return "SingleValue(" + getComplexValueForeignKey() + "," + getId() +
- ") " + get();
- }
- }
- }
|