Skip to main content

Examples

Common reads and writes with the query builder. All examples use the default configuration through Query.query(...). With your own configuration, write config.query(...) instead.

The examples read users into this record:

public record User(int id, UUID uuid, String name) {
public static RowMapping<User> map() {
return row -> new User(
row.getInt("id"),
row.get("uuid", StandardReader.UUID_FROM_STRING),
row.getString("name"));
}
}

Read all matching rows​

List<User> users = Query.query("SELECT id, uuid, name FROM users WHERE name LIKE :name")
.single(Call.of().bind("name", "li%"))
.map(User.map())
.all();

Read the first row​

Optional<User> user = Query.query("SELECT id, uuid, name FROM users WHERE id = :id")
.single(Call.of().bind("id", 1))
.map(User.map())
.first();

Read a single value​

int count = Query.query("SELECT count(*) AS count FROM users")
.single()
.map(row -> row.getInt("count"))
.first()
.orElse(0);

Insert a row​

InsertionResult result = Query.query("INSERT INTO users(uuid, name) VALUES (?, ?)")
.single(Call.of().bind(UUID.randomUUID(), UUIDAdapter.AS_STRING).bind("lilly"))
.insert();

if (result.changed()) {
log.info("Inserted {} row", result.rows());
}

Insert a row and get its id​

Optional<Long> id = Query.query("INSERT INTO users(uuid, name) VALUES (?, ?)")
.single(Call.of().bind(UUID.randomUUID(), UUIDAdapter.AS_STRING).bind("lilly"))
.insertAndGetKeys()
.keys()
.stream()
.findFirst();

Insert many rows​

List<String> names = List.of("lilly", "chojo", "yuki");

InsertionBatchResult<InsertionResult> result = Query.query("INSERT INTO users(uuid, name) VALUES (?, ?)")
.batch(names.stream().map(name -> Call.of().bind(UUID.randomUUID(), UUIDAdapter.AS_STRING).bind(name)))
.insert();

log.info("Inserted {} rows", result.rows());

Update rows​

boolean renamed = Query.query("UPDATE users SET name = :name WHERE id = :id")
.single(Call.of().bind("name", "lilly").bind("id", 1))
.update()
.changed();

Delete rows​

int deleted = Query.query("DELETE FROM users WHERE name = ?")
.single(Call.of().bind("lilly"))
.delete()
.rows();

Map rows with a registered mapper​

With a row mapper for User, you do not need map(...):

List<User> users = Query.query("SELECT id, uuid, name FROM users")
.single()
.mapAs(User.class)
.all();

See Transactions.