Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add emit_sql_as_comment option to Go code plugin #2735

Merged
merged 1 commit into from
Jan 2, 2024

Conversation

jasoncabot
Copy link
Contributor

@jasoncabot jasoncabot commented Sep 15, 2023

This option adds the raw SQL query as a comment to the generated query function, defaulted to false.

This is useful when working in an IDE that displays comments over functions, you are able to glance at the actual SQL query that will be executed without having to lose context of where you are in the current file you are working on.

If you have a query with comments already, it will add the query as a Code Block underneath

Input:

-- name: ListBar :many
-- Lists all bars
SELECT id FROM (
  SELECT * FROM bar
) bar;

Output:

// Lists all bars
//
//	SELECT id FROM (
//	  SELECT id FROM bar
//	) bar
func (q *Queries) ListBar(ctx context.Context) ([]int32, error) {

Example of how this shows up in VS Code when referencing the function:

image

If you haven't already got a comment, it will add the name of the function - I wasn't 100% sure about this but it allows the godoc comment to still have valid formatting when running gofmt

Input:

-- name: RemoveBar :exec
DELETE FROM bar WHERE id = $1;

Output:

// RemoveBar
//
//	DELETE FROM bar WHERE id = $1
func (q *Queries) RemoveBar(ctx context.Context, id int32) error {

Example of how this shows up in VS Code when referencing the function:

image

@jasoncabot
Copy link
Contributor Author

@kyleconroy could you take a look at this PR and let me know if it's an option that could be merged in? It's a small quality-of-life improvement that really helps in our project (and behind config, off by default)

Also let me know if I missed anything with the update - I wasn't 100% sure of the config changes

@andrewmbenton
Copy link
Collaborator

Sorry for the delayed response. I understand the improved ergonomics of having the query in a godoc for certain editors but adding yet another configuration flag for a small addition to our templates system isn't a trade-off we want to make at the moment.

Ideally our Go codegen would be extensible enough that you could add this without changing anything about sqlc itself, but sadly it isn't that flexible yet.

@andrewmbenton
Copy link
Collaborator

We've now had at least one other request for this feature, so I'm going to reopen and attempt to rebase in order to merge.

@jasoncabot
Copy link
Contributor Author

Thanks for picking this up again. I actually had a rebased (on v1.24) version on my fork at jasoncabot@aa15d6a

@andrewmbenton
Copy link
Collaborator

Thanks for picking this up again. I actually had a rebased (on v1.24) version on my fork at jasoncabot@aa15d6a

Oh excellent. I will close my PR if you pull your rebased changes into this one.

This option adds the raw SQL query as a comment to the generated query function

This is useful when working in an IDE that displays comments over functions, you are able to glance at the actual SQL query that will be executed without having to lose context of where you are in the current file you are working on.
@jasoncabot
Copy link
Contributor Author

Sorry it took a while, finally got around to pushing up my changes here

@kyleconroy kyleconroy merged commit d336e4a into sqlc-dev:main Jan 2, 2024
8 checks passed
@jasoncabot jasoncabot deleted the add-sql-as-comment branch January 4, 2024 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants