Skip to content

Queries

Sonatype Nexus (Releases) Sonatype Nexus (Snapshots)

Tip

Queries is a complete replacement for the old queries module. While it is the successor it lacks some features that are not implemented on purpose.

SADU provides a query builder, which allows easy guidance through requesting data from your database. Import into your project to use it.

dependencies {
    implementation("de.chojo.sadu", "sadu-queries", "<version>")
}

Why use the query builder?

Before I give you a long talk about how much nicer the syntax and code is let me simple show you a comparison.

Without the query builder your code would ideally look like this:

class MyQueries {

    DataSource dataSource;

    MyQueries(DataSource dataSource){
        this.dataSource = dataSource;
    }

    public Optional<MyResultClass> getResultOld(int id) {
        try (Connection conn = source().getConnection(); PreparedStatement stmt = conn.prepareStatement("SELECT result FROM results WHERE id = ?")) {
            stmt.setInt(id);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                return Optional.of(new MyResultClass(rs.getString("result"));
            }
        } catch (SQLException e) {
            logger.error("Something went wrong", e);
        }
        return Optional.empty();
    }
}

Using the query builder your code becomes this:

class MyQueries { 
    public Optional<MyResultClass> getResultNew(int id) {
        return Query.query("SELECT result FROM results WHERE id = ?")
                .single(Call.of().bind(id))
                .map(row -> new MyResultClass(row.getString("result")))
                .first();
    }
}

Beautiful, isn't it? The query builder will:

  • Enforce try with resources
  • Set parameters in the order defined by you
  • Read the result set
  • Handle the exceptions for you.

How does it work?

The query builder guides you through different phases. These phases will allow you to only call methods which make sense in the current context. It is for exampe not possible to read a row without defining a query first.