Author: gbadner
Date: 2010-08-19 19:34:26 -0400 (Thu, 19 Aug 2010)
New Revision: 20198
Modified:
core/branches/Branch_3_5/core/src/main/java/org/hibernate/sql/Template.java
core/branches/Branch_3_5/core/src/main/java/org/hibernate/sql/ordering/antlr/OrderByFragmentParser.java
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/hql/FunctionNameAsColumnTest.java
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/hql/FunctionNamesAsColumns.hbm.xml
Log:
HHH-5225 : Cannot parse order-by fragment if it contains a registered function without
parentheses
Modified: core/branches/Branch_3_5/core/src/main/java/org/hibernate/sql/Template.java
===================================================================
--- core/branches/Branch_3_5/core/src/main/java/org/hibernate/sql/Template.java 2010-08-19
23:06:32 UTC (rev 20197)
+++ core/branches/Branch_3_5/core/src/main/java/org/hibernate/sql/Template.java 2010-08-19
23:34:26 UTC (rev 20198)
@@ -334,11 +334,9 @@
// lcToken does not refer to a function
return false;
}
- // if function.hasArguments() and function.hasParenthesesIfNoArguments() is true,
- // then assume that lcToken is not a function, since it is not followed by
"(";
- // can't seem to use function.hasParenthesesIfNoArguments() alone because
- // function definitions may return true if "()" is optional when there are no
arguments.
- return function.hasArguments() && function.hasParenthesesIfNoArguments() ?
false : true;
+ // if function.hasParenthesesIfNoArguments() is true, then assume
+ // lcToken is not a function (since it is not followed by '(')
+ return ! function.hasParenthesesIfNoArguments();
}
private static boolean isIdentifier(String token, Dialect dialect) {
Modified:
core/branches/Branch_3_5/core/src/main/java/org/hibernate/sql/ordering/antlr/OrderByFragmentParser.java
===================================================================
---
core/branches/Branch_3_5/core/src/main/java/org/hibernate/sql/ordering/antlr/OrderByFragmentParser.java 2010-08-19
23:06:32 UTC (rev 20197)
+++
core/branches/Branch_3_5/core/src/main/java/org/hibernate/sql/ordering/antlr/OrderByFragmentParser.java 2010-08-19
23:34:26 UTC (rev 20198)
@@ -103,7 +103,20 @@
* {@inheritDoc}
*/
protected boolean isFunctionName(AST ast) {
- return context.getSqlFunctionRegistry().hasFunction( ast.getText() );
+ AST child = ast.getFirstChild();
+ // assume it is a function if it has parameters
+ if ( child != null && "{param list}".equals( child.getText() ) ) {
+ return true;
+ }
+
+ final SQLFunction function = context.getSqlFunctionRegistry().findSQLFunction(
ast.getText() );
+ if ( function == null ) {
+ return false;
+ }
+
+ // if function.hasParenthesesIfNoArguments() is true, then assume
+ // ast.getText() is not a function.
+ return ! function.hasParenthesesIfNoArguments();
}
/**
@@ -111,8 +124,10 @@
*/
protected AST resolveFunction(AST ast) {
AST child = ast.getFirstChild();
- assert "{param list}".equals( child.getText() );
- child = child.getFirstChild();
+ if ( child != null ) {
+ assert "{param list}".equals( child.getText() );
+ child = child.getFirstChild();
+ }
final String functionName = ast.getText();
final SQLFunction function = context.getSqlFunctionRegistry().findSQLFunction(
functionName );
Modified:
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/hql/FunctionNameAsColumnTest.java
===================================================================
---
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/hql/FunctionNameAsColumnTest.java 2010-08-19
23:06:32 UTC (rev 20197)
+++
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/hql/FunctionNameAsColumnTest.java 2010-08-19
23:34:26 UTC (rev 20198)
@@ -37,6 +37,8 @@
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.criterion.Restrictions;
+import org.hibernate.dialect.function.SQLFunction;
+import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.junit.functional.FunctionalTestCase;
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
@@ -196,6 +198,13 @@
}
public void testGetMultiColumnSameNameAsNoArgFunctionHQL() throws Exception {
+ SQLFunction function =
+ ( ( SessionFactoryImplementor ) getSessions()
).getSqlFunctionRegistry().findSQLFunction( "current_date" );
+ if ( function == null || function.hasParenthesesIfNoArguments() ) {
+ reportSkip( "current_date reuires ()", "tests noarg function that does
not require ()" );
+ return;
+ }
+
Session s = openSession();
Transaction t = s.beginTransaction();
EntityWithNoArgFunctionAsColumn e1 = new EntityWithNoArgFunctionAsColumn();
@@ -235,6 +244,13 @@
}
public void testGetMultiColumnSameNameAsNoArgFunctionCriteria() {
+ SQLFunction function =
+ ( ( SessionFactoryImplementor ) getSessions()
).getSqlFunctionRegistry().findSQLFunction( "current_date" );
+ if ( function == null || function.hasParenthesesIfNoArguments() ) {
+ reportSkip( "current_date reuires ()", "tests noarg function that does
not require ()" );
+ return;
+ }
+
Session s = openSession();
Transaction t = s.beginTransaction();
EntityWithNoArgFunctionAsColumn e1 = new EntityWithNoArgFunctionAsColumn();
@@ -274,6 +290,13 @@
}
public void testNoArgFcnAndColumnSameNameAsNoArgFunctionHQL() {
+ SQLFunction function =
+ ( ( SessionFactoryImplementor ) getSessions()
).getSqlFunctionRegistry().findSQLFunction( "current_date" );
+ if ( function == null || function.hasParenthesesIfNoArguments() ) {
+ reportSkip( "current_date reuires ()", "tests noarg function that does
not require ()" );
+ return;
+ }
+
Session s = openSession();
Transaction t = s.beginTransaction();
EntityWithNoArgFunctionAsColumn e1 = new EntityWithNoArgFunctionAsColumn();
Modified:
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/hql/FunctionNamesAsColumns.hbm.xml
===================================================================
---
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/hql/FunctionNamesAsColumns.hbm.xml 2010-08-19
23:06:32 UTC (rev 20197)
+++
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/hql/FunctionNamesAsColumns.hbm.xml 2010-08-19
23:34:26 UTC (rev 20198)
@@ -9,11 +9,13 @@
<generator class="increment"/>
</id>
<many-to-one name="nextHolder" cascade="all"/>
- <set name="entityWithArgFunctionAsColumns" inverse="false"
lazy="true" cascade="all-delete-orphan">
+ <set name="entityWithArgFunctionAsColumns" inverse="false"
lazy="true" cascade="all-delete-orphan"
+ order-by="lower,lower( upper )">
<key column="HOLDER_ID"/>
<one-to-many class="EntityWithArgFunctionAsColumn"/>
</set>
- <set name="entityWithNoArgFunctionAsColumns"
inverse="false" lazy="true" cascade="all-delete-orphan">
+ <set name="entityWithNoArgFunctionAsColumns"
inverse="false" lazy="true" cascade="all-delete-orphan"
+ order-by="current_date, `current_date`">
<key column="HOLDER_ID"/>
<one-to-many class="EntityWithNoArgFunctionAsColumn"/>
</set>
@@ -31,7 +33,7 @@
<id name="id" column="ID" type="long">
<generator class="increment"/>
</id>
- <property name="currentDate" column="`current_date`"
type="string"/>
+ <property name="currentDate" column="`current_date`"
type="string"/>
</class>
</hibernate-mapping>
\ No newline at end of file
Show replies by date