Core querying and entity functions
Core querying and entity functions
(add-joins query ent rel)
(add-joins query ent rel type)
(aggregate query agg alias & [group-by])
Use a SQL aggregator function, aliasing the results, and optionally grouping by a field:
(select users (aggregate (count :*) :cnt :status))
Aggregates available: count, sum, avg, min, max, first, last
Use a SQL aggregator function, aliasing the results, and optionally grouping by a field: (select users (aggregate (count :*) :cnt :status)) Aggregates available: count, sum, avg, min, max, first, last
(as-sql query)
Force a query to return a string of SQL when (exec) is called.
Force a query to return a string of SQL when (exec) is called.
(assoc-db-to-entity query ent)
(belongs-to ent sub-ent)
(belongs-to ent sub-ent opts)
Add a belongs-to relationship for the given entity. It is assumed that the foreign key
is on the current entity with the format sub-ent-table_id: email.user_id = user.id.
Can optionally pass a map with a :fk key collection or use the helper fk
function
to explicitly set the foreign key.
(belongs-to users email) (belongs-to users email (fk :userId))
Add a belongs-to relationship for the given entity. It is assumed that the foreign key is on the current entity with the format sub-ent-table_id: email.user_id = user.id. Can optionally pass a map with a :fk key collection or use the helper `fk` function to explicitly set the foreign key. (belongs-to users email) (belongs-to users email (fk :userId))
(create-entity table)
Create an entity representing a table in a database.
Create an entity representing a table in a database.
(create-relation ent sub-ent type opts)
(database ent db)
Set the database connection to be used for this entity.
Set the database connection to be used for this entity.
(defentity ent & body)
Define an entity representing a table in the database, applying any modifications in the body.
Define an entity representing a table in the database, applying any modifications in the body.
(delete ent & body)
Creates a delete query, applies any modifying functions in the body and then
executes it. ent
is either a string or an entity created by defentity.
Returns number of deleted rows as provided by the JDBC driver.
ex: (delete user (where {:id 7}))
Creates a delete query, applies any modifying functions in the body and then executes it. `ent` is either a string or an entity created by defentity. Returns number of deleted rows as provided by the JDBC driver. ex: (delete user (where {:id 7}))
(delete* ent)
Create an empty delete query. Ent can either be an entity defined by defentity, or a string of the table name
Create an empty delete query. Ent can either be an entity defined by defentity, or a string of the table name
(dry-run & body)
Wrap around a set of queries to print to the console all SQL that would be run and return dummy values instead of executing them.
Wrap around a set of queries to print to the console all SQL that would be run and return dummy values instead of executing them.
(empty-query ent)
(entity-fields ent & fields)
Set the fields to be retrieved by default in select queries for the entity.
Set the fields to be retrieved by default in select queries for the entity.
(entity? x)
(exec query)
Execute a query map and return the results.
Execute a query map and return the results.
(exec-raw conn? & [sql with-results?])
Execute a raw SQL string, supplying whether results should be returned. sql
can either be a string or a vector of the sql string and its params. You can
also optionally provide the connection to execute against as the first
parameter.
(exec-raw ["SELECT * FROM users WHERE age > ?" [5]] :results)
Execute a raw SQL string, supplying whether results should be returned. `sql` can either be a string or a vector of the sql string and its params. You can also optionally provide the connection to execute against as the first parameter. (exec-raw ["SELECT * FROM users WHERE age > ?" [5]] :results)
(fields query & vs)
Set the fields to be selected in a query. Fields can either be a keyword or a vector of two keywords [field alias]:
(fields query :name [:firstname :first])
Set the fields to be selected in a query. Fields can either be a keyword or a vector of two keywords [field alias]: (fields query :name [:firstname :first])
(fk & fk)
Set the foreign key used for an entity relationship.
Set the foreign key used for an entity relationship.
(from query table)
Add tables to the from clause.
Add tables to the from clause.
(get-rel ent sub-ent)
(group query & fields)
Add a group-by clause to a select query
Add a group-by clause to a select query
(has-many ent sub-ent)
(has-many ent sub-ent opts)
Add a has-many relation for the given entity. It is assumed that the foreign key
is on the sub-entity with the format table_id: user.id = email.user_id
Can optionally pass a map with a :fk key collection or use the helper fk
function
to explicitly set the foreign key.
(has-many users email) (has-many users email (fk :emailID))
Add a has-many relation for the given entity. It is assumed that the foreign key is on the sub-entity with the format table_id: user.id = email.user_id Can optionally pass a map with a :fk key collection or use the helper `fk` function to explicitly set the foreign key. (has-many users email) (has-many users email (fk :emailID))
(has-one ent sub-ent)
(has-one ent sub-ent opts)
Add a has-one relationship for the given entity. It is assumed that the foreign key
is on the sub-entity with the format table_id: user.id = address.user_id
Can optionally pass a map with a :fk key collection or use the helper fk
function
to explicitly set the foreign key.
(has-one users address) (has-one users address (fk :userId))
Add a has-one relationship for the given entity. It is assumed that the foreign key is on the sub-entity with the format table_id: user.id = address.user_id Can optionally pass a map with a :fk key collection or use the helper `fk` function to explicitly set the foreign key. (has-one users address) (has-one users address (fk :userId))
(having query form)
Add a having clause to the query, expressing the clause in clojure expressions with keywords used to reference fields. e.g. (having query (or (= :hits 1) (> :hits 5)))
Available predicates: and, or, =, not=, <, >, <=, >=, in, like, not, between
Having can also take a map at any point and will create a clause that compares keys to values. The value can be a vector with one of the above predicate functions describing how the key is related to the value: (having query {:name [like "chris"})
Having only works if you have an aggregation, using it without one will cause an error.
Add a having clause to the query, expressing the clause in clojure expressions with keywords used to reference fields. e.g. (having query (or (= :hits 1) (> :hits 5))) Available predicates: and, or, =, not=, <, >, <=, >=, in, like, not, between Having can also take a map at any point and will create a clause that compares keys to values. The value can be a vector with one of the above predicate functions describing how the key is related to the value: (having query {:name [like "chris"}) Having only works if you have an aggregation, using it without one will cause an error.
(having* query clause)
Add a having clause to the query. Clause can be either a map or a string, and will be AND'ed to the other clauses.
Add a having clause to the query. Clause can be either a map or a string, and will be AND'ed to the other clauses.
(insert ent & body)
Creates an insert query, applies any modifying functions in the body
and then executes it. ent
is either a string or an entity created by
defentity. The return value is the last inserted item, but its
representation is dependent on the database driver used
(e.g. postgresql returns the full row as a hash while MySQL and MSSQL
both return a {:generated_key <ID>} hash)
ex: (insert user (values [{:name "chris"} {:name "john"}]))
Creates an insert query, applies any modifying functions in the body and then executes it. `ent` is either a string or an entity created by defentity. The return value is the last inserted item, but its representation is dependent on the database driver used (e.g. postgresql returns the full row as a hash while MySQL and MSSQL both return a {:generated_key <ID>} hash) ex: (insert user (values [{:name "chris"} {:name "john"}]))
(insert* ent)
Create an empty insert query. Ent can either be an entity defined by defentity, or a string of the table name
Create an empty insert query. Ent can either be an entity defined by defentity, or a string of the table name
(intersect & body)
Creates an intersect query, applies any modifying functions in the body and then executes it.
ex: (intersect (queries (subselect user (where {:id 7})) (subselect user-backup (where {:id 8}))) (order :name))
Creates an intersect query, applies any modifying functions in the body and then executes it. ex: (intersect (queries (subselect user (where {:id 7})) (subselect user-backup (where {:id 8}))) (order :name))
(intersect*)
Create an empty intersect query.
Create an empty intersect query.
(join query ent)
(join query type ent)
(join query table clause)
(join query type table clause)
Add a join clause to a select query, specifying an entity defined by defentity, or the table name to join and the predicate to join on. If the entity relationship uses a join table then two clauses will be added. Otherwise, only one clause will be added.
(join query addresses) (join query :right addresses) (join query addresses (= :addres.users_id :users.id)) (join query :right addresses (= :address.users_id :users.id))
Add a join clause to a select query, specifying an entity defined by defentity, or the table name to join and the predicate to join on. If the entity relationship uses a join table then two clauses will be added. Otherwise, only one clause will be added. (join query addresses) (join query :right addresses) (join query addresses (= :addres.users_id :users.id)) (join query :right addresses (= :address.users_id :users.id))
(join* query type table clause)
(lfk & lfk)
Set the left foreign key used for an entity relationship.
Set the left foreign key used for an entity relationship.
(limit query v)
Add a limit clause to a select query.
Add a limit clause to a select query.
(many-to-many ent sub-ent join-table)
(many-to-many ent sub-ent join-table opts)
(many-to-many ent sub-ent join-table fk1 fk2)
Add a many-to-many relation for the given entity. It is assumed that a join
table is used to implement the relationship and that the foreign keys are in
the join table with the format table_id:
user.id = user_email.user_id
user_email.email_id = email.id
Can optionally pass a map with :lfk and :rfk key collections or use the
helper lfk
, rfk
functions to explicitly set the join keys.
(many-to-many email :user_email) (many-to-many email :user_email (lfk :user_id) (rfk :email_id))
Add a many-to-many relation for the given entity. It is assumed that a join table is used to implement the relationship and that the foreign keys are in the join table with the format table_id: user.id = user_email.user_id user_email.email_id = email.id Can optionally pass a map with :lfk and :rfk key collections or use the helper `lfk`, `rfk` functions to explicitly set the join keys. (many-to-many email :user_email) (many-to-many email :user_email (lfk :user_id) (rfk :email_id))
(many-to-many-fn ent sub-ent-var join-table opts)
(modifier query & modifiers)
Add a modifer to the beginning of a query:
(select orders (modifier "DISTINCT"))
Add a modifer to the beginning of a query: (select orders (modifier "DISTINCT"))
(offset query v)
Add an offset clause to a select query.
Add an offset clause to a select query.
(order query field)
(order query field dir)
Add an ORDER BY clause to a select, union, union-all, or intersect query. field should be a keyword of the field name, dir is ASC by default.
(order query :created :asc)
Add an ORDER BY clause to a select, union, union-all, or intersect query. field should be a keyword of the field name, dir is ASC by default. (order query :created :asc)
(pk ent & pk)
Set the primary key used for an entity. :id by default.
Set the primary key used for an entity. :id by default.
(post-query query post)
Add a function representing a query that should be executed for each result in a select. This is done lazily over the result set.
Add a function representing a query that should be executed for each result in a select. This is done lazily over the result set.
(prepare ent func)
Add a function to be applied to records/values going into the database
Add a function to be applied to records/values going into the database
(queries query & queries)
Adds a group of queries to a union, union-all or intersect
Adds a group of queries to a union, union-all or intersect
(query-only & body)
Wrap around a set of queries to force them to return their query objects.
Wrap around a set of queries to force them to return their query objects.
(raw s)
Embed a raw string of SQL in a query. This is used when Korma doesn't provide some specific functionality you're looking for:
(select users (fields (raw "PERIOD(NOW(), NOW())")))
Embed a raw string of SQL in a query. This is used when Korma doesn't provide some specific functionality you're looking for: (select users (fields (raw "PERIOD(NOW(), NOW())")))
(rel ent sub-ent type opts)
(rfk & rfk)
Set the right foreign key used for an entity relationship.
Set the right foreign key used for an entity relationship.
(select ent & body)
Creates a select query, applies any modifying functions in the body and then
executes it. ent
is either a string or an entity created by defentity.
ex: (select user (fields :name :email) (where {:id 2}))
Creates a select query, applies any modifying functions in the body and then executes it. `ent` is either a string or an entity created by defentity. ex: (select user (fields :name :email) (where {:id 2}))
(select* ent)
Create a select query with fields provided in Ent. If fields are not provided, create an empty select query. Ent can either be an entity defined by defentity, or a string of the table name
Create a select query with fields provided in Ent. If fields are not provided, create an empty select query. Ent can either be an entity defined by defentity, or a string of the table name
(set-fields query fields-map)
Set the fields and values for an update query.
Set the fields and values for an update query.
(sql-only & body)
Wrap around a set of queries so that instead of executing, each will return a string of the SQL that would be used.
Wrap around a set of queries so that instead of executing, each will return a string of the SQL that would be used.
(sqlfn func & params)
Call an arbitrary SQL function by providing func as a symbol or keyword and its params
Call an arbitrary SQL function by providing func as a symbol or keyword and its params
(sqlfn* fn-name & params)
Call an arbitrary SQL function by providing the name of the function and its params
Call an arbitrary SQL function by providing the name of the function and its params
(subselect & parts)
Create a subselect clause to be used in queries. This works exactly like (select ...) execept it will wrap the query in ( .. ) and make sure it can be used in any current query:
(select users (where {:id [in (subselect users2 (fields :id))]}))
Create a subselect clause to be used in queries. This works exactly like (select ...) execept it will wrap the query in ( .. ) and make sure it can be used in any current query: (select users (where {:id [in (subselect users2 (fields :id))]}))
(table ent t & [alias])
Set the name of the table and an optional alias to be used for the entity. By default the table is the name of entity's symbol.
Set the name of the table and an optional alias to be used for the entity. By default the table is the name of entity's symbol.
(transform ent func)
Add a function to be applied to results coming from the database
Add a function to be applied to results coming from the database
(union & body)
Creates a union query, applies any modifying functions in the body and then executes it.
ex: (union (queries (subselect user (where {:id 7})) (subselect user-backup (where {:id 7}))) (order :name))
Creates a union query, applies any modifying functions in the body and then executes it. ex: (union (queries (subselect user (where {:id 7})) (subselect user-backup (where {:id 7}))) (order :name))
(union-all & body)
Creates a union-all query, applies any modifying functions in the body and then executes it.
ex: (union-all (queries (subselect user (where {:id 7})) (subselect user-backup (where {:id 7}))) (order :name))
Creates a union-all query, applies any modifying functions in the body and then executes it. ex: (union-all (queries (subselect user (where {:id 7})) (subselect user-backup (where {:id 7}))) (order :name))
(union-all*)
Create an empty union-all query.
Create an empty union-all query.
(update ent & body)
Creates an update query, applies any modifying functions in the body and then
executes it. ent
is either a string or an entity created by defentity.
Returns number of updated rows as provided by the JDBC driver.
ex: (update user (set-fields {:name "chris"}) (where {:id 4}))
Creates an update query, applies any modifying functions in the body and then executes it. `ent` is either a string or an entity created by defentity. Returns number of updated rows as provided by the JDBC driver. ex: (update user (set-fields {:name "chris"}) (where {:id 4}))
(update* ent)
Create an empty update query. Ent can either be an entity defined by defentity, or a string of the table name.
Create an empty update query. Ent can either be an entity defined by defentity, or a string of the table name.
(values query values)
Add records to an insert clause. values can either be a vector of maps or a single map.
(values query [{:name "john"} {:name "ed"}])
Add records to an insert clause. values can either be a vector of maps or a single map. (values query [{:name "john"} {:name "ed"}])
(where query form)
Add a where clause to the query, expressing the clause in clojure expressions with keywords used to reference fields. e.g. (where query (or (= :hits 1) (> :hits 5)))
Available predicates: and, or, =, not=, <, >, <=, >=, in, like, not, between
Where can also take a map at any point and will create a clause that compares keys to values. The value can be a vector with one of the above predicate functions describing how the key is related to the value: (where query {:name [like "chris"]})
Add a where clause to the query, expressing the clause in clojure expressions with keywords used to reference fields. e.g. (where query (or (= :hits 1) (> :hits 5))) Available predicates: and, or, =, not=, <, >, <=, >=, in, like, not, between Where can also take a map at any point and will create a clause that compares keys to values. The value can be a vector with one of the above predicate functions describing how the key is related to the value: (where query {:name [like "chris"]})
(where* query clause)
Add a where clause to the query. Clause can be either a map or a string, and will be AND'ed to the other clauses.
Add a where clause to the query. Clause can be either a map or a string, and will be AND'ed to the other clauses.
(with query ent & body)
Add a related entity to the given select query. If the entity has a relationship type of :belongs-to or :has-one, the requested fields will be returned directly in the result map. If the entity is a :has-many, a second query will be executed lazily and a key of the entity name will be assoc'd with a vector of the results.
(defentity email (entity-fields :email)) (defentity user (has-many email)) (select user (with email) => [{:name "chris" :email [{email: "c@c.com"}]} ...
With can also take a body that will further refine the relation: (select user (with address (with state) (fields :address.city :state.state) (where {:address.zip x})))
Add a related entity to the given select query. If the entity has a relationship type of :belongs-to or :has-one, the requested fields will be returned directly in the result map. If the entity is a :has-many, a second query will be executed lazily and a key of the entity name will be assoc'd with a vector of the results. (defentity email (entity-fields :email)) (defentity user (has-many email)) (select user (with email) => [{:name "chris" :email [{email: "c@c.com"}]} ... With can also take a body that will further refine the relation: (select user (with address (with state) (fields :address.city :state.state) (where {:address.zip x})))
(with* query sub-ent body-fn)
(with-batch query ent & body)
Add a related entity. This behaves like with
, except that, for has-many
relationships, it runs a single query to get relations of all fetched rows.
This is faster than regular with
but it doesn't support many of the
additional options (order, limit, offset, group, having)
Add a related entity. This behaves like `with`, except that, for has-many relationships, it runs a single query to get relations of all fetched rows. This is faster than regular `with` but it doesn't support many of the additional options (order, limit, offset, group, having)
(with-batch* query sub-ent body-fn)
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close