org.skife.jdbi.v2.Update.executeAndReturnGeneratedKeys()方法的使用及代码示例

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

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

Update.executeAndReturnGeneratedKeys介绍

[英]Execute the statement and returns any auto-generated keys. This requires the JDBC driver to support the Statement#getGeneratedKeys() method.
[中]执行该语句并返回所有自动生成的键。这需要JDBC驱动程序支持语句#getGeneratedKeys()方法。

代码示例

代码示例来源:origin: rakam-io/rakam

public void updateUserInfo(int id, String name) {
  try (Handle handle = dbi.open()) {
    handle.createStatement("UPDATE web_user SET name = :name WHERE id = :id")
        .bind("id", id)
        .bind("name", name)
        .executeAndReturnGeneratedKeys(IntegerMapper.FIRST).first();
  }
}

代码示例来源:origin: rakam-io/rakam

@ApiOperation(value = "Create custom event mapper",
    authorizations = @Authorization(value = "master_key")
)
@Path("/create")
@JsonRequest
public long create(@Named("project") RequestContext context, @ApiParam("name") String name, @ApiParam("script") String script, @ApiParam(value = "image", required = false) String image, @ApiParam(value = "parameters", required = false) Map<String, Parameter> parameters) {
  try (Handle handle = dbi.open()) {
    GeneratedKeys<Long> longs = handle.createStatement("INSERT INTO custom_event_mappers (project, name, script, parameters, image) " +
        "VALUES (:project, :name, :script, :parameters, :image)")
        .bind("project", context.project)
        .bind("script", script)
        .bind("name", name)
        .bind("image", image)
        .bind("parameters", JsonHelper.encode(ofNullable(parameters).orElse(ImmutableMap.of())))
        .executeAndReturnGeneratedKeys((index, r, ctx) -> r.getLong(1));
    return longs.first();
  }
}

代码示例来源:origin: rakam-io/rakam

public Integer saveApiKeys(Handle handle, int user, int projectId, String readKey, String writeKey, String masterKey) {
  if (!hasMasterAccess(handle, projectId, user)) {
    throw new RakamException("You do not have master key permission", UNAUTHORIZED);
  }
  return handle.createStatement("INSERT INTO web_user_api_key " +
      "(user_id, project_id, read_key, write_key, master_key) " +
      "VALUES (:userId, :project, :readKey, :writeKey, :masterKey)")
      .bind("userId", user)
      .bind("project", projectId)
      .bind("readKey", readKey)
      .bind("writeKey", writeKey)
      .bind("masterKey", masterKey)
      .executeAndReturnGeneratedKeys((index, r, ctx) -> r.getInt("id")).first();
}

代码示例来源:origin: rakam-io/rakam

public WebUser.UserApiKey registerProject(int user, String apiUrl, String project, String readKey, String writeKey, String masterKey) {
  int projectId;
  try (Handle handle = dbi.open()) {
    try {
      projectId = (Integer) handle.createStatement("INSERT INTO web_user_project " +
          "(project, api_url, user_id) " +
          "VALUES (:project, :apiUrl, :userId)")
          .bind("userId", user)
          .bind("project", project)
          .bind("apiUrl", apiUrl)
          .executeAndReturnGeneratedKeys().first().get("id");
    } catch (Exception e) {
      if (e.getMessage().contains("project_check")) {
        throw new RakamException("Project already exists.", BAD_REQUEST);
      }
      throw e;
    }
    handle.createStatement("INSERT INTO web_user_api_key " +
        "(user_id, project_id, read_key, write_key, master_key) " +
        "VALUES (:userId, :project, :readKey, :writeKey, :masterKey)")
        .bind("userId", user)
        .bind("project", projectId)
        .bind("readKey", readKey)
        .bind("writeKey", writeKey)
        .bind("masterKey", masterKey)
        .execute();
  }
  eventBus.post(new UIEvents.ProjectCreatedEvent(projectId));
  return new WebUser.UserApiKey(projectId, readKey, writeKey, masterKey);
}

代码示例来源:origin: rakam-io/rakam

.bind("external", external)
.bind("googleId", googleId)
.bind("password", scrypt).executeAndReturnGeneratedKeys(IntegerMapper.FIRST).first();
    .bind("email", email)
    .bind("name", name)
    .bind("password", scrypt).executeAndReturnGeneratedKeys(IntegerMapper.FIRST).first();
if (id > 0) {
  webuser = new WebUser(id, email, name, false, Instant.now(), generateIntercomHash(email), ImmutableList.of());

代码示例来源:origin: rakam-io/rakam

.bind("email", email).executeAndReturnGeneratedKeys(IntegerMapper.FIRST).first();

代码示例来源:origin: org.kill-bill.commons/killbill-jdbi

@Override
  public Object value(Update update, HandleDing baton)
  {
    GeneratedKeys o = update.executeAndReturnGeneratedKeys(mapper);
    return magic.result(o, baton);
  }
};

代码示例来源:origin: com.ning.billing/killbill-osgi-bundles-analytics

public GeneratedKeys<Map<String, Object>> executeAndReturnGeneratedKeys()
  {
    return executeAndReturnGeneratedKeys(new DefaultMapper());
  }
}

代码示例来源:origin: org.kill-bill.commons/killbill-jdbi

public GeneratedKeys<Map<String, Object>> executeAndReturnGeneratedKeys()
  {
    return executeAndReturnGeneratedKeys(new DefaultMapper());
  }
}

代码示例来源:origin: com.ning.billing/killbill-osgi-bundles-analytics

public Object value(Update update, HandleDing baton)
  {
    GeneratedKeys o = update.executeAndReturnGeneratedKeys(mapper);
    return magic.result(o, baton);
  }
};

代码示例来源:origin: com.intuit.wasabi/wasabi-database

