hibernate – 创建函数 – UncategorizedScriptException – ArrayIndexOutOfBoundsException

描述

我是Spring Boot Project的新成员,它使用2个不同的属性文件,用于与DB设置相关的2种不同配置:

>生产模式:postgres SQL DB
>开发模式:内存DB中的h2

由于我试图最小化两个脚本中的差异,我已经开始编写函数,这些函数将考虑与日期/时间处理相关的差异.

一个例子是添加小时,因为postgres使用interval并且h2使用oracle类似的date_add函数.

不幸的是我得到了在控制台中工作的函数创建语句的异常.

现有文件

配置/性能

spring.profiles.active=pre-prod

spring.datasource.url=jdbc:postgresql://localhost:5432/db
spring.datasource.username=postgres
spring.datasource.password=root
spring.datasource.driver-class-name=org.postgresql.Driver

spring.datasource.data=classpath:db/migration/postgres/db_functions.sql,classpath:db/migration/postgres/data.sql
spring.jpa.hibernate.ddl-auto=create

db_functions.sql

--Adds a cast to date to the specific statement
--for h2 simply make a wrapper
CREATE OR REPLACE FUNCTION db_pgres_cast_varchar_to_date(d VARCHAR ) RETURNS date AS $$
        BEGIN
                RETURN d::date;
        END;
$$LANGUAGE plpgsql;

--Common function for h2 and vct to add hours
--References: https://stackoverflow.com/questions/9376350/postgresql-how-to-concat-interval-value-2-days
CREATE OR REPLACE FUNCTION db_add_hours(d timestamp, hours int) RETURNS timestamp AS $$
        BEGIN
                RETURN d +  (hours || ' hours')::interval;
        END;
$$LANGUAGE plpgsql;

例外

NFO] org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor Shutting down ExecutorService 'createTaskExecutor'
Exception in thread "main" org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script from resource [class path resource [db/migration/postgres/vct_functions.sql]]; nested exception is java.lang.ArrayIndexOutOfBoundsException
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:509)

更新

我开始调试并看到,而不是创建2个语句的split / parser代码,我的脚本被解释为6个SQL命令:

《hibernate – 创建函数 – UncategorizedScriptException – ArrayIndexOutOfBoundsException》

更新2

问题似乎与splitSqlScript的实现有关,定义:

public static void splitSqlScript(EncodedResource resource, String script, String separator, String commentPrefix,
            String blockCommentStartDelimiter, String blockCommentEndDelimiter, List<String> statements)
            throws ScriptException

https://www.codatlas.com/github.com/spring-projects/spring-framework/HEAD/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java?line=166

更新3

由于Spring的Spring Utils似乎是问题,我正在尝试使用不同的语法来创建我的函数:

--Adds a cast to date to the specific statement
--for h2 simply a wrapper
CREATE OR REPLACE FUNCTION vct_pgres_cast_varchar_to_date(VARCHAR ) RETURNS date
    AS 'select $1::date;'
    LANGUAGE SQL
    RETURNS NULL ON NULL INPUT;


--Common function for h2 and vct to add hours
--References: https://stackoverflow.com/questions/9376350/postgresql-how-to-concat-interval-value-2-days

CREATE OR REPLACE FUNCTION vct_add_hours(timestamp, integer) RETURNS timestamp
    AS 'select $1 +  ($2 || '' hours'')::interval'
    LANGUAGE SQL
    RETURNS NULL ON NULL INPUT;

最佳答案 问题是Spring的SpringSqlScript:
https://www.codatlas.com/github.com/spring-projects/spring-framework/HEAD/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java?line=166

我通过更改create function语句的语法来避免$$并将SQL语句括在引号内来解决这个问题.

例如.

CREATE OR REPLACE FUNCTION vct_pgres_cast_varchar_to_date(VARCHAR ) RETURNS date
    AS 'select $1::date;'
    LANGUAGE SQL
    RETURNS NULL ON NULL INPUT;
点赞