Frozen Queries

Ultra-fast and unmodifiable 

From the very beginning, Daobab is able to work with SQL as a regular Java code by the flexible Daobab Queries.

Building the entire Daobab Query, with all the necessary SQL functionalities may need a few milliseconds though. Also, building a SQL query from it takes some additional time.

However, there are many situations where the query is fixed and the most important requirement is performance.

For that reason, the Frozen Queries feature was introduced in version 1.7.

A Frozen Query can be created from a Daobab Query object. The idea is to have the SQL already built and cached internally.

Once built, a Frozen Query can be used to find one or many objects without rebuilding the SQL for every JDBC call.

A frozen query can be built from any regular Daobab query:

        var frozenQuery = db.select(tabCustomer.colFirstName())
                .whereLess(tabCustomer.colID(), 10)
                .freezeQuery();


In this form the frozen query is ready to use:

        var result = frozenQuery.findMany();

A Frozen Query has performance very similar to a regular JDBC call.

Since the SQL command is already built, the query behaves like a native SQL statement and the set of retrieved columns cannot be changed.

Although a Frozen Query is unmodifiable, it can still accept dynamic parameters, for example from a WHERE clause.

This means it can work like a regular Hibernate or JPA @Query expression.

Building a Frozen Query with dynamic parameters:

        frozenQuery = db.select(tabCustomer.colFirstName())
                        .whereLess(tabCustomer.colID(), param(1))
                        .freezeQuery();


After that, provide the parameters using the withParameters(Object[] params) method:


Adding parameters and retrieving the result:

        return frozenQuery.withParameters(10).findMany();
Daobab replaces the parameters in the query in array order.



A frozen query can be unfrozen.

After unfreezing, it becomes a regular Daobab Query again, so it is possible to add additional conditions, for example:


var unfrozenQuery = fistFrozenQuery.unfreeze();
var secondQuery = unfrozenQuery.orderDescBy(tabCustomer.colFirstName());
var secondFrozenQuery = secondQuery.freezeQuery();




© Copyright 2018-2023 Elephant Software Klaudiusz Wojtkowiak 
e-mail: contact@daobab.io