/**
   * {@inheritDoc}
   */
  @Override
  public Object value(Handle handle) {
    Update statement = handle.createStatement(query);
    for (int i = 0; i < params.length; i++) {
      statement.bind(i, params[i]);
    }
    return shouldReturnId ?
        statement.executeAndReturnGeneratedKeys(IntegerMapper.FIRST).first() : statement.execute();
  }
});

代码示例来源:origin: bwajtr/java-persistence-frameworks-comparison

@Override
public Integer insertProject(Project project) {
  try (Handle h = dbi.open()) {
    return h.createStatement("INSERT INTO project (name, datestarted) VALUES (:name, :datestarted)")
        .bind("name", project.getName())
        .bind("datestarted", project.getDate())
        .executeAndReturnGeneratedKeys(IntegerColumnMapper.WRAPPER)
        .first();
  }
}

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

@Test
  public void testDelete() throws Exception
  {
    Handle h = openHandle();

    Update insert = h.createStatement("insert into something_else (name) values (:name)");
    insert.bind("name", "Brian");
    Long id1 = insert.executeAndReturnGeneratedKeys(LongColumnMapper.WRAPPER).first();

    Assert.assertNotNull(id1);

    Update delete = h.createStatement("delete from something_else where id = :id");
    delete.bind("id", id1);
    Long id2 = delete.executeAndReturnGeneratedKeys(LongColumnMapper.WRAPPER).first();

    Assert.assertNull(id2);
  }
}

代码示例来源:origin: org.kill-bill.commons/killbill-jdbi

@Test
  public void testDelete() throws Exception
  {
    Handle h = openHandle();

    Update insert = h.createStatement("insert into something_else (name) values (:name)");
    insert.bind("name", "Brian");
    Long id1 = insert.executeAndReturnGeneratedKeys(LongMapper.FIRST).first();

    Assert.assertNotNull(id1);

    Update delete = h.createStatement("delete from something_else where id = :id");
    delete.bind("id", id1);
    Long id2 = delete.executeAndReturnGeneratedKeys(LongMapper.FIRST).first();

    Assert.assertNull(id2);
  }
}

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

@Test
public void testUpdate() throws Exception
{
  Handle h = openHandle();
  Update insert = h.createStatement("insert into something_else (name) values (:name)");
  insert.bind("name", "Brian");
  Long id1 = insert.executeAndReturnGeneratedKeys(LongColumnMapper.WRAPPER).first();
  Assert.assertNotNull(id1);
  Update update = h.createStatement("update something_else set name = :name where id = :id");
  update.bind("id", id1);
  update.bind("name", "Tom");
  Long id2 = update.executeAndReturnGeneratedKeys(LongColumnMapper.WRAPPER).first();
  Assert.assertNull(id2);
}

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

@Test
public void testInsert() throws Exception
{
  Handle h = openHandle();
  Update insert1 = h.createStatement("insert into something_else (name) values (:name)");
  insert1.bind("name", "Brian");
  Long id1 = insert1.executeAndReturnGeneratedKeys(LongColumnMapper.WRAPPER).first();
  Assert.assertNotNull(id1);
  Update insert2 = h.createStatement("insert into something_else (name) values (:name)");
  insert2.bind("name", "Tom");
  Long id2 = insert2.executeAndReturnGeneratedKeys(LongColumnMapper.WRAPPER).first();
  Assert.assertNotNull(id2);
  Assert.assertTrue(id2 > id1);
}

代码示例来源:origin: org.kill-bill.commons/killbill-jdbi

@Test
public void testInsert() throws Exception
{
  Handle h = openHandle();
  Update insert1 = h.createStatement("insert into something_else (name) values (:name)");
  insert1.bind("name", "Brian");
  Long id1 = insert1.executeAndReturnGeneratedKeys(LongMapper.FIRST).first();
  Assert.assertNotNull(id1);
  Update insert2 = h.createStatement("insert into something_else (name) values (:name)");
  insert2.bind("name", "Tom");
  Long id2 = insert2.executeAndReturnGeneratedKeys(LongMapper.FIRST).first();
  Assert.assertNotNull(id2);
  Assert.assertTrue(id2 > id1);
}

代码示例来源:origin: org.kill-bill.commons/killbill-jdbi

@Test
public void testUpdate() throws Exception
{
  Handle h = openHandle();
  Update insert = h.createStatement("insert into something_else (name) values (:name)");
  insert.bind("name", "Brian");
  Long id1 = insert.executeAndReturnGeneratedKeys(LongMapper.FIRST).first();
  Assert.assertNotNull(id1);
  Update update = h.createStatement("update something_else set name = :name where id = :id");
  update.bind("id", id1);
  update.bind("name", "Tom");
  Long id2 = update.executeAndReturnGeneratedKeys(LongMapper.FIRST).first();
  Assert.assertNull(id2);
}

代码示例来源:origin: perspilling/jdbi-examples

@Override
public Person save(Person person) {
  long pk;
  try (Handle h = dbi.open()) {
    if (person.getId() == null)  {
      pk = h.createStatement("insert into PERSON (name, email, phone) values (:name, :email, :phone)")
          .bind("name", person.getName())
          .bind("email", person.getEmail().getVal())
          .bind("phone", person.getPhone())
          .executeAndReturnGeneratedKeys(LongMapper.FIRST).first();
    } else {
      pk = h.createStatement("update PERSON set name = :p.name, email = :p.emailVal, phone = :p.phone where personId = :p.id")
          .bind("name", person.getName())
          .bind("email", person.getEmail().getVal())
          .bind("phone", person.getPhone())
          .executeAndReturnGeneratedKeys(LongMapper.FIRST).first();
    }
    return new Person(pk, person.getName(), person.getEmail(), person.getPhone());
  }
}

相关文章