@Test public void testConvenienceMethod() { h.execute("insert into something (id, name) values (1, 'eric')"); h.execute("insert into something (id, name) values (2, 'brian')"); List<Map<String, Object>> r = h.select("select * from something order by id").mapToMap().list(); assertThat(r).hasSize(2); assertThat(r.get(0).get("name")).isEqualTo("eric"); }
@Test public void testConvenienceMethodWithParam() { h.execute("insert into something (id, name) values (1, 'eric')"); h.execute("insert into something (id, name) values (2, 'brian')"); List<Map<String, Object>> r = h.select("select * from something where id = ?", 1).mapToMap().list(); assertThat(r).hasSize(1); assertThat(r.get(0).get("name")).isEqualTo("eric"); }
@Override public void evaluate() { assertThat( rule.getHandle() .select("select value from custom_migration_location") .mapTo(String.class) .findOnly()) .isEqualTo("inserted in migration script in a custom location"); } };
@Benchmark public List<Tribble> mapByExactName() { return jdbi.withHandle(h -> h.select("select name from exact_name") .mapTo(QualifiedType.of(Tribble.class).with(EnumByName.class)) .list()); }
@Benchmark public List<Tribble> mapByRandomCaseName() { return jdbi.withHandle(h -> h.select("select name from random_case") .mapTo(QualifiedType.of(Tribble.class).with(EnumByName.class)) .list()); }
@Test public void bindQualified() { dao.insert(1, "abc"); assertThat(handle.select("SELECT name FROM something WHERE id = 1") .mapTo(String.class) .findOnly()) .isEqualTo("cba"); }
@Test public void testInsertWithPositionalParameters() { somethingDao.insertSomething(4, "Dave", 90); List<Map<String, Object>> rows = handle.select("select * from something where something_id=?", 4).mapToMap().list(); assertThat(rows).containsExactlyElementsOf(ImmutableList.of( ImmutableMap.of("something_id", 4, "name", "Dave", "code", 90))); }
@Test public void testInsertWithDefaultParams() { somethingDao.insertWithDefaultParams("Greg", 21); List<Map<String, Object>> rows = handle.select("select * from something where something_id=?", 19).mapToMap().list(); assertThat(rows).containsExactlyElementsOf(ImmutableList.of( ImmutableMap.of("something_id", 19, "name", "Greg", "code", 21))); }
@Test public void testFuzzyScript() { Handle h = dbRule.openHandle(); Script script = h.createScript(getResourceOnClasspath("script/fuzzy-script.sql")); script.executeAsSeparateStatements(); List<Map<String, Object>> rows = h.select("select id, name from something order by id").mapToMap().list(); assertThat(rows).isEqualTo(ImmutableList.of( ImmutableMap.of("id", 1L, "name", "eric"), ImmutableMap.of("id", 2L, "name", "sally;ann"), ImmutableMap.of("id", 3L, "name", "bob"), ImmutableMap.of("id", 12L, "name", "sally;ann;junior"))); }
@Override public void evaluate() { assertThat( rule.getHandle() .select("select value from custom_migration_location") .mapTo(String.class) .list()) .containsOnly( "inserted in migration script in a custom location", "inserted in migration script in another custom location"); } };
@Test public void testExecuteSomeStatements() { try (Handle h = dbRule.openHandle()) { h.execute("insert into something (id, name) values (?, ?)", 3, "Patrick"); List<Map<String, Object>> rs = h.select("select id, name from something").mapToMap().list(); assertThat(rs).containsExactlyElementsOf(ImmutableList.of(ImmutableMap.of("id", 3L, "name", "Patrick"))); } }
@Test public void testLocateNamed() { Handle h = dbRule.openHandle(); h.execute(ClasspathSqlLocator.findSqlOnClasspath("insert-keith")); assertThat(h.select("select name from something").mapTo(String.class).list()).hasSize(1); }
@Test public void testCommentsInExternalSql() { Handle h = dbRule.openHandle(); h.execute(ClasspathSqlLocator.findSqlOnClasspath("insert-eric-with-comments")); assertThat(h.select("select name from something").mapTo(String.class).list()).hasSize(1); }
@Test public void bindQualifiedField() { FieldDao fieldDao = handle.attach(FieldDao.class); fieldDao.insertBindFields(new QualifiedFieldThing(1, "abc")); assertThat( handle.select("select name from something") .mapTo(String.class) .findOnly()) .isEqualTo("cba"); }
@Test public void registerFactory() { TestDao dao = handle.attach(TestDao.class); dao.insertStringValue(StringValue.of("foo")); dao.insertStringValue(StringValue.of("bar")); List<StringValue> values = handle.select("select string_value from serialized_types") .map((rs, ctx) -> (StringValue) rs.getObject("string_value")) .list(); assertThat(values).containsExactly(StringValue.of("foo"), StringValue.of("bar")); }
@Test public void testScriptWithStringSemicolon() { Handle h = dbRule.openHandle(); Script script = h.createScript(getResourceOnClasspath("script/insert-with-string-semicolons.sql")); script.execute(); assertThat(h.select("select * from something").mapToMap()).hasSize(3); }
@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 testNestedHalfPresent() { Handle handle = dbRule.getSharedHandle(); assertThat(handle .registerRowMapper(FieldMapper.factory(NullableNestedThing.class)) .select("SELECT 42 as testValue, '3' as s") .mapTo(NullableNestedThing.class) .findOnly()) .extracting("testValue", "nested.i", "nested.s") .containsExactly(42, null, "3"); }
@Test public void nestedParameters() { assertThat(dbRule.getSharedHandle() .registerRowMapper(ConstructorMapper.factory(NestedBean.class)) .select("select s, i from bean") .mapTo(NestedBean.class) .findOnly()) .extracting("nested.s", "nested.i") .containsExactly("3", 2); }
@Test public void testNestedPrefix() { Handle handle = dbRule.getSharedHandle(); handle.execute("insert into something (id, name) values (1, 'foo')"); assertThat(handle .registerRowMapper(FieldMapper.factory(NestedPrefixThing.class)) .select("select id nested_id, name nested_name from something") .mapTo(NestedPrefixThing.class) .findOnly()) .extracting("nested.i", "nested.s") .containsExactly(1, "foo"); }