@Test public void testBean() { handle.createUpdate(INSERT_NAMED).bindBean(new StringBean("herp")).execute(); assertThat(named).isEqualTo("herp"); }
private void insert(String binding, Object bean) { dbRule.getJdbi().useHandle(h -> { String insert = String.format("INSERT INTO test VALUES(:id, :%s)", binding); h.createUpdate(insert).bindBean(bean).execute(); }); }
@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 testBeanPropertyPrefixBinding() { Handle h = dbRule.openHandle(); Something original = new Something(0, "Keith"); assertThat(h .createUpdate("insert into something (id, name) values (:my.id, :my.name)") .bindBean("my", original) .execute()) .isEqualTo(1); assertThat(h .select("select * from something where id = ?", original.getId()) .mapToBean(Something.class) .findOnly()) .isEqualTo(original); }
@Test public void testFluentApiWithNesting() { jdbi.useHandle(h -> { h.execute("create table bean (id serial primary key, nested1 json, nested2 json)"); assertThat(h.createUpdate("insert into bean(id, nested1, nested2) values (:id, :nested1, :nested2)") .bindBean(new NestedJsonBean(42, 64, "quux")) .execute()).isEqualTo(1); NestedJsonBean beany = h.createQuery("select * from bean") .mapToBean(NestedJsonBean.class) .findOnly(); assertThat(beany.getId()).isEqualTo(42); assertThat(beany.getNested1().getA()).isEqualTo(64); assertThat(beany.getNested2().getA()).isEqualTo("quux"); }); }
@Test public void testBeanPropertyBinding() { Handle h = dbRule.openHandle(); Something original = new Something(0, "Keith"); assertThat(h .createUpdate("insert into something (id, name) values (:id, :name)") .bindBean(original) .execute()) .isEqualTo(1); assertThat(h .select("select * from something where id = ?", original.getId()) .mapToBean(Something.class) .findOnly()) .isEqualTo(original); }
@Test public void testBeanPropertyNestedBinding() { Handle h = dbRule.openHandle(); Something thing = new Something(0, "Keith"); assertThat(h .createUpdate("insert into something (id, name) values (:my.nested.id, :my.nested.name)") .bindBean("my", new NestsSomething(thing)) .execute()).isEqualTo(1); assertThat(h .select("select * from something where id = ?", thing.getId()) .mapToBean(Something.class) .findOnly()) .isEqualTo(thing); }
@Test public void bindBeanQualifiedSetter() { dbRule.getJdbi() .registerArgument(new ReversedStringArgumentFactory()) .useHandle(handle -> { handle.createUpdate("INSERT INTO something (id, name) VALUES (:id, :name)") .bindBean(new QualifiedSetterThing(1, "abc")) .execute(); assertThat(handle.select("SELECT name FROM something") .mapTo(String.class) .findOnly()) .isEqualTo("cba"); }); }
@Test public void bindBeanQualifiedGetter() { dbRule.getJdbi() .registerArgument(new ReversedStringArgumentFactory()) .useHandle(handle -> { handle.createUpdate("INSERT INTO something (id, name) VALUES (:id, :name)") .bindBean(new QualifiedGetterThing(1, "abc")) .execute(); assertThat(handle.select("SELECT name FROM something") .mapTo(String.class) .findOnly()) .isEqualTo("cba"); }); }
@Test public void bindBeanQualifiedSetterParam() { dbRule.getJdbi() .registerArgument(new ReversedStringArgumentFactory()) .useHandle(handle -> { handle.createUpdate("INSERT INTO something (id, name) VALUES (:id, :name)") .bindBean(new QualifiedSetterParamThing(1, "abc")) .execute(); assertThat(handle.select("SELECT name FROM something") .mapTo(String.class) .findOnly()) .isEqualTo("cba"); }); }
@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(); }
.bindBean(new User(3, "David")) .execute();
@Override public A save(A article) { entityRepository.save(article); dbi.withHandle(h -> h.createUpdate("INSERT INTO articles(uuid, text) VALUES (:uuid, :text::JSONB)") .bindBean(article) .execute()); return findOne(article.getUuid()); }
@Override public W save(W website) { entityRepository.save(website); dbi.withHandle(h -> h.createUpdate("INSERT INTO websites(url, registration_date, uuid) VALUES (:url, :registrationDate, :uuid)") .bindBean(website) .execute()); return findOne(website.getUuid()); }
@Override public C save(C contentNode) { identifiableRepository.save(contentNode); dbi.withHandle(h -> h.createUpdate("INSERT INTO contentnodes(uuid) VALUES (:uuid)") .bindBean(contentNode) .execute()); return findOne(contentNode.getUuid()); }
@Override public W update(W webpage) { identifiableRepository.update(webpage); dbi.withHandle(h -> h.createUpdate("UPDATE webpages SET text=:text::JSONB WHERE uuid=:uuid") .bindBean(webpage) .execute()); return findOne(webpage.getUuid()); }
@Override public A update(A article) { entityRepository.update(article); dbi.withHandle(h -> h.createUpdate("UPDATE articles SET text=:text::JSONB WHERE uuid=:uuid") .bindBean(article) .execute()); return findOne(article.getUuid()); }
@Override public W update(W website) { entityRepository.update(website); // do not update/left out from statement: created, uuid dbi.withHandle(h -> h.createUpdate("UPDATE websites SET url=:url, registration_date=:registrationDate WHERE uuid=:uuid") .bindBean(website) .execute()); return findOne(website.getUuid()); } }
@Override public W save(W webpage) { identifiableRepository.save(webpage); dbi.withHandle(h -> h.createUpdate("INSERT INTO webpages(uuid, text) VALUES (:uuid, :text::JSONB)") .bindBean(webpage) .execute()); return findOne(webpage.getUuid()); }
@Override public E update(E entity) { identifiableRepository.update(entity); // do not update/left out from statement: created, uuid dbi.withHandle(h -> h.createUpdate("UPDATE entities SET entity_type=:entityType WHERE uuid=:uuid") .bindBean(entity) .execute()); return findOne(entity.getUuid()); } }