Search in sources :

Example 6 with BadSqlGrammarException

use of org.springframework.jdbc.BadSqlGrammarException in project dhis2-core by dhis2.

the class JdbcEventAnalyticsManager method getEventCount.

@Override
public long getEventCount(EventQueryParams params) {
    String sql = "select count(psi) ";
    sql += getFromClause(params);
    sql += getWhereClause(params);
    long count = 0;
    try {
        log.debug("Analytics event count SQL: " + sql);
        if (params.analyzeOnly()) {
            executionPlanStore.addExecutionPlan(params.getExplainOrderId(), sql);
        } else {
            count = jdbcTemplate.queryForObject(sql, Long.class);
        }
    } catch (BadSqlGrammarException ex) {
        log.info(AnalyticsUtils.ERR_MSG_TABLE_NOT_EXISTING, ex);
    } catch (DataAccessResourceFailureException ex) {
        log.warn(E7131.getMessage(), ex);
        throw new QueryRuntimeException(E7131, ex);
    } catch (DataIntegrityViolationException ex) {
        log.warn(E7132.getMessage(), ex);
        throw new QueryRuntimeException(E7132, ex);
    }
    return count;
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) QueryRuntimeException(org.hisp.dhis.common.QueryRuntimeException) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 7 with BadSqlGrammarException

use of org.springframework.jdbc.BadSqlGrammarException in project spring-framework by spring-projects.

the class SQLErrorCodeSQLExceptionTranslatorTests method errorCodeTranslation.

@Test
public void errorCodeTranslation() {
    SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
    SQLException badSqlEx = new SQLException("", "", 1);
    BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", badSqlEx);
    assertThat(bsgex.getSql()).isEqualTo("SQL");
    assertThat((Object) bsgex.getSQLException()).isEqualTo(badSqlEx);
    SQLException invResEx = new SQLException("", "", 4);
    InvalidResultSetAccessException irsex = (InvalidResultSetAccessException) sext.translate("task", "SQL", invResEx);
    assertThat(irsex.getSql()).isEqualTo("SQL");
    assertThat((Object) irsex.getSQLException()).isEqualTo(invResEx);
    checkTranslation(sext, 5, DataAccessResourceFailureException.class);
    checkTranslation(sext, 6, DataIntegrityViolationException.class);
    checkTranslation(sext, 7, CannotAcquireLockException.class);
    checkTranslation(sext, 8, DeadlockLoserDataAccessException.class);
    checkTranslation(sext, 9, CannotSerializeTransactionException.class);
    checkTranslation(sext, 10, DuplicateKeyException.class);
    SQLException dupKeyEx = new SQLException("", "", 10);
    DataAccessException dksex = sext.translate("task", "SQL", dupKeyEx);
    assertThat(dksex).isInstanceOf(DataIntegrityViolationException.class);
    // Test fallback. We assume that no database will ever return this error code,
    // but 07xxx will be bad grammar picked up by the fallback SQLState translator
    SQLException sex = new SQLException("", "07xxx", 666666666);
    BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", sex);
    assertThat(bsgex2.getSql()).isEqualTo("SQL2");
    assertThat((Object) bsgex2.getSQLException()).isEqualTo(sex);
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) SQLException(java.sql.SQLException) InvalidResultSetAccessException(org.springframework.jdbc.InvalidResultSetAccessException) DataAccessException(org.springframework.dao.DataAccessException) DeadlockLoserDataAccessException(org.springframework.dao.DeadlockLoserDataAccessException) Test(org.junit.jupiter.api.Test)

Example 8 with BadSqlGrammarException

use of org.springframework.jdbc.BadSqlGrammarException in project spring-framework by spring-projects.

the class SQLErrorCodeSQLExceptionTranslatorTests method batchExceptionTranslation.

@Test
public void batchExceptionTranslation() {
    SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
    SQLException badSqlEx = new SQLException("", "", 1);
    BatchUpdateException batchUpdateEx = new BatchUpdateException();
    batchUpdateEx.setNextException(badSqlEx);
    BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", batchUpdateEx);
    assertThat(bsgex.getSql()).isEqualTo("SQL");
    assertThat((Object) bsgex.getSQLException()).isEqualTo(badSqlEx);
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) SQLException(java.sql.SQLException) BatchUpdateException(java.sql.BatchUpdateException) Test(org.junit.jupiter.api.Test)

