I'm new to Clojure. I'm using next.jdbc and sqlite. On insert (`next.jdbc.sql/insert!`) I get back what looks like a map: {:last_insert_rowid() 22} What is the best way to deal with the parens? I want the last insert ID (22).
Things I've tried:
(get foo (first (keys foo))) ; 22
(get foo ":last_insert_rowid()") ; nil
(get foo "last_insert_rowid()") ; nil
(get foo :last_insert_rowid()) ; ()
(get foo (keyword "last_insert_rowid()")) ; 22I would just use (comp first vals)
Another possibility is to use the option hash map {:suffix "returning *"} in the insert! call, and you should get back the newly-inserted row so you can get at the generated key that way (and any other generated/modified columns).
Each DB returns its own unique thing from an insert, unfortunately, so the suggestion to use (comp first vals) as a fn to get the ID is portable across several DBs. PostgreSQL behaves as if you said RETURNING * and you get the whole row back.
Thanks, {:suffix "returning *"} works well. I just found "for an insert! call :suffix "RETURNING *". The latter is particularly useful for databases, such as SQLite these days" in the docs 🙂 I'm not sure how to use (comp first vals) It looks like it returns a function?
Yes, (comp first vals) produces a function that operates on a hash map, that returns the first of the values in the map (and since the insert returns a map with just one key, you get the one value back). You could (def ->id (comp first vals)) and then (->id (sql/insert! db ...)) would give you just the ID value.
https://github.com/seancorfield/next-jdbc 1.3.1002 -- A modern low-level Clojure wrapper for JDBC-based access to databases.
This is a minor bug fix / quality of life release:
• Address https://github.com/seancorfield/next-jdbc/issues/296 by adding an explicit check (and throw) for sql-params in next.jdbc functions.
• Address https://github.com/seancorfield/next-jdbc/issues/295 by providing a way to tell next.jdbc that certain options should be passed "as-is" in the Properties object when creating a Connection -- :next.jdbc/as-is-properties accepts a sequence (or set) of keywords, identifying properties that should not be converted to strings.
• Fix https://github.com/seancorfield/next-jdbc/issues/181 (again!) by adding Wrapped protocol as a way for DefaultOptions and SQLLogging to consistently expose the underlying connectable, even when nested.