Skip to content

Conversation

rodionstolyarov
Copy link

sqlc already have an option to emit the methods with db argument emit_methods_with_db_argument, but sometimes it is more convenient to keep the current db session in the context. It would be nice if sqlc could extract db from the context itself.

To do this, we need to pass a function that knows how to get db form the context to the New method of Querier and then use this function in all the methods generated.

This PR adds support for a new option get_db_from_context that implements this feature.

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang labels Jan 4, 2025
@kyleconroy
Copy link
Collaborator

@rodionstolyarov Thank you very much for your first contribution to the project!

While I understand that you'd like this feature, it's not something that's been asked for often. Since it adds for configuration options and complicates the generated code. Instead, I'd suggest building this either as a plugin or a separate codegen package that wraps the existing emit_methods_with_db_argument code with methods that pull the database session out of the context and pass it to those methods.

@rodionstolyarov
Copy link
Author

I see, it's wise not to complicate the code unnecessarily. Thanks for your feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants