honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
aratare 2021-04-01T06:46:48.078300Z

Hi there. I’m having a problem with Postgresql not being very happy with HoneySQL@v2 converting :foo/bar to "foo"."bar" when set-ing a new value. For example:

(-> (helpers/update :user)
      (helpers/set {:foo/bar 1})
      (helpers/where [:= :user/id id])
      (helpers/returning :*)
      (sql/format {:dialect :ansi}))
will not work because PSQL requires that columns do not contain the table name. Is there a way to disable this behaviour? I can just convert those keywords to simple keywords but that’s a bit ugly. Thanks in advance.

seancorfield 2021-04-01T06:49:22.078600Z

Open an issue on github and I'll fix it tomorrow. It's a bug.

aratare 2021-04-01T06:49:34.078800Z

Will do. Thanks 🙂

aratare 2021-04-01T13:30:46.080400Z

Hi there. Where should I be looking into if I want to configure automatic conversion between kebab-case and snake_case for both table names and column names? Thanks in advance.

dharrigan 2021-04-01T13:33:27.080600Z

it does that already

aratare 2021-04-01T13:35:12.081Z

It doesn’t for me for some reason…

aratare 2021-04-01T13:36:05.082100Z

I’m using Hikari for pooling, and every time I use honeysql it always yell at me for not having the proper relation

dharrigan 2021-04-01T13:36:06.082300Z

are you using next jdbc?

aratare 2021-04-01T13:36:10.082500Z

yep

dharrigan 2021-04-01T13:36:16.082800Z

do you have csk on your classpath?

aratare 2021-04-01T13:36:31.083100Z

I dont know what that is sorry

dharrigan 2021-04-01T13:36:39.083400Z

camel snake kebab

aratare 2021-04-01T13:36:43.083600Z

oh yeah I do

aratare 2021-04-01T13:36:49.083800Z

I have it in lein

aratare 2021-04-01T13:37:06.084200Z

do I need to also require it like next.jdbc.datetime?

aratare 2021-04-01T13:38:39.085Z

so here’s what I have atm:

;; conn
(connection/->pool HikariDataSource (select-keys env [:jdbcUrl]))

(s/defn read-user-tab :- [TabDB]
  [{:user/keys [id]} :- {:user/id s/Uuid}]
  (log/info "Reading tabs for user id" id)
  (nj/with-transaction [tx db]
    (nj/execute! tx (-> (helpers/select :*)
                        (helpers/from :tab)
                        (helpers/where [:= :tab/user-id id])
                        (sql/format {:dialect :ansi})))))

aratare 2021-04-01T13:39:19.085600Z

I’m getting yelled at that user-id relation doesn’t exist

aratare 2021-04-01T13:39:29.086Z

it’s called user_id in the db

dharrigan 2021-04-01T13:39:31.086200Z

you don't need the :tab/user-id

dharrigan 2021-04-01T13:39:41.086500Z

just [:= :user-id id]

aratare 2021-04-01T13:40:23.087300Z

let me test real quick

aratare 2021-04-01T13:49:26.087700Z

@dharrigan No luck. It’s still giving me ERROR: column "user-id" of relation "tab" does not exist

aratare 2021-04-01T13:50:29.088Z

with or without the tab namespace

aratare 2021-04-01T13:50:39.088300Z

changing it to user_id works as expected

dharrigan 2021-04-01T13:50:55.088600Z

question why dialect ansi?

aratare 2021-04-01T13:51:14.089Z

because I want quoting

dharrigan 2021-04-01T13:51:26.089500Z

the quoting may disable the undasherizing

aratare 2021-04-01T13:51:32.090Z

@_@

aratare 2021-04-01T13:52:21.090200Z

holy heck

aratare 2021-04-01T13:52:23.090500Z

you’re right

aratare 2021-04-01T13:52:29.090700Z

I just removed it and it works

dharrigan 2021-04-01T13:52:36.091Z

:party-corgi:

aratare 2021-04-01T13:52:49.091300Z

haha is that even intended?

dharrigan 2021-04-01T13:53:07.091700Z

probably. Sean wouldn't do anything without a reason 🙂

dharrigan 2021-04-01T13:53:27.092200Z

it's probably because as you're using ansi, it won't make any assumptions about the identifiers

dharrigan 2021-04-01T13:53:33.092500Z

it'll treat them "literally"

dharrigan 2021-04-01T13:53:43.092800Z

rather than trying to undasherize them

aratare 2021-04-01T13:54:06.093200Z

I mean I can rename the table, but it’s hard trying to find anything more suitable than user

aratare 2021-04-01T13:56:37.094Z

I have another problem it seems. The returned map still keeps it as user_id

aratare 2021-04-01T13:58:38.094500Z

Well that’s tomorrow’s problem. Time for me to do something else 😅

dharrigan 2021-04-01T14:02:06.095100Z

User is the only convention I break (keeping tables singular) and I call it users

dharrigan 2021-04-01T14:02:16.095300Z

makes life simple 🙂

seancorfield 2021-04-01T16:34:06.098300Z

@rextruong To clarify some stuff about the camel snake kebab stuff: that only affects next.jdbc and it only affects 1) names in ResultSet’s that the library converts to Clojure and 2) names in Clojure data structures that the library converts to SQL — it does not (cannot) affect anything in the SQL string itself.

seancorfield 2021-04-01T16:36:14.100Z

HoneySQL V2 defaults to :ansi dialect so you don’t need to provide it as an option (it’s relatively harmless to do so). If you specify a dialect, you get quoting. You can also ask for :quoted true to get quoted with the current/default dialect.

seancorfield 2021-04-01T16:37:49.101300Z

If you ask HoneySQL to quote names, it does so without dealing with -/`_` — in both V1 (which is the code @dharrigan linked to) and V2 here: https://github.com/seancorfield/honeysql/blob/v2/src/honey/sql.cljc#L150-L169

seancorfield 2021-04-01T16:39:20.101600Z

user=> (-> {:select :* :from :tab :where [:= :tab/user-id 42]} (sql/format))
["SELECT * FROM tab WHERE tab.user_id = ?" 42]
user=> (-> {:select :* :from :tab :where [:= :tab/user-id 42]} (sql/format {:quoted true}))
["SELECT * FROM \"tab\" WHERE \"tab\".\"user-id\" = ?" 42]

seancorfield 2021-04-01T16:40:45.102100Z

See some of the important differences around V1/V2 handling of names here https://github.com/seancorfield/honeysql/blob/v2/doc/differences-from-1-x.md#option-changes