honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
aratare 2021-04-02T01:33:01.104Z

Thanks for the explanation and the fix @seancorfield. I have one more question if that’s okay. I’m currently getting user-id returned as snake case from execute-one! with the following code:

(let [tab {:id       (java.util.UUID/fromString "a768c90a-e1f2-4b66-bfd6-a9d8773dffd8"),
             :name     "foo",
             :password ".Xp8",
             :user-id  (java.util.UUID/fromString "740cb441-ce5e-4f8f-b328-009dc44ebda6")}]
    (nj/with-transaction [tx db]
                         (nj/execute-one! tx (-> (helpers/insert-into :tab)
                                                 (helpers/values [tab])
                                                 (helpers/returning :*)
                                                 (sql/format)))))

=> #:tab{:id #uuid"a768c90a-e1f2-4b66-bfd6-a9d8773dffd8",
      :name "",
      :password ".Xp8",
      :created #object[java.time.OffsetDateTime 0x227987af "2021-04-02T01:30:20.962643Z"],
      :updated #object[java.time.OffsetDateTime 0x46632f55 "2021-04-02T01:30:20.962643Z"],
      :user_id #uuid"740cb441-ce5e-4f8f-b328-009dc44ebda6"}
Am I doing something wrong here? Thanks in advance.

seancorfield 2021-04-02T01:35:54.104700Z

What is the column actually called in the DB @rextruong?

aratare 2021-04-02T01:36:33.105300Z

user_id, hence the reason why I was asking about how HoneySQL handles conversion between the two cases.

aratare 2021-04-02T01:37:01.106200Z

You mentioned “it only affects 1) names in ResultSet’s that the library converts to Clojure” so that’s why I’m expecting user-id instead

seancorfield 2021-04-02T01:37:22.106800Z

Since you are using the default result set builder, you get <table>/<column>.

seancorfield 2021-04-02T01:37:52.107400Z

If you have Camel Snake Kebab on your classpath, that enables a couple of new result set builders that you can choose to use.

seancorfield 2021-04-02T01:38:03.107800Z

It doesn’t change the default behavior.

aratare 2021-04-02T01:38:18.108200Z

Ah that makes sense

seancorfield 2021-04-02T01:39:19.109200Z

next.jdbc’s default is to leave names alone going in both directions (with the caveat that you get qualified names out).

aratare 2021-04-02T01:39:58.109900Z

I see. So I’d need to change the builder when I’m setting next.jdbc up.

aratare 2021-04-02T01:40:25.110300Z

Will have a go at it now. Thanks a lot 🙂

seancorfield 2021-04-02T01:41:30.111600Z

You can specify :table-fn and :column-fn to affect how the “friendly” SQL functions deal with (Clojure data structure) input and you can specify :builder-fn to affect how almost all functions deal with (`ResultSet` to Clojure data structure) output.

👍 1
seancorfield 2021-04-02T01:43:00.113Z

If you want a particular set of behaviors to be your “default” (for most calls), take a look at next.jdbc/with-options but also be aware of the caveats around that near the end of https://cljdoc.org/d/seancorfield/next.jdbc/1.1.646/doc/getting-started#options--result-set-builders

seancorfield 2021-04-02T01:43:54.114500Z

HoneySQL operates completely “independently” of next.jdbc insofar as all it does is turn Clojure data into SQL (plus parameters) so any translation of names there only corresponds to next.jdbc’s “input” via the “friendly” SQL functions.

seancorfield 2021-04-02T01:44:42.115300Z

(I definitely need to clarify that in HoneySQL’s docs!)

aratare 2021-04-02T01:45:45.115700Z

So I was reading that link and I noticed that the examples are mostly just jdbc/get-datasource. I assume with-options also works with connection/->pool?

seancorfield 2021-04-02T01:46:14.116300Z

Yes, because it “is-a” DataSource.

aratare 2021-04-02T01:46:28.116600Z

Lovely. Thanks.

seancorfield 2021-04-02T01:47:22.117200Z

with-options: “Given a connectable/transactable object and a set of (default) options that should be used on all operations on that object, return a new wrapper object that can be used in its place.” — a connectable is anything you can get a Connection from.

seancorfield 2021-04-02T01:47:57.117800Z

(you just have to be careful that if you get a Connection from it, that’s a bare Java object with no “options” attached)

👍 1