Brain Phrye

code cooking diy fiction personal photos politics reviews tools 


sqlc

[ Listen to article ]

I’ve spent the past month rebuilding bulletin, a bbs-like system I used in college. In doing so I ended up doing a bunch of SQL.

Go is a much more strongly typed language than things like Python or Perl. A lot of my previous experience with doing SQL was in those languages. So doing SQL in Go has been interesting.

I’m not a fan of ORMs in any language. In production I need to be able to connect misbehaving SQL to code and ORMs almost always get in the way of that. For performant SQL you need to understand, well, SQL. Not the vague DSL that an ORM tries to make.

OTOH, raw SQL has some issues. Prepared statements avoid almost all the security issues, but the bigger issue is that with raw SQL you’re essentially compiling code at run time. That’s not ideal.

So sqlc has been an interesting middle ground. It converts raw SQL to Go code. It does this by pulling in migrations and creating an internal structure of the schema that it can then use when parsing the statements and generating code.

It’s clear it’s an idea just at its beginning. And more work is happening here - bob is another version of the same idea. It has rough edges. Forgetting a semicolon can lead to some really confusing errors for example. But the story for types is complicated. You can’t always get what you want and unless you’re really careful there’s a lot of data copying between slightly different structs.

It’s quite usable now. However it’s clear it will make things way better as the idea improves.