Skip to content

Commit 607130a

Browse files
committed
Misc. improvements to Statements
1 parent 8dc49e8 commit 607130a

1 file changed

Lines changed: 31 additions & 10 deletions

File tree

src/docs/asciidoc/chapters/statements/statements.adoc

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
After obtaining a connection, the next thing to do is to execute an SQL statement.
55
The JDBC specification distinguishes three kinds of statements:
66

7-
1. Regular statements to execute SQL statements without parameters,
8-
2. prepared statements to execute SQL statements with parameters,
9-
3. and callable statements to execute stored procedures.
7+
1. <<stmt-statement,Regular statements>> to execute SQL statements without parameters,
8+
2. <<stmt-prepared,prepared statements>> to execute SQL statements with parameters,
9+
3. and <<stmt-callable,callable statements>> to execute stored procedures.
1010

11+
[#stmt-statement]
1112
=== The java.sql.Statement interface
1213

1314
The `java.sql.Statement` interface is the simplest interface to execute SQL statements.
@@ -52,10 +53,11 @@ A statement can be executed using the following methods:
5253

5354
* `Statement.executeQuery(String)` -- executes a `SELECT` or other result set producing statement, and returns a result set.
5455
If the specified statement does not produce a result set, an `SQLException` is thrown after statement executionfootnote:[This is an implementation detail and may change in the future to throw an exception _before_ execution].
55-
* `Statement.executeUpdate(String)` -- executes other DML (Data Manipulation Language, e.g. `INSERT`, `UPDATE`, `DELETE`) or DDL (Data Definition Languagefootnote:[This term is used to group all statements that are used to manipulate database schema, i.e. creation of tables, indices, views, etc.]) statements and returns the number of updated rows.
56+
* `Statement.executeUpdate(String)` -- executes other DML (Data Manipulation Language, e.g. `INSERT`, `UPDATE`, `DELETE`) or DDL (Data Definition Languagefootnote:[This term is used to group all statements that are used to manipulate database schema, i.e. creation of tables, indices, views, etc.]) statements and returns the number of updated rows -- as `int`.
5657
If the specified statement is a query or otherwise produces a result set, an `SQLException` is thrown.
58+
* `Statement.executeLargeUpdate(String)` -- same as previous, except it returns the update count as `long`.
5759
* `Statement.execute(String)` -- executes a statement and returns `true` when the statement returned a result set, or `false` if the result has no result set, but has an update count or nothing as its result.
58-
You can use `Statement.getResultSet()` to get the result of the executed query, or you can use `Statement.getUpdateCount()` when you have executed an update statement.
60+
You can use `Statement.getResultSet()` to get the result of the executed query, or you can use `Statement.getUpdateCount()` or `getLargeUpdateCount()` when you have executed an update statement.
5961
+
6062
Formally, this method is also used for statements with multiple results (result sets and update counts), but Firebird only supports at most one result set and at most one update count.
6163

@@ -216,6 +218,7 @@ In that case, statements are not released even if the `close()` method is called
216218
Likely, the only possibility to close pooled statements is to close the pooled connections.
217219
Please check the documentation of your connection pool for more information.
218220

221+
[#stmt-prepared]
219222
=== The java.sql.PreparedStatement interface
220223

221224
As we have seen, Jaybird already performs internal optimization when it comes to multiple statement execution -- it can reuse the allocated statement handle in subsequent calls.
@@ -340,7 +343,7 @@ A JDBC driver can ignore the request to close the prepared statement, save it in
340343

341344
NOTE: Jaybird currently does not perform statement caching
342345

343-
[[callable-statement]]
346+
[#stmt-callable]
344347
=== The java.sql.CallableStatement interface
345348

346349
The `CallableStatement` interface extends `PreparedStatement` with methods for executing and retrieving results from stored procedures.
@@ -359,7 +362,7 @@ Though the interface is generic enough to support database engines that can retu
359362
These features are of no interest to Jaybird users, since Firebird does not support them.
360363

361364
The IN and OUT parameters are specified in one statement.
362-
The syntax above does not allow to specify the type of the parameter, therefore additional facilities are needed to tell the driver which parameter is will contain output values, the rest are considered to be IN parameters.
365+
The syntax above does not allow to specify the type of the parameter, therefore additional facilities are needed to tell the driver which parameter contains output values, the rest are considered to be IN parameters.
363366

364367
==== Firebird stored procedures
365368

@@ -526,6 +529,17 @@ Not that it makes much sense to do this, but it might help in some cases when po
526529
It is also allowed to use a procedure call parameter both as an input and output parameter.
527530
It is recommended to use this only when porting applications from the database servers that allow INOUT parameter types, such as Oracle.
528531

532+
[WARNING]
533+
====
534+
Future Jaybird versions may become stricter in how they handle IN and OUT parameters.
535+
They will likely require a strict ordering of parameters to match the actual procedure definition, so interleaving IN and OUT, or marking a parameter position as both IN and OUT may no longer be supported.
536+
537+
Our recommendation: always use the order of IN parameters in the procedure definition, followed by the OUT parameters in the `RETURNS` clause of the definition.
538+
539+
Mixing order of parameters will then probably require use of Firebird 6.0 and its native `CALL` statement.
540+
This is not final yet, and we may offer a backwards compatibility option, for a transition period of at least one major Jaybird version.
541+
====
542+
529543
The actual stored procedure call using the `CallableStatement` is equivalent to the call using the prepared statement as shown in the first example.
530544
There is no measurable performance differences when using the callable statement interface.
531545

@@ -550,13 +564,20 @@ Note, that input parameter now has index 2, and not 1 as in the previous example
550564
This syntax seems to be more intuitive, as it looks like a function call.
551565
It is possible to use this syntax for stored procedures that return more than one parameter by combining code from the second and the last examples.
552566

567+
[WARNING]
568+
====
569+
Future Jaybird versions may become stricter in how they handle IN and OUT parameters, and especially the `++{?=call ...}++` syntax may be restricted to only support procedures with a single OUT parameter (i.e. a single parameter in the `RETURNS` clause of its definition).
570+
571+
This is not final yet, and we may offer a backwards compatibility option, for a transition period of at least one major Jaybird version.
572+
====
573+
553574
Firebird stored procedures can also return result sets.
554-
This is achieved by using the SUSPEND keyword inside the procedure body.
575+
This is achieved by using the `SUSPEND` keyword inside the procedure body.
555576
This keyword returns the current values of the output parameters as a single row to the client.
556577

557578
The following example is more complex and shows a stored procedure that computes a set of factorial of the numbers up to the specified number of rows.
558579

559-
The SELECT SQL statement is the natural way of accessing the selectable procedures in Firebird.
580+
The `SELECT` SQL statement is the natural way of accessing the selectable procedures in Firebird.
560581
You "`select`" from such procedures using the `Statement` or `PreparedStatement` objects.
561582

562583
// TODO Simplify example below
@@ -819,7 +840,7 @@ SELECT * FROM {oj tableA a
819840
[#stmt-escape-sp]
820841
==== Stored procedures
821842

822-
The escaped syntax for stored procedures is described in details in section <<callable-statement>>.
843+
The escaped syntax for stored procedures is described in details in section <<stmt-callable>>.
823844

824845
[#stmt-escape-like-escape]
825846
==== LIKE escape character

0 commit comments

Comments
 (0)