H2用户定义函数被多次调用



我使用的是h2 v1.3176。
我有执行RECURSIVE查询的用户定义函数。

public static ResultSet getChildCategories(Connection connection, long categoryId) throws SQLException {
    String sql = 
            "WITH RECURSIVE r(CATEGORY_ID, PARENT_ID) AS (n" +
            "    SELECT   CATEGORY_IDn" +
            "            ,PARENT_IDn" +
            "    FROM     CATEGORYn" +
            "    WHERE    CATEGORY_ID = " + categoryId + "n" +
            "    UNION ALLn" +
            "    SELECT   CATEGORY.CATEGORY_IDn" +
            "            ,CATEGORY.PARENT_IDn" +
            "    FROM     CATEGORY, rn" +
            "    WHERE    CATEGORY.PARENT_ID = r.CATEGORY_IDn" +
            ")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;
}

我已经通过以下SQL注册了此函数。

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

我的问题是,当我执行以下查询时,getChildCategories函数将被调用多次。

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

为什么会多次调用这个函数?

从H2文档中提取

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

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

连接url测试为:

connection.getMetaData().getURL().equals("jdbc:columnlist:connection");

相关内容

  • 没有找到相关文章

最新更新