Fork me on GitHub
#sql
<
2017-08-17
>
michaelblume20:08:43

Probably a newbie question — as far as I can tell, inserting multiple maps with java.jdbc results in a separate PreparedStatement created per row; is there a way to avoid this?

seancorfield21:08:41

Use vector of column names and vector of vector of row values.

seancorfield21:08:02

Hmm, actually that may be for the old API. Let me check the docs.

seancorfield21:08:04

clojure.java.jdbc/insert-multi!
([db table rows] [db table cols-or-rows values-or-opts] [db table cols values opts])
  Given a database connection, a table name and either a sequence of maps (for
  rows) or a sequence of column names, followed by a sequence of vectors (for
  the values in each row), and possibly a map of options, insert that data into
  the database.

  When inserting rows as a sequence of maps, the result is a sequence of the
  generated keys, if available (note: PostgreSQL returns the whole rows). A
  separate database operation is used for each row inserted. This may be slow
  for if a large sequence of maps is provided.

  When inserting rows as a sequence of lists of column values, the result is
  a sequence of the counts of rows affected (a sequence of 1's), if available.
  Yes, that is singularly unhelpful. Thank you getUpdateCount and executeBatch!
  A single database operation is used to insert all the rows at once. This may
  be much faster than inserting a sequence of rows (which performs an insert for
  each map in the sequence).

  The :transaction? option specifies whether to run in a transaction or not.
  The default is true (use a transaction). The :entities option specifies how
  to convert the table name and column names to SQL entities.

seancorfield21:08:15

http://clojure-doc.org still needs the Using SQL section updating to the new API. http://clojure.github.io's doc generator is broken (due to specs) so it's lagging behind.

michaelblume21:08:18

cool, thanks =)

seancorfield21:08:35

OK, I updated the repo. Will need Michael Klishin to regen the docs.