Search in sources :

Example 1 with AbstractImportDataHandler

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;
}
Also used : ImportXmlData(com.varsql.core.data.importdata.ImportXmlData) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) ImportJsonData(com.varsql.core.data.importdata.ImportJsonData) ExportColumnInfo(com.varsql.core.sql.beans.ExportColumnInfo) ImportData(com.varsql.core.data.importdata.ImportData) AbstractImportDataHandler(com.varsql.core.data.importdata.handler.AbstractImportDataHandler) File(java.io.File) Map(java.util.Map)

Aggregations

ImportData (com.varsql.core.data.importdata.ImportData)1 ImportJsonData (com.varsql.core.data.importdata.ImportJsonData)1 ImportXmlData (com.varsql.core.data.importdata.ImportXmlData)1 AbstractImportDataHandler (com.varsql.core.data.importdata.handler.AbstractImportDataHandler)1 ExportColumnInfo (com.varsql.core.sql.beans.ExportColumnInfo)1 File (java.io.File)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 Map (java.util.Map)1