Variants of 'query' functions from clojure.java.jdbc that support the new clojure.datafy functionality in Clojure 1.10.
The whole schema/column lookup piece is very likely to change!
Currently, the :schema option for a 'query' function is a mapping from column name to a tuple of table name, key column, and optionally the cardinality (:one -- the default -- or :many). The cardinality determines whether navigation should produce a single row (hash map) or a result set.
One of the problems is that the general case -- query -- doesn't have any concept of an associated table name (and may of course join across multiple tables), so there's no good way to take the table name into account when mapping a column to another table.
For find-by-keys and get-by-id, you do have the starting table name so you could map [table1 column1] to [table2 column2] and have table-specific mappings.
The obvious, logical thing would be to use SQL metadata to figure out actual foreign key constraints but not everyone uses them, for a variety of reasons. For folks who do use them, they can build their schema structure from the database, and pass the relevant part of it to the functions below (via :schema in options).
Variants of 'query' functions from clojure.java.jdbc that support the new clojure.datafy functionality in Clojure 1.10. The whole schema/column lookup piece is very likely to change! Currently, the :schema option for a 'query' function is a mapping from column name to a tuple of table name, key column, and optionally the cardinality (:one -- the default -- or :many). The cardinality determines whether navigation should produce a single row (hash map) or a result set. One of the problems is that the general case -- query -- doesn't have any concept of an associated table name (and may of course join across multiple tables), so there's no good way to take the table name into account when mapping a column to another table. For find-by-keys and get-by-id, you do have the starting table name so you could map [table1 column1] to [table2 column2] and have table-specific mappings. The obvious, logical thing would be to use SQL metadata to figure out actual foreign key constraints but not everyone uses them, for a variety of reasons. For folks who do use them, they can build their schema structure from the database, and pass the relevant part of it to the functions below (via :schema in options).
(find-by-keys db table columns)(find-by-keys db table columns opts)Given a database connection, a table name, a map of column name/value pairs, and an optional options map, return any matching rows.
An :order-by option may be supplied to sort the rows, e.g.,
{:order-by [{:name :asc} {:age :desc} {:income :asc}]}
;; equivalent to:
{:order-by [:name {:age :desc} :income]}
The :order-by value is a sequence of column names (to sort in ascending order) and/or maps from column names to directions (:asc or :desc). The directions may be strings or keywords and are not case-sensitive. They are mapped to ASC or DESC in the generated SQL.
Note: if a ordering map has more than one key, the order of the columns in the generated SQL ORDER BY clause is unspecified (so such maps should only contain one key/value pair).
Given a database connection, a table name, a map of column name/value
pairs, and an optional options map, return any matching rows.
An :order-by option may be supplied to sort the rows, e.g.,
{:order-by [{:name :asc} {:age :desc} {:income :asc}]}
;; equivalent to:
{:order-by [:name {:age :desc} :income]}
The :order-by value is a sequence of column names (to sort in ascending
order) and/or maps from column names to directions (:asc or :desc). The
directions may be strings or keywords and are not case-sensitive. They
are mapped to ASC or DESC in the generated SQL.
Note: if a ordering map has more than one key, the order of the columns
in the generated SQL ORDER BY clause is unspecified (so such maps should
only contain one key/value pair).(get-by-id db table pk-value)(get-by-id db table pk-value pk-name-or-opts)(get-by-id db table pk-value pk-name opts)Given a database connection, a table name, a primary key value, an optional primary key column name, and an optional options map, return a single matching row, or nil. The primary key column name defaults to :id.
Given a database connection, a table name, a primary key value, an optional primary key column name, and an optional options map, return a single matching row, or nil. The primary key column name defaults to :id.
(query db sql-params)(query db sql-params opts)Given a database connection and a vector containing SQL and optional parameters, perform a simple database query. The options specify how to construct the result set (and are also passed to prepare-statement as needed): :as-arrays? - return the results as a set of arrays, default false. :identifiers - applied to each column name in the result set, default lower-case :keywordize? - defaults to true, can be false to opt-out of converting identifiers to keywords :qualifier - optionally provides the namespace qualifier for identifiers :result-set-fn - applied to the entire result set, default doall / vec if :as-arrays? true, :result-set-fn will default to vec if :as-arrays? false, :result-set-fn will default to doall :row-fn - applied to each row as the result set is constructed, default identity The second argument is a vector containing a SQL string or PreparedStatement, followed by any parameters it needs. See also prepare-statement for additional options.
Given a database connection and a vector containing SQL and optional parameters,
perform a simple database query. The options specify how to construct the result
set (and are also passed to prepare-statement as needed):
:as-arrays? - return the results as a set of arrays, default false.
:identifiers - applied to each column name in the result set, default lower-case
:keywordize? - defaults to true, can be false to opt-out of converting
identifiers to keywords
:qualifier - optionally provides the namespace qualifier for identifiers
:result-set-fn - applied to the entire result set, default doall / vec
if :as-arrays? true, :result-set-fn will default to vec
if :as-arrays? false, :result-set-fn will default to doall
:row-fn - applied to each row as the result set is constructed, default identity
The second argument is a vector containing a SQL string or PreparedStatement, followed
by any parameters it needs.
See also prepare-statement for additional options.cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |