Back
select! (clj)
(source)function
(select! connectable cols sql-params)
(select! connectable cols sql-params opts)
Execute the SQL and params using `next.jdbc/plan` and (by default)
return a vector of rows with just the selected columns.
`(plan/select! ds [:id :name] ["select * from table"])`
If the `cols` argument is a vector of columns to select, then it is
applied as:
`(into [] (map #(select-keys % cols)) (jdbc/plan ...))`
Otherwise, the `cols` argument is used as a function and mapped over
the raw result set as:
`(into [] (map cols) (jdbc/plan ...))`
The usual caveats apply about operations on a raw result set that
can be done without realizing the whole row.
Note: this allows for the following usage, which returns a vector
of all the values for a single column:
`(plan/select! ds :id ["select * from table"])`
The result is a vector by default, but can be changed using the
`:into` option to provide the initial data structure into which
the selected columns are poured, e.g., `:into #{}`
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-vector-tests
(is (= [{:id 1} {:id 2} {:id 3} {:id 4}]
(plan/select! (ds) [:id] ["select * from fruit order by id"])))
(is (= [1 2 3 4]
(plan/select! (ds) :id ["select * from fruit order by id"])))
(is (= ["Banana"]
(plan/select! (ds) :name ["select * from fruit where id = ?" 2])))
(is (= [[2 "Banana"]]
(plan/select! (ds) (juxt :id :name)
["select * from fruit where id = ?" 2])))
(is (= [{:id 2 :name "Banana"}]
(plan/select! (ds) [:id :name]
["select * from fruit where id = ?" 2]))))
(deftest select-set-tests
(is (= #{{:id 1} {:id 2} {:id 3} {:id 4}}
(plan/select! (ds) [:id] ["select * from fruit order by id"]
{:into #{}})))
(is (= #{1 2 3 4}
(plan/select! (ds) :id ["select * from fruit order by id"]
{:into #{}}))))
(deftest select-map-tests
(is (= {1 "Apple", 2 "Banana", 3 "Peach", 4 "Orange"}
(plan/select! (ds) (juxt :id :name) ["select * from fruit order by id"]
{:into {}}))))
(deftest select-issue-227
(is (= ["Apple"]
(plan/select! (ds) :name ["select * from fruit where id = ?" 1]
{:column-fn #(str/replace % "-" "_")})))
(is (= ["Apple"]
(plan/select! (ds) :foo/name ["select * from fruit where id = ?" 1]
{:column-fn #(str/replace % "-" "_")})))
(is (= ["Apple"]
(plan/select! (ds) #(get % "name") ["select * from fruit where id = ?" 1]
{:column-fn #(str/replace % "-" "_")})))
(is (= [["Apple"]]
(plan/select! (ds) (juxt :name) ["select * from fruit where id = ?" 1]
{:column-fn #(str/replace % "-" "_")}))))
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-vector-tests
(is (= [{:id 1} {:id 2} {:id 3} {:id 4}]
(plan/select! (ds) [:id] ["select * from fruit order by id"])))
(is (= [1 2 3 4]
(plan/select! (ds) :id ["select * from fruit order by id"])))
(is (= ["Banana"]
(plan/select! (ds) :name ["select * from fruit where id = ?" 2])))
(is (= [[2 "Banana"]]
(plan/select! (ds) (juxt :id :name)
["select * from fruit where id = ?" 2])))
(is (= [{:id 2 :name "Banana"}]
(plan/select! (ds) [:id :name]
["select * from fruit where id = ?" 2]))))
(deftest select-set-tests
(is (= #{{:id 1} {:id 2} {:id 3} {:id 4}}
(plan/select! (ds) [:id] ["select * from fruit order by id"]
{:into #{}})))
(is (= #{1 2 3 4}
(plan/select! (ds) :id ["select * from fruit order by id"]
{:into #{}}))))
(deftest select-map-tests
(is (= {1 "Apple", 2 "Banana", 3 "Peach", 4 "Orange"}
(plan/select! (ds) (juxt :id :name) ["select * from fruit order by id"]
{:into {}}))))
(deftest select-issue-227
(is (= ["Apple"]
(plan/select! (ds) :name ["select * from fruit where id = ?" 1]
{:column-fn #(str/replace % "-" "_")})))
(is (= ["Apple"]
(plan/select! (ds) :foo/name ["select * from fruit where id = ?" 1]
{:column-fn #(str/replace % "-" "_")})))
(is (= ["Apple"]
(plan/select! (ds) #(get % "name") ["select * from fruit where id = ?" 1]
{:column-fn #(str/replace % "-" "_")})))
(is (= [["Apple"]]
(plan/select! (ds) (juxt :name) ["select * from fruit where id = ?" 1]
{:column-fn #(str/replace % "-" "_")}))))