📄 expoperator.java
字号:
package cn.myapps.core.expimp.exp.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.ddlutils.Platform;
import org.apache.ddlutils.PlatformFactory;
import org.apache.ddlutils.io.DataWriter;
import org.apache.ddlutils.model.Database;
import org.apache.ddlutils.model.Table;
import org.apache.ddlutils.platform.oracle.Oracle8Platform;
public class ExpOperator {
private Platform platform;
private String schema;
private String dataPath = "f:/expdata.xml";
private String modelPath = "f:/expmodel.xml";
private Database model = null;
public ExpOperator(DataSource ds) {
if (ds != null) {
BasicDataSource dbcpDS = ((BasicDataSource) ds);
platform = PlatformFactory.createNewPlatformInstance(dbcpDS);
if (platform instanceof Oracle8Platform) {
schema = (dbcpDS.getUsername()).toUpperCase();
}
}
}
public void writeModelToXML() {
}
public void writeDataToXML(Map SQLOfTables) throws Exception {
Database model = getModel();
if (model == null) {
return;
}
File file = new File(dataPath);
OutputStream out = new FileOutputStream(file);
//DataWriter writer = new DataWriter(out, "gb2312");
DataWriter writer = new DataWriter(out);
writer.writeDocumentStart();
for (Iterator iter = SQLOfTables.values().iterator(); iter.hasNext();) {
SQLPackage info = (SQLPackage) iter.next();
String tableName = info.getTableName();
String sql = info.getSql();
Table table = this.getTableByName(model, tableName);
if (table != null) {
int count = 0;
Collection beans = null;
do {
int start = count * 3000;
int end = (++count) * 3000 - 1;
beans = platform.fetch(model, sql, new Table[] { table },
start, end);
resetColumnValue(beans, info.getResetColumn()); // 重置column
writer.write(beans);
} while (beans != null && beans.size() >= 3000);
}
}
writer.writeDocumentEnd();
out.close();
}
private Table getTableByName(Database model, String tableName) {
Table[] tables = model.getTables();
for (int i = 0; i < tables.length; i++) {
String name = tables[i].getName();
if (name.equalsIgnoreCase(tableName)) {
return tables[i];
}
}
return null;
}
private void resetColumnValue(Collection beans, Collection columnNames) {
String[] values = (String[]) columnNames.toArray(new String[columnNames
.size()]);
resetColumnValue(beans, values);
}
private void resetColumnValue(Collection beans, String[] columnNames) {
for (int i = 0; i < columnNames.length; i++) {
resetColumnValue(beans, columnNames[i]);
}
}
private void resetColumnValue(Collection beans, String columnName) {
for (Iterator iter = beans.iterator(); iter.hasNext();) {
DynaBean bean = (DynaBean) iter.next();
DynaProperty property = bean.getDynaClass().getDynaProperty(
columnName);
if (property != null) {
resetColumnValue(bean, columnName);
}
}
}
private void resetColumnValue(DynaBean dynaBean, String columnName) {
dynaBean.set(columnName, null);
}
private Database getModel() {
if (platform == null) {
return null;
}
if (model == null) {
if (schema != null && schema.trim().length() > 0) {
model = platform.readModelFromDatabase(" ", null, schema, null);
} else {
model = platform.readModelFromDatabase(" ");
}
}
return model;
}
public String getModelPath() {
return modelPath;
}
public void setModelPath(String modelPath) {
this.modelPath = modelPath;
}
public String getDataPath() {
return dataPath;
}
public void setDataPath(String dataPath) {
this.dataPath = dataPath;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -