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