sqlite-net icon indicating copy to clipboard operation
sqlite-net copied to clipboard

Cancelable queries

Open csm101 opened this issue 1 year ago • 3 comments

Hi, I anticipated I was implementing this in Issue #1174 (this code contains also the small fix I proposed in pull request #1175 )

This modification extends SQLiteConnection and SQLiteAsyncConnection with a mechanism that allows to terminate long running queries by the means of setting a cancellation token. to interrupt "sqlite server side" processing it makes use of the sqlite3_interrupt() api, that I had to map myself, since it wasn't already imported in the source code. to interrupt record fetch loops it checks the cancellation token status for each record fetched. When an operation is aborted by the cancellation token you always get a OperationCanceledException.

I added also some tests of this feature.

for connection.Table() access methods I added a specific CancelToken(cancTok) call to be added in the "builder pattern" chain, so a cancellable query would look like this:

CancellationTokenSource tokSource= new ();

try
{
    var recs = conn.Table<MyRecType>()
        .Where(x=>....)
        .CancelToken(tokSource.Token) // <<<
        .ToList();
}
catch (OperationCanceledException)
{
   Writelog('execution has been aborted!');
}

of course tokSource.Cancel() must be executed by some other task while the query is still executing.

Whenever possible, I added an optional CancellationToken? cancTok=null parameter to existing apis, but for Apis that were expecting a params args[] for the last parameter, this wasn't possible. in such cases I defined an overloaded version that accepts the cancellation token as first parameter.

For example I had to define a sqliteConnection.Query<T>(CancellationToken tok, string sql, params args[]) overload.


background : why I need this? I have an android application where the end user can type a search criteria and see the results of that search criteria being found immediately every time he changes the search text, even while he is typing (actually the query is re-executed whenever the user stops typing for at least 300 ms). the problem is that, if the table contains a lot of data I really need to be able to cancel the execution of the current query (if it has not finished yet) as soon the user starts typing again. it might be useful also in other scenarios, but in the one I need it it would really make the application much more responsive.

csm101 avatar Jun 21 '23 17:06 csm101

That is a great addition to the package, I hope that this gets merged soon, as I would also love to use this functionality. Nice work!

Dokug avatar Aug 10 '23 12:08 Dokug

the checks fail because of this error: "Testhost process for source(s) '/Users/runner/work/sqlite-net/sqlite-net/tests/SQLite.Tests/bin/Debug/netcoreapp3.1/SQLite.Tests.dll' exited with error: You must install or update .NET to run this application."

I don't understand why, with this commit, is failing: I didn't change any project settings, just the .cs source file...

csm101 avatar Sep 22 '23 09:09 csm101

But... is now sqlite-net a dead project?

csm101 avatar Sep 27 '23 07:09 csm101