sql

All things SQL and JDBC...
bartuka 2020-02-20T14:46:19.250100Z

hi, is it possible to make an insert using next.jdbc and get the inserted row back? I have a timestamp column that is created by postgresql and I would like to get this value after insert without the roundtrip of going there again for a select.

dharrigan 2020-02-20T14:47:49.250300Z

insert returning?

bartuka 2020-02-20T14:48:08.250500Z

yes

dharrigan 2020-02-20T14:48:35.250700Z

would that not work for you?

dharrigan 2020-02-20T14:49:09.251100Z

For example, if you're using honeysql, then https://github.com/nilenso/honeysql-postgres has some extensions

dharrigan 2020-02-20T14:49:14.251400Z

one of which is returning

dharrigan 2020-02-20T14:49:44.251700Z

https://github.com/nilenso/honeysql-postgres#upsert

bartuka 2020-02-20T14:49:58.252200Z

I would not like to add a new dependency to the project. I am using next.jdbc right now

dharrigan 2020-02-20T14:50:12.252600Z

you can hand-craft it if you like

dharrigan 2020-02-20T14:50:26.253Z

but it's a tiny library, so <shrug> reuse, rather than re-invent?

bartuka 2020-02-20T15:03:36.253400Z

looks like the return value of sql/insert! already have this information o/

seancorfield 2020-02-20T15:15:07.254100Z

You can either pass :return-keys true in the execute! call or use sql/insert! which automatically passes that option in.

seancorfield 2020-02-20T15:16:05.254600Z

@iagwanderson Read "Getting Started" https://cljdoc.org/d/seancorfield/next.jdbc/1.0.13/doc/getting-started#execute--execute-one -- it has an example of this exact thing! 🙂

seancorfield 2020-02-20T15:16:56.255600Z

(although the example just shows the generated keys coming back in H2 -- PostgreSQL returns the entire row... unlike any other database!)

seancorfield 2020-02-20T15:22:39.256400Z

I'm adding a note to the PostgreSQL section of Tips &amp; Tricks that PG returns the entire row with either insert! or :return-keys true

🦜 3
dharrigan 2020-02-20T15:34:59.256700Z

you're da man!

bartuka 2020-02-20T15:36:54.257Z

thanks!