metabase / toucan

A classy high-level Clojure library for defining application models and retrieving them from a DB
Eclipse Public License 1.0
570 stars 49 forks source link

Does Toucan support enumerated types? #80

Open githorse opened 3 years ago

githorse commented 3 years ago

@camsaul My postgres db has an enumerated type. I see this possible hack for making that work in java-jdbc, but that looks messy and possibly destroys the type safety of the enum in the first place (not clear). I can probably live with just strings here, but any advice on making enums work with Toucan?

camsaul commented 3 years ago

Toucan doesn't support Postgres enums directly but it uses HoneySQL under the hood so you can do something like this

(defn- pg-enum [enum-type value]
  (reify
    honeysql.format/ToSql
    (to-sql [_]
      (honeysql.format/add-anon-param value)
      (format "?::\"%s\"" enum-type))))

Then you can use them in Toucan:

(db/select Birds :bird_type (pg-enum "bird_type" "toucan"))
marksto commented 2 years ago

I would suggest using a different approach that is idiomatic to the clojure.java.jdbc and does not require one to prepare query params each time for HoneySQL to understand them: https://github.com/metabase/toucan/pull/89/files#diff-ef52c6468aae561cdf12e47e3ef10706d625572b4c6af3fc7967c845285593c6