getting the ResultSetMetaData or the ResultSet even

29 views
Skip to first unread message

Tom Willis

unread,
Sep 7, 2015, 3:36:23 PM9/7/15
to Korma
Hello, 

Is there anyway to hook into the ResultSetMetaData for a query in korma?


I was hoping it would be on the Map that represents the query maybe as :meta  but it isn't.

Or if there's a way to get the corresponding resultset object from the query, I could probably do it in interop. 

Tom Willis

unread,
Sep 8, 2015, 9:09:49 PM9/8/15
to Korma
Well, I managed to cobble something together after looking at the source for resultset-seq, just in case someone is looking for something similar. 

(ns navigator.core
  (:require [clojure.java.jdbc :as jdbc]
            [korma.core :as k]
            [korma.db :refer :all]
            [korma.sql.engine :as engine])
  (:import org.postgresql.util.PSQLException))

(def db-settings (postgres {:db "cordata"}))

(defdb db db-settings)

(defn get-meta-for-qry [q]
  "get's metadata from resultset for qry"
  (let [current-db (or (:db q) @_default)
        cn (get-connection current-db)
        cb (fn [r] (. r (getMetaData)))
        the-q (engine/->sql q)
        sql-str (:sql-str the-q)
        params (:params the-q)]
    (jdbc/db-query-with-resultset cn
                           (apply vector sql-str params)
                           cb)))

(defn get-columns-for-qry [q]
  (let [m (get-meta-for-qry (-> q (k/where (= 1 0))))
        idxs (range 1 (inc (. m (getColumnCount))))
        keys (map (comp keyword #(.toLowerCase ^String %))
                  (map (fn [i] (. m (getColumnLabel i))) idxs))]
    keys))

Immo Heikkinen

unread,
Sep 23, 2015, 2:03:23 PM9/23/15
to Korma
There are no plans add support for this in Korma. 

The recent release of clojure/java.jdbc (0.4.2) introduced helper for working with metadata, I don't know if it makes things any easier: http://dev.clojure.org/jira/browse/JDBC-107


--
You received this message because you are subscribed to the Google Groups "Korma" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlkorma+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages