Search in sources :

Example 1 with DBDContentValueHandler

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);
}
Also used : DBDContentValueHandler(org.jkiss.dbeaver.model.data.DBDContentValueHandler) DBDContent(org.jkiss.dbeaver.model.data.DBDContent) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) Reader(java.io.Reader) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPNamedObject(org.jkiss.dbeaver.model.DBPNamedObject) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) File(java.io.File) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) IOException(java.io.IOException) DBException(org.jkiss.dbeaver.DBException)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 DBException (org.jkiss.dbeaver.DBException)1 DBPNamedObject (org.jkiss.dbeaver.model.DBPNamedObject)1 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)1 DBDContent (org.jkiss.dbeaver.model.data.DBDContent)1 DBDContentStorage (org.jkiss.dbeaver.model.data.DBDContentStorage)1 DBDContentValueHandler (org.jkiss.dbeaver.model.data.DBDContentValueHandler)1 SQLDialect (org.jkiss.dbeaver.model.sql.SQLDialect)1 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)1