Jaybird (Firebird JDBC) absolute() 方法

Jaybird (Firebird JDBC) absolute() method

absolute(int row) Java Doc 说:

Moves the cursor to the given row number in this ResultSet object. If the row number is positive, the cursor moves to the given row number with respect to the beginning of the result set. The first row is row 1, the second is row 2, and so on.

If the given row number is negative, the cursor moves to an absolute row position with respect to the end of the result set. For example, calling the method absolute(-1) positions the cursor on the last row; calling the method absolute(-2) moves the cursor to the next-to-last row, and so on.

If the row number specified is zero, the cursor is moved to before the first row.

An attempt to position the cursor beyond the first/last row in the result set leaves the cursor before the first row or after the last row.

Note: Calling absolute(1) is the same as calling first(). Calling absolute(-1) is the same as calling last().

当将 0 传递给 absolute(int row) 方法时,应该表现为 beforeFirst() 将光标定位在第一行之前。

但是使用 Jaybird 我遇到了这个异常:

Exception in thread "main" org.firebirdsql.jdbc.FBSQLException: You cannot position to the row 0 with absolute() method.
    at org.firebirdsql.jdbc.FBCachedFetcher.absolute(FBCachedFetcher.java:243)
    at org.firebirdsql.jdbc.FBCachedFetcher.absolute(FBCachedFetcher.java:232)
    at org.firebirdsql.jdbc.AbstractResultSet.absolute(AbstractResultSet.java:1371)
    at chapterA.ResultSets.main(ResultSets.java:180)

在 Jaybird 源上搜索 (FBCachedFetcher.java) 我发现当行参数为 0 时,它会抛出异常:

private boolean absolute(int row, boolean internal) throws SQLException {
        checkScrollable();

        if (row < 0) {
            row = rows.size() + row + 1;
        }

        if (row == 0 && !internal) {
            throw new SQLException("You cannot position to row 0 with absolute() method.");
        }

这样做有什么理由吗?

提前致谢!

根据 Java文档,这是 Jaybird 中的错误。我创建了 JDBC-453 来修复 Jaybird 2.2.12(和 3.0.0)中的这个问题。我做了一些考古学以找出为什么以这种方式实施。

历史上调用 absolute(0) 在 JDBC 2 / Java 1.3(及更早版本)中是不允许的。 Java 1.3.1 中 ResultSet.absolute 的 javadoc 说(强调我的):

Moves the cursor to the given row number in this ResultSet object.

If the row number is positive, the cursor moves to the given row number with respect to the beginning of the result set. The first row is row 1, the second is row 2, and so on.

If the given row number is negative, the cursor moves to an absolute row position with respect to the end of the result set. For example, calling the method absolute(-1) positions the cursor on the last row; calling the method absolute(-2) moves the cursor to the next-to-last row, and so on.

An attempt to position the cursor beyond the first/last row in the result set leaves the cursor before the first row or after the last row.

Note: Calling absolute(1) is the same as calling first(). Calling absolute(-1) is the same as calling last().

Returns:
  true if the cursor is on the result set; false otherwise
Throws:
  SQLException - if a database access error occurs, the row is 0, or the result set type is TYPE_FORWARD_ONLY

换句话说,不允许使用 0 作为参数值。然而,在同一个条目中,句子 "An attempt to position the cursor beyond the first/last row in the result set leaves the cursor before the first row or after the last row." 表明它应该被允许。

对于 JDBC 3 / Java 1.4.2(和 Java 5)这已更改为:

Throws: SQLException - if a database access error occurs, or the result set type is TYPE_FORWARD_ONLY

您突出显示的句子 ("If the row number specified is zero, the cursor is moved to before the first row.") 仅在 JDBC 4.1 (Java 7) 中添加以进一步说明。

但是查看 FBCachedFetcher 的修订历史,此限制是在 2004 年 7 月添加的,当时 Java 1.4.2 已经可用了一段时间(而 Java 5 是马上就好了)。当时我还没有加入这个项目,但我能想到的唯一原因是代码是用 JDBC 2 TCK(技术兼容性套件)测试的,因为那是最后一个公开可用的,并且这已更改为解决 TCK 报告的问题。

披露:我是 Jaybird/Firebird JDBC 驱动程序的开发人员。