Search in sources :

Example 1 with JDBCStructImpl

use of org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl in project dbeaver by serge-rider.

the class PostgreStructValueHandler method convertStringToStruct.

private JDBCCompositeStatic convertStringToStruct(@NotNull DBCSession session, @NotNull PostgreDataType compType, @NotNull String value) throws DBException {
    if (value.startsWith("(") && value.endsWith(")")) {
        value = value.substring(1, value.length() - 1);
    }
    final Collection<PostgreDataTypeAttribute> attributes = compType.getAttributes(session.getProgressMonitor());
    if (attributes == null) {
        throw new DBException("Composite type '" + compType.getTypeName() + "' has no attributes");
    }
    String[] parsedValues = PostgreValueParser.parseSingleObject(value);
    if (parsedValues.length != attributes.size()) {
        log.debug("Number of attributes (" + attributes.size() + ") doesn't match actual number of parsed strings (" + parsedValues.length + ")");
    }
    Object[] attrValues = new Object[attributes.size()];
    Iterator<PostgreDataTypeAttribute> attrIter = attributes.iterator();
    for (int i = 0; i < parsedValues.length && attrIter.hasNext(); i++) {
        final PostgreDataTypeAttribute itemAttr = attrIter.next();
        attrValues[i] = PostgreValueParser.convertStringToValue(session, itemAttr, parsedValues[i]);
    }
    Struct contents = new JDBCStructImpl(compType.getTypeName(), attrValues, value);
    return new JDBCCompositeStatic(session, compType, contents);
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCCompositeStatic(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeStatic) PostgreDataTypeAttribute(org.jkiss.dbeaver.ext.postgresql.model.PostgreDataTypeAttribute) JDBCStructImpl(org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) Struct(java.sql.Struct)

Example 2 with JDBCStructImpl

use of org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl in project dbeaver by dbeaver.

the class PostgreStructValueHandler method convertStringToStruct.

private JDBCCompositeStatic convertStringToStruct(@NotNull DBCSession session, @NotNull PostgreDataType compType, @NotNull String value) throws DBException {
    if (value.startsWith("(") && value.endsWith(")")) {
        value = value.substring(1, value.length() - 1);
    }
    final Collection<PostgreDataTypeAttribute> attributes = compType.getAttributes(session.getProgressMonitor());
    if (attributes == null) {
        throw new DBException("Composite type '" + compType.getTypeName() + "' has no attributes");
    }
    String[] parsedValues = PostgreValueParser.parseSingleObject(value);
    if (parsedValues.length != attributes.size()) {
        log.debug("Number of attributes (" + attributes.size() + ") doesn't match actual number of parsed strings (" + parsedValues.length + ")");
    }
    Object[] attrValues = new Object[attributes.size()];
    Iterator<PostgreDataTypeAttribute> attrIter = attributes.iterator();
    for (int i = 0; i < parsedValues.length && attrIter.hasNext(); i++) {
        final PostgreDataTypeAttribute itemAttr = attrIter.next();
        attrValues[i] = PostgreValueParser.convertStringToValue(session, itemAttr, parsedValues[i]);
    }
    Struct contents = new JDBCStructImpl(compType.getTypeName(), attrValues, value);
    return new JDBCCompositeStatic(session, compType, contents);
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCCompositeStatic(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeStatic) PostgreDataTypeAttribute(org.jkiss.dbeaver.ext.postgresql.model.PostgreDataTypeAttribute) JDBCStructImpl(org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) Struct(java.sql.Struct)

Example 3 with JDBCStructImpl

use of org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl in project dbeaver by dbeaver.

the class JDBCComposite method getStructValue.

public Struct getStructValue() throws DBCException {
    if (rawStruct != null) {
        return rawStruct;
    }
    Object[] attrs = new Object[values.length];
    for (int i = 0; i < values.length; i++) {
        Object attr = values[i];
        if (attr instanceof DBDValue) {
            attr = ((DBDValue) attr).getRawValue();
        }
        attrs[i] = attr;
    }
    final DBSDataType dataType = getDataType();
    try (DBCSession session = DBUtils.openUtilSession(new VoidProgressMonitor(), dataType, "Create JDBC struct")) {
        if (session instanceof Connection) {
            return ((Connection) session).createStruct(dataType.getTypeName(), attrs);
        } else {
            return new JDBCStructImpl(dataType.getTypeName(), attrs, getStringRepresentation());
        }
    } catch (Throwable e) {
        throw new DBCException("Error creating struct", e);
    }
}
Also used : DBDValue(org.jkiss.dbeaver.model.data.DBDValue) JDBCStructImpl(org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl) DBCException(org.jkiss.dbeaver.model.exec.DBCException) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DBCSession(org.jkiss.dbeaver.model.exec.DBCSession)

Example 4 with JDBCStructImpl

use of org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl in project dbeaver by dbeaver.

the class JDBCStructValueHandler method getValueFromObject.

@Override
public Object getValueFromObject(@NotNull DBCSession session, @NotNull DBSTypedObject type, Object object, boolean copy, boolean validateValue) throws DBCException {
    if (object instanceof JDBCComposite) {
        return copy ? ((JDBCComposite) object).cloneValue(session.getProgressMonitor()) : object;
    }
    String typeName;
    try {
        if (object instanceof Struct) {
            typeName = ((Struct) object).getSQLTypeName();
        } else {
            typeName = type.getTypeName();
        }
    } catch (SQLException e) {
        throw new DBCException(e, session.getExecutionContext());
    }
    DBSDataType dataType = null;
    try {
        dataType = DBUtils.resolveDataType(session.getProgressMonitor(), session.getDataSource(), typeName);
    } catch (DBException e) {
        log.debug("Error resolving data type '" + typeName + "'", e);
    }
    if (dataType == null) {
        if (object instanceof Struct) {
            return new JDBCCompositeDynamic(session, (Struct) object, null);
        } else {
            return new JDBCCompositeUnknown(session, object);
        }
    }
    if (object == null) {
        return new JDBCCompositeStatic(session, dataType, new JDBCStructImpl(dataType.getTypeName(), null, ""));
    } else if (object instanceof Struct) {
        return new JDBCCompositeStatic(session, dataType, (Struct) object);
    } else {
        return new JDBCCompositeUnknown(session, object);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCCompositeStatic(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeStatic) SQLException(java.sql.SQLException) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) JDBCCompositeDynamic(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeDynamic) JDBCStructImpl(org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl) JDBCCompositeUnknown(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeUnknown) DBCException(org.jkiss.dbeaver.model.exec.DBCException) JDBCComposite(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCComposite) Struct(java.sql.Struct)

Example 5 with JDBCStructImpl

use of org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl in project dbeaver by serge-rider.

the class JDBCComposite method getStructValue.

public Struct getStructValue() throws DBCException {
    if (rawStruct != null) {
        return rawStruct;
    }
    Object[] attrs = new Object[values.length];
    for (int i = 0; i < values.length; i++) {
        Object attr = values[i];
        if (attr instanceof DBDValue) {
            attr = ((DBDValue) attr).getRawValue();
        }
        attrs[i] = attr;
    }
    final DBSDataType dataType = getDataType();
    try (DBCSession session = DBUtils.openUtilSession(new VoidProgressMonitor(), dataType, "Create JDBC struct")) {
        if (session instanceof Connection) {
            return ((Connection) session).createStruct(dataType.getTypeName(), attrs);
        } else {
            return new JDBCStructImpl(dataType.getTypeName(), attrs, getStringRepresentation());
        }
    } catch (Throwable e) {
        throw new DBCException("Error creating struct", e);
    }
}
Also used : DBDValue(org.jkiss.dbeaver.model.data.DBDValue) JDBCStructImpl(org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl) DBCException(org.jkiss.dbeaver.model.exec.DBCException) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DBCSession(org.jkiss.dbeaver.model.exec.DBCSession)

Aggregations

JDBCStructImpl (org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl)6 Struct (java.sql.Struct)4 DBException (org.jkiss.dbeaver.DBException)4 DBCException (org.jkiss.dbeaver.model.exec.DBCException)4 JDBCCompositeStatic (org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeStatic)4 SQLException (java.sql.SQLException)2 PostgreDataTypeAttribute (org.jkiss.dbeaver.ext.postgresql.model.PostgreDataTypeAttribute)2 DBDValue (org.jkiss.dbeaver.model.data.DBDValue)2 DBCSession (org.jkiss.dbeaver.model.exec.DBCSession)2 JDBCComposite (org.jkiss.dbeaver.model.impl.jdbc.data.JDBCComposite)2 JDBCCompositeDynamic (org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeDynamic)2 JDBCCompositeUnknown (org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeUnknown)2 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)2 DBSDataType (org.jkiss.dbeaver.model.struct.DBSDataType)2 DBSTypedObject (org.jkiss.dbeaver.model.struct.DBSTypedObject)2