honeysql

igrishaev 2024-04-03T13:08:25.116419Z

How can I build a where clause that involves the @? operator for jsonb path? What I'm doing is:

(sql/register-op! (keyword "@?"))

(sql/format [(keyword "@?") 
               :entity 
               "$.foo.bar[*].id ? (@ == 42)"]
              {:numbered true})

["entity @?? $1" "$.foo.bar[*].id ? (@ == 42)"]
But I'm getting @?? for some reason. Is it because of extra quoting of question marks or something?

igrishaev 2024-04-04T14:12:56.086159Z

Dear @seancorfield Iโ€™m asking your forgiveness. You were right about double ??. The thing is, I was experimenting with honeysql in repl and then copied queries to psql. Thatโ€™s why it didnโ€™t like ??. But in JDBC , an extra ? is required. Can you please rollback the last PR? My shame.

seancorfield 2024-04-04T17:05:58.923259Z

No problem. The changes for #528 have been reverted.

igrishaev 2024-04-04T17:06:22.181159Z

Thank you! Again, sorry for the false alert.

kolstae 2024-04-03T13:23:46.402359Z

Could be down to what you pass into sql/format - that's not a valid honeysql query I think. Try using it in a valid query

igrishaev 2024-04-03T13:26:39.447479Z

It still doesn't work for me:

(sql/format {:select 42 :where [:and [honey.sql.pg-ops/at? "aa" "bb"]]})
["SELECT ? WHERE (? @?? ?)" 42 "aa" "bb"]

igrishaev 2024-04-03T13:27:07.447349Z

the honey.sql.pg-ops/at? variable is just (keyword "@?") as I did before

๐Ÿ’ฏ 1
igrishaev 2024-04-03T13:29:40.504189Z

Ah! The same issue is in the tests:

kolstae 2024-04-03T13:30:33.615819Z

Yeah sorry I couldn't make it work either I just assumed ๐Ÿ™ˆ

valerauko 2024-04-03T13:38:49.458039Z

:@? (or for that matter any keyword that starts with @ ) isn't really valid it just happens to work sometimes ๐Ÿ˜ž i've never used jsonb through honeysql but if you can use string operators that should be way safer

igrishaev 2024-04-03T13:41:40.309849Z

sorry @vale what do you mean by "string operators"?

igrishaev 2024-04-03T13:44:20.977309Z

https://github.com/seancorfield/honeysql/issues/528

igrishaev 2024-04-03T13:45:17.976189Z

Yeah I'm trying to build a raw expression. But I don't want to put the jsonpath inline. It might lead to injections.

kolstae 2024-04-03T13:46:24.989349Z

Seems :raw still supports params though:

(sql/format {:select [:a [[:raw ["@var := " ["foo"]]]]]})
;;=> ["SELECT a, @var := (?)" "foo"]

igrishaev 2024-04-03T13:50:24.732099Z

option one with params

(sql/format {:select [:a [[:raw ["entity @? " [:param :path]]]]]}
              {:numbered true
               :params {:path "$.foo.bar"}})
option two with inline
(sql/format [:raw "entity @?" \space [:inline "$.foo.bar[*].id ? (@ == 42)"]])
["entity @? '$.foo.bar[*].id ? (@ == 42)'"]

p-himik 2024-04-03T14:10:55.621499Z

FWIW I myself would probably use jsonb_path_exists with silent set to TRUE till the issue with @? (and, seemingly, any ops containing ?) is fixed.

igrishaev 2024-04-03T14:13:32.584419Z

well, the jsonb_path_* functions do not work with my index which is USING GIN (entity jsonb_path_ops)

๐Ÿ‘ 1
seancorfield 2024-04-03T17:36:07.123939Z

I was under the impression that all SQL dialects always treated ?? as an escaped ? and therefore @?? was correct (since I don't use PostgreSQL). Thanks for the issue -- will get it addressed shortly.

2024-04-03T13:44:17.434309Z

2024-04-04T00:00:47.778169Z