forked from p81075629/datagear
SQL导出添加是否导出建表语句设置项
This commit is contained in:
parent
c942bdeb7f
commit
3d13214f65
|
@ -29,7 +29,7 @@ public class SqlDataExport extends QueryTextDataExport
|
|||
super();
|
||||
}
|
||||
|
||||
public SqlDataExport(ConnectionFactory connectionFactory, DataFormat dataFormat, TextDataExportOption exportOption,
|
||||
public SqlDataExport(ConnectionFactory connectionFactory, DataFormat dataFormat, SqlDataExportOption exportOption,
|
||||
Query query, String tableName, ResourceFactory<Writer> writerFactory)
|
||||
{
|
||||
super(connectionFactory, dataFormat, exportOption, query);
|
||||
|
@ -37,6 +37,21 @@ public class SqlDataExport extends QueryTextDataExport
|
|||
this.writerFactory = writerFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SqlDataExportOption getExportOption()
|
||||
{
|
||||
return (SqlDataExportOption) super.getExportOption();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExportOption(TextDataExportOption exportOption)
|
||||
{
|
||||
if (!(exportOption instanceof SqlDataExportOption))
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
super.setExportOption(exportOption);
|
||||
}
|
||||
|
||||
public String getTableName()
|
||||
{
|
||||
return tableName;
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (c) 2018 datagear.tech. All Rights Reserved.
|
||||
*/
|
||||
|
||||
package org.datagear.dataexchange.support;
|
||||
|
||||
import org.datagear.dataexchange.TextDataExportOption;
|
||||
|
||||
/**
|
||||
* SQL导出设置项。
|
||||
*
|
||||
* @author datagear@163.com
|
||||
*
|
||||
*/
|
||||
public class SqlDataExportOption extends TextDataExportOption
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private boolean exportCreationSql = false;
|
||||
|
||||
public SqlDataExportOption()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public SqlDataExportOption(boolean nullForIllegalColumnValue, boolean exportCreationSql)
|
||||
{
|
||||
super(nullForIllegalColumnValue);
|
||||
this.exportCreationSql = exportCreationSql;
|
||||
}
|
||||
|
||||
public boolean isExportCreationSql()
|
||||
{
|
||||
return exportCreationSql;
|
||||
}
|
||||
|
||||
public void setExportCreationSql(boolean exportCreationSql)
|
||||
{
|
||||
this.exportCreationSql = exportCreationSql;
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@ import java.sql.Connection;
|
|||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Types;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.datagear.dataexchange.AbstractDevotedDbInfoAwareDataExchangeService;
|
||||
|
@ -16,7 +17,6 @@ import org.datagear.dataexchange.DataExchangeContext;
|
|||
import org.datagear.dataexchange.IndexFormatDataExchangeContext;
|
||||
import org.datagear.dataexchange.RowDataIndex;
|
||||
import org.datagear.dataexchange.TextDataExportListener;
|
||||
import org.datagear.dataexchange.TextDataExportOption;
|
||||
import org.datagear.dbinfo.ColumnInfo;
|
||||
import org.datagear.dbinfo.DatabaseInfoResolver;
|
||||
|
||||
|
@ -76,12 +76,15 @@ public class SqlDataExportService extends AbstractDevotedDbInfoAwareDataExchange
|
|||
Writer out, IndexFormatDataExchangeContext exportContext) throws Throwable
|
||||
{
|
||||
TextDataExportListener listener = dataExchange.getListener();
|
||||
TextDataExportOption exportOption = dataExchange.getExportOption();
|
||||
SqlDataExportOption exportOption = dataExchange.getExportOption();
|
||||
int columnCount = columnInfos.size();
|
||||
|
||||
DatabaseMetaData metaData = cn.getMetaData();
|
||||
String quote = metaData.getIdentifierQuoteString();
|
||||
|
||||
if (exportOption.isExportCreationSql())
|
||||
writeCreationSql(dataExchange, cn, columnInfos, rs, quote, out, exportContext);
|
||||
|
||||
long row = 0;
|
||||
|
||||
while (rs.next())
|
||||
|
@ -160,6 +163,128 @@ public class SqlDataExportService extends AbstractDevotedDbInfoAwareDataExchange
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 写建表语句。
|
||||
*
|
||||
* @param dataExchange
|
||||
* @param cn
|
||||
* @param columnInfos
|
||||
* @param rs
|
||||
* @param quote
|
||||
* @param out
|
||||
* @param exportContext
|
||||
* @throws Throwable
|
||||
*/
|
||||
protected void writeCreationSql(SqlDataExport dataExchange, Connection cn, List<ColumnInfo> columnInfos,
|
||||
ResultSet rs, String quote, Writer out, IndexFormatDataExchangeContext exportContext) throws Throwable
|
||||
{
|
||||
out.write("CREATE TABLE ");
|
||||
out.write(quote);
|
||||
out.write(dataExchange.getTableName());
|
||||
out.write(quote);
|
||||
out.write(LINE_SEPARATOR);
|
||||
out.write('(');
|
||||
out.write(LINE_SEPARATOR);
|
||||
|
||||
String[] primaryColumnNames = getDatabaseInfoResolver().getPrimaryKeyColumnNames(cn,
|
||||
dataExchange.getTableName());
|
||||
List<String> filterPkNames = filterPrimaryColumnNames(primaryColumnNames, columnInfos);
|
||||
|
||||
for (int i = 0, len = columnInfos.size(); i < len; i++)
|
||||
{
|
||||
ColumnInfo columnInfo = columnInfos.get(i);
|
||||
|
||||
out.write(" ");
|
||||
out.write(quote);
|
||||
out.write(columnInfo.getName());
|
||||
out.write(quote);
|
||||
out.write(' ');
|
||||
|
||||
out.write(columnInfo.getTypeName());
|
||||
|
||||
if (columnInfo.getSize() > 0)
|
||||
{
|
||||
out.write('(');
|
||||
out.write(Integer.toString(columnInfo.getSize()));
|
||||
|
||||
if (columnInfo.getDecimalDigits() > 0)
|
||||
{
|
||||
out.write(',');
|
||||
out.write(Integer.toString(columnInfo.getDecimalDigits()));
|
||||
}
|
||||
|
||||
out.write(')');
|
||||
}
|
||||
|
||||
if (!columnInfo.isNullable())
|
||||
out.write(" NOT NULL");
|
||||
|
||||
if (i < len - 1)
|
||||
out.write(',');
|
||||
else if (i == len - 1 && !filterPkNames.isEmpty())
|
||||
out.write(',');
|
||||
|
||||
out.write(LINE_SEPARATOR);
|
||||
}
|
||||
|
||||
if (!filterPkNames.isEmpty())
|
||||
{
|
||||
out.write(" ");
|
||||
out.write("PRIMARY KEY (");
|
||||
|
||||
for (int i = 0, len = filterPkNames.size(); i < len; i++)
|
||||
{
|
||||
out.write(quote);
|
||||
out.write(filterPkNames.get(i));
|
||||
out.write(quote);
|
||||
|
||||
if (i < len - 1)
|
||||
out.write(',');
|
||||
}
|
||||
|
||||
out.write(")");
|
||||
out.write(LINE_SEPARATOR);
|
||||
}
|
||||
|
||||
out.write(");");
|
||||
out.write(LINE_SEPARATOR);
|
||||
out.write(LINE_SEPARATOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* 过滤主键列名,仅保留在{@code columnInfos}包含的。
|
||||
*
|
||||
* @param primaryColumnNames
|
||||
* @param columnInfos
|
||||
* @return
|
||||
*/
|
||||
protected List<String> filterPrimaryColumnNames(String[] primaryColumnNames, List<ColumnInfo> columnInfos)
|
||||
{
|
||||
List<String> names = new ArrayList<String>(3);
|
||||
|
||||
if (primaryColumnNames == null)
|
||||
return names;
|
||||
|
||||
for (int i = 0; i < primaryColumnNames.length; i++)
|
||||
{
|
||||
if (columnInfos == null)
|
||||
names.add(primaryColumnNames[i]);
|
||||
else
|
||||
{
|
||||
for (ColumnInfo columnInfo : columnInfos)
|
||||
{
|
||||
if (columnInfo.getName().equals(primaryColumnNames[i]))
|
||||
{
|
||||
names.add(primaryColumnNames[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
protected String escapeSqlStringValue(String value)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
|
|
@ -98,6 +98,11 @@ public class ColumnInfo extends ResultSetSpecBean
|
|||
this.size = size;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取小数位数,返回{@code <= 0}表示无此值。
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getDecimalDigits()
|
||||
{
|
||||
return decimalDigits;
|
||||
|
|
|
@ -61,6 +61,7 @@ import org.datagear.dataexchange.support.CsvDataImport;
|
|||
import org.datagear.dataexchange.support.ExcelDataExport;
|
||||
import org.datagear.dataexchange.support.ExcelDataImport;
|
||||
import org.datagear.dataexchange.support.SqlDataExport;
|
||||
import org.datagear.dataexchange.support.SqlDataExportOption;
|
||||
import org.datagear.dataexchange.support.SqlDataImport;
|
||||
import org.datagear.dbinfo.DatabaseInfoResolver;
|
||||
import org.datagear.dbinfo.TableInfo;
|
||||
|
@ -1740,6 +1741,21 @@ public class DataExchangeController extends AbstractSchemaConnController
|
|||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SqlDataExportOption getExportOption()
|
||||
{
|
||||
return (SqlDataExportOption) super.getExportOption();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExportOption(TextDataExportOption exportOption)
|
||||
{
|
||||
if (!(exportOption instanceof SqlDataExportOption))
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
super.setExportOption(exportOption);
|
||||
}
|
||||
|
||||
public String[] getTableNames()
|
||||
{
|
||||
return tableNames;
|
||||
|
|
|
@ -507,4 +507,5 @@ dataExport.exportStatusWithSuccessFail=\u5BFC\u51FA\u8FDB\u5EA6\uFF08\u6210\u529
|
|||
dataExport.addAllTable=\u6DFB\u52A0\u5168\u90E8\u8868
|
||||
dataExport.startExport=\u5F00\u59CB\u5BFC\u51FA
|
||||
dataExport.finishExport=\u5B8C\u6210\u5BFC\u51FA
|
||||
dataExport.sqlExportTableName=SQL\u8868\u540D
|
||||
dataExport.sqlExportTableName=SQL\u8868\u540D
|
||||
dataExport.exportCreationSql=\u5BFC\u51FA\u5EFA\u8868\u8BED\u53E5
|
|
@ -40,6 +40,17 @@ Schema schema 数据库,不允许为null
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-item">
|
||||
<div class="form-item-label"><@spring.message code='dataExport.exportCreationSql' /></div>
|
||||
<div class="form-item-value">
|
||||
<div id="${pageId}-exportCreationSql">
|
||||
<label for="${pageId}-exportCreationSql-0"><@spring.message code='yes' /></label>
|
||||
<input id="${pageId}-exportCreationSql-0" type="radio" name="exportOption.exportCreationSql" value="true" />
|
||||
<label for="${pageId}-exportCreationSql-1"><@spring.message code='no' /></label>
|
||||
<input id="${pageId}-exportCreationSql-1" type="radio" name="exportOption.exportCreationSql" value="false" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<h3><@spring.message code='dataExport.selectAndExportData' /></h3>
|
||||
<div>
|
||||
|
@ -142,6 +153,8 @@ Schema schema 数据库,不允许为null
|
|||
po.initDataExportSteps();
|
||||
po.initDataExchangeUIs();
|
||||
po.initDataExportUIs();
|
||||
po.element("#${pageId}-exportCreationSql").buttonset();
|
||||
po.element("#${pageId}-exportCreationSql-1").click();
|
||||
po.initDataExportDataTable();
|
||||
po.initDataExchangeActions();
|
||||
po.initDataExportActions();
|
||||
|
|
Loading…
Reference in New Issue