@Test public void testStatement() { int rows = h.createUpdate("insert into something (id, name) values (1, 'eric')").execute(); assertThat(rows).isEqualTo(1); }
@Before public void addData() { Handle handle = dbRule.openHandle(); handle.createUpdate("insert into something (id, name) values (1, 'eric')").execute(); handle.createUpdate("insert into something (id) values (2)").execute(); }
@Test public void testInsertParamBinding() { int count = h.createUpdate("insert into something (id, name) values (?, 'eric')") .bind(0, 1) .execute(); assertThat(count).isEqualTo(1); }
@Test public void testString() { handle.createUpdate(INSERT_POSITIONAL).bind(0, "herp").execute(); assertThat(positional).isEqualTo("herp"); }
@Test public void testNotClosing() { h.createUpdate("insert into something (id, name) values (1, 'eric')").execute(); h.createUpdate("insert into something (id, name) values (2, 'brian')").execute(); List<Map<String, Object>> results = h.createQuery("select * from something order by id").mapToMap().list(); assertThat(results).hasSize(2); assertThat(results.get(0)).containsEntry("name", "eric"); assertThat(h.isClosed()).isFalse(); }
@Test public void testMapToValueTypeFromColumnMapper() { h.createUpdate("insert into someBean (valueType) values ('foo')").execute(); List<ValueType> list = dao.listValueTypes(); assertThat(list).containsExactly(ValueType.valueOf("foo")); }
@Test public void testIterateKeepHandle() { h.createUpdate("insert into something (id, name) values (1, 'eric')").execute(); h.createUpdate("insert into something (id, name) values (2, 'brian')").execute(); ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .mapToMap() .iterator(); assertThat(it).hasSize(2); assertThat(h.isClosed()).isFalse(); }
@Test public void testInsert() { Handle h = dbRule.openHandle(); Update insert = h.createUpdate("insert into something (id, name) values (:id, :name)"); insert.bind("id", 1); insert.bind("name", "Brian"); int count = insert.execute(); assertThat(count).isEqualTo(1); }
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 testInsertNullUrlUsingBindByType() { handle.createUpdate("INSERT INTO foo VALUES (:url)") .bindByType("url", null, URL.class) .execute(); assertThatThrownBy(() -> handle.createQuery("SELECT url FROM foo").mapTo(URL.class).findOnly()) .isInstanceOf(ResultSetException.class); }
@Test public void testArgumentHasToString() { handle.registerArgument(new ToStringFooArgumentFactory()); handle.createUpdate(INSERT_POSITIONAL).bind(0, new Foo()).execute(); assertThat(positional).isEqualTo("this is a Foo"); }
@Test public void testMapPrimitiveIntFromNull() { h.createUpdate("insert into someBean (primitiveInt) values (null)").execute(); List<SomeBean> beans = dao.listBeans(); assertThat(beans).extracting(SomeBean::getPrimitiveInt).containsExactly(0); }
@Test public void testMapString() { h.createUpdate("insert into someBean (string) values ('foo')").execute(); List<SomeBean> beans = dao.listBeans(); assertThat(beans).extracting(SomeBean::getString).containsExactly("foo"); }
@Test public void testMapEnumValues() { Handle h = dbRule.openHandle(); h.createUpdate("insert into something (id, name) values (1, 'eric')").execute(); h.createUpdate("insert into something (id, name) values (2, 'brian')").execute(); List<SomethingElse> results = h.createQuery("select * from something order by id") .mapToBean(SomethingElse.class) .list(); assertThat(results).extracting(se -> se.name).containsExactly(SomethingElse.Name.eric, SomethingElse.Name.brian); }
@Before public void createTable() { h = dbRule.openHandle(); h.createUpdate("create table testBean (valueType varchar(50))").execute(); dao = h.attach(TestDao.class); }
@Test public void testMapWrappedCharFromNull() { h.createUpdate("insert into someBean (wrappedChar) values (null)").execute(); List<SomeBean> beans = dao.listBeans(); assertThat(beans).extracting(SomeBean::getWrappedChar).hasSize(1).containsNull(); }
@Test public void testNamedParamsInExternal() { Handle h = dbRule.openHandle(); h.createUpdate(ClasspathSqlLocator.findSqlOnClasspath("insert-id-name")) .bind("id", 1) .bind("name", "Tip") .execute(); assertThat(h.select("select name from something").mapTo(String.class).list()).hasSize(1); }
@Test public void testNeitherHasToString() { handle.registerArgument(new FooArgumentFactory()); handle.createUpdate(INSERT_POSITIONAL).bind(0, new Foo()).execute(); assertThat(positional).containsPattern("@[0-9a-f]{1,8}$"); }
@Test public void testUri() { Handle h = dbRule.openHandle(); h.createUpdate("insert into something (id, name) values (1, :uri)") .bind("uri", TEST_URI).execute(); assertThat(h.createQuery("SELECT name FROM something") .mapTo(URI.class) .findOnly()).isEqualTo(TEST_URI); } }
@Before public void before() { db.getSharedHandle().createUpdate("create table foo(bar varchar)").execute(); db.getSharedHandle().prepareBatch("insert into foo(bar) values(:bar)") .bind("bar", "bam").add() .bind("bar", "gamma").add() .bind("bar", ":hello").add() .execute(); }