A Clojure interface to SQL databases via JDBC
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 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).
Given a string, return it as-is. Given a keyword, return it as a string using the current naming strategy.
Given a string, return it as-is. Given a keyword, return it as a string using the current naming strategy.
Given a string, return it as a keyword using the current naming strategy. Given a keyword, return it as-is.
Given a string, return it as 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
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.
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.
(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
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 values 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 values 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
(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*
Creates and returns a lazy sequence of structmaps corresponding to the rows in the java.sql.ResultSet rs. Based on clojure.core/resultset-seq but it respects the current naming strategy.
Creates and returns a lazy sequence of structmaps corresponding to the rows in the java.sql.ResultSet rs. Based on clojure.core/resultset-seq but it respects the current naming strategy.
(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.
(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: :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: :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
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: :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: :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
(with-naming-strategy naming-strategy & body)
Evaluates body in the context of a naming strategy.
Evaluates body in the context of a naming strategy.
(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 a string providing the (optionally parameterized) SQL query followed by values for any parameters.
Executes a query, then evaluates body with results bound to a seq of the results. sql-params is a vector containing a string providing the (optionally parameterized) SQL query followed by values for any parameters.
(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