Search in sources :

Example 6 with SQLFailureException

use of org.dbflute.exception.SQLFailureException in project dbflute-core by dbflute.

the class DfOutsideSqlTestTask method handleSqlFileFailure.

// ===================================================================================
// Exception Handling
// ==================
protected void handleSqlFileFailure(DfSqlFileFireResult fireResult, List<File> sqlFileList) {
    final SQLFailureException topCause = fireResult.getBreakCause();
    if (topCause != null) {
        throw topCause;
    }
    final List<DfSqlFileRunnerResult> resultList = fireResult.getRunnerResultList();
    for (DfSqlFileRunnerResult runnerResult : resultList) {
        final SQLFailureException elementCause = runnerResult.getBreakCause();
        if (elementCause != null) {
            throw elementCause;
        }
        final List<ErrorContinuedSql> continuedSqlList = runnerResult.getErrorContinuedSqlList();
        if (!continuedSqlList.isEmpty()) {
            throwOutsideSqlTestFailureFoundException();
        }
    }
}
Also used : ErrorContinuedSql(org.dbflute.helper.jdbc.sqlfile.DfSqlFileRunnerResult.ErrorContinuedSql) DfSqlFileRunnerResult(org.dbflute.helper.jdbc.sqlfile.DfSqlFileRunnerResult) SQLFailureException(org.dbflute.exception.SQLFailureException)

Example 7 with SQLFailureException

use of org.dbflute.exception.SQLFailureException in project dbflute-core by dbflute.

the class SQLExceptionHandler method throwSQLFailureException.

protected void throwSQLFailureException(SQLException e, SQLExceptionResource resource) {
    final ExceptionMessageBuilder br = createExceptionMessageBuilder();
    final List<String> noticeList = resource.getNoticeList();
    if (!noticeList.isEmpty()) {
        for (String notice : noticeList) {
            br.addNotice(notice);
        }
    } else {
        // as default
        br.addNotice("The SQL failed to execute.");
    }
    br.addItem("Advice");
    br.addElement("Read the SQLException message.");
    final String advice = askAdvice(e, ResourceContext.currentDBDef());
    if (advice != null && advice.trim().length() > 0) {
        br.addElement("*" + advice);
    }
    setupCommonElement(br, e, resource);
    final String msg = br.buildExceptionMessage();
    throw new SQLFailureException(msg, e);
}
Also used : ExceptionMessageBuilder(org.dbflute.helper.message.ExceptionMessageBuilder) SQLFailureException(org.dbflute.exception.SQLFailureException)

Example 8 with SQLFailureException

use of org.dbflute.exception.SQLFailureException in project dbflute-core by dbflute.

the class DfSqlFileFireMan method buildDetailMessage.

protected String buildDetailMessage(DfSqlFileFireResult fireResult) {
    final StringBuilder sb = new StringBuilder();
    final List<DfSqlFileRunnerResult> runnerResultList = fireResult.getRunnerResultList();
    for (DfSqlFileRunnerResult runnerResult : runnerResultList) {
        final List<ErrorContinuedSql> errorContinuedSqlList = runnerResult.getErrorContinuedSqlList();
        final String fileName = runnerResult.getSqlFile().getName();
        final SQLFailureException breakCause = runnerResult.getBreakCause();
        if (sb.length() > 0) {
            sb.append(ln());
        }
        if (breakCause != null) {
            // break by error
            sb.append("x ").append(fileName);
            sb.append(ln()).append(" >> (failed: Look at the exception message)");
        } else {
            // normal or error-continued
            if (errorContinuedSqlList.isEmpty()) {
                // OK or skipped
                sb.append(!runnerResult.isSkippedFile() ? "o " : "v ").append(fileName);
            } else {
                sb.append("x ").append(fileName);
                doBuildErrorContinuedMessage(sb, errorContinuedSqlList);
            }
        }
    }
    return sb.toString();
}
Also used : ErrorContinuedSql(org.dbflute.helper.jdbc.sqlfile.DfSqlFileRunnerResult.ErrorContinuedSql) SQLFailureException(org.dbflute.exception.SQLFailureException)

Example 9 with SQLFailureException

use of org.dbflute.exception.SQLFailureException in project dbflute-core by dbflute.

the class DfSqlFileRunnerBase method runTransaction.

