Fixes HHH-10800 - InformixDialect: add support for current_timestamp() and current_date()
(cherry picked from commit 5e82c2e414
)
Conflicts:
hibernate-core/src/main/java/org/hibernate/dialect/InformixDialect.java
hibernate-core/src/test/java/org/hibernate/dialect/InformixDialectTestCase.java
hibernate-core/src/test/java/org/hibernate/engine/query/InformixFunctionTest.java
This commit is contained in:
parent
ddad1b6c70
commit
a0b70ffece
|
@ -6,6 +6,9 @@
|
|||
*/
|
||||
package org.hibernate.dialect;
|
||||
|
||||
import org.hibernate.dialect.function.NoArgSQLFunction;
|
||||
import org.hibernate.dialect.function.NvlFunction;
|
||||
import org.hibernate.dialect.function.SQLFunctionTemplate;
|
||||
import org.hibernate.dialect.function.VarArgsSQLFunction;
|
||||
import org.hibernate.dialect.identity.IdentityColumnSupport;
|
||||
import org.hibernate.dialect.identity.InformixIdentityColumnSupport;
|
||||
|
@ -72,7 +75,13 @@ public class InformixDialect extends Dialect {
|
|||
registerColumnType( Types.VARCHAR, 32739, "lvarchar($l)" );
|
||||
|
||||
registerFunction( "concat", new VarArgsSQLFunction( StandardBasicTypes.STRING, "(", "||", ")" ) );
|
||||
|
||||
registerFunction( "substring", new SQLFunctionTemplate(StandardBasicTypes.STRING, "substring(?1 FROM ?2 FOR ?3)"));
|
||||
registerFunction( "substr", new SQLFunctionTemplate( StandardBasicTypes.STRING, "substr(?1, ?2, ?3)"));
|
||||
registerFunction( "coalesce", 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 );
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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.junit4.BaseUnitTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Testing of patched support for Informix boolean type; see HHH-9894, HHH-10800
|
||||
*
|
||||
* @author Greg Jones
|
||||
*/
|
||||
public class InformixDialectTestCase extends BaseUnitTestCase {
|
||||
|
||||
private final InformixDialect dialect = new InformixDialect();
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-9894")
|
||||
public void testToBooleanValueStringTrue() {
|
||||
assertEquals( "'t'", dialect.toBooleanValueString( true ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-9894")
|
||||
public void testToBooleanValueStringFalse() {
|
||||
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 ) );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,207 @@
|
|||
package org.hibernate.engine.query;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.dialect.InformixDialect;
|
||||
|
||||
import org.hibernate.testing.RequiresDialect;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
@RequiresDialect(InformixDialect.class)
|
||||
public class InformixFunctionTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
private Event event;
|
||||
|
||||
@Override
|
||||
protected void prepareTest() throws Exception {
|
||||
Session session = openSession();
|
||||
session.getTransaction().begin();
|
||||
event = new Event();
|
||||
event.country = "Romania";
|
||||
event.city = "Cluj-Napoca";
|
||||
session.persist( event );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-10846" )
|
||||
public void testConcat() throws Exception {
|
||||
Session session = openSession();
|
||||
session.getTransaction().begin();
|
||||
String location = (String) session.createQuery(
|
||||
"select concat(e.country, ' - ', e.city) " +
|
||||
"from Event e " +
|
||||
"where e.id = :id")
|
||||
.setParameter( "id", event.id )
|
||||
.uniqueResult();
|
||||
assertEquals( "Romania - Cluj-Napoca", location );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-10846" )
|
||||
public void testSubstring() throws Exception {
|
||||
Session session = openSession();
|
||||
session.getTransaction().begin();
|
||||
String location = (String) session.createQuery(
|
||||
"select substring(e.city, 0, 5) " +
|
||||
"from Event e " +
|
||||
"where e.id = :id")
|
||||
.setParameter( "id", event.id )
|
||||
.uniqueResult();
|
||||
assertEquals( "Cluj", location);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-10846" )
|
||||
public void testSubstr() throws Exception {
|
||||
Session session = openSession();
|
||||
session.getTransaction().begin();
|
||||
String location = (String) session.createQuery(
|
||||
"select substr(e.city, 0, 4) " +
|
||||
"from Event e " +
|
||||
"where e.id = :id")
|
||||
.setParameter( "id", event.id )
|
||||
.uniqueResult();
|
||||
assertEquals( "Cluj", location);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-10846" )
|
||||
public void testCoalesceAndNvl() throws Exception {
|
||||
Session session = openSession();
|
||||
session.getTransaction().begin();
|
||||
String location = (String) session.createQuery(
|
||||
"select coalesce(e.district, 'N/A') " +
|
||||
"from Event e " +
|
||||
"where e.id = :id")
|
||||
.setParameter( "id", event.id )
|
||||
.uniqueResult();
|
||||
assertEquals( "N/A", location);
|
||||
|
||||
location = (String) session.createQuery(
|
||||
"select nvl(e.district, 'N/A') " +
|
||||
"from Event e " +
|
||||
"where e.id = :id")
|
||||
.setParameter( "id", event.id )
|
||||
.uniqueResult();
|
||||
assertEquals( "N/A", location);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-10800" )
|
||||
public void testCurrentDate() throws Exception {
|
||||
Session session = openSession();
|
||||
session.getTransaction().begin();
|
||||
Date date = (Date) session.createQuery(
|
||||
"select current_date() " +
|
||||
"from Event e " +
|
||||
"where e.id = :id")
|
||||
.setParameter( "id", event.id )
|
||||
.uniqueResult();
|
||||
|
||||
assertNotNull( date );
|
||||
assertTrue( date.getTime() > 0 );
|
||||
|
||||
Calendar resultCalendar = Calendar.getInstance();
|
||||
resultCalendar.setTime(date);
|
||||
|
||||
assertEquals( 0, todayCalendar().compareTo(resultCalendar) );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-10800" )
|
||||
public void testCurrentTimestamp() throws Exception {
|
||||
Session session = openSession();
|
||||
session.getTransaction().begin();
|
||||
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 )
|
||||
.uniqueResult();
|
||||
|
||||
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 );
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
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
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Event.class
|
||||
};
|
||||
}
|
||||
|
||||
@Entity(name = "Event")
|
||||
public static class Event {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@Column
|
||||
private String country;
|
||||
|
||||
private String city;
|
||||
|
||||
private String district;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue