Commit 173cb855 authored by Quxl's avatar Quxl

x

parent ae09ddfe
package com.egolm.common.jdbc;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import javax.sql.DataSource;
public class DBPort {
private DataSource dataSource;
public DBPort(DataSource dataSource) {
this.dataSource = dataSource;
}
public void doImport(File file, String tableName) throws IOException, SQLException, ClassNotFoundException {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
FileChannel channel = fis.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
int i = 1;
while(i > 0) {
i = channel.read(byteBuffer);
}
this.doImport(byteBuffer.array(), tableName);
} finally {
if(fis != null) {
fis.close();
}
}
}
public void doExport(File file, String sql, Object[] args) throws IOException, SQLException {
if(!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(file);
this.doExport(fos, sql, args);
}
public void doExport(OutputStream os, String sql, Object[] args) throws IOException, SQLException {
try {
os.write(this.doExport(sql, args));
os.flush();
} finally {
if(os != null) {
os.close();
}
}
}
public byte[] doExport(String sql, Object[] args) throws IOException, SQLException {
Connection connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
Object argObj = args[i];
if (argObj instanceof java.util.Date) {
java.util.Date argDate = (java.util.Date) argObj;
ps.setObject(i + 1, new java.sql.Timestamp(argDate.getTime()));
} else if (argObj.getClass().isEnum()) {
ps.setObject(i + 1, args[i].toString());
} else {
ps.setObject(i + 1, args[i]);
}
}
ResultSet resultSet = ps.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
ArrayList<Map<String, Map<String, Object>>> datas = new ArrayList<Map<String, Map<String, Object>>>();
while (resultSet.next()) {
Map<String, Map<String, Object>> rowData = new LinkedHashMap<String, Map<String, Object>>();
for (int i = 1; i <= columnCount; i++) {
String colType = metaData.getColumnTypeName(i);
String columnLabel = metaData.getColumnLabel(i);
if (columnLabel == null || columnLabel.trim().length() == 0) {
columnLabel = metaData.getColumnName(i);
}
Map<String, Object> map = new HashMap<String, Object>();
map.put("type", colType);
map.put("value", resultSet.getObject(i));
rowData.put(columnLabel, map);
}
datas.add(rowData);
}
ByteArrayOutputStream bos = null;
GZIPOutputStream gzip = null;
ObjectOutputStream out = null;
try {
bos = new ByteArrayOutputStream();
gzip = new GZIPOutputStream(bos);
out = new ObjectOutputStream(gzip);
out.writeObject(datas);
out.flush();
gzip.flush();
gzip.finish();
bos.flush();
return bos.toByteArray();
} finally {
if(out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void doImport(byte[] data, String tableName) throws IOException, ClassNotFoundException, SQLException {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
GZIPInputStream gzip = new GZIPInputStream(bis);
ObjectInputStream out = new ObjectInputStream(gzip);
List<?> datas = (List<?>)out.readObject();
StringBuffer sql = new StringBuffer("insert into ").append(tableName).append("(");
Object first = datas.get(0);
Map<?, ?> firstMap = (Map<?, ?>) first;
int columnIndex = 0;
for(Object keyObject : firstMap.keySet()) {
String columnName = (String)keyObject;
sql.append(columnName);
if(columnIndex < firstMap.size() - 1) {
sql.append(", ");
} else {
sql.append(") values (");
for(int k = 0; k < firstMap.size(); k++) {
sql.append("?");
if(k < firstMap.size() - 1) {
sql.append(", ");
} else {
sql.append(")");
}
}
}
columnIndex += 1;
}
Connection connection = dataSource.getConnection();
connection.setAutoCommit(false);
PreparedStatement ps = connection.prepareStatement(sql.toString());
for(int i = 0; i < datas.size(); i++) {
Object obj = datas.get(i);
Map<?, ?> map = (Map<?, ?>) obj;
int index = 0;
for(Object keyObject : map.keySet()) {
index += 1;
Object colObj = map.get(keyObject);
Map<?, ?> columnMap = (Map<?, ?>) colObj;
String columnType = (String) columnMap.get("type");
Object columnValue = columnMap.get("value");
if(columnType.startsWith("image") || columnType.startsWith("blob")) {
columnValue = new ByteArrayInputStream(Base64.getDecoder().decode((String)columnValue));
}
ps.setObject(index, columnValue);
}
ps.addBatch();
}
connection.commit();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment