summaryrefslogtreecommitdiff
path: root/src/clojure_sql
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-06-24 17:54:19 +1000
committerCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-06-24 17:54:19 +1000
commitd86e2c996ab86ea65c36105fb6c77831a772786d (patch)
treefa57e0e91e64c3725f210e7141651f9e7657a5b5 /src/clojure_sql
parent68c206aab2ddac42bef6de97fd065f75edcb1a98 (diff)
Make dotted fields result in nested maps when querying
So now something like this: (-> (table :users) (project {:id :user.id, :email :user.email, :person :person.id}) (join (-> (table :people) (project {:id :person.id, :first-name :person.first-name})))) will result in a map like this for each result: {:user {:id 1, :email "..."}, :person {:id 1, :first-name "..."}} Although I've just thought of an issue that will need to be detected where an error should be thrown. I'll deal with that now and commit it soon.
Diffstat (limited to 'src/clojure_sql')
-rw-r--r--src/clojure_sql/jdbc.clj16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/clojure_sql/jdbc.clj b/src/clojure_sql/jdbc.clj
index 45b1839..5b9de12 100644
--- a/src/clojure_sql/jdbc.clj
+++ b/src/clojure_sql/jdbc.clj
@@ -1,13 +1,25 @@
(ns clojure-sql.jdbc
(:require [clojure.java.jdbc :as jdbc]
- [clojure-sql.core :refer [set-query-executor!]]))
+ [clojure-sql.core :refer [set-query-executor!]]
+ [clojure.string :as string]))
+
+(defn ^:private dotted-to-nested-map-one [obj]
+ (reduce (fn [acc [key val]]
+ (let [dotted (name key)
+ keys (string/split dotted #"\.")
+ keywords (map keyword keys)]
+ (assoc-in acc keywords val)))
+ {} obj))
+
+(defn ^:private dotted-to-nested-maps [objs]
+ (mapv dotted-to-nested-map-one objs))
(defn use-jdbc! [connection-string]
(set-query-executor! (fn [type query]
(jdbc/with-connection connection-string
(case type
:select (jdbc/with-query-results results query
- (vec results))
+ (dotted-to-nested-maps results))
:insert (jdbc/do-prepared-return-keys (first query) (next query))
:update (jdbc/do-prepared-return-keys (first query) (next query))
:delete (first (jdbc/do-prepared (first query) (next query)))