Merge pull request #2013 from LuckyPickleZZ/wenmu_fix_sql_writer

fix(SqlWriter): set null value to NULL in SqlWriter
This commit is contained in:
dingxiaobo 2024-04-14 11:26:32 +08:00 committed by GitHub
commit 7d9d587763
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 4 deletions

View File

@ -15,13 +15,15 @@ public class SqlWriter implements UnstructuredWriter {
private String quoteChar;
private String lineSeparator;
private String tableName;
private String nullFormat;
private StringBuilder insertPrefix;
public SqlWriter(Writer writer, String quoteChar, String tableName, String lineSeparator, List<String> columnNames) {
public SqlWriter(Writer writer, String quoteChar, String tableName, String lineSeparator, List<String> columnNames, String nullFormat) {
this.sqlWriter = writer;
this.quoteChar = quoteChar;
this.lineSeparator = lineSeparator;
this.tableName = tableName;
this.tableName = quoteChar + tableName + quoteChar;
this.nullFormat = nullFormat;
buildInsertPrefix(columnNames);
}
@ -33,7 +35,12 @@ public class SqlWriter implements UnstructuredWriter {
}
StringBuilder sqlPatten = new StringBuilder(4096).append(insertPrefix);
sqlPatten.append(splitedRows.stream().map(e -> "'" + DataXCsvWriter.replace(e, "'", "''") + "'").collect(Collectors.joining(",")));
sqlPatten.append(splitedRows.stream().map(e -> {
if (nullFormat.equals(e)) {
return "NULL";
}
return "'" + DataXCsvWriter.replace(e, "'", "''") + "'";
}).collect(Collectors.joining(",")));
sqlPatten.append(");").append(lineSeparator);
this.sqlWriter.write(sqlPatten.toString());
}

View File

@ -283,7 +283,8 @@ public class UnstructuredStorageWriterUtil {
String lineSeparator = config.getString(Key.LINE_DELIMITER, IOUtils.LINE_SEPARATOR);
List<String> headers = config.getList(Key.HEADER, String.class);
Preconditions.checkArgument(CollectionUtils.isNotEmpty(headers), "column names are empty");
unstructuredWriter = new SqlWriter(writer, quoteChar, tableName, lineSeparator, headers);
String nullFormat = config.getString(Key.NULL_FORMAT, Constant.DEFAULT_NULL_FORMAT);
unstructuredWriter = new SqlWriter(writer, quoteChar, tableName, lineSeparator, headers, nullFormat);
}
return unstructuredWriter;