Make Oracle last_day function be compatible with H2 database

只愿长相守 提交于 2020-01-02 13:55:42

问题


I am writing integration testing and I want to test my sql queries by using a H2 database. In production these queries are running against an Oracle database.

I have to run this query

I have tries to use the compatibility mode

SELECT last_day(MY_CURRENT_DATE) from MY_TIME

by using the oracle compatibility mode jdbc:h2:mem:default;MODE=Oracle;DB_CLOSE_DELAY=-1 but I receive this error:

org.h2.jdbc.JdbcSQLException: Function "LAST_DAY" not found; SQL statement:
SELECT last_day(MY_CURRENT_DATE) from MY_TIME [90022-176]

It just seems that the compatibility mode does not cover the vendor-specific functions.

I have an idea how to solve this in testing. Just override the Oracle last_day function with a H2 user-defined function:

DROP ALIAS IF EXISTS last_day;

CREATE ALIAS last_day AS $$
String last_day(java.sql.Connection con, Date date) throws Exception {
        //get the last day of month
  }
}
$$;

But I am wondering if it is the best idea? Is there a way to re-write the Oracle LAST_DAY function (that calculates the last day of month) in a way which is compatible with all other databases (including H2)?


回答1:


It is clear for me that for acceptance/system tests you should use a database very similar to the one in production.

For the integration and unit testing you can mock the database server component entirely.



来源:https://stackoverflow.com/questions/41377051/make-oracle-last-day-function-be-compatible-with-h2-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!