I'm trying to dynamically run queries against my Postgres database, but can't fully wrap my head around it.
The solution I'm looking for is one where I can set the query dynamically, perhaps by appending parameters to the final query throughout the code, and then have only one instance of the query being executed.
As mentioned in the title I am using SQLBoiler to interface with Postgres.
Here's what I'm looking for in pseudo code:
final_query := QueryMod{
Where("(mt_mas = ? or mt_mem like ?) and mt_group = ?", uint(uid), `%"`+strconv.Itoa(uid)+`"%`, bool(mt_group_bool)),
}
if a == 1 {
final_query = append(final_query, And(" and mt_important = ?", bool(false)))
} else {
final_query = append(final_query, And(" and mt_ness = ?", bool(true)))
}
res_mt_count, err := models.MTs(
final_query,
).All(CTX, DB)
Thankful for any help along the way! :)
invalid composite literal type qm.QueryModon the linefinal_query := QueryMod{.QueryModis an interface type so yes, that expression is illegal. If you want a slice of a type you just need to prepend[]in front of that type. e.g.final_query := []QueryMod{Where(...)}.cannot use final_query (type []qm.QueryMod) as type qm.QueryMod in argument to models.MTson the rowres_mt_count, err := models.MTs(. Any idea how to move forward?QueryModinterface. Then either use that instead of[]QueryMod{...}or keep using[]QueryModbut convertfinal_queryto the declared slice type before passing it toMTs. Or, also, you could just redefineMTsto accept[]QueryModinstead of justQueryMod.