Liking cljdoc? Tell your friends :D

korma.core

Core querying and entity functions

Core querying and entity functions
raw docstring

*exec-mode*clj


add-joinsclj

(add-joins query ent rel)
(add-joins query ent rel type)

aggregatecljmacro

(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
raw docstring

as-sqlclj

(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.
raw docstring

assoc-db-to-entityclj

(assoc-db-to-entity query ent)

belongs-tocljmacro

(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))
raw docstring

create-entityclj

(create-entity table)

Create an entity representing a table in a database.

Create an entity representing a table in a database.
raw docstring

create-relationclj

(create-relation ent sub-ent type opts)

databaseclj

(database ent db)

Set the database connection to be used for this entity.

Set the database connection to be used for this entity.
raw docstring

defentitycljmacro

(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.
raw docstring

deletecljmacro

(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}))
raw docstring

delete*clj

(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
raw docstring

dry-runcljmacro

(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.
raw docstring

empty-queryclj

(empty-query ent)

entity-fieldsclj

(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.
raw docstring

entity?clj

(entity? x)

execclj

(exec query)

Execute a query map and return the results.

Execute a query map and return the results.
raw docstring

exec-rawclj

(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)
raw docstring

fieldsclj

(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])
raw docstring

fkclj

(fk & fk)

Set the foreign key used for an entity relationship.

Set the foreign key used for an entity relationship.
raw docstring

fromclj

(from query table)

Add tables to the from clause.

Add tables to the from clause.
raw docstring

get-relclj

(get-rel ent sub-ent)

groupclj

(group query & fields)

Add a group-by clause to a select query

Add a group-by clause to a select query
raw docstring

has-manycljmacro

(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))
raw docstring

has-onecljmacro

(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))
raw docstring

havingcljmacro

(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.
raw docstring

having*clj

(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.
raw docstring

insertcljmacro

(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"}]))
raw docstring

insert*clj

(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
raw docstring

intersectcljmacro

(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))
raw docstring

intersect*clj

(intersect*)

Create an empty intersect query.

Create an empty intersect query.
raw docstring

joincljmacro

(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))
raw docstring

join*clj

(join* query type table clause)

lfkclj

(lfk & lfk)

Set the left foreign key used for an entity relationship.

Set the left foreign key used for an entity relationship.
raw docstring

limitclj

(limit query v)

Add a limit clause to a select query.

Add a limit clause to a select query.
raw docstring

many-to-manycljmacro

(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))
raw docstring

many-to-many-fnclj

(many-to-many-fn ent sub-ent-var join-table opts)

modifierclj

(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"))
raw docstring

offsetclj

(offset query v)

Add an offset clause to a select query.

Add an offset clause to a select query.
raw docstring

orderclj

(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)
raw docstring

pkclj

(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.
raw docstring

post-queryclj

(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.
raw docstring

prepareclj

(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
raw docstring

queriesclj

(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
raw docstring

query-onlycljmacro

(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 docstring

rawclj

(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())")))
raw docstring

relclj

(rel ent sub-ent type opts)

rfkclj

(rfk & rfk)

Set the right foreign key used for an entity relationship.

Set the right foreign key used for an entity relationship.
raw docstring

selectcljmacro

(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}))
raw docstring

select*clj

(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
raw docstring

set-fieldsclj

(set-fields query fields-map)

Set the fields and values for an update query.

Set the fields and values for an update query.
raw docstring

sql-onlycljmacro

(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.
raw docstring

sqlfncljmacro

(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
raw docstring

sqlfn*clj

(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
raw docstring

subselectcljmacro

(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))]}))
raw docstring

tableclj

(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.
raw docstring

transformclj

(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
raw docstring

unioncljmacro

(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))
raw docstring

union*clj

(union*)

Create an empty union query.

Create an empty union query.
raw docstring

union-allcljmacro

(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))
raw docstring

union-all*clj

(union-all*)

Create an empty union-all query.

Create an empty union-all query.
raw docstring

updatecljmacro

(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}))
raw docstring

update*clj

(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.
raw docstring

valuesclj

(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"}])
raw docstring

wherecljmacro

(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"]})
raw docstring

where*clj

(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.
raw docstring

withcljmacro

(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})))
raw docstring

with*clj

(with* query sub-ent body-fn)

with-batchcljmacro

(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)
raw docstring

with-batch*clj

(with-batch* query sub-ent body-fn)

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close