mirror of
https://github.com/alibaba/DataX.git
synced 2025-05-02 08:41:53 +08:00
Merge pull request #2013 from LuckyPickleZZ/wenmu_fix_sql_writer
fix(SqlWriter): set null value to NULL in SqlWriter
This commit is contained in:
commit
7d9d587763
@ -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());
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user