Example 9 with BadSqlGrammarException

use of org.springframework.jdbc.BadSqlGrammarException in project uPortal by Jasig.

the class DataSourceSchemaExport method perform.

private void perform(String[] sqlCommands, boolean executeSql, String outputFile, boolean append, boolean failFast) {
    final PrintWriter sqlWriter = getSqlWriter(outputFile, append);
    try {
        for (final String sqlCommand : sqlCommands) {
            final String formatted = formatter.format(sqlCommand);
            sqlWriter.println(formatted);
            if (executeSql) {
                try {
                    jdbcOperations.execute(sqlCommand);
                    logger.info(sqlCommand);
                } catch (BadSqlGrammarException | UncategorizedSQLException e) {
                    // https://hibernate.atlassian.net/browse/HHH-7002.
                    if (sqlCommand.contains("drop constraint")) {
                        logger.info("Failed to execute (probably ignorable): {}, error message {}", sqlCommand, e.getMessage());
                    } else {
                        handleSqlException(failFast, sqlCommand, e);
                    }
                } catch (Exception e) {
                    handleSqlException(failFast, sqlCommand, e);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(sqlWriter);
    }
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) DataAccessException(org.springframework.dao.DataAccessException) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) IOException(java.io.IOException) BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) PrintWriter(java.io.PrintWriter)

Example 10 with BadSqlGrammarException

use of org.springframework.jdbc.BadSqlGrammarException in project dhis2-core by dhis2.

the class JdbcEnrollmentAnalyticsManager method getAggregatedEventData.

// -------------------------------------------------------------------------
// EnrollmentAnalyticsManager implementation
// -------------------------------------------------------------------------
@Override
public Grid getAggregatedEventData(EventQueryParams params, Grid grid, int maxLimit) {
    // ---------------------------------------------------------------------
    // Select
    // ---------------------------------------------------------------------
    String countClause = getAggregateClause(params);
    String sql = "select " + countClause + " as value," + StringUtils.join(getSelectColumns(params), ",") + " ";
    // ---------------------------------------------------------------------
    // Criteria
    // ---------------------------------------------------------------------
    sql += getFromWhereClause(params, Lists.newArrayList("psi"));
    // ---------------------------------------------------------------------
    // Group by
    // ---------------------------------------------------------------------
    sql += "group by " + StringUtils.join(getSelectColumns(params), ",") + " ";
    if (params.hasSortOrder()) {
        sql += "order by value " + params.getSortOrder().toString().toLowerCase() + " ";
    }
    if (params.hasLimit()) {
        sql += "limit " + params.getLimit();
    } else if (maxLimit > 0) {
        sql += "limit " + (maxLimit + 1);
    }
    try {
        getAggregatedEventData(grid, params, sql);
    } catch (BadSqlGrammarException ex) {
        log.info(QUERY_ERR_MSG, ex);
    }
    return grid;
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) DateUtils.getMediumDateString(org.hisp.dhis.system.util.DateUtils.getMediumDateString)

Aggregations

BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)16 SQLException (java.sql.SQLException)7 DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)6 QueryRuntimeException (org.hisp.dhis.common.QueryRuntimeException)4 DateUtils.getMediumDateString (org.hisp.dhis.system.util.DateUtils.getMediumDateString)4 Test (org.junit.jupiter.api.Test)4 DataAccessException (org.springframework.dao.DataAccessException)4 TextUtils.getQuotedCommaDelimitedString (org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString)3 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)3 TransientDataAccessResourceException (org.springframework.dao.TransientDataAccessResourceException)3 BatchUpdateException (java.sql.BatchUpdateException)2 DateUtils.getMediumDateString (org.hisp.dhis.util.DateUtils.getMediumDateString)2 DeadlockLoserDataAccessException (org.springframework.dao.DeadlockLoserDataAccessException)2 PermissionDeniedDataAccessException (org.springframework.dao.PermissionDeniedDataAccessException)2 InvalidResultSetAccessException (org.springframework.jdbc.InvalidResultSetAccessException)2 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)2 Async (org.springframework.scheduling.annotation.Async)2 AsyncResult (org.springframework.scheduling.annotation.AsyncResult)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1