sql

dharrigan 2025-06-26T13:45:05.323309Z

With MySQL and using next.jdbc (and honeysql as the query creator), if I do select * from foo where foo is a view over foo_history, I get back a collection of foo-history namespaced keys, i.e., [{:foo-history/name "blah" :foo-history/gender "male"}]. Is it possible instead to have next.jdbc return the view name as the namespaced key, i.e., [{:foo/name "blah" :foo/gender "male"}]?

seancorfield 2025-06-26T13:46:38.892469Z

It's all down to what JDBC itself returns from .getTableName() on the ResultSetMetaData object.

seancorfield 2025-06-26T13:47:26.465369Z

You can override it with :table-fn in a modified builder if you really need to, but understand that next.jdbc by default does whatever JDBC does (in terms of what or whether there is a table name associated with a column).

dharrigan 2025-06-26T13:48:32.335429Z

kk, that's helpful. We have a lot of views and I really just want to work with the view names, not the underlying table names. I'll do some playing around with :table-fn.

dharrigan 2025-06-26T13:48:35.107919Z

Thanks!

seancorfield 2025-06-26T13:49:13.827989Z

Are they materialized views or "plain" views? That might affect what JDBC returns...?

dharrigan 2025-06-26T13:49:18.976739Z

plain

seancorfield 2025-06-26T13:49:48.530599Z

Ah, so JDBC essentially maps the query to the underlying tables etc that the view is defined as I guess.

dharrigan 2025-06-26T13:49:52.308329Z

i.e., create or replace view foo as select * from foo_history

seancorfield 2025-06-26T13:53:21.626109Z

Since next.jdbc is using ResultSetMetaData, .getTableName() "Gets the designated column's table name." so it's operating at the column level in the result set (and metadata) so the view is "gone" at that point. Bear in mind that doing something like :table-fn (constantly "foo") will cause all columns in the result to be :foo/* even if they came from a different table/view or were computed (and would normally have no table name associated).

dharrigan 2025-06-26T13:54:55.372369Z

hmm, yes, good point. I'll have to ponder. I may have to remap the namespaced keys in clojure

seancorfield 2025-06-26T13:56:07.549509Z

I think that would be better/safer since it feels like you're dealing with application domain concerns rather than JDBC concerns. I guess I'd rephrase that as "why do you want the view name instead of the underlying table name?"

dharrigan 2025-06-26T13:56:59.434399Z

The table names are mostly junk, i.e., person_history, organisation_history, patient_history, clinician_history and so on

dharrigan 2025-06-26T13:57:18.992949Z

the view is easier to think about, person, organisation, patient, clinician.

dharrigan 2025-06-26T13:57:36.201169Z

But, I'l have a ponder.

dharrigan 2025-06-26T13:58:14.443569Z

Perhaps I'll just to have come-to-terms with my aesthetic desires

dharrigan 2025-06-26T13:58:18.745749Z

and go with history 🙂

dharrigan 2025-06-26T13:59:34.613929Z

Yeah, I'll go with history, might as well. I'll adapt 🙂

seancorfield 2025-06-26T14:08:42.729279Z

I think it's better, overall, to have the raw queries come back reflecting the underlying DB structure and then to have an explicit transform layer to turn that into app domain data. That's what we do at work, e.g., mapping all sorts of tables down to :wsbilling/* before handing it off to the domain logic.

👍 1
emccue 2025-06-30T18:16:22.218109Z

I wonder if postgres will have different behavior here