honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
seancorfield 2021-04-12T19:13:04.151100Z

There’s some additional discussion on https://github.com/seancorfield/honeysql/issues/315 that I’d like more input on. I’m inclined to roll that change back out at this point…?

sandqvist 2021-04-12T19:46:53.157600Z

Assuming a function such as

(defn find-foos [ids]
  (-> (select :*)
      (from :foo)
      (where [:in :id ids])))
I would expect to pass either a subselect or a seq of primary keys as ids. Values such as nil, [nil] and [1 2 nil] would all be programming errors in my code and I'm happy to catch them early. Especially the last one (when actually desired) I would always write explicitly as two clauses so that the next person to see the code can see how NULL is handled.

seancorfield 2021-04-12T19:48:37.158200Z

Thanks, @sandqvist ! Good feedback. “avoid magic, make programmer errors visible”

seancorfield 2021-04-12T19:51:33.160Z

So I guess then a question would be: should HoneySQL throw an exception if the IN collection is empty and/or contains nil? The former seems reasonable since WHERE col IN () is going to blow up in JDBC-land anyway, but the latter is probably a grey area since it “works” in JDBC-land, it just doesn’t necessarily do what the developer might want/expect…?

sandqvist 2021-04-12T20:16:15.166800Z

This is a tricky one. For the [] and nil cases an exception would be useful if it makes complex statements easier to debug and fix than looking at the database logs and reading the SQL statement which is what I currently do as I'm still on v1. An exception without a good HoneySQL data structure context might be tricky to figure out. What I mean is unless the IN clause has a helper fn, you can only throw in format, right?

seancorfield 2021-04-12T20:19:53.168600Z

Correct. But the error message could include the SQL fragment that would fail, e.g., col IN () so you would at least see the col (or expression).

sandqvist 2021-04-12T20:25:11.171600Z

I compose HoneySQL queries quite a bit. What I'm afraid of is an error like [:in :id nil] is not allowed when I've composed three or four subqueries for tables that all have a surrogate primary key column called id. In that case, just looking at the database log is simpler.

sandqvist 2021-04-12T20:27:15.172600Z

But just a debugging option to ignore the check and let the DB complain would also be fine.

seancorfield 2021-04-12T20:28:18.173600Z

Or perhaps leave the default behavior “as-is” and add a :checking true opt-in feature that performs this check (and potential other checks in the future)?

sandqvist 2021-04-12T20:34:35.177500Z

I would prefer that, with the possibility of later adding helper fns for IN and other clauses. The stacktrace from those would be very helpful. Of course, anyone can make those.