Search in sources :

Example 36 with DBDContent

use of org.jkiss.dbeaver.model.data.DBDContent 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 37 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(VoidProgressMonitor.INSTANCE, DBCExecutionPurpose.UTIL, "Make text value from editor")) {
        return getValueController().getValueHandler().getValueFromObject(session, getValueController().getValueType(), rawValue, false);
    } catch (Exception e) {
        UIUtils.showErrorDialog(getShell(), "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) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DBCSession(org.jkiss.dbeaver.model.exec.DBCSession)

Example 38 with DBDContent

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

the class DataExporterJSON method exportRow.

@Override
public void exportRow(DBCSession session, Object[] row) throws DBException, IOException {
    if (rowNum > 0) {
        out.write(",\n");
    }
    rowNum++;
    out.write("\t{\n");
    for (int i = 0; i < row.length; i++) {
        DBDAttributeBinding column = columns.get(i);
        String columnName = column.getLabel();
        if (CommonUtils.isEmpty(columnName)) {
            columnName = column.getName();
        }
        out.write("\t\t\"" + JSONUtils.escapeJsonString(columnName) + "\" : ");
        Object cellValue = row[i];
        if (DBUtils.isNullValue(cellValue)) {
            writeTextCell(null);
        } else if (cellValue instanceof DBDContent) {
            // Content
            // Inline textual content and handle binaries in some special way
            DBDContent content = (DBDContent) cellValue;
            try {
                DBDContentStorage cs = content.getContents(session.getProgressMonitor());
                if (cs != null) {
                    if (ContentUtils.isTextContent(content)) {
                        try (Reader in = cs.getContentReader()) {
                            out.write("\"");
                            writeCellValue(in);
                            out.write("\"");
                        }
                    } else {
                        getSite().writeBinaryData(cs);
                    }
                }
            } finally {
                content.release();
            }
        } else {
            if (cellValue instanceof Number || cellValue instanceof Boolean) {
                out.write(cellValue.toString());
            } else if (cellValue instanceof Date && formatDateISO) {
                writeTextCell(JSONUtils.formatDate((Date) cellValue));
            } else {
                writeTextCell(super.getValueDisplayString(column, cellValue));
            }
        }
        if (i < row.length - 1) {
            out.write(",");
        }
        out.write("\n");
    }
    out.write("\t}");
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) Reader(java.io.Reader) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) Date(java.util.Date)

Example 39 with DBDContent

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

the class DataExporterMarkdownTable method exportRow.

@Override
public void exportRow(DBCSession session, Object[] row) throws DBException, IOException {
    writeDelimiter();
    for (int i = 0; i < row.length && i < columns.size(); i++) {
        DBDAttributeBinding column = columns.get(i);
        if (DBUtils.isNullValue(row[i])) {
            if (!CommonUtils.isEmpty(nullString)) {
                out.write(nullString);
            }
        } else if (row[i] instanceof DBDContent) {
            // Content
            // Inline textual content and handle binaries in some special way
            DBDContent content = (DBDContent) row[i];
            try {
                DBDContentStorage cs = content.getContents(session.getProgressMonitor());
                if (cs == null) {
                    writeCellValue(DBConstants.NULL_VALUE_LABEL);
                } else if (ContentUtils.isTextContent(content)) {
                    writeCellValue(cs.getContentReader());
                } else {
                    getSite().writeBinaryData(cs);
                }
            } finally {
                content.release();
            }
        } else {
            writeCellValue(super.getValueDisplayString(column, row[i]));
        }
        writeDelimiter();
    }
    writeRowLimit();
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage)

Example 40 with DBDContent

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

the class DataExporterSQL method exportRow.

@Override
public void exportRow(DBCSession session, Object[] row) throws DBException, IOException {
    SQLDialect.MultiValueInsertMode insertMode = rowsInStatement == 1 ? SQLDialect.MultiValueInsertMode.NOT_SUPPORTED : getMultiValueInsertMode();
    int columnsSize = columns.size();
    boolean firstRow = false;
    if (insertMode == SQLDialect.MultiValueInsertMode.NOT_SUPPORTED || rowCount % rowsInStatement == 0) {
        sqlBuffer.setLength(0);
        if (rowCount > 0) {
            if (insertMode == SQLDialect.MultiValueInsertMode.PLAIN) {
                sqlBuffer.append(");").append(rowDelimiter);
            } else if (insertMode == SQLDialect.MultiValueInsertMode.GROUP_ROWS) {
                sqlBuffer.append(";").append(rowDelimiter);
            }
        }
        sqlBuffer.append("INSERT INTO ").append(tableName).append(" (");
        boolean hasColumn = false;
        for (int i = 0; i < columnsSize; i++) {
            DBDAttributeBinding column = columns.get(i);
            if (isSkipColumn(column)) {
                continue;
            }
            if (hasColumn) {
                sqlBuffer.append(',');
            }
            hasColumn = true;
            sqlBuffer.append(DBUtils.getQuotedIdentifier(column));
        }
        sqlBuffer.append(") VALUES ");
        if (insertMode != SQLDialect.MultiValueInsertMode.GROUP_ROWS) {
            sqlBuffer.append("(");
        }
        if (rowsInStatement > 1) {
            sqlBuffer.append(rowDelimiter);
        }
        out.write(sqlBuffer.toString());
        firstRow = true;
    }
    if (insertMode != SQLDialect.MultiValueInsertMode.NOT_SUPPORTED && !firstRow) {
        out.write(",");
    }
    if (insertMode == SQLDialect.MultiValueInsertMode.GROUP_ROWS) {
        out.write("(");
    }
    rowCount++;
    boolean hasValue = false;
    for (int i = 0; i < columnsSize; i++) {
        DBDAttributeBinding column = columns.get(i);
        if (isSkipColumn(column)) {
            continue;
        }
        if (hasValue) {
            out.write(',');
        }
        hasValue = true;
        Object value = row[i];
        if (DBUtils.isNullValue(value)) {
            // just skip it
            out.write(SQLConstants.NULL_VALUE);
        } else if (row[i] instanceof DBDContent) {
            DBDContent content = (DBDContent) row[i];
            try {
                if (column.getValueHandler() instanceof DBDContentValueHandler) {
                    ((DBDContentValueHandler) column.getValueHandler()).writeStreamValue(session.getProgressMonitor(), session.getDataSource(), column, content, out);
                } else {
                    // Content
                    // Inline textual content and handle binaries in some special way
                    DBDContentStorage cs = content.getContents(session.getProgressMonitor());
                    if (cs != null) {
                        if (ContentUtils.isTextContent(content)) {
                            try (Reader contentReader = cs.getContentReader()) {
                                writeStringValue(contentReader);
                            }
                        } else {
                            getSite().writeBinaryData(cs);
                        }
                    }
                }
            } catch (Exception e) {
                log.warn(e);
            } finally {
                content.release();
            }
        } else if (value instanceof File) {
            out.write("@");
            out.write(((File) value).getAbsolutePath());
        } else {
            out.write(SQLUtils.convertValueToSQL(session.getDataSource(), column, row[i]));
        }
    }
    if (insertMode != SQLDialect.MultiValueInsertMode.PLAIN) {
        out.write(")");
    }
    if (insertMode == SQLDialect.MultiValueInsertMode.NOT_SUPPORTED) {
        out.write(";");
    }
    out.write(rowDelimiter);
}
Also used : DBDContentValueHandler(org.jkiss.dbeaver.model.data.DBDContentValueHandler) DBDContent(org.jkiss.dbeaver.model.data.DBDContent) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) Reader(java.io.Reader) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPNamedObject(org.jkiss.dbeaver.model.DBPNamedObject) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) File(java.io.File) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) IOException(java.io.IOException) DBException(org.jkiss.dbeaver.DBException)

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