Search in sources :

Example 11 with BadSqlGrammarException

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

the class JdbcEventAnalyticsManager method getEvents.

@Override
public Grid getEvents(EventQueryParams params, Grid grid, int maxLimit) {
    List<String> fixedCols = Lists.newArrayList("psi", "ps", "executiondate", "longitude", "latitude", "ouname", "oucode");
    List<String> selectCols = ListUtils.distinctUnion(fixedCols, getSelectColumns(params));
    String sql = "select " + StringUtils.join(selectCols, ",") + " ";
    // ---------------------------------------------------------------------
    // Criteria
    // ---------------------------------------------------------------------
    sql += getFromWhereClause(params, fixedCols);
    if (params.isSorting()) {
        sql += "order by ";
        for (DimensionalItemObject item : params.getAsc()) {
            sql += statementBuilder.columnQuote(item.getUid()) + " asc,";
        }
        for (DimensionalItemObject item : params.getDesc()) {
            sql += statementBuilder.columnQuote(item.getUid()) + " desc,";
        }
        sql = removeLastComma(sql) + " ";
    }
    if (params.isPaging()) {
        sql += "limit " + params.getPageSizeWithDefault() + " offset " + params.getOffset();
    } else if (maxLimit > 0) {
        sql += "limit " + (maxLimit + 1);
    }
    try {
        getEvents(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)

Example 12 with BadSqlGrammarException

use of org.springframework.jdbc.BadSqlGrammarException in project syncope by apache.

the class SchedulerDBInit method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    Assert.state(this.dataSource != null, "DataSource must be set");
    Assert.state(this.databasePopulator != null, "DatabasePopulator must be set");
    JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
    boolean existingData;
    try {
        existingData = jdbcTemplate.queryForObject("SELECT COUNT(0) FROM QRTZ_SCHEDULER_STATE", Integer.class) > 0;
    } catch (BadSqlGrammarException e) {
        LOG.debug("Could not access to table QRTZ_SCHEDULER_STATE", e);
        existingData = false;
    }
    if (existingData) {
        LOG.info("Quartz tables found in the database, leaving untouched");
    } else {
        LOG.info("No Quartz tables found, creating");
        DatabasePopulatorUtils.execute(databasePopulator, this.dataSource);
    }
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 13 with BadSqlGrammarException

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

the class CustomSQLExceptionTranslatorRegistrarTests method customErrorCodeTranslation.

@Test
@SuppressWarnings("resource")
public void customErrorCodeTranslation() {
    new ClassPathXmlApplicationContext("test-custom-translators-context.xml", CustomSQLExceptionTranslatorRegistrarTests.class);
    SQLErrorCodes codes = SQLErrorCodesFactory.getInstance().getErrorCodes("H2");
    SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator();
    sext.setSqlErrorCodes(codes);
    DataAccessException exFor4200 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 42000));
    assertThat(exFor4200).as("Should have been translated").isNotNull();
    assertThat(BadSqlGrammarException.class.isAssignableFrom(exFor4200.getClass())).as("Should have been instance of BadSqlGrammarException").isTrue();
    DataAccessException exFor2 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 2));
    assertThat(exFor2).as("Should have been translated").isNotNull();
    assertThat(TransientDataAccessResourceException.class.isAssignableFrom(exFor2.getClass())).as("Should have been instance of TransientDataAccessResourceException").isTrue();
    DataAccessException exFor3 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 3));
    assertThat(exFor3).as("Should not have been translated").isNull();
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) TransientDataAccessResourceException(org.springframework.dao.TransientDataAccessResourceException) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) SQLException(java.sql.SQLException) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.jupiter.api.Test)

Example 14 with BadSqlGrammarException

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

the class SQLExceptionSubclassTranslatorTests method errorCodeTranslation.

@Test
public void errorCodeTranslation() {
    SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
    SQLException dataIntegrityViolationEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 0);
    DataIntegrityViolationException divex = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx);
    assertThat(divex.getCause()).isEqualTo(dataIntegrityViolationEx);
    SQLException featureNotSupEx = SQLExceptionSubclassFactory.newSQLFeatureNotSupportedException("", "", 0);
    InvalidDataAccessApiUsageException idaex = (InvalidDataAccessApiUsageException) sext.translate("task", "SQL", featureNotSupEx);
    assertThat(idaex.getCause()).isEqualTo(featureNotSupEx);
    SQLException dataIntegrityViolationEx2 = SQLExceptionSubclassFactory.newSQLIntegrityConstraintViolationException("", "", 0);
    DataIntegrityViolationException divex2 = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx2);
    assertThat(divex2.getCause()).isEqualTo(dataIntegrityViolationEx2);
    SQLException permissionDeniedEx = SQLExceptionSubclassFactory.newSQLInvalidAuthorizationSpecException("", "", 0);
    PermissionDeniedDataAccessException pdaex = (PermissionDeniedDataAccessException) sext.translate("task", "SQL", permissionDeniedEx);
    assertThat(pdaex.getCause()).isEqualTo(permissionDeniedEx);
    SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLNonTransientConnectionException("", "", 0);
    DataAccessResourceFailureException darex = (DataAccessResourceFailureException) sext.translate("task", "SQL", dataAccessResourceEx);
    assertThat(darex.getCause()).isEqualTo(dataAccessResourceEx);
    SQLException badSqlEx2 = SQLExceptionSubclassFactory.newSQLSyntaxErrorException("", "", 0);
    BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", badSqlEx2);
    assertThat(bsgex2.getSql()).isEqualTo("SQL2");
    assertThat((Object) bsgex2.getSQLException()).isEqualTo(badSqlEx2);
    SQLException tranRollbackEx = SQLExceptionSubclassFactory.newSQLTransactionRollbackException("", "", 0);
    ConcurrencyFailureException cfex = (ConcurrencyFailureException) sext.translate("task", "SQL", tranRollbackEx);
    assertThat(cfex.getCause()).isEqualTo(tranRollbackEx);
    SQLException transientConnEx = SQLExceptionSubclassFactory.newSQLTransientConnectionException("", "", 0);
    TransientDataAccessResourceException tdarex = (TransientDataAccessResourceException) sext.translate("task", "SQL", transientConnEx);
    assertThat(tdarex.getCause()).isEqualTo(transientConnEx);
    SQLException transientConnEx2 = SQLExceptionSubclassFactory.newSQLTimeoutException("", "", 0);
    QueryTimeoutException tdarex2 = (QueryTimeoutException) sext.translate("task", "SQL", transientConnEx2);
    assertThat(tdarex2.getCause()).isEqualTo(transientConnEx2);
    SQLException recoverableEx = SQLExceptionSubclassFactory.newSQLRecoverableException("", "", 0);
    RecoverableDataAccessException rdaex2 = (RecoverableDataAccessException) sext.translate("task", "SQL", recoverableEx);
    assertThat(rdaex2.getCause()).isEqualTo(recoverableEx);
    // Test classic error code translation. We should move there next if the exception we pass in is not one
    // of the new sub-classes.
    SQLException sexEct = new SQLException("", "", 1);
    BadSqlGrammarException bsgEct = (BadSqlGrammarException) sext.translate("task", "SQL-ECT", sexEct);
    assertThat(bsgEct.getSql()).isEqualTo("SQL-ECT");
    assertThat((Object) bsgEct.getSQLException()).isEqualTo(sexEct);
    // 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 sexFbt = new SQLException("", "07xxx", 666666666);
    BadSqlGrammarException bsgFbt = (BadSqlGrammarException) sext.translate("task", "SQL-FBT", sexFbt);
    assertThat(bsgFbt.getSql()).isEqualTo("SQL-FBT");
    assertThat((Object) bsgFbt.getSQLException()).isEqualTo(sexFbt);
    // and 08xxx will be data resource failure (non-transient) picked up by the fallback SQLState translator
    SQLException sexFbt2 = new SQLException("", "08xxx", 666666666);
    DataAccessResourceFailureException darfFbt = (DataAccessResourceFailureException) sext.translate("task", "SQL-FBT2", sexFbt2);
    assertThat(darfFbt.getCause()).isEqualTo(sexFbt2);
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) TransientDataAccessResourceException(org.springframework.dao.TransientDataAccessResourceException) SQLException(java.sql.SQLException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) PermissionDeniedDataAccessException(org.springframework.dao.PermissionDeniedDataAccessException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) QueryTimeoutException(org.springframework.dao.QueryTimeoutException) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) RecoverableDataAccessException(org.springframework.dao.RecoverableDataAccessException) Test(org.junit.jupiter.api.Test)

Example 15 with BadSqlGrammarException

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

the class JdbcAnalyticsManager method getAggregatedDataValues.

// -------------------------------------------------------------------------
// AnalyticsManager implementation
// -------------------------------------------------------------------------
@Override
@Async
public Future<Map<String, Object>> getAggregatedDataValues(DataQueryParams params, AnalyticsTableType tableType, int maxLimit) {
    assertQuery(params);
    try {
        ListMap<DimensionalItemObject, DimensionalItemObject> dataPeriodAggregationPeriodMap = params.getDataPeriodAggregationPeriodMap();
        if (params.isDisaggregation() && params.hasDataPeriodType()) {
            params = DataQueryParams.newBuilder(params).withDataPeriodsForAggregationPeriods(dataPeriodAggregationPeriodMap).build();
            params = queryPlanner.assignPartitionsFromQueryPeriods(params, tableType);
        }
        String sql = getSelectClause(params);
        sql += getFromClause(params);
        sql += getWhereClause(params, tableType);
        sql += getGroupByClause(params);
        if (params.hasMeasureCriteria() && params.isDataType(DataType.NUMERIC)) {
            sql += getMeasureCriteriaSql(params);
        }
        log.debug(sql);
        if (params.analyzeOnly()) {
            executionPlanStore.addExecutionPlan(params.getExplainOrderId(), sql);
            return new AsyncResult<>(Maps.newHashMap());
        }
        Map<String, Object> map;
        try {
            map = getKeyValueMap(params, sql, maxLimit);
        } catch (BadSqlGrammarException ex) {
            log.info(AnalyticsUtils.ERR_MSG_TABLE_NOT_EXISTING, ex);
            return new AsyncResult<>(Maps.newHashMap());
        }
        replaceDataPeriodsWithAggregationPeriods(map, params, dataPeriodAggregationPeriodMap);
        return new AsyncResult<>(map);
    } catch (DataAccessResourceFailureException ex) {
        log.warn(ErrorCode.E7131.getMessage(), ex);
        throw new QueryRuntimeException(ErrorCode.E7131, ex);
    } catch (RuntimeException ex) {
        log.error(DebugUtils.getStackTrace(ex));
        throw ex;
    }
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) QueryRuntimeException(org.hisp.dhis.common.QueryRuntimeException) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) QueryRuntimeException(org.hisp.dhis.common.QueryRuntimeException) DimensionalObject(org.hisp.dhis.common.DimensionalObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString) AsyncResult(org.springframework.scheduling.annotation.AsyncResult) Async(org.springframework.scheduling.annotation.Async)

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