mirror of
https://github.com/alibaba/DataX.git
synced 2025-05-02 19:50:29 +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 quoteChar;
|
||||||
private String lineSeparator;
|
private String lineSeparator;
|
||||||
private String tableName;
|
private String tableName;
|
||||||
|
private String nullFormat;
|
||||||
private StringBuilder insertPrefix;
|
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.sqlWriter = writer;
|
||||||
this.quoteChar = quoteChar;
|
this.quoteChar = quoteChar;
|
||||||
this.lineSeparator = lineSeparator;
|
this.lineSeparator = lineSeparator;
|
||||||
this.tableName = tableName;
|
this.tableName = quoteChar + tableName + quoteChar;
|
||||||
|
this.nullFormat = nullFormat;
|
||||||
buildInsertPrefix(columnNames);
|
buildInsertPrefix(columnNames);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,7 +35,12 @@ public class SqlWriter implements UnstructuredWriter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder sqlPatten = new StringBuilder(4096).append(insertPrefix);
|
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);
|
sqlPatten.append(");").append(lineSeparator);
|
||||||
this.sqlWriter.write(sqlPatten.toString());
|
this.sqlWriter.write(sqlPatten.toString());
|
||||||
}
|
}
|
||||||
|
@ -283,7 +283,8 @@ public class UnstructuredStorageWriterUtil {
|
|||||||
String lineSeparator = config.getString(Key.LINE_DELIMITER, IOUtils.LINE_SEPARATOR);
|
String lineSeparator = config.getString(Key.LINE_DELIMITER, IOUtils.LINE_SEPARATOR);
|
||||||
List<String> headers = config.getList(Key.HEADER, String.class);
|
List<String> headers = config.getList(Key.HEADER, String.class);
|
||||||
Preconditions.checkArgument(CollectionUtils.isNotEmpty(headers), "column names are empty");
|
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;
|
return unstructuredWriter;
|
||||||
|
Loading…
Reference in New Issue
Block a user