sql

All things SQL and JDBC...
kirill.salykin 2020-09-09T09:10:07.268900Z

It looks like this:

(defn query
  "Runs a query."
  ([db sql-params]
   (jdbc/with-db-connection [conn db]
     (.setAutoCommit (:connection conn) false)
     (jdbc/query conn sql-params {:identifiers format-column-name}))))

seancorfield 2020-09-09T16:11:55.269400Z

Hmm, that shouldn't cause a transaction either. Are you calling this from inside other code that does set up a transaction?

kirill.salykin 2020-09-09T16:12:27.269600Z

nope, no explicit transaction started

seancorfield 2020-09-09T16:13:36.269800Z

Then I've really no idea, sorry.

kirill.salykin 2020-09-09T16:13:53.270Z

no problem, thanks!

isak 2020-09-09T16:45:47.271Z

Is it possible to do this via next.jdbc? (Processing more than one ResultSet)

String SQL = "SELECT 1; SELECT * FROM nonexistentTable;";
try (Statement statement = connection.createStatement();) {
    // Does not throw an exception on execute().
    boolean hasResult = statement.execute(SQL);
    while (hasResult) {
        try (ResultSet rs = statement.getResultSet()) {
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }
        }
        // Moves the next result set that generates the exception.
        hasResult = statement.getMoreResults();
    }
} catch (SQLException e) {
    e.printStackTrace();
}

isak 2020-09-09T16:48:48.271800Z

Oh never mind, I see it: {:multi-rs true}

seancorfield 2020-09-09T17:11:57.272800Z

@isak The test suite only tests that against MS SQL Server, although it does have a test on stored procs returning multiple result sets that runs on a few more types of DB https://github.com/seancorfield/next-jdbc/blob/develop/test/next/jdbc_test.clj#L446

isak 2020-09-09T17:22:38.273100Z

I'm on SQL Server, seems to work well :thumbsup::skin-tone-2:

Aviv Kotek 2020-09-09T18:30:21.273300Z

it seems that if you insert multiple rows via execute! - only the first id is returned. while insert-multi! will return a sequence of returned-keys (what I wanted). although looking https://github.com/clojure/java.jdbc/blob/master/src/test/clojure/clojure/java/jdbc_test.clj#L958 it should work well, i'm looking to do an insert-only-if-not-exists and grab the returned id's. so insert-multi! will force me to pre-look on every new inserted item

seancorfield 2020-09-09T19:47:16.273600Z

Per that test, you need to specify :multi? true to get back multiple generated keys -- and structure your parameter values slightly differently (because you need to specify groups of values, not just a sequence of values).

seancorfield 2020-09-09T20:23:39.275300Z

seancorfield/next.jdbc {:mvn/version "1.1.588"} -- https://github.com/seancorfield/next-jdbc -- adds next.jdbc.plan/select! and next.jdbc.plan/select-one! to make some common usages of plan easier to write (based on my usage on next.jdbc/plan at work). See next.jdbc.plan docs https://cljdoc.org/d/seancorfield/next.jdbc/1.1.588/api/next.jdbc.plan and updated examples in the second half of https://cljdoc.org/d/seancorfield/next.jdbc/1.1.588/doc/getting-started#plan--reducing-result-sets

dharrigan 2020-09-09T20:29:38.275900Z

that's pretty neat!

seancorfield 2020-09-09T20:35:46.276600Z

I found I was writing (reduce (fn [_ row] (reduced (f row))) nil (jdbc/plan ...)) all over the place...

seancorfield 2020-09-09T20:36:37.277700Z

...and then I looked at all my plan uses and found that (into [] (map f) (jdbc/plan ...)) was also pretty common. Except in a few places where it was (into #{} (map f) (jdbc/plan ...))

seancorfield 2020-09-09T20:38:13.279300Z

...and then I realized that we also commonly turn some result sets into lookup hash maps based on the primary key and one other column... so that is now (plan/select! ds (juxt :id :name) ["select..."] {:into {}})

dharrigan 2020-09-09T20:38:49.279600Z

It's a great addition to the library

seancorfield 2020-09-09T20:39:13.280Z

It's the first thing that's really grown out of daily usage...

2020-09-09T21:45:14.280100Z

you want create domain, not create type.. https://www.postgresql.org/docs/12/domains.html