Search in sources :

Example 21 with DBDContent

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

the class ContentEditorInput method loadFromExternalFile.

void loadFromExternalFile(File extFile, IProgressMonitor monitor) throws CoreException {
    try {
        release();
        contentFile = extFile;
        contentDetached = true;
        Object value = getValue();
        if (value instanceof DBDContent) {
            ((DBDContent) value).updateContents(new DefaultProgressMonitor(monitor), new ExternalContentStorage(DBWorkbench.getPlatform(), extFile));
        } else {
            updateStringValueFromFile(extFile);
        }
        refreshContentParts(extFile);
    } catch (Throwable e) {
        throw new CoreException(GeneralUtils.makeExceptionStatus(e));
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) DBDContent(org.jkiss.dbeaver.model.data.DBDContent) ExternalContentStorage(org.jkiss.dbeaver.model.data.storage.ExternalContentStorage) DefaultProgressMonitor(org.jkiss.dbeaver.model.runtime.DefaultProgressMonitor)

Example 22 with DBDContent

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

the class AbstractTextPanelEditor method extractEditorValue.

@Override
public void extractEditorValue(@NotNull DBRProgressMonitor monitor, @NotNull StyledText control, @NotNull DBDContent value) throws DBException {
    if (valueController.getValue() instanceof DBDContent) {
        monitor.beginTask("Extract text", 1);
        try {
            monitor.subTask("Extracting text from editor");
            editor.doSave(RuntimeUtils.getNestedMonitor(monitor));
            final IEditorInput editorInput = editor.getEditorInput();
            if (editorInput instanceof ContentEditorInput) {
                final ContentEditorInput contentEditorInput = (ContentEditorInput) editorInput;
                contentEditorInput.updateContentFromFile(monitor, value);
            }
        } catch (Exception e) {
            throw new DBException("Error extracting text from editor", e);
        } finally {
            monitor.done();
        }
    } else {
        value.updateContents(monitor, new StringContentStorage(control.getText()));
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBDContent(org.jkiss.dbeaver.model.data.DBDContent) ContentEditorInput(org.jkiss.dbeaver.ui.editors.content.ContentEditorInput) StringContentStorage(org.jkiss.dbeaver.model.data.storage.StringContentStorage) IEditorInput(org.eclipse.ui.IEditorInput) PartInitException(org.eclipse.ui.PartInitException) DBException(org.jkiss.dbeaver.DBException)

Example 23 with DBDContent

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

the class ContentPanelEditor method extractEditorValue.

@Override
public Object extractEditorValue() throws DBException {
    final Object content = valueController.getValue();
    if (isStringValue()) {
        StringContent stringContent = new StringContent(valueController.getExecutionContext(), null);
        streamEditor.extractEditorValue(new VoidProgressMonitor(), control, stringContent);
        return stringContent.getRawValue();
    } else {
        if (content == null) {
            log.warn("NULL content value. Must be DBDContent.");
        } else if (streamEditor == null) {
            log.warn("NULL content editor.");
        } else {
            try {
                streamEditor.extractEditorValue(new VoidProgressMonitor(), control, (DBDContent) content);
            } catch (Throwable e) {
                log.debug(e);
                valueController.showMessage(e.getMessage(), DBPMessageType.ERROR);
            }
        }
        return content;
    }
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) StringContent(org.jkiss.dbeaver.model.impl.data.StringContent) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)

Example 24 with DBDContent

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

the class TextViewDialog method extractEditorValue.

@Override
public Object extractEditorValue() {
    Object prevValue = getValueController().getValue();
    Object rawValue;
    if (prevValue instanceof DBDContent) {
        if (ContentUtils.isTextContent((DBDContent) prevValue)) {
            rawValue = isTextEditorActive() ? textEdit.getText() : getBinaryString();
        } else {
            rawValue = isTextEditorActive() ? GeneralUtils.convertToBytes(textEdit.getText()) : getBinaryContent();
        }
    } else {
        if (isTextEditorActive()) {
            rawValue = textEdit.getText();
        } else {
            rawValue = getBinaryString();
        }
    }
    try (DBCSession session = getValueController().getExecutionContext().openSession(new VoidProgressMonitor(), DBCExecutionPurpose.UTIL, "Make text value from editor")) {
        return getValueController().getValueHandler().getValueFromObject(session, getValueController().getValueType(), rawValue, false, false);
    } catch (Exception e) {
        DBWorkbench.getPlatformUI().showError("Extract editor value", "Can't extract editor value", e);
        return null;
    }
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DBCSession(org.jkiss.dbeaver.model.exec.DBCSession)

Example 25 with DBDContent

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

the class DataExporterDbUnit method exportRow.

@Override
public void exportRow(DBCSession session, DBCResultSet resultSet, Object[] row) throws DBException, IOException {
    PrintWriter out = getWriter();
    out.write("    <" + tableName);
    for (int i = 0; i < row.length; i++) {
        if (DBUtils.isNullValue(row[i]) && !includeNullValues) {
            continue;
        }
        DBDAttributeBinding column = columns[i];
        String columnName = escapeXmlElementName(column.getName());
        if (columnName != null && upperCaseColumnNames) {
            columnName = columnName.toUpperCase();
        }
        out.write(" " + columnName + "=\"");
        Object columnValue = row[i];
        if (DBUtils.isNullValue(columnValue)) {
            writeTextCell("" + getSite().getProperties().get(PROP_NULL_VALUE_STRING));
        } else if (columnValue instanceof Float || columnValue instanceof Double || columnValue instanceof BigDecimal) {
            int scale = column.getMetaAttribute().getScale() != null && column.getMetaAttribute().getScale() > 0 ? column.getMetaAttribute().getScale() : 1;
            try {
                out.write(String.format(Locale.ROOT, "%." + scale + "f", columnValue));
            } catch (Exception e) {
                out.write(columnValue.toString());
            }
        } else if (columnValue instanceof Boolean) {
            out.write(columnValue.toString());
        } else if (columnValue instanceof Number) {
            out.write(columnValue.toString());
        } else if (columnValue instanceof Timestamp) {
            try {
                int nanoseconds = ((Timestamp) columnValue).getNanos();
                out.write(String.format(Locale.ROOT, "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%2$d", columnValue, nanoseconds));
            } catch (Exception e) {
                out.write(columnValue.toString());
            }
        } else if (columnValue instanceof Time) {
            try {
                out.write(String.format(Locale.ROOT, "%1$tH:%1$tM:%1$tS", columnValue));
            } catch (Exception e) {
                out.write(columnValue.toString());
            }
        } else if (columnValue instanceof Date) {
            try {
                out.write(String.format(Locale.ROOT, "%1$tY-%1$tm-%1$td", columnValue));
            } catch (Exception e) {
                out.write(columnValue.toString());
            }
        } else if (columnValue instanceof DBDContent) {
            // Content
            // Inline textual content and handle binaries in some special way
            DBDContent content = (DBDContent) columnValue;
            try {
                DBDContentStorage cs = content.getContents(session.getProgressMonitor());
                if (cs != null) {
                    if (ContentUtils.isTextContent(content)) {
                        try (Reader reader = cs.getContentReader()) {
                            writeCellValue(reader);
                        }
                    } else {
                        try (final InputStream stream = cs.getContentStream()) {
                            Base64.encode(stream, cs.getContentLength(), getSite().getWriter());
                        }
                    }
                }
            } finally {
                content.release();
            }
        } else {
            writeTextCell(super.getValueDisplayString(column, columnValue));
        }
        out.write("\"");
    }
    out.write("/>" + CommonUtils.getLineSeparator());
}
Also used : InputStream(java.io.InputStream) Reader(java.io.Reader) Time(java.sql.Time) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) IOException(java.io.IOException) DBException(org.jkiss.dbeaver.DBException) Date(java.sql.Date) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBPNamedObject(org.jkiss.dbeaver.model.DBPNamedObject) PrintWriter(java.io.PrintWriter)

Aggregations

DBDContent (org.jkiss.dbeaver.model.data.DBDContent)52 DBDContentStorage (org.jkiss.dbeaver.model.data.DBDContentStorage)33 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)23 Reader (java.io.Reader)15 DBException (org.jkiss.dbeaver.DBException)13 PrintWriter (java.io.PrintWriter)10 File (java.io.File)9 Date (java.util.Date)8 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)8 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 DBDContentStorageLocal (org.jkiss.dbeaver.model.data.DBDContentStorageLocal)6 DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)6 DBSTypedObject (org.jkiss.dbeaver.model.struct.DBSTypedObject)6 IOException (java.io.IOException)5 InputStream (java.io.InputStream)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 DBDContentCached (org.jkiss.dbeaver.model.data.DBDContentCached)4 StringContentStorage (org.jkiss.dbeaver.model.data.storage.StringContentStorage)4 DBCException (org.jkiss.dbeaver.model.exec.DBCException)4