1

I'm using the h2 v1.3.176.
I have user-defined function which execute RECURSIVE query.

public static ResultSet getChildCategories(Connection connection, long categoryId) throws SQLException {
    String sql = 
            "WITH RECURSIVE r(CATEGORY_ID, PARENT_ID) AS (\n" +
            "    SELECT   CATEGORY_ID\n" +
            "            ,PARENT_ID\n" +
            "    FROM     CATEGORY\n" +
            "    WHERE    CATEGORY_ID = " + categoryId + "\n" +
            "    UNION ALL\n" +
            "    SELECT   CATEGORY.CATEGORY_ID\n" +
            "            ,CATEGORY.PARENT_ID\n" +
            "    FROM     CATEGORY, r\n" +
            "    WHERE    CATEGORY.PARENT_ID = r.CATEGORY_ID\n" +
            ")\n" +
            "SELECT CATEGORY_ID FROM r";
    ResultSet resultSet = connection.createStatement().executeQuery(sql);
    SimpleResultSet rs = new SimpleResultSet();
    rs.addColumn("CATEGORY_ID", Types.INTEGER, 12, 0);
    try {
        while(resultSet.next()) {
            rs.addRow(resultSet.getLong(1));
        }
    } finally {
        resultSet.close();
    }
    return rs;
}

I have registered this function by following SQL.

create alias GET_CHILD_CATEGORIES for "com.myapp.db.function.Functions.getChildCategories";

My problem is the getChildCategories function will be called many times when I execute the following query.

SELECT DISTINCT  B.BOOK_ID
                ,B.SERIES_ID
                ,B.TITLE
                ,B.ISBN
                ,B.VOLUME
                ,(
                    SELECT  MAX(SAME_SERIES.VOLUME)
                    FROM    BOOK SAME_SERIES
                    WHERE   SAME_SERIES.SERIES_ID = B.SERIES_ID
                    AND     SAME_SERIES.VOLUME IS NOT NULL
                ) AS VOLUME_COUNT
                ,B.PAGE_COUNT
                ,B.FILE_PATH
                ,B.SORTABLE_FILE_NAME
                ,B.SIZE
                ,B.HASH
                ,B.COVER_IMAGE_TYPE
                ,B.COVER_PAGE_NO
                ,B.COVER_LARGE_IMAGE_URL
                ,B.COVER_SMALL_IMAGE_URL
                ,B.COVER_CROP_COORD
                ,B.IS_ENCRYPT
                ,B.PUBLISHER_ID
                ,B.PUBLISHED_DATE
                ,B.CREATION_TIME
                ,B.LAST_MODIFIED_TIME
                ,B.NOTE
                ,B.IS_ISBN_SEARCH
                ,S.CATEGORY_ID
                ,S.TITLE
                ,BA.AUTHOR_ID
                ,BT.TAG_ID
FROM             BOOK AS B
INNER JOIN       SERIES AS S ON S.SERIES_ID = B.SERIES_ID
LEFT OUTER JOIN  BOOK_TAG AS BT ON BT.BOOK_ID = B.BOOK_ID
LEFT OUTER JOIN  BOOK_AUTHOR AS BA ON BA.BOOK_ID = B.BOOK_ID
WHERE 
(
    S.CATEGORY_ID IN (SELECT CATEGORY_ID FROM GET_CHILD_CATEGORIES(106))
    And 
    S.IS_COMPLETION = 1
)
ORDER BY  BA.AUTHOR_ID

Why do many times would be called this function?

4

1 回答 1

1

摘自H2 文档

返回结果集的函数可以像表一样使用。但是,在这种情况下,该函数至少被调用两次:第一次是在解析语句以收集列名时(在编译时未知的参数设置为 null)。然后,在执行语句以获取数据时(如果这是一个连接,可能会多次)。如果调用该函数只是为了获取列列表,则传递给该函数的连接的 URL 是 jdbc:columnlist:connection。否则,连接的 URL 是 jdbc:default:connection。

第一次调用仅用于检索结果集列类型。然后你必须检查连接 url 是否为“jdbc:columnlist:connection”。如果为 true,您必须返回一个带有列列表的空结果集。

连接url测试是:

connection.getMetaData().getURL().equals("jdbc:columnlist:connection");
于 2014-10-10T07:25:28.490 回答