Search in sources :

Example 11 with DBDContentStorage

use of org.jkiss.dbeaver.model.data.DBDContentStorage in project dbeaver by serge-rider.

the class ContentEditorInput method prepareContent.

private void prepareContent(DBRProgressMonitor monitor) throws DBException {
    DBDContent content = getContent();
    DBDContentStorage storage = content.getContents(monitor);
    if (contentDetached) {
        release(monitor);
        contentDetached = false;
    }
    if (storage instanceof DBDContentStorageLocal) {
        // User content's storage directly
        contentFile = ((DBDContentStorageLocal) storage).getDataFile();
        contentDetached = true;
    } else {
        // Copy content to local file
        try {
            // Create file
            if (contentFile == null) {
                String valueId;
                if (valueController instanceof IAttributeController) {
                    valueId = ((IAttributeController) valueController).getColumnId();
                } else {
                    valueId = valueController.getValueName();
                }
                contentFile = ContentUtils.createTempContentFile(monitor, DBeaverCore.getInstance(), valueId);
            }
            // Write value to file
            copyContentToFile(content, monitor);
        } catch (IOException e) {
            // Delete temp file
            if (contentFile != null && contentFile.exists()) {
                if (!contentFile.delete()) {
                    log.warn("Can't delete temporary content file '" + contentFile.getAbsolutePath() + "'");
                }
            }
            throw new DBException("Can't delete content file", e);
        }
    }
    // Mark file as readonly
    if (valueController.isReadOnly()) {
        markReadOnly(true);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBDContentStorageLocal(org.jkiss.dbeaver.model.data.DBDContentStorageLocal) IAttributeController(org.jkiss.dbeaver.ui.data.IAttributeController) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage)

Example 12 with DBDContentStorage

use of org.jkiss.dbeaver.model.data.DBDContentStorage in project dbeaver by serge-rider.

the class DialogUtils method saveToFile.

public static void saveToFile(IValueController controller) {
    if (!(controller.getValue() instanceof DBDContent)) {
        log.error(CoreMessages.model_jdbc_bad_content_value_ + controller.getValue());
        return;
    }
    Shell shell = UIUtils.getShell(controller.getValueSite());
    final File saveFile = selectFileForSave(shell, controller.getValueName());
    if (saveFile == null) {
        return;
    }
    final DBDContent value = (DBDContent) controller.getValue();
    try {
        DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    DBDContentStorage storage = value.getContents(monitor);
                    if (ContentUtils.isTextContent(value)) {
                        try (Reader cr = storage.getContentReader()) {
                            ContentUtils.saveContentToFile(cr, saveFile, GeneralUtils.UTF8_ENCODING, monitor);
                        }
                    } else {
                        try (InputStream cs = storage.getContentStream()) {
                            ContentUtils.saveContentToFile(cs, saveFile, monitor);
                        }
                    }
                } catch (Exception e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InvocationTargetException e) {
        UIUtils.showErrorDialog(shell, CoreMessages.model_jdbc_could_not_save_content, //$NON-NLS-2$
        CoreMessages.model_jdbc_could_not_save_content_to_file_ + saveFile.getAbsolutePath() + "'", e.getTargetException());
    } catch (InterruptedException e) {
    // do nothing
    }
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) InputStream(java.io.InputStream) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) Reader(java.io.Reader) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 13 with DBDContentStorage

use of org.jkiss.dbeaver.model.data.DBDContentStorage in project dbeaver by serge-rider.

the class ContentUtils method getContentStringValue.

@NotNull
public static String getContentStringValue(@NotNull DBRProgressMonitor monitor, @NotNull DBDContent object) throws DBCException {
    DBDContentStorage data = object.getContents(monitor);
    if (data != null) {
        if (data instanceof DBDContentCached) {
            Object cachedValue = ((DBDContentCached) data).getCachedValue();
            if (cachedValue instanceof String) {
                return (String) cachedValue;
            }
        }
        try {
            Reader contentReader = data.getContentReader();
            if (contentReader != null) {
                try {
                    StringWriter buf = new StringWriter();
                    ContentUtils.copyStreams(contentReader, object.getContentLength(), buf, monitor);
                    return buf.toString();
                } finally {
                    IOUtils.close(contentReader);
                }
            }
        } catch (IOException e) {
            log.debug("Can't extract string from content", e);
        }
    }
    return object.toString();
}
Also used : DBDContentCached(org.jkiss.dbeaver.model.data.DBDContentCached) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) NotNull(org.jkiss.code.NotNull)

Example 14 with DBDContentStorage

use of org.jkiss.dbeaver.model.data.DBDContentStorage in project dbeaver by serge-rider.

the class ContentUtils method getContentBinaryValue.

@NotNull
public static byte[] getContentBinaryValue(@NotNull DBRProgressMonitor monitor, @NotNull DBDContent object) throws DBCException {
    DBDContentStorage data = object.getContents(monitor);
    if (data != null) {
        if (data instanceof DBDContentCached) {
            Object cachedValue = ((DBDContentCached) data).getCachedValue();
            if (cachedValue instanceof byte[]) {
                return (byte[]) cachedValue;
            }
        }
        try {
            InputStream contentStream = data.getContentStream();
            if (contentStream != null) {
                try {
                    ByteArrayOutputStream buf = new ByteArrayOutputStream();
                    ContentUtils.copyStreams(contentStream, object.getContentLength(), buf, monitor);
                    return buf.toByteArray();
                } finally {
                    IOUtils.close(contentStream);
                }
            }
        } catch (IOException e) {
            log.debug("Can't extract string from content", e);
        }
    }
    return null;
}
Also used : DBDContentCached(org.jkiss.dbeaver.model.data.DBDContentCached) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) NotNull(org.jkiss.code.NotNull)

Example 15 with DBDContentStorage

use of org.jkiss.dbeaver.model.data.DBDContentStorage 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

DBDContentStorage (org.jkiss.dbeaver.model.data.DBDContentStorage)15 DBDContent (org.jkiss.dbeaver.model.data.DBDContent)7 DBException (org.jkiss.dbeaver.DBException)4 File (java.io.File)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Reader (java.io.Reader)3 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)3 DBDContentCached (org.jkiss.dbeaver.model.data.DBDContentCached)3 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 NotNull (org.jkiss.code.NotNull)2 DBDContentStorageLocal (org.jkiss.dbeaver.model.data.DBDContentStorageLocal)2 DBCException (org.jkiss.dbeaver.model.exec.DBCException)2 DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Date (java.util.Date)1 ExternalContentStorage (org.jkiss.dbeaver.model.impl.ExternalContentStorage)1 StringContentStorage (org.jkiss.dbeaver.model.impl.StringContentStorage)1 TemporaryContentStorage (org.jkiss.dbeaver.model.impl.TemporaryContentStorage)1