sql

All things SQL and JDBC...
Aviv Kotek 2021-03-22T13:55:26.040300Z

that's exactly the PS thing we discussed right? ty sean!

seancorfield 2021-03-22T15:26:43.040500Z

A form of it, I think.

Aviv Kotek 2021-03-22T15:57:52.040800Z

Is there anyway to get the "updated-rows" keys/columns from an update query (with next.jdbc 1.1.582 & MySQL)

(next.jdbc/execute!
  ds
  ["UPDATE table SET col1=1 WHERE col2=test"]
  {:return-keys ["col1" "col2"]})
=> []
although an update occurred..

seancorfield 2021-03-22T16:23:21.041500Z

@aviv The JDBC driver doesn’t provide that information (otherwise the above would work).

Aviv Kotek 2021-03-22T16:25:02.043500Z

doesn't it support .getGeneratedKeys() or this won't do in MYSQL, I remember from Java days this is usually supported

seancorfield 2021-03-22T16:25:04.043600Z

There is no ResultSet from the updatenext.jdbc tries to return a ResultSet first in all cases and only if one isn’t available does it ask for the update count. If you ask for :return-keys, it will also try .getGeneratedKeys() (after asking for a ResultSet but before falling back to update counts).

Aviv Kotek 2021-03-22T16:25:13.043800Z

I see

Aviv Kotek 2021-03-22T16:27:43.045100Z

so no way to escape 2x db-operations

seancorfield 2021-03-22T16:27:45.045300Z

next.jdbc is pretty close to the JDBC layer: when you execute! or execute-one!, it does this:

(if (.execute stmt)
    (.getResultSet stmt)
    (when (:return-keys opts)
      (try
        (.getGeneratedKeys stmt)
        (catch Exception _))))

seancorfield 2021-03-22T16:28:28.045400Z

You mean an update followed by a select in a single transaction?

seancorfield 2021-03-22T16:29:09.045600Z

(or even sending multiple statements in a single operation — that’s documented for MySQL but not advised because it’s easier to succumb to a SQL injection attack that way)

Aviv Kotek 2021-03-22T16:31:44.046200Z

Update without a followed select (for simplicity), but I guess this is not possible

Aviv Kotek 2021-03-22T16:38:02.046400Z

update&select in a single transaction is possible by wrapping with the transaction macro

seancorfield 2021-03-22T16:42:42.046600Z

Per the link above you can do update ...; select ... if you add that JDBC connection setting. Like I say, not recommended in general.

Aviv Kotek 2021-03-22T16:44:41.046800Z

cool, thx