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
}
}
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;
}
}
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}");
}
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();
}
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);
}
Aggregations