org.jdbi.v3.core.Jdbi.withHandle()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(7.0k)|赞(0)|评价(0)|浏览(212)

本文整理了Java中org.jdbi.v3.core.Jdbi.withHandle()方法的一些代码示例,展示了Jdbi.withHandle()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Jdbi.withHandle()方法的具体详情如下:
包路径:org.jdbi.v3.core.Jdbi
类名称:Jdbi
方法名:withHandle

Jdbi.withHandle介绍

[英]A convenience function which manages the lifecycle of a handle and yields it to a callback for use by clients.
[中]一种方便的函数,用于管理句柄的生命周期,并将其生成回调以供客户端使用。

代码示例

代码示例来源:origin: jdbi/jdbi

/**
 * A convenience function which manages the lifecycle of a handle and yields it to a callback
 * for use by clients.
 *
 * @param callback A callback which will receive an open Handle
 * @param <X> exception type thrown by the callback, if any.
 *
 * @throws X any exception thrown by the callback
 */
public <X extends Exception> void useHandle(final HandleConsumer<X> callback) throws X {
  withHandle(h -> {
    callback.useHandle(h);
    return null;
  });
}

代码示例来源:origin: jdbi/jdbi

/**
 * A convenience function which manages the lifecycle of a handle and yields it to a callback
 * for use by clients. The handle will be in a transaction when the callback is invoked, and
 * that transaction will be committed if the callback finishes normally, or rolled back if the
 * callback raises an exception.
 *
 * @param callback A callback which will receive an open Handle, in a transaction
 * @param <R> type returned by the callback
 * @param <X> exception type thrown by the callback, if any.
 *
 * @return the value returned by callback
 *
 * @throws X any exception thrown by the callback
 */
public <R, X extends Exception> R inTransaction(final HandleCallback<R, X> callback) throws X {
  return withHandle(handle -> handle.<R, X>inTransaction(callback));
}

代码示例来源:origin: jdbi/jdbi

private List<SwedishChef> run(String table) {
  return jdbi.withHandle(h -> h.createQuery("select value from " + table).mapTo(SwedishChef.class).list());
}
public enum SwedishChef {

代码示例来源:origin: jdbi/jdbi

@Benchmark
public UnqualifiedBean mapUnqualifiedBean() {
  return jdbi.withHandle(h -> h.createQuery("select 'a' as a, 'b' as b, 'c' as c").mapTo(UnqualifiedBean.class).findOnly());
}

代码示例来源:origin: jdbi/jdbi

@Benchmark
public QualifiedBean mapQualifiedBean() {
  return jdbi.withHandle(h -> h.createQuery("select 'a' as a, 'b' as b, 'c' as c").mapTo(QualifiedBean.class).findOnly());
}

代码示例来源:origin: jdbi/jdbi

@Test
  public void nullByteArrayIsTypedAsVarbinary() throws SQLException {
    Argument nullByteArrayArg = db.getJdbi().withHandle(h -> h.getConfig(Arguments.class).findFor(byte[].class, null)).get();

    nullByteArrayArg.apply(0, stmt, null);

    verify(stmt, never()).setNull(anyInt(), eq(Types.ARRAY));
    verify(stmt).setNull(anyInt(), eq(Types.VARBINARY));
  }
}

代码示例来源:origin: jdbi/jdbi

@Test
public void byteArrayIsTypedAsVarbinary() throws SQLException {
  Argument nullByteArrayArg = db.getJdbi().withHandle(h -> h.getConfig(Arguments.class).findFor(byte[].class, new byte[] {1})).get();
  nullByteArrayArg.apply(0, stmt, null);
  verify(stmt, never()).setArray(anyInt(), any(Array.class));
  verify(stmt).setBytes(anyInt(), any(byte[].class));
}

代码示例来源:origin: jdbi/jdbi

@Benchmark
public List<Tribble> mapByOrdinal() {
  return jdbi.withHandle(h ->
    h.select("select ordinal from ordinals")
      .mapTo(QualifiedType.of(Tribble.class).with(EnumByOrdinal.class))
      .list());
}

代码示例来源:origin: jdbi/jdbi

@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());
}

代码示例来源:origin: jdbi/jdbi

@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());
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testConnected() {
  int four = dbRule.getJdbi().withHandle(handle ->
      handle.createQuery("select 2 + 2").mapTo(Integer.class).findOnly());
  assertThat(four).isEqualTo(4);
}

代码示例来源:origin: jdbi/jdbi

@Test
  public void isAlive() {
    Integer one = postgres.getJdbi().withHandle(h -> h.createQuery("select 1").mapTo(Integer.class).findOnly());

    assertThat(one).isEqualTo(1);
  }
}

代码示例来源:origin: jdbi/jdbi

@Test
  public void isAlive() {
    Integer one = h2.getJdbi().withHandle(h -> h.createQuery("select 1").mapTo(Integer.class).findOnly());

    assertThat(one).isEqualTo(1);
  }
}

代码示例来源:origin: jdbi/jdbi

@Test
  public void isAlive() {
    Integer one = sqlite.getJdbi().withHandle(h -> h.createQuery("select 1").mapTo(Integer.class).findOnly());

    assertThat(one).isEqualTo(1);
  }
}

代码示例来源:origin: jdbi/jdbi

private Optional<IdValue> select() {
  return dbRule.getJdbi().withHandle(
    h -> h.createQuery("SELECT id, value FROM test")
      .map((rs, ctx) -> new IdValue(rs.getLong("id"), rs.getString("value")))
      .findFirst());
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testMapCollectorWithGlobalKeyValueShouldSucceed() {
  Jdbi jdbiWithKeyColAndValCol = dbRule.getJdbi()
      .setMapKeyColumn("key_c")
      .setMapValueColumn("val_c");
  Boolean executed = jdbiWithKeyColAndValCol.withHandle(h -> {
    HashMap<String, String> valueMap = h.createQuery("select val_c, key_c from keyval")
        .collectInto(new GenericType<HashMap<String, String>>() {});
    assertThat(valueMap).containsOnlyElementsOf(expectedMap);
    return true;
  });
  assertTrue(executed);
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testWithHandle() {
  Jdbi db = Jdbi.create(this.dbRule.getConnectionString());
  String value = db.withHandle(handle -> {
    handle.execute("insert into something (id, name) values (1, 'Brian')");
    return handle.createQuery("select name from something where id = 1").mapToBean(Something.class).findOnly().getName();
  });
  assertThat(value).isEqualTo("Brian");
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testSillyNumberOfCallbacks() throws Exception {
  try (Handle h = dbRule.openHandle()) {
    h.execute("insert into something (id, name) values (1, 'Keith')");
  }
  // strangely enough, the compiler can't infer this and thinks the throws is redundant
  String value = dbRule.getJdbi().<String, Exception>withHandle(handle ->
      handle.inTransaction(handle1 ->
          handle1.createQuery("select name from something where id = 1").mapTo(String.class).findOnly()));
  assertThat(value).isEqualTo("Keith");
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testRegisterInferredOnJdbi() {
  db.registerRowMapper(new SomethingMapper());
  Something sam = db.withHandle(handle1 -> {
    handle1.execute("insert into something (id, name) values (18, 'Sam')");
    return handle1.createQuery("select id, name from something where id = :id")
      .bind("id", 18)
      .mapTo(Something.class)
      .findOnly();
  });
  assertThat(sam.getName()).isEqualTo("Sam");
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testRawSql() {
  db.useHandle(h -> {
    PreparedBatch batch = h.prepareBatch("insert into something (id, name) values (?, ?)");
    batch.add(1, "Mary");
    batch.add(2, "David");
    batch.add(3, "Kate");
    batch.execute();
  });
  List<String> names = db.withHandle(h -> h.createQuery("select name from something order by name")
      .mapTo(String.class)
      .list());
  assertThat(names).containsExactly("David", "Kate", "Mary");
  assertThat(timingCollector.statementNames).containsOnly(
      "sql.raw.insert into something (id, name) values (?, ?)",
      "sql.raw.select name from something order by name");
}

相关文章