Search in sources :

Example 1 with TemporaryContentStorage

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

the class OracleContentBFILE method getContents.

@Override
public DBDContentStorage getContents(DBRProgressMonitor monitor) throws DBCException {
    if (storage == null && bfile != null) {
        try {
            openFile();
            long contentLength = getContentLength();
            DBPPlatform platform = dataSource.getContainer().getPlatform();
            if (contentLength < platform.getPreferenceStore().getInt(ModelPreferences.MEMORY_CONTENT_MAX_SIZE)) {
                try {
                    try (InputStream bs = getInputStream()) {
                        storage = BytesContentStorage.createFromStream(bs, contentLength, getDefaultEncoding());
                    }
                } catch (IOException e) {
                    throw new DBCException("IO error while reading content", e);
                }
            } else {
                // Create new local storage
                File tempFile;
                try {
                    tempFile = ContentUtils.createTempContentFile(monitor, platform, "blob" + bfile.hashCode());
                } catch (IOException e) {
                    throw new DBCException("Can't create temporary file", e);
                }
                try (OutputStream os = new FileOutputStream(tempFile)) {
                    try (InputStream bs = getInputStream()) {
                        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();
        } finally {
            closeFile();
        }
    }
    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 2 with TemporaryContentStorage

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

the class ContentEditorInput method updateContentFromFile.

public void updateContentFromFile(IProgressMonitor monitor) throws DBException {
    if (valueController.isReadOnly()) {
        throw new DBCException("Can't update read-only value");
    }
    DBRProgressMonitor localMonitor = RuntimeUtils.makeMonitor(monitor);
    DBDContent content = getContent();
    DBDContentStorage storage = content.getContents(localMonitor);
    if (storage instanceof DBDContentStorageLocal) {
        // Nothing to update - we user content's storage
        contentDetached = true;
    } else if (storage instanceof DBDContentCached) {
        // Create new storage and pass it to content
        try (FileInputStream is = new FileInputStream(contentFile)) {
            if (storage instanceof StringContentStorage) {
                try (Reader reader = new InputStreamReader(is, fileCharset)) {
                    storage = StringContentStorage.createFromReader(reader);
                }
            } else {
                storage = BytesContentStorage.createFromStream(is, contentFile.length(), fileCharset);
            }
            //StringContentStorage.
            contentDetached = content.updateContents(localMonitor, storage);
        } catch (IOException e) {
            throw new DBException("Error reading content from file", e);
        }
    } else {
        // Create new storage and pass it to content
        storage = new TemporaryContentStorage(DBeaverCore.getInstance(), contentFile, fileCharset);
        contentDetached = content.updateContents(localMonitor, storage);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBCException(org.jkiss.dbeaver.model.exec.DBCException) TemporaryContentStorage(org.jkiss.dbeaver.model.impl.TemporaryContentStorage) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) DBDContentCached(org.jkiss.dbeaver.model.data.DBDContentCached) DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBDContentStorageLocal(org.jkiss.dbeaver.model.data.DBDContentStorageLocal) StringContentStorage(org.jkiss.dbeaver.model.impl.StringContentStorage)

Example 3 with TemporaryContentStorage

use of org.jkiss.dbeaver.model.impl.TemporaryContentStorage 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 4 with TemporaryContentStorage

use of org.jkiss.dbeaver.model.impl.TemporaryContentStorage 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)

Aggregations

DBCException (org.jkiss.dbeaver.model.exec.DBCException)4 TemporaryContentStorage (org.jkiss.dbeaver.model.impl.TemporaryContentStorage)4 DBPPlatform (org.jkiss.dbeaver.model.app.DBPPlatform)3 DBException (org.jkiss.dbeaver.DBException)1 DBDContent (org.jkiss.dbeaver.model.data.DBDContent)1 DBDContentCached (org.jkiss.dbeaver.model.data.DBDContentCached)1 DBDContentStorage (org.jkiss.dbeaver.model.data.DBDContentStorage)1 DBDContentStorageLocal (org.jkiss.dbeaver.model.data.DBDContentStorageLocal)1 StringContentStorage (org.jkiss.dbeaver.model.impl.StringContentStorage)1 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)1