Created
August 23, 2012 10:22
-
-
Save samaaron/3435248 to your computer and use it in GitHub Desktop.
How to suck an entire postgres db into Clojure datastructures in memory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns ormclj.pg | |
(:require [clojure.java.jdbc :as sql])) | |
(defn pg-db | |
[hostname port db-name user passwd] | |
{:classname "org.postgresql.Driver" | |
:subprotocol "postgresql" | |
:subname (str "//" hostname ":" port "/" db-name) | |
:user user | |
:password passwd}) | |
(def conn (pg-db "localhost" 5432 "dbname" "user-name" "password")) | |
(def tables (sql/with-connection conn | |
(sql/with-query-results rs ["SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';"] | |
(doall (for [row rs] row))))) | |
(def db | |
(reduce | |
(fn [res t] | |
(assoc res | |
(keyword (:table_name t)) | |
(sql/with-connection db | |
(sql/with-query-results rs [(str "select * from " (:table_name t))] | |
(doall (for [row rs] row)))))) | |
{} | |
tables)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment