Search in sources :

Example 6 with TemporaryContentStorage

use of org.jkiss.dbeaver.model.data.storage.TemporaryContentStorage in project dbeaver by dbeaver.

the class ContentEditorInput method updateContentFromFile.

public void updateContentFromFile(DBRProgressMonitor monitor, Object value) throws DBException {
    if (valueController.isReadOnly()) {
        throw new DBCException("Can't update read-only value");
    }
    if (value instanceof DBDContent) {
        DBDContent content = (DBDContent) value;
        DBDContentStorage storage = content.getContents(monitor);
        if (storage instanceof DBDContentStorageLocal) {
            // Nothing to update - we use content's storage
            content.updateContents(monitor, storage);
            contentDetached = true;
        } else if (storage instanceof DBDContentCached) {
            // Create new storage and pass it to content
            try (FileInputStream is = new FileInputStream(contentFile)) {
                if (ContentUtils.isTextContent(content)) {
                    try (Reader reader = new InputStreamReader(is, fileCharset)) {
                        storage = StringContentStorage.createFromReader(reader);
                    }
                } else {
                    storage = BytesContentStorage.createFromStream(is, contentFile.length(), fileCharset);
                }
                // StringContentStorage.
                contentDetached = content.updateContents(monitor, 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(DBWorkbench.getPlatform(), contentFile, fileCharset);
            contentDetached = content.updateContents(monitor, storage);
        }
    } else if (stringStorage != null) {
        // Just read as string
        valueController.updateValue(stringStorage.getString(), false);
        contentDetached = true;
    }
}
Also used : DBDContentCached(org.jkiss.dbeaver.model.data.DBDContentCached) DBException(org.jkiss.dbeaver.DBException) DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBCException(org.jkiss.dbeaver.model.exec.DBCException) TemporaryContentStorage(org.jkiss.dbeaver.model.data.storage.TemporaryContentStorage) DBDContentStorageLocal(org.jkiss.dbeaver.model.data.DBDContentStorageLocal) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage)

Example 7 with TemporaryContentStorage

use of org.jkiss.dbeaver.model.data.storage.TemporaryContentStorage in project dbeaver by dbeaver.

the class JDBCContentCLOB method getContents.

@Override
public DBDContentStorage getContents(DBRProgressMonitor monitor) throws DBCException {
    if (storage == null && clob != null) {
        long contentLength = getContentLength();
        DBPPlatform platform = executionContext.getDataSource().getContainer().getPlatform();
        if (contentLength < platform.getPreferenceStore().getInt(ModelPreferences.MEMORY_CONTENT_MAX_SIZE)) {
            try {
                String subString = clob.getSubString(1, (int) contentLength);
                storage = new JDBCContentChars(executionContext, subString);
            } catch (Exception e) {
                log.debug("Can't get CLOB as substring", e);
                try {
                    storage = StringContentStorage.createFromReader(clob.getCharacterStream(), contentLength);
                } catch (IOException e1) {
                    throw new DBCException("IO error while reading content", e);
                } catch (Throwable e1) {
                    throw new DBCException(e, executionContext);
                }
            }
        } 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, executionContext);
            }
            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.data.storage.TemporaryContentStorage) DBPPlatform(org.jkiss.dbeaver.model.app.DBPPlatform) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) DBCException(org.jkiss.dbeaver.model.exec.DBCException) SQLException(java.sql.SQLException)

Example 8 with TemporaryContentStorage

use of org.jkiss.dbeaver.model.data.storage.TemporaryContentStorage in project dbeaver by dbeaver.

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 = executionContext.getDataSource().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, executionContext);
                }
                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.data.storage.TemporaryContentStorage) DBPPlatform(org.jkiss.dbeaver.model.app.DBPPlatform)

Aggregations

TemporaryContentStorage (org.jkiss.dbeaver.model.data.storage.TemporaryContentStorage)8 DBCException (org.jkiss.dbeaver.model.exec.DBCException)8 DBPPlatform (org.jkiss.dbeaver.model.app.DBPPlatform)6 SQLException (java.sql.SQLException)2 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)2 DBException (org.jkiss.dbeaver.DBException)2 DBDContent (org.jkiss.dbeaver.model.data.DBDContent)2 DBDContentCached (org.jkiss.dbeaver.model.data.DBDContentCached)2 DBDContentStorage (org.jkiss.dbeaver.model.data.DBDContentStorage)2 DBDContentStorageLocal (org.jkiss.dbeaver.model.data.DBDContentStorageLocal)2