Implementation of select
and variations.
The args spec used by select
lives in toucan2.query
, specifically :toucan2.query/default-args
.
Code for building Honey SQL for a SELECT lives in toucan2.map-backend.honeysql2
.
Functions that return primary keys such as select-pks-set
determine which primary keys to return by
calling toucan2.model/select-pks-fn
, which is based on the model's implementation
of toucan2.model/primary-keys
. Models with just a single primary key column will return primary keys 'unwrapped',
i.e., the values of that column will be returned directly. Models with compound primary keys (i.e., primary keys
consisting of more than one column) will be returned in vectors as if by calling juxt
.
;; A model with a one-column primary key, :id
(t2/select-pks-vec :models/venues :category "bar")
;; => [1 2]
;; A model with a compound primary key, [:id :name]
(t2/select-pks-vec :models/venues.compound-key :category "bar")
;; => [[1 "Tempest"] [2 "Ho's Tavern"]]
Implementation of [[select]] and variations. The args spec used by [[select]] lives in [[toucan2.query]], specifically `:toucan2.query/default-args`. Code for building Honey SQL for a SELECT lives in [[toucan2.map-backend.honeysql2]]. ### Functions that return primary keys Functions that return primary keys such as [[select-pks-set]] determine which primary keys to return by calling [[toucan2.model/select-pks-fn]], which is based on the model's implementation of [[toucan2.model/primary-keys]]. Models with just a single primary key column will return primary keys 'unwrapped', i.e., the values of that column will be returned directly. Models with compound primary keys (i.e., primary keys consisting of more than one column) will be returned in vectors as if by calling `juxt`. ```clj ;; A model with a one-column primary key, :id (t2/select-pks-vec :models/venues :category "bar") ;; => [1 2] ;; A model with a compound primary key, [:id :name] (t2/select-pks-vec :models/venues.compound-key :category "bar") ;; => [[1 "Tempest"] [2 "Ho's Tavern"]] ```
(count modelable-columns & kv-args? query?)
(count :conn connectable modelable-columns & kv-args? query?)
Like select
, but returns the number of rows that match in an efficient way.
The default Honey SQL 2 map query compilation backend builds an efficient
SELECT count(*) AS "count" FROM ...
query. Custom query compilation backends should do the equivalent by implementing toucan2.pipeline/build
for the
query type :toucan.query-type/select.count
and build a query that returns the key :count
, If an efficient
implementation does not exist, this will fall back to simply counting all matching rows.
Like [[select]], but returns the number of rows that match in an efficient way. ### Implementation note: The default Honey SQL 2 map query compilation backend builds an efficient ```sql SELECT count(*) AS "count" FROM ... ``` query. Custom query compilation backends should do the equivalent by implementing [[toucan2.pipeline/build]] for the query type `:toucan.query-type/select.count` and build a query that returns the key `:count`, If an efficient implementation does not exist, this will fall back to simply counting all matching rows.
(exists? modelable-columns & kv-args? query?)
(exists? :conn connectable modelable-columns & kv-args? query?)
Like select
, but returns whether or not any rows match in an efficient way.
The default Honey SQL 2 map query compilation backend builds an efficient
SELECT exists(SELECT 1 FROM ... WHERE ...) AS exists
Like [[select]], but returns whether or not *any* rows match in an efficient way. ### Implementation note: The default Honey SQL 2 map query compilation backend builds an efficient ```sql SELECT exists(SELECT 1 FROM ... WHERE ...) AS exists ```
(reducible-select modelable-columns & kv-args? query?)
(reducible-select :conn connectable modelable-columns & kv-args? query?)
Like select
, but returns an IReduceInit
.
Like [[select]], but returns an `IReduceInit`.
(select modelable-columns & kv-args? query?)
(select :conn connectable modelable-columns & kv-args? query?)
(select-fn->fn f1 f2 modelable-columns & kv-args? query?)
(select-fn->fn f1 f2 :conn connectable modelable-columns & kv-args? query?)
Return a map of (f1 instance)
-> (f2 instance)
for instances matching the query.
(t2/select-fn->fn :id (comp str/upper-case :name) :models/people)
;; => {1 "CAM", 2 "SAM", 3 "PAM", 4 "TAM"}
Return a map of `(f1 instance)` -> `(f2 instance)` for instances matching the query. ```clj (t2/select-fn->fn :id (comp str/upper-case :name) :models/people) ;; => {1 "CAM", 2 "SAM", 3 "PAM", 4 "TAM"} ```
(select-fn->pk f modelable-columns & kv-args? query?)
(select-fn->pk f :conn connectable modelable-columns & kv-args? query?)
The inverse of select-pk->fn
. Return a map of (f instance)
-> primary key for instances matching the query.
(t2/select-fn->pk (comp str/upper-case :name) :models/people)
;; => {"CAM" 1, "SAM" 2, "PAM" 3, "TAM" 4}
The inverse of [[select-pk->fn]]. Return a map of `(f instance)` -> *primary key* for instances matching the query. ```clj (t2/select-fn->pk (comp str/upper-case :name) :models/people) ;; => {"CAM" 1, "SAM" 2, "PAM" 3, "TAM" 4} ```
(select-fn-reducible f modelable-columns & kv-args? query?)
(select-fn-reducible f :conn connectable modelable-columns & kv-args? query?)
Like reducible-select
, but returns a reducible sequence of results of (f row)
.
Like [[reducible-select]], but returns a reducible sequence of results of `(f row)`.
(select-fn-set f modelable-columns & kv-args? query?)
(select-fn-set f :conn connectable modelable-columns & kv-args? query?)
Like select
, but returns a set of values of (f instance)
for the results. Returns nil
if the set is empty.
(t2/select-fn-set (comp str/upper-case :category) :models/venues :category "bar")
;; =>
#{"BAR"}
Like [[select]], but returns a *set* of values of `(f instance)` for the results. Returns `nil` if the set is empty. ```clj (t2/select-fn-set (comp str/upper-case :category) :models/venues :category "bar") ;; => #{"BAR"} ```
(select-fn-vec f modelable-columns & kv-args? query?)
(select-fn-vec f :conn connectable modelable-columns & kv-args? query?)
Like select
, but returns a vector of values of (f instance)
for the results. Returns nil
if the vector is
empty.
(t2/select-fn-vec (comp str/upper-case :category) :models/venues :category "bar")
;; =>
["BAR" "BAR"]
NOTE: If your query does not specify an :order-by
clause (or equivalent), the results are like indeterminate. Keep
this in mind!
Like [[select]], but returns a *vector* of values of `(f instance)` for the results. Returns `nil` if the vector is empty. ```clj (t2/select-fn-vec (comp str/upper-case :category) :models/venues :category "bar") ;; => ["BAR" "BAR"] ``` NOTE: If your query does not specify an `:order-by` clause (or equivalent), the results are like indeterminate. Keep this in mind!
(select-one modelable-columns & kv-args? query?)
(select-one :conn connectable modelable-columns & kv-args? query?)
Like select
, but only fetches a single row, and returns only that row.
Like [[select]], but only fetches a single row, and returns only that row.
(select-one-fn f modelable-columns & kv-args? query?)
(select-one-fn f :conn connectable modelable-columns & kv-args? query?)
Like select-one
, but applies f
to the result.
(t2/select-one-fn :id :models/people :name "Cam")
;; => 1
Like [[select-one]], but applies `f` to the result. ```clj (t2/select-one-fn :id :models/people :name "Cam") ;; => 1 ```
(select-one-pk modelable-columns & kv-args? query?)
(select-one-pk :conn connectable modelable-columns & kv-args? query?)
Return the primary key of the first row matching the query. Models with just a single primary key columns will be
'unwrapped' (i.e., the values of that column will be returned); models with compound primary keys (i.e., more than one
column) will be returned in vectors as if by calling juxt
.
(t2/select-one-pk :models/people :name "Cam")
;; => 1
Return the primary key of the first row matching the query. Models with just a single primary key columns will be 'unwrapped' (i.e., the values of that column will be returned); models with compound primary keys (i.e., more than one column) will be returned in vectors as if by calling `juxt`. ```clj (t2/select-one-pk :models/people :name "Cam") ;; => 1 ```
(select-pk->fn f modelable-columns & kv-args? query?)
(select-pk->fn f :conn connectable modelable-columns & kv-args? query?)
The inverse of select-fn->pk
. Return a map of primary key -> (f instance)
for instances matching the query.
(t2/select-pk->fn (comp str/upper-case :name) :models/people)
;; => {1 "CAM", 2 "SAM", 3 "PAM", 4 "TAM"}
The inverse of [[select-fn->pk]]. Return a map of *primary key* -> `(f instance)` for instances matching the query. ```clj (t2/select-pk->fn (comp str/upper-case :name) :models/people) ;; => {1 "CAM", 2 "SAM", 3 "PAM", 4 "TAM"} ```
(select-pks-reducible modelable-columns & kv-args? query?)
(select-pks-reducible :conn connectable modelable-columns & kv-args? query?)
Returns a reducible sequence of all primary keys
(into [] (t2/select-pks-reducible :models/venues :category "bar"))
;; => [1 2]
Returns a reducible sequence of all primary keys ```clj (into [] (t2/select-pks-reducible :models/venues :category "bar")) ;; => [1 2] ```
(select-pks-set modelable-columns & kv-args? query?)
(select-pks-set :conn connectable modelable-columns & kv-args? query?)
Returns a set of all primary keys (as determined by toucan2.model/primary-keys
and toucan2.model/select-pks-fn
) of instances matching the query. Models with just a single primary key columns
will be 'unwrapped' (i.e., the values of that column will be returned); models with compound primary keys (i.e., more
than one column) will be returned in vectors as if by calling juxt
.
(t2/select-pks-set :models/venues :category "bar")
;; => #{1 2}
Returns a *set* of all primary keys (as determined by [[toucan2.model/primary-keys]] and [[toucan2.model/select-pks-fn]]) of instances matching the query. Models with just a single primary key columns will be 'unwrapped' (i.e., the values of that column will be returned); models with compound primary keys (i.e., more than one column) will be returned in vectors as if by calling `juxt`. ```clj (t2/select-pks-set :models/venues :category "bar") ;; => #{1 2} ```
(select-pks-vec modelable-columns & kv-args? query?)
(select-pks-vec :conn connectable modelable-columns & kv-args? query?)
Returns a vector of all primary keys (as determined by toucan2.model/primary-keys
and toucan2.model/select-pks-fn
) of instances matching the query. Models with just a single primary key columns
will be 'unwrapped' (i.e., the values of that column will be returned); models with compound primary keys (i.e., more
than one column) will be returned in vectors as if by calling juxt
.
(t2/select-pks-vec :models/venues :category "bar")
;; => [1 2]
NOTE: If your query does not specify an :order-by
clause (or equivalent), the results are like indeterminate. Keep
this in mind!
Returns a *vector* of all primary keys (as determined by [[toucan2.model/primary-keys]] and [[toucan2.model/select-pks-fn]]) of instances matching the query. Models with just a single primary key columns will be 'unwrapped' (i.e., the values of that column will be returned); models with compound primary keys (i.e., more than one column) will be returned in vectors as if by calling `juxt`. ```clj (t2/select-pks-vec :models/venues :category "bar") ;; => [1 2] ``` NOTE: If your query does not specify an `:order-by` clause (or equivalent), the results are like indeterminate. Keep this in mind!
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close