Fork me on GitHub
#honeysql
<
2022-03-20
>
aratare08:03:12

Hi there. I have a question regarding keyword->column conversion in honeysql. I have a Tab table that has a column called is_protected?. Naturally, when using honeysql Clojure's keywords are converted into snake case upon formatting, but in this case that won't work because is_protected? needs to be quoted properly (i.e. "is_protected?") otherwise the database will get upset. I've tried :quoted true but that will ignore conversion all together, which is not what I want. Is there any nice way to be able to quote after conversion has been done? Thanks in advance.

dharrigan10:03:27

Not sure, but you could use raw for the time being?

dharrigan10:03:30

user=> (-> (select :foo) (from :bar) (where [[:raw "is_protected? = true"]]) sql/format)
["SELECT foo FROM bar WHERE (is_protected? = true)"]

dharrigan10:03:34

would that help?

dharrigan10:03:58

sorry, this:

dharrigan10:03:00

user=> (-> (select :foo) (from :bar) (where [[:raw "'is_protected?' = true"]]) sql/format)
["SELECT foo FROM bar WHERE ('is_protected?' = true)"]

dharrigan10:03:05

forgot about quoting.

aratare10:03:12

@dharrigan Ah I see. Would that also work with insertion?

aratare10:03:47

I've decided to just change the column name in my PostgreSQL db for the time being

dharrigan10:03:14

yes, also for insertion.

dharrigan10:03:34

(that's a better approach, having ? in column names is a bit weird)

aratare10:03:13

Yeah it's not a common way to name your column, I just decided to make it for seamless transition between the db and Clojure land

dharrigan10:03:25

Maybe, I don't know, but maybe, Sean might consider something - a switch perhaps? that if the user so-chooses to enable, that on the column translation on the way in/out, if the data type is boolean to convert the clojure to/from ?

dharrigan10:03:54

i.e,. if a column name is enabled and type boolean, then on the data map on the way out, it might be :table_name/enabled? as the key.

dharrigan10:03:36

<shrug> perhaps asking and gauging his thoughts on that one 🙂

aratare10:03:08

I think a more general approach would be to have some sort of conversion layer where you can provide a map of from/to keys?

aratare10:03:01

So if you feed it is-protected? it can be converted into is_protected and vice versa.

aratare10:03:08

May be there's already something like this which I haven't got to yet 😅

dharrigan10:03:27

I think you're looking at a more low level solution here, for example, I use next.jdbc which is the one that actually does the querying etc., of the db

dharrigan10:03:47

in there, you can define your own protocols to decide on how column names etc. are handled.

dharrigan10:03:33

Reading around here might help

aratare10:03:41

my brain is a bit fried at the moment but I'll have a look

aratare10:03:47

Thanks a lot for the help 🙂

dharrigan10:03:51

you're most welcome.