Fork me on GitHub
#sql
<
2019-07-19
>
gko09:07:49

Could Emacs + CIDER + next.jdbc + org-mode + some code make a great universal SQL client, something like TRAMP but for databases ?

sheepy 4
Ben Hammond15:07:57

what is the nice way to pass a java.time.Period/`java.time.Duration` into a hugsql query and use it as a Postgres interval? left to my own devices I would write a Clojure function that converts java.time.Period into org.postgresql.util.PGInterval but open to better suggestions...

Ben Hammond15:07:38

something crude-yet-efffective like

(defn period->pginterval
  [^java.time.Period p]
  (doto (org.postgresql.util.PGInterval.)
    (.setYears (.getYears p))
    (.setMonths (.getMonths p))
    (.setDays (.getDays p))))

Ben Hammond15:07:44

er so I ended up with

(ns postgres-intervalable)

(defn period->pginterval
  [^java.time.Period p]
  (doto (org.postgresql.util.PGInterval.)
    (.setYears (.getYears p))
    (.setMonths (.getMonths p))
    (.setDays (.getDays p))))

(defn duration->pginterval
  [^java.time.Duration d]
  (doto (org.postgresql.util.PGInterval.)
    (.setSeconds (.getSeconds d))))

(defprotocol PGIntervalable
  (to-pginterval [d]
    "converts data to equivalent org.postgresql.util.PGInterval instance"))

(extend-protocol PGIntervalable
  java.time.Period
  (to-pginterval [p] (period->pginterval p))

  java.time.Duration
  (to-pginterval [d] (duration->pginterval d)))

Ben Hammond15:07:20

feels like reinvention though...

Ben Hammond15:07:10

(map postgres-intervalable/to-pginterval
  [(java-time/period "P1Y")
   (java-time/period "P1M")
   (java-time/period "P1D")
   (java-time/duration "PT24H")
   (java-time/duration "PT24M")
   (java-time/duration "PT24S")
   ])
=>
(#object[org.postgresql.util.PGInterval
         0x33754f2a
         "1 years 0 mons 0 days 0 hours 0 mins 0.00 secs"]
 #object[org.postgresql.util.PGInterval
         0x608f2210
         "0 years 1 mons 0 days 0 hours 0 mins 0.00 secs"]
 #object[org.postgresql.util.PGInterval
         0x6be3b450
         "0 years 0 mons 1 days 0 hours 0 mins 0.00 secs"]
 #object[org.postgresql.util.PGInterval
         0x90622fb
         "0 years 0 mons 0 days 0 hours 0 mins 86400.00 secs"]
 #object[org.postgresql.util.PGInterval
         0x101320ee
         "0 years 0 mons 0 days 0 hours 0 mins 1440.00 secs"]
 #object[org.postgresql.util.PGInterval
         0x9150f5e
         "0 years 0 mons 0 days 0 hours 0 mins 24.00 secs"])

seancorfield15:07:01

This feels like something you'd use SettableParameter for in next.jdbc...

Ben Hammond16:07:07

not an option for me at the moment sadly