sql

All things SQL and JDBC...
dharrigan 2020-11-16T15:55:19.325700Z

@seancorfield In next.jdbc, you now have the as-*-snake-kebab if the csk library is on the path. Would you consider also adding as-*-camel-case? The rationale is that it's very common when interfacing with external consumers to send to them JSON (i.e., javascript frontends). Thus, having the ability to simply do, for example {:builder-fn rs/as-unqualified-camel-case-maps}) would help tremendously, rather than having to do something like this:

dharrigan 2020-11-16T15:55:20.325900Z

(defn camel-case
  [rs opts]
  (let [camelCase #(->camelCase %)]
    (result-set/as-unqualified-modified-maps rs (assoc opts :qualifier-fn camelCase :label-fn camelCase))))

dharrigan 2020-11-16T15:55:29.326100Z

I can raise an issue if it helps 🙂

dharrigan 2020-11-16T15:56:18.327Z

(Or, I was looking at the code on the v1 branch, and I could copy pasta the defmacro for def-snake-kebab [] in the result_set.clj)

dharrigan 2020-11-16T15:56:25.327200Z

as a PR

dharrigan 2020-11-16T15:56:28.327400Z

.

seancorfield 2020-11-16T18:25:07.331300Z

I won't accept a PR for that: snake_case is much more common than camelCase (in both the DB tables/columns and in external JSON systems) and it's easy enough to write a builder to produce camelCase based on the existing adapters -- you could stick that function above in a common ns in your code and easily reuse it everywhere. Another argument against this is that it is fairly unlikely that an external system is going to just magically match your DB naming so an automatic conversion won't help you (and tying your DB structure directly to that external system's current naming convention is a Bad Idea(™) anyway).

dharrigan 2020-11-16T18:58:31.331800Z

Oooh, that's not the case Sean

dharrigan 2020-11-16T19:04:36.335500Z

I don't know which systems you work with, but in my experience, camelCase for JSON is a lot more common. I rarely see JSON of this form "foo_bar", more like "fooBar"

dharrigan 2020-11-16T19:06:59.337300Z

but no matter. I do use already the function as I have shown above.

seancorfield 2020-11-16T19:45:47.339200Z

Fair enough. Either way, I only baked in snake/kebab because I kept seeing people reinvent it in buggy ways -- I still sort of regret it, since it's easy enough for folks to "do it right" with the CSK library. Yours is the first request I've seen for camelCase (ever, as far as I can remember, across all the years of c.j.j and next.jdbc).

dharrigan 2020-11-16T19:56:23.339500Z

There's always one, isn't there 🙂

seancorfield 2020-11-16T20:02:09.339700Z

And it's always you, isn't it? 🙂

dharrigan 2020-11-16T20:02:27.340500Z

someone has to 🙂

Asko Nõmm 2020-11-16T20:02:38.340800Z

Hi! I’ve been told to write about my concern here, so I’m reposting this: Does anyone have any experience with jdbc / mysql here? Namely I have an issue where after some time of inactivity, and then again trying to connect to the database .. it fails with a `Operation timed out (Read failed)` . I’ve been trying to find a way to configure it to reconnect on timeout, but haven’t yet found a way. Does anyone have any ideas?

dharrigan 2020-11-16T20:03:07.341600Z

Do you use a connection pooler?

Asko Nõmm 2020-11-16T20:03:12.341900Z

I do indeed

dharrigan 2020-11-16T20:03:23.342200Z

For example, HikariCP will automatically reestablish timeouts

seancorfield 2020-11-16T20:06:54.343400Z

How long is "some time of inactivity"? I seem to recall MySQL's JDBC driver auto-closes inactive connections after some time period (but I think it's hours).

Asko Nõmm 2020-11-16T20:07:58.343700Z

It could easily be an hour or so

Asko Nõmm 2020-11-16T20:08:18.344200Z

For what it’s worth, I’m using Korma as an ORM. I’m looking into HikariCP tho’!

dharrigan 2020-11-16T20:09:14.345400Z

I use HikariCP with PostgreSQL (I have used mysql and mariadb too), and it will keep the connection alive to the db, even when the db goes down, when it comes back up again, it will reestablish the connection. which is neat.

seancorfield 2020-11-16T20:10:41.346900Z

Korma is unmaintained and uses a very old clojure.java.jdbc version. I would suggest you reconsider that path. We really don't use ORM-style libraries in Clojure (but, if you must, look at Toucan which is at least still maintained).

dharrigan 2020-11-16T20:11:34.347700Z

yeah, agree there. I am sooo thankful of not having to mess around with hibernate or java jpa shenanigans since moving to clojure

Asko Nõmm 2020-11-16T20:12:31.349Z

Excuse my naiveness, I’m new to working in the back-end with Clojure, coming from CLJS. Thankfully my project is new enough to re-work that part rather easily, so I think I’ll just go with Hikari and report back! 🙂

dharrigan 2020-11-16T20:12:43.349300Z

saying that, I do also work in Kotlin and I can recommend JOOQ as a very nice abstraction layer (akin to honeysql/nextjdbc on Clojure)

dharrigan 2020-11-16T20:14:42.350400Z

If you want to see a stupid little example of using hikaricp and nextjdbc/honeysql (targeting a postgresql database, but it’s so simple, could be mysql/mariadb too), then here you go https://github.com/dharrigan/startrek/blob/master/src/startrek/db.clj

dharrigan 2020-11-16T20:15:15.350800Z

<https://github.com/dharrigan/startrek>

❤️ 1
Asko Nõmm 2020-11-16T20:17:15.351400Z

Holy moly, that’s awesome! Thank you very much!

dharrigan 2020-11-16T20:18:31.352100Z

it’s only a small example, others have examples too, e.g, <https://github.com/seancorfield/usermanager-example>

dharrigan 2020-11-16T20:19:14.353Z

enjoy! I hope you find not having to worry about ORMs liberating! 🙂 of course, usual cavets apply, YMMV, don’t trust anyone on t’interwebs etc….

seancorfield 2020-11-16T20:29:27.353800Z

next.jdbc has built-in support for connection pools with both HikariCP and c3p0 (we use the latter at work in production but most people use HikariCP).

seancorfield 2020-11-16T20:29:59.354500Z

We use HoneySQL very heavily in production at work, with a combination of clojure.java.jdbc (in our legacy Clojure code) and next.jdbc in our more recent code.

seancorfield 2020-11-16T20:30:46.355300Z

I maintain all three of those. This channel is good for general SQL Qs and also for c.j.j and next.jdbc. There's a #honeysql channel if you want to dig into that library.