ClojureQL is designed to have a consistent uniform API. The following rules are always guaranteed:
@(table db :t1)
(deref (table db :t1))
ClojureQL uses a bit of syntax to keep your queries concise and elegant. Below are example conversions of keywords to SQL column specifications. (note: It is not necessary to learn this syntax as strings can be used instead, however it is recommended)
Input | Output |
:table | "table" |
:table.col | "table.col" |
:function/col | "function(col)" |
:function/col1:col2 | "function(col1, col2)" |
:col#asc | "col asc" |
:col#desc | "col desc" |
Example
(def db
{:classname "com.mysql.jdbc.Driver"
:subprotocol "mysql"
:user "cql"
:password "cql"
:subname "//localhost:3306/cql"})
Complete specification
:classname
(required) a String
, the jdbc driver class name.
:subprotocol
(required) a String
, the jdbc subprotocol.
:subname
(required) a String
, the jdbc subname.
(others)
(optional) passed to the driver as properties.
:datasource
(optional) a javax.sql.DataSource
:username
(optional) a String
:password
(optional) a String
, required if :username
is supplied
:name
(optional) a String or javax.naming.Name
:environment
(optional) a java.util.Map
:auto-commit
(optional) a Boolean
:fetch-size
(optional) an integer
Creates a table object optionally associated with a connection
specification. This can be either a keyword from a previous call to
(open-global
) or a hash-map
.
Confines the query to rows for which the predicate is true
Ex.
(select (table :users) (where (= :id 5)))
Confines the query to the fieldlist supplied in fields
Ex.
(project (table :users) [:email])
Limits the query to n number of rows
Skips n number of rows
Sorts a table as per the definition given in spec.
Ex.
(sort (table :t1) [:id :rank]) ; implicitly ASC
(sort (table :t2) [:id#asc :rank#desc])
Makes the query distinct
Joins two tables on join_on
Ex.
(join (table :one) (table :two) :id)
(join (table :one) (table :two) (where (= :one.col :two.col)))
Joins two tables on join_on and sets the direction of the join. type
can be :right
, :left
, :full
etc. Backend support may vary.
Ex.
(outer-join (table :one) (table :two) :left :id)
(outer-join (table :one) (table :two) :left (where (= :one.id :two.id)))
Renames colums when joining. Newnames is a map of replacement pairs
Ex.
(-> (join (table :one) (table :two) :id)
(project [:id]) (rename {:one.id :idx}))
Selects aggregates from a table. Aggregates are denoted with the
:function/field
syntax. They can be aliased by supplying a vector
[:function/field :as :myfn]
. Optionally accepts a group-by argument
Ex.
(-> (table :one) (aggregate [[:count/* :as :cnt]] [:id]))
Allows for arbitrary modifiers to be applied on the result. Can either
be called directly or via helper interfaces like distinct
.
Ex.
(-> (table :one) (modify \"TOP 5\")) ; MSSqls special LIMIT syntax (-> (table :one) distinct)
Selects the difference between tables. Mode can take a keyword which
can be anything which your backend supports. Commonly :all
is used
to allow duplicate rows.
Ex.
(-> (table :one) (difference (table :two) :all))
Selects the intersection between tables. Mode can take a keyword which
can be anything which your backend supports. Commonly :all
is used to
allow duplicate rows.
Ex.
(-> (table :one) (intersection (table :two) :all))
Selects the union between tables. Mode can take a keyword which can be
anything which your backend supports. Commonly :all
is used to allow
duplicate rows.
Ex.
(-> (table :one) (union (table :two) :all))
Inserts record(s) into the table
Ex.
(conj! (table :one) {:age 22})
(conj! (table :one) [{:age 22} {:age 23}])
Deletes record(s) from the table
Ex.
(disj! (table :one) (where (= :age 22)))
Inserts or updates record(s) where pred
is true
. Accepts records
as both maps and collections.
Ex.
(update-in! (table :one) (where (= :id 5)) {:age 22})
For queries where you know only a single result will be returned,
pick!
calls the keyword on that result. You can supply multiple
keywords in a collection. Returns nil for no-hits, throws an exception
on multiple hits.
Ex.
(-> (table :users) (select (where (= :id 5))) ; We know this will only match 1 row
(pick! :email))
Constructs a where-clause for queries.
Ex.
(where (or (< :a 2) (>= :b 4))) => \"((a < ?) OR (b >= ?))\"
If you call str on the result, you'll get the above. If you call
(:env
) you will see the captured environment
Use as: (select tble (where ...))
Executes the body, wherein the results of the query can be accessed via the name supplies as results.
Ex.
(with-results [res table]
(println res))
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close