A Clojure interface to SQL databases via JDBC
This namespace contains the old API (0.2.3) which was deprecated in the 0.3.0 release and is provided for backward compatibility. This API will be removed completely before a 1.0.0 release so will need to migrate code to the new API before that release.
clojure.java.jdbc provides a simple abstraction for CRUD (create, read, update, delete) operations on a SQL database, along with basic transaction support. Basic DDL operations are also supported (create table, drop table, access to table metadata).
Maps are used to represent records, making it easy to store and retrieve data. Results can be processed using any standard sequence operations.
For most operations, Java's PreparedStatement is used so your SQL and parameters can be represented as simple vectors where the first element is the SQL string, with ? for each parameter, and the remaining elements are the parameter values to be substituted. In general, operations return the number of rows affected, except for a single record insert where any generated keys are returned (as a map).
A Clojure interface to SQL databases via JDBC This namespace contains the old API (0.2.3) which was deprecated in the 0.3.0 release and is provided for backward compatibility. This API will be removed completely before a 1.0.0 release so will need to migrate code to the new API before that release. clojure.java.jdbc provides a simple abstraction for CRUD (create, read, update, delete) operations on a SQL database, along with basic transaction support. Basic DDL operations are also supported (create table, drop table, access to table metadata). Maps are used to represent records, making it easy to store and retrieve data. Results can be processed using any standard sequence operations. For most operations, Java's PreparedStatement is used so your SQL and parameters can be represented as simple vectors where the first element is the SQL string, with ? for each parameter, and the remaining elements are the parameter values to be substituted. In general, operations return the number of rows affected, except for a single record insert where any generated keys are returned (as a map).
(as-identifier x)
(as-identifier x f-entity)
Given a keyword, convert it to a string using the current naming strategy. Given a string, return it as-is.
Given a keyword, convert it to a string using the current naming strategy. Given a string, return it as-is.
(as-key f x)
Given a naming strategy and a string, return the string as a keyword per that naming strategy. Given (a naming strategy and) a keyword, return it as-is.
Given a naming strategy and a string, return the string as a keyword per that naming strategy. Given (a naming strategy and) a keyword, return it as-is.
(as-keyword x)
(as-keyword x f-keyword)
Given an entity name (string), convert it to a keyword using the current naming strategy. Given a keyword, return it as-is.
Given an entity name (string), convert it to a keyword using the current naming strategy. Given a keyword, return it as-is.
(as-named-identifier naming-strategy x)
Given a naming strategy and a keyword, return the keyword as a string using the entity naming strategy. Given a naming strategy and a string, return the string as-is. The naming strategy should either be a function (the entity naming strategy) or a map containing :entity and/or :keyword keys which provide the entity naming strategy and/or keyword naming strategy respectively.
Given a naming strategy and a keyword, return the keyword as a string using the entity naming strategy. Given a naming strategy and a string, return the string as-is. The naming strategy should either be a function (the entity naming strategy) or a map containing :entity and/or :keyword keys which provide the entity naming strategy and/or keyword naming strategy respectively.
(as-named-keyword naming-strategy x)
Given a naming strategy and a string, return the string as a keyword using the keyword naming strategy. Given a naming strategy and a keyword, return the keyword as-is. The naming strategy should either be a function (the entity naming strategy) or a map containing :entity and/or :keyword keys which provide the entity naming strategy and/or keyword naming strategy respectively. Note that providing a single function will cause the default keyword naming strategy to be used!
Given a naming strategy and a string, return the string as a keyword using the keyword naming strategy. Given a naming strategy and a keyword, return the keyword as-is. The naming strategy should either be a function (the entity naming strategy) or a map containing :entity and/or :keyword keys which provide the entity naming strategy and/or keyword naming strategy respectively. Note that providing a single function will cause the default keyword naming strategy to be used!
(as-quoted-identifier q x)
Given a quote pattern - either a single character or a pair of characters in a vector - and a keyword, return the keyword as a string using a simple quoting naming strategy. Given a qote pattern and a string, return the string as-is. (as-quoted-identifier X :name) will return XnameX as a string. (as-quoted-identifier [A B] :name) will return AnameB as a string.
Given a quote pattern - either a single character or a pair of characters in a vector - and a keyword, return the keyword as a string using a simple quoting naming strategy. Given a qote pattern and a string, return the string as-is. (as-quoted-identifier X :name) will return XnameX as a string. (as-quoted-identifier [A B] :name) will return AnameB as a string.
(as-quoted-str q x)
Given a quoting pattern - either a single character or a vector pair of characters - and a string, return the quoted string: (as-quoted-str X foo) will return XfooX (as-quoted-str [A B] foo) will return AfooB
Given a quoting pattern - either a single character or a vector pair of characters - and a string, return the quoted string: (as-quoted-str X foo) will return XfooX (as-quoted-str [A B] foo) will return AfooB
(as-str f x)
Given a naming strategy and a keyword, return the keyword as a
string per that naming strategy. Given (a naming strategy and)
a string, return it as-is.
A keyword of the form :x.y is treated as keywords :x and :y,
both are turned into strings via the naming strategy and then
joined back together so :x.y might become x
.y
if the naming
strategy quotes identifiers with `.
Given a naming strategy and a keyword, return the keyword as a string per that naming strategy. Given (a naming strategy and) a string, return it as-is. A keyword of the form :x.y is treated as keywords :x and :y, both are turned into strings via the naming strategy and then joined back together so :x.y might become `x`.`y` if the naming strategy quotes identifiers with `.
(connection)
Returns the current database connection (or throws if there is none)
Returns the current database connection (or throws if there is none)
(create-table name & specs)
Creates a table on the open database connection given a table name and specs. Each spec is either a column spec: a vector containing a column name and optionally a type and other constraints, or a table-level constraint: a vector containing words that express the constraint. An optional suffix to the CREATE TABLE DDL describing table attributes may by provided as :table-spec {table-attributes-string}. All words used to describe the table may be supplied as strings or keywords.
Creates a table on the open database connection given a table name and specs. Each spec is either a column spec: a vector containing a column name and optionally a type and other constraints, or a table-level constraint: a vector containing words that express the constraint. An optional suffix to the CREATE TABLE DDL describing table attributes may by provided as :table-spec {table-attributes-string}. All words used to describe the table may be supplied as strings or keywords.
(create-table-ddl name & specs)
Given a table name and column specs with an optional table-spec return the DDL string for creating a table based on that.
Given a table name and column specs with an optional table-spec return the DDL string for creating a table based on that.
(delete-rows table where-params)
Deletes rows from a table. where-params is a vector containing a string providing the (optionally parameterized) selection criteria followed by values for any parameters.
Deletes rows from a table. where-params is a vector containing a string providing the (optionally parameterized) selection criteria followed by values for any parameters.
(do-commands & commands)
Executes SQL commands on the open database connection.
Executes SQL commands on the open database connection.
(do-prepared sql & param-groups)
Executes an (optionally parameterized) SQL prepared statement on the open database connection. Each param-group is a seq of values for all of the parameters. Return a seq of update counts (one count for each param-group).
Executes an (optionally parameterized) SQL prepared statement on the open database connection. Each param-group is a seq of values for all of the parameters. Return a seq of update counts (one count for each param-group).
(do-prepared-return-keys sql param-group)
Executes an (optionally parameterized) SQL prepared statement on the open database connection. The param-group is a seq of values for all of the parameters. Return the generated keys for the (single) update/insert.
Executes an (optionally parameterized) SQL prepared statement on the open database connection. The param-group is a seq of values for all of the parameters. Return the generated keys for the (single) update/insert.
(drop-table name)
Drops a table on the open database connection given its name, a string or keyword
Drops a table on the open database connection given its name, a string or keyword
(find-connection)
Returns the current database connection (or nil if there is none)
Returns the current database connection (or nil if there is none)
(insert-record table record)
Inserts a single record into a table. A record is a map from strings or keywords (identifying columns) to values. Returns a map of the generated keys.
Inserts a single record into a table. A record is a map from strings or keywords (identifying columns) to values. Returns a map of the generated keys.
(insert-records table & records)
Inserts records into a table. records are maps from strings or keywords (identifying columns) to values. Inserts the records one at a time. Returns a sequence of maps containing the generated keys for each record.
Inserts records into a table. records are maps from strings or keywords (identifying columns) to values. Inserts the records one at a time. Returns a sequence of maps containing the generated keys for each record.
(insert-rows table & rows)
Inserts complete rows into a table. Each row is a vector of values for each of the table's columns in order. If a single row is inserted, returns a map of the generated keys.
Inserts complete rows into a table. Each row is a vector of values for each of the table's columns in order. If a single row is inserted, returns a map of the generated keys.
(insert-values table column-names & value-groups)
Inserts rows into a table with values for specified columns only. column-names is a vector of strings or keywords identifying columns. Each value-group is a vector containing a value for each column in order. When inserting complete rows (all columns), consider using insert-rows instead. If a single set of values is inserted, returns a map of the generated keys.
Inserts rows into a table with values for specified columns only. column-names is a vector of strings or keywords identifying columns. Each value-group is a vector containing a value for each column in order. When inserting complete rows (all columns), consider using insert-rows instead. If a single set of values is inserted, returns a map of the generated keys.
(is-rollback-only)
Returns true if the outermost transaction will rollback rather than commit when complete
Returns true if the outermost transaction will rollback rather than commit when complete
(prepare-statement con
sql
&
{:keys [return-keys result-type concurrency cursors
fetch-size max-rows]})
Create a prepared statement from a connection, a SQL string and an optional list of parameters: :return-keys true | false - default false :result-type :forward-only | :scroll-insensitive | :scroll-sensitive :concurrency :read-only | :updatable :fetch-size n :max-rows n
Create a prepared statement from a connection, a SQL string and an optional list of parameters: :return-keys true | false - default false :result-type :forward-only | :scroll-insensitive | :scroll-sensitive :concurrency :read-only | :updatable :fetch-size n :max-rows n
(print-sql-exception exception)
Prints the contents of an SQLException to out
Prints the contents of an SQLException to *out*
(print-sql-exception-chain exception)
Prints a chain of SQLExceptions to out
Prints a chain of SQLExceptions to *out*
(print-update-counts exception)
Prints the update counts from a BatchUpdateException to out
Prints the update counts from a BatchUpdateException to *out*
(resultset-seq rs)
Creates and returns a lazy sequence of maps corresponding to the rows in the java.sql.ResultSet rs. Based on clojure.core/resultset-seq but it respects the current naming strategy. Duplicate column names are made unique by appending _N before applying the naming strategy (where N is a unique integer).
Creates and returns a lazy sequence of maps corresponding to the rows in the java.sql.ResultSet rs. Based on clojure.core/resultset-seq but it respects the current naming strategy. Duplicate column names are made unique by appending _N before applying the naming strategy (where N is a unique integer).
(set-rollback-only)
Marks the outermost transaction such that it will rollback rather than commit when complete
Marks the outermost transaction such that it will rollback rather than commit when complete
(transaction & body)
Evaluates body as a transaction on the open database connection. Any nested transactions are absorbed into the outermost transaction. By default, all database updates are committed together as a group after evaluating the outermost body, or rolled back on any uncaught exception. If set-rollback-only is called within scope of the outermost transaction, the entire transaction will be rolled back rather than committed when complete.
Evaluates body as a transaction on the open database connection. Any nested transactions are absorbed into the outermost transaction. By default, all database updates are committed together as a group after evaluating the outermost body, or rolled back on any uncaught exception. If set-rollback-only is called within scope of the outermost transaction, the entire transaction will be rolled back rather than committed when complete.
(transaction* func)
Evaluates func as a transaction on the open database connection. Any nested transactions are absorbed into the outermost transaction. By default, all database updates are committed together as a group after evaluating the outermost body, or rolled back on any uncaught exception. If rollback is set within scope of the outermost transaction, the entire transaction will be rolled back rather than committed when complete.
Evaluates func as a transaction on the open database connection. Any nested transactions are absorbed into the outermost transaction. By default, all database updates are committed together as a group after evaluating the outermost body, or rolled back on any uncaught exception. If rollback is set within scope of the outermost transaction, the entire transaction will be rolled back rather than committed when complete.
(update-or-insert-values table where-params record)
Updates values on selected rows in a table, or inserts a new row when no existing row matches the selection criteria. where-params is a vector containing a string providing the (optionally parameterized) selection criteria followed by values for any parameters. record is a map from strings or keywords (identifying columns) to updated values.
Updates values on selected rows in a table, or inserts a new row when no existing row matches the selection criteria. where-params is a vector containing a string providing the (optionally parameterized) selection criteria followed by values for any parameters. record is a map from strings or keywords (identifying columns) to updated values.
(update-values table where-params record)
Updates values on selected rows in a table. where-params is a vector containing a string providing the (optionally parameterized) selection criteria followed by values for any parameters. record is a map from strings or keywords (identifying columns) to updated values.
Updates values on selected rows in a table. where-params is a vector containing a string providing the (optionally parameterized) selection criteria followed by values for any parameters. record is a map from strings or keywords (identifying columns) to updated values.
(with-connection db-spec & body)
Evaluates body in the context of a new connection to a database then closes the connection. db-spec is a map containing values for one of the following parameter sets:
Factory: :factory (required) a function of one argument, a map of params (others) (optional) passed to the factory function in a map
DriverManager: :subprotocol (required) a String, the jdbc subprotocol :subname (required) a String, the jdbc subname :classname (optional) a String, the jdbc driver class name (others) (optional) passed to the driver as properties.
DataSource: :datasource (required) a javax.sql.DataSource :username (optional) a String :password (optional) a String, required if :username is supplied
JNDI: :name (required) a String or javax.naming.Name :environment (optional) a java.util.Map
Raw: :connection-uri (required) a String Passed directly to DriverManager/getConnection
URI: Parsed JDBC connection string - see below
String: subprotocol://user:password@host:post/subname An optional prefix of jdbc: is allowed.
Evaluates body in the context of a new connection to a database then closes the connection. db-spec is a map containing values for one of the following parameter sets: Factory: :factory (required) a function of one argument, a map of params (others) (optional) passed to the factory function in a map DriverManager: :subprotocol (required) a String, the jdbc subprotocol :subname (required) a String, the jdbc subname :classname (optional) a String, the jdbc driver class name (others) (optional) passed to the driver as properties. DataSource: :datasource (required) a javax.sql.DataSource :username (optional) a String :password (optional) a String, required if :username is supplied JNDI: :name (required) a String or javax.naming.Name :environment (optional) a java.util.Map Raw: :connection-uri (required) a String Passed directly to DriverManager/getConnection URI: Parsed JDBC connection string - see below String: subprotocol://user:password@host:post/subname An optional prefix of jdbc: is allowed.
(with-connection* db-spec func)
Evaluates func in the context of a new connection to a database then closes the connection.
Evaluates func in the context of a new connection to a database then closes the connection.
(with-naming-strategy naming-strategy & body)
Evaluates body in the context of a naming strategy. The naming strategy is either a function - the entity naming strategy - or a map containing :entity and/or :keyword keys which provide the entity naming strategy and/or the keyword naming strategy respectively. The default entity naming strategy is identity; the default keyword naming strategy is lower-case.
Evaluates body in the context of a naming strategy. The naming strategy is either a function - the entity naming strategy - or a map containing :entity and/or :keyword keys which provide the entity naming strategy and/or the keyword naming strategy respectively. The default entity naming strategy is identity; the default keyword naming strategy is lower-case.
(with-query-results results sql-params & body)
Executes a query, then evaluates body with results bound to a seq of the results. sql-params is a vector containing either: [sql & params] - a SQL query, followed by any parameters it needs [stmt & params] - a PreparedStatement, followed by any parameters it needs (the PreparedStatement already contains the SQL query) [options sql & params] - options and a SQL query for creating a PreparedStatement, follwed by any parameters it needs See prepare-statement for supported options.
Executes a query, then evaluates body with results bound to a seq of the results. sql-params is a vector containing either: [sql & params] - a SQL query, followed by any parameters it needs [stmt & params] - a PreparedStatement, followed by any parameters it needs (the PreparedStatement already contains the SQL query) [options sql & params] - options and a SQL query for creating a PreparedStatement, follwed by any parameters it needs See prepare-statement for supported options.
(with-query-results* sql-params func)
Executes a query, then evaluates func passing in a seq of the results as an argument. The first argument is a vector containing either: [sql & params] - a SQL query, followed by any parameters it needs [stmt & params] - a PreparedStatement, followed by any parameters it needs (the PreparedStatement already contains the SQL query) [options sql & params] - options and a SQL query for creating a PreparedStatement, follwed by any parameters it needs See prepare-statement for supported options.
Executes a query, then evaluates func passing in a seq of the results as an argument. The first argument is a vector containing either: [sql & params] - a SQL query, followed by any parameters it needs [stmt & params] - a PreparedStatement, followed by any parameters it needs (the PreparedStatement already contains the SQL query) [options sql & params] - options and a SQL query for creating a PreparedStatement, follwed by any parameters it needs See prepare-statement for supported options.
(with-quoted-identifiers q & body)
Evaluates body in the context of a simple quoting naming strategy.
Evaluates body in the context of a simple quoting naming strategy.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close