org.jdbi.v3.core.statement.Update类的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(102)

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

Update介绍

[英]Used for INSERT, UPDATE, and DELETE statements
[中]用于插入、更新和删除语句

代码示例

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

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

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

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

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

@Test
public void testBean() {
  handle.createUpdate(INSERT_NAMED).bindBean(new StringBean("herp")).execute();
  assertThat(named).isEqualTo("herp");
}

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

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

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

@Test
  public void testFoo() {
    Handle h = dbRule.openHandle();
    final int inserted = h.createUpdate("insert into <table> (id, name) values (:id, :name)")
        .bind("id", 7)
        .bind("name", "Martin")
        .define("table", "something")
        .execute();
    assertThat(inserted).isEqualTo(1);
  }
}

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

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

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

.bind(0, 1) // 0-based parameter indexes
.bind(1, "Bob")
.execute();
.bind("id", 2)
.bind("name", "Clarice")
.execute();
.bindBean(new User(3, "David"))
.execute();

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

/**
 * Executes the statement, returning the update count.
 *
 * @return the number of rows modified
 */
public int execute() {
  return execute(returningUpdateCount());
}

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

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

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

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

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

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

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

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

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

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

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

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

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

@Test
public void testStatement() {
  h.createUpdate(CREATE).define("x", "foo").execute();
  assertThat(logger.getAttributes())
    .hasSize(2)
    .allMatch(x -> x.get("x").equals("foo"))
    .allMatch(x -> x.size() == 1);
}

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

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

代码示例来源:origin: de.digitalcollections.cudami/dc-cudami-server-backend-jdbi

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

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

@Test
public void testMapToValueTypeFromColumnMapperFactory() {
  h.createUpdate("insert into someBean (valueType) values ('foo')").execute();
  List<ValueType> list = dao.listValueTypesFactoryMapped();
  assertThat(list).containsExactly(ValueType.valueOf("foo"));
}

代码示例来源:origin: org.jdbi/jdbi3

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

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

@Test
public void testFluentUpdate() {
  try (Handle h = dbRule.openHandle()) {
    h.createUpdate("insert into something(id, name) values (:id, :name)")
      .bind("id", 4)
      .bind("name", "Martin")
      .execute();
  }
}

相关文章