Search in sources :

Example 56 with DBCException

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

the class JDBCContentBytes method updateContents.

@Override
public boolean updateContents(DBRProgressMonitor monitor, DBDContentStorage storage) throws DBException {
    if (storage == null) {
        data = null;
    } else {
        try {
            InputStream is = storage.getContentStream();
            try {
                data = new byte[(int) storage.getContentLength()];
                int count = is.read(data);
                if (count != data.length) {
                    //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    log.warn("Actual content length (" + count + ") is less than declared (" + data.length + ")");
                }
            } finally {
                ContentUtils.close(is);
            }
        } catch (IOException e) {
            throw new DBCException("IO error while reading content", e);
        }
    }
    this.modified = true;
    return false;
}
Also used : DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 57 with DBCException

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

the class JDBCContentCLOB method getContents.

@Override
public DBDContentStorage getContents(DBRProgressMonitor monitor) throws DBCException {
    if (storage == null && clob != null) {
        long contentLength = getContentLength();
        DBPPlatform platform = dataSource.getContainer().getPlatform();
        if (contentLength < platform.getPreferenceStore().getInt(ModelPreferences.MEMORY_CONTENT_MAX_SIZE)) {
            try {
                storage = StringContentStorage.createFromReader(clob.getCharacterStream(), contentLength);
            } 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, "clob" + clob.hashCode());
            } catch (IOException e) {
                throw new DBCException("Can't create temp file", e);
            }
            try (Writer os = new OutputStreamWriter(new FileOutputStream(tempFile), getDefaultEncoding())) {
                ContentUtils.copyStreams(clob.getCharacterStream(), contentLength, os, monitor);
            } catch (IOException e) {
                ContentUtils.deleteTempFile(tempFile);
                throw new DBCException("IO error while copying content", e);
            } catch (Throwable e) {
                ContentUtils.deleteTempFile(tempFile);
                throw new DBCException(e, dataSource);
            }
            this.storage = new TemporaryContentStorage(platform, tempFile, getDefaultEncoding());
        }
        // Free lob - we don't need it anymore
        releaseClob();
    }
    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 58 with DBCException

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

the class JDBCContentCLOB method bindParameter.

@Override
public void bindParameter(JDBCSession session, JDBCPreparedStatement preparedStatement, DBSTypedObject columnType, int paramIndex) throws DBCException {
    try {
        if (storage != null) {
            //                String stringValue = ContentUtils.getContentStringValue(session.getProgressMonitor(), this);
            //                preparedStatement.setString(paramIndex, stringValue);
            // Try 3 jdbc methods to set character stream
            releaseTempStream();
            tmpReader = storage.getContentReader();
            try {
                preparedStatement.setNCharacterStream(paramIndex, tmpReader);
            } catch (Throwable e) {
                if (e instanceof SQLException && !(e instanceof SQLFeatureNotSupportedException)) {
                    throw (SQLException) e;
                } else {
                    long streamLength = ContentUtils.calculateContentLength(storage.getContentReader());
                    try {
                        preparedStatement.setCharacterStream(paramIndex, tmpReader, streamLength);
                    } catch (Throwable e1) {
                        if (e1 instanceof SQLException && !(e instanceof SQLFeatureNotSupportedException)) {
                            throw (SQLException) e1;
                        } else {
                            preparedStatement.setCharacterStream(paramIndex, tmpReader, (int) streamLength);
                        }
                    }
                }
            }
        } else if (clob != null) {
            preparedStatement.setClob(paramIndex, clob);
        } else {
            preparedStatement.setNull(paramIndex, java.sql.Types.CLOB);
        }
    } catch (SQLException e) {
        throw new DBCException(e, dataSource);
    } catch (Throwable e) {
        throw new DBCException("IO error while binding content", e);
    }
}
Also used : SQLException(java.sql.SQLException) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 59 with DBCException

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

the class JDBCContentChars method updateContents.

@Override
public boolean updateContents(DBRProgressMonitor monitor, DBDContentStorage storage) throws DBException {
    if (storage == null) {
        data = null;
    } else {
        try {
            Reader reader = storage.getContentReader();
            try {
                StringWriter sw = new StringWriter((int) storage.getContentLength());
                ContentUtils.copyStreams(reader, storage.getContentLength(), sw, monitor);
                data = sw.toString();
            } finally {
                ContentUtils.close(reader);
            }
        } catch (IOException e) {
            throw new DBCException("IO error while reading content", e);
        }
    }
    this.modified = true;
    return false;
}
Also used : DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 60 with DBCException

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

the class JDBCContentLOB method cloneValue.

@Override
public DBDValueCloneable cloneValue(DBRProgressMonitor monitor) throws DBCException {
    JDBCContentLOB copy = createNewContent();
    DBDContentStorage storage = getContents(monitor);
    if (storage != null) {
        try {
            copy.updateContents(monitor, storage.cloneStorage(monitor));
        } catch (IOException e) {
            throw new DBCException("IO error while clone content", e);
        }
    }
    return copy;
}
Also used : DBCException(org.jkiss.dbeaver.model.exec.DBCException) IOException(java.io.IOException) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage)

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