使Oracle last_day函数与H2数据库兼容 [英] Make Oracle last_day function be compatible with H2 database
问题描述
我正在编写集成测试,我想使用H2数据库测试我的sql查询.在生产中,这些查询是针对Oracle数据库运行的.
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.
我必须运行此查询
我尝试使用兼容模式
SELECT last_day(MY_CURRENT_DATE) from MY_TIME
通过使用Oracle兼容模式jdbc:h2:mem:default;MODE=Oracle;DB_CLOSE_DELAY=-1
,但我收到此错误:
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.
我有一个如何在测试中解决此问题的想法.只需使用H2用户定义的函数覆盖Oracle last_day
函数:
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
}
}
$$;
但是我想知道这是否是最好的主意?
有没有一种方法可以以与所有其他数据库(包括H2)兼容的方式来重写Oracle LAST_DAY
函数(该函数计算一个月的最后一天)?
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)?
推荐答案
对我来说很明显,对于验收/系统测试,您应该使用与生产数据库非常相似的数据库.
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.
这篇关于使Oracle last_day函数与H2数据库兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!