提交 8d626f35 编写于 作者: L LonwoLonwo

#9696 extra formatting options for SQL INSERT export format added


Former-commit-id: 3652a364
上级 97b87c65
......@@ -76,6 +76,12 @@ dataTransfer.processor.sql.property.rowsInStatement.name=Data rows per statement
dataTransfer.processor.sql.property.rowsInStatement.description=Number of data rows per single insert statement
dataTransfer.processor.sql.property.nativeFormat.name=Native date/time format
dataTransfer.processor.sql.property.nativeFormat.description=Use native date/time format in INSERT statements
dataTransfer.processor.sql.property.lineBeforeValues.name = Insert line before VALUES
dataTransfer.processor.sql.property.lineBeforeValues.description = Insert line feeds before keyword VALUES
dataTransfer.processor.sql.property.lineBeforeRows.name = Insert line before rows
dataTransfer.processor.sql.property.lineBeforeRows.description = Insert line feeds before each VALUES line (for multi-row inserts)
dataTransfer.processor.sql.property.keywordCase.name = Keyword case
dataTransfer.processor.sql.property.keywordCase.description = You can choose lower or upper keyword case
dataTransfer.processor.xml.name=XML
dataTransfer.processor.xml.description=Export to XML file(s)
dataTransfer.processor.xml.propertyGroup.general.label = General
......
......@@ -167,6 +167,10 @@
<property id="nativeFormat" label="%dataTransfer.processor.sql.property.nativeFormat.name" type="boolean" description="%dataTransfer.processor.sql.property.nativeFormat.description" defaultValue="true" />
<property id="omitSchema" label="%dataTransfer.processor.sql.property.omitSchema.name" type="boolean" description="%dataTransfer.processor.sql.property.omitSchema.description" required="false" defaultValue="false"/>
<property id="rowsInStatement" label="%dataTransfer.processor.sql.property.rowsInStatement.name" type="integer" description="%dataTransfer.processor.sql.property.rowsInStatement.description" defaultValue="10" required="true"/>
<property id="lineBeforeValues" label="%dataTransfer.processor.sql.property.lineBeforeValues.name" type="boolean" description="%dataTransfer.processor.sql.property.lineBeforeValues.description" defaultValue="false" />
<property id="lineBeforeRows" label="%dataTransfer.processor.sql.property.lineBeforeRows.name" type="boolean" description="%dataTransfer.processor.sql.property.lineBeforeRows.description" defaultValue="true" />
<property id="keywordCase" label="%dataTransfer.processor.sql.property.keywordCase.name" type="string" description="%dataTransfer.processor.sql.property.keywordCase.description" defaultValue="upper" required="true" validValues="upper,lower"/>
</propertyGroup>
</processor>
<processor
......
......@@ -37,6 +37,8 @@ import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Locale;
import java.util.Map;
/**
* SQL Exporter
......@@ -50,19 +52,33 @@ public class DataExporterSQL extends StreamExporterAbstract {
private static final String PROP_ROWS_IN_STATEMENT = "rowsInStatement";
private static final String PROP_DATA_FORMAT = "nativeFormat";
private static final char STRING_QUOTE = '\'';
private static final String PROP_LINE_BEFORE_VALUES = "lineBeforeValues";
private static final String PROP_LINE_BEFORE_ROWS = "lineBeforeRows";
private static final String PROP_KEYWORD_CASE = "keywordCase";
private boolean includeAutoGenerated;
private String rowDelimiter;
private boolean omitSchema;
private int rowsInStatement;
private boolean useNativeDataFormat = true;
private boolean lineBeforeValues;
private boolean lineBeforeRows = true;
private String tableName;
private DBDAttributeBinding[] columns;
private final String KEYWORD_INSERT_INTO = "INSERT INTO";
private final String KEYWORD_VALUES = "VALUES";
private transient StringBuilder sqlBuffer = new StringBuilder(100);
private transient long rowCount;
private SQLDialect dialect;
enum KeywordCase {
upper,
lower
}
private KeywordCase keywordCase;
private boolean isSkipColumn(DBDAttributeBinding attr) {
return attr.isPseudoAttribute() || (!includeAutoGenerated && attr.isAutoGenerated()) ||
attr instanceof DBDAttributeBindingCustom;
......@@ -72,20 +88,26 @@ public class DataExporterSQL extends StreamExporterAbstract {
public void init(IStreamDataExporterSite site) throws DBException {
super.init(site);
if (site.getProperties().containsKey(PROP_INCLUDE_AUTO_GENERATED)) {
includeAutoGenerated = CommonUtils.toBoolean(site.getProperties().get(PROP_INCLUDE_AUTO_GENERATED));
Map<String, Object> properties = site.getProperties();
if (properties.containsKey(PROP_INCLUDE_AUTO_GENERATED)) {
includeAutoGenerated = CommonUtils.toBoolean(properties.get(PROP_INCLUDE_AUTO_GENERATED));
}
if (site.getProperties().containsKey(PROP_OMIT_SCHEMA)) {
omitSchema = CommonUtils.toBoolean(site.getProperties().get(PROP_OMIT_SCHEMA));
if (properties.containsKey(PROP_OMIT_SCHEMA)) {
omitSchema = CommonUtils.toBoolean(properties.get(PROP_OMIT_SCHEMA));
}
try {
rowsInStatement = Integer.parseInt(String.valueOf(site.getProperties().get(PROP_ROWS_IN_STATEMENT)));
rowsInStatement = Integer.parseInt(String.valueOf(properties.get(PROP_ROWS_IN_STATEMENT)));
} catch (NumberFormatException e) {
rowsInStatement = 10;
}
useNativeDataFormat = CommonUtils.toBoolean(site.getProperties().get(PROP_DATA_FORMAT));
useNativeDataFormat = CommonUtils.toBoolean(properties.get(PROP_DATA_FORMAT));
lineBeforeValues = CommonUtils.toBoolean(properties.get(PROP_LINE_BEFORE_VALUES));
lineBeforeRows = CommonUtils.toBoolean(properties.get(PROP_LINE_BEFORE_ROWS));
rowDelimiter = GeneralUtils.getDefaultLineSeparator();
dialect = SQLUtils.getDialectFromObject(site.getSource());
keywordCase = CommonUtils.valueOf(KeywordCase.class, String.valueOf(properties.get(PROP_KEYWORD_CASE)), KeywordCase.upper);
}
@Override
......@@ -110,6 +132,7 @@ public class DataExporterSQL extends StreamExporterAbstract {
@Override
public void exportRow(DBCSession session, DBCResultSet resultSet, Object[] row) throws DBException, IOException {
PrintWriter out = getWriter();
boolean upperCase = keywordCase == KeywordCase.upper;
SQLDialect.MultiValueInsertMode insertMode = rowsInStatement == 1 ? SQLDialect.MultiValueInsertMode.NOT_SUPPORTED : getMultiValueInsertMode();
if (insertMode == SQLDialect.MultiValueInsertMode.NOT_SUPPORTED) {
rowsInStatement = 1;
......@@ -120,12 +143,20 @@ public class DataExporterSQL extends StreamExporterAbstract {
sqlBuffer.setLength(0);
if (rowCount > 0) {
if (insertMode == SQLDialect.MultiValueInsertMode.PLAIN) {
sqlBuffer.append(");").append(rowDelimiter);
sqlBuffer.append(");");
} else if (insertMode == SQLDialect.MultiValueInsertMode.GROUP_ROWS) {
sqlBuffer.append(";").append(rowDelimiter);
sqlBuffer.append(";");
}
if (lineBeforeRows) {
sqlBuffer.append(rowDelimiter);
}
}
sqlBuffer.append("INSERT INTO ").append(tableName).append(" (");
if (upperCase) {
sqlBuffer.append(KEYWORD_INSERT_INTO);
} else {
sqlBuffer.append(KEYWORD_INSERT_INTO.toLowerCase(Locale.ENGLISH));
}
sqlBuffer.append(" ").append(tableName).append(" (");
boolean hasColumn = false;
for (int i = 0; i < columnsSize; i++) {
DBDAttributeBinding column = columns[i];
......@@ -138,11 +169,21 @@ public class DataExporterSQL extends StreamExporterAbstract {
hasColumn = true;
sqlBuffer.append(DBUtils.getQuotedIdentifier(column));
}
sqlBuffer.append(") VALUES ");
sqlBuffer.append(")");
if (lineBeforeValues) {
sqlBuffer.append(rowDelimiter).append("\t");
} else {
sqlBuffer.append(" ");
}
if (upperCase) {
sqlBuffer.append(KEYWORD_VALUES);
} else {
sqlBuffer.append(KEYWORD_VALUES.toLowerCase(Locale.ENGLISH));
}
if (insertMode != SQLDialect.MultiValueInsertMode.GROUP_ROWS) {
sqlBuffer.append("(");
sqlBuffer.append("\t(");
}
if (rowsInStatement > 1) {
if (rowsInStatement > 1 && lineBeforeRows) {
sqlBuffer.append(rowDelimiter);
}
out.write(sqlBuffer.toString());
......@@ -150,9 +191,15 @@ public class DataExporterSQL extends StreamExporterAbstract {
}
if (insertMode != SQLDialect.MultiValueInsertMode.NOT_SUPPORTED && !firstRow) {
out.write(",");
if (lineBeforeRows) {
out.write(rowDelimiter);
}
}
if (insertMode == SQLDialect.MultiValueInsertMode.GROUP_ROWS) {
out.write("(");
if (lineBeforeRows) {
out.write("\t");
}
out.write(" (");
}
rowCount++;
boolean hasValue = false;
......@@ -206,9 +253,8 @@ public class DataExporterSQL extends StreamExporterAbstract {
out.write(")");
}
if (insertMode == SQLDialect.MultiValueInsertMode.NOT_SUPPORTED) {
out.write(";");
out.write(";" + rowDelimiter);
}
out.write(rowDelimiter);
}
@Override
......@@ -265,7 +311,7 @@ public class DataExporterSQL extends StreamExporterAbstract {
private SQLDialect.MultiValueInsertMode getMultiValueInsertMode() {
SQLDialect.MultiValueInsertMode insertMode = SQLDialect.MultiValueInsertMode.NOT_SUPPORTED;
if (dialect != null) {
if (dialect != null && rowsInStatement != 1) {
insertMode = dialect.getMultiValueInsertMode();
}
return insertMode;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册