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(); } }
@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(); } }
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(); }
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); }
.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());
.bind("email", email).executeAndReturnGeneratedKeys(IntegerMapper.FIRST).first();
@Override public Object value(Update update, HandleDing baton) { GeneratedKeys o = update.executeAndReturnGeneratedKeys(mapper); return magic.result(o, baton); } };
public GeneratedKeys<Map<String, Object>> executeAndReturnGeneratedKeys() { return executeAndReturnGeneratedKeys(new DefaultMapper()); } }
public GeneratedKeys<Map<String, Object>> executeAndReturnGeneratedKeys() { return executeAndReturnGeneratedKeys(new DefaultMapper()); } }
public Object value(Update update, HandleDing baton) { GeneratedKeys o = update.executeAndReturnGeneratedKeys(mapper); return magic.result(o, baton); } };
/** * {@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(); } });
@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(); } }
@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); } }
@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); } }
@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); }
@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); }
@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); }
@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); }
@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()); } }