bind-parameter provides a way to bind parameters differently based upon their tag. This allows type-specific setters to be used, such as .setString, instead of the generic .setObject. When no other binding type is available it uses .setObject.
bind-parameter provides a way to bind parameters differently based upon their tag. This allows type-specific setters to be used, such as .setString, instead of the generic .setObject. When no other binding type is available it uses .setObject.
(column-labels rs)
Extracts the column labels from a result set to be used as keys in row maps. This function pairs with the row-to-map.
Extracts the column labels from a result set to be used as keys in row maps. This function pairs with the row-to-map.
(definsert name & decl)
Like defquery, but uses .executeUpdate to execute the statement. The PreparedStatement is created with RETURN_GENERATED_KEYS, and the return value is retrieved from the PreparedStatement's .getGeneratedKeys result set.
Like defquery, but uses .executeUpdate to execute the statement. The PreparedStatement is created with RETURN_GENERATED_KEYS, and the return value is retrieved from the PreparedStatement's .getGeneratedKeys result set.
(definsert- name & decls)
Same as definsert, yielding non-public def
Same as definsert, yielding non-public def
(defquery name & decl)
Creates a function that performs a SQL query via JDBC. The name' parameter is the name of the resulting function. The
params' is an
argument list for the function, with the JDBC connection parameter
automatically inserted at the beginning. The `sql' parameter is a
SQL string that will be used to create the prepared statement.
Occurances of keywords in the SQL string are replaced by
PreparedStatement binds to the associated parameter in the parameter
list. For example:
(defquery select-users-by-country [country] "SELECT * FROM user WHERE user_country = :country")
is expanded to the equivalent to the following:
(defn select-users-by-country [conn country] (with-open [ps (. conn (prepareStatement "SELECT * FROM user WHERE user_country = ?")) rs (. (doto ps (.setObject 1 country)) (executeQuery))] (read-rows rs)))
Tagging parameters will cause expansion to use more specific PreparedStatement binding methods. If the above example had tagged [^String country], then the expansion would use (.setString 1 country).
For primitives, tagging with the box class (e.g., [^Long id]) will allow nulls, whereas tagging with the primitive (e.g., [^long id]) will throw an exception on null. The primitive (non-nullable) version is marginally faster, and can allow early detection of errors when nulls are not allowed.
Adding ^:json to a parameter will cause defquery to serialized the parameter to a JSON string, then use .setString to bind it.
Adding ^{:array "VARCHAR"} to a parameter will cause defquery to convert the argument (via object-array) to a SQL Array (of type 'VARCHAR') and bind it using (.setArray).
Queries may also contain references to dynamic variables, provided the dynamic variable is named according to the varname convention. Both fully-qualified (my-ns/varname) and unqualified (varname) dynamic variables are supported. However due to a limitation, binding based upon metadata is currently only supported for fully-qualified dynamic variables. Thus, with
(def ^:dynamic ^java.sql.Timestamp now)
"SELECT ... now" will bind with (.setObject now)
"SELECT ... my-ns/now" will bind with (.setTimestamp my-ns/now)
Use the optional options map to change standard behaviors.
The {:row rowfn} option, where `rowfn' is a function that takes a ResultSet can be used to specify a row reader function (useful in combination with defrow).
The {:rows :first} option returns the first row from the ResultSet (may be used in combination with {:row rowfn}) instead of the usual vector of rows. If there are no results, nil is returned.
When {:rows :single} is present, the ResultSet is expected to have exactly 1 result. An exception will be thrown if 0 or 2+ results are returned. Otherwise the single row is returned.
Creates a function that performs a SQL query via JDBC. The `name' parameter is the name of the resulting function. The `params' is an argument list for the function, with the JDBC connection parameter automatically inserted at the beginning. The `sql' parameter is a SQL string that will be used to create the prepared statement. Occurances of keywords in the SQL string are replaced by PreparedStatement binds to the associated parameter in the parameter list. For example: (defquery select-users-by-country [country] "SELECT * FROM user WHERE user_country = :country") is expanded to the equivalent to the following: (defn select-users-by-country [conn country] (with-open [ps (. conn (prepareStatement "SELECT * FROM user WHERE user_country = ?")) rs (. (doto ps (.setObject 1 country)) (executeQuery))] (read-rows rs))) Tagging parameters will cause expansion to use more specific PreparedStatement binding methods. If the above example had tagged [^String country], then the expansion would use (.setString 1 country). For primitives, tagging with the box class (e.g., [^Long id]) will allow nulls, whereas tagging with the primitive (e.g., [^long id]) will throw an exception on null. The primitive (non-nullable) version is marginally faster, and can allow early detection of errors when nulls are not allowed. Adding ^:json to a parameter will cause defquery to serialized the parameter to a JSON string, then use .setString to bind it. Adding ^{:array "VARCHAR"} to a parameter will cause defquery to convert the argument (via object-array) to a SQL Array (of type 'VARCHAR') and bind it using (.setArray). Queries may also contain references to dynamic variables, provided the dynamic variable is named according to the *varname* convention. Both fully-qualified (my-ns/*varname*) and unqualified (*varname*) dynamic variables are supported. However due to a limitation, binding based upon metadata is currently only supported for fully-qualified dynamic variables. Thus, with (def ^:dynamic ^java.sql.Timestamp *now*) "SELECT ... *now*" will bind with (.setObject *now*) "SELECT ... my-ns/*now*" will bind with (.setTimestamp my-ns/*now*) Use the optional options map to change standard behaviors. The {:row rowfn} option, where `rowfn' is a function that takes a ResultSet can be used to specify a row reader function (useful in combination with defrow). The {:rows :first} option returns the first row from the ResultSet (may be used in combination with {:row rowfn}) instead of the usual vector of rows. If there are no results, nil is returned. When {:rows :single} is present, the ResultSet is expected to have exactly 1 result. An exception will be thrown if 0 or 2+ results are returned. Otherwise the single row is returned.
(defquery- name & decls)
Same as defquery, yielding non-public def
Same as defquery, yielding non-public def
(defrow nom fields & opts+specs)
Creates a defrecord and a ResultSet to record function with the name rs->RecordName. The fields may be declared with tags and deserialization information to define how the ResultSet will be converted to a row. For example,
(defrow MyUser [^Long user-id ^keyword state ^:json data])
Results in a MyUser defrecord, and a function similar to the following:
(defn rs->MyUser [^ResultSet rs] (new MyUser (.getLong rs 1) (keyword (.getString rs 2)) (cheshire.core/parse-string (.getString rs 3) 3)))
(Except that SQL NULLs will result in nil instead of NullPointerExceptions like would happen in this example.)
Creates a defrecord and a ResultSet to record function with the name rs->RecordName. The fields may be declared with tags and deserialization information to define how the ResultSet will be converted to a row. For example, (defrow MyUser [^Long user-id ^keyword state ^:json data]) Results in a MyUser defrecord, and a function similar to the following: (defn rs->MyUser [^ResultSet rs] (new MyUser (.getLong rs 1) (keyword (.getString rs 2)) (cheshire.core/parse-string (.getString rs 3) 3))) (Except that SQL NULLs will result in nil instead of NullPointerExceptions like would happen in this example.)
(defupdate name & decl)
Like defquery, but uses .executeUpdate to execute the statement. The return value is the result of the executeUpdate (that is, an integer). Does not support :row and :rows options.
Like defquery, but uses .executeUpdate to execute the statement. The return value is the result of the executeUpdate (that is, an integer). Does not support :row and :rows options.
(defupdate- name & decls)
Same as defupdate, yielding non-public def
Same as defupdate, yielding non-public def
(prepare-and-execute params bindorder bindsql pmeta c opts executor)
This method recursively iterates through the params list, setting up each parameter as needed. Once params is empty, this method then prepares and executes the SQL using the provided executor fn.
Parameters that require special processing before sending to JDBC (e.g., Array and JSON), are bound using (let) blocks. This allows for the same parameter to be used multiple times in the same query without multiple setups. It also, as in the case of Arrays, allows for exceptions to be thrown before the PreparedStatement is created.
This method recursively iterates through the params list, setting up each parameter as needed. Once params is empty, this method then prepares and executes the SQL using the provided executor fn. Parameters that require special processing before sending to JDBC (e.g., Array and JSON), are bound using (let) blocks. This allows for the same parameter to be used multiple times in the same query without multiple setups. It also, as in the case of Arrays, allows for exceptions to be thrown before the PreparedStatement is created.
(read-rows rs)
(read-rows rs rowfn)
The default defquery row reader. This function iterates through all rows in the result set and returns a vector with one entry per row. The `rowfn' parameter, if present is used to convert a row to a record in the resulting vector. The single argument version uses the metadata from the result set to create a map for each entry.
The default defquery row reader. This function iterates through all rows in the result set and returns a vector with one entry per row. The `rowfn' parameter, if present is used to convert a row to a record in the resulting vector. The single argument version uses the metadata from the result set to create a map for each entry.
(row-to-map labels rs)
Converts a row to a map, using the labels vector for keys.
Converts a row to a map, using the labels vector for keys.
(set-nullable-float* ps idx f)
Helper for setting a nullable float value
Helper for setting a nullable float value
(set-nullable-integer* ps idx i)
Helper for setting a nullable integer value
Helper for setting a nullable integer value
(set-nullable-long* ps idx l)
Helper for setting a nullable long value
Helper for setting a nullable long value
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close