/** * Execute a SQL statement, and return the number of rows affected by the statement. * * @param sql the SQL statement to execute, using positional parameters (if any) * @param args positional arguments * * @return the number of rows affected */ public int execute(String sql, Object... args) { Update stmt = createUpdate(sql); int position = 0; for (Object arg : args) { stmt.bind(position++, arg); } return stmt.execute(); }
@Test public void testUpdate() { Handle h = dbRule.openHandle(); Update insert = h.createUpdate("insert into something_else (name) values (:name)"); insert.bind("name", "Brian"); Long id1 = insert.executeAndReturnGeneratedKeys().mapTo(long.class).findOnly(); assertThat(id1).isNotNull(); Update update = h.createUpdate("update something_else set name = :name where id = :id"); update.bind("id", id1); update.bind("name", "Tom"); Optional<Long> id2 = update.executeAndReturnGeneratedKeys().mapTo(long.class).findFirst(); assertThat(id2.isPresent()).isFalse(); }
/** * Executes the update, returning the result obtained from the given {@link ResultProducer}. * * @param <R> the result type * @param producer the result producer. * @return value returned by the result producer. */ public <R> R execute(ResultProducer<R> producer) { try { return producer.produce(this::internalExecute, getContext()); } catch (SQLException e) { try { close(); } catch (Exception e1) { e.addSuppressed(e1); } throw new UnableToProduceResultException("Could not produce statement result", e, getContext()); } }
@Test public void testUuidArrayList() { assertThat( h.createUpdate(U_INSERT) .bindByType("u", new ArrayList<>(Arrays.asList(testUuids)), UUID_LIST) .execute()) .isEqualTo(1); assertThat( h.createQuery(U_SELECT) .mapTo(UUID_ARRAYLIST) .findOnly()) .containsExactly(testUuids); }
/** * Executes the statement, returning the update count. * * @return the number of rows modified */ public int execute() { return execute(returningUpdateCount()); }
@Test public void testUsefulArgumentOutputForDebug() { expectedException.expect(StatementException.class); expectedException.expectMessage("arguments:{positional:{7:8}, named:{name:brian}, finder:[{one=two},{lazy bean property arguments \"java.lang.Object"); h.createUpdate("insert into something (id, name) values (:id, :name)") .bind("name", "brian") .bind(7, 8) .bindMap(new HandyMapThing<String>().add("one", "two")) .bindBean(new Object()) .execute(); }
dbRule.getJdbi().useHandle(handle -> { handle.createUpdate("INSERT INTO nvarchars (id, name) VALUES (?, ?)") .bind(0, 1) .bindNVarchar(1, "foo") .execute(); .bind("id", 2) .bindNVarchar("name", "bar") .execute(); .bind(0, 3) .bindByType(1, "baz", NVARCHAR_STRING) .execute(); .bind("id", 4) .bindByType("name", "qux", NVARCHAR_STRING) .execute();
@Test public void testRegisterOnStatement() { dbRule.getSharedHandle().createUpdate("insert into something (id, name) values (:id, :name)") .registerArgument(new NameAF()) .bind("id", 1) .bind("name", new Name("Brian", "McCallister")) .execute(); }
@Test public void testWritesViaFluentApi() { handle.createUpdate("insert into campaigns(id, caps) values (:id, :caps)") .bind("id", 3) .bindByType("caps", caps, QualifiedType.of(STRING_MAP).with(HStore.class)) .execute(); Map<String, String> newCaps = handle.createQuery("select caps from campaigns where id=?") .bind(0, 3) .mapTo(QualifiedType.of(STRING_MAP).with(HStore.class)) .findOnly(); assertThat(newCaps).isEqualTo(caps); }
public SqlUpdateHandler(Class<?> sqlObjectType, Method method) { super(sqlObjectType, method); if (method.isAnnotationPresent(UseRowReducer.class)) { throw new UnsupportedOperationException("Cannot declare @UseRowReducer on a @SqlUpdate method."); } boolean isGetGeneratedKeys = method.isAnnotationPresent(GetGeneratedKeys.class); QualifiedType<?> returnType = QualifiedType.of( GenericTypes.resolveType(method.getGenericReturnType(), sqlObjectType)) .with(getQualifiers(method)); if (isGetGeneratedKeys) { ResultReturner magic = ResultReturner.forMethod(sqlObjectType, method); String[] columnNames = method.getAnnotation(GetGeneratedKeys.class).value(); this.returner = update -> { ResultBearing resultBearing = update.executeAndReturnGeneratedKeys(columnNames); UseRowMapper useRowMapper = method.getAnnotation(UseRowMapper.class); ResultIterable<?> iterable = useRowMapper == null ? resultBearing.mapTo(returnType) : resultBearing.map(rowMapperFor(useRowMapper)); return magic.mappedResult(iterable, update.getContext()); }; } else if (isNumeric(method.getReturnType())) { this.returner = update -> update.execute(); } else if (isBoolean(method.getReturnType())) { this.returner = update -> update.execute() > 0; } else { throw new UnableToCreateSqlObjectException(invalidReturnTypeMessage(method, returnType)); } }
@Test public void testCascadedLazyArgs() { Handle h = dbRule.openHandle(); Update s = h.createUpdate("insert into something (id, name) values (:id, :name)"); Map<String, Object> args = new HashMap<>(); args.put("id", 0); s.bindMap(args); s.bindBean(new Keith()); int insertCount = s.execute(); assertThat(insertCount).isEqualTo(1); Something something = h.createQuery("select id, name from something").mapToBean(Something.class).findOnly(); assertThat(something).isEqualTo(new Something(0, "Keith")); }
@Test public void testUuidList() { assertThat( h.createUpdate(U_INSERT) .bindByType("u", Arrays.asList(testUuids), UUID_LIST) .execute()) .isEqualTo(1); assertThat( h.createQuery(U_SELECT) .mapTo(UUID_LIST) .findOnly()) .containsExactly(testUuids); }
@Override public C saveWithParentContentTree(C contentNode, UUID parentContentTreeUuid) { identifiableRepository.save(contentNode); dbi.withHandle(h -> h.createUpdate("INSERT INTO contentnodes(uuid) VALUES (:uuid)") .bindBean(contentNode) .execute()); Integer sortIndex = selectNextSortIndexForParentChildren(dbi, "contenttree_contentnode", "contenttree_uuid", parentContentTreeUuid); dbi.withHandle(h -> h.createUpdate( "INSERT INTO contenttree_contentnode(contenttree_uuid, contentnode_uuid, sortIndex)" + " VALUES (:parent_contenttree_uuid, :uuid, :sortIndex)") .bind("parent_contenttree_uuid", parentContentTreeUuid) .bind("sortIndex", sortIndex) .bindBean(contentNode) .execute()); return findOne(contentNode.getUuid()); }
@Test public void testMapToValueTypeFromColumnMapperFactory() { h.createUpdate("insert into someBean (valueType) values ('foo')").execute(); List<ValueType> list = dao.listValueTypesFactoryMapped(); assertThat(list).containsExactly(ValueType.valueOf("foo")); }
/** * Executes the update, returning the result obtained from the given {@link ResultProducer}. * * @param <R> the result type * @param producer the result producer. * @return value returned by the result producer. */ public <R> R execute(ResultProducer<R> producer) { try { return producer.produce(this::internalExecute, getContext()); } catch (SQLException e) { try { close(); } catch (Exception e1) { e.addSuppressed(e1); } throw new UnableToProduceResultException("Could not produce statement result", e, getContext()); } }