simolus3 / drift

Drift is an easy to use, reactive, typesafe persistence library for Dart & Flutter.
https://drift.simonbinder.eu/
MIT License
2.46k stars 355 forks source link

Drift files and customStatement's in Batch #2303

Open FaFre opened 1 year ago

FaFre commented 1 year ago

I use some custom inserts during my Batch which I add as customStatement. The downside here is, that I have now two different sources for SQL statements: somewhere inside code and in my drift files. I would like to have all SQL statements neatly in my drift files, where it belongs to.

I'm willing to contribute code, but I don't know what would be a good way to achieve that.

One idea I have would be to generate a GenerationContext that could be added to the batch.

simolus3 commented 1 year ago

Thanks for the suggestion and for offering to help :) I think this idea would be neat, but I also don't have any obvious ideas on how to expose this.

For transactions, we can use keep the interface as it is and push the information that we're in a transaction into the current zone. This doesn't work for batches, since batches don't directly execute the SQL when customStatement is called. Maybe there could be a marker in a .drift file to indicate whether the statement should be used in a batch? Then we can expose it under a different interface (perhaps even as an extension on Batch).

FaFre commented 1 year ago

I have two more ideas to add, I don't know what is preferable tbh, I don't have the complete picture:

  1. Adding the optional variable Batch? batch to the generated function and act accordingly if not null. This could be added to all generated functions.
  2. Allow a new annotation like @rawSql: SELECT 1 to define statements as pure strings