Skip to content

Commit

Permalink
decode SQL results so they can be presented correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
mpisanko committed Jan 23, 2025
1 parent 6d04b5e commit a51ddc1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
22 changes: 14 additions & 8 deletions src/clj/xt_play/transactions.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns xt-play.transactions
(:require [clojure.string :as str]
[clojure.data.json :as json]
[jsonista.core :as j]
[clojure.instant :refer [read-instant-date]]
[clojure.tools.logging :as log]
[xt-play.util :as util]
Expand Down Expand Up @@ -33,18 +33,24 @@
(defn format-system-time [s]
(when s (read-instant-date s)))

(defn- PGobject->clj [v]
(if (= org.postgresql.util.PGobject (type v))
(json/read-str (.getValue v) :key-fn keyword)
v))
(defn- PG->clj [v]
(cond
(instance? org.postgresql.util.PGobject v) (-> (.getValue v)
(j/write-value-as-bytes j/default-object-mapper)
(j/read-value j/default-object-mapper))
(instance? org.postgresql.jdbc.PgArray v) (->> (.getArray v)
(into [])
(str/join ",")
(format "[%s]"))
:else v))

(defn- parse-result [result]
;; TODO - this shouldn't be needed, a fix is on the way in
;; a later version of xtdb-jdb
;; This will only pick up top level objects
;; This should do it for now - get decent string representation so it looks more like psql output
(mapv
(fn [row]
(mapv PGobject->clj row))
(mapv PG->clj row))
result))

(defn- run!-tx [node tx-type tx-batches query]
Expand All @@ -68,7 +74,7 @@
(xtdb/jdbc-execute! conn statement))
(log/info "beta running query:" query)
(let [res (xtdb/jdbc-execute! conn [query])]
(log/info "beta query resoponse" res)
(log/info "beta query response" res)
(parse-result res)))))

(defn run!!
Expand Down
2 changes: 1 addition & 1 deletion src/cljs/xt_play/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
(pr-str value)]
;; default
[hl/code {:language "json"}
(js/JSON.stringify (clj->js value))])]))]))]]))
(str value)])]))]))]]))

(defn- title [& body]
(into [:h2 {:class "text-lg font-semibold"}]
Expand Down

0 comments on commit a51ddc1

Please sign in to comment.