Search in sources :

Example 46 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, DBCResultSet resultSet, Object[] row) throws DBException, IOException {
    PrintWriter out = getWriter();
    out.write("  <DATA_RECORD>\n");
    for (int i = 0; i < row.length; i++) {
        DBDAttributeBinding column = columns[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) PrintWriter(java.io.PrintWriter)

Example 47 with DBDContent

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

the class DataExporterXLSX method exportRow.

@Override
public void exportRow(DBCSession session, DBCResultSet resultSet, Object[] row) throws DBException, IOException {
    Worksheet wsh = getWsh(resultSet, 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[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) {
            if (booleRedefined) {
                cell.setCellValue((Boolean) row[i] ? boolTrue : boolFalse);
            } else {
                cell.setCellValue((Boolean) row[i]);
            }
        } else if (row[i] instanceof Number) {
            cell.setCellValue(((Number) row[i]).doubleValue());
        } else if (row[i] instanceof Date) {
            cell.setCellValue((Date) row[i]);
            cell.setCellStyle(styleDate);
        } else {
            String stringValue = super.getValueDisplayString(column, row[i]);
            cell.setCellValue(stringValue);
        }
    }
    wsh.incRow();
    rowCount++;
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) Date(java.util.Date)

Example 48 with DBDContent

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

the class ContentEditorInput method prepareContent.

private void prepareContent(DBRProgressMonitor monitor) throws DBException {
    Object value = getValue();
    DBDContent content;
    if (value instanceof DBDContent) {
        content = (DBDContent) value;
    } else {
        // No need to do init
        stringStorage = new StringEditorInput(getName(), CommonUtils.toString(value), isReadOnly(), fileCharset).getStorage();
        return;
    }
    DBDContentStorage storage = content.getContents(monitor);
    if (contentDetached) {
        release();
        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, DBWorkbench.getPlatform(), 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) StringEditorInput(org.jkiss.dbeaver.ui.editors.StringEditorInput) IAttributeController(org.jkiss.dbeaver.ui.data.IAttributeController) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage)

Example 49 with DBDContent

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

the class ContentInlineEditor method extractEditorValue.

@Override
public Object extractEditorValue() {
    String newValue = control.getText();
    final DBDContent content = (DBDContent) valueController.getValue();
    assert content != null;
    try {
        if (isText) {
            content.updateContents(new VoidProgressMonitor(), new StringContentStorage(newValue));
        } else {
            content.updateContents(new VoidProgressMonitor(), new BytesContentStorage(newValue.getBytes(GeneralUtils.getDefaultFileEncoding()), GeneralUtils.getDefaultFileEncoding()));
        }
    } catch (Exception e) {
        log.error(e);
    }
    return content;
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) BytesContentStorage(org.jkiss.dbeaver.model.data.storage.BytesContentStorage) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) StringContentStorage(org.jkiss.dbeaver.model.data.storage.StringContentStorage) DBException(org.jkiss.dbeaver.DBException)

Example 50 with DBDContent

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

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)

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