honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
lwhorton 2018-06-05T16:55:29.000585Z

heya folks.. i’m trying to use honey with the good ol clojure.java.jdbc, but i’m not really sure how to do insert! given the format that comes out of a honeysql/format:

(-&gt; (insert-into :accounts) (columns :email :password) (values [["<mailto:hi@me.com|hi@me.com>" "pw"]]) sql/format)
&gt; ["INSERT INTO accounts (email, password) VALUES (?, ?)" "<mailto:hi@me.com|hi@me.com>" "pw"]
but the expected format for insert doesn’t line up very well jdbc/insert! :table-name { map of columns -&gt; values }

lwhorton 2018-06-05T16:55:44.000454Z

certainly this is a common problem, how do people thread from honey -> jdbc?

lwhorton 2018-06-05T16:57:05.000178Z

i can get away ( i think ) with just doing a jdbc/query [" not really a query, just regular SQL insert into "] but that seems wonky to me

bja 2018-06-05T17:00:52.000497Z

@lwhorton if you are returning rows, INSERT INTO ... RETURNING ... then you use jdbc/query. Otherwise use jdbc/execute!

bja 2018-06-05T17:01:32.000373Z

in your example, you'd use (jdbc/execute! some-db-reference (sql/format ...))

lwhorton 2018-06-05T17:01:58.000572Z

aha, thanks :thumbsup:

seancorfield 2018-06-05T19:03:41.000746Z

@lwhorton Also note that with the most recent java.jdbc versions, you can pass {:return-keys true} to execute! and get back the generated keys (assuming your driver/DB supports that).

seancorfield 2018-06-05T19:05:17.000082Z

To be honest, tho', I think using java.jdbc/insert! directly is nicer than going through HoneySQL:

(jdbc/insert! db-spec :accounts {:email "<mailto:hi@me.com|hi@me.com>" :password "pw"})

👍 1
seancorfield 2018-06-05T19:05:38.000501Z

There's also insert-multi! for inserting a sequence of records (hash maps).

bja 2018-06-05T20:05:31.000452Z

@seancorfield I almost never use jdbc/insert! because most of my inserts end up using RETURNING due to wanting data from triggers

seancorfield 2018-06-05T20:07:01.000536Z

@bja interesting. I don't think I've ever used returning...

bja 2018-06-05T20:08:08.000107Z

it might just be a pgsql extension

seancorfield 2018-06-05T20:08:13.000521Z

Perhaps adding support for returning to insert! would be possible/worthwhile?

bja 2018-06-05T20:08:41.000366Z

it's especially useful with UPSERT or ON CONFLICT ... in postgres

bja 2018-06-05T20:09:05.000576Z

possibly. I just use honeysql-postgres to support it

bja 2018-06-05T20:09:49.000258Z

the only time I ever step outside of jdbc/query and jdbc/execute! involves DDL (which I handle outside of clojure) and server-side cursors

seancorfield 2018-06-05T20:13:12.000692Z

Looks like returning is DB-specific. I thought Postgres already returned the whole inserted record anyway?

seancorfield 2018-06-05T20:14:10.000710Z

(at least it does in the test suite for java.jdbc).