Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f2d0aec

Browse files
committed
Adds get-by-id / find-by-keys for 0.6.0 RC 1
1 parent f802deb commit f2d0aec

File tree

4 files changed

+63
-17
lines changed

4 files changed

+63
-17
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Changes coming in 0.6.0-rc1
2+
3+
* Adds `get-by-id` and `find-by-keys` convenience functions (these were easy to add after the API changes in 0.6.0 and we rely very heavily on them at World Singles so putting them in the core for everyone seemed reasonable).
4+
* REMINDER: ALL DEPRECATED FUNCTIONALITY HAS BEEN REMOVED! [JDBC-118](http://dev.clojure.org/jira/browse/JDBC-118).
5+
- See alpha2 / alpha1 below for more details.
6+
17
Changes in 0.6.0-alpha2
28

39
* ALL DEPRECATED FUNCTIONALITY HAS BEEN REMOVED! [JDBC-118](http://dev.clojure.org/jira/browse/JDBC-118).

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
;; develop and test java.jdbc locally. The pom.xml file is the
33
;; "system of record" as far as the project version is concerned.
44

5-
(defproject org.clojure/java.jdbc "0.6.0-alpha2"
5+
(defproject org.clojure/java.jdbc "0.6.0-rc1"
66
:description "A low-level Clojure wrapper for JDBC-based access to databases."
77
:parent [org.clojure/pom.contrib "0.1.2"]
88
:url "https://github.com/clojure/java.jdbc"

src/main/clojure/clojure/java/jdbc.clj

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ http://clojure-doc.org/articles/ecosystem/java_jdbc/home.html" }
8181
(str (first q) x (last q))
8282
(str q x q))))
8383

84+
(defn- table-str
85+
"Transform a table spec to an entity name for SQL. The table spec may be a
86+
string, a keyword or a map with a single pair - table name and alias."
87+
[table entities]
88+
(let [entities (or entities identity)]
89+
(if (map? table)
90+
(let [[k v] (first table)]
91+
(str (as-sql-name entities k) " " (as-sql-name entities v)))
92+
(as-sql-name entities table))))
93+
94+
(defn- kv-sql
95+
"Given a sequence of column name keys and a matching sequence of column
96+
values, and an entities mapping function, return a sequence of SQL fragments
97+
that can be joined for part of an UPDATE SET or a SELECT WHERE clause."
98+
[ks vs entities]
99+
(map (fn [k v]
100+
(str (as-sql-name entities k)
101+
" = "
102+
(if (nil? v) "NULL" "?")))
103+
ks vs))
104+
84105
(defn- ^Properties as-properties
85106
"Convert any seq of pairs to a java.utils.Properties instance.
86107
Uses as-sql-name to convert both keys and values into strings."
@@ -867,6 +888,35 @@ http://clojure-doc.org/articles/ecosystem/java_jdbc/home.html" }
867888
(map row-fn rs))))
868889
(result-set-seq rset {:identifiers identifiers :as-arrays? as-arrays?})))))))
869890

891+
(defn find-by-keys
892+
"Given a database connection, a table name, a map of column name/value
893+
pairs, and an optional options map, return any matching rows."
894+
([db table columns] (find-by-keys db table columns {}))
895+
([db table columns opts]
896+
(let [entities (:entities opts identity)
897+
ks (keys columns)
898+
vs (vals columns)]
899+
(query db (into [(str "SELECT * FROM " (table-str table entities)
900+
" WHERE " (str/join " AND "
901+
(kv-sql ks vs entities)))]
902+
(remove nil? vs))
903+
opts))))
904+
905+
(defn get-by-id
906+
"Given a database connection, a table name, a primary key value, an
907+
optional primary key column name, and an optional options map, return
908+
a single matching row, or nil.
909+
The primary key column name defaults to :id."
910+
([db table pk-value] (get-by-id db table pk-value :id {}))
911+
([db table pk-value pk-name-or-opts]
912+
(if (map? pk-name-or-opts)
913+
(get-by-id db table pk-value :id pk-name-or-opts)
914+
(get-by-id db table pk-value pk-name-or-opts {})))
915+
([db table pk-value pk-name opts]
916+
(let [r-s-fn (or (:result-set-fn opts) identity)]
917+
(find-by-keys db table {pk-name pk-value}
918+
(assoc opts :result-set-fn (comp first r-s-fn))))))
919+
870920
(defn execute!
871921
"Given a database connection and a vector containing SQL (or PreparedStatement)
872922
followed by optional parameters, perform a general (non-select) SQL operation.
@@ -891,16 +941,6 @@ http://clojure-doc.org/articles/ecosystem/java_jdbc/home.html" }
891941
(with-open [con (get-connection db)]
892942
(execute-helper (add-connection db con)))))))
893943

894-
(defn- table-str
895-
"Transform a table spec to an entity name for SQL. The table spec may be a
896-
string, a keyword or a map with a single pair - table name and alias."
897-
[table entities]
898-
(let [entities (or entities identity)]
899-
(if (map? table)
900-
(let [[k v] (first table)]
901-
(str (as-sql-name entities k) " " (as-sql-name entities v)))
902-
(as-sql-name entities table))))
903-
904944
(defn- delete-sql
905945
"Given a table name, a where class and its parameters and an optional entities spec,
906946
return a vector of the SQL for that delete operation followed by its parameters. The
@@ -1051,11 +1091,7 @@ http://clojure-doc.org/articles/ecosystem/java_jdbc/home.html" }
10511091
(cons (str "UPDATE " (table-str table entities)
10521092
" SET " (str/join
10531093
","
1054-
(map (fn [k v]
1055-
(str (as-sql-name entities k)
1056-
" = "
1057-
(if (nil? v) "NULL" "?")))
1058-
ks vs))
1094+
(kv-sql ks vs entities))
10591095
(when where " WHERE ")
10601096
where)
10611097
(concat (remove nil? vs) params))))

src/test/clojure/clojure/java/test_jdbc.clj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,11 @@
379379
(is (= 2 (sql/with-db-connection [con db]
380380
(sql/query con [(sql/prepare-statement (sql/db-connection con) "SELECT * FROM fruit" {:max-rows 2})] {:result-set-fn count}))))
381381
(is (= "Apple" (sql/query db ["SELECT * FROM fruit WHERE appearance = ?" "red"] {:row-fn :name :result-set-fn first})))
382-
(is (= "juicy" (sql/query db ["SELECT * FROM fruit WHERE name = ?" "Orange"] {:row-fn :appearance :result-set-fn first})))))
382+
(is (= "juicy" (sql/query db ["SELECT * FROM fruit WHERE name = ?" "Orange"] {:row-fn :appearance :result-set-fn first})))
383+
(is (= "Apple" (:name (sql/get-by-id db :fruit 1))))
384+
(is (= ["Apple"] (map :name (sql/find-by-keys db :fruit {:appearance "red"}))))
385+
(is (= "Peach" (:name (sql/get-by-id db :fruit 3 :id))))
386+
(is (= ["Peach"] (map :name (sql/find-by-keys db :fruit {:id 3 :cost 139}))))))
383387

384388
(deftest test-insert-values
385389
(doseq [db (test-specs)]

0 commit comments

Comments
 (0)