4

I have some long-running queries that I'd like to cancel mid execution if the API request is canceled by the browser. I'm using SqlKata to execute my queries and I don't see a param for cancellation tokens. How would I be able to have my query canceled?

I would want something like this query.GetAsync<T>(cancelationToken: cancelationToken)

Moshe
  • 91
  • 7
  • 1
    Welcome to StackOverflow. Have you checked [this issue](https://github.com/sqlkata/querybuilder/issues/302) of SqlKata? – Peter Csala May 31 '21 at 13:12
  • I saw the ticket but it's pretty old and I didn't see any resolution. I was hoping that by now there's a fix. – Moshe May 31 '21 at 13:38
  • If it is not yet fix by the SqlKata community then you can find at most workarounds. – Peter Csala May 31 '21 at 13:56

1 Answers1

5

Looks like the latest version of sql kata has it as one of the params. I just upgraded packages SqlKata and SqlKata.Execution to version 2.3.3 and I see it in the source code.

Alternatively, you can manually run it via Dapper by getting the SQL string and bindings from SqlKata and passing it into Dapper. I didn't test this but it would be something like this:

var connection = new SqlConnection(connectionString);
var compiler = new SqlServerCompiler();
var db = new QueryFactory(connection, compiler);
var query = db.Query("TableName"); // write your query here

var sqlResult = db.Compiler.Compile(query);
var sqlString = sqlResult.Sql;
var bindings = sqlResult.Bindings;

await using var connection = new SqlConnection(connectionString);
var result = await connection.QueryAsync(new CommandDefinition(sqlString, bindings, cancellationToken: cancellationToken));
Moshe
  • 91
  • 7
  • Yes it was recently added, the correct version at this date should be https://www.nuget.org/packages/SqlKata/2.3.3, the 3.2.3 was published by mistake and unlisted from Nuget – amd Jun 01 '21 at 17:10
  • Thanks. I edited my response to the correct version. – Moshe Jun 01 '21 at 18:16