Log SQL operations with Kripton Persistence Library

Abubusoft Blog

Log SQL operations with Kripton Persistence Library

As I have already said in other posts, StackOverflow is a good place where find interesting ideas for writing a post. Some days ago I found the following post:

Android Room Database DAO debug log

The question was: is there a way to log SQL queries with Room library? The answer was no.

And with Kripton? The answer is YES! Since version 1 SQL log was in the features of the ORM module. You have to do nothing. Just use Kripton. In the generated code, Kripton pulls all the necessary code to write log (that you can easily read into logcat).

You can simply check the basic sample on kripton-examples.

https://gist.github.com/xcesco/511a057be3c45b556787fd3c77ec016a#file-appdatasource-java

@BindDataSourceOptions(populator = AppDataSourcePopulator.class)
@BindDataSource(version = 1,
        daoSet = {CommentDao.class, ProductDao.class},
        fileName = "app.db",
        typeAdapters = {
            @BindSqlAdapter(adapter = DateMillisecondsTypeAdapter.class)}
)
public interface AppDataSource {
}
@BindDao(ProductEntity.class)
public interface ProductDao extends AbstractDao<ProductEntity> {
    @BindSqlSelect(orderBy = "name")
    MutableLiveData<List<ProductEntity>> loadAllProducts();

    @BindSqlSelect(where = "id = :productId")
    MutableLiveData<ProductEntity> loadProduct(long productId);

    @BindSqlSelect(where = "id = :productId")
    ProductEntity loadProductSync(long productId);

    @BindSqlSelect(orderBy = "name", pageSize = 1)
    ProductEntity selectFirst();

    @BindSqlDelete(where = "id=:{bean.id}")
    int delete(ProductEntity bean);

}

When you run the sample code, you will see in your log, something like this:

https://gist.github.com/xcesco/ec0205ce91e4743fb65592fd96c09246#file-sample-log

I/ProductDaoImpl, insert (line 94): INSERT OR REPLACE INTO products (description, name, price) VALUES (:description, :name, :price)
I/ProductDaoImpl, insert (line 103): ==> :description = 'Special edition Three-headed Monkey is finally here' (java.lang.String)
    ==> :name = 'Special edition Three-headed Monkey' (java.lang.String)
    ==> :price = '109' (java.lang.Integer)
I/ProductDaoImpl, insert (line 94): INSERT OR REPLACE INTO products (description, name, price) VALUES (:description, :name, :price)
I/ProductDaoImpl, insert (line 103): ==> :description = 'Special edition Rubber Chicken is recommended by Stan S. Stanman' (java.lang.String)
I/ProductDaoImpl, insert (line 103): ==> :name = 'Special edition Rubber Chicken' (java.lang.String)
    ==> :price = '128' (java.lang.Integer)
I/ProductDaoImpl, insert (line 94): INSERT OR REPLACE INTO products (description, name, price) VALUES (:description, :name, :price)
I/ProductDaoImpl, insert (line 103): ==> :description = 'Special edition Pint of Grog is the best sold product on Mêlé...' (java.lang.String)
    ==> :name = 'Special edition Pint of Grog' (java.lang.String)
    ==> :price = '15' (java.lang.Integer)
I/ProductDaoImpl, insert (line 94): INSERT OR REPLACE INTO products (description, name, price) VALUES (:description, :name, :price)
I/ProductDaoImpl, insert (line 103): ==> :description = 'Special edition Monocle is 💯' (java.lang.String)
    ==> :name = 'Special edition Monocle' (java.lang.String)
I/ProductDaoImpl, insert (line 103): ==> :price = '190' (java.lang.Integer)
I/ProductDaoImpl, insert (line 94): INSERT OR REPLACE INTO products (description, name, price) VALUES (:description, :name, :price)
I/ProductDaoImpl, insert (line 103): ==> :description = 'New Three-headed Monkey is finally here' (java.lang.String)
I/ProductDaoImpl, insert (line 103): ==> :name = 'New Three-headed Monkey' (java.lang.String)
    ==> :price = '42' (java.lang.Integer)
I/ProductDaoImpl, insert (line 94): INSERT OR REPLACE INTO products (description, name, price) VALUES (:description, :name, :price)
I/ProductDaoImpl, insert (line 103): ==> :description = 'New Rubber Chicken is recommended by Stan S. Stanman' (java.lang.String)
    ==> :name = 'New Rubber Chicken' (java.lang.String)
    ==> :price = '73' (java.lang.Integer)
I/ProductDaoImpl, insert (line 94): INSERT OR REPLACE INTO products (description, name, price) VALUES (:description, :name, :price)
I/ProductDaoImpl, insert (line 103): ==> :description = 'New Pint of Grog is the best sold product on Mêlée Island' (java.lang.String)
    ==> :name = 'New Pint of Grog' (java.lang.String)
    ==> :price = '209' (java.lang.Integer)

Kripton generates for us on compile-time all the code necessary to log SQL operations. The pieces of information printed are:

  • The SQL code
  • The parameter with its value and type
  • For the select operation, the number of the rows extracted.

If you want to disable this feature, you can simply set to false an attribute named logs on @BindDataSource to disable SQL logs generation.

https://gist.github.com/xcesco/1ab0133a0b7d31cb39b4077276149cd1#file-appdatasource-java


@BindDataSourceOptions(populator = AppDataSourcePopulator.class)
@BindDataSource(version = 1,
        daoSet = {CommentDao.class, ProductDao.class},
        fileName = "app.db",
        log = false,
        typeAdapters = {
            @BindSqlAdapter(adapter = DateMillisecondsTypeAdapter.class)}
)
public interface AppDataSource {
}

As usual, for more information about Kripton Persistence Library, please visit:

Happy coding!

xcesco

Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *