H2 数据库 - 如何访问在另一个别名中创建的函数

H2 database - How to access function created in another alias

我正在尝试在 H2 中复制我们的数据库以进行测试。我想创建 2 个函数,其中一个依赖于另一个。以下代码适用于 oracle :

GET_FULL_ADDRESS:

CREATE OR REPLACE FUNCTION "GET_FULL_ADDRESS"
(
    flat IN VARCHAR2,
    street IN VARCHAR2,
    district IN VARCHAR2,
    country_code IN VARCHAR2
)
RETURN VARCHAR2
IS
BEGIN
    RETURN flat || ', ' || street || ', ' || district || ', ' || CONVERT_COUNTRY_CODE(country_code);
END;

CONVERT_COUNTRY_CODE:

CREATE OR REPLACE FUNCTION "CONVERT_COUNTRY_CODE"
(
    country_code IN VARCHAR2
)
RETURN VARCHAR2
IS
BEGIN
    CASE country_code
        WHEN 'UK' THEN RETURN 'United Kingdom';
        WHEN 'US' THEN RETURN 'United States';
        -- Skipping the rest of the cases...
        ELSE RETURN country_code;
    END CASE;
END;

我尝试使用 ALIAS 将它们转换为 H2

GET_FULL_ADDRESS:

CREATE ALIAS GET_FULL_ADDRESS AS '
    String getFullAddress(String flat, String street, String district, String country_code) {
        return flat + ", " + street + ", " + district + ", " + convertCountryCode(country_code);
    }
';

CONVERT_COUNTRY_CODE:

CREATE ALIAS CONVERT_COUNTRY_CODE AS '
    String convertCountryCode(String country_code) {
        switch(country_code) {
            case "UK": return "United Kingdom";
            case "US": return "United States";
            // Skipping the rest of the cases...
            default: return country_code;
        }
    }
';

以上代码产生 "cannot find symbol" 错误

    convertCountryCode(country_code);
    ^
symbol:   method convertCountryCode(String)
location: class GET_FULL_ADDRESS

如果我将这 2 个函数放入单个 CREATE ALIAS 语句中,它可以工作,但在我的视图中引用了这两个函数,因此我需要单独的 CREATE ALIAS 语句。

如何调用其他CREATE ALIAS语句定义的函数?

有两种可能的方法。

  1. 您可以从 SQL 代码调用另一个别名。
CREATE ALIAS CONVERT_COUNTRY_CODE AS '
    String convertCountryCode(String country_code) {
        switch(country_code) {
            case "UK": return "United Kingdom";
            case "US": return "United States";
            // Skipping the rest of the cases...
            default: return country_code;
        }
    }
';

CREATE ALIAS GET_FULL_ADDRESS AS '
    String getFullAddress(Connection conn, String flat, String street, String district, String country_code)
            throws SQLException {
        PreparedStatement prep = conn.prepareStatement("CALL CONVERT_COUNTRY_CODE(?)");
        prep.setString(1, country_code);
        ResultSet rs = prep.executeQuery();
        rs.next();
        return flat + ", " + street + ", " + district + ", " + rs.getString(1);
    }
';
  1. 如果可以将Java 类添加到类路径(如果使用客户端-服务器模型,则为H2服务器进程的类路径),则可以将这两个函数定义为正常的Java代码,直接调用另一个方法。
package test;

public class CustomFunctions {

    public static String convertCountryCode(String country_code) {
        switch(country_code) {
            case "UK": return "United Kingdom";
            case "US": return "United States";
            // Skipping the rest of the cases...
            default: return country_code;
        }
    }

    public static String getFullAddress(String flat, String street, String district, String country_code) {
        return flat + ", " + street + ", " + district + ", " + convertCountryCode(country_code);
    }

}
CREATE ALIAS CONVERT_COUNTRY_CODE FOR "test.CustomFunctions.convertCountryCode";
CREATE ALIAS GET_FULL_ADDRESS FOR "test.CustomFunctions.getFullAddress";