Attempt to clarify when prepared statements are created
This commit is contained in:
parent
d2b898d044
commit
79d2772e14
3 changed files with 11 additions and 4 deletions
|
|
@ -9,6 +9,7 @@ The following changes have been committed to the **master** branch since the 1.0
|
|||
* Fix #82 by adding `clojure.java.data`-based support for setting arbitrary properties on `Connection` and `PreparedStatement` objects, post-creation. Note: this uses the Java reflection API under the hood.
|
||||
* Adds `next.jdbc.prepare/statement` to create `Statement` objects with all the options available to `prepare` except `:return-keys`.
|
||||
* Update `org.clojure/java.data` to 0.1.5 (for property setting).
|
||||
* Additional clarifications in the documentation based on feedback on Slack.
|
||||
|
||||
## Stable Builds
|
||||
|
||||
|
|
|
|||
|
|
@ -30,9 +30,11 @@ Any path that calls `get-connection` will accept the following options:
|
|||
|
||||
If you need additional options set on a connection, you can either use Java interop to set them directly, or provide them as part of the "db spec" hash map passed to `get-datasource` (although then they will apply to _all_ connections obtained from that datasource).
|
||||
|
||||
> Note: If `plan`, `execute!`, or `execute-one!` are passed a `DataSource`, a "db spec" hash map, or a JDBC URI string, they will call `get-connection`, so they will accept the above options in those cases.
|
||||
|
||||
## Generating SQL
|
||||
|
||||
The "friendly" SQL functions all accept the following options:
|
||||
The "friendly" SQL functions all accept the following options (in addition to all the options that `plan`, `execute!`, and `execute-one!` can accept):
|
||||
|
||||
* `:table-fn` -- the quoting function to be used on the string that identifies the table name, if provided,
|
||||
* `:column-fn` -- the quoting function to be used on any string that identifies a column name, if provided.
|
||||
|
|
@ -45,6 +47,8 @@ Any function that might realize a row or a result set will accept:
|
|||
* `:label-fn` -- if `:builder-fn` is specified as one of `next.jdbc.result-set`'s `as-modified-*` builders, this option must be present and should specify a string-to-string transformation that will be applied to the column label for each returned column name.
|
||||
* `:qualifier-fn` -- if `:builder-fn` is specified as one of `next.jdbc.result-set`'s `as-modified-*` builders, this option should specify a string-to-string transformation that will be applied to the table name for each returned column name for which the table name is known. It can be omitted for the `as-unqualified-modified-*` variants.
|
||||
|
||||
> Note: Subject to the caveats above about `:builder-fn`, that means that `plan`, `execute!`, `execute-one!`, and the "friendly" SQL functions will all accept these options for generating rows and result sets.
|
||||
|
||||
## Statements & Prepared Statements
|
||||
|
||||
Any function that creates a `Statement` or a `PreparedStatement` will accept the following options (see below for additional options for `PreparedStatement`):
|
||||
|
|
@ -67,7 +71,9 @@ Any function that creates a `PreparedStatement` will additionally accept the fol
|
|||
|
||||
Not all databases or drivers support all of these options, or all values for any given option. If `:return-keys` is a vector of column names and that is not supported, `next.jdbc` will attempt a generic "return generated keys" option instead. If that is not supported, `next.jdbc` will fall back to a regular SQL operation. If other options are not supported, you may get a `SQLException`.
|
||||
|
||||
In addition, `next.jdbc.prepare/execute-batch!` accepts an options hash map that can contain the following:
|
||||
> Note: If `plan`, `execute!`, or `execute-one!` are passed a `DataSource`, a "db spec" hash map, or a JDBC URI string, they will call `prepare` to create a `PreparedStatement`, so they will accept the above options in those cases.
|
||||
|
||||
In addition the the above, `next.jdbc.prepare/execute-batch!` accepts an options hash map that can also contain the following:
|
||||
|
||||
* `:batch-size` -- an integer that determines how to partition the parameter groups for submitting to the database in batches,
|
||||
* `:large` -- a Boolean flag that indicates whether the batch will produce large update counts (`long` rather than `int` values).
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
# Prepared Statements
|
||||
|
||||
Under the hood, whenever you ask `next.jdbc` to execute some SQL it creates a `java.sql.PreparedStatement`, adds in the parameters you provide, and then calls `.execute` on it. Then it attempts to get a `ResultSet` from that and either return it or process it. If you asked for generated keys to be returned, that `ResultSet` will contain those generated keys if your database supports it, otherwise it will be whatever the `.execute` function produces. If no `ResultSet` is available at all, `next.jdbc` will ask for the count of updated rows and return that as if it were a result set.
|
||||
Under the hood, whenever you ask `next.jdbc` to execute some SQL (via `plan`, `execute!`, `execute-one!` or the "friendly" SQL functions) it calls `prepare` to create a `java.sql.PreparedStatement`, adds in the parameters you provide, and then calls `.execute` on it. Then it attempts to get a `ResultSet` from that and either return it or process it. If you asked for generated keys to be returned, that `ResultSet` will contain those generated keys if your database supports it, otherwise it will be whatever the `.execute` function produces. If no `ResultSet` is available at all, `next.jdbc` will ask for the count of updated rows and return that as if it were a result set.
|
||||
|
||||
> Note: Some databases do not support all SQL operations via `PreparedStatement`, in which case you may need to create a `java.sql.Statement` instead and pass that into `plan`, `execute!`, or `execute-one!`, along with the SQL you wish to execute. Note that such statement execution may not have parameters. See the [Prepared Statement Caveat in Getting Started](/doc/getting-started.md#prepared-statement-caveat) for an example.
|
||||
> Note: Some databases do not support all SQL operations via `PreparedStatement`, in which case you may need to create a `java.sql.Statement` instead, via `next.jdbc.prepare/statement`, and pass that into `plan`, `execute!`, or `execute-one!`, along with the SQL you wish to execute. Note that such statement execution may not have parameters. See the [Prepared Statement Caveat in Getting Started](/doc/getting-started.md#prepared-statement-caveat) for an example.
|
||||
|
||||
If you have a SQL operation that you intend to run multiple times on the same `java.sql.Connection`, it may be worth creating the prepared statement yourself and reusing it. `next.jdbc/prepare` accepts a connection and a vector of SQL and optional parameters and returns a `java.sql.PreparedStatement` which can be passed to `plan`, `execute!`, or `execute-one!` as the first argument. It is your responsibility to close the prepared statement after it has been used.
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue