sql

All things SQL and JDBC...
practicalli-john 2021-05-28T12:14:07.043Z

For each row in a database table, I'd like to clean a specific string field in each row (using re-seq and some regex) and write each record back to the database, ideally in another table so I can compare the results. Would plan be a good approach or is this just a doseq approach again? If I understand correctly, I suspect plan may not be right as the connection would close after the query but before the insert. Happy to be corrected though ๐Ÿ™‚ Or would I simply put the insert in the same reducing function that cleans the string field, after its has cleaned it? Time for some repl experiments ๐Ÿ™‚ Thanks for any thoughts on this.

practicalli-john 2021-05-28T14:08:06.046900Z

This feels a little hacky, but seems to work okay on a limited set of records in the database.

(reduce
   (fn [_ row]
     (sql/insert! db-spec :transactions_clean (assoc row :domain/field_name "result-of-clean-name-function")))
   0  ;; accumulator not used
   (jdbc/plan db-spec ["select * from datarows where interesting_rows = ?" 1234554321]))
Now to try it on the whole database ๐Ÿ™‚ Doh! out of heap space when I try to do the whole database Oh, a doseq over the distinct customer names with the above query should do it (probably very slowly)

2021-05-28T14:27:00.048500Z

Thanks @seancorfield for the cond-> tip! That improve readability so much IMO. ๐Ÿ‘ https://github.com/prestancedesign/pingcrm-clojure/blob/main/src/pingcrm/models/users.clj#L16-L35

2021-05-28T14:28:32.049Z

This is exactly the kind of advice I was expecting!

1
seancorfield 2021-05-28T15:30:26.049300Z

Hmm, Iโ€™m a bit surprised at the OoM โ€” maybe the select isnโ€™t streaming? (how to do that is DB-dependent but there are examples for a few DBs in the docs)

seancorfield 2021-05-28T15:31:47.049500Z

If you have a reduce where the accumulator is not used, you can always use run! instead:

(run! #(sql/insert! db-spec :transaction_clean (assoc % :domain/field_name (clean-up-row %))) (jdbc/plan db-spec ...))

seancorfield 2021-05-28T15:31:57.049700Z

run! is a reduce under the hood.

practicalli-john 2021-05-28T16:06:33.050300Z

Ah good point about run!, thanks The db is your favourite, postgres. So I assume the select not streaming is a postgres thing. I have a workable solution for now. Thanks for the advice.

seancorfield 2021-05-28T16:19:27.050500Z

Streaming for PostgreSQL is specifically documented for next.jdbc (see Tips & Tricks section โ€” I think? Itโ€™s definitely in there somewhere).

๐Ÿ‘ 1
dpsutton 2021-05-28T19:13:19.051500Z

how can i achieve this order-by clause with honeysql? ORDER BY date_login IS NOT NULL, date_login DESC

dpsutton 2021-05-28T19:14:08.051800Z

i'm needing to control where my dates go in an order in mysql

seancorfield 2021-05-28T19:15:22.052200Z

Ah, I was wondering when someone was going to request that ๐Ÿ™‚

seancorfield 2021-05-28T19:17:20.053200Z

I think you can do {:order-by [[:date_login :is-not-null] [:date_login :desc]]}

dpsutton 2021-05-28T19:17:28.053400Z

ha. mysql doesn't like :nulls last and i can't seem to coax it otherwise

seancorfield 2021-05-28T19:17:35.053700Z

(if youโ€™re on v2)

seancorfield 2021-05-28T19:18:01.054Z

dev=> (sql/format {:order-by [[:date_login :is-not-null] [:date_login :desc]]})
["ORDER BY date_login IS NOT NULL, date_login DESC"]

dpsutton 2021-05-28T19:18:01.054100Z

"1.0.461" ๐Ÿ˜•

seancorfield 2021-05-28T19:18:48.054900Z

You can always bring in v2 just for that one query โ€” you can use v1 and v2 side-by-side since they have different namespaces (and they are at different maven coords).

dpsutton 2021-05-28T19:20:11.055300Z

ah, a nice side effect of the security changes on clojars?

seancorfield 2021-05-28T19:22:16.055800Z

No, a deliberate choice to help encourage people to migrate to v2 piecemeal ๐Ÿ™‚

๐Ÿ‘ 1
dpsutton 2021-05-28T19:26:07.056800Z

we have toucan involved so can't swap out for v2 here unfortunately (or easily i guess). toucan author is coworker and principal for years

seancorfield 2021-05-28T19:44:27.057900Z

Well, thatโ€™s one of the big disadvantages of layering abstractions upon abstractions I guessโ€ฆ At least if theyโ€™re a coworker, you can petition them to migrate Toucan to HoneySQL v2 ๐Ÿ™‚

๐Ÿ‘ 1
seancorfield 2021-05-28T19:45:38.058900Z

Iโ€™m already working with the author of Gungnir to see if I can provide extension points in v2 that will address his needs (Gungnir re-implements a bunch of internal HoneySQL stuff, unfortunately).

dpsutton 2021-05-28T19:51:07.059100Z

haven't heard of that one