On Apr 20, 8:51 pm, Remco van 't Veer <rwvtv...@gmail.com> wrote:
> I am doing the following after an insert for a Derby database:
>
>   (sql/with-query-results res
>     ["VALUES IDENTITY_VAL_LOCAL()"]
>     (first (vals (first res))))
>
> For MySQL it would be something like:
>
>   (sql/with-query-results res
>     ["SELECT LAST_INSERT_ID()"]
>     (first (vals (first res))))
>

Thanks Remco. I'm using H2 and its seems possible to do something
similar by accessing the INFORMATION_SCHEMA. However, this also feels
wrong to me so I'm trying the JDBC route which should be more
consistent across databases. My first working attempt is at:

http://gist.github.com/373564#file_sql.clj

It contains a alternative to insert-records called 'insert-record'
which returns a database id and a macro that wraps up multiple inserts
in a transaction. The macro doesn't feel very idiomatic so I'd welcome
any feedback on how to change it.

-------------------------
extended.sql/insert-record
([table record])
  Equivalent of clojure.contrib.sql/insert-records that only inserts a
single
   record but returns the autogenerated id of that record if
available.

-------------------------
extended.sql/insert-with-id
([db & table-records])
Macro
  Insert records within a single transaction into the database
described by
   the given db spec. The record format is :table  { record-hash }.
   The record hashes can optionally access a hashmap 'id' which holds
the
   autogenerated ids of previous inserts keyed by the table name. e.g.

      (insert-with-id db-spec
          :department {:name "xfiles"
                       :location "secret"}
          :employee   {:department (id :department)
                       :name "Mr X"})

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to