Hey, I am trying to figure out how to make a raw query in ecto.
The query shall search in an array of nested jsonb elements. and I had troubles with the parameter in ecto.
I specifically looked at these issues:
My question is a “mixture” of these I guess:
and this issue:
The working solution at the moment is: variable
is the parameter, it comes in form of a "username"
string.
raw_sql = ~s{
SELECT * FROM
(SELECT *,
generate_subscripts(docs_map, 1) AS s
FROM docassoc where archived_timestamp = '9999-01-01 01:01:00') AS foo
WHERE jsonb_path_exists(docs_map[s], '$.ad_name[*] ? (@ == "#{variable}" )') }
Ecto.Adapters.SQL.query!(
AwardDB.Repo, raw_sql , []
)
because I was not able to pass the parameter via $1
when the sql variable is wrapped like this. (as in the first issue above)
Can someone help my by pointing out how this should/could be improved. I guess like this it is not really “safe” (or is it?).
The ecto learning-curve is quite steep for me, I have not managed by using fragments (of course starting with jsonb adds to the steepness ). Can every query, thus also mine be represented using fragments? Then I’ll try this again. On the other hand if it gets much more complicated, I think readability would also be an issue somewhen
Many thanks for thinking through and for possible ideas!