创建函数-UncategorizedScriptException-ArrayIndexOutOfBoundsException

Men*_*los 5 postgresql hibernate function spring-boot

描述

我是Spring Boot项目的新成员,该项目使用2个不同的属性文件进行与数据库设置有关的2种不同的配置:

  • 生产模式:postgres SQL DB
  • 开发模式:内存数据库中的h2

由于我试图最小化两个脚本之间的差异,因此我开始编写一些函数,这些函数将处理与日期/时间处理有关的差异。

一个示例是增加时间,因为postgres使用间隔,而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
Run Code Online (Sandbox Code Playgroud)

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: http://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;
Run Code Online (Sandbox Code Playgroud)

例外

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)
Run Code Online (Sandbox Code Playgroud)

更新资料

我开始调试,发现不是将split / parser代码创建2条语句,而是将我的脚本解释为6条SQL命令:

在此处输入图片说明

更新2

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

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

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: http://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;
Run Code Online (Sandbox Code Playgroud)

Men*_*los 2

问题是spring的SplitSqlScript: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;
Run Code Online (Sandbox Code Playgroud)