Fixes HHH-10800 - InformixDialect: add support for current_timestamp() and current_date()
This commit is contained in:
parent
e814eb930b
commit
5e82c2e414
|
@ -10,6 +10,7 @@ import java.sql.SQLException;
|
||||||
import java.sql.Types;
|
import java.sql.Types;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import org.hibernate.dialect.function.NoArgSQLFunction;
|
||||||
import org.hibernate.dialect.function.NvlFunction;
|
import org.hibernate.dialect.function.NvlFunction;
|
||||||
import org.hibernate.dialect.function.SQLFunctionTemplate;
|
import org.hibernate.dialect.function.SQLFunctionTemplate;
|
||||||
import org.hibernate.dialect.function.VarArgsSQLFunction;
|
import org.hibernate.dialect.function.VarArgsSQLFunction;
|
||||||
|
@ -78,6 +79,8 @@ public class InformixDialect extends Dialect {
|
||||||
registerFunction( "substr", new SQLFunctionTemplate( StandardBasicTypes.STRING, "substr(?1, ?2, ?3)"));
|
registerFunction( "substr", new SQLFunctionTemplate( StandardBasicTypes.STRING, "substr(?1, ?2, ?3)"));
|
||||||
registerFunction( "coalesce", new NvlFunction());
|
registerFunction( "coalesce", new NvlFunction());
|
||||||
registerFunction( "nvl", new NvlFunction());
|
registerFunction( "nvl", new NvlFunction());
|
||||||
|
registerFunction( "current_timestamp", new NoArgSQLFunction( "current", StandardBasicTypes.TIMESTAMP, false ) );
|
||||||
|
registerFunction( "current_date", new NoArgSQLFunction( "today", StandardBasicTypes.DATE, false ) );
|
||||||
|
|
||||||
uniqueDelegate = new InformixUniqueDelegate( this );
|
uniqueDelegate = new InformixUniqueDelegate( this );
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,16 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.dialect;
|
package org.hibernate.dialect;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.hibernate.dialect.function.SQLFunction;
|
||||||
|
import org.hibernate.engine.spi.Mapping;
|
||||||
|
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||||
|
import org.hibernate.type.StandardBasicTypes;
|
||||||
|
import org.hibernate.type.Type;
|
||||||
|
|
||||||
import org.hibernate.testing.TestForIssue;
|
import org.hibernate.testing.TestForIssue;
|
||||||
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -13,22 +23,55 @@ import org.junit.Test;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Testing of patched support for Informix boolean type; see HHH-9894
|
* Testing of patched support for Informix boolean type; see HHH-9894, HHH-10800
|
||||||
*
|
*
|
||||||
* @author Greg Jones
|
* @author Greg Jones
|
||||||
*/
|
*/
|
||||||
@TestForIssue( jiraKey = "HHH-9894" )
|
|
||||||
public class InformixDialectTestCase extends BaseUnitTestCase {
|
public class InformixDialectTestCase extends BaseUnitTestCase {
|
||||||
|
|
||||||
private final InformixDialect dialect = new InformixDialect();
|
private final InformixDialect dialect = new InformixDialect();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "HHH-9894")
|
||||||
public void testToBooleanValueStringTrue() {
|
public void testToBooleanValueStringTrue() {
|
||||||
assertEquals( "'t'", dialect.toBooleanValueString( true ) );
|
assertEquals( "'t'", dialect.toBooleanValueString( true ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "HHH-9894")
|
||||||
public void testToBooleanValueStringFalse() {
|
public void testToBooleanValueStringFalse() {
|
||||||
assertEquals( "'f'", dialect.toBooleanValueString( false ) );
|
assertEquals( "'f'", dialect.toBooleanValueString( false ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "HHH-10800")
|
||||||
|
public void testCurrentTimestampFunction() {
|
||||||
|
Map<String, SQLFunction> functions = dialect.getFunctions();
|
||||||
|
SQLFunction sqlFunction = functions.get( "current_timestamp" );
|
||||||
|
|
||||||
|
Type firstArgumentType = null;
|
||||||
|
Mapping mapping = null;
|
||||||
|
assertEquals( StandardBasicTypes.TIMESTAMP, sqlFunction.getReturnType( firstArgumentType, mapping ) );
|
||||||
|
|
||||||
|
firstArgumentType = null;
|
||||||
|
List arguments = Collections.emptyList();
|
||||||
|
SessionFactoryImplementor factory = null;
|
||||||
|
assertEquals( "current", sqlFunction.render( firstArgumentType, arguments, factory ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "HHH-10800")
|
||||||
|
public void testCurrentDateFunction() {
|
||||||
|
Map<String, SQLFunction> functions = dialect.getFunctions();
|
||||||
|
SQLFunction sqlFunction = functions.get( "current_date" );
|
||||||
|
|
||||||
|
Type firstArgumentType = null;
|
||||||
|
Mapping mapping = null;
|
||||||
|
assertEquals( StandardBasicTypes.DATE, sqlFunction.getReturnType( firstArgumentType, mapping ) );
|
||||||
|
|
||||||
|
firstArgumentType = null;
|
||||||
|
List arguments = Collections.emptyList();
|
||||||
|
SessionFactoryImplementor factory = null;
|
||||||
|
assertEquals( "today", sqlFunction.render( firstArgumentType, arguments, factory ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
package org.hibernate.engine.query;
|
package org.hibernate.engine.query;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
|
||||||
import org.hibernate.Session;
|
|
||||||
import org.hibernate.Transaction;
|
|
||||||
import org.hibernate.dialect.InformixDialect;
|
import org.hibernate.dialect.InformixDialect;
|
||||||
|
|
||||||
import org.hibernate.testing.RequiresDialect;
|
import org.hibernate.testing.RequiresDialect;
|
||||||
|
@ -14,7 +15,10 @@ import org.hibernate.testing.TestForIssue;
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Vlad Mihalcea
|
* @author Vlad Mihalcea
|
||||||
|
@ -26,26 +30,18 @@ public class InformixFunctionTest extends BaseCoreFunctionalTestCase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void prepareTest() throws Exception {
|
protected void prepareTest() throws Exception {
|
||||||
Session s = openSession();
|
doInHibernate( this::sessionFactory, session -> {
|
||||||
try {
|
|
||||||
Transaction transaction = s.beginTransaction();
|
|
||||||
event = new Event();
|
event = new Event();
|
||||||
event.country = "Romania";
|
event.country = "Romania";
|
||||||
event.city = "Cluj-Napoca";
|
event.city = "Cluj-Napoca";
|
||||||
s.persist( event );
|
session.persist( event );
|
||||||
transaction.commit();
|
} );
|
||||||
}
|
|
||||||
finally {
|
|
||||||
s.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestForIssue( jiraKey = "HHH-10846" )
|
@TestForIssue( jiraKey = "HHH-10846" )
|
||||||
public void testConcat() throws Exception {
|
public void testConcat() throws Exception {
|
||||||
Session s = openSession();
|
doInHibernate( this::sessionFactory, session -> {
|
||||||
try {
|
|
||||||
s.beginTransaction();
|
|
||||||
String location = (String) session.createQuery(
|
String location = (String) session.createQuery(
|
||||||
"select concat(e.country, ' - ', e.city) " +
|
"select concat(e.country, ' - ', e.city) " +
|
||||||
"from Event e " +
|
"from Event e " +
|
||||||
|
@ -53,20 +49,13 @@ public class InformixFunctionTest extends BaseCoreFunctionalTestCase {
|
||||||
.setParameter( "id", event.id )
|
.setParameter( "id", event.id )
|
||||||
.getSingleResult();
|
.getSingleResult();
|
||||||
assertEquals( "Romania - Cluj-Napoca", location);
|
assertEquals( "Romania - Cluj-Napoca", location);
|
||||||
|
} );
|
||||||
s.getTransaction().commit();
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
s.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestForIssue( jiraKey = "HHH-10846" )
|
@TestForIssue( jiraKey = "HHH-10846" )
|
||||||
public void testSubstring() throws Exception {
|
public void testSubstring() throws Exception {
|
||||||
Session s = openSession();
|
doInHibernate( this::sessionFactory, session -> {
|
||||||
try {
|
|
||||||
s.beginTransaction();
|
|
||||||
String location = (String) session.createQuery(
|
String location = (String) session.createQuery(
|
||||||
"select substring(e.city, 0, 5) " +
|
"select substring(e.city, 0, 5) " +
|
||||||
"from Event e " +
|
"from Event e " +
|
||||||
|
@ -74,20 +63,13 @@ public class InformixFunctionTest extends BaseCoreFunctionalTestCase {
|
||||||
.setParameter( "id", event.id )
|
.setParameter( "id", event.id )
|
||||||
.getSingleResult();
|
.getSingleResult();
|
||||||
assertEquals( "Cluj", location);
|
assertEquals( "Cluj", location);
|
||||||
|
} );
|
||||||
s.getTransaction().commit();
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
s.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestForIssue( jiraKey = "HHH-10846" )
|
@TestForIssue( jiraKey = "HHH-10846" )
|
||||||
public void testSubstr() throws Exception {
|
public void testSubstr() throws Exception {
|
||||||
Session s = openSession();
|
doInHibernate( this::sessionFactory, session -> {
|
||||||
try {
|
|
||||||
s.beginTransaction();
|
|
||||||
String location = (String) session.createQuery(
|
String location = (String) session.createQuery(
|
||||||
"select substr(e.city, 0, 4) " +
|
"select substr(e.city, 0, 4) " +
|
||||||
"from Event e " +
|
"from Event e " +
|
||||||
|
@ -95,20 +77,13 @@ public class InformixFunctionTest extends BaseCoreFunctionalTestCase {
|
||||||
.setParameter( "id", event.id )
|
.setParameter( "id", event.id )
|
||||||
.getSingleResult();
|
.getSingleResult();
|
||||||
assertEquals( "Cluj", location);
|
assertEquals( "Cluj", location);
|
||||||
|
} );
|
||||||
s.getTransaction().commit();
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
s.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestForIssue( jiraKey = "HHH-10846" )
|
@TestForIssue( jiraKey = "HHH-10846" )
|
||||||
public void testCoalesceAndNvl() throws Exception {
|
public void testCoalesceAndNvl() throws Exception {
|
||||||
Session s = openSession();
|
doInHibernate( this::sessionFactory, session -> {
|
||||||
try {
|
|
||||||
s.beginTransaction();
|
|
||||||
String location = (String) session.createQuery(
|
String location = (String) session.createQuery(
|
||||||
"select coalesce(e.district, 'N/A') " +
|
"select coalesce(e.district, 'N/A') " +
|
||||||
"from Event e " +
|
"from Event e " +
|
||||||
|
@ -124,12 +99,72 @@ public class InformixFunctionTest extends BaseCoreFunctionalTestCase {
|
||||||
.setParameter( "id", event.id )
|
.setParameter( "id", event.id )
|
||||||
.getSingleResult();
|
.getSingleResult();
|
||||||
assertEquals( "N/A", location);
|
assertEquals( "N/A", location);
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
s.getTransaction().commit();
|
@Test
|
||||||
|
@TestForIssue( jiraKey = "HHH-10800" )
|
||||||
|
public void testCurrentDate() throws Exception {
|
||||||
|
doInHibernate( this::sessionFactory, session -> {
|
||||||
|
Date date = (Date) session.createQuery(
|
||||||
|
"select current_date() " +
|
||||||
|
"from Event e " +
|
||||||
|
"where e.id = :id")
|
||||||
|
.setParameter( "id", event.id )
|
||||||
|
.getSingleResult();
|
||||||
|
|
||||||
|
assertNotNull( date );
|
||||||
|
assertTrue( date.getTime() > 0 );
|
||||||
|
|
||||||
|
Calendar resultCalendar = Calendar.getInstance();
|
||||||
|
resultCalendar.setTime(date);
|
||||||
|
|
||||||
|
assertEquals( 0, todayCalendar().compareTo(resultCalendar) );
|
||||||
|
} );
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
s.close();
|
@Test
|
||||||
|
@TestForIssue( jiraKey = "HHH-10800" )
|
||||||
|
public void testCurrentTimestamp() throws Exception {
|
||||||
|
doInHibernate( this::sessionFactory, session -> {
|
||||||
|
int tries = 2;
|
||||||
|
while ( tries-- > 0 ) {
|
||||||
|
Timestamp timestamp = (Timestamp) session.createQuery(
|
||||||
|
"select current_timestamp() " +
|
||||||
|
"from Event e " +
|
||||||
|
"where e.id = :id" )
|
||||||
|
.setParameter( "id", event.id )
|
||||||
|
.getSingleResult();
|
||||||
|
|
||||||
|
assertNotNull( timestamp );
|
||||||
|
assertTrue( timestamp != null && timestamp.getTime() > 0 );
|
||||||
|
|
||||||
|
Calendar resultCalendar = Calendar.getInstance();
|
||||||
|
resultCalendar.setTime( timestamp );
|
||||||
|
|
||||||
|
long millis = resultCalendar.getTime().getTime() - todayCalendar().getTime().getTime();
|
||||||
|
|
||||||
|
if(millis == 0) {
|
||||||
|
//What are the odds that ou've run this test exactly at midnight?
|
||||||
|
try {
|
||||||
|
Thread.sleep( 1000 );
|
||||||
}
|
}
|
||||||
|
catch ( InterruptedException ignore ) {}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue( millis > 0 );
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
|
private Calendar todayCalendar() {
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
||||||
|
calendar.set(Calendar.MINUTE, 0);
|
||||||
|
calendar.set(Calendar.SECOND, 0);
|
||||||
|
calendar.set(Calendar.MILLISECOND, 0);
|
||||||
|
return calendar;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue