提交 c5968d67 编写于 作者: M Marat Kalibekov

7593- Use enum for find out strategy


Former-commit-id: 2a0923be
上级 1e2f7697
......@@ -16,7 +16,6 @@
*/
package org.jkiss.dbeaver.tools.transfer.stream.exporter;
import org.apache.commons.lang3.math.NumberUtils;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.DBConstants;
import org.jkiss.dbeaver.model.DBPDataKind;
......@@ -69,7 +68,7 @@ public class DataExporterCSV extends StreamExporterAbstract {
private String delimiter;
private char quoteChar = '"';
private boolean useQuotes = true;
private String quoteAlways = "default";
private QuoteStrategy quoteStrategy = QuoteStrategy.DISABLED;
private String rowDelimiter;
private String nullString;
private HeaderPosition headerPosition;
......@@ -99,7 +98,10 @@ public class DataExporterCSV extends StreamExporterAbstract {
Object nullStringProp = properties.get(PROP_NULL_STRING);
nullString = nullStringProp == null ? null : nullStringProp.toString();
useQuotes = quoteChar != ' ';
quoteAlways = CommonUtils.toString(properties.get(PROP_QUOTE_ALWAYS));
String quoteStrategyStr = CommonUtils.toString(properties.get(PROP_QUOTE_ALWAYS));
if (quoteStrategyStr!=null) {
quoteStrategy = QuoteStrategy.fromValue(quoteStrategyStr);
}
try {
headerPosition = HeaderPosition.valueOf(String.valueOf(properties.get(PROP_HEADER)));
} catch (Exception e) {
......@@ -181,28 +183,20 @@ public class DataExporterCSV extends StreamExporterAbstract {
} else {
String stringValue = super.getValueDisplayString(column, row[i]);
boolean quote = false;
boolean isNumber = NumberUtils.isNumber(stringValue);
boolean isEmpty = stringValue.isEmpty();
boolean rowIsDate = row[i] instanceof Date;
boolean rowIsNumber = row[i] instanceof Number;
boolean startsWithDigit = Character.isDigit(stringValue.charAt(0));
if (quoteAlways.equalsIgnoreCase("disabled") || quoteAlways.equalsIgnoreCase("false")) {
if (!isEmpty && !rowIsNumber && !rowIsDate && startsWithDigit) {
if (quoteStrategy == QuoteStrategy.DISABLED) {
if (!stringValue.isEmpty() && !(row[i] instanceof Number) && !(row[i] instanceof Date) && Character.isDigit(stringValue.charAt(0))) {
// Quote string values which starts from number
quote = true;
}
} else if (quoteAlways.equalsIgnoreCase("all") || quoteAlways.equalsIgnoreCase("true")) {
quote = true;
} else if (quoteAlways.equalsIgnoreCase("strings")) {
if (!rowIsNumber && !isNumber) {
} else if (quoteStrategy == QuoteStrategy.STRINGS) {
if (!stringValue.isEmpty() && !(row[i] instanceof Number) && !(row[i] instanceof Date)) {
quote = true;
}
} else if (quoteAlways.equalsIgnoreCase("all but numbers")) {
if (rowIsNumber || isNumber) {
quote = false;
} else if (quoteStrategy == QuoteStrategy.ALL_BUT_NUMBERS) {
if (!(row[i] instanceof Number)) {
quote = true;
}
quote = true;
}
writeCellValue(stringValue, quote);
}
......@@ -228,7 +222,7 @@ public class DataExporterCSV extends StreamExporterAbstract {
// check for needed quote
final boolean hasQuotes = useQuotes && value.indexOf(quoteChar) != -1;
if (quoteAlways.equalsIgnoreCase("all") || (useQuotes && value.isEmpty())) {
if (quoteStrategy == QuoteStrategy.ALL || (useQuotes && value.isEmpty())) {
quote = true;
} else if (!quote) {
if (hasQuotes ||
......
package org.jkiss.dbeaver.tools.transfer.stream.exporter;
public enum QuoteStrategy {
DISABLED("disabled"), ALL("all"), STRINGS("strings"), ALL_BUT_NUMBERS("all but numbers");
private final String value;
QuoteStrategy(String v) {
value = v;
}
public String value() {
return value;
}
public static QuoteStrategy fromValue(String v) {
for (QuoteStrategy s: QuoteStrategy.values()) {
if (s.value.equals(v)) {
return s;
}
}
// backward compability
if ("true".equalsIgnoreCase(v)) {
return ALL;
} else if ("false".equalsIgnoreCase(v)) {
return DISABLED;
}
throw new IllegalArgumentException(v);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册