未验证 提交 e7f6f1b3 编写于 作者: S Serge Rider 提交者: GitHub

Merge pull request #3430 from henriklen/copy-as-dbunit-dataset

Support for export in DbUnit dataset format
......@@ -445,6 +445,14 @@ dataTransfer.processor.xml.description=Export to XML file(s)
dataTransfer.processor.xml.propertyGroup.general.label = General
dataTransfer.processor.xml.property.extension.label = File extension
dataTransfer.processor.xml.property.format.label = Dates/numbers format
dataTransfer.processor.dbunit.name=DbUnit
dataTransfer.processor.dbunit.description=Export to DbUnit XML file(s)
dataTransfer.processor.dbunit.propertyGroup.general.label = General
dataTransfer.processor.dbunit.property.extension.label = File extension
dataTransfer.processor.dbunit.property.uppercase.table.name.label = Force upper case table name
dataTransfer.processor.dbunit.property.uppercase.column.names.label = Force upper case column names
dataTransfer.processor.dbunit.property.include.null.values.label=Include NULL values in export
dataTransfer.processor.dbunit.property.null.value.string.label=Replace NULL values with
dataTransfer.processor.json.name=JSON
dataTransfer.processor.json.description=Export to JSON file(s)
dataTransfer.processor.json.propertyGroup.general.label = General
......
......@@ -3551,6 +3551,20 @@
<property id="extension" label="%dataTransfer.processor.xml.property.extension.label" defaultValue="xml"/>
</propertyGroup>
</processor>
<processor
id="stream.dbunit"
class="org.jkiss.dbeaver.tools.transfer.stream.impl.DataExporterDbUnit"
description="%dataTransfer.processor.dbunit.description"
icon="icons/misc/xml.png"
label="%dataTransfer.processor.dbunit.name">
<propertyGroup label="%dataTransfer.processor.dbunit.propertyGroup.general.label">
<property id="upperCaseTableName" label="%dataTransfer.processor.dbunit.property.uppercase.table.name.label" type="boolean" defaultValue="true"/>
<property id="upperCaseColumnNames" label="%dataTransfer.processor.dbunit.property.uppercase.column.names.label" type="boolean" defaultValue="true"/>
<property id="extension" label="%dataTransfer.processor.dbunit.property.extension.label" defaultValue="xml"/>
<property id="includeNullValues" label="%dataTransfer.processor.dbunit.property.include.null.values.label" type="boolean" defaultValue="true"/>
<property id="nullValueString" label="%dataTransfer.processor.dbunit.property.null.value.string.label" defaultValue="[NULL]"/>
</propertyGroup>
</processor>
<processor
id="stream.json"
class="org.jkiss.dbeaver.tools.transfer.stream.impl.DataExporterJSON"
......
......@@ -48,5 +48,7 @@ public interface IStreamDataExporterSite {
void flush() throws IOException;
void writeBinaryData(@NotNull DBDContentStorage cs) throws IOException;
String getOutputEncoding();
}
\ No newline at end of file
......@@ -549,5 +549,10 @@ public class StreamTransferConsumer implements IDataTransferConsumer<StreamConsu
}
}
}
@Override
public String getOutputEncoding() {
return settings == null ? null : settings.getOutputEncoding();
}
}
}
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
* Copyright (C) 2012 Eugene Fradkin (eugene.fradkin@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.tools.transfer.stream.impl;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.List;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.data.DBDAttributeBinding;
import org.jkiss.dbeaver.model.data.DBDContent;
import org.jkiss.dbeaver.model.data.DBDContentStorage;
import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCColumnMetaData;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSAttributeBase;
import org.jkiss.dbeaver.tools.transfer.stream.IStreamDataExporterSite;
import org.jkiss.dbeaver.utils.ContentUtils;
import org.jkiss.utils.Base64;
import org.jkiss.utils.CommonUtils;
/**
* DbUnit Dataset Exporter
*
* DbUnit is a framwork for populating a database with test data before
* running an integration test. This export uses the format used by FlatXmlDataSet/ReplacementDataSet
* described at http://dbunit.sourceforge.net/components.html
*/
public class DataExporterDbUnit extends StreamExporterAbstract {
private static final String PROP_UPPER_CASE_TABLE_NAME = "upperCaseTableName";
private static final String PROP_NULL_VALUE_STRING = "nullValueString";
private static final String PROP_UPPER_CASE_COLUMN_NAMES = "upperCaseColumnNames";
private static final String PROP_INCLUDE_NULL_VALUES = "includeNullValues";
private PrintWriter out;
private List<DBDAttributeBinding> columns;
private String tableName;
private boolean upperCaseTableName;
private boolean upperCaseColumnNames;
private boolean includeNullValues;
@Override
public void init(IStreamDataExporterSite site) throws DBException
{
super.init(site);
out = site.getWriter();
upperCaseTableName = CommonUtils.getBoolean(site.getProperties().get(PROP_UPPER_CASE_TABLE_NAME), true);
upperCaseColumnNames = CommonUtils.getBoolean(site.getProperties().get(PROP_UPPER_CASE_COLUMN_NAMES), true);
includeNullValues = CommonUtils.getBoolean(site.getProperties().get(PROP_INCLUDE_NULL_VALUES), true);
}
@Override
public void dispose()
{
out = null;
tableName = null;
columns = null;
super.dispose();
}
private String getTableName()
{
String result = "UNKNOWN_TABLE_NAME";
if (getSite() == null || getSite().getAttributes() == null || getSite().getAttributes().isEmpty()) {
return result;
}
DBSAttributeBase metaAttribute = getSite().getAttributes().get(0).getMetaAttribute();
if (metaAttribute != null && metaAttribute instanceof JDBCColumnMetaData) {
JDBCColumnMetaData metaData = (JDBCColumnMetaData) metaAttribute;
result = metaData.getEntityName();
}
if (upperCaseTableName) {
result = result == null ? null : result.toUpperCase();
}
return result;
}
@Override
public void exportHeader(DBCSession session) throws DBException, IOException
{
columns = getSite().getAttributes();
tableName = getTableName();
String outputEncoding = getSite().getOutputEncoding() == null || getSite().getOutputEncoding().length() == 0 ? "UTF-8" : getSite().getOutputEncoding();
out.append("<?xml version=\"1.0\" encoding=\"").append(outputEncoding).append("\"?>").append(CommonUtils.getLineSeparator());
out.append("<dataset>").append(CommonUtils.getLineSeparator());
}
@Override
public void exportRow(DBCSession session, Object[] row) throws DBException, IOException
{
out.write(" <" + tableName);
for (int i = 0; i < row.length; i++) {
if (DBUtils.isNullValue(row[i]) && !includeNullValues) {
continue;
}
DBDAttributeBinding column = columns.get(i);
String columnName = escapeXmlElementName(column.getName());
if (columnName != null && upperCaseColumnNames) {
columnName = columnName.toUpperCase();
}
out.write(" " + columnName + "=\"");
if (DBUtils.isNullValue(row[i])) {
writeTextCell("" + getSite().getProperties().get(PROP_NULL_VALUE_STRING));
} else if (row[i] instanceof DBDContent) {
// Content
// Inline textual content and handle binaries in some special way
DBDContent content = (DBDContent)row[i];
try {
DBDContentStorage cs = content.getContents(session.getProgressMonitor());
if (cs != null) {
if (ContentUtils.isTextContent(content)) {
try (Reader reader = cs.getContentReader()) {
writeCellValue(reader);
}
} else {
// BLOB format http://dbunit.sourceforge.net/datatypes.html
out.write("[BASE64]");
try (final InputStream stream = cs.getContentStream()) {
Base64.encode(stream, cs.getContentLength(), getSite().getWriter());
}
}
}
}
finally {
content.release();
}
} else {
writeTextCell(super.getValueDisplayString(column, row[i]));
}
out.write("\"");
}
out.write(">" + CommonUtils.getLineSeparator());
}
@Override
public void exportFooter(DBRProgressMonitor monitor) throws IOException
{
out.write("</dataset>\n");
}
private void writeTextCell(@Nullable String value)
{
if (value != null) {
value = value.replace("<", "&lt;").replace(">", "&gt;").replace("&", "&amp;");
out.write(value);
}
}
private void writeCellValue(Reader reader) throws IOException
{
// Copy reader
char buffer[] = new char[2000];
for (;;) {
int count = reader.read(buffer);
if (count <= 0) {
break;
}
for (int i = 0; i < count; i++) {
if (buffer[i] == '<') {
out.write("&lt;");
}
else if (buffer[i] == '>') {
out.write("&gt;");
} else if (buffer[i] == '&') {
out.write("&amp;");
} else {
out.write(buffer[i]);
}
}
}
}
private String escapeXmlElementName(String name) {
return name.replaceAll("[^\\p{Alpha}\\p{Digit}]+","_");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册