// ===================================================================================
// Run Transaction
// ===============
public DfSqlFileRunnerResult runTransaction() {
    _goodSqlCount = 0;
    _totalSqlCount = 0;
    _skippedSqlCount = 0;
    if (_sqlFile == null) {
        String msg = "The attribute '_srcFile' should not be null.";
        throw new IllegalStateException(msg);
    }
    boolean skippedFile = false;
    String currentSql = null;
    BufferedReader br = null;
    try {
        br = new BufferedReader(newInputStreamReader());
        final List<String> sqlList = extractSqlList(br);
        setupConnection();
        setupStatement();
        int sqlNumber = 0;
        for (String sql : sqlList) {
            ++sqlNumber;
            currentSql = sql;
            if (sqlNumber == 1 && !isTargetFile(sql)) {
                // first SQL only
                skippedFile = true;
                break;
            }
            if (!isTargetSql(sql)) {
                continue;
            }
            ++_totalSqlCount;
            final String realSql = filterSql(sql);
            if (!_runInfo.isSuppressLoggingSql()) {
                traceSql(realSql);
            }
            execSQL(realSql);
        }
        rollbackOrCommit();
    } catch (SQLFailureException breakCause) {
        if (_runInfo.isBreakCauseThrow()) {
            throw breakCause;
        } else {
            _runnerResult.setGoodSqlCount(_goodSqlCount);
            _runnerResult.setTotalSqlCount(_totalSqlCount);
            _runnerResult.setBreakCause(breakCause);
            return _runnerResult;
        }
    } catch (SQLException e) {
        // here is for the exception except executing SQL
        // so it always does not continue
        throwSQLFailureException(currentSql, e);
    } finally {
        try {
            rollback();
        } catch (SQLException ignored) {
        }
        closeStatement();
        closeConnection();
        closeReader(br);
    }
    // re-calculate total count with skipped count
    _totalSqlCount = _totalSqlCount - _skippedSqlCount;
    traceResult(_goodSqlCount, _totalSqlCount);
    _runnerResult.setGoodSqlCount(_goodSqlCount);
    _runnerResult.setTotalSqlCount(_totalSqlCount);
    _runnerResult.setSkippedFile(skippedFile);
    return _runnerResult;
}
Also used : SQLException(java.sql.SQLException) BufferedReader(java.io.BufferedReader) SQLFailureException(org.dbflute.exception.SQLFailureException)

Example 10 with SQLFailureException

use of org.dbflute.exception.SQLFailureException in project dbflute-core by dbflute.

the class DfSqlFileRunnerBase method throwSQLFailureException.

// ===================================================================================
// Exception Handling
// ==================
protected void throwSQLFailureException(String sql, SQLException e) {
    final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
    br.addNotice("Failed to execute the SQL!");
    br.addItem("SQL File");
    br.addElement(_sqlFile);
    br.addItem("Executed SQL");
    br.addElement(sql);
    br.addItem("SQLState");
    br.addElement(e.getSQLState());
    br.addItem("ErrorCode");
    br.addElement(e.getErrorCode());
    br.addItem("SQLException");
    br.addElement(e.getClass().getName());
    br.addElement(extractMessage(e));
    final SQLException nextEx = e.getNextException();
    if (nextEx != null) {
        br.addItem("NextException");
        br.addElement(nextEx.getClass().getName());
        br.addElement(extractMessage(nextEx));
        final SQLException nextNextEx = nextEx.getNextException();
        if (nextNextEx != null) {
            br.addItem("NextNextException");
            br.addElement(nextNextEx.getClass().getName());
            br.addElement(extractMessage(nextNextEx));
        }
    }
    final String msg = br.buildExceptionMessage();
    throw new SQLFailureException(msg, e);
}
Also used : ExceptionMessageBuilder(org.dbflute.helper.message.ExceptionMessageBuilder) SQLException(java.sql.SQLException) SQLFailureException(org.dbflute.exception.SQLFailureException)

Aggregations

SQLFailureException (org.dbflute.exception.SQLFailureException)31 SQLException (java.sql.SQLException)19 Statement (java.sql.Statement)9 ResultSet (java.sql.ResultSet)7 LinkedHashMap (java.util.LinkedHashMap)5 ExceptionMessageBuilder (org.dbflute.helper.message.ExceptionMessageBuilder)5 ArrayList (java.util.ArrayList)4 Connection (java.sql.Connection)3 DatabaseMetaData (java.sql.DatabaseMetaData)3 DfTakeFinallyAssertionFailureException (org.dbflute.exception.DfTakeFinallyAssertionFailureException)3 DfProcedureMeta (org.dbflute.logic.jdbc.metadata.info.DfProcedureMeta)3 DfTableMeta (org.dbflute.logic.jdbc.metadata.info.DfTableMeta)3 File (java.io.File)2 Map (java.util.Map)2 ErrorContinuedSql (org.dbflute.helper.jdbc.sqlfile.DfSqlFileRunnerResult.ErrorContinuedSql)2 DfProcedureExtractor (org.dbflute.logic.jdbc.metadata.basic.DfProcedureExtractor)2 DfTableExtractor (org.dbflute.logic.jdbc.metadata.basic.DfTableExtractor)2 DfTakeFinallyProcess (org.dbflute.logic.replaceschema.process.DfTakeFinallyProcess)2 DfClassificationElement (org.dbflute.properties.assistant.classification.DfClassificationElement)2 DfClassificationJdbcCloser (org.dbflute.properties.assistant.classification.coins.DfClassificationJdbcCloser)2