honeysql

MegaMatt 2025-03-25T21:09:07.199279Z

it seems like honey sql replaces forward slash with . eg. :my-field/id becomes my_field.id is there a way to keep the slash? it seems postgres supports slashes in field names? for that matter how can i keep dashes as well?

MegaMatt 2025-03-25T21:20:09.021309Z

oh, i think i might have figured it out, it is some combination of a keyword of a quote (not sure if i’m saying that right)

p-himik 2025-03-25T21:38:41.965069Z

Yeah, :'my-field/id. But note that it will not replace - with _, so you have to use :'my_field/id.

MegaMatt 2025-03-25T21:45:08.279889Z

(defmacro kw->quoted-kw [kw]
    (let [as-txt (symbol (str "'" (namespace kw) "/" (name kw)))]
      `(keyword (quote ~as-txt))))
I built this to convert a keyword but it won’t quote it, although it does leave the characters i want.

MegaMatt 2025-03-25T21:45:41.102799Z

I can get it to work with a literal :'my-field/id but having trouble building a general purpose kw->quoted-kw

p-himik 2025-03-25T22:02:11.704839Z

symbol parses the string so it ends up being a symbol with a namespace. keyword inherits the namespace, so it's exactly the same as if you wrote :'.../... by hand anyway.

MegaMatt 2025-03-25T22:04:21.399979Z

since i always need to format keywords this way should i be looking at registering a quote-fn somehow or maybe a default-quote atom or something similar?

seancorfield 2025-03-25T22:09:37.359769Z

What database are you working with, that you have / in column names?

MegaMatt 2025-03-25T22:10:17.801019Z

i’m migrating a mongo db into postgres and would prefer to keep the same naming conventions unless i bump into solid reasons to change that.

seancorfield 2025-03-25T22:42:27.929409Z

You might get close to what you want with:

(honey.sql/register-dialect!
  :mongo
  {:quote    #(honey.sql/strop "\"" % "\"")
   :col-fn   #(if (keyword? %) (subs (str %) 1) (str %))
   :parts-fn vector})
and then
(honey.sql/set-dialect! :mongo)

MegaMatt 2025-03-25T22:43:18.709119Z

I was barking up that tree, i’ll see if this version helps get me across the line 🙏

seancorfield 2025-03-25T22:43:27.709279Z

The existing :nrql dialect treats :my-field/id as a single column name, but uses MySQL-style quotes. This is a copy of that but using ANSI-style quotes.

MegaMatt 2025-03-26T17:10:17.711209Z

thanks, this worked great