Search in sources :

Example 6 with DBDContentStorage

use of org.jkiss.dbeaver.model.data.DBDContentStorage 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 7 with DBDContentStorage

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

the class OracleCLOBValueHandler method writeStreamValue.

@Override
public void writeStreamValue(DBRProgressMonitor monitor, @NotNull DBPDataSource dataSource, @NotNull DBSTypedObject type, @NotNull DBDContent object, @NotNull Writer writer) throws DBCException, IOException {
    DBDContentStorage contents = object.getContents(monitor);
    if (contents == null) {
        writer.write("NULL");
        return;
    }
    String strValue = ContentUtils.getContentStringValue(monitor, object);
    String[] parts = splitString(strValue);
    for (int i = 0; i < parts.length; i++) {
        String part = parts[i];
        if (i > 0)
            writer.write("||");
        writer.write("TO_CLOB('");
        writer.write(part.replace("'", "''"));
        writer.write("')");
    }
}
Also used : DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage)

Example 8 with DBDContentStorage

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

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.getName();
        out.write("\t\t\"" + 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(dateFormat.format(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 9 with DBDContentStorage

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

the class ContentEditorInput method copyContentToFile.

private void copyContentToFile(DBDContent contents, DBRProgressMonitor monitor) throws DBException, IOException {
    DBDContentStorage storage = contents.getContents(monitor);
    markReadOnly(false);
    try (OutputStream os = new FileOutputStream(contentFile)) {
        if (contents.isNull()) {
            ContentUtils.copyStreams(new ByteArrayInputStream(new byte[0]), 0, os, monitor);
        } else {
            if (storage == null) {
                log.warn("Can't get data from null storage");
                return;
            }
            try (InputStream is = storage.getContentStream()) {
                ContentUtils.copyStreams(is, storage.getContentLength(), os, monitor);
            }
        }
    }
    markReadOnly(valueController.isReadOnly());
}
Also used : DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage)

Example 10 with DBDContentStorage

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

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);
    DBDContent content = getContent();
    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);
    }
}
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

DBDContentStorage (org.jkiss.dbeaver.model.data.DBDContentStorage)15 DBDContent (org.jkiss.dbeaver.model.data.DBDContent)7 DBException (org.jkiss.dbeaver.DBException)4 File (java.io.File)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Reader (java.io.Reader)3 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)3 DBDContentCached (org.jkiss.dbeaver.model.data.DBDContentCached)3 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 NotNull (org.jkiss.code.NotNull)2 DBDContentStorageLocal (org.jkiss.dbeaver.model.data.DBDContentStorageLocal)2 DBCException (org.jkiss.dbeaver.model.exec.DBCException)2 DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Date (java.util.Date)1 ExternalContentStorage (org.jkiss.dbeaver.model.impl.ExternalContentStorage)1 StringContentStorage (org.jkiss.dbeaver.model.impl.StringContentStorage)1 TemporaryContentStorage (org.jkiss.dbeaver.model.impl.TemporaryContentStorage)1