Search in sources :

Example 51 with DBCException

use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.

the class JDBCReferenceValueHandler method getValueFromObject.

@Override
public JDBCReference getValueFromObject(@NotNull DBCSession session, @NotNull DBSTypedObject type, Object object, boolean copy) throws DBCException {
    String typeName;
    try {
        if (object instanceof Ref) {
            typeName = ((Ref) object).getBaseTypeName();
        } else {
            typeName = type.getTypeName();
        }
    } catch (SQLException e) {
        throw new DBCException(e, session.getDataSource());
    }
    DBSDataType dataType = null;
    try {
        dataType = DBUtils.resolveDataType(session.getProgressMonitor(), session.getDataSource(), typeName);
    } catch (DBException e) {
        log.error("Error resolving data type '" + typeName + "'", e);
    }
    if (dataType == null) {
        dataType = new JDBCDataType(session.getDataSource().getContainer(), Types.REF, typeName, "Synthetic struct type for reference '" + typeName + "'", false, false, 0, 0, 0);
    }
    if (object == null) {
        return new JDBCReference(dataType, null);
    } else if (object instanceof JDBCReference) {
        return (JDBCReference) object;
    } else if (object instanceof Ref) {
        return new JDBCReference(dataType, (Ref) object);
    } else {
        throw new DBCException("Unsupported struct type: " + object.getClass().getName());
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCDataType(org.jkiss.dbeaver.model.impl.jdbc.struct.JDBCDataType) Ref(java.sql.Ref) SQLException(java.sql.SQLException) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) DBCException(org.jkiss.dbeaver.model.exec.DBCException) JDBCReference(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCReference)

Example 52 with DBCException

use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.

the class JDBCStructValueHandler method getValueFromObject.

@Override
public Object getValueFromObject(@NotNull DBCSession session, @NotNull DBSTypedObject type, Object object, boolean copy) throws DBCException {
    String typeName;
    try {
        if (object instanceof Struct) {
            typeName = ((Struct) object).getSQLTypeName();
        } else {
            typeName = type.getTypeName();
        }
    } catch (SQLException e) {
        throw new DBCException(e, session.getDataSource());
    }
    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 JDBCCompositeStatic) {
        return copy ? ((JDBCCompositeStatic) object).cloneValue(session.getProgressMonitor()) : object;
    } 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) Struct(java.sql.Struct)

Example 53 with DBCException

use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.

the class JDBCComposite method getStructValue.

public Struct getStructValue() throws DBCException {
    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(VoidProgressMonitor.INSTANCE, dataType.getDataSource(), "Create JDBC struct")) {
        if (session instanceof Connection) {
            return ((Connection) session).createStruct(dataType.getTypeName(), attrs);
        } else {
            return new JDBCStructImpl(dataType.getTypeName(), attrs);
        }
    } 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) DBCSession(org.jkiss.dbeaver.model.exec.DBCSession)

Example 54 with DBCException

use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.

the class JDBCContentBLOB method getContents.

@Override
public DBDContentStorage getContents(DBRProgressMonitor monitor) throws DBCException {
    if (storage == null && blob != null) {
        long contentLength = getContentLength();
        DBPPlatform platform = dataSource.getContainer().getPlatform();
        if (contentLength < platform.getPreferenceStore().getInt(ModelPreferences.MEMORY_CONTENT_MAX_SIZE)) {
            try {
                try (InputStream bs = blob.getBinaryStream()) {
                    storage = BytesContentStorage.createFromStream(bs, contentLength, getDefaultEncoding());
                }
            } catch (IOException e) {
                throw new DBCException("IO error while reading content", e);
            } catch (Throwable e) {
                throw new DBCException(e, dataSource);
            }
        } else {
            // Create new local storage
            File tempFile;
            try {
                tempFile = ContentUtils.createTempContentFile(monitor, platform, "blob" + blob.hashCode());
            } catch (IOException e) {
                throw new DBCException("Can't create temporary file", e);
            }
            try (OutputStream os = new FileOutputStream(tempFile)) {
                try (InputStream bs = blob.getBinaryStream()) {
                    ContentUtils.copyStreams(bs, contentLength, os, monitor);
                }
            } catch (IOException e) {
                ContentUtils.deleteTempFile(tempFile);
                throw new DBCException("IO error while copying stream", e);
            } catch (Throwable e) {
                ContentUtils.deleteTempFile(tempFile);
                throw new DBCException(e, dataSource);
            }
            this.storage = new TemporaryContentStorage(platform, tempFile, getDefaultEncoding());
        }
        // Free blob - we don't need it anymore
        releaseBlob();
    }
    return storage;
}
Also used : DBCException(org.jkiss.dbeaver.model.exec.DBCException) TemporaryContentStorage(org.jkiss.dbeaver.model.impl.TemporaryContentStorage) DBPPlatform(org.jkiss.dbeaver.model.app.DBPPlatform)

Example 55 with DBCException

use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.

the class JDBCContentBLOB method bindParameter.

@Override
public void bindParameter(JDBCSession session, JDBCPreparedStatement preparedStatement, DBSTypedObject columnType, int paramIndex) throws DBCException {
    try {
        if (storage != null) {
            // Write new blob value
            releaseTempStream();
            tmpStream = storage.getContentStream();
            try {
                preparedStatement.setBinaryStream(paramIndex, tmpStream);
            } catch (Throwable e) {
                try {
                    if (e instanceof SQLException) {
                        throw (SQLException) e;
                    } else {
                        try {
                            preparedStatement.setBinaryStream(paramIndex, tmpStream, storage.getContentLength());
                        } catch (Throwable e1) {
                            if (e1 instanceof SQLException) {
                                throw (SQLException) e1;
                            } else {
                                preparedStatement.setBinaryStream(paramIndex, tmpStream, (int) storage.getContentLength());
                            }
                        }
                    }
                } catch (SQLFeatureNotSupportedException e1) {
                    // Stream values seems to be unsupported
                    // Let's try bytes
                    int contentLength = (int) storage.getContentLength();
                    ByteArrayOutputStream buffer = new ByteArrayOutputStream(contentLength);
                    ContentUtils.copyStreams(tmpStream, contentLength, buffer, session.getProgressMonitor());
                    preparedStatement.setBytes(paramIndex, buffer.toByteArray());
                }
            }
        } else if (blob != null) {
            try {
                preparedStatement.setBlob(paramIndex, blob);
            } catch (Throwable e0) {
                // Write new blob value
                releaseTempStream();
                tmpStream = blob.getBinaryStream();
                try {
                    preparedStatement.setBinaryStream(paramIndex, tmpStream);
                } catch (Throwable e) {
                    if (e instanceof SQLException) {
                        throw (SQLException) e;
                    } else {
                        try {
                            preparedStatement.setBinaryStream(paramIndex, tmpStream, blob.length());
                        } catch (Throwable e1) {
                            if (e1 instanceof SQLException) {
                                throw (SQLException) e1;
                            } else {
                                preparedStatement.setBinaryStream(paramIndex, tmpStream, (int) blob.length());
                            }
                        }
                    }
                }
            }
        } else {
            preparedStatement.setNull(paramIndex, java.sql.Types.BLOB);
        }
    } catch (SQLException e) {
        throw new DBCException(e, session.getDataSource());
    } catch (Throwable e) {
        throw new DBCException("Error while reading content", e);
    }
}
Also used : SQLException(java.sql.SQLException) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Aggregations

DBCException (org.jkiss.dbeaver.model.exec.DBCException)60 SQLException (java.sql.SQLException)28 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)16 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)14 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)13 DBException (org.jkiss.dbeaver.DBException)11 NotNull (org.jkiss.code.NotNull)9 DBCStatement (org.jkiss.dbeaver.model.exec.DBCStatement)5 DBSDataType (org.jkiss.dbeaver.model.struct.DBSDataType)5 DBCResultSet (org.jkiss.dbeaver.model.exec.DBCResultSet)4 DBSTypedObject (org.jkiss.dbeaver.model.struct.DBSTypedObject)4 IOException (java.io.IOException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Tree (org.eclipse.swt.widgets.Tree)3 TreeColumn (org.eclipse.swt.widgets.TreeColumn)3 TreeItem (org.eclipse.swt.widgets.TreeItem)3 Nullable (org.jkiss.code.Nullable)3 DBPPlatform (org.jkiss.dbeaver.model.app.DBPPlatform)3 TemporaryContentStorage (org.jkiss.dbeaver.model.impl.TemporaryContentStorage)3 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)3