square / sqlbrite

A lightweight wrapper around SQLiteOpenHelper which introduces reactive stream semantics to SQL operations.
https://square.github.io/sqlbrite/3.x/sqlbrite/
Apache License 2.0
4.57k stars 416 forks source link

SQLDelight 0.7.0 and SQLBrite 3.2.0 Compatibility #251

Closed jfmoy closed 6 years ago

jfmoy commented 6 years ago

Hello everyone,

We have been using SQLDelight and SQLBrite for a long time and would like to thank you guys for your hard work in creating these two very useful libraries.

We are currently upgrading our application to the latest versions (Delight 0.7.0 and Brite 3.2.0); we are a bit confused regarding the usage of the new SQLDelightStatement in combination with the BriteDatabase to get notifications of database operations.

Indeed, with the latest SQLDelight, Insert, update and delete statements now generate SQLDelightStatement classes meant to be used in combination with a SupportSQLiteDatabase. Since SQLBrite is a wrapper and not an implementation of SupportSQLiteDatabase, the only option seems to be passing the wrapped database like below:

final InsertModel insertModel = new InsertModel(briteDatabase.getWritableDatabase(), Model.FACTORY);
insertModel.bind("bla");
insertModel.executeInsert();

The problem in this case, is that the insertion is not done through the BriteDatabase wrapper, and therefore will not send any table triggers, defeating the purpose of SQLBrite. My assumption is that these libraries are meant to work together, so I think we are missing something.

Could you point me in the right direction for leveraging the last SQLDelight version while keeping SQLBrite notifications?

Many thanks for your help.

drymarau commented 6 years ago

The problem in this case, is that the insertion is not done through the BriteDatabase wrapper, and therefore will not send any table triggers, defeating the purpose of SQLBrite. My assumption is that these libraries are meant to work together, so I think we are missing something.

BriteDatabase has a number of methods that will trigger a notification, most interesting are executeInsert and executeUpdateDelete. They take a table (or a Set of tables) and a SupportSQLiteStatement. SqlDelightStatement in 0.7.0 happens to implement SupportSQLiteStatement, so you can pass it as a second parameter.

Your example in this case will look like this:

final InsertModel insertModel = new InsertModel(briteDatabase.getWritableDatabase(), Model.FACTORY);
insertModel.bind("bla");
briteDatabase.executeInsert(insertModel.getTable(), insertModel);
JakeWharton commented 6 years ago

Yeah sorry we don't have a comprehensive sample of the two, but what was said above is correct. Make sure you cache the InsertModel instance rather than create one each time.

Thanks, @drymarev!