use of com.varsql.core.data.importdata.handler.AbstractImportDataHandler in project varsql by varsqlinfo.
the class FileImportExecutor method execute.
@Override
public SQLExecuteResult execute(SqlStatementInfo statementInfo) throws SQLException {
SQLExecuteResult result = new SQLExecuteResult();
File importFile;
try {
importFile = ResourceUtils.getResource(String.valueOf(statementInfo.getCustom().get(IMPORT_FILE_PARAM_NAME))).getFile();
if (!importFile.exists() || importFile.length() == 0) {
result.setResultCode(VarsqlAppCode.EC_SQL_EXECUTOR);
result.setMessage(" import file size zero ");
return result;
}
} catch (IOException e1) {
result.setResultCode(VarsqlAppCode.EC_SQL_EXECUTOR);
result.setMessage(" error message : " + e1.getMessage());
return result;
}
result.setStartTime(System.currentTimeMillis());
Connection conn = ConnectionFactory.getInstance().getConnection(statementInfo.getVconnid());
final HandlerVariable handlerVariable = new HandlerVariable(conn);
try {
conn.setAutoCommit(false);
ImportData ijd = null;
if (VarsqlFileType.JSON.equals(statementInfo.getExportType())) {
ijd = new ImportJsonData(importFile);
} else if (VarsqlFileType.XML.equals(statementInfo.getExportType())) {
ijd = new ImportXmlData(importFile);
}
ijd.startImport(new AbstractImportDataHandler() {
boolean firstFlag = true;
ExportColumnInfo[] columnArr = null;
private PreparedStatement statement;
@Override
public void handler(Map rowInfo) throws SQLException {
if (firstFlag) {
statement = handlerVariable.getStatement(getSql());
firstFlag = false;
columnArr = getColumns().toArray(new ExportColumnInfo[0]);
}
for (int i = 0; i < columnArr.length; i++) {
ExportColumnInfo eci = columnArr[i];
// SQLDataType.getCode(eci.getType()).setParameter(this.statement,i + 1, rowInfo.get(eci.getAlias()));
statement.setObject(i + 1, rowInfo.get(eci.getAlias()));
}
statement.addBatch();
statement.clearParameters();
handlerVariable.addCount();
if (handlerVariable.getCount() % getBatchCount() == 0) {
statement.executeBatch();
statement.clearBatch();
}
}
});
if (handlerVariable.getCount() % getBatchCount() != 0) {
handlerVariable.getStatement().executeBatch();
handlerVariable.getStatement().clearBatch();
}
conn.commit();
} catch (Throwable e) {
if (conn != null)
conn.rollback();
result.setResultCode(VarsqlAppCode.EC_SQL_EXECUTOR);
result.setMessage("errorLine : " + handlerVariable.getCount() + ", error message : " + e.getMessage());
logger.error("update : {} ", e.getMessage(), e);
} finally {
if (conn != null) {
conn.setAutoCommit(true);
JdbcUtils.close(conn, handlerVariable.getStatement());
}
}
result.setTotalCount(handlerVariable.getCount());
result.setEndTime(System.currentTimeMillis());
result.setExecuteCount(handlerVariable.getCount());
result.setResult(handlerVariable.getCount());
return result;
}
Aggregations