public void save(String project, ABTestingReport report) { if (report.id != -1) { throw new RakamException("Report already has an id.", HttpResponseStatus.BAD_REQUEST); } try (Handle handle = dbi.open()) { handle.createStatement("INSERT INTO ab_testing (project, name, variants, collection_name, connector_field, goals, options)" + " VALUES (:project, :name, :variants, :collection_name, :goals, :options)") .bind("project", project) .bind("name", report.name) .bind("collection_name", report.collectionName) .bind("variants", JsonHelper.encode(report.variants)) .bind("goals", JsonHelper.encode(ImmutableList.of(report.goal))) .bind("options", JsonHelper.encode(report.options, false)) .execute(); } catch (UnableToExecuteStatementException e) { if (e.getCause() instanceof SQLException && ((SQLException) e.getCause()).getSQLState().equals("23505")) { // TODO: replace throw new RakamException("Report already exists", HttpResponseStatus.BAD_REQUEST); } } }
public <T> T catchConflict(NewResourceAction<T> function, String messageFormat, Object... messageParameters) throws ResourceConflictException { try { return function.call(); } catch (UnableToExecuteStatementException ex) { if (ex.getCause() instanceof SQLException) { SQLException sqlEx = (SQLException) ex.getCause(); if (isConflictException(sqlEx)) { throw new ResourceConflictException("Resource already exists: " + String.format(messageFormat, messageParameters)); } } throw ex; } }
public <T> T catchForeignKeyNotFound(NewResourceAction<T> function, String messageFormat, Object... messageParameters) throws ResourceNotFoundException, ResourceConflictException { try { return function.call(); } catch (UnableToExecuteStatementException ex) { if (ex.getCause() instanceof SQLException) { SQLException sqlEx = (SQLException) ex.getCause(); if (isForeignKeyException(sqlEx)) { throw new ResourceNotFoundException("Resource not found: " + String.format(messageFormat, messageParameters)); } } throw ex; } }
@Test(groups = "slow") public void testWithoutArgument() throws IOException { final SomethingSqlDao somethingSqlDao = dbi.onDemand(SomethingSqlDao.class); try { somethingSqlDao.create(1, "pierre", new LocalDate(DateTimeZone.UTC)); Assert.fail(); } catch (final UnableToExecuteStatementException e) { Assert.assertTrue(e.getCause() instanceof JdbcSQLException); } }
@Test(groups = "slow") public void testWithoutArgument() throws IOException { final SomethingSqlDao somethingSqlDao = dbi.onDemand(SomethingSqlDao.class); try { somethingSqlDao.create(1, "pierre", new DateTime(DateTimeZone.UTC)); Assert.fail(); } catch (final UnableToExecuteStatementException e) { Assert.assertTrue(e.getCause() instanceof JdbcSQLException); } }
@Test(groups = "slow") public void testWithoutArgument() throws IOException { final SomethingSqlDao somethingSqlDao = dbi.onDemand(SomethingSqlDao.class); try { somethingSqlDao.create(1, "pierre", new LocalDate(DateTimeZone.UTC)); Assert.fail(); } catch (final UnableToExecuteStatementException e) { Assert.assertTrue(e.getCause() instanceof JdbcSQLException); } }
@Test(groups = "slow") public void testWithoutArgument() throws IOException { final SomethingSqlDao somethingSqlDao = dbi.onDemand(SomethingSqlDao.class); try { somethingSqlDao.create(1, "pierre", new DateTime(DateTimeZone.UTC)); Assert.fail(); } catch (final UnableToExecuteStatementException e) { Assert.assertTrue(e.getCause() instanceof JdbcSQLException); } }
@Test(groups = "slow") public void testWithoutArgument() throws IOException { final SomethingSqlDao somethingSqlDao = dbi.onDemand(SomethingSqlDao.class); try { somethingSqlDao.create(1, "pierre", DateTimeZone.UTC); Assert.fail(); } catch (final UnableToExecuteStatementException e) { Assert.assertTrue(e.getCause() instanceof JdbcSQLException); } }
@Test(groups = "slow") public void testWithoutArgument() throws IOException { final SomethingSqlDao somethingSqlDao = dbi.onDemand(SomethingSqlDao.class); try { somethingSqlDao.create(1, "pierre", Bier.ipa); Assert.fail(); } catch (final UnableToExecuteStatementException e) { Assert.assertTrue(e.getCause() instanceof JdbcSQLException); } }
@Test(groups = "slow") public void testWithoutArgument() throws IOException { final SomethingSqlDao somethingSqlDao = dbi.onDemand(SomethingSqlDao.class); try { somethingSqlDao.create(1, "pierre", DateTimeZone.UTC); Assert.fail(); } catch (final UnableToExecuteStatementException e) { Assert.assertTrue(e.getCause() instanceof JdbcSQLException); } }
public GitInfo upsert(GitInfo gitInfo) { Optional<GitInfo> existing = getByRepositoryAndBranch(gitInfo.getRepositoryId(), gitInfo.getBranch()); if (existing.isPresent()) { gitInfo = gitInfo.withId(existing.get().getId().get()); if (!existing.get().equals(gitInfo)) { int updated = branchDao.update(gitInfo); Preconditions.checkState(updated == 1, "Expected to update 1 row but updated %s", updated); } return gitInfo; } else { try { int id = branchDao.insert(gitInfo); return gitInfo.withId(id); } catch (UnableToExecuteStatementException e) { if (e.getCause() instanceof SQLIntegrityConstraintViolationException) { return getByRepositoryAndBranch(gitInfo.getRepositoryId(), gitInfo.getBranch()).get(); } else { throw e; } } } }
@Test public void testExternalBatches() { assertFalse(dao.externalBatchExists("foo")); assertFalse(dao.externalBatchExists("bar")); dao.insertExternalBatch("foo"); assertTrue(dao.externalBatchExists("foo")); assertFalse(dao.externalBatchExists("bar")); try { dao.insertExternalBatch("foo"); fail("expected exception"); } catch (UnableToExecuteStatementException e) { assertInstanceOf(e.getCause(), SQLException.class); assertTrue(((SQLException) e.getCause()).getSQLState().startsWith("23")); } }