Skip to content

feat: add support for CancellationToken in generated methods#420

Open
ballistic-booger wants to merge 6 commits into
DaredevilOSS:mainfrom
ballistic-booger:add-query-cancellation-token-support
Open

feat: add support for CancellationToken in generated methods#420
ballistic-booger wants to merge 6 commits into
DaredevilOSS:mainfrom
ballistic-booger:add-query-cancellation-token-support

Conversation

@ballistic-booger

Copy link
Copy Markdown
Contributor
  • implement CancellationGen to handle optional CancellationToken threading
  • update DbDriver to expose Cancellation property
  • modify CommonGen to support cancellation arguments in method parameters and reader calls
  • update multiple generator types (One, Many, Exec, ExecRows, ExecLastId, CopyFrom) to include CancellationToken in signatures and DB calls
  • update MySqlConnector, Npgsql, and Sqlite drivers to pass CancellationToken to connection and command execution
  • add withCancellationToken option to PluginOptions and RawOptions
  • update README.md with documentation and usage examples for cancellation
  • add unit tests for CancellationGen and integration tests for generated code across all engines

@ballistic-booger
ballistic-booger marked this pull request as draft June 25, 2026 22:55
@ballistic-booger
ballistic-booger force-pushed the add-query-cancellation-token-support branch from 4763a95 to 58b0769 Compare June 25, 2026 23:00
- implement CancellationGen to handle CancellationToken injection into generated method signatures and DB calls
- update DbDriver and CommonGen to support cancellation arguments in async methods
- thread CancellationToken into OpenConnectionAsync, ExecuteAsync, QueryAsync, and other database operations across MySqlConnector, Npgsql, and Sqlite drivers
- add WithCancellationToken option to PluginOptions and RawOptions
- update README.md with documentation and examples for cancellation
- add comprehensive unit tests for CancellationGen and end-to-end codegen validation
- update example request files to include the new plugin option
@ballistic-booger
ballistic-booger force-pushed the add-query-cancellation-token-support branch from 58b0769 to e1bd84d Compare June 25, 2026 23:20
…n option

The withCancellationToken option was added to serialized plugin options,
but only the Postgres request.message fixtures were regenerated. Regenerate
the MySQL and SQLite ones so the codegen sync check passes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@doron050

Copy link
Copy Markdown
Collaborator

@ballistic-booger Hi, need any help with this? 🙏

@ballistic-booger
ballistic-booger marked this pull request as ready for review July 16, 2026 01:32
@ballistic-booger

Copy link
Copy Markdown
Contributor Author

@ballistic-booger Hi, need any help with this? 🙏

Apologies @doron050, I got pulled by other priorities.
I've resolved the conflicts, checks are all passing, and this is now ready for review if you wanted to have a look.
Thanks for reaching out 🍻

@ballistic-booger
ballistic-booger force-pushed the add-query-cancellation-token-support branch from cd13999 to 3ff096c Compare July 16, 2026 05:11

@doron050 doron050 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job! Take a look at the two comments I left, the rest looks solid 🎉

Comment thread Drivers/NpgsqlDriver.cs Outdated
{ "anyarray", new() }
{ "any", new() },
{ "anyarray", new() },
{ "hstore", new() }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

364-366 Is this related in any way to the PR? if so I don't how

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, sorry I forgot to pull this out into a different PR. I'll do that the moment I get a chance.

As for what this is, when sqlc is unable to determine the type, instead of throwing it'll just make it an object

@ballistic-booger ballistic-booger Jul 18, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done with this, I'll follow up with these changes in a different PR

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR for the above 🙂 #423

Comment thread Drivers/Generators/OneDeclareGen.cs Outdated
var dapperArgs = CommonGen.GetDapperArgs(query);
var returnType = dbDriver.AddNullableSuffixIfNeeded(returnInterface, false);

if (dbDriver.Options.WithCancellationToken)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks redundant, the dbDriver.Cancellation.WrapDapperArgs handles the default logic inside so just leave what's in the if as is (same for ManyDeclareGen, ExecDeclareGen and ExecRowsDeclareGen)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure thing 🫡

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed this now too 🙂, thanks for taking the time to review

@ballistic-booger
ballistic-booger force-pushed the add-query-cancellation-token-support branch from 3ff096c to 53e05ae Compare July 18, 2026 20:23
…edundant conditional checks

- remove explicit check for WithCancellationToken in ExecDeclareGen,
ExecRowsDeclareGen, ManyDeclareGen, and OneDeclareGen
- use wrapped callArgs directly in all generated SQL execution methods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants