/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.model.impl.jdbc.data;
import org.jkiss.dbeaver.Log;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.data.DBDValueHandler;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSDataType;
import org.jkiss.dbeaver.model.struct.DBSEntity;
import org.jkiss.dbeaver.model.struct.DBSEntityAttribute;
import org.jkiss.utils.CommonUtils;
import java.sql.SQLException;
import java.sql.Struct;
import java.util.*;
/**
* Static struct holder.
* Attributes described by static data type.
*/
public class JDBCCompositeStatic extends JDBCComposite {
private static final Log log = Log.getLog(JDBCCompositeStatic.class);
public JDBCCompositeStatic(@NotNull JDBCComposite struct, @NotNull DBRProgressMonitor monitor) throws DBCException {
super(struct, monitor);
}
public JDBCCompositeStatic(DBCSession session, @NotNull DBSDataType type, @Nullable Struct contents) throws DBCException
{
this.type = type;
// Extract structure data
try {
Object[] attrValues = contents == null ? null : contents.getAttributes();
if (type instanceof DBSEntity) {
DBSEntity entity = (DBSEntity)type;
Collection<? extends DBSEntityAttribute> entityAttributes = CommonUtils.safeCollection(entity.getAttributes(session.getProgressMonitor()));
int valueCount = attrValues == null ? 0 : attrValues.length;
if (attrValues != null && entityAttributes.size() != valueCount) {
log.warn("Number of entity attributes (" + entityAttributes.size() + ") differs from real values (" + valueCount + ")");
}
attributes = entityAttributes.toArray(new DBSEntityAttribute[entityAttributes.size()]);
values = new Object[attributes.length];
for (int i = 0; i < attributes.length; i++) {
DBSEntityAttribute attr = attributes[i];
int ordinalPosition = attr.getOrdinalPosition();
if (ordinalPosition < 0 || attrValues != null && ordinalPosition >= valueCount) {
log.warn("Attribute '" + attr.getName() + "' ordinal position (" + ordinalPosition + ") is out of range (" + valueCount + ")");
continue;
}
Object value = attrValues != null ? attrValues[ordinalPosition] : null;
DBDValueHandler valueHandler = DBUtils.findValueHandler(session, attr);
value = valueHandler.getValueFromObject(session, attr, value, false);
values[ordinalPosition] = value;
}
} else {
attributes = EMPTY_ATTRIBUTE;
values = EMPTY_VALUES;
}
} catch (DBException e) {
throw new DBCException("Can't obtain attributes meta information", e);
} catch (SQLException e) {
throw new DBCException(e, session.getDataSource());
}
}
@Override
public JDBCCompositeStatic cloneValue(DBRProgressMonitor monitor) throws DBCException
{
return new JDBCCompositeStatic(this, monitor);
}
}