How to use getDate() in h2 as like sql server - h2

I'm testing the already developed application through Junit test cases, uses SQL server in production but while executing test cases we used embedded h2 database.
And some of the SQL server specifications are not working in h2 (ex: conditional queries, getDate(), DATEPART,..........) can anyone help me how to tackle this kind issues further in my JUnit testing with h2 DB
SQL SERVER: select * from getDate() -: 2006-12-30 00:38:54.840
h2 DB : select * from getDate() -: 2006-12-30
While executing test cases that scripts are executing in h2 database and as a part of logic, I'm parsing the value from getdate() into SimpleDate format of 2006-12-30 00:38:54.840, and I'm getting parse exception that 2006-12-30 couldn't parse.
Thanks in Advance

To use getDate in H2, simply run the following query before using a query with getDate()
drop alias if exists getDate;
create alias getDate as '
java.util.Date getDate() {
return new java.util.Date();
}
';

You can use User-Defined Functions and Stored Procedures built in H2 database.

Related

rawToHex(sys_guid()) usage in h2 in memory with oracle mode

I'm trying to write some tests for my java application and my entity persistence uses oracle's sys_guid() for primary key.
Everything works fine when connected to oracle, but when I use H2 in memory for my testing, the following statement used to generate the primary guid value as a char(32) returns a much bigger string (144).
select rawtohex(sys_guid()) from dual
Is there an extra configuration to correctly setup oracle mode or is this somehow a bug
I'm using springboot with spring-data and hibernate to handle database comunication, here is a sample method that generates the guid.
#Repository
public interface TokenRecuperacaoSenhaRepository extends ExtendedSpringRepo<TokenRecuperacaoSenha, String>
{
#Query(value = "select rawtohex(sys_guid()) from dual", nativeQuery = true)
public String genereateToken();
}
When connected to Oracle, returns "8E0FFC48082AB39FE0539BD3E10AC537"
While with H2, returns "00660032003300620037003000360065002d0031006400630066002d0034003700620030002d0039003400360066002d006100620065003400320034006400360030006300360038"
I'm guessing there is some conversion issue with the functions used.
Both SYS_GUID() and RAWTOHEX() functions are non-standard and aren't fully compatible between Oracle and H2.
This incompatibility was fixed in Oracle compatibility mode of H2, so if you can build H2 from its current sources you will be able to use these functions in this compatibility mode.
You can get the sources from the GitHub:
https://github.com/h2database/h2database
Building instructions are here:
https://h2database.com/html/build.html#building
You need the jar target.
Don't forget to set the compatibility mode by appending ;MODE=Oracle to your connection URL or by using SET MODE Oracle; command.
If you cannot use an own build of H2 you need an alternative method for H2 that will use something like SELECT CAST(CAST(UUID() AS BINARY) AS VARCHAR)

Make Oracle last_day function be compatible with H2 database

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)?
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.

Spring embedded DB schema not found issue

I am using embedded HSQLDB in my JUnits. It is throwing
SQL state [3F000]; error code [-4850]; invalid schema name:info
when it executes query following query
select db.id, db.name,info.desc from user_db db,user_info info where db.user_id = info.user_id and info.active = 'Y' and db.dept = info.dept(+)
the above query runs fine in oracle but not in embedded HSQLDB
please let me know what is wrong in this.

Not able to access the tables from H2 database using Java

I created a script for Database Testing using H2 database. I am facing issue, not able to read the tables in database. It throwing the message "Table not Found" and below code. But it is able to connect the database.
Configuration : h2-3.3.jar and h2.jar for Database Engine
Class.forName("org.h2.Driver");
Connection con=DriverManager.getConnection("jdbc:h2:file:C:\\keymanager\\etc\\H2/kms;CIPHER=AES","km_user","87654321 12345678");
System.out.println(con.getCatalog());
Statement statement = con.createStatement();
ResultSet resultSet1 = con.createStatement().executeQuery("SELECT * FROM KM_AUDITLOGS");
while(resultSet1.next()){
System.out.println("CREATEDATE:" +resultSet1.getString("USERIP"));
}
After executing the Script error message has been displayed
"org.h2.jdbc.JdbcSQLException: Table KM_AUDITLOGS not found; SQL statement:
SELECT * FROM KM_AUDITLOGS [42102-73]"
Any one can help me?
When using the latest version of H2, it is working (according to the last comment).

How do I use the current date in an HQL query with an Oracle database?

I'm trying to write this query using Hibernate 3 and Oracle 10.
from Alert alert
where alert.expiration > current_date()
order by alert.priority, alert.updated, alert.name
It's creating SQL like this -
Hibernate: select alert0_.ANNOUNCEMENTS_ID as ANNOUNCE1_1_, alert0_.ANNOUNCEMENT
S_NAME as ANNOUNCE2_1_, alert0_.ANNOUNCEMENTS_PRIORITY as ANNOUNCE3_1_, alert0_.
ANNOUNCEMENTS_EXPIRATION as ANNOUNCE4_1_, alert0_.ANNOUNCEMENTS_UPDATE_DATE as A
NNOUNCE5_1_ from NYC311_ANNOUNCEMENTS alert0_ where (alert0_.ANNOUNCEMENTS_EXPIR
ATION>current_date()) order by alert0_.ANNOUNCEMENTS_PRIORITY , alert0_.ANNOUNC
EMENTS_UPDATE_DATE , alert0_.ANNOUNCEMENTS_NAME
I'm getting all of these wacky errors like "missing right parenthesis" when there is apparently perfectly balanced parenthesis.
Why is Oracle freaking out at this? Is there a better way to write my HQL query?
Shouldn't it be current_date?
Hibernate will translate it to the proper dialect.
I did not find a real "Hibernate will translate this to that" reference documentation, but the expression, in general, can be found in HQL Expressions for Hibernate 4.3.
Then there is the Java Persistence API 2.0 (JPA) specification which defines expressions for the Java Persistence query language (JPQL) and their meaning e.g. for current_date:
4.6.17.2.3 Datetime Functions functions_returning_datetime:= CURRENT_DATE | CURRENT_TIME | CURRENT_TIMESTAMP The datetime functions
return the value of current date, time, and timestamp on the database
server.
Is current_date() a Hibernate function?
I would use sysdate instead. like this:
where alert.expiration > sysdate
Or to ignore time of day:
where alert.expiration > trunc(sysdate)

Resources