From db259df4576c4e950445b92ccf0828070b31ed85 Mon Sep 17 00:00:00 2001 From: Jonathan Bregler Date: Fri, 4 May 2018 14:43:51 +0200 Subject: [PATCH] HHH-12546: locate function doesn't work on SAP HANA --- .../dialect/AbstractHANADialect.java | 2 +- .../dialect/function/HANAFunctionsTest.java | 114 ++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/dialect/function/HANAFunctionsTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/AbstractHANADialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/AbstractHANADialect.java index 5a30510095..64ce2e4740 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/AbstractHANADialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/AbstractHANADialect.java @@ -873,7 +873,7 @@ public abstract class AbstractHANADialect extends Dialect { registerFunction( "lcase", new StandardSQLFunction( "lcase", StandardBasicTypes.STRING ) ); registerFunction( "left", new StandardSQLFunction( "left", StandardBasicTypes.STRING ) ); registerFunction( "length", new StandardSQLFunction( "length", StandardBasicTypes.INTEGER ) ); - registerFunction( "locate", new StandardSQLFunction( "locate", StandardBasicTypes.INTEGER ) ); + registerFunction( "locate", new SQLFunctionTemplate( StandardBasicTypes.INTEGER, "locate(?2, ?1, ?3)" ) ); registerFunction( "lpad", new StandardSQLFunction( "lpad", StandardBasicTypes.STRING ) ); registerFunction( "ltrim", new StandardSQLFunction( "ltrim", StandardBasicTypes.STRING ) ); registerFunction( "nchar", new StandardSQLFunction( "nchar", StandardBasicTypes.STRING ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/dialect/function/HANAFunctionsTest.java b/hibernate-core/src/test/java/org/hibernate/test/dialect/function/HANAFunctionsTest.java new file mode 100644 index 0000000000..352eeb313c --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/dialect/function/HANAFunctionsTest.java @@ -0,0 +1,114 @@ +/* + * 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 . + */ +package org.hibernate.test.dialect.function; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.math.BigDecimal; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.dialect.AbstractHANADialect; +import org.hibernate.query.Query; +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +@RequiresDialect(AbstractHANADialect.class) +public class HANAFunctionsTest extends BaseCoreFunctionalTestCase { + + @Override + public String[] getMappings() { + return new String[]{ "dialect/function/Product.hbm.xml" }; + } + + @Override + protected void afterSessionFactoryBuilt() { + Product product = new Product(); + product.setLength( 100 ); + product.setPrice( new BigDecimal( 1.298 ) ); + try ( Session s = openSession() ) { + Transaction tx = s.beginTransaction(); + s.save( product ); + tx.commit(); + } + } + + @Test + @TestForIssue(jiraKey = "HHH-12546") + public void testLocateFunction() { + try ( Session s = openSession() ) { + Transaction tx = s.beginTransaction(); + Query q = s.createQuery( "select p from Product p where locate('.', cast(p.price as string)) > 0", Product.class ); + Product p = q.uniqueResult(); + assertNotNull( p ); + assertEquals( 100, p.getLength() ); + assertEquals( BigDecimal.valueOf( 1.29 ), p.getPrice() ); + tx.commit(); + } + + try ( Session s = openSession() ) { + Transaction tx = s.beginTransaction(); + Query q = s.createQuery( "select p from Product p where locate('.', cast(p.price as string)) = 0", Product.class ); + Product p = q.uniqueResult(); + assertNull( p ); + tx.commit(); + } + + try ( Session s = openSession() ) { + Transaction tx = s.beginTransaction(); + Query q = s.createQuery( "select p from Product p where locate('.', cast(p.price as string), 3) > 0", Product.class ); + Product p = q.uniqueResult(); + assertNull( p ); + tx.commit(); + } + + } + + @Test + public void testSubstringFunction() { + try ( Session s = openSession() ) { + Transaction tx = s.beginTransaction(); + Query q = s.createQuery( "select p from Product p where substring(cast(p.price as string), 1, 2) = '1.'", Product.class ); + Product p = q.uniqueResult(); + assertNotNull( p ); + assertEquals( 100, p.getLength() ); + assertEquals( BigDecimal.valueOf( 1.29 ), p.getPrice() ); + tx.commit(); + } + + try ( Session s = openSession() ) { + Transaction tx = s.beginTransaction(); + Query q = s.createQuery( "select p from Product p where substring(cast(p.price as string), 1, 2) = '.1'", Product.class ); + Product p = q.uniqueResult(); + assertNull( p ); + tx.commit(); + } + + try ( Session s = openSession() ) { + Transaction tx = s.beginTransaction(); + Query q = s.createQuery( "select p from Product p where substring(cast(p.price as string), 1) = '1.29'", Product.class ); + Product p = q.uniqueResult(); + assertNotNull( p ); + assertEquals( 100, p.getLength() ); + assertEquals( BigDecimal.valueOf( 1.29 ), p.getPrice() ); + tx.commit(); + } + + try ( Session s = openSession() ) { + Transaction tx = s.beginTransaction(); + Query q = s.createQuery( "select p from Product p where substring(cast(p.price as string), 1) = '1.'", Product.class ); + Product p = q.uniqueResult(); + assertNull( p ); + tx.commit(); + } + } + +}