seancorfield / next-jdbc

A modern low-level Clojure wrapper for JDBC-based access to databases.
https://cljdoc.org/d/com.github.seancorfield/next.jdbc/
Eclipse Public License 1.0
768 stars 90 forks source link

Support for qualified columns #150

Closed darkleaf closed 3 years ago

darkleaf commented 3 years ago

I want to use qualified column names like "user/login" or "agg/id" for my tables. I can use it in some cases by passing strings: (jdbc.sql/get-by-id tx "user" id "agg/id" {}) but I can't in other ones: (jdbc.sql/find-by-keys tx "user" :all {:order-by [["user/login" :asc]]}) throws an exeption. The exception is :order-by expected keyword or keyword pair, found: ["user/login" :asc]

There is usage like (entity-fn (name X)) and I can't define column-fn like (comp quoted/postgres str symbol)

Unfortunately, I guess any solution will break the current library's API. Maybe I'm wrong?

seancorfield commented 3 years ago

The current machinery allows for qualified keys to be sent in from Clojure and map to simple column names in SQL, discarding the namespace portion. That's a very deliberate design decision because, by default, when you run a query you get back a hash map with keys that are the column names qualified by the table name: that has to be reversible, otherwise the mapping just doesn't work.

Having a / in your column names in SQL is pretty unusual and definitely outside the scope of things I had anticipated anyone doing. I'm pretty sure that won't work with clojure.java.jdbc either.

That said, it just means you can't use the "friendly SQL functions". So you may be able to use HoneySQL to generate queries and statements you can run via jdbc/execute!. Even with HoneySQL, that won't work as the default behavior, you'll need to provide options to preserve the / in column names.