Back
select-one! (clj)
(source)function
(select-one! connectable cols sql-params)
(select-one! connectable cols sql-params opts)
Execute the SQL and params using `next.jdbc/plan` and return just the
selected columns from just the first row.
`(plan/select-one! ds [:total] ["select count(*) as total from table"])`
;;=> {:total 42}
If the `cols` argument is a vector of columns to select, then it is
applied using `select-keys`, otherwise, the `cols` argument is used as
a function directly. That means it can be a simple keyword to return
just that column -- which is the most common expected usage:
`(plan/select-one! ds :total ["select count(*) as total from table"])`
;;=> 42
The usual caveats apply about operations on a raw result set that
can be done without realizing the whole row.
Examples
next-jdbc
(ns next.jdbc.plan-test
"Tests for the plan helpers."
(:require [clojure.test :refer [deftest is use-fixtures]]
[next.jdbc.plan :as plan]
[next.jdbc.specs :as specs]
[next.jdbc.test-fixtures
:refer [with-test-db ds]]
[clojure.string :as str]))
(deftest select-one!-tests
(is (= {:id 1}
(plan/select-one! (ds) [:id] ["select * from fruit order by id"])))
(is (= 1
(plan/select-one! (ds) :id ["select * from fruit order by id"])))
(is (= "Banana"
(plan/select-one! (ds) :name ["select * from fruit where id = ?" 2])))
(is (= [1 "Apple"]
(plan/select-one! (ds) (juxt :id :name)
["select * from fruit order by id"])))
(is (= {:id 1 :name "Apple"}
(plan/select-one! (ds) #(select-keys % [:id :name])
["select * from fruit order by id"]))))
seancorfield/next-jdbc
(ns next.jdbc.plan-test
"Tests for the plan helpers."
(:require [clojure.test :refer [deftest is use-fixtures]]
[next.jdbc.plan :as plan]
[next.jdbc.specs :as specs]
[next.jdbc.test-fixtures
:refer [with-test-db ds]]
[clojure.string :as str]))
(deftest select-one!-tests
(is (= {:id 1}
(plan/select-one! (ds) [:id] ["select * from fruit order by id"])))
(is (= 1
(plan/select-one! (ds) :id ["select * from fruit order by id"])))
(is (= "Banana"
(plan/select-one! (ds) :name ["select * from fruit where id = ?" 2])))
(is (= [1 "Apple"]
(plan/select-one! (ds) (juxt :id :name)
["select * from fruit order by id"])))
(is (= {:id 1 :name "Apple"}
(plan/select-one! (ds) #(select-keys % [:id :name])
["select * from fruit order by id"]))))