use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by dbeaver.
the class DataExporterMarkdownTable method exportRow.
@Override
public void exportRow(DBCSession session, DBCResultSet resultSet, Object[] row) throws DBException, IOException {
writeDelimiter();
for (int i = 0; i < row.length && i < columns.length; i++) {
DBDAttributeBinding column = columns[i];
if (DBUtils.isNullValue(row[i])) {
if (!CommonUtils.isEmpty(nullString)) {
getWriter().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.DBDAttributeBinding in project dbeaver by dbeaver.
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]");
}
}
use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by dbeaver.
the class DataExporterTXT method exportRow.
@Override
public void exportRow(DBCSession session, DBCResultSet resultSet, Object[] row) {
StringBuilder txt = new StringBuilder();
if (delimLeading)
txt.append("|");
for (int k = 0; k < columns.length; k++) {
if (k > 0)
txt.append("|");
DBDAttributeBinding attr = columns[k];
String displayString = getCellString(attr, row[k], DBDDisplayFormat.EDIT);
if (displayString.length() > colWidths[k]) {
displayString = CommonUtils.truncateString(displayString, colWidths[k]);
}
txt.append(displayString);
for (int j = colWidths[k] - displayString.length(); j > 0; j--) {
txt.append(" ");
}
}
if (delimTrailing)
txt.append("|");
txt.append("\n");
getWriter().print(txt);
}
use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by dbeaver.
the class DataExporterTXT method printHeader.
private void printHeader() {
colWidths = new int[columns.length];
for (int i = 0; i < columns.length; i++) {
DBDAttributeBinding attr = columns[i];
int maxLength = (int) attr.getMaxLength();
if (attr.getDataKind() == DBPDataKind.DATETIME) {
// DATETIME attributes are converted to strings so their actual length may differ
maxLength = getCellString(attr, new Date(), DBDDisplayFormat.EDIT).length();
}
colWidths[i] = Math.max(getAttributeName(attr).length(), maxLength);
}
for (int i = 0; i < colWidths.length; i++) {
if (colWidths[i] > maxColumnSize) {
colWidths[i] = maxColumnSize;
} else if (colWidths[i] < minColumnSize) {
colWidths[i] = minColumnSize;
}
}
StringBuilder txt = new StringBuilder();
if (delimLeading)
txt.append("|");
for (int i = 0; i < columns.length; i++) {
if (i > 0)
txt.append("|");
DBDAttributeBinding attr = columns[i];
String attrName = getAttributeName(attr);
txt.append(attrName);
for (int k = colWidths[i] - attrName.length(); k > 0; k--) {
txt.append(" ");
}
}
if (delimTrailing)
txt.append("|");
txt.append("\n");
if (delimHeader) {
// Print header
if (delimLeading)
txt.append("|");
for (int i = 0; i < columns.length; i++) {
if (i > 0)
txt.append("|");
for (int k = colWidths[i]; k > 0; k--) {
txt.append("-");
}
}
if (delimTrailing)
txt.append("|");
txt.append("\n");
}
getWriter().print(txt);
}
use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by dbeaver.
the class DatabaseMappingContainer method readAttributes.
private void readAttributes(DBRProgressMonitor monitor) throws DBException {
if (source instanceof DBSEntity && !(source instanceof DBSDocumentContainer)) {
for (DBSEntityAttribute attr : CommonUtils.safeCollection(((DBSEntity) source).getAttributes(monitor))) {
if (DBUtils.isHiddenObject(attr)) {
continue;
}
addAttributeMapping(monitor, attr);
}
} else {
// Seems to be a dynamic query. Execute it to get metadata
DBPDataSource dataSource = source.getDataSource();
assert (dataSource != null);
DBCExecutionContext context;
if (source instanceof DBPContextProvider) {
context = ((DBPContextProvider) source).getExecutionContext();
} else {
context = DBUtils.getDefaultContext(source, false);
}
if (context == null) {
throw new DBCException("No execution context");
}
DBExecUtils.tryExecuteRecover(monitor, context.getDataSource(), monitor1 -> {
try (DBCSession session = context.openSession(monitor1, DBCExecutionPurpose.META, "Read query meta data")) {
MetadataReceiver receiver = new MetadataReceiver();
try {
source.readData(new AbstractExecutionSource(source, session.getExecutionContext(), this), session, receiver, null, 0, 1, DBSDataContainer.FLAG_NONE, 1);
for (DBDAttributeBinding attr : receiver.attributes) {
if (DBUtils.isHiddenObject(attr)) {
continue;
}
addAttributeMapping(monitor1, attr);
}
} catch (Exception e) {
throw new InvocationTargetException(e);
}
}
});
}
}
Aggregations