With an ORM you can construct the prepared statements programmatically.
With an ORM you can construct the prepared statements programmatically.
Absolutely, prepared statements would not be appropriate for that.
> With an ORM you can construct the prepared statements programmatically.
That's not limited to ORMs though. You can also just use a query builder. In fact I would argue that an ORM that provides that functionality is a query builder with additional ORM features on top.
You're still working with SQL as a language, but you're manipulating it directly instead of trying to go via string formatting.
A quick search for "query builder golang" (We are on HN after all) provides the following library:
https://github.com/fragmenta/query
An example from the README:
// In your handlers, construct queries and ask your models for the data
// Find a simple model by id
page, err := pages.Find(1)
// Start querying the database using chained finders
q := page.Query().Where("id IN (?,?)",4,5).Order("id desc").Limit(30)
// Build up chains depending on other app logic, still no db requests
if shouldRestrict {
q.Where("id > ?",3).OrWhere("keywords ~* ?","Page")
}You can use functions to create basic "getters" for application data structures. Layering these through further functions for limits/offsets/etc is simple (and you can write those layers in a generic way).
You just have to get used to the mental mindshift that all your data manipulation code lives in the database, and your application simply consumes data from single function calls.