Skip to content

Query Examples

To create a query class we will extend the QueryFactory. As an alternative you can also call a QueryFactory instance directly. You can also initialise the StaticQueryAdapter and use the builder method from there.

Now we can create functions which use the query builder.

// Our class will extend the QueryFactory.
// This allows us to simply create preconfigured builder.
public class MyQueries extends QueryFactory {

    /**
     * Create a new queries object.
     *
     * @param dataSource data source used to query data from a database
     */
    public MyQueries(DataSource dataSource) {
        super(dataSource);
    }
}

Now we can start to add our methods.

Retrieve single result

    /**
     * Retrieve a result by id.
     *
     * @param id id to retrieve
     * @return An optional holding a result if found.
     */
    public CompletableFuture<Optional<MyResultClass>> getResult(int id) {
        // We want to have a class of type MyResultClass.
        // This can be any class like Integer, String or your own class.
        // It specifies into what the result should be mapped.
        return builder(MyResultClass.class)
                // We define our query
                .query("SELECT result FROM results WHERE id = ?")
                // We set the first parameter. No need to define the index.
                .parameter(stmt -> stmt.setInt(id))
                // We map our current row to a MyResultClass.
                // This is the class you defined earlier in the builder call
                .readRow(rs -> new MyResultClass(rs.getString("result")))
                // We retrieve only the first result we get.
                .first();
    }

Retrieve a list of results

    /**
     * Retrieve a list of all results in the result table.
     *
     * @return list of results
     */
    public CompletableFuture<List<MyResultClass>> getResults() {
        // We want to have a class of type MyResultClass.
        // This can be any class like Integer, String or your own class.
        // It specifies into what the result should be mapped.
        return builder(MyResultClass.class)
                // We define our query
                .query("SELECT result FROM results")
                // We skip the parameter assignment
                .emptyParams()
                // We map our current row to a MyResultClass.
                // This is the class you defined earlier in the builder call
                .readRow(rs -> new MyResultClass(rs.getString("result")))
                // We retrieve only the first result we get.
                .all();
    }

Delete

    /**
     * Delete an entry.
     *
     * @param id the id to delete
     * @return true if result was present and got deleted
     */
    public CompletableFuture<Boolean> deleteResult(int id) {
        // We want to delete. We leave the expected class empty.
        return builder()
                // We define our query
                .query("DELETE FROM results WHERE id = ?")
                // We set the first parameter. No need to define the index.
                .parameter(stmt -> stmt.setInt(id))
                // We say that we want to execute a deletion
                .delete()
                // We execute the query asynchronously
                .send()
                // We check if a row was changed
                .thenApply(UpdateResult::changed);
    }

Update

    /**
     * Update an entry.
     *
     * @param id the id to delete
     * @return true if result was present and got updated
     */
    public CompletableFuture<Boolean> updateResult(int id, String newValue) {
        // We want to update. We leave the expected class empty.
        return builder()
                // We define our query
                .query("UPDATE results SET result = ? WHERE id = ?")
                // The param builder will set the parameters in the order you define them
                .parameter(stmt -> stmt.setString(newValue).setInt(id))
                // We say that we want to execute an update
                .update()
                // We execute the query asynchronously
                .send()
                // We check if a row was changed
                .thenApply(UpdateResult::changed);
    }

Insert

    /**
     * Insert a result.
     *
     * @param result the result to add
     * @return returns the id of the new result
     */
    public CompletableFuture<Optional<Long>> addResult(String result) {
        // We want to insert. We leave the expected class empty.
        return builder()
                // We define our query
                .query("INSERT INTO results(result) VALUES(?)")
                // We set the first parameter. No need to define the index.
                .parameter(stmt -> stmt.setString(result))
                // We say that we want to execute an insertion
                .insert()
                // We execute the query asynchronously and get the created key.
                .key();
    }
}