void applyCustomizers(final StatementType stmt, Object[] args) { statementCustomizers.forEach(b -> { try { b.apply(stmt, args); } catch (SQLException e) { throw new UnableToExecuteStatementException(e, stmt.getContext()); } }); }
@Test public void testGlobalResourceGroupProperties() { H2ResourceGroupsDao dao = setup("global_properties"); dao.createResourceGroupsGlobalPropertiesTable(); dao.insertResourceGroupsGlobalProperties("cpu_quota_period", "1h"); ResourceGroupGlobalProperties globalProperties = new ResourceGroupGlobalProperties(Optional.of(Duration.valueOf("1h"))); ResourceGroupGlobalProperties records = dao.getResourceGroupGlobalProperties().get(0); assertEquals(globalProperties, records); try { dao.insertResourceGroupsGlobalProperties("invalid_property", "1h"); } catch (UnableToExecuteStatementException ex) { assertTrue(ex.getCause() instanceof JdbcSQLException); assertTrue(ex.getCause().getMessage().startsWith("Check constraint violation:")); } try { dao.updateResourceGroupsGlobalProperties("invalid_property_name"); } catch (UnableToExecuteStatementException ex) { assertTrue(ex.getCause() instanceof JdbcSQLException); assertTrue(ex.getCause().getMessage().startsWith("Check constraint violation:")); } }
public Optional<Dataset> get(@NonNull DatasetUrn urn) throws UnexpectedException { try { final Optional<DatasetRow> datasetRowIfFound = datasetDao.findBy(urn); return datasetRowIfFound.map(DatasetMapper::map); } catch (UnableToExecuteStatementException e) { log.error(e.getMessage()); throw new UnexpectedException(); } }
@Test public void testContextGetsBinding() { try { h.prepareBatch("insert into something (id, name) values (:id, :name)") .bind("id", 0) .bind("name", "alice") .add() .bind("id", 0) .bind("name", "bob") .add() .execute(); fail("expected exception"); } catch (UnableToExecuteStatementException e) { final StatementContext ctx = e.getStatementContext(); assertThat(ctx.getBinding().findForName("name", ctx).toString()).contains("bob"); } }
public List<Dataset> getAll( @NonNull Namespace namespace, @NonNull Integer limit, @NonNull Integer offset) throws UnexpectedException { try { final List<DatasetRow> datasetRows = datasetDao.findAll(namespace, limit, offset); return Collections.unmodifiableList(DatasetMapper.map(datasetRows)); } catch (UnableToExecuteStatementException e) { log.error(e.getMessage()); throw new UnexpectedException(); } } }
/** * @return the finalized {@link ParsedSql} object. */ public ParsedSql build() { if (positional && named) { throw new UnableToExecuteStatementException( "Cannot mix named and positional parameters in a SQL statement: " + parameterNames); } ParsedParameters parameters = new ParsedParameters(positional, parameterNames); return new ParsedSql(sql.toString(), parameters); } }
@Test public void testPreparedBatch() { PreparedBatch b = dbRule.openHandle().prepareBatch("insert into something (id, name) values (?,?)"); b.add(0, "a"); b.add(0, "a"); assertThatExceptionOfType(UnableToExecuteStatementException.class) .isThrownBy(b::execute) .satisfies(e -> assertSuppressions(e.getCause())); }
private void putTime(final IRI identifier, final Instant instant) { try { jdbi.useHandle(handle -> handle.execute("INSERT INTO memento (subject, moment) VALUES (?, ?)", identifier.getIRIString(), instant.getEpochSecond())); } catch (final UnableToExecuteStatementException ex) { LOGGER.debug("Unable to insert memento value: {}", ex.getMessage()); } } }
final void callCustomizers(StatementCustomizerInvocation invocation) { for (StatementCustomizer customizer : getCustomizers()) { try { invocation.call(customizer); } catch (SQLException e) { throw new UnableToExecuteStatementException("Exception thrown in statement customization", e, ctx); } } }
@Test public void testSimpleBatch() { Batch b = dbRule.openHandle().createBatch(); b.add("insert into something (id, name) values (0, 'Keith')"); b.add("insert into something (id, name) values (0, 'Keith')"); assertThatExceptionOfType(UnableToExecuteStatementException.class) .isThrownBy(b::execute) .satisfies(e -> assertSuppressions(e.getCause())); }
public Dataset create(@NonNull Namespace namespace, @NonNull DbTableVersion dbTableVersion) throws UnexpectedException { final DataSourceRow dataSourceRow = DataSourceRowMapper.map(dbTableVersion); final DatasetRow datasetRow = DatasetRowMapper.map(namespace, dataSourceRow, dbTableVersion); final DbTableInfoRow dbTableInfoRow = DbTableInfoRowMapper.map(dbTableVersion); final DbTableVersionRow dbTableVersionRow = DbTableVersionRowMapper.map(datasetRow, dbTableInfoRow, dbTableVersion); try { datasetDao.insertAll(dataSourceRow, datasetRow, dbTableInfoRow, dbTableVersionRow); final Optional<DatasetRow> datasetRowIfFound = datasetDao.findBy(datasetRow.getUuid()); return datasetRowIfFound.map(DatasetMapper::map).orElseThrow(UnexpectedException::new); } catch (UnableToExecuteStatementException e) { log.error(e.getMessage()); throw new UnexpectedException(); } }
throw new UnableToExecuteStatementException("Could not get OUT parameter from statement", e, getContext());
@Test public void testGlobalResourceGroupProperties() { H2ResourceGroupsDao dao = setup("global_properties"); dao.createResourceGroupsGlobalPropertiesTable(); dao.insertResourceGroupsGlobalProperties("cpu_quota_period", "1h"); ResourceGroupGlobalProperties globalProperties = new ResourceGroupGlobalProperties(Optional.of(Duration.valueOf("1h"))); ResourceGroupGlobalProperties records = dao.getResourceGroupGlobalProperties().get(0); assertEquals(globalProperties, records); try { dao.insertResourceGroupsGlobalProperties("invalid_property", "1h"); } catch (UnableToExecuteStatementException ex) { assertTrue(ex.getCause() instanceof JdbcSQLException); assertTrue(ex.getCause().getMessage().startsWith("Check constraint violation:")); } try { dao.updateResourceGroupsGlobalProperties("invalid_property_name"); } catch (UnableToExecuteStatementException ex) { assertTrue(ex.getCause() instanceof JdbcSQLException); assertTrue(ex.getCause().getMessage().startsWith("Check constraint violation:")); } }
throw new UnableToExecuteStatementException("Unable to configure JDBC statement", e, getContext()); return SqlLoggerUtil.wrap(stmt::executeBatch, getContext(), getConfig(SqlStatements.class).getSqlLogger()); } catch (SQLException e) { throw new UnableToExecuteStatementException(mungeBatchException(e), getContext());
@Test public void testGlobalResourceGroupProperties() { H2ResourceGroupsDao dao = setup("global_properties"); dao.createResourceGroupsGlobalPropertiesTable(); dao.insertResourceGroupsGlobalProperties("cpu_quota_period", "1h"); ResourceGroupGlobalProperties globalProperties = new ResourceGroupGlobalProperties(Optional.of(Duration.valueOf("1h"))); ResourceGroupGlobalProperties records = dao.getResourceGroupGlobalProperties().get(0); assertEquals(globalProperties, records); try { dao.insertResourceGroupsGlobalProperties("invalid_property", "1h"); } catch (UnableToExecuteStatementException ex) { assertTrue(ex.getCause() instanceof JdbcSQLException); assertTrue(ex.getCause().getMessage().startsWith("Check constraint violation:")); } try { dao.updateResourceGroupsGlobalProperties("invalid_property_name"); } catch (UnableToExecuteStatementException ex) { assertTrue(ex.getCause() instanceof JdbcSQLException); assertTrue(ex.getCause().getMessage().startsWith("Check constraint violation:")); } }
throw new UnableToExecuteStatementException("Exception while binding parameters", e, getContext()); throw new UnableToExecuteStatementException(Batch.mungeBatchException(e), getContext());
throw new UnableToExecuteStatementException(e, ctx);
final void beforeExecution(final PreparedStatement stmt) { for (StatementCustomizer customizer : customizers) { try { customizer.beforeExecution(stmt, ctx); } catch (SQLException e) { throw new UnableToExecuteStatementException("Exception thrown in statement customization", e, ctx); } } }
final void afterExecution(final PreparedStatement stmt) { for (StatementCustomizer customizer : customizers) { try { customizer.afterExecution(stmt, ctx); } catch (SQLException e) { throw new UnableToExecuteStatementException("Exception thrown in statement customization", e, ctx); } } }
/** * @return the finalized {@link ParsedSql} object. */ public ParsedSql build() { if (positional && named) { throw new UnableToExecuteStatementException( "Cannot mix named and positional parameters in a SQL statement: " + parameterNames); } ParsedParameters parameters = new ParsedParameters(positional, parameterNames); return new ParsedSql(sql.toString(), parameters); } }