sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Preserve upper-case when mapping SQL identifiers to Go identifiers

Open em-ily-dev opened this issue 3 weeks ago • 6 comments

What do you want to change?

It would be great if sqlc could leave upper-case letters as upper-case when mapping SQL identifiers to Go identifiers.

The existing behavior works pretty well for all-lowercase SQL identifiers using underscores:

(SQL)          (Go)
request_id  => RequestID
http_id     => HttpID
artwork_key => ArtworkKey

But some databases use camel-case naming convention. Currently, sqlc changes upper-case letters to lower-case, producing:

(SQL)         (Go)
RequestID  => Requestid
HTTPID     => Httpid
ArtworkKey => Artworkkey

My proposal: leave upper-case letters as upper-case, and otherwise keep the existing behavior. This wouldn't affect anyone who uses all-lowercase SQL names, and it would produce nicer results for mixed-case SQL names:

(SQL)         (Go)
RequestID  => RequestID
HTTPID     => HTTPID
ArtworkKey => ArtworkKey

If this is acceptable, I'd be happy to submit a patch.

How does it sound?

What database engines need to be changed?

No response

What programming language backends need to be changed?

Go

em-ily-dev avatar Nov 30 '25 00:11 em-ily-dev

I think the desired behaviour should be to only uppercase the first character and let the reset of the characters preserve their original case. If that's the case, I have fixed it by replacing strings.Title with code that only uppercases the first character.

theAnuragMishra avatar Dec 01 '25 20:12 theAnuragMishra

It's good IMO for sqlc to uppercase letters that follow underscores, as it currently does. The existing call to strings.Title uppercases the first letter and preserves the case of the rest. The problem as I see it is that the entire name is lowercased at some point before StructName is even called.

(BTW, this also means that the rename map in sqlc.json has to use all-lowercase keys. Which is kind of surprising and arguably a bug?)

em-ily-dev avatar Dec 04 '25 23:12 em-ily-dev

strings.Title lowercases the rest of the characters except the first one, and that's the problem I think?

theAnuragMishra avatar Dec 04 '25 23:12 theAnuragMishra

The strings.Title function doesn’t lowercase the other letters. It only uppercases letters that follow word boundaries. It leaves the rest as they were.

https://go.dev/play/p/LfFvIXQqXsc

em-ily-dev avatar Dec 05 '25 20:12 em-ily-dev

Oh I stand corrected here! My source of information about strings.Title was wrong.

theAnuragMishra avatar Dec 05 '25 21:12 theAnuragMishra

I tried everything to find where the field names are being lowercased but hard luck..

theAnuragMishra avatar Dec 07 '25 15:12 theAnuragMishra