Search in sources :

Example 26 with DBDContent

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

the class DataExporterJSON method exportRow.

@Override
public void exportRow(DBCSession session, DBCResultSet resultSet, Object[] row) throws DBException, IOException {
    PrintWriter out = getWriter();
    if (rowNum > 0) {
        out.write(",\n");
    }
    rowNum++;
    if (isJsonDocumentResults(session.getProgressMonitor(), row)) {
        DBDDocument document = (DBDDocument) row[0];
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        document.serializeDocument(session.getProgressMonitor(), buffer, StandardCharsets.UTF_8);
        String jsonText = buffer.toString(StandardCharsets.UTF_8.name());
        out.write(jsonText);
    } else {
        out.write("\t{\n");
        for (int i = 0; i < columns.length; i++) {
            DBDAttributeBinding column = columns[i];
            String columnName = column.getLabel();
            if (CommonUtils.isEmpty(columnName)) {
                columnName = column.getName();
            }
            out.write("\t\t\"" + JSONUtils.escapeJsonString(columnName) + "\" : ");
            Object cellValue = row[column.getOrdinalPosition()];
            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 < columns.length - 1) {
                out.write(",");
            }
            out.write("\n");
        }
        out.write("\t}");
    }
}
Also used : Reader(java.io.Reader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) Date(java.util.Date) DBDDocument(org.jkiss.dbeaver.model.data.DBDDocument) DBDContent(org.jkiss.dbeaver.model.data.DBDContent) PrintWriter(java.io.PrintWriter)

Example 27 with DBDContent

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

the class DataExporterHTML method exportRow.

@Override
public void exportRow(DBCSession session, DBCResultSet resultSet, Object[] row) throws DBException, IOException {
    PrintWriter out = getWriter();
    out.write("<tr" + (rowCount++ % 2 == 0 ? " class=\"odd\"" : "") + ">");
    for (int i = 0; i < row.length; i++) {
        DBDAttributeBinding column = columns[i];
        if (DBUtils.isNullValue(row[i])) {
            writeTextCell(null, false);
        } 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());
                out.write("<td>");
                if (cs != null) {
                    if (ContentUtils.isTextContent(content)) {
                        writeCellValue(cs.getContentReader());
                    } else {
                        getSite().writeBinaryData(cs);
                    }
                }
                out.write("</td>");
            } finally {
                content.release();
            }
        } else {
            String stringValue = super.getValueDisplayString(column, row[i]);
            boolean isImage = row[i] instanceof File && stringValue != null && stringValue.endsWith(".jpg");
            if (isImage) {
                writeImageCell((File) row[i]);
            } else {
                writeTextCell(stringValue, false);
            }
        }
    }
    out.write("</tr>\n");
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) File(java.io.File) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) PrintWriter(java.io.PrintWriter)

Example 28 with DBDContent

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

the class DataExporterSourceCode method exportRow.

@Override
public void exportRow(DBCSession session, DBCResultSet resultSet, Object[] row) throws DBException, IOException {
    PrintWriter out = getWriter();
    if (rowNum > 0) {
        out.write("," + rowDelimiter);
    }
    rowNum++;
    if (language == ProgramLanguages.PHP_VERSION_LESS_5_and_4) {
        out.write("\tarray(" + rowDelimiter);
    } else {
        out.write("\t[" + rowDelimiter);
    }
    for (int i = 0; i < columns.length; i++) {
        DBDAttributeBinding column = columns[i];
        String columnName = column.getLabel();
        if (CommonUtils.isEmpty(columnName)) {
            columnName = column.getName();
        }
        out.write("\t\t" + quoteChar + JSONUtils.escapeJsonString(columnName) + quoteChar + " => ");
        Object cellValue = row[column.getOrdinalPosition()];
        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(quoteChar);
                            writeCellValue(in);
                            out.write(quoteChar);
                        }
                    } 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 < columns.length - 1) {
            out.write(",");
        }
        out.write(rowDelimiter);
    }
    if (language == ProgramLanguages.PHP_VERSION_LESS_5_and_4) {
        out.write("\t)");
    } else {
        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) PrintWriter(java.io.PrintWriter)

Example 29 with DBDContent

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

the class ContentPanelEditor method createControl.

@Override
protected Control createControl(Composite editPlaceholder) {
    final DBDContent content = (DBDContent) valueController.getValue();
    if (curStreamManager == null) {
        detectStreamManager(content);
    }
    if (curStreamManager != null) {
        try {
            streamEditor = curStreamManager.getInstance().createPanelEditor(valueController);
        } catch (Throwable e) {
            UIUtils.showErrorDialog(editPlaceholder.getShell(), "No stream editor", "Can't create stream editor", e);
        }
    }
    if (streamEditor == null) {
        return UIUtils.createInfoLabel(editPlaceholder, "No Editor");
    }
    editorControl = streamEditor.createControl(valueController);
    return editorControl;
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent)

Example 30 with DBDContent

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

the class ContentPanelEditor method primeEditorValue.

@Override
public void primeEditorValue(@Nullable final Object value) throws DBException {
    final DBDContent content = (DBDContent) valueController.getValue();
    if (content == null) {
        valueController.showMessage("NULL content value. Must be DBDContent.", DBPMessageType.ERROR);
        return;
    }
    if (streamEditor == null) {
        valueController.showMessage("NULL content editor.", DBPMessageType.ERROR);
        return;
    }
    DBeaverUI.runInUI(valueController.getValueSite().getWorkbenchWindow(), new DBRRunnableWithProgress() {

        @Override
        public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                streamEditor.primeEditorValue(monitor, control, content);
            } catch (Throwable e) {
                log.debug(e);
                valueController.showMessage(e.getMessage(), DBPMessageType.ERROR);
            }
        }
    });
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) InvocationTargetException(java.lang.reflect.InvocationTargetException)

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