API for sql
- ()
by Stephen C. Gilardi
Usage:
(ns your-namespace
(:require clojure.contrib.sql))
Overview
A Clojure interface to sql databases via jdbc
See clojure.contrib.sql.test for an example
See also:
Example code
Public Variables and Functions
connection
function
Usage: (connection)
Returns the current database connection (or throws if there is none)
Source
create-table
function
Usage: (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. All
words used to describe the table may be supplied as strings or keywords.
Source
delete-rows
function
Usage: (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.
Source
do-commands
function
Usage: (do-commands & commands)
Executes SQL commands on the open database connection.
Source
do-prepared
function
Usage: (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.
Source
drop-table
function
Usage: (drop-table name)
Drops a table on the open database connection given its name, a string
or keyword
Source
find-connection
function
Usage: (find-connection)
Returns the current database connection (or nil if there is none)
Source
insert-records
function
Usage: (insert-records table & records)
Inserts records into a table. records are maps from strings or
keywords (identifying columns) to values.
Source
insert-rows
function
Usage: (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.
Source
insert-values
function
Usage: (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.
Source
is-rollback-only
function
Usage: (is-rollback-only)
Returns true if the outermost transaction will rollback rather than
commit when complete
Source
set-rollback-only
function
Usage: (set-rollback-only)
Marks the outermost transaction such that it will rollback rather than
commit when complete
Source
transaction
macro
Usage: (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.
Source
update-or-insert-values
function
Usage: (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.
Source
update-values
function
Usage: (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.
Source
with-connection
macro
Usage: (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
Source
with-query-results
macro
Usage: (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.
Source
sql.internal
connection*
function
Usage: (connection*)
Returns the current database connection (or throws if there is none)
Source
find-connection*
function
Usage: (find-connection*)
Returns the current database connection (or nil if there is none)
Source
get-connection
function
Usage: (get-connection {:keys [factory classname subprotocol subname datasource username password name environment], :as db-spec})
Creates a connection to a database. 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
Source
print-sql-exception
function
Usage: (print-sql-exception stream exception)
Prints the contents of an SQLException to stream
Source
print-sql-exception-chain
function
Usage: (print-sql-exception-chain stream exception)
Prints a chain of SQLExceptions to stream
Source
print-update-counts
function
Usage: (print-update-counts stream exception)
Prints the update counts from a BatchUpdateException to stream
Source
rollback
function
Usage: (rollback)
(rollback val)
Accessor for the rollback flag on the current connection
Source
throw-rollback
function
Usage: (throw-rollback e)
Sets rollback and throws a wrapped exception
Source
transaction*
function
Usage: (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.
Source
with-connection*
function
Usage: (with-connection* db-spec func)
Evaluates func in the context of a new connection to a database then
closes the connection.
Source
with-query-results*
function
Usage: (with-query-results* [sql & params :as 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 the (optionally
parameterized) sql query string followed by values for any parameters.
Source
sql.test
create-fruit
function
Usage: (create-fruit)
Create a table
Source
db-batchupdate-exception
function
Usage: (db-batchupdate-exception)
Demonstrate a batch update exception
Source
db-exception
function
Usage: (db-exception)
Demonstrate rolling back a partially completed transaction on exception
Source
db-get-tables
function
Usage: (db-get-tables)
Demonstrate getting table info
Source
db-grade-a
function
Usage: (db-grade-a)
Print rows describing all grade a fruit (grade between 90 and 100)
Source
db-grade-range
function
Usage: (db-grade-range min max)
Print rows describing fruit that are within a grade range
Source
db-read
function
Usage: (db-read)
Read the entire fruit table
Source
db-read-all
function
Usage: (db-read-all)
Return all the rows of the fruit table as a vector
Source
db-rollback
function
Usage: (db-rollback)
Demonstrate a rollback-only trasaction
Source
db-sql-exception
function
Usage: (db-sql-exception)
Demonstrate an sql exception
Source
db-update
function
Usage: (db-update)
Update two fruits as a transaction
Source
db-update-appearance-cost
function
Usage: (db-update-appearance-cost name appearance cost)
Update the appearance and cost of the named fruit
Source
db-update-or-insert
function
Usage: (db-update-or-insert record)
Updates or inserts a fruit
Source
db-write
function
Usage: (db-write)
Write initial values to the database as a transaction
Source
drop-fruit
function
Usage: (drop-fruit)
Drop a table
Source
insert-records-fruit
function
Usage: (insert-records-fruit)
Insert records, maps from keys specifying columns to values
Source
insert-rows-fruit
function
Usage: (insert-rows-fruit)
Insert complete rows
Source
insert-values-fruit
function
Usage: (insert-values-fruit)
Insert rows with values for only specific columns
Source