4 ms·
Manual via https://pkg.go.dev/database/sql https://pkg.go.dev/database/sql with handwritten SQL in the majority of places... but with a wrapper to handle the mo
by buro9 5y ago
Manual via https://pkg.go.dev/database/sql https://pkg.go.dev/database/sql with handwritten SQL in the majority of places... but with a wrapper to handle the more complex search scenarios.
For example these from a multi-tenant SaaS forum platform (it's old, but it's also OSS so I can show you)...
This helper to get connections: https://github.com/microcosm-cc/microcosm/blob/master/helpers/db.go https://github.com/microcosm-cc/microcosm/blob/master/helper... used like this for inserts: https://github.com/microcosm-cc/microcosm/blob/master/models/conversations.go#L191-L225 https://github.com/microcosm-cc/microcosm/blob/master/models... and this for reads https://github.com/microcosm-cc/microcosm/blob/master/models/conversations.go#L436-L487 https://github.com/microcosm-cc/microcosm/blob/master/models... .
But searches... i.e. highly consistent SELECT queries with different WHERE statements (and potentially FROM statements), then in each project I tend to have an idea of a search struct ( https://github.com/microcosm-cc/microcosm/blob/master/models/search_query.go https://github.com/microcosm-cc/microcosm/blob/master/models... ) that will validate the inputs and represent the search query, and then something that will consume that and build the SQL for it ( https://github.com/microcosm-cc/microcosm/blob/master/models/search_metadata.go https://github.com/microcosm-cc/microcosm/blob/master/models... ). This isn't pretty... but it's easy for me to tune, debug, and keeps the rest of the code base very maintainable... all the complexity is here in the search.
The vast majority of SQL is very very simple and needs no ORM, and the complexity is just in the search scenario where I want to be able to tune the performance more than an ORM would allow me to do so.
- ggregoire 5y agoIs there a way to write and import the queries from a SQL file in Go? Like HugSQL (Clojure) or PugSQL (Python). To benefit from the SQL tooling (syntax highlighting, autocomplete and so on).