Skip to content

feat(codegen): suffix generated query constants with SQL - #4704

Open
robsonpeixoto wants to merge 2 commits into
sqlc-dev:mainfrom
robsonpeixoto:claude/query-constant-sql-suffix-2d19dded
Open

robsonpeixoto wants to merge 2 commits into
sqlc-dev:mainfrom
robsonpeixoto:claude/query-constant-sql-suffix-2d19dded

Conversation

@robsonpeixoto

Copy link
Copy Markdown
Contributor

Summary

The Go generator now ends every query-text constant with SQL, so a call site shows that the value is the query text.

-const getAuthor = `-- name: GetAuthor :one
+const getAuthorSQL = `-- name: GetAuthor :one
 SELECT id, name, bio FROM authors
 WHERE id = $1 LIMIT 1
 `

 func (q *Queries) GetAuthor(ctx context.Context, id int64) (Author, error) {
-	row := q.db.QueryRow(ctx, getAuthor, id)
+	row := q.db.QueryRow(ctx, getAuthorSQL, id)
 // internal/codegen/golang/result.go
 if options.EmitExportedQueries {
-	constantName = sdk.Title(query.Name)
+	constantName = sdk.Title(query.Name) + "SQL"
 } else {
-	constantName = escape(sdk.LowerTitle(query.Name))
+	constantName = sdk.LowerTitle(query.Name) + "SQL"
 }
  • The rule is the same for every driver (database/sql, pgx v4/v5, batch, prepared statements) because all of them use ConstantName.
  • escape is not needed anymore. No Go keyword ends in SQL, so a query named Select now gives selectSQL and not select_.
  • With emit_exported_queries, the constant Foo collided with an enum or struct named Foo. The suffix removes that collision. The duplicate_go_names/query_constant case expected this collision as an error, so it is now a passing case that keeps the two names separate.
  • The golden files under internal/endtoend/testdata/, the examples/ module and the samples in docs/howto/ are regenerated. docs/reference/changelog.md is not changed.

Evidence

  • Before: the committed golden files contain const getAuthor = ..., and duplicate_go_names/query_constant expects query constant name conflicts with enum name: Foo.
    After: with PostgreSQL and MySQL running, go test ./... passes in the base and managed-db contexts. go build ./... passes under internal/endtoend/testdata, and go vet --tags=examples ./... passes under examples/.
  • The opt-in core context (SQLC_TEST_CORE=1) fails on the same 293 subtests before and after this change, so nothing new fails there.

Merge Danger

Door: two-way

Revert the commit to undo the change. Users get the old names back the next time they run sqlc generate.

Blast Radius: generated-code

Every Go user gets a diff in each query.sql.go the next time they run sqlc generate, and sqlc diff in CI fails until they regenerate. Unexported constants are only visible inside the generated package. With emit_exported_queries: true, the constants are public API, so code in other packages that uses queries.GetAuthor will not compile until it changes to queries.GetAuthorSQL. Mention this in the release notes.

Name the constant holding each query's text <name>SQL (or <Name>SQL with
emit_exported_queries), so it reads as SQL at the call site and no longer
collides with an enum or struct of the query's name. The suffix also keeps
the name clear of Go keywords, so it no longer needs escaping.

The duplicate_go_names/query_constant case expected that collision as an
error; it now generates, and pins down that the names stay apart.
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.

1 participant