Search in sources :

Example 1 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, Object[] row) throws DBException, IOException {
    out.write("<tr" + (rowCount++ % 2 == 0 ? " class=\"odd\"" : "") + ">");
    for (int i = 0; i < row.length; i++) {
        DBDAttributeBinding column = columns.get(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)

Example 2 with DBDContent

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

the class DataExporterXML method exportRow.

@Override
public void exportRow(DBCSession session, Object[] row) throws DBException, IOException {
    out.write("  <DATA_RECORD>\n");
    for (int i = 0; i < row.length; i++) {
        DBDAttributeBinding column = columns.get(i);
        String columnName = escapeXmlElementName(column.getName());
        out.write("    <" + columnName + ">");
        if (DBUtils.isNullValue(row[i])) {
            writeTextCell(null);
        } 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) {
                    if (ContentUtils.isTextContent(content)) {
                        try (Reader reader = cs.getContentReader()) {
                            writeCellValue(reader);
                        }
                    } else {
                        getSite().writeBinaryData(cs);
                    }
                }
            } finally {
                content.release();
            }
        } else {
            writeTextCell(super.getValueDisplayString(column, row[i]));
        }
        out.write("</" + columnName + ">\n");
    }
    out.write("  </DATA_RECORD>\n");
}
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)

Example 3 with DBDContent

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

the class DialogUtils method loadFromFile.

public static boolean loadFromFile(final IValueController controller) {
    if (!(controller.getValue() instanceof DBDContent)) {
        log.error(CoreMessages.model_jdbc_bad_content_value_ + controller.getValue());
        return false;
    }
    Shell shell = UIUtils.getShell(controller.getValueSite());
    final File openFile = openFile(shell);
    if (openFile == null) {
        return false;
    }
    final DBDContent value = (DBDContent) controller.getValue();
    DBeaverUI.runInUI(PlatformUI.getWorkbench().getActiveWorkbenchWindow(), new DBRRunnableWithProgress() {

        @Override
        public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                DBDContentStorage storage;
                if (ContentUtils.isTextContent(value)) {
                    storage = new ExternalContentStorage(DBeaverCore.getInstance(), openFile, GeneralUtils.UTF8_ENCODING);
                } else {
                    storage = new ExternalContentStorage(DBeaverCore.getInstance(), openFile);
                }
                value.updateContents(monitor, storage);
                controller.updateValue(value, true);
            } catch (Exception e) {
                throw new InvocationTargetException(e);
            }
        }
    });
    return true;
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) ExternalContentStorage(org.jkiss.dbeaver.model.impl.ExternalContentStorage) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) 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 4 with DBDContent

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

the class DataExporterXLSX method exportRow.

@Override
public void exportRow(DBCSession session, Object[] row) throws DBException, IOException {
    Worksheet wsh = getWsh(row);
    Row rowX = wsh.getSh().createRow(wsh.getCurrentRow());
    int startCol = 0;
    if (rowNumber) {
        Cell cell = rowX.createCell(startCol, CellType.NUMERIC);
        cell.setCellStyle(style);
        cell.setCellValue(String.valueOf(wsh.getCurrentRow()));
        startCol++;
    }
    for (int i = 0; i < row.length; i++) {
        DBDAttributeBinding column = columns.get(i);
        Cell cell = rowX.createCell(i + startCol, getCellType(column));
        cell.setCellStyle(style);
        if (DBUtils.isNullValue(row[i])) {
            if (!CommonUtils.isEmpty(nullString)) {
                cell.setCellValue(nullString);
            } else {
                cell.setCellValue("");
            }
        } else if (row[i] instanceof DBDContent) {
            DBDContent content = (DBDContent) row[i];
            try {
                DBDContentStorage cs = content.getContents(session.getProgressMonitor());
                if (cs == null) {
                    cell.setCellValue(DBConstants.NULL_VALUE_LABEL);
                } else if (ContentUtils.isTextContent(content)) {
                    writeCellValue(cell, cs.getContentReader());
                } else {
                    cell.setCellValue(BINARY_FIXED);
                }
            } finally {
                content.release();
            }
        } else if (row[i] instanceof Boolean) {
            cell.setCellValue((Boolean) row[i]);
        } else if (row[i] instanceof Number) {
            cell.setCellValue(((Number) row[i]).doubleValue());
        } else {
            String stringValue = super.getValueDisplayString(column, row[i]);
            cell.setCellValue(stringValue);
        }
    }
    wsh.incRow();
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage)

Example 5 with DBDContent

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

the class ContentEditorInput method updateContentFromFile.

public void updateContentFromFile(IProgressMonitor monitor) throws DBException {
    if (valueController.isReadOnly()) {
        throw new DBCException("Can't update read-only value");
    }
    DBRProgressMonitor localMonitor = RuntimeUtils.makeMonitor(monitor);
    Object value = getValue();
    if (value instanceof DBDContent) {
        DBDContent content = (DBDContent) value;
        DBDContentStorage storage = content.getContents(localMonitor);
        if (storage instanceof DBDContentStorageLocal) {
            // Nothing to update - we user content's storage
            contentDetached = true;
        } else if (storage instanceof DBDContentCached) {
            // Create new storage and pass it to content
            try (FileInputStream is = new FileInputStream(contentFile)) {
                if (storage instanceof StringContentStorage) {
                    try (Reader reader = new InputStreamReader(is, fileCharset)) {
                        storage = StringContentStorage.createFromReader(reader);
                    }
                } else {
                    storage = BytesContentStorage.createFromStream(is, contentFile.length(), fileCharset);
                }
                // StringContentStorage.
                contentDetached = content.updateContents(localMonitor, 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(DBeaverCore.getInstance(), contentFile, fileCharset);
            contentDetached = content.updateContents(localMonitor, storage);
        }
    } else {
        // Just read as string
        updateStringValueFromFile(contentFile);
        contentDetached = true;
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBCException(org.jkiss.dbeaver.model.exec.DBCException) TemporaryContentStorage(org.jkiss.dbeaver.model.impl.TemporaryContentStorage) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) DBDContentCached(org.jkiss.dbeaver.model.data.DBDContentCached) DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBDContentStorageLocal(org.jkiss.dbeaver.model.data.DBDContentStorageLocal) StringContentStorage(org.jkiss.dbeaver.model.impl.StringContentStorage